# SPDX-License-Identifier: MIT """Rung 3: PeterLemon's CPUTest/SPC700 in the harness (validation.md). Hardware-verified: the author runs them on a SNES through an SD2SNES, with expected screens in the repository. Each sub-test writes $0n (pass) or $8n (fail) to $F4, waiting about a second on timer 0 between sub-tests, so the seven programs take 166 M cycles: ``slow``. Each exists in two forms: ``ADC/SPC700ADC.spc`` is the raw program for $0200, the bytes the ``.sfc`` uploads; the top-level ``SPC700ADC.spc`` is a true ``.spc`` snapshot. The rung runs both. """ from pathlib import Path import pytest from spc700_python import SPC700 from spc700_python.harness import run_cpu_test CPUTEST = Path(__file__).resolve().parents[1] / "reference" / "src" / "PeterLemon-SNES" CPUTEST = CPUTEST / "CPUTest" / "SPC700" #: sub-tests per program (docs/mame-oracle.md, read from each program's source) SUBTESTS = {"ADC": 26, "AND": 28, "DEC": 14, "EOR": 26, "INC": 14, "ORA": 28, "SBC": 26} pytestmark = pytest.mark.skipif( not CPUTEST.is_dir(), reason="run scripts/fetch_references.py first" ) def program(name: str) -> bytes: return (CPUTEST / name / f"SPC700{name}.spc").read_bytes() def snapshot(name: str) -> bytes: return (CPUTEST / f"SPC700{name}.spc").read_bytes() def test_count_is_162(): assert sum(SUBTESTS.values()) == 162 @pytest.mark.parametrize("name", SUBTESTS) def test_first_subtests_quickly(name): """The first two sub-tests, a short run on the quick loop.""" report = run_cpu_test(program(name), max_cycles=2_400_000) assert report.codes[:2] == (1, 2) report = run_cpu_test(snapshot(name), max_cycles=2_400_000) assert report.codes[:2] == (1, 2) @pytest.mark.slow @pytest.mark.parametrize("name", SUBTESTS) @pytest.mark.parametrize("form", [program, snapshot], ids=["raw", "snapshot"]) def test_all_subtests(name, form): report = run_cpu_test(form(name)) assert report.reason == "self-branch" assert report.failures == () assert report.passes == tuple(range(1, SUBTESTS[name] + 1)) def test_the_driver_sees_a_failed_subtest(monkeypatch): """The gate can fail: an ADC that ignores the carry fails the first sub-test.""" adc = SPC700._adc def wrong(self, x, y): result = adc(self, x, y) self.psw ^= 0x01 return result monkeypatch.setattr(SPC700, "_adc", wrong) report = run_cpu_test(program("ADC"), max_cycles=2_400_000) assert report.failures