# SPDX-License-Identifier: MIT """Rungs 4 and 4b: the capture lockstep, the core's only tier-1 judge. The two reset captures (0.7 million instructions each: a BBC Master's G65SC12 and a BBC B's Rockwell R65C02 from reset through the boot, with the interrupts that arrive) and George Foot's data-only capture run here. The six long captures (166 million instructions) are ``scripts/run_capture.py``; their numbers are in docs/validation.md. The method is tests/capture_replay.py's. """ import pytest from capture_replay import FOOT, MACHINES, captures_present, replay, replay_data_only from w65c02_python import G65SC02, R65C02, W65C02S pytestmark = pytest.mark.skipif( not captures_present(), reason="captures not fetched: python -I scripts/fetch_references.py, then build_tools.py", ) @pytest.mark.parametrize(("machine", "instructions"), [("master", 60_000), ("beebr65c02", 60_000)]) def test_the_reset_and_the_first_instructions_agree_with_the_capture(machine, instructions) -> None: """The quick loop: the reset sequence and the start of the boot, in step with the chip.""" report = replay(machine, "reset", max_instructions=instructions) assert report.mismatch is None, report.mismatch assert not report.model_differences, report.model_examples assert report.interrupts["reset"] == 1 assert report.instructions == instructions @pytest.mark.slow @pytest.mark.parametrize("machine", sorted(MACHINES)) def test_the_reset_capture_replays_without_a_mismatch(machine: str) -> None: """CAP: every cycle's direction, SYNC and written byte, and every read's byte against the memory model, from reset to the end of the capture.""" report = replay(machine, "reset") assert report.mismatch is None, report.mismatch assert not report.model_differences, report.model_examples assert report.ended == "end of capture" assert report.instructions > 700_000 assert report.interrupts["irq"] > 100 and report.interrupts["nmi"] == 0 def test_the_two_parts_resets_differ_as_captured() -> None: """CAP: the G65SC12 writes in cycles 3 to 5 of its reset and the R65C02 reads; each row replayed against the other part's reset capture is out of step at the third cycle.""" report = replay("master", "reset", max_instructions=10, row=R65C02) assert report.mismatch is not None and "cycle 3 of reset" in report.mismatch report = replay("beebr65c02", "reset", max_instructions=10, row=G65SC02) assert report.mismatch is not None and "cycle 3 of reset" in report.mismatch class CorpusIndirectY(R65C02): """A seeded fault: `STA (zp),Y` with the corpus's dummy read (PC+1 again; C15).""" def _ea_idy_fixed(self) -> int: zp = self._fetch() low = self._read(zp) base = self._read((zp + 1) & 0xFF) << 8 | low self._read((self.pc - 1) & 0xFFFF) return (base + self.y) & 0xFFFF class DroppedDummy(G65SC02): """A seeded fault: INY and DEY without the dropped read of the next byte.""" def _op_iny(self) -> None: """INY -- increment Y in one cycle (seeded fault).""" self.y = self._set_nz((self.y + 1) & 0xFF) self._extra -= 1 def _op_dey(self) -> None: """DEY -- decrement Y in one cycle (seeded fault).""" self.y = self._set_nz((self.y - 1) & 0xFF) self._extra -= 1 def test_a_wrong_dummy_address_shows_as_model_differences() -> None: """The oracle can fail: a dummy read from the corpus's address disagrees with the model (the boot's memory clear is a loop of `STA (zp),Y`).""" report = replay("beebr65c02", "reset", max_instructions=60_000, row=CorpusIndirectY) assert report.mismatch is None assert report.model_differences[0x91, 5] > 1_000 assert report.model_agreed[0x91, 5] == 0 def test_a_missing_cycle_is_a_mismatch() -> None: """The oracle can fail: a dropped cycle puts the core out of step with SYNC.""" report = replay("master", "reset", max_instructions=60_000, row=DroppedDummy) assert report.mismatch is not None # -- rung 4b: Foot's data-only capture ------------------------------------------------------ @pytest.mark.slow @pytest.mark.skipif(not FOOT.is_file(), reason="not fetched: python -I scripts/fetch_references.py") @pytest.mark.parametrize("row", [W65C02S, R65C02], ids=lambda row: row.__name__) def test_foots_capture_replays_on_the_rows_with_bit_instructions(row: type) -> None: """CAP, data bus only: every written byte and every read's byte against the model, for the whole capture (1.47 million instructions of Dormann's 65C02 test).""" report = replay_data_only(row) assert report.mismatch is None, report.mismatch assert not report.model_differences, report.model_examples assert report.ended == "end of capture" and report.instructions > 1_400_000 @pytest.mark.slow @pytest.mark.skipif(not FOOT.is_file(), reason="not fetched: python -I scripts/fetch_references.py") def test_foots_chip_is_not_the_g65sc02_row() -> None: """The report says which row the chip was not: without the bit instructions the replay falls out of step where the program first uses one.""" report = replay_data_only(G65SC02) assert report.mismatch is not None assert report.instructions > 1_000_000