import io
data = open(r"C:/Users/vikto/Documents/Claude/rt-4d/radio-spi-dump.bin","rb").read()
out=io.open(r"C:/Users/vikto/Documents/Claude/rt-4d/analyze/out10.txt","w",encoding="utf-8")

# Render candidate glyphs at 0x19c000 as bitmaps to confirm font.
# Try 16x16 (32 bytes/glyph) and 12x12 etc.
def render(off, w_bytes, rows, label):
    out.write("glyph @%#x %dx%d bytes/row=%d:\n"%(off,rows,w_bytes*8,w_bytes))
    for r in range(rows):
        line=""
        for cb in range(w_bytes):
            b=data[off+r*w_bytes+cb]
            for bit in range(8):
                line += "#" if (b>>bit)&1 else "."
        out.write(line+"\n")
    out.write("\n")

# 16x16 = 2 bytes/row, 16 rows = 32 bytes
for g in range(6):
    render(0x19c000+g*32, 2, 16, "16x16 glyph %d"%g)

# Occupancy across 0x198000..0x242000: how much real font data
seg=data[0x198000:0x242000]
from collections import Counter
c=Counter(seg)
out.write("region 0x198000-0x242000 total=%d ff=%d zero=%d other=%d\n"%(len(seg),c[0xff],c[0],len(seg)-c[0xff]-c[0]))

# The font is likely: big CJK 16x16 bitmap set. count of 32-byte glyphs that fit in nonFF area
# Check periodicity of 32 in the 0x19c000+ area by counting nonzero glyph cells
out.write("\nSample far into region 0x200000:\n")
def hx(off,n=48):
    s=""
    for j in range(0,n,16):
        ch=data[off+j:off+j+16]
        s+="%08x  %s\n"%(off+j,' '.join('%02x'%b for b in ch))
    return s
out.write(hx(0x200000,0x40))
out.write("\n0x240000:\n"+hx(0x240000,0x40))
out.close()
print("done")
