]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-dad.git/blob - hw/packager.c
Implemented multiple samples per pixel and times tuning in the test software.
[fpga/lx-cpu1/lx-dad.git] / hw / packager.c
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <endian.h>
5
6 /* This util converts bitstream to more friendly format
7  * for select map interface with x16 line.
8  */
9
10 /* Endianness */
11 #if __BYTE_ORDER == __LITTLE_ENDIAN
12 int source_be = 0;
13 #elif __BYTE_ORDER == __BIG_ENDIAN
14 int source_be = 1;
15 #else
16 #error Invalid endianness.
17 #endif
18
19 uint32_t swab32(uint32_t x)
20 {
21         return x<<24 | x>>24 |
22                 (x & (uint32_t)0x0000ff00UL)<<8 |
23                 (x & (uint32_t)0x00ff0000UL)>>8;
24 }
25
26 uint8_t fpga_swap(uint8_t x)
27 {
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);
32 }
33
34 void __attribute__((noreturn)) error_usage(const char* argv0)
35 {
36         printf("Usage: %s le|be <bin> <pkg>\n", argv0);
37         exit(1);
38 }
39
40 void __attribute__((noreturn)) error_reason(const char* argv0, const char* reason)
41 {
42         printf("ERROR: %s\n", reason);
43         printf("Usage: %s le|be <bin> <pkg>\n", argv0);
44         exit(1);
45 }
46
47 int main(int argc, char** argv)
48 {
49         int target_be, i;
50         uint32_t sz, szs;
51         uint8_t hb, lb;
52         char magic[4];
53         FILE *fin, *fout;
54
55         if (argc != 4)
56                 error_usage(argv[0]);
57
58         if (!strcmp(argv[1], "le"))
59                 target_be = 0;
60         else if (!strcmp(argv[1], "be"))
61                 target_be = 1;
62         else
63                 error_usage(argv[0]);
64
65         fin = fopen(argv[2], "r");
66         if (!fin)
67                 error_reason(argv[0], "Failed to open input file.");
68
69         fseek(fin, 0L, SEEK_END);
70         sz = ftell(fin);
71         fseek(fin, 0L, SEEK_SET);
72
73         if (sz & 0x01)
74                 error_reason(argv[0], "Invalid size (not aligned).");
75
76         fout = fopen(argv[3], "w+");
77         if (!fout)
78                 error_reason(argv[0], "Failed to open output file.");
79
80         /* If we're switching endianness, we need to swap the bytes */
81         if (target_be != source_be)
82                 szs = swab32(sz);
83         else
84                 szs = sz;
85
86         /* Write magic */
87         magic[0] = 'F';
88         magic[1] = 'P';
89         magic[2] = 'G';
90         magic[3] = 'A';
91         fwrite(&magic, 1, 4, fout);
92
93         /* Write size */
94         fwrite(&szs, 1, sizeof(uint32_t), fout);
95
96         /* Binary must be aligned */
97         for (i = 0; i < sz / 2; i++)
98         {
99                 /* Bitstream is big endian */
100                 fread(&hb, 1, 1, fin);
101                 fread(&lb, 1, 1, fin);
102
103                 /* Swap the bites */
104                 hb = fpga_swap(hb);
105                 lb = fpga_swap(lb);
106
107                 /* Write it */
108                 if (target_be)
109                 {
110                         fwrite(&hb, 1, 1, fout);
111                         fwrite(&lb, 1, 1, fout);
112                 }
113                 else
114                 {
115                         fwrite(&lb, 1, 1, fout);
116                         fwrite(&hb, 1, 1, fout);
117                 }
118         }
119
120         fclose(fout);
121         return 0;
122 }