# SPDX-License-Identifier: MIT """Trace the V30MZ in MAME 0.285 from reset (a closed-box detector, tier 5). MAME_ROMPATH=... python -I scripts/mame_trace.py --machine wswan --frames 1 Runs MAME headless on a WonderSwan machine with no cartridge, so the trace is the boot ROM's own instruction stream from the reset vector. Each line is MAME's disassembly (NEC mnemonics) prefixed with the registers named by --regs (MAME's symbols for its v30mz device: AW BW CW DW SP BP IX IY PS SS DS0 DS1 PSW IP) and MAME's running cycle count (`totalcycles`), so per-instruction clocks are the differences between lines. Output goes to reference/mame/trace.log (git-ignored); the run's cwd is reference/mame/ so error.log, cfg/ and nvram/ land there too. The boot ROM is read from $MAME_ROMPATH; nothing is copied into the repository. """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUN = ROOT / "reference" / "mame" LUA = """ local dbg = manager.machine.debugger dbg.visible_cpu = manager.machine.devices[":maincpu"] if {trace} then dbg:command('trace trace.log,maincpu,noloop,{{tracelog "{fmt}",{regs}}}') end dbg:command('go') frames = 0 button_a = manager.machine.ioport.ports[":BUTTONS"].fields["Button A"] notifier = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {press} then button_a:set_value(1) end if frames == {press} + 6 then button_a:set_value(0) end if frames == {frames} then dbg:command('trace off,maincpu') {snap} print(string.format("FRAMES %d TIME %.6f", frames, manager.machine.time:as_double())) manager.machine:exit() end end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--machine", default="wswan", choices=("wswan", "wscolor", "pockchv2")) parser.add_argument("--frames", type=int, default=1) parser.add_argument("--regs", default="AW,BW,CW,DW,SP,BP,IX,IY,PS,SS,DS0,DS1,PSW") parser.add_argument("--cart", type=Path, help="a test program (.ws/.wsc, or a zip holding one) to boot") parser.add_argument("--no-trace", action="store_true", help="run without the instruction trace") parser.add_argument("--snap", action="store_true", help="save a PNG of the last frame under reference/mame/snap/") parser.add_argument("--press-a", type=int, default=-100, help="hold button A for 6 frames from this frame") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (the wswan / wscolor sets)", file=sys.stderr) return 2 RUN.mkdir(parents=True, exist_ok=True) regs = [r for r in args.regs.split(",") if r] + ["totalcycles"] fmt = " ".join(f"{r}=%04X" for r in regs[:-1]) + " T=%d " (RUN / "trace.lua").write_text(LUA.format(fmt=fmt, regs=",".join(regs), frames=args.frames, press=args.press_a, trace="false" if args.no_trace else "true", snap="manager.machine.video:snapshot()" if args.snap else ""), encoding="utf-8") # fmt: skip command = [ "nice", args.mame, args.machine, "-rompath", rompath, "-homepath", ".", "-cfg_directory", "cfg", "-nvram_directory", "nvram", "-snapshot_directory", "snap", "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-debug", "-debugger", "none", "-autoboot_script", "trace.lua", "-seconds_to_run", str(args.frames // 75 + 5), ] if args.cart: command += ["-cart", str(args.cart.resolve())] result = subprocess.run(command, cwd=RUN, capture_output=True, text=True, check=False) done = [line for line in result.stdout.splitlines() if line.startswith("FRAMES")] if not done: print(result.stdout[-2000:], result.stderr[-2000:], sep="\n", file=sys.stderr) return 1 print(done[0]) if not args.no_trace: with (RUN / "trace.log").open("rb") as handle: count = sum(1 for _ in handle) print(f"{count} instructions in reference/mame/trace.log") return 0 if __name__ == "__main__": sys.exit(main())