#!/usr/bin/env python3 """Run MAME's `jaguar` driver headless and log an instruction trace. The recipe of docs/mame-oracle.md, as it was run on 2026-11-11 with MAME 0.285. MAME is a detector for this board, never a judge: its own driver header calls console support "very poor". python -I scripts/mame_trace.py # boot ROM, no cart, 68000 trace, 2 s python -I scripts/mame_trace.py --cpu gpu --seconds 1 # the GPU's trace instead python -I scripts/mame_trace.py --cart X.j64 --no-trace --seconds 5 # snapshot only Each run gets a fresh directory tests/mame_traces// (git-ignored) holding trace.lua, error.log and snap/. The boot ROM is read in place from the MAME set (``--rompath``; the default is the local non-merged set). A zipped cart is unpacked into the run directory, which is why that directory is ignored. Trace lines, one per instruction, before it executes: M PC SR D0 D1 A0 A1 A7 totalcycles beamy beamx frame (--cpu maincpu) G PC FLAGS R0 R1 R2 R3 R28 R29 R30 R31 totalcycles (--cpu gpu; D for dsp) """ from __future__ import annotations import argparse import os import subprocess import sys import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] ROMPATH = os.environ.get("MAME_ROMPATH") M68K = ( 'trace cpu.trace,:maincpu,noloop,{logerror "M %X %X %X %X %X %X %X %d %d %d %d\\\\n",' "pc,sr,d0,d1,a0,a1,sp,totalcycles,beamy,beamx,frame}" ) JRISC = ( 'trace cpu.trace,:%s,noloop,{logerror "%s %%X %%X %%X %%X %%X %%X %%X %%X %%X %%X %%d\\\\n",' "pc,flags,r0,r1,r2,r3,r28,r29,r30,r31,totalcycles}" ) def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--driver", default="jaguar") parser.add_argument("--cart", type=Path, help=".j64/.rom/.jag image, or a zip holding one") parser.add_argument("--cpu", choices=("maincpu", "gpu", "dsp"), default="maincpu") parser.add_argument("--seconds", type=int, default=2, help="emulated seconds (integer)") parser.add_argument("--no-trace", action="store_true", help="snapshot at exit only") parser.add_argument( "--frames", help="A,B: trace only from frame A up to frame B (the GPU logs ~10 MB per frame)", ) parser.add_argument("--rompath", default=ROMPATH) parser.add_argument("--name", help="run directory name (default: derived)") args = parser.parse_args() if not args.rompath: raise SystemExit("set MAME_ROMPATH to the MAME (non-merged) ROM folder, or pass --rompath") stem = args.cart.stem if args.cart else "nocart" name = args.name or f"{args.driver}-{stem}-{'snap' if args.no_trace else args.cpu}" out = ROOT / "tests" / "mame_traces" / name if out.exists(): print(f"{out} exists: choose --name (runs are never overwritten)") return 1 out.mkdir(parents=True) command = [ "mame", args.driver, "-rompath", args.rompath, "-homepath", str(out), "-snapshot_directory", "snap", "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-seconds_to_run", str(args.seconds), ] if args.cart: cart = args.cart if cart.suffix == ".zip": with zipfile.ZipFile(cart) as archive: member = archive.namelist()[0] archive.extract(member, out) cart = out / member command += ["-cart", str(cart)] if not args.no_trace: line = M68K if args.cpu == "maincpu" else JRISC % (args.cpu, args.cpu[0].upper()) script = out / "trace.lua" if args.frames: first, last = (int(part) for part in args.frames.split(",")) # The notifier must live in a global or Lua collects it. body = ( "local dbg = manager.machine.debugger\n" f"dbg.visible_cpu = manager.machine.devices[':{args.cpu}']\n" "local frame = 0\n" "dbg:command('go')\n" "frame_tap = emu.add_machine_frame_notifier(function()\n" " frame = frame + 1\n" f" if frame == {first} then dbg:command('{line}') end\n" f" if frame == {last} then dbg:command('trace off,:{args.cpu}') end\n" "end)\n" ) else: # Symbols in the trace action resolve against the *visible* CPU: without # this line a GPU/DSP action names registers the 68000 lacks and logs nothing. body = ( "local dbg = manager.machine.debugger\n" f"dbg.visible_cpu = manager.machine.devices[':{args.cpu}']\n" f"dbg:command('{line}')\n" "dbg:command('go')\n" ) script.write_text(body, encoding="utf-8") command += ["-debug", "-debugger", "none", "-log", "-autoboot_script", str(script)] print(" ".join(command)) result = subprocess.run(command, cwd=out, check=False) log = out / "error.log" if log.exists(): print(f"{log}: {log.stat().st_size} bytes") return result.returncode if __name__ == "__main__": sys.exit(main())