# SPDX-License-Identifier: MIT """Run MAME 0.285's Odyssey 2 / Videopac driver headless, as a closed-box detector. python -I scripts/mame_trace.py --frames 3 # trace the 8048, BIOS only python -I scripts/mame_trace.py --driver videopacp --frames 3 # the G7400 python -I scripts/mame_trace.py --snap 4 # no trace: run, save a PNG python -I scripts/mame_trace.py --cart PATH.zip --frames 3 # with a cartridge MAME is only run, never read: its source (GPL-2.0+ as a whole; per-file BSD-3 lines not opened, by the Phase 0 brief) is not fetched into this repository. The romsets come from MAME_ROMPATH (videopac.zip, odyssey2.zip, videopacp.zip, ... hold the 1 KB BIOS images; i8244.zip and ef9340_1.zip hold MAME's character ROM dumps). Output goes to reference/mame// (git-ignored): trace.log one line per 8048 instruction from the debugger's trace (MAME's own disassembly), and in error.log one register line per instruction: "S pc a psw p1 p2 tc totalcycles" error.log also one line per external data access (MOVX, the 8244/RAM/keyboard window), "W addr data totalcycles" / "R addr data totalcycles" snap/ a PNG at the end of the run Traps (found while writing this, see docs/mame-oracle.md): the 8048's external data accesses are MAME's I/O space, so the watchpoint command is wpiset, not wpset; the PSW is "curflags" (there is no "psw" symbol, and naming a missing symbol makes the whole trace action print nothing). """ from __future__ import annotations import argparse import os import shutil import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] DRIVERS = ("videopac", "odyssey2", "videopacf", "videopacp", "odyssey3", "jopac") def lua(frames: int, trace: bool, io: bool) -> str: lines: list[str] = [] if trace: lines += [ "local dbg = manager.machine.debugger", 'dbg.visible_cpu = manager.machine.devices[":maincpu"]', "dbg:command('trace trace.log,:maincpu,noloop," '{logerror "S %03X %02X %02X %02X %02X %02X %d\\\\n",curpc,a,curflags,p1,p2,tc,totalcycles}\')', ] if io: lines += [ "dbg:command('wpiset 0,100,w,1," '{logerror "W %02X %02X %d\\\\n",wpaddr,wpdata,totalcycles; g}\')', "dbg:command('wpiset 0,100,r,1," '{logerror "R %02X %02X %d\\\\n",wpaddr,wpdata,totalcycles; g}\')', ] lines.append('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="videopac", choices=DRIVERS) 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("--no-io", action="store_true", help="do not log MOVX accesses") parser.add_argument("--cart", help="a cartridge image (read in place, never copied)") parser.add_argument("--rompath", default=os.environ.get("MAME_ROMPATH")) parser.add_argument("--mame", default=shutil.which("mame") or "/usr/games/mame") args = parser.parse_args() if not args.rompath: print("set MAME_ROMPATH to the MAME non-merged ROM folder", file=sys.stderr) return 2 if bool(args.frames) == bool(args.snap): print("give exactly one of --frames N or --snap SECONDS", file=sys.stderr) return 2 trace = args.frames > 0 out = ROOT / "reference" / "mame" / args.driver out.mkdir(parents=True, exist_ok=True) for name in ("error.log", "trace.log"): stale = out / name if stale.exists(): stale.replace(out / (name + ".prev")) frames = args.frames if trace else max(1, round(args.snap * 60)) script = out / "run.lua" script.write_text(lua(frames, trace, not args.no_io)) cmd = [ args.mame, args.driver, "-rompath", args.rompath, "-homepath", ".", "-cfg_directory", "cfg", "-nvram_directory", "nvram", "-snapshot_directory", "snap", "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-autoboot_script", str(script), ] if args.cart: cmd += ["-cart", str(Path(args.cart).resolve())] if trace: cmd += ["-debug", "-debugger", "none", "-log"] print(" ".join(cmd[:2]), "...", f"(cwd {out.relative_to(ROOT)})") result = subprocess.run(cmd, cwd=out, timeout=600) log = out / "error.log" if trace and log.exists(): kinds = {"S": 0, "W": 0, "R": 0} for line in log.read_text(errors="replace").splitlines(): head = line[:2] if head in ("S ", "W ", "R "): kinds[head[0]] += 1 print(f"instructions {kinds['S']} MOVX writes {kinds['W']} MOVX reads {kinds['R']}") snaps = sorted((out / "snap").rglob("*.png")) if (out / "snap").exists() else [] if snaps: print("snapshot", snaps[-1].relative_to(ROOT)) return result.returncode if __name__ == "__main__": sys.exit(main())