#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Trace the V810 in MAME 0.285 (a closed-box detector, tier 5). Runs MAME headless on the Virtual Boy (``vboy``, a cartridge from $NO_INTRO_DIR given by --cart) or the PC-FX (``pcfx``, its BIOS from $MAME_ROMPATH), switches the debugger's instruction trace on at frame --start and off --frames later, and exits. Each line is MAME's own disassembly, prefixed with the registers named by --regs (MAME's symbols for its v810 device: PC, PSW, R1..R31 and the system registers; docs/mame-oracle.md). MAME_ROMPATH=... python -I scripts/mame_trace.py --machine pcfx --bios v101 \ --start 0 --frames 2 --out tests/mame_traces/pcfx-boot.trace python -I scripts/mame_trace.py --machine vboy --cart "$NO_INTRO_DIR//.zip" ... The run's cwd is tests/mame_traces/ (error.log, cfg/ and nvram/ land there; all git-ignored). Firmware and games are read in place, never copied. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "tests" / "mame_traces" LUA = """ dbg = manager.machine.debugger if {start} == 0 then dbg:command('trace {out},maincpu,noloop{action}') end dbg:command('go') frames = 0 t0 = 0 sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {start} and {start} > 0 then t0 = manager.machine.time:as_double() dbg:command('trace {out},maincpu,noloop{action}') end if frames == {stop} then dbg:command('trace off,maincpu') print(string.format("WINDOW %.6f s", manager.machine.time:as_double() - t0)) {snap} manager.machine:exit() end end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--machine", choices=["vboy", "pcfx", "pcfxga"], default="pcfx") parser.add_argument("--bios", help="MAME BIOS set name (pcfx: v100 or v101)") parser.add_argument("--cart", type=Path, help="Virtual Boy cartridge (.vb or a zip holding one)") parser.add_argument("--start", type=int, default=0, help="frame at which tracing starts (0 = from reset)") parser.add_argument("--frames", type=int, default=2) parser.add_argument("--regs", default="PSW", help="comma-separated MAME symbols, or empty") parser.add_argument("--snap", action="store_true", help="save a PNG when the window ends") parser.add_argument("--out", type=Path, default=RUNS / "trace.trace") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if args.machine != "vboy" and not rompath: print("set MAME_ROMPATH (MAME's rompath holding pcfx.zip)", file=sys.stderr) return 2 if args.machine == "vboy" and not args.cart: print("--cart is required for vboy (MAME has no Virtual Boy firmware)", file=sys.stderr) return 2 RUNS.mkdir(parents=True, exist_ok=True) out = args.out.resolve() regs = [r for r in args.regs.split(",") if r] action = "" if regs: fmt = " ".join(f"{r}=%08X" for r in regs) + " " action = ',{tracelog "' + fmt + '",' + ",".join(regs) + "}" snap = "manager.machine.video:snapshot()" if args.snap else "" script = RUNS / "trace.lua" script.write_text(LUA.format(start=args.start, stop=args.start + args.frames, out=out.as_posix(), action=action, snap=snap), encoding="utf-8") command = ["nice", args.mame, args.machine] if args.bios: command += ["-bios", args.bios] if rompath: command += ["-rompath", rompath] command += [ "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-snapshot_directory", str(RUNS / "snap"), "-debug", "-debugger", "none", "-autoboot_script", str(script), "-seconds_to_run", str(max(5, (args.start + args.frames) // 50 + 5)), ] if args.cart: command += ["-cart", str(args.cart.resolve())] result = subprocess.run(command, cwd=RUNS, capture_output=True, text=True, check=False) for line in result.stdout.splitlines(): if line.startswith("WINDOW"): print(line) if not out.exists(): print(result.stdout[-2000:], result.stderr[-2000:], sep="\n", file=sys.stderr) return 1 with out.open("rb") as handle: count = sum(1 for _ in handle) print(f"{count} lines traced to {out}") return 0 if __name__ == "__main__": sys.exit(main())