# SPDX-License-Identifier: MIT """Run MAME 0.285's Arcadia 2001 or VC 4000 driver headless, as a closed-box detector. python -I scripts/mame_trace.py --cart "$NO_INTRO_DIR/Emerson - Arcadia 2001/Escape (USA, Europe).zip" --frames 3 python -I scripts/mame_trace.py --driver vc4000 --cart "$NO_INTRO_DIR/Interton - VC 4000/Golf (Europe).zip" --frames 3 python -I scripts/mame_trace.py --cart ... --snap 5 # no trace: run, save a PNG Neither console has a BIOS: a cartridge is required. Output goes to reference/mame/--/ (git-ignored): run.log with one line per 2650 instruction ("S pc ps r0 r1 r2 r3 totalcycles"; `ps` is PSU<<8|PSL) and one line per write into the video chip's window ("V address data totalcycles": the UVI at 0x1800-0x1BFF on the Arcadia, the PVI at 0x1F00-0x1FFF on the VC 4000; docs/memory-map.md). MAME is a detector here, never a judge (docs/validation.md). Nothing of its source is fetched or read. """ from __future__ import annotations import argparse import os import re import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] FIELDS = "pc,ps,r0,r1,r2,r3" VIDEO = {"arcadia": ("1800", "400"), "vc4000": ("1f00", "100"), "elektor": ("1f00", "100")} def lua(driver: str, frames: int, trace: bool) -> str: lines = [] if trace: fmt = " ".join(["S"] + ["%X"] * len(FIELDS.split(",")) + ["%d"]) start, length = VIDEO.get(driver, VIDEO["vc4000"]) lines += [ "local dbg = manager.machine.debugger", 'dbg.visible_cpu = manager.machine.devices[":maincpu"]', f"dbg:command('trace S.trace,:maincpu,noloop,{{logerror \"{fmt}\\\\n\",{FIELDS},totalcycles}}')", f"dbg:command('wpset {start},{length},w,1,{{logerror \"V %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="arcadia", help="arcadia (or a clone), vc4000 (or a clone), elektor") parser.add_argument("--cart", help="a cartridge image or zip (required except for elektor)") 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.cart and args.driver != "elektor": print("a cartridge is required (--cart, e.g. from $NO_INTRO_DIR)", file=sys.stderr) return 2 trace = args.frames > 0 frames = args.frames if trace else max(1, round(args.snap * 50)) stem = re.sub(r"[^A-Za-z0-9]+", "_", Path(args.cart).stem)[:24] if args.cart else "nocart" out = ROOT / "reference" / "mame" / f"{args.driver}-{stem}-{'trace' if trace else 'snap'}" out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.driver, frames, trace), encoding="utf-8") command = [ args.mame, args.driver, "-homepath", str(out), "-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 args.rompath: command += ["-rompath", args.rompath] 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 / "S.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())