#!/usr/bin/env python3 """Run MAME headless and write a per-instruction CP1610 trace. python scripts/mame_trace.py # intv, EXEC only, 120 frames python scripts/mame_trace.py --frames 60 --machine unichamp python scripts/mame_trace.py --cart "/path/to/Game.bin" --frames 300 python scripts/mame_trace.py --symbols # list the CPU's state symbols and stop Output goes to tests/mame_traces// (gitignored): trace.txt MAME's disassembly, one line per instruction (noloop) error.log one register line per instruction, written BEFORE it executes: PC R0 R1 R2 R3 R4 R5 R6 R7 FLAGS CYCLES FLAGS is MAME's m_flags (S 80, Z 40, OV 20, C 10); CYCLES is the debugger's totalcycles for the CPU (microcycles). MAME is an emulator-tier oracle: a detector, never a judge (docs/validation.md). One MAME at a time, short runs only. ROMs are read in place with -rompath and never copied; nothing written here is committed. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] ROMPATH = os.environ.get("MAME_ROMPATH") LUA = r""" local cpu = manager.machine.devices[":maincpu"] if os.getenv("TRACE_SYMBOLS") then for name, entry in pairs(cpu.state) do print("STATE", name, entry.symbol, entry.visible) end manager.machine:exit() return end local dbg = manager.machine.debugger dbg.visible_cpu = cpu local stop = tonumber(os.getenv("TRACE_FRAMES")) local frames = 0 dbg:command('trace trace.txt,,noloop,{logerror "%04X %04X %04X %04X %04X %04X %04X %04X %04X %02X %d\n",' .. 'curpc,r0,r1,r2,r3,r4,r5,r6,r7,curflags,totalcycles}') -- Held in a global: a local subscription is garbage-collected and stops firing. frame_tap = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames >= stop then dbg:command("trace off") manager.machine:exit() end end) dbg:command("go") """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--machine", default="intv") parser.add_argument("--frames", type=int, default=120) parser.add_argument("--cart", help="a cartridge image for -cart (read in place)") parser.add_argument("--rompath", default=ROMPATH) parser.add_argument("--mame", default="mame") parser.add_argument("--symbols", action="store_true", help="print the CPU's state symbols") args = parser.parse_args() if not args.rompath: raise SystemExit("set MAME_ROMPATH to the MAME (non-merged) ROM folder, or pass --rompath") out = ROOT / "tests" / "mame_traces" / args.machine out.mkdir(parents=True, exist_ok=True) (out / "trace.lua").write_text(LUA, encoding="utf-8") command = [ args.mame, args.machine, "-rompath", args.rompath, "-homepath", ".", "-cfg_directory", "./cfg", "-nvram_directory", "./nvram", "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-debug", "-debugger", "none", "-log", "-autoboot_script", "trace.lua", ] if args.cart: command += ["-cart", args.cart] env = dict(os.environ, TRACE_FRAMES=str(args.frames)) if args.symbols: env["TRACE_SYMBOLS"] = "1" # error.log and the trace file land in the current directory, so run there. result = subprocess.run(command, cwd=out, env=env, check=False) print(f"mame exit {result.returncode}; output in {out}") return result.returncode if __name__ == "__main__": sys.exit(main())