# SPDX-License-Identifier: MIT """Run MAME 0.285's Konami TMNT-era drivers headless, as a closed-box detector. MAME_ROMPATH=... python -I scripts/mame_trace.py tmnt --cpu main --seconds 2 MAME_ROMPATH=... python -I scripts/mame_trace.py tmnt --cpu sound --seconds 3 MAME_ROMPATH=... python -I scripts/mame_trace.py simpsons --cpu konami --seconds 2 MAME_ROMPATH=... python -I scripts/mame_trace.py ssriders --maps # address maps only MAME_ROMPATH=... python -I scripts/mame_trace.py xmen --snap 4 # PNG every 4 s One instruction per line goes to reference/mame/-/error.log (MAME's -log output; the run's cwd), as "T pc totalcycles frame vpos". Only short runs (a few emulated seconds): one MAME process at a time on this machine. Tags and state names (probed on tmnt, simpsons, xmen, MAME 0.285): ":maincpu" is the MC68000 (D0-D7 A0-A7 SR PC) or, on simpsons and aliens, the KONAMI CPU (A B X Y U S DP CC PC); ":audiocpu" is the Z80 (AF BC DE HL IX IY SP PC). Adapted from namcos2-python's mame_trace.py (the family's own MIT tooling). """ 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,sr,d0,d1,a0,a7"), "konami": (":maincpu", "pc,a,b,x,y,u,s,cc"), "sound": (":audiocpu", "pc,af,bc,de,hl,sp"), } 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") ap.add_argument("--maps", action="store_true", help="print the address maps and exit") a = ap.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: sys.exit("set MAME_ROMPATH") label = a.cpu or ("maps" if a.maps else "snap") work = ROOT / "reference" / "mame" / f"{a.game}-{label}" work.mkdir(parents=True, exist_ok=True) for stale in ("error.log", "T.trace", "maps.txt"): 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}-{label}-{stale}")) script = ROOT / "scripts" / "mame_maps.lua" if a.maps else work / "run.lua" if not a.maps: script.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"), "-cfg_directory", str(work / "cfg"), "-nvram_directory", str(work / "nvram"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-seconds_to_run", str(a.seconds), "-autoboot_script", str(script)] if a.cpu: cmd += ["-debug", "-debugger", "none", "-log"] print(" ".join(cmd[:2]), "...", file=sys.stderr) if a.maps: r = subprocess.run(cmd, cwd=work, capture_output=True, text=True) lines = [ln for ln in r.stdout.splitlines() if ln.startswith("MAP ")] (work / "maps.txt").write_text("\n".join(lines) + "\n") print(f"{a.game}: {len(lines)} map entries -> {work / 'maps.txt'}") return 0 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())