# SPDX-License-Identifier: MIT """Run MAME 0.285's System 24 / System 32 drivers headless, as a closed-box detector. python -I scripts/mame_trace.py hotrod --frames 3 # System 24 main 68000 python -I scripts/mame_trace.py hotrod --frames 3 --cpu sub # ... the sub 68000 python -I scripts/mame_trace.py gground --frames 3 --cpu sub # ... an FD1094 python -I scripts/mame_trace.py radm --frames 3 --cpu v60 # System 32 V60 python -I scripts/mame_trace.py radm --frames 3 --cpu s32sound # ... its Z80 python -I scripts/mame_trace.py hotrod --snap 30 # no trace: run, save a PNG MAME is run, never read (docs/mame-oracle.md). 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 totalcycles"), `--watch` write watchpoints ("W address data totalcycles"), 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] # System 32 single-board sets put their devices under ":mainpcb:" in MAME 0.285; the # --tag option overrides for the few that do not (docs/mame-oracle.md). CPUS = { "main": (":maincpu", "pc,sr,d0,a7"), "sub": (":subcpu", "pc,sr,d0,a7"), "v60": (":mainpcb:maincpu", "pc,sp"), "s32sound": (":mainpcb:soundcpu", "pc,af,sp"), "v25": (":mainpcb:mcu", "pc"), } def lua(tag: str, fields: str, frames: int, trace: bool, watch: list[str], after: int) -> str: lines = ["local dbg = manager.machine.debugger"] if trace else [] start = [] # the trace and its watchpoints, issued at once (at frame `after` if given) if trace: fmt = " ".join(["S"] + ["%X"] * len(fields.split(",")) + ["%d"]) lines.append(f'dbg.visible_cpu = manager.machine.devices["{tag}"]') start.append(f"dbg:command('trace S.trace,{tag},noloop," f'{{logerror "{fmt}\\\\n",{fields},totalcycles}}\')') # fmt: skip for spec in watch: # "start,length" in the traced CPU's program space, writes start.append( f"dbg:command('wpset {spec},w,1," f'{{logerror "W %X %X %d\\\\n",wpaddr,wpdata,totalcycles; g}}\')' ) if not after: lines += start start = [] lines.append('dbg:command("go")') lines += [ "count = 0", "notifier = emu.add_machine_frame_notifier(function()", " count = count + 1", *([f" if count == {after} then {' '.join(start)} end"] if start else []), f" if count == {frames + after} 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: hotrod, gground, radm, ...") parser.add_argument("--cpu", default="main", choices=sorted(CPUS)) parser.add_argument("--tag", help="override the CPU's device tag") 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("--after", type=int, default=0, help="start tracing after N frames") parser.add_argument("--watch", action="append", default=[], help="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 tag, fields = CPUS[args.cpu] tag = args.tag or tag 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(tag, fields, frames, trace, args.watch, args.after), encoding="utf-8") 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 + args.after) / 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())