#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Run text-screen test ROMs on the board, save each last frame as a PNG, and read the text. For the ROMs that print through libgba's console (hw-test, BSD-3; its lib/source/test.c prints "NAME: PASS value" or "NAME: FAIL value" and then "congratulations!" or "pass: n / total: m"), the console draws ASCII character n with tile n in background 0, so the text is read from the map the picture is drawn from: the screen, not a register. Every other ROM (krom's BIOS tests, GBA-Test-Collection, Hades-Tests: run only) gets its PNG under tests/referee_runs/NAME-board.png to be read by eye, as the detectors' screenshots are (docs/validation.md). python scripts/board_screens.py --bios "$NO_INTRO_DIR/.../(World).zip" --jobs 8 python scripts/board_screens.py ROM.gba ... --frames 900 """ from __future__ import annotations import argparse import json import os 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 gba_python.board import GBA from gba_python.cartridge.rom import GamePakROM from gba_python.ppu.view import png_bytes RUNS = ROOT / "tests" / "referee_runs" HW_TEST = ROOT / "reference" / "src" / "hw-test" # hw-test's text ROMs, rung 0-1's judges (docs/validation.md "hw-test"); the menus # (ppu/status-irq-dma, ppu/ram-access-timing) and picture ROMs are later rungs. HW_TEXT = ( "bus/128kb-boundary", "dma/burst-into-tears", "dma/force-nseq-access", "dma/latch", "dma/start-delay", "haltcnt", "irq/irq-delay", "timer/reload", "timer/start-stop", "timer/tick-before-reload", "ppu/vram-mirror", ) def hw_test_roms() -> list[Path]: return [HW_TEST / name / f"{Path(name).name}.gba" for name in HW_TEXT] def screen_text(board: GBA) -> list[str]: """Background 0's visible 30x20 cells as ASCII (tile n draws character n).""" regs = board.ppu.regs control = regs[4] base = ((control >> 8) & 0x1F) * 0x800 vram = board.bus.vram lines = [] for row in range(20): chars = [] for column in range(30): at = base + (row * 32 + column) * 2 tile = (vram[at] | vram[at + 1] << 8) & 0x3FF chars.append(chr(tile) if 32 <= tile < 127 else " ") lines.append("".join(chars).rstrip()) return lines def verdict(lines: list[str]) -> tuple[str, int, int]: """hw-test's verdict: every result line PASS. Most ROMs end with "congratulations!"; bus/128kb-boundary prints its 18 lines and no summary ("Sorry, not enough space").""" passes = sum(": PASS" in line for line in lines) fails = sum(": FAIL" in line for line in lines) if passes + fails == 0: return "no verdict", 0, 0 if fails == 0: return "pass", passes, passes return "fail", passes, passes + fails def run_one(job: tuple) -> dict: rom, bios, frames, settings = job from board_suite import apply_settings apply_settings(settings) board = GBA(GamePakROM.from_path(rom), bios=bios) board.run_frames(frames) RUNS.mkdir(parents=True, exist_ok=True) png = RUNS / f"{Path(rom).stem}-board.png" png.write_bytes(png_bytes(board.picture())) lines = screen_text(board) status, passes, total = verdict(lines) name = ( str(Path(rom).resolve().relative_to(ROOT)) if Path(rom).resolve().is_relative_to(ROOT) else rom ) return { "rom": name, "status": status, "passes": passes, "total": total, "screen": lines, "png": str(png.relative_to(ROOT)), } def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("roms", nargs="*", help="default: hw-test's text ROMs") parser.add_argument("--bios", default="free") parser.add_argument("--frames", type=int, default=600) parser.add_argument("--jobs", type=int, default=4) parser.add_argument("--quiet", action="store_true", help="no screen text") 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") roms = args.roms or [str(p) for p in hw_test_roms()] with ProcessPoolExecutor(max_workers=args.jobs) as pool: results = list(pool.map(run_one, [(r, args.bios, args.frames, args.set) for r in roms])) for result in results: print(f"{result['status']:10} {result['passes']}/{result['total']:<3} {result['rom']}") if not args.quiet: for line in result["screen"]: if line.strip(): print(" |", line) good = sum(r["status"] == "pass" for r in results) print(f"{good}/{len(results)} pass") if args.json: args.json.write_text(json.dumps(results, indent=1) + "\n", encoding="utf-8") return 0 if __name__ == "__main__": sys.exit(main())