#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run endrift's mGBA test suite (MIT) on the board and read each sub-suite's score from the screen. The suite prints "passes/total" at row 1, column 21 of its text grid once a sub-suite has run (suite src/main.c ``runSuite``: ``sprintf(TEXT_LOC(1, 21), "%4u/%-4u", ...)``), drawn in background 1 from the map at screen block 1 with tile number = character - ' ' (``updateTextGrid``). That is the verdict, read from what the screen shows (the map entries the picture is drawn from); the "END:" lines the detectors' runner reads go through mGBA's debug port, which a GBA does not have. Failures are also kept in SRAM as text (``savprintf``) and are printed with ``--fails``. Each sub-suite is chosen as a player would: wait out the BIOS's boot, press Down to move the cursor, then A (scripts/referee_run.py's key script, the same frames). python scripts/board_suite.py --bios free --jobs 13 python scripts/board_suite.py --bios "$NO_INTRO_DIR/.../[BIOS] ... (World).zip" 0 2 3 """ from __future__ import annotations import argparse import json import os import re import sys from concurrent.futures import ProcessPoolExecutor from pathlib import Path ROOT = Path(__file__).resolve().parents[1] # GBA_PYTHON_SRC: a copy of src/ to run instead (scripts/seeded_faults.py). sys.path.insert(0, os.environ.get("GBA_PYTHON_SRC", str(ROOT / "src"))) sys.path.insert(0, str(Path(__file__).resolve().parent)) from referee_run import BOOT_FRAMES, KEY_A, KEY_DOWN, SUITE, SUITES from gba_python.board import GBA from gba_python.cartridge.rom import GamePakROM from gba_python.ppu.timing import FRAME_CYCLES SCORE = re.compile(r"(\d+)\s*/\s*(\d+)") def screen_line(board: GBA, row: int) -> str: """Row ``row`` of the suite's text grid as the screen draws it (BG1, screen block 1).""" vram = board.bus.vram base = 0x800 + row * 64 chars = [] for column in range(32): entry = vram[base + column * 2] | vram[base + column * 2 + 1] << 8 chars.append(chr((entry & 0x3FF) + 32)) return "".join(chars) def apply_settings(settings: list[str]) -> None: """``module.NAME=value`` overrides, for trying a fitted constant (docs/timing.md).""" import importlib for setting in settings: target, value = setting.split("=") module, name = target.rsplit(".", 1) setattr(importlib.import_module(module), name, int(value, 0)) def run_one(args: tuple) -> dict: index, bios, frames, settings = args apply_settings(settings) board = GBA(GamePakROM.from_path(SUITE), bios=bios) keys = {0: 0} frame = BOOT_FRAMES for _ in range(index): keys[frame], keys[frame + 2] = KEY_DOWN, 0 frame += 4 keys[frame], keys[frame + 2] = KEY_A, 0 pressed_at = frame score = None for f in range(frames): if f in keys: board.set_keys(keys[f]) board.run_until((f + 1) * FRAME_CYCLES) if f > pressed_at + 10 and f % 30 == 0: match = SCORE.search(screen_line(board, 1)[21:]) if match: score = (int(match.group(1)), int(match.group(2))) break log = board.backup.save_data().split(b"\x00")[0].decode("latin-1") fails = [line for line in log.splitlines() if "FAIL" in line or line.startswith("Timing test")] return { "suite": SUITES[index], "passes": score[0] if score else None, "total": score[1] if score else None, "frames": f + 1, "fails": fails, } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("suites", nargs="*", type=int, help="indices (default: 0-12)") parser.add_argument("--bios", default="free") parser.add_argument("--frames", type=int, default=20000, help="give up after this many") parser.add_argument("--jobs", type=int, default=4) parser.add_argument("--fails", action="store_true", help="print the failure lines") parser.add_argument("--json", type=Path) parser.add_argument("--set", action="append", default=[], help="module.NAME=value") args = parser.parse_args() os.environ.setdefault("PYPY_GC_NURSERY", "1M") chosen = args.suites or list(range(13)) # 13 (Video) is interactive: no score work = [(i, args.bios, args.frames, args.set) for i in chosen] with ProcessPoolExecutor(max_workers=args.jobs) as pool: results = list(pool.map(run_one, work)) for result in results: shown = f"{result['passes']}/{result['total']}" if result["total"] else "no score" print(f"{result['suite']:26} {shown:12} ({result['frames']} frames)") if args.fails: for line in result["fails"]: print(" ", line) if args.json: args.json.write_text(json.dumps(results, indent=1) + "\n", encoding="utf-8") return 0 if __name__ == "__main__": sys.exit(main())