#!/usr/bin/env python3 """Disassemble exactly the instructions recorded in a Mesen coverage file (re/lua/mech_cov.lua output), using the M/X flags captured at each PC. Gaps between covered runs are marked. usage: covdis.py ROM COVFILE [BANK_HEX ...] > out.asm """ import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from dis65816 import disasm # noqa: E402 rom = open(sys.argv[1], 'rb').read() cov = {} for line in open(sys.argv[2]): a, f = line.split() cov[int(a, 16)] = int(f, 16) banks = {int(b, 16) for b in sys.argv[3:]} prev_end = None for a in sorted(cov): if banks and (a >> 16) not in banks: continue if (a & 0xFFFF) < 0x8000: continue f = cov[a] lines = [] disasm(rom, a, 1, m8=bool(f & 0x20) or bool(f & 1), x8=bool(f & 0x10) or bool(f & 1), out=lines.append) line = lines[0] if prev_end is not None and a > prev_end: print(f'; ---- gap {prev_end:06X}..{a - 1:06X}') elif prev_end is not None and a < prev_end: print('; ---- overlap (mode change / mid-instruction entry)') print(line) n = len(line[9:21].split()) prev_end = a + n