#!/usr/bin/env python3 """Run dma_stop.gba on mGBA, NanoBoyAdvance 1.8.3 and this board, and decode the SRAM. The referee drivers are built by scripts/build_referees.py. The retail BIOS is read in place from the No-Intro zip (unpacked to a temporary file for the drivers only, never kept); pass --bios for another dump. Each referee run saves the three result pages next to this file as referee--p.png. The board has no picture until rung 3, so only its SRAM is decoded. python tests/hw/dma_stop/run_referees.py python tests/hw/dma_stop/run_referees.py --emu board python tests/hw/dma_stop/run_referees.py --decode my_cart.sav """ from __future__ import annotations import argparse import struct import subprocess import sys import tempfile import zipfile from pathlib import Path HERE = Path(__file__).resolve().parent ROM = HERE / "dma_stop.gba" ROOT = HERE.parents[2] DRIVERS = { "mgba": ROOT / "reference/build/mgba/mgba_driver", "nba": ROOT / "reference/build/nba/nba_driver", } BIOS_ZIP = Path( "/data/emu/source/myrient.erista.me/files/No-Intro/Nintendo - Game Boy Advance/" "[BIOS] Nintendo Game Boy Advance Boot ROM (World).zip" ) # The ROM finishes well before frame 500 everywhere; its pages then turn every # 180 frames, so three runs 180 frames apart capture all three. FRAMES = (520, 700, 880) BOARD_FRAMES = 520 def decode(sram: bytes) -> list[str]: if sram[:4] != b"DMAS": return ["no DMAS header: the ROM did not reach its first probe"] count = sram[5] done = sram[6:7] == b"D" lines = [f"layout v{sram[4]}, {count} probes, {'done' if done else 'NOT finished'}"] lines.append("id slot t1 t2 d ov waitcnt runs") for i in range(count): base = 0x10 + 16 * i ident = sram[base : base + 3].decode("ascii", "replace") slot, t1, t2, ov, status, stable, waitcnt = struct.unpack_from(" list[str]: driver = DRIVERS[emu] if not driver.exists(): return [f"{emu}: driver not built ({driver})"] sram = scratch / f"{emu}.sav" for page, frames in enumerate(FRAMES, 1): png = HERE / f"referee-{emu}-p{page}.png" command = [str(driver), str(ROM), "--bios", str(bios), "--frames", str(frames)] command += ["--sram", str(sram), "--png", str(png)] subprocess.run(command, check=True, capture_output=True) return [f"== {emu}", *decode(sram.read_bytes() if sram.exists() else b"")] def run_board(bios: Path) -> list[str]: from gba_python import bios as bios_module from gba_python.board import GBA gba = GBA(bios_module.find(str(bios)), ROM.read_bytes()) for _ in range(BOARD_FRAMES): gba.run_frame() return [f"== board ({gba.bios.label} BIOS)", *decode(bytes(gba.cartridge.sram.data))] def main() -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument( "--bios", type=Path, default=BIOS_ZIP, help="BIOS dump (.bin, or .zip read in place)" ) parser.add_argument( "--emu", choices=(*DRIVERS, "board"), action="append", help="default: all three" ) parser.add_argument("--decode", type=Path, help="only decode this .sav (e.g. a flash cart's)") args = parser.parse_args() if args.decode: print("\n".join(decode(args.decode.read_bytes()))) return 0 with tempfile.TemporaryDirectory() as tmp: scratch = Path(tmp) bios = args.bios if bios.suffix == ".zip": with zipfile.ZipFile(bios) as archive: name = next(n for n in archive.namelist() if not n.endswith("/")) bios = scratch / "bios.bin" bios.write_bytes(archive.read(name)) for emu in args.emu or [*DRIVERS, "board"]: lines = run_board(bios) if emu == "board" else run_driver(emu, bios, scratch) print("\n".join(lines), flush=True) return 0 if __name__ == "__main__": sys.exit(main())