# SPDX-License-Identifier: MIT """Run MAME 0.285 headless on a WonderSwan machine as a closed-box detector (tier 5). MAME_ROMPATH=... python -I scripts/mame_trace.py --machine wswan --frames 3 MAME_ROMPATH=... NO_INTRO_DIR=... python -I scripts/mame_trace.py --machine wscolor \ --cart "Bandai - WonderSwan Color/.zip" --frames 120 --trace-frames 0 --snap What it writes, all under reference/mame/run/ (git-ignored): `trace.log`, MAME's debugger trace of the V30MZ from reset for --trace-frames frames, each line 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; NEC names, docs/mame-oracle.md); `io.log`, every I/O port write (frame, port, value) through a Lua write tap on the CPU's I/O space; and with --snap a PNG of the last frame. The run's cwd is that directory (error.log, cfg/, nvram/ land there). The boot ROM comes from $MAME_ROMPATH; a cartridge, if any, is read from inside its No-Intro zip under $NO_INTRO_DIR. Nothing is copied into the repository. """ 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] RUN = ROOT / "reference" / "mame" / "run" LUA = """ local dbg = manager.machine.debugger local cpu = manager.machine.devices[":maincpu"] dbg.visible_cpu = cpu io_log = io.open("io.log", "w") frames = 0 io_space = cpu.spaces["io"] io_tap = io_space:install_write_tap(0x00, 0xff, "iolog", function(offset, data, mask) io_log:write(string.format("%d %02X %04X %04X\\n", frames, offset, data, mask)) end) if {trace_frames} > 0 then dbg:command('trace trace.log,maincpu,noloop{action}') end dbg:command('go') notifier = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {trace_frames} then dbg:command('trace off,maincpu') end if frames == {frames} then {snap} io_log:close() 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("--cart", help="a zip path relative to $NO_INTRO_DIR") parser.add_argument("--frames", type=int, default=3, help="frames to run in all") parser.add_argument("--trace-frames", type=int, default=1, help="frames to trace from reset (0: none)") parser.add_argument("--regs", default="AW,BW,CW,DW,SP,PS,DS0,SS,PSW") parser.add_argument("--snap", action="store_true", help="save a PNG of the last 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) for old in ("trace.log", "io.log"): # an earlier run's logs go aside, never deleted if (RUN / old).exists(): aside = ROOT / "trash" / time.strftime("mame-%Y%m%d-%H%M%S") aside.mkdir(parents=True, exist_ok=True) shutil.move(str(RUN / old), aside / old) regs = [r for r in args.regs.split(",") if r] action = "" if regs: fmt = " ".join(f"{r}=%04X" for r in regs) + " " action = ',{tracelog "' + fmt + '",' + ",".join(regs) + "}" snap = "manager.machine.video:snapshot()" if args.snap else "" script = RUN / "trace.lua" script.write_text(LUA.format(frames=args.frames, trace_frames=args.trace_frames, action=action, snap=snap), 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: base = os.environ.get("NO_INTRO_DIR") if not base: print("set NO_INTRO_DIR to load a cartridge", file=sys.stderr) return 2 command += ["-cart", str(Path(base) / args.cart)] 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]) for name in ("trace.log", "io.log"): path = RUN / name if path.exists(): with path.open("rb") as handle: print(f"{sum(1 for _ in handle)} lines in reference/mame/run/{name}") return 0 if __name__ == "__main__": sys.exit(main())