6 /* This util converts bitstream to more friendly format
7 * for select map interface with x16 line.
11 #if __BYTE_ORDER == __LITTLE_ENDIAN
13 #elif __BYTE_ORDER == __BIG_ENDIAN
16 #error Invalid endianness.
19 uint32_t swab32(uint32_t x)
21 return x<<24 | x>>24 |
22 (x & (uint32_t)0x0000ff00UL)<<8 |
23 (x & (uint32_t)0x00ff0000UL)>>8;
26 uint8_t fpga_swap(uint8_t x)
28 return ((x & (uint8_t)0x01) << 7) | ((x & (uint8_t)0x02) << 5) |
29 ((x & (uint8_t)0x04) << 3) | ((x & (uint8_t)0x08) << 1) |
30 ((x & (uint8_t)0x10) >> 1) | ((x & (uint8_t)0x20) >> 3) |
31 ((x & (uint8_t)0x40) >> 5) | ((x & (uint8_t)0x80) >> 7);
34 void __attribute__((noreturn)) error_usage(const char* argv0)
36 printf("Usage: %s le|be <bin> <pkg>\n", argv0);
40 void __attribute__((noreturn)) error_reason(const char* argv0, const char* reason)
42 printf("ERROR: %s\n", reason);
43 printf("Usage: %s le|be <bin> <pkg>\n", argv0);
47 int main(int argc, char** argv)
58 if (!strcmp(argv[1], "le"))
60 else if (!strcmp(argv[1], "be"))
65 fin = fopen(argv[2], "r");
67 error_reason(argv[0], "Failed to open input file.");
69 fseek(fin, 0L, SEEK_END);
71 fseek(fin, 0L, SEEK_SET);
74 error_reason(argv[0], "Invalid size (not aligned).");
76 fout = fopen(argv[3], "w+");
78 error_reason(argv[0], "Failed to open output file.");
80 /* If we're switching endianness, we need to swap the bytes */
81 if (target_be != source_be)
91 fwrite(&magic, 1, 4, fout);
94 fwrite(&szs, 1, sizeof(uint32_t), fout);
96 /* Binary must be aligned */
97 for (i = 0; i < sz / 2; i++)
99 /* Bitstream is big endian */
100 fread(&hb, 1, 1, fin);
101 fread(&lb, 1, 1, fin);
110 fwrite(&hb, 1, 1, fout);
111 fwrite(&lb, 1, 1, fout);
115 fwrite(&lb, 1, 1, fout);
116 fwrite(&hb, 1, 1, fout);