source: Hardware/WARP_v3/Rev1.1/Config_CPLD/src/spi_boot_OpenCores_src/sw/misc/bit_reverse.c

Last change on this file was 1799, checked in by murphpo, 12 years ago

Adding WARP v3 hardware files (schematics, FPGA pinout, configuration CPLD source)

File size: 1.5 KB
Line 
1// Altera requires configuration bytes to be sent LSB first but the
2// SD Card reads bytes MSB first
3// This code reverses the bits of the altera bitstream so
4// it will come out correct when read from the SD card
5// $Log: bit_reverse.c,v $
6// Revision 1.1  2006/01/06 14:44:17  mbl
7// initial version
8//
9
10
11
12#include "stdio.h"
13#include "string.h"
14
15FILE*   fileOut;
16FILE*   fileIn;
17
18void    outIOerror(char* pfn);
19void    inIOerror(char* pfn);
20
21int main(int argc, char* arg[])
22{
23    unsigned char input, output;
24    unsigned char in_mask, out_mask;
25    int i;
26
27    fileOut = fopen(arg[2],"wb");
28    if (fileOut == NULL)
29    {
30        outIOerror(arg[2]);
31        exit(-1);
32    }
33
34    printf("Opening input file %s\n", arg[1]);
35        fileIn = fopen(arg[1],"rb");
36        if (fileIn == NULL)
37        {
38            inIOerror(arg[1]);
39            exit(-1);
40        }
41
42        while (!feof(fileIn) && fgets((char*)&input, 2 ,fileIn) != NULL)
43        {
44            in_mask = 1;
45            out_mask = 0x80;
46        output = 0;
47
48            for ( i=0; i < 8; ++i )
49            {
50            if (input & in_mask) 
51            {
52                output |= out_mask;
53            }
54            out_mask = out_mask >> 1;
55            in_mask = in_mask << 1;
56            }
57        fwrite((void*)&output,sizeof(char),1,fileOut);
58    }
59
60    fclose(fileIn);
61    fclose(fileOut);
62    printf("\n%s has been created\n", arg[2]);
63    exit(0);
64}
65
66void outIOerror(char *pfn)
67{
68    printf("I/O Error while writing to file=%s\n",pfn);
69}
70
71void inIOerror(char *pfn)
72{
73    printf("I/O Error while reading file=%s\n",pfn);
74}
Note: See TracBrowser for help on using the repository browser.