# SPDX-License-Identifier: MIT """Milestone 3's acceptance through the whole board: the S-SMP on the S-CPU's timeline. gilyon's spctest (557 tests) and krom's 7 CPUTest/SPC700 ROMs (TEST ROM). The IPL ROM is bring-your-own: ``SNES_IPL`` (a 64-byte file) or ``snes.zip`` in ``MAME_ROMPATH``; the tests skip without one. """ from __future__ import annotations import hashlib import os import re import zipfile from pathlib import Path import pytest from roms import ROOT, TokenTap, vram_text from snes_python.board import SNES from snes_python.cartridge import Cartridge pytestmark = pytest.mark.slow IPL_SHA1 = "97e352553e94242ae823547cd853eecda55c20f0" # MAME's spc700.rom def _ipl() -> bytes | None: data = None if os.environ.get("SNES_IPL"): data = Path(os.environ["SNES_IPL"]).read_bytes() elif os.environ.get("MAME_ROMPATH"): path = Path(os.environ["MAME_ROMPATH"]) / "snes.zip" if path.exists(): data = zipfile.ZipFile(path).read("spc700.rom") if data is not None and hashlib.sha1(data).hexdigest() != IPL_SHA1: return None return data IPL = _ipl() needs_ipl = pytest.mark.skipif(IPL is None, reason="no IPL ROM (SNES_IPL or MAME_ROMPATH)") @needs_ipl def test_gilyon_spctest() -> None: rom = ROOT / "gilyon" / "spctest" / "spctest.sfc" if not rom.exists(): pytest.skip("gilyon's ROMs are not fetched") snes = SNES(Cartridge(rom.read_bytes()), ipl=IPL) for frame in range(1, 1201): snes.run_frame() if frame % 30 == 0 and ("Success" in vram_text(snes) or "Failed" in vram_text(snes)): break snes.run_frames(2) text = vram_text(snes) assert "Success" in text assert re.search(r"Test number:.(\d{4})", text).group(1) == "0557" @needs_ipl @pytest.mark.parametrize( "rom", sorted((ROOT / "krom" / "CPUTest" / "SPC700").glob("*/*.sfc")), ids=lambda p: p.stem ) def test_krom_spc700(rom: Path) -> None: expected = len(re.findall(r"PrintText\(Pass", rom.with_suffix(".asm").read_text("latin-1"))) snes = SNES(Cartridge(rom.read_bytes()), ipl=IPL) tap = TokenTap(snes) last, still = -1, 0 for _ in range(4000): snes.run_frame() if tap.fails: break seen = tap.passes still = still + 1 if seen == last else 0 last = seen # Each check waits 256 x 4 ms on the S-SMP's timer 0 (about 61 # frames) before the next, so silence shorter than that is normal. if seen and still >= 150: break assert tap.fails == 0 assert tap.passes == expected