Files

37 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Disassemble the per-power AI predicates (pointer table 80:DB7B, 43 x 24-bit). usage: ai2_preds.py ROM"""
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
import dis65816 as D
from collections import defaultdict
d = open(sys.argv[1], 'rb').read()
off = lambda a: ((a >> 16) & 0x7f) * 0x8000 + (a & 0xffff) - 0x8000
g = defaultdict(list)
for i in range(0x2B):
g[int.from_bytes(d[off(0x80DB7B + 3 * i):][:3], 'little')].append(i)
for p, ids in sorted(g.items()):
print(f'\n;=== predicate {p:06X} for power ' + ' '.join(f'{i:02X}' for i in ids))
addr, m8, x8, maxfwd, n = p, True, False, p, 0
while n < 90:
lines = []
D.disasm(d, addr, 1, m8, x8, out=lines.append)
L = lines[0]
print(L)
parts = L.split()
nb = 0
for q in parts[1:]:
if len(q) == 2 and all(c in '0123456789ABCDEF' for c in q): nb += 1
else: break
mn = parts[1 + nb]
if mn in ('REP', 'SEP'):
v = int(parts[2 + nb][2:], 16)
if mn == 'REP': m8 = m8 and not v & 0x20; x8 = x8 and not v & 0x10
else: m8 = m8 or bool(v & 0x20); x8 = x8 or bool(v & 0x10)
if mn in ('BEQ', 'BNE', 'BPL', 'BMI', 'BCC', 'BCS', 'BRA', 'BVC', 'BVS'):
t = int(parts[1 + nb + 1][1:], 16)
maxfwd = max(maxfwd, t)
addr = (addr & 0xFF0000) | ((addr + nb) & 0xFFFF)
n += 1
if mn in ('RTL', 'JML', 'JMP') and addr > maxfwd:
break