# SPDX-License-Identifier: MIT """Run MAME 0.285's Namco System 1 driver headless, as a closed-box detector. MAME_ROMPATH=... python -I scripts/mame_trace.py pacmania --cpu main --seconds 2 MAME_ROMPATH=... python -I scripts/mame_trace.py splatter --cpu mcu --seconds 2 MAME_ROMPATH=... python -I scripts/mame_trace.py galaga88 --snap 10 # no trace, PNGs One instruction per line goes to reference/mame/-/error.log (MAME's -log output; the run's cwd), as "T pc totalcycles frame vpos", and the disassembly to T.trace beside it. --snap saves a PNG each N emulated seconds into the same folder's snap/. Only short runs (a few emulated seconds): MAME is one process at a time on this machine. Tags and state names (probed on pacmania, MAME 0.285): ":maincpu", ":subcpu", ":audiocpu" are MC6809E (A B D X Y U S DP CC PC); ":mcu" is the HD63701V0 (A B X S CC PC; it has no D in MAME's state list). """ from __future__ import annotations import argparse import os import shutil import subprocess import sys import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPUS = { "main": (":maincpu", "pc,a,b,x,y,u,s,dp,cc"), "sub": (":subcpu", "pc,a,b,x,y,u,s,dp,cc"), "sound": (":audiocpu", "pc,a,b,x,y,u,s,dp,cc"), "mcu": (":mcu", "pc,a,b,x,s,cc"), } def lua(cpu: str | None, snap: int | None) -> str: out = [] if cpu: tag, fields = CPUS[cpu] fmt = " ".join(["T"] + ["%X"] * len(fields.split(",")) + ["%d", "%d", "%d"]) out += [ "local dbg = manager.machine.debugger", f'dbg.visible_cpu = manager.machine.devices["{tag}"]', f"dbg:command('trace T.trace,{tag},noloop," f'{{logerror "{fmt}\\\\n",{fields},totalcycles,frame,beamy}}\')', 'dbg:command("go")', ] if snap: out += [ "frames = 0", "tap = emu.add_machine_frame_notifier(function()", " frames = frames + 1", f" if frames % {snap * 60} == 0 then manager.machine.video:snapshot() end", "end)", ] return "\n".join(out) + "\n" def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("game") ap.add_argument("--cpu", choices=sorted(CPUS)) ap.add_argument("--seconds", type=int, default=2) ap.add_argument("--snap", type=int, help="save a PNG every N emulated seconds") a = ap.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: sys.exit("set MAME_ROMPATH") work = ROOT / "reference" / "mame" / f"{a.game}-{a.cpu or 'snap'}" work.mkdir(parents=True, exist_ok=True) for stale in ("error.log", "T.trace"): if (work / stale).exists(): trash = ROOT / "trash" / "mame-runs" trash.mkdir(parents=True, exist_ok=True) shutil.move(str(work / stale), str(trash / f"{time.strftime('%Y%m%d-%H%M%S')}-{a.game}-{a.cpu}-{stale}")) (work / "run.lua").write_text(lua(a.cpu, a.snap)) mame = os.environ.get("MAME") or shutil.which("mame") or sys.exit("put mame on PATH or set MAME") cmd = [mame, a.game, "-rompath", rompath, "-homepath", str(work), "-snapshot_directory", str(work / "snap"), "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-seconds_to_run", str(a.seconds), "-autoboot_script", str(work / "run.lua")] cmd += ["-video", "none"] if not a.snap else ["-video", "none", "-window"] if a.cpu: cmd += ["-debug", "-debugger", "none", "-log"] print(" ".join(cmd[:2]), "...", file=sys.stderr) r = subprocess.run(cmd, cwd=work) log = work / "error.log" if a.cpu and log.exists(): n = sum(1 for line in log.open(errors="replace") if line.startswith("T ")) print(f"{a.game} {a.cpu}: {n} instructions traced in {a.seconds} s -> {log}") return r.returncode if __name__ == "__main__": raise SystemExit(main())