#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run the PC-FX in MAME 0.285 (a closed-box detector, tier 5). Runs `mame pcfx -bios ` headless (optionally with a disc: --cdrom, a .cue or .chd), for --frames frames, counts the CPU's writes into the I/O-space ranges named by --tap (a write tap on the V810's `io` space; the chips' ports, docs/memory-map.md), saves a PNG at the end (--snap) and exits. The BIOS is read from $MAME_ROMPATH; nothing is written outside tests/mame_runs/ (the run's cwd; git-ignored). MAME_ROMPATH=... python -I scripts/mame_run.py --bios v101 --frames 600 --snap \ --tap king=600-6FF --tap vdc-a=400-4FF --tap vdc-b=500-5FF --tap mixer=300-3FF """ from __future__ import annotations import argparse import os import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUNS = ROOT / "tests" / "mame_runs" LUA = """ space = manager.machine.devices[':maincpu'].spaces['{space}'] counts = {{}} taps = {{}} {taps} frames = 0 sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {stop} then for name, n in pairs(counts) do print(string.format("TAP %s %d", name, n)) end print(string.format("TIME %.6f", manager.machine.time:as_double())) {snap} manager.machine:exit() end end) """ TAP = """counts['{name}'] = 0 taps['{name}'] = space:install_write_tap(0x{lo}, 0x{hi}, '{name}', function(offset, data, mask) counts['{name}'] = counts['{name}'] + 1 end)""" def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument("--machine", default="pcfx", choices=["pcfx", "pcfxga"]) parser.add_argument("--bios", default="v101") parser.add_argument("--cdrom", type=Path) parser.add_argument("--frames", type=int, default=300) parser.add_argument("--snap", action="store_true") parser.add_argument("--space", default="io", choices=["io", "program"]) parser.add_argument("--tap", action="append", default=[], help="name=LO-HI (hex)") parser.add_argument("--mame", default="mame") args = parser.parse_args() rompath = os.environ.get("MAME_ROMPATH") if not rompath: print("set MAME_ROMPATH (the rompath holding pcfx.zip)", file=sys.stderr) return 2 RUNS.mkdir(parents=True, exist_ok=True) taps = [] for spec in args.tap: name, span = spec.split("=") lo, hi = span.split("-") taps.append(TAP.format(name=name, lo=lo, hi=hi)) snap = "manager.machine.video:snapshot()" if args.snap else "" script = RUNS / "run.lua" script.write_text(LUA.format(space=args.space, taps="\n".join(taps), stop=args.frames, snap=snap), encoding="utf-8") command = ["nice", args.mame, args.machine] if args.machine == "pcfx": command += ["-bios", args.bios] command += [ "-rompath", rompath, "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-snapshot_directory", str(RUNS / "snap"), "-autoboot_script", str(script), "-seconds_to_run", str(args.frames // 60 + 5), ] if args.cdrom: command += ["-cdrom", str(args.cdrom.resolve())] result = subprocess.run(command, cwd=RUNS, capture_output=True, text=True, check=False) lines = [line for line in result.stdout.splitlines() if line.startswith(("TAP", "TIME"))] print("\n".join(lines) if lines else (result.stdout[-2000:] + result.stderr[-2000:])) return 0 if lines else 1 if __name__ == "__main__": sys.exit(main())