# SPDX-License-Identifier: MIT """Test-ROM readers: what a self-checking ROM wrote to VRAM. gilyon's cputest/spctest write ASCII to the low bytes of BG1 map words ("Success" or "Failed", then "Test number: NNNN") (their main.asm). krom's CPUTest ROMs print "PASS" through VMDATAL for each passed check and loop printing "FAIL" on the first failure (their CPU*.asm: ``PrintText``). The test ROMs are fetched by scripts/fetch_references.py into tests/test_roms/, never committed. """ from __future__ import annotations import re from pathlib import Path from snes_python.board import SNES from snes_python.cartridge import Cartridge ROOT = Path(__file__).resolve().parent / "test_roms" def load(path: Path, **kwargs) -> SNES: return SNES(Cartridge(path.read_bytes()), **kwargs) def vram_text(snes: SNES) -> str: """The low byte of every VRAM word as printable ASCII ('.' otherwise).""" return "".join( chr(w & 0xFF) if 0x20 <= (w & 0xFF) < 0x7F else "." for w in snes.ppu.vram ) def gilyon(path: Path, max_frames: int = 1200) -> tuple[str, int | None, int]: """Run a gilyon ROM to its verdict: ("Success"|"Failed"|"", test number, frames).""" snes = load(path) for frame in range(1, max_frames + 1): snes.run_frame() if frame % 30 == 0: text = vram_text(snes) for verdict in ("Success", "Failed"): if verdict in text: snes.run_frames(2) text = vram_text(snes) m = re.search(r"Test number:.([0-9A-F]{4})", text) return verdict, int(m.group(1), 16) if m else None, frame return "", None, max_frames class TokenTap: """Counts "PASS"/"FAIL" written byte by byte through $2118.""" def __init__(self, snes: SNES) -> None: self.window = "" self.passes = 0 self.fails = 0 ppu = snes.ppu write = ppu.write def tapped(address: int, value: int) -> None: if address & 0x3F == 0x18: self.window = (self.window + chr(value))[-4:] if self.window == "PASS": self.passes += 1 elif self.window == "FAIL": self.fails += 1 write(address, value) ppu.write = tapped # type: ignore[method-assign] def krom(path: Path, max_frames: int = 3000, quiet: int = 120) -> tuple[int, int, int]: """Run a krom CPUTest ROM until no new token for ``quiet`` frames. Returns (passes, fails, frames). A failing ROM loops on FAIL, so any fail count above zero is a failure. """ snes = load(path) tap = TokenTap(snes) last, still = -1, 0 for frame in range(1, max_frames + 1): snes.run_frame() seen = tap.passes + tap.fails if tap.fails: return tap.passes, tap.fails, frame if seen == last: still += 1 if still >= quiet and seen: return tap.passes, tap.fails, frame else: last, still = seen, 0 return tap.passes, tap.fails, max_frames