# SPDX-License-Identifier: MIT """Run MAME 0.285's Astrocade drivers headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_trace.py --frames 3 # trace the BIOS's Z80 python -I scripts/mame_trace.py --frames 3 --driver gorf # an arcade board python -I scripts/mame_trace.py --snap 6 # no trace: run, save a PNG python -I scripts/mame_trace.py --frames 3 --cart "$NO_INTRO_DIR/Bally - Astrocade/X.zip" With no cartridge the BIOS runs to its own menu (the built-in Gunfight, Checkmate, Calculator and Scribbling), which is all the board's first milestones need. Romsets come from MAME_ROMPATH. Output goes to reference/mame/-/ (git-ignored): run.log with one line per instruction ("Z pc af bc de hl sp ix iy totalcycles") and one line per I/O write ("IO port data totalcycles": the Data/Address chip and I/O chip registers), and snap/. MAME is a detector here, never a judge: GPL-2.0+ as a whole, but its Astrocade files are BSD-3-Clause (docs/sources.md). Nothing of its source is fetched or read. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] FIELDS = "pc,af,bc,de,hl,sp,ix,iy" def lua(frames: int, trace: bool) -> str: lines = [] if trace: fmt = " ".join(["Z"] + ["%X"] * len(FIELDS.split(",")) + ["%d"]) lines += [ "local dbg = manager.machine.debugger", 'dbg.visible_cpu = manager.machine.devices[":maincpu"]', f"dbg:command('trace Z.trace,:maincpu,noloop,{{logerror \"{fmt}\\\\n\",{FIELDS},totalcycles}}')", # Every I/O write, whatever the high byte (the I/O chip decodes it on some ports). "dbg:command('wpiset 0,10000,w,1,{logerror \"IO %X %X %d\\\\n\",wpaddr,wpdata,totalcycles; g}')", '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", default="astrocde", help="astrocde, astrocdl, astrocdw, gorf, wow, ...") 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("--cart", help="a cartridge image or zip (home drivers only)") 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 (it must hold astrocde.zip or the arcade set)", 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.driver}-{'trace' if trace else 'snap'}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(frames, trace), encoding="utf-8") command = [ args.mame, args.driver, "-rompath", args.rompath, "-homepath", str(out), "-snapshot_directory", str(out / "snap"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-seconds_to_run", str(int(frames / 60) + 5), "-autoboot_script", str(script), ] # fmt: skip if args.cart: command += ["-cart", args.cart] 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 / "Z.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())