Fix reading with gmunpack and clangformat

This commit is contained in:
Miodrag Milanovic 2025-07-09 12:52:00 +02:00
parent 9148a1b81d
commit 542863a768
1 changed files with 25 additions and 32 deletions

View File

@ -59,17 +59,16 @@ static constexpr const uint8_t CFG_FILL_RAM = 0x20;
static constexpr const uint8_t CFG_SERDES = 0x40;
static const std::vector<std::pair<std::string, uint8_t>> crc_modes = {
{"check", 0x00},
{"ignore", 0x01},
{"unused", 0x02}
{"check", 0x00}, // Check CRC
{"ignore", 0x01}, // Ignore added CRC
{"unused", 0x02} // CRC is unused
};
static const std::vector<std::pair<std::string, std::vector<uint8_t>>> spi_modes = {
{"single", {}},
{"dual", {0x50, 0x21, 0x18, 0x3B}},
{"quad", {0xF0, 0x23, 0x18, 0x6B}}
{"single", {}}, // Single SPI mode
{"dual", {0x50, 0x21, 0x18, 0x3B}}, // Dual SPI mode
{"quad", {0xF0, 0x23, 0x18, 0x6B}} // Quad SPI mode
};
static const uint16_t crc_table_x25[256] = {
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5,
0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, 0x9cc9, 0x8d40, 0xbfdb, 0xae52,
@ -117,7 +116,6 @@ class BitstreamReadWriter
bool crc_unused = false;
Crc16 crc16;
// Return a single byte and update CRC
inline uint8_t get_byte()
{
@ -222,22 +220,9 @@ class BitstreamReadWriter
// Get the offset into the bitstream
size_t get_offset() { return size_t(distance(data.begin(), iter)); }
// Check the calculated CRC16 against an actual CRC16, expected in the next 2
// bytes
void check_crc16()
{
uint8_t crc_bytes[2];
uint16_t actual_crc = crc16.get_crc16();
get_bytes(crc_bytes, 2);
// cerr << hex << int(crc_bytes[0]) << " " << int(crc_bytes[1]) << endl;
uint16_t exp_crc = (crc_bytes[0] << 8) | crc_bytes[1];
if (actual_crc != exp_crc) {
std::ostringstream err;
err << "crc fail, calculated 0x" << std::hex << actual_crc << " but expecting 0x" << exp_crc;
throw BitstreamParseError(err.str(), get_offset());
}
crc16.reset_crc16();
}
void set_crc_unused(bool val) { crc_unused = val; }
bool get_crc_unused() { return crc_unused; }
// Insert the calculated CRC16 into the bitstream
void insert_crc16()
@ -396,6 +381,8 @@ class BitstreamReadWriter
void check_crc(BitstreamReadWriter &rd)
{
if (rd.get_crc_unused())
return;
uint16_t actual_crc = rd.crc16.get_crc16();
uint16_t exp_crc = rd.get_crc(); // crc
if (actual_crc != exp_crc) {
@ -461,11 +448,14 @@ int Bitstream::determine_size(int *max_die_x, int *max_die_y)
rd.get_vector(block, length);
// Check data CRC
check_crc(rd);
// Set CRC flag
if (cmd == CMD_CFGMODE)
rd.set_crc_unused(block[1] == 0x02);
// Skip bytes
if (cmd == CMD_SLAVE_MODE)
rd.skip_bytes(3);
if (cmd == CMD_CFGMODE)
rd.skip_bytes(3);
rd.skip_bytes(4);
if (cmd == CMD_PLL)
rd.skip_bytes(6);
if (cmd == CMD_CHG_STATUS)
@ -759,7 +749,7 @@ Chip Bitstream::deserialise_chip()
case CMD_CFGMODE:
BITSTREAM_DEBUG("CMD_CFGMODE");
if (length > 20)
BITSTREAM_FATAL("PLL data longer than expected", rd.get_offset());
BITSTREAM_FATAL("CFGMODE data longer than expected", rd.get_offset());
// Check header CRC
check_crc(rd);
@ -768,8 +758,10 @@ Chip Bitstream::deserialise_chip()
// Check data CRC
check_crc(rd);
rd.set_crc_unused(block[1] == 0x02);
// Skip bytes
rd.skip_bytes(3);
rd.skip_bytes(4);
break;
case CMD_SERDES:
@ -844,7 +836,7 @@ Bitstream Bitstream::serialise_chip(const Chip &chip, const std::map<std::string
auto crcmode = crc_modes.begin();
if (options.count("crcmode")) {
change_crc = true;
crcmode = find_if(crc_modes.begin(), crc_modes.end(), [&](const std::pair<std::string, uint8_t> &fp){
crcmode = find_if(crc_modes.begin(), crc_modes.end(), [&](const std::pair<std::string, uint8_t> &fp) {
return fp.first == options.at("crcmode");
});
if (crcmode == crc_modes.end()) {
@ -856,7 +848,8 @@ Bitstream Bitstream::serialise_chip(const Chip &chip, const std::map<std::string
auto spimode = spi_modes.begin();
if (options.count("spimode")) {
change_spi = true;
spimode = find_if(spi_modes.begin(), spi_modes.end(), [&](const std::pair<std::string, std::vector<uint8_t>> &fp){
spimode = find_if(spi_modes.begin(), spi_modes.end(),
[&](const std::pair<std::string, std::vector<uint8_t>> &fp) {
return fp.first == options.at("spimode");
});
if (spimode == spi_modes.end()) {