#!/usr/bin/env python3 """Run a GBA test ROM on MAME 0.285's ``gba`` driver and keep the last frame. MAME is a detector here, never a judge (docs/mame-oracle.md): its GBA driver is old, and it does not boot with the free BIOS at all (Cult-of-GBA's stops on its splash screen), so this needs your own dump of Nintendo's BIOS (BYOR). Pass a ``.zip`` or ``.bin``; it is copied as ``gba/gba.bin`` into a temporary ROM path for the run and nowhere else. Each run happens in ``tests/mame_runs//`` (gitignored): the frame as ``snap/gba/0000.png`` and ``result.txt`` with the registers at that frame. Read the screen, not r12: when the frame ends the CPU is usually inside the BIOS's interrupt handler, where r12 is not the test's. python scripts/mame_run.py --bios ~/bios/gba_bios.zip reference/src/gba-tests/arm/arm.gba """ from __future__ import annotations import argparse import shutil import subprocess import sys import tempfile import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] MAME = "/usr/games/mame" # Notifier subscriptions are unsubscribed when garbage-collected: keep it global. LUA = """\ local cpu = manager.machine.devices[":maincpu"] local frames = 0 frame_sub = emu.add_machine_frame_notifier(function() frames = frames + 1 if frames == {frames} then local s = cpu.state local f = io.open("result.txt", "w") f:write(string.format("frame %d PC %08X CPSR %08X R12 %08X\\n", frames, s["PC"].value, s["CPSR"].value, s["R12"].value)) f:close() manager.machine.video:snapshot() manager.machine:exit() end end) """ def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("roms", nargs="+", type=Path) parser.add_argument("--bios", type=Path, required=True, help="your BIOS dump (.bin or .zip)") parser.add_argument("--frames", type=int, default=600, help="frames before the snapshot") parser.add_argument("--mame", default=MAME) args = parser.parse_args() with tempfile.TemporaryDirectory() as scratch: bios = Path(scratch) / "gba" / "gba.bin" bios.parent.mkdir() if args.bios.suffix == ".zip": with zipfile.ZipFile(args.bios) as archive: bios.write_bytes(archive.read(archive.namelist()[0])) else: shutil.copyfile(args.bios, bios) for rom in args.roms: out = ROOT / "tests" / "mame_runs" / rom.stem if out.exists(): shutil.rmtree(out) out.mkdir(parents=True) shutil.copyfile(rom, out / rom.name) (out / "run.lua").write_text( LUA.replace("{frames}", str(args.frames)), encoding="utf-8" ) subprocess.run( [ args.mame, "gba", "-cart", rom.name, "-rompath", scratch, "-homepath", ".", "-snapshot_directory", "snap", "-video", "none", "-sound", "none", "-nothrottle", "-skip_gameinfo", "-autoboot_script", "run.lua", ], cwd=out, capture_output=True, check=False, timeout=600, ) result = out / "result.txt" print(rom.stem, result.read_text().strip() if result.exists() else "no result") return 0 if __name__ == "__main__": sys.exit(main())