static int downloaded_bitstream = FPGA_BITSTREAM_ERR;
// this is where the bitstreams are located in memory:
-extern uint8_t _binary_fpga_lf_bit_start, _binary_fpga_lf_bit_end;
-extern uint8_t _binary_fpga_hf_bit_start, _binary_fpga_hf_bit_end;
+extern uint8_t _binary_obj_fpga_all_bit_z_start, _binary_obj_fpga_all_bit_z_end;
+
static uint8_t *fpga_image_ptr = NULL;
+static uint32_t uncompressed_bytes_cnt;
static const uint8_t _bitparse_fixed_header[] = {0x00, 0x09, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x0f, 0xf0, 0x00, 0x00, 0x01};
#define FPGA_BITSTREAM_FIXED_HEADER_SIZE sizeof(_bitparse_fixed_header)
-#define OUTPUT_BUFFER_LEN 80
+#define OUTPUT_BUFFER_LEN 80
+#define FPGA_INTERLEAVE_SIZE 288
//-----------------------------------------------------------------------------
// Set up the Serial Peripheral Interface as master
}
-static int get_from_fpga_stream(z_streamp compressed_fpga_stream, uint8_t *output_buffer)
+static int get_from_fpga_combined_stream(z_streamp compressed_fpga_stream, uint8_t *output_buffer)
{
if (fpga_image_ptr == compressed_fpga_stream->next_out) { // need more data
compressed_fpga_stream->next_out = output_buffer;
}
}
+ uncompressed_bytes_cnt++;
+
return *fpga_image_ptr++;
}
+static int get_from_fpga_stream(int bitstream_version, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
+{
+ while((uncompressed_bytes_cnt / FPGA_INTERLEAVE_SIZE) % FPGA_BITSTREAM_MAX != (bitstream_version - 1)) {
+ // skip undesired data belonging to other bitstream_versions
+ get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
+ }
+
+ return get_from_fpga_combined_stream(compressed_fpga_stream, output_buffer);
+
+}
+
+
static voidpf fpga_inflate_malloc(voidpf opaque, uInt items, uInt size)
{
Dbprintf("zlib requested %d bytes", items*size);
static bool reset_fpga_stream(int bitstream_version, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
{
uint8_t header[FPGA_BITSTREAM_FIXED_HEADER_SIZE];
- uint8_t *fpga_image_start;
- uint32_t fpga_image_size;
- if (bitstream_version == FPGA_BITSTREAM_LF) {
- fpga_image_start = &_binary_fpga_lf_bit_start;
- fpga_image_size = (uint32_t)&_binary_fpga_lf_bit_end - (uint32_t)&_binary_fpga_lf_bit_start;
- } else if (bitstream_version == FPGA_BITSTREAM_HF) {
- fpga_image_start = &_binary_fpga_hf_bit_start;
- fpga_image_size = (uint32_t)&_binary_fpga_hf_bit_end - (uint32_t)&_binary_fpga_hf_bit_start;
- } else {
- return false;
- }
-
+ uncompressed_bytes_cnt = 0;
+
// initialize z_stream structure for inflate:
- compressed_fpga_stream->next_in = fpga_image_start;
- compressed_fpga_stream->avail_in = fpga_image_size;
+ compressed_fpga_stream->next_in = &_binary_obj_fpga_all_bit_z_start;
+ compressed_fpga_stream->avail_in = &_binary_obj_fpga_all_bit_z_start - &_binary_obj_fpga_all_bit_z_end;
compressed_fpga_stream->next_out = output_buffer;
compressed_fpga_stream->avail_out = OUTPUT_BUFFER_LEN;
compressed_fpga_stream->zalloc = &fpga_inflate_malloc;
fpga_image_ptr = output_buffer;
for (uint16_t i = 0; i < FPGA_BITSTREAM_FIXED_HEADER_SIZE; i++) {
- header[i] = get_from_fpga_stream(compressed_fpga_stream, output_buffer);
+ header[i] = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
}
// Check for a valid .bit file (starts with _bitparse_fixed_header)
}
// Download the fpga image starting at current stream position with length FpgaImageLen bytes
-static void DownloadFPGA(int FpgaImageLen, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
+static void DownloadFPGA(int bitstream_version, int FpgaImageLen, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
{
Dbprintf("DownloadFPGA(len: %d)", FpgaImageLen);
}
for(i = 0; i < FpgaImageLen; i++) {
- int b = get_from_fpga_stream(compressed_fpga_stream, output_buffer);
+ int b = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
if (b < 0) {
Dbprintf("Error %d during FpgaDownload", b);
break;
* (big endian), <length> bytes content. Except for section 'e' which has 4 bytes
* length.
*/
-static int bitparse_find_section(char section_name, unsigned int *section_length, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
+static int bitparse_find_section(int bitstream_version, char section_name, unsigned int *section_length, z_streamp compressed_fpga_stream, uint8_t *output_buffer)
{
int result = 0;
#define MAX_FPGA_BIT_STREAM_HEADER_SEARCH 100 // maximum number of bytes to search for the requested section
uint16_t numbytes = 0;
while(numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH) {
- char current_name = get_from_fpga_stream(compressed_fpga_stream, output_buffer);
+ char current_name = get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
numbytes++;
unsigned int current_length = 0;
if(current_name < 'a' || current_name > 'e') {
switch(current_name) {
case 'e':
/* Four byte length field */
- current_length += get_from_fpga_stream(compressed_fpga_stream, output_buffer) << 24;
- current_length += get_from_fpga_stream(compressed_fpga_stream, output_buffer) << 16;
+ current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 24;
+ current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 16;
numbytes += 2;
default: /* Fall through, two byte length field */
- current_length += get_from_fpga_stream(compressed_fpga_stream, output_buffer) << 8;
- current_length += get_from_fpga_stream(compressed_fpga_stream, output_buffer) << 0;
+ current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 8;
+ current_length += get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer) << 0;
numbytes += 2;
}
}
for (uint16_t i = 0; i < current_length && numbytes < MAX_FPGA_BIT_STREAM_HEADER_SEARCH; i++) {
- get_from_fpga_stream(compressed_fpga_stream, output_buffer);
+ get_from_fpga_stream(bitstream_version, compressed_fpga_stream, output_buffer);
numbytes++;
}
}
}
unsigned int bitstream_length;
- if(bitparse_find_section('e', &bitstream_length, &compressed_fpga_stream, output_buffer)) {
- DownloadFPGA(bitstream_length, &compressed_fpga_stream, output_buffer);
+ if(bitparse_find_section(bitstream_version, 'e', &bitstream_length, &compressed_fpga_stream, output_buffer)) {
+ DownloadFPGA(bitstream_version, bitstream_length, &compressed_fpga_stream, output_buffer);
downloaded_bitstream = bitstream_version;
}
return;
}
- if(bitparse_find_section('a', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
+ if(bitparse_find_section(bitstream_version, 'a', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
for (uint16_t i = 0; i < fpga_info_len; i++) {
- char c = (char)get_from_fpga_stream(&compressed_fpga_stream, output_buffer);
+ char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
if (i < sizeof(tempstr)) {
tempstr[i] = c;
}
strncat(dst, "HF ", len-1);
}
strncat(dst, "FPGA image built", len-1);
- if(bitparse_find_section('b', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
+ if(bitparse_find_section(bitstream_version, 'b', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
strncat(dst, " for ", len-1);
for (uint16_t i = 0; i < fpga_info_len; i++) {
- char c = (char)get_from_fpga_stream(&compressed_fpga_stream, output_buffer);
+ char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
if (i < sizeof(tempstr)) {
tempstr[i] = c;
}
}
strncat(dst, tempstr, len-1);
}
- if(bitparse_find_section('c', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
+ if(bitparse_find_section(bitstream_version, 'c', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
strncat(dst, " on ", len-1);
for (uint16_t i = 0; i < fpga_info_len; i++) {
- char c = (char)get_from_fpga_stream(&compressed_fpga_stream, output_buffer);
+ char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
if (i < sizeof(tempstr)) {
tempstr[i] = c;
}
}
strncat(dst, tempstr, len-1);
}
- if(bitparse_find_section('d', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
+ if(bitparse_find_section(bitstream_version, 'd', &fpga_info_len, &compressed_fpga_stream, output_buffer)) {
strncat(dst, " at ", len-1);
for (uint16_t i = 0; i < fpga_info_len; i++) {
- char c = (char)get_from_fpga_stream(&compressed_fpga_stream, output_buffer);
+ char c = (char)get_from_fpga_stream(bitstream_version, &compressed_fpga_stream, output_buffer);
if (i < sizeof(tempstr)) {
tempstr[i] = c;
}
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
+#include <stdbool.h>
#include "zlib.h"
#define MAX(a,b) ((a)>(b)?(a):(b))
// zlib configuration
#define COMPRESS_LEVEL 9 // use best possible compression
-
-#define FPGA_CONFIG_SIZE 42175
-static uint8_t fpga_config[FPGA_CONFIG_SIZE];
+#define COMPRESS_WINDOW_BITS 15 // default = 15 for a window of 2^15 = 32KBytes
+#define COMPRESS_MEM_LEVEL 9 // determines the amount of memory allocated during compression. Default = 8. Must be < 9
+/* COMPRESS_STRATEGY can be
+ Z_DEFAULT_STRATEGY (the default),
+ Z_FILTERED (more huffmann, less string matching),
+ Z_HUFFMAN_ONLY (huffman only, no string matching)
+ Z_RLE (distances limited to one)
+ Z_FIXED (prevents the use of dynamic Huffman codes)
+*/
+#define COMPRESS_STRATEGY Z_DEFAULT_STRATEGY
+// zlib tuning parameters:
+#define COMPRESS_GOOD_LENGTH 258
+#define COMPRESS_MAX_LAZY 258
+#define COMPRESS_MAX_NICE_LENGTH 258
+#define COMPRESS_MAX_CHAIN 8192
+
+#define FPGA_INTERLEAVE_SIZE 288 // (the FPGA's internal config frame size is 288 bits. Interleaving with 288 bytes should give best compression)
+#define FPGA_CONFIG_SIZE 42336 // our current fpga_[lh]f.bit files are 42175 bytes. Rounded up to next multiple of FPGA_INTERLEAVE_SIZE
static void usage(char *argv0)
{
- fprintf(stderr, "Usage: %s <infile> <outfile>\n\n", argv0);
+ fprintf(stderr, "Usage: %s <infile1> <infile2> ... <infile_n> <outfile>\n\n", argv0);
+ fprintf(stderr, "Combines n FPGA bitstream files and compresses them into one.\n\n");
}
}
-int zlib_compress(FILE *infile, FILE *outfile)
+static bool all_feof(FILE *infile[], uint8_t num_infiles)
+{
+ for (uint16_t i = 0; i < num_infiles; i++) {
+ if (!feof(infile[i])) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+
+int zlib_compress(FILE *infile[], uint8_t num_infiles, FILE *outfile)
{
- int i, ret;
+ uint8_t *fpga_config;
+ uint32_t i;
+ int ret;
+ uint8_t c;
z_stream compressed_fpga_stream;
+
+ fpga_config = malloc(num_infiles * FPGA_CONFIG_SIZE);
- // read the input file into fpga_config[] and count occurrences of each symbol:
+ // read the input files interleaving into fpga_config[]
i = 0;
- while(!feof(infile)) {
- uint8_t c;
- c = fgetc(infile);
- fpga_config[i++] = c;
- if (i > FPGA_CONFIG_SIZE+1) {
- fprintf(stderr, "Input file too big (> %d bytes). This is probably not a PM3 FPGA config file.", FPGA_CONFIG_SIZE);
- fclose(infile);
- fclose(outfile);
+ do {
+ for(uint16_t j = 0; j < num_infiles; j++) {
+ for(uint16_t k = 0; k < FPGA_INTERLEAVE_SIZE; k++) {
+ c = fgetc(infile[j]);
+ if (!feof(infile[j])) fpga_config[i++] = c; else fpga_config[i++] = '\0';
+ }
+ }
+
+ if (i > num_infiles * FPGA_CONFIG_SIZE) {
+ 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);
+ for(uint16_t j = 0; j < num_infiles; j++) {
+ fclose(infile[j]);
+ }
return -1;
}
- }
+ } while (!all_feof(infile, num_infiles));
+ fprintf(stderr, "Read a total of %ld bytes from %d files\n", i, num_infiles);
+
// initialize zlib structures
compressed_fpga_stream.next_in = fpga_config;
compressed_fpga_stream.avail_in = i;
compressed_fpga_stream.zalloc = fpga_deflate_malloc;
compressed_fpga_stream.zfree = fpga_deflate_free;
+ ret = deflateInit2(&compressed_fpga_stream,
+ COMPRESS_LEVEL,
+ Z_DEFLATED,
+ COMPRESS_WINDOW_BITS,
+ COMPRESS_MEM_LEVEL,
+ COMPRESS_STRATEGY);
+
// estimate the size of the compressed output
unsigned int outsize_max = deflateBound(&compressed_fpga_stream, compressed_fpga_stream.avail_in);
+ fprintf(stderr, "Allocating %ld bytes for output file (estimated upper bound)\n", outsize_max);
uint8_t *outbuf = malloc(outsize_max);
compressed_fpga_stream.next_out = outbuf;
compressed_fpga_stream.avail_out = outsize_max;
- fprintf(stderr, "Allocated %d bytes for output file (estimated upper bound)\n", outsize_max);
- ret = deflateInit(&compressed_fpga_stream, COMPRESS_LEVEL);
-
+
+ if (ret == Z_OK) {
+ ret = deflateTune(&compressed_fpga_stream,
+ COMPRESS_GOOD_LENGTH,
+ COMPRESS_MAX_LAZY,
+ COMPRESS_MAX_NICE_LENGTH,
+ COMPRESS_MAX_CHAIN);
+ }
+
if (ret == Z_OK) {
ret = deflate(&compressed_fpga_stream, Z_FINISH);
}
fprintf(stderr, "Error in deflate(): %d %s\n", ret, compressed_fpga_stream.msg);
free(outbuf);
deflateEnd(&compressed_fpga_stream);
- fclose(infile);
+ for(uint16_t j = 0; j < num_infiles; j++) {
+ fclose(infile[j]);
+ }
fclose(outfile);
+ free(infile);
+ free(fpga_config);
return -1;
}
free(outbuf);
deflateEnd(&compressed_fpga_stream);
- fclose(infile);
+ for(uint16_t j = 0; j < num_infiles; j++) {
+ fclose(infile[j]);
+ }
fclose(outfile);
-
+ free(infile);
+ free(fpga_config);
+
return 0;
}
int main(int argc, char **argv)
{
- char *infilename;
- char *outfilename;
+ FILE **infiles;
+ FILE *outfile;
- if (argc != 3) {
+ if (argc == 1 || argc == 2) {
usage(argv[0]);
return -1;
- } else {
- infilename = argv[1];
- outfilename = argv[2];
- }
-
- FILE *infile = fopen(infilename, "rb");
- if (infile == NULL) {
- fprintf(stderr, "Error. Cannot open input file %s", infilename);
- return -1;
+ }
+
+ infiles = calloc(argc-2, sizeof(FILE*));
+
+ for (uint16_t i = 0; i < argc-2; i++) {
+ infiles[i] = fopen(argv[i+1], "rb");
+ if (infiles[i] == NULL) {
+ fprintf(stderr, "Error. Cannot open input file %s", argv[i+1]);
+ return -1;
}
-
- FILE *outfile = fopen(outfilename, "wb");
+ }
+
+ outfile = fopen(argv[argc-1], "wb");
if (outfile == NULL) {
- fprintf(stderr, "Error. Cannot open output file %s", outfilename);
- fclose(infile);
+ fprintf(stderr, "Error. Cannot open output file %s", argv[argc-1]);
return -1;
}
- return zlib_compress(infile, outfile);
+ return zlib_compress(infiles, argc-2, outfile);
}