MEGA65 / mega65-tools

Tools and Utilities for the MEGA65 Retro Computers
GNU General Public License v3.0
29 stars 31 forks source link

BIT2CORE: Fix problem retrieving fpga-part-id #64

Closed gurcei closed 3 years ago

gurcei commented 3 years ago

Recently, we discovered a problem on converting recent .bit files to .cor, discussed in this Discord thread:

$ bin/bit2core.exe mega65r3 ~/Downloads/mega65r3_b70.bit mega65r3-b70 mega65r3-b70 mega65r3_b70.cor
Bitstream file is 6165570 bytes long.
This bitstream's FPGA part is for an unknown mega65 target: "???" (FPGA part: )"
ERROR: Provided bitstream is for a different mega65 target to the one you specified: "mega65r1|mega65r3" (FPGA part: 7a200tfbg484)

Looking into it, bit2core assumed that the FPGA-part-id field would always be located at offset 0x4C.

00000000  00 09 0f f0 0f f0 0f f0  0f f0 00 00 01 61 00 39  |.............a.9|
00000010  63 6f 6e 74 61 69 6e 65  72 3b 43 4f 4d 50 52 45  |container;COMPRE|
00000020  53 53 3d 54 52 55 45 3b  55 73 65 72 49 44 3d 30  |SS=TRUE;UserID=0|
00000030  58 46 46 46 46 46 46 46  46 3b 56 65 72 73 69 6f  |XFFFFFFFF;Versio|
00000040  6e 3d 32 30 31 39 2e 32  00 62 00 0d 37 61 32 30  |n=2019.2.b..7a20|
00000050  30 74 66 62 67 34 38 34  00 63 00 0b 32 30 32 31  |0tfbg484.c..2021|

But looks like in this new bitstream, it is at offset 0x4e.

00000000  00 09 0f f0 0f f0 0f f0  0f f0 00 00 01 61 00 3b  |.............a.;|
00000010  63 6f 6e 74 61 69 6e 65  72 3b 43 4f 4d 50 52 45  |container;COMPRE|
00000020  53 53 3d 54 52 55 45 3b  55 73 65 72 49 44 3d 30  |SS=TRUE;UserID=0|
00000030  58 46 46 46 46 46 46 46  46 3b 56 65 72 73 69 6f  |XFFFFFFFF;Versio|
00000040  6e 3d 32 30 32 30 2e 32  2e 32 00 62 00 0d 37 61  |n=2020.2.2.b..7a|
00000050  32 30 30 74 66 62 67 34  38 34 00 63 00 0b 32 30  |200tfbg484.c..20|

I've found a description here of the xilinx bitstream file format:

Ah ok, that doc helps a lot, they use variable-length fields. I'll need to tweak the logic inside bit2core to suit this...