1 //-----------------------------------------------------------------------------
2 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
3 // at your option, any later version. See the LICENSE.txt file for the text of
5 //-----------------------------------------------------------------------------
6 // Compression tool for FPGA config files. Compress several *.bit files at
7 // compile time. Decompression is done at run time (see fpgaloader.c).
8 // This uses the zlib library tuned to this specific case. The small file sizes
9 // allow to use "insane" parameters for optimum compression ratio.
10 //-----------------------------------------------------------------------------
19 #define MAX(a,b) ((a)>(b)?(a):(b))
22 #define COMPRESS_LEVEL 9 // use best possible compression
23 #define COMPRESS_WINDOW_BITS 15 // default = max = 15 for a window of 2^15 = 32KBytes
24 #define COMPRESS_MEM_LEVEL 9 // determines the amount of memory allocated during compression. Default = 8.
25 /* COMPRESS_STRATEGY can be
26 Z_DEFAULT_STRATEGY (the default),
27 Z_FILTERED (more huffmann, less string matching),
28 Z_HUFFMAN_ONLY (huffman only, no string matching)
29 Z_RLE (distances limited to one)
30 Z_FIXED (prevents the use of dynamic Huffman codes)
32 #define COMPRESS_STRATEGY Z_DEFAULT_STRATEGY
33 // zlib tuning parameters:
34 #define COMPRESS_GOOD_LENGTH 258
35 #define COMPRESS_MAX_LAZY 258
36 #define COMPRESS_MAX_NICE_LENGTH 258
37 #define COMPRESS_MAX_CHAIN 8192
39 #define FPGA_INTERLEAVE_SIZE 288 // (the FPGA's internal config frame size is 288 bits. Interleaving with 288 bytes should give best compression)
40 #define FPGA_CONFIG_SIZE 42336 // our current fpga_[lh]f.bit files are 42175 bytes. Rounded up to next multiple of FPGA_INTERLEAVE_SIZE
42 static void usage(void)
44 fprintf(stderr
, "Usage: fpga_compress <infile1> <infile2> ... <infile_n> <outfile>\n");
45 fprintf(stderr
, " Combine n FPGA bitstream files and compress them into one.\n\n");
46 fprintf(stderr
, " fpga_compress -d <infile> <outfile>");
47 fprintf(stderr
, " Decompress <infile>. Write result to <outfile>");
51 static voidpf
fpga_deflate_malloc(voidpf opaque
, uInt items
, uInt size
)
53 return malloc(items
*size
);
57 static void fpga_deflate_free(voidpf opaque
, voidpf address
)
63 static bool all_feof(FILE *infile
[], uint8_t num_infiles
)
65 for (uint16_t i
= 0; i
< num_infiles
; i
++) {
66 if (!feof(infile
[i
])) {
75 int zlib_compress(FILE *infile
[], uint8_t num_infiles
, FILE *outfile
)
81 z_stream compressed_fpga_stream
;
83 fpga_config
= malloc(num_infiles
* FPGA_CONFIG_SIZE
);
85 // read the input files. Interleave them into fpga_config[]
88 for(uint16_t j
= 0; j
< num_infiles
; j
++) {
89 for(uint16_t k
= 0; k
< FPGA_INTERLEAVE_SIZE
; k
++) {
91 if (!feof(infile
[j
])) {
93 } else if (num_infiles
> 1) {
94 fpga_config
[i
++] = '\0';
99 if (i
> num_infiles
* FPGA_CONFIG_SIZE
) {
100 fprintf(stderr
, "Input files too big (total of %ld > %d bytes). These are probably not PM3 FPGA config files.", i
, num_infiles
*FPGA_CONFIG_SIZE
);
101 for(uint16_t j
= 0; j
< num_infiles
; j
++) {
106 } while (!all_feof(infile
, num_infiles
));
108 // initialize zlib structures
109 compressed_fpga_stream
.next_in
= fpga_config
;
110 compressed_fpga_stream
.avail_in
= i
;
111 compressed_fpga_stream
.zalloc
= fpga_deflate_malloc
;
112 compressed_fpga_stream
.zfree
= fpga_deflate_free
;
114 ret
= deflateInit2(&compressed_fpga_stream
,
117 COMPRESS_WINDOW_BITS
,
121 // estimate the size of the compressed output
122 unsigned int outsize_max
= deflateBound(&compressed_fpga_stream
, compressed_fpga_stream
.avail_in
);
123 uint8_t *outbuf
= malloc(outsize_max
);
124 compressed_fpga_stream
.next_out
= outbuf
;
125 compressed_fpga_stream
.avail_out
= outsize_max
;
128 ret
= deflateTune(&compressed_fpga_stream
,
129 COMPRESS_GOOD_LENGTH
,
131 COMPRESS_MAX_NICE_LENGTH
,
136 ret
= deflate(&compressed_fpga_stream
, Z_FINISH
);
139 fprintf(stderr
, "compressed %d input bytes to %d output bytes\n", i
, compressed_fpga_stream
.total_out
);
141 if (ret
!= Z_STREAM_END
) {
142 fprintf(stderr
, "Error in deflate(): %d %s\n", ret
, compressed_fpga_stream
.msg
);
144 deflateEnd(&compressed_fpga_stream
);
145 for(uint16_t j
= 0; j
< num_infiles
; j
++) {
154 for (i
= 0; i
< compressed_fpga_stream
.total_out
; i
++) {
155 fputc(outbuf
[i
], outfile
);
159 deflateEnd(&compressed_fpga_stream
);
160 for(uint16_t j
= 0; j
< num_infiles
; j
++) {
172 int zlib_decompress(FILE *infile
, FILE *outfile
)
174 #define DECOMPRESS_BUF_SIZE 1024
175 uint8_t outbuf
[DECOMPRESS_BUF_SIZE
];
176 uint8_t inbuf
[DECOMPRESS_BUF_SIZE
];
179 z_stream compressed_fpga_stream
;
181 // initialize zlib structures
182 compressed_fpga_stream
.next_in
= inbuf
;
183 compressed_fpga_stream
.avail_in
= 0;
184 compressed_fpga_stream
.next_out
= outbuf
;
185 compressed_fpga_stream
.avail_out
= DECOMPRESS_BUF_SIZE
;
186 compressed_fpga_stream
.zalloc
= fpga_deflate_malloc
;
187 compressed_fpga_stream
.zfree
= fpga_deflate_free
;
189 ret
= inflateInit2(&compressed_fpga_stream
, 0);
192 if (compressed_fpga_stream
.avail_in
== 0) {
193 compressed_fpga_stream
.next_in
= inbuf
;
196 uint8_t c
= fgetc(infile
);
199 compressed_fpga_stream
.avail_in
++;
203 } while (i
< DECOMPRESS_BUF_SIZE
);
206 ret
= inflate(&compressed_fpga_stream
, Z_SYNC_FLUSH
);
208 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
212 if (compressed_fpga_stream
.avail_out
== 0) {
213 for (uint16_t i
= 0; i
< DECOMPRESS_BUF_SIZE
; i
++) {
214 fputc(outbuf
[i
], outfile
);
216 compressed_fpga_stream
.avail_out
= DECOMPRESS_BUF_SIZE
;
217 compressed_fpga_stream
.next_out
= outbuf
;
219 } while (ret
== Z_OK
);
221 if (ret
== Z_STREAM_END
) { // reached end of input
223 while (compressed_fpga_stream
.avail_out
< DECOMPRESS_BUF_SIZE
) {
224 fputc(outbuf
[i
++], outfile
);
225 compressed_fpga_stream
.avail_out
++;
231 fprintf(stderr
, "Error. Inflate() returned error %d, %s", ret
, compressed_fpga_stream
.msg
);
240 int main(int argc
, char **argv
)
245 if (argc
== 1 || argc
== 2) {
250 if (!strcmp(argv
[1], "-d")) { // Decompress
251 infiles
= calloc(1, sizeof(FILE*));
256 infiles
[0] = fopen(argv
[2], "rb");
257 if (infiles
[0] == NULL
) {
258 fprintf(stderr
, "Error. Cannot open input file %s", argv
[2]);
261 outfile
= fopen(argv
[3], "wb");
262 if (outfile
== NULL
) {
263 fprintf(stderr
, "Error. Cannot open output file %s", argv
[3]);
266 return zlib_decompress(infiles
[0], outfile
);
270 infiles
= calloc(argc
-2, sizeof(FILE*));
271 for (uint16_t i
= 0; i
< argc
-2; i
++) {
272 infiles
[i
] = fopen(argv
[i
+1], "rb");
273 if (infiles
[i
] == NULL
) {
274 fprintf(stderr
, "Error. Cannot open input file %s", argv
[i
+1]);
278 outfile
= fopen(argv
[argc
-1], "wb");
279 if (outfile
== NULL
) {
280 fprintf(stderr
, "Error. Cannot open output file %s", argv
[argc
-1]);
283 return zlib_compress(infiles
, argc
-2, outfile
);