#!/usr/bin/env python3 """Linear-sweep 65816 disassembly of a code range, resetting M/X to defaults after RTS/RTL/RTI. usage: ai2_sweep.py ROM START END [--m16] [--x8]""" import sys, os sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) import dis65816 as D a = [x for x in sys.argv[1:] if not x.startswith('--')] fl = set(x for x in sys.argv[1:] if x.startswith('--')) rom = open(a[0], 'rb').read() addr, end = int(a[1].replace(':', ''), 16), int(a[2].replace(':', ''), 16) m8d, x8d = '--m16' not in fl, '--x8' in fl m8, x8 = m8d, x8d while addr < end: lines = [] D.disasm(rom, addr, 1, m8, x8, out=lines.append) line = lines[0] print(line) parts = line.split() nbytes = 0 # count hex bytes for p in parts[1:]: if len(p) == 2 and all(c in '0123456789ABCDEF' for c in p): nbytes += 1 else: break mn = parts[1 + nbytes] if mn in ('REP', 'SEP'): v = int(parts[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 ('RTS', 'RTL', 'RTI'): print() m8, x8 = m8d, x8d addr += nbytes