#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run thumb_undefined.gba on gba-python's referee drivers and decode the SRAM. The drivers (mGBA and NanoBoyAdvance 1.8.3, headless) are built by gba-python's scripts/build_referees.py, in the gba-python checkout named by GBA_PYTHON_ROOT (default: beside this repository). The retail BIOS is read in place from the No-Intro zip under NO_INTRO_DIR (unpacked to a temporary file only, never kept); pass --bios to use another dump. Each run saves the last frame next to this file as referee-.png. python tests/hw/thumb_undefined/run_referees.py python tests/hw/thumb_undefined/run_referees.py --decode my_cart.sav """ from __future__ import annotations import argparse import os import struct import subprocess import sys import tempfile import zipfile from pathlib import Path HERE = Path(__file__).resolve().parent ROM = HERE / "thumb_undefined.gba" GBA = Path(os.environ.get("GBA_PYTHON_ROOT", HERE.parents[3] / "gba-python")) DRIVERS = { "mgba": GBA / "reference/build/mgba/mgba_driver", "nba": GBA / "reference/build/nba/nba_driver", } BIOS_ZIP = ( Path(os.environ.get("NO_INTRO_DIR", ".")) / "Nintendo - Game Boy Advance" / "[BIOS] Nintendo Game Boy Advance Boot ROM (World).zip" ) FRAMES = 600 NAMES = { "F": "fell through (no-op)", "B": "branch", "T": "undefined trap", "X": "other (see pc, cpsr)", "?": "fell through, odd timing", "P": "pending (never finished)", } def decode(sram: bytes) -> list[str]: if sram[:4] != b"TUND": return ["no TUND header: the ROM did not reach its first probe"] count = sram[5] lines = [ f"layout v{sram[4]}, {count} probes, {'done' if sram[6:7] == b'D' else 'NOT finished'}" ] lines.append("opcode out cycles r7 pc cpsr lr_und r6 meaning") for i in range(count): base = 0x10 + 32 * i opcode, outcome, state, cycles, pc, cpsr, lr_und, r7, r6, _ret = struct.unpack_from( " list[str]: driver = DRIVERS[emu] if not driver.exists(): return [f"{emu}: driver not built ({driver})"] sram = scratch / f"{emu}.sav" png = HERE / f"referee-{emu}.png" command = [ str(driver), str(ROM), "--bios", str(bios), "--frames", str(FRAMES), "--sram", str(sram), "--png", str(png), ] subprocess.run(command, check=True, capture_output=True) return [f"== {emu} (screen: {png.name})", *decode(sram.read_bytes() if sram.exists() else b"")] 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=tuple(DRIVERS), action="append", help="default: both") parser.add_argument( "--decode", type=Path, help="only decode this .sav (e.g. from a flash cart)" ) 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 list(DRIVERS): print("\n".join(run(emu, bios, scratch))) return 0 if __name__ == "__main__": sys.exit(main())