# SPDX-License-Identifier: MIT """Run MAME 0.285's System 16 drivers headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py shinobi --frames 3 # trace the 68000 python -I scripts/mame_trace.py goldnaxe --frames 3 --cpu mcu # ... the 8751 python -I scripts/mame_trace.py goldnaxe --frames 3 --cpu sound # ... the Z80 python -I scripts/mame_trace.py shinobi --snap 20 # no trace: run, save a PNG MAME is run, never read. The romsets come from MAME_ROMPATH (non-merged sets: one zip holds the whole machine). Output goes to reference/mame/-/ (git-ignored): run.log with one line per instruction ("S pc totalcycles"), the 315-5195 register writes and the sound-latch writes the watchpoints log, and snap/*.png. One MAME process at a time; a few emulated seconds at most. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPUS = {"main": ":maincpu", "sound": ":soundcpu", "mcu": ":mcu", "sample": ":upd7751"} FIELDS = {"main": "pc,sr,d0,a7", "sound": "pc,af,sp", "mcu": "pc,acc,sp", "sample": "pc,a"} def lua(cpu: str, frames: int, trace: bool, watch: list[str]) -> str: lines = ["local dbg = manager.machine.debugger"] if trace else [] if trace: names = FIELDS[cpu].split(",") fmt = " ".join(["S"] + ["%X"] * len(names) + ["%d"]) lines += [ f'dbg.visible_cpu = manager.machine.devices["{CPUS[cpu]}"]', f"dbg:command('trace S.trace,{CPUS[cpu]},noloop," f'{{logerror "{fmt}\\\\n",{FIELDS[cpu]},totalcycles}}\')', ] for spec in watch: # "start,length" on the main CPU's program space, writes lines.append( f"dbg:command('wpset {spec},w,1," f'{{logerror "W %X %X %X %d\\\\n",wpaddr,wpsize,wpdata,totalcycles; g}}\')' ) lines.append('dbg:command("go")') lines += [ "count = 0", "notifier = emu.add_machine_frame_notifier(function()", " count = count + 1", f" if count == {frames} then", " manager.machine.video:snapshot()", " manager.machine:exit()", " end", "end)", ] return "\n".join(lines) + "\n" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("machine", help="a MAME machine name: shinobi, altbeast, goldnaxe, ...") parser.add_argument("--cpu", default="main", choices=sorted(CPUS)) parser.add_argument("--frames", type=int, default=0, help="trace this many frames") parser.add_argument("--snap", type=float, default=0, help="untraced run of N seconds") parser.add_argument("--watch", action="append", default=[], help="main-CPU write watchpoint 'start,length' (hex), repeatable") # fmt: skip parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (it must hold the machine's zip)", file=sys.stderr) return 2 trace = args.frames > 0 frames = args.frames if trace else max(1, round(args.snap * 60)) out = ROOT / "reference" / "mame" / f"{args.machine}-{args.cpu if trace else 'snap'}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.cpu, frames, trace, args.watch if args.cpu == "main" else []), encoding="utf-8") # fmt: skip command = [ args.mame, args.machine, "-rompath", rompath, "-homepath", str(out), "-cfg_directory", str(out / "cfg"), "-nvram_directory", str(out / "nvram"), "-snapshot_directory", str(out / "snap"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-seconds_to_run", str(int(frames / 50) + 5), "-autoboot_script", str(script), ] # fmt: skip if trace: command += ["-debug", "-debugger", "none"] result = subprocess.run(["nice", *command], cwd=out, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) # fmt: skip (out / "stdout.txt").write_text(result.stdout, encoding="utf-8") log = out / "error.log" if log.exists(): log.replace(out / "run.log") (out / "S.trace").unlink(missing_ok=True) # the debugger's disassembly; run.log has the state print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())