# SPDX-License-Identifier: MIT """Run MAME 0.285's Dreamcast driver headless, as a closed-box detector (docs/mame-oracle.md). python -I scripts/mame_run.py --snap 600,1200 --frames 1200 # PNGs, no trace python -I scripts/mame_run.py --trace 1000 --frames 1003 # SH-4 trace window python -I scripts/mame_run.py --trace 0 --frames 3 --cpu sound # the AICA's ARM7 No disc is mounted: the boot ROM runs its own screens (the swirl, then Set Date/Time on an empty flash and NVRAM), which is what the board's first milestones need. Romsets come from MAME_ROMPATH (dc.zip holds the boot ROMs and the US flash). Output goes to reference/mame// (git-ignored): run.log (MAME's log with one "S pc sr pr r15 totalcycles" line per traced instruction), snap/, stdout.txt. The disassembly file MAME writes beside it is moved to trash/ (never deleted). Traps: the SH-4 recompiler must be off while tracing (-nodrc). Tags are ":maincpu" (the SH7091 at 200 MHz) and ":soundcpu" (the ARM7 at 2.82 MHz). A traced frame writes about 130 MB of log; keep windows to a few frames. """ from __future__ import annotations import argparse import os import shutil import subprocess import sys import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] CPUS = {"main": (":maincpu", "pc,sr,pr,r15"), "sound": (":soundcpu", "curpc")} def lua(cpu: str, trace_from: int | None, frames: int, snaps: list[int]) -> str: tag, fields = CPUS[cpu] fmt = " ".join(["S"] + ["%X"] * len(fields.split(",")) + ["%d"]) trace = (f"dbg:command('trace S.trace,{tag},noloop," f'{{logerror "{fmt}\\\\n",{fields},totalcycles}}\')') # fmt: skip lines = [] if trace_from is not None: lines += ["dbg = manager.machine.debugger", f'dbg.visible_cpu = manager.machine.devices["{tag}"]'] if trace_from == 0: lines.append(trace) lines.append('dbg:command("go")') lines += ["count = 0", "notifier = emu.add_machine_frame_notifier(function()", " count = count + 1"] if trace_from: lines.append(f" if count == {trace_from} then {trace} end") for frame in snaps: lines.append(f" if count == {frame} then manager.machine.video:snapshot() end") lines += [f" if count == {frames} then 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="dc", help="dc, dcjp, dceu, dcdev") parser.add_argument("--cpu", default="main", choices=sorted(CPUS)) parser.add_argument("--frames", type=int, required=True, help="exit after this many frames") parser.add_argument("--trace", type=int, default=None, help="start tracing at this frame (0: reset)") parser.add_argument("--snap", default="", help="comma-separated frames to snapshot") 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 dc.zip)", file=sys.stderr) return 2 snaps = [int(x) for x in args.snap.split(",") if x] name = f"{args.driver}-{args.cpu}-t{args.trace}" if args.trace is not None else f"{args.driver}-snap" out = ROOT / "reference" / "mame" / name out.mkdir(parents=True, exist_ok=True) script = out / "run.lua" script.write_text(lua(args.cpu, args.trace, args.frames, snaps), encoding="utf-8") command = [ args.mame, args.driver, "-rompath", args.rompath, "-homepath", str(out), "-snapshot_directory", str(out / "snap"), "-cfg_directory", str(out / "cfg"), "-nvram_directory", str(out / "nvram"), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-log", "-seconds_to_run", str(int(args.frames / 50) + 30), "-autoboot_script", str(script), ] # fmt: skip if args.trace is not None: command += ["-nodrc", "-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") if (out / "error.log").exists(): (out / "error.log").replace(out / "run.log") if (out / "S.trace").exists(): trash = ROOT / "trash" / time.strftime("%Y%m%d-%H%M%S") trash.mkdir(parents=True, exist_ok=True) shutil.move(str(out / "S.trace"), trash / f"{name}-S.trace") print(f"exit {result.returncode}; output in {out.relative_to(ROOT)}") return result.returncode if __name__ == "__main__": sys.exit(main())