#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run a Virtual Boy cartridge in MAME 0.285 (a closed-box detector, tier 5). Runs `mame vboy -cart ` headless for --frames frames, counts the CPU's writes into the address ranges named by --tap (a write tap on the main CPU's program space), saves a PNG of both eyes' screens at the end (--snap) and exits. Nothing is written outside tests/mame_runs/ (the run's cwd; git-ignored). The cartridge is read in place. python -I scripts/mame_run.py --cart "$NO_INTRO_DIR/Nintendo - Virtual Boy/.zip" \ --frames 600 --snap --tap vip-io=0005F800-0005F87F --tap vsu=01000000-010007FF """ from __future__ import annotations import argparse 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['program'] 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("--cart", type=Path, required=True) parser.add_argument("--frames", type=int, default=300) parser.add_argument("--snap", action="store_true") parser.add_argument("--tap", action="append", default=[], help="name=LO-HI (hex)") parser.add_argument("--mame", default="mame") args = parser.parse_args() 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(taps="\n".join(taps), stop=args.frames, snap=snap), encoding="utf-8") command = [ "nice", args.mame, "vboy", "-cart", str(args.cart.resolve()), "-video", "none", "-sound", "none", "-nothrottle", "-noreadconfig", "-skip_gameinfo", "-snapshot_directory", str(RUNS / "snap"), "-autoboot_script", str(script), "-seconds_to_run", str(args.frames // 50 + 5), ] 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())