#!/usr/bin/env python3 """Dump the CPU-AI planner tables (bank $81) in readable form, or as a C++ byte blob. usage: ai_tables.py ROM -> readable dump (re/notes/ai_tables.txt) ai_tables.py ROM --cpp -> `static constexpr uint8_t kAiBank81[]` covering $E400-$F1FF """ import sys PIECES = 'O L J S Z I T'.split() BLOB_LO, BLOB_HI = 0xE400, 0xF200 # every planner ROM read observed lies in $E40A-$F197 def main(): rom = open(sys.argv[1], 'rb').read() rd = lambda a: rom[0x8000 + (a - 0x8000)] rw = lambda a: rd(a) | (rd(a + 1) << 8) if '--cpp' in sys.argv: blob = bytes(rd(a) for a in range(BLOB_LO, BLOB_HI)) print('// Generated by re/tools/ai_tables.py from the JP ROM: bank $81 $%04X-$%04X (CPU-AI tables).' % (BLOB_LO, BLOB_HI - 1)) print('// Index with (addr - 0x%04X). The planner reads it through 16-bit pointers, quirks included.' % BLOB_LO) print('inline constexpr std::uint16_t kAiBlobBase = 0x%04X;' % BLOB_LO) print('inline constexpr std::uint8_t kAiBlob[%d] = {' % len(blob)) for i in range(0, len(blob), 16): print(' ' + ', '.join(f'0x{b:02X}' for b in blob[i:i + 16]) + ',') print('};') return h = lambda bs: ' '.join(f'{b:02X}' for b in bs) out = [] P = out.append P('TBG (J) CPU-AI planner tables, bank $81. Generated by re/tools/ai_tables.py. Values hex.') P('Piece index = ROM type mod 7: ' + ' '.join(f'{i}={n}' for i, n in enumerate(PIECES))) P('Template byte v vs observed value m: 63 = wildcard; 0A..7F: m >= v/10 (integer div); 00..09: m == v;') P(' 80..FF: special (rank/profile): m <= 0x100-v (FF: <=1, FE: <=2); pattern fit: m == v exactly') P(' (m is FF where the column is lower than the base); panic: m negative and 0x100-m >= 0x100-v (FF matches FF/FE).') P(' All comparisons are 8-bit with the 65816 N flag (see re/tools/ai_ref.py Planner.elem_ok).') P('') P('== E46B piece column fill counts, per piece -> 4 rotations x 4 columns (0 = column unused)') for t in range(7): p = rw(0xE46B + 2 * t) P(f' {PIECES[t]} @{p:04X}: ' + ' | '.join(h(rom[0x8000 + p - 0x8000 + 4 * r:][:4]) for r in range(4))) P('') P('== Special templates (81:DE38). count E4E9[piece]; per variant v:') P(' window start offset E4F0[piece][v] (window = ranks of columns sorted[0]-off .. +5, pad 0A),') P(' rank template E4FE[piece] (6 bytes/v), profile template E50C[piece] (4 bytes/v),') P(' result: rotation E51A[piece][v], column = sorted[0] - E528[piece][v]') for t in range(7): n = rd(0xE4E9 + t) po, pr, pp, prot, pcol = (rw(base + 2 * t) for base in (0xE4F0, 0xE4FE, 0xE50C, 0xE51A, 0xE528)) P(f' {PIECES[t]}: {n} variants (off@{po:04X} rank@{pr:04X} prof@{pp:04X} rot@{prot:04X} col@{pcol:04X})') for v in range(n): P(f' v{v}: off {rd(po + v):02X} rank [{h(rom[0x8000 + pr - 0x8000 + 6 * v:][:6])}]' f' prof [{h(rom[0x8000 + pp - 0x8000 + 4 * v:][:4])}] -> rot {rd(prot + v):02X} coloff {rd(pcol + v):02X}') P('') P('== Wells (81:DFA5): per piece, indexed by well type 0 (equal sides) / 1 (right lower) / 2 (right higher):') P(' rotation E77F[piece][type] (63 = piece not used for this well), column offset E7A2[piece][type]') for t in range(7): a, b = rw(0xE77F + 2 * t), rw(0xE7A2 + 2 * t) P(f' {PIECES[t]}: rot [{h(rom[0x8000 + a - 0x8000:][:3])}] coloff [{h(rom[0x8000 + b - 0x8000:][:3])}]') P('') def sets(title, cnt, win, pat, rot, col, nsets): P(title) for s in range(nsets): tc, tw, tp, tr, tl = (rw(base + 2 * s) for base in (cnt, win, pat, rot, col)) P(f' set {s}: count@{tc:04X} window@{tw:04X} pattern@{tp:04X} rot@{tr:04X} coloff@{tl:04X}') for t in range(7): n = rd(tc + t) if n == 0: P(f' {PIECES[t]}: 0 variants') continue pw_, pp_, pr_, pl_ = rw(tw + 2 * t), rw(tp + 2 * t), rw(tr + 2 * t), rw(tl + 2 * t) P(f' {PIECES[t]}: {n} variants (win@{pw_:04X} pat@{pp_:04X} rot@{pr_:04X} col@{pl_:04X})') for v in range(n): P(f' v{v:<2d} win {rd(pw_ + v):02X} pattern [{h(rom[0x8000 + pp_ - 0x8000 + 5 * v:][:5])}]' f' -> rot {rd(pr_ + v):02X} coloff {rd(pl_ + v):02X}') P('') sets('== Pattern fit (81:DB40): 5 sets x piece. count E7C2, window offset E7CC, 5-byte pattern E7D6,\n' ' rotation E7E0, column offset E7EA. Window = heights of columns sorted[k]-win .. +4 minus sorted height k\n' ' (FF if lower, off-board = 16-base).', 0xE7C2, 0xE7CC, 0xE7D6, 0xE7E0, 0xE7EA, 5) sets('== Panic search (81:DA9F): 4 sets x piece. count EC1F, window EC27, pattern EC2F, rotation EC37,\n' ' column offset EC3F. Window like pattern fit but negatives FF/FE and off-board 63.', 0xEC1F, 0xEC27, 0xEC2F, 0xEC37, 0xEC3F, 4) print('\n'.join(out)) if __name__ == '__main__': main()