# SPDX-License-Identifier: MIT """Rung 3: krom's (Peter Lemon's) 23 SNES CPU tests pass every row in the harness. PeterLemon/SNES @ 350b394e86ec, CPUTest/CPU (validation.md: hardware- verified; the author tests on a real SNES). Each ROM runs an instruction over chosen inputs, compares the result and NVZC with values stored in the ROM, and prints "PASS" or "FAIL" for the row with ``PrintText(Pass, $DEST, 4)`` or ``PrintText(Fail, $DEST, 4)``; pages of rows are cleared and reprinted, so the verdict is read from the stream of VRAM writes rather than the last screen. The pass: every ``PrintText(Pass, ...)`` in the source printed "PASS" at its destination as many times as the source has it, and nothing printed "FAIL" at any Fail destination. CPUMSC ends its STP row with "Please Reset To PASS STP": after STP the harness resets the CPU once, WRAM kept, as a player pressing reset would. """ import re from collections import Counter from pathlib import Path import pytest from w65c816.harness import Harness ROOT = Path(__file__).resolve().parents[1] / "reference" / "src" / "PeterLemon-SNES" TESTS = ROOT / "CPUTest" / "CPU" GROUPS = [ "ADC", "AND", "ASL", "BIT", "BRA", "CMP", "DEC", "EOR", "INC", "JMP", "LDR", "LSR", "MOV", "MSC", "ORA", "PHL", "PSR", "RET", "ROL", "ROR", "SBC", "STR", "TRN", ] PRINT = re.compile(r"PrintText\((Pass|Fail), \$([0-9A-F]{4}), 4\)") MAX_CYCLES = 10_000_000 # the longest, LDR, needs about 1.5 million class TextLog(Harness): """The harness, noting every VRAM low-byte write with its word address.""" def __init__(self, rom: bytes) -> None: self.writes: list[tuple[int, int]] = [] super().__init__(rom) def io_write(self, offset: int, value: int) -> None: if offset == 0x2118: self.writes.append((self.vram_address, value)) super().io_write(offset, value) def printed(self, word: bytes) -> Counter[int]: """How many times ``word`` was written at consecutive VRAM words, by its first word's byte address (as the sources write ``$DEST``).""" found: Counter[int] = Counter() size = len(word) for index in range(len(self.writes) - size + 1): run = self.writes[index : index + size] if bytes(value for _, value in run) == word and all( address == run[0][0] + k for k, (address, _) in enumerate(run) ): found[run[0][0] << 1 & 0xFFFF] += 1 return found def failing(self) -> bool: return bytes(value for _, value in self.writes[-4:]) == b"FAIL" def expected(source: str) -> tuple[Counter[int], set[int]]: passes: Counter[int] = Counter() fails: set[int] = set() for kind, destination in PRINT.findall(source): if kind == "Pass": passes[int(destination, 16)] += 1 else: fails.add(int(destination, 16)) return passes, fails @pytest.mark.slow @pytest.mark.parametrize("group", GROUPS) def test_every_row_passes(group: str) -> None: folder = TESTS / group if not folder.is_dir(): pytest.skip("PeterLemon/SNES not fetched: python -I scripts/fetch_references.py") passes, fails = expected((folder / f"CPU{group}.asm").read_text(encoding="utf-8")) harness = TextLog((folder / f"CPU{group}.sfc").read_bytes()) result = harness.run(MAX_CYCLES, TextLog.failing) if result.reason == "stopped" and group == "MSC": harness.cpu.request_reset() result = harness.run(MAX_CYCLES, TextLog.failing) failed = harness.printed(b"FAIL") assert not set(failed) & fails, f"FAIL printed at {sorted(set(failed) & fails)}" assert result.reason == "spin", result printed = harness.printed(b"PASS") assert {at: printed[at] for at in passes} == dict(passes) assert sum(passes.values()) > 0