import struct

DUMP = r"C:/Users/vikto/Documents/Claude/rt-4d/radio-spi-dump.bin"
with open(DUMP, "rb") as f:
    cal = f.read(0x1000)

base = 0x20
REC = 0x70

# Record layout hypothesis:
#  +0x00 b0 : band index/tag (0a, 11, then 00)
#  +0x01 b1 : flag 0x80 (band-valid?)
#  +0x02..0x08 (7 bytes): const A "12 e2 34 40 2a 4f b3"
#  +0x09..0x0f (7 bytes): const B (identical copy)
#  +0x10.. : then 6 tables of 16 bytes each = 96 bytes -> 0x10+96 = 0x70. EXACT.
# So after 16-byte header (b0,b1 + 14 const bytes), 6 x 16-byte tables.
# 0x10-0x1f table0, 0x20-0x2f table1, 0x30-0x3f table2, 0x40-0x4f table3, 0x50-0x5f table4, 0x60-0x6f table5.

for idx in [0,1,2]:
    off = base+idx*REC
    r = cal[off:off+REC]
    print(f"\n### REC {idx} @0x{off:03x} tag={r[0]:02x} flag={r[1]:02x}")
    print("  constA:", " ".join(f"{b:02x}" for b in r[2:9]))
    print("  constB:", " ".join(f"{b:02x}" for b in r[9:16]))
    for t in range(6):
        tab = r[0x10+t*16:0x10+t*16+16]
        print(f"  table{t} @+0x{0x10+t*16:02x}: " + " ".join(f"{b:02x}" for b in tab) + f"   (dec: {[b for b in tab]})")

# The 16 header-tail bytes 3a..52 are an ascending curve of 16 => matches 16 columns.
# Interpret header:
hdr = cal[0:0x20]
print("\nHEADER analysis:")
print("  +0x00 u16:", struct.unpack("<H", hdr[0:2])[0])
print("  +0x02..0x07: ", " ".join(f"{b:02x}" for b in hdr[2:8]))
# 37 a0 38 6b 40 ab  -> maybe two u24 or freq. as u32 at 0x02:
print("  +0x02 u32:", hex(struct.unpack("<I", hdr[2:6])[0]), struct.unpack("<I", hdr[2:6])[0])
print("  +0x04 u32:", hex(struct.unpack("<I", hdr[4:8])[0]), struct.unpack("<I", hdr[4:8])[0])
# 05 00 05 00 0a 00 05 00 -> four u16: 5,5,10,5
print("  +0x08 four u16:", struct.unpack("<4H", hdr[8:16]))
print("  +0x10..0x1f (16-col x-axis?):", [b for b in hdr[0x10:0x20]])
