# SPDX-License-Identifier: MIT """Run MAME 0.285 headless on one Model 1/2/3 set, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --driver vf2 --frames 2 # trace the i960 python -I scripts/mame_trace.py --driver vf2 --frames 2 --cpu copro # ... or the TGP python -I scripts/mame_trace.py --driver vf2 --snap 20 # untraced, PNG after 20 s The romsets come from MAME_ROMPATH (non-merged sets, so each zip is complete). Output goes to reference/mame/-/ (git-ignored): run.log with one line per instruction ("T pc totalcycles"), MAME's own log lines, stdout.txt, snap/. One MAME process at a time, a few emulated seconds at most (the machine is shared). """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] # Tags from `mame -listxml` (chip tag attributes); fields are debugger symbols that # exist for that CPU in MAME 0.285 (listed from Lua `device.state`, docs/mame-oracle.md). # Trap: on the i960, MAME's `pc` is the Process Controls register; the instruction # pointer is `ip`. CPUS = { "main": (":maincpu", "ip,ac,pc,fp,sp"), "copro": (":copro_tgp", "curpc"), "sharc": (":copro_adsp", "curpc"), "tgpx4": (":copro_tgpx4", "curpc"), "sound": (":audiocpu", "PC,SR"), } def lua(tag: str, fields: str, frames: int, trace: bool) -> str: lines = [] if trace: names = fields.split(",") fmt = " ".join(["T"] + ["%X"] * len(names) + ["%d"]) lines += [ "local dbg = manager.machine.debugger", f'dbg.visible_cpu = manager.machine.devices["{tag}"]', f"dbg:command('trace T.trace,{tag},noloop,{{logerror \"{fmt}\\\\n\",{fields},totalcycles}}')", '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("--driver", required=True, help="a MAME set name, e.g. vf2, daytona, scud") parser.add_argument("--cpu", default="main", choices=sorted(CPUS)) parser.add_argument("--fields", help="debugger symbols to log (default: the CPU's pc)") 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("--rompath", default=os.environ.get("MAME_ROMPATH")) parser.add_argument("--mame", default="mame") args = parser.parse_args() if not args.rompath: print("set MAME_ROMPATH (non-merged MAME sets)", file=sys.stderr) return 2 tag, default_fields = CPUS[args.cpu] trace = args.frames > 0 frames = args.frames if trace else max(1, round(args.snap * 60)) out = ROOT / "reference" / "mame" / f"{args.driver}-{args.cpu if trace else 'snap'}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(tag, args.fields or default_fields, frames, trace), encoding="utf-8") command = [ args.mame, args.driver, "-rompath", args.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 += ["-nodrc", "-debug", "-debugger", "none"] result = subprocess.run(["nice", "taskset", "-c", "24-31", *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 / "T.trace").unlink(missing_ok=True) # the 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())