# SPDX-License-Identifier: MIT """Rung 2: Klaus Dormann's 65C02 extended-opcodes test and his NMOS functional test, per row. The programs, their images and their tier are tests/programs.py's. Slow: 22 and 31 million instructions a run; ``scripts/run_programs.py`` prints the counts. """ import programs import pytest from programs import EXTENDED, FUNCTIONAL, FUNCTIONAL_SUCCESS, ROWS, extended_image, run_to_trap from w65c02_python import G65SC02, R65C02, W65C02S pytestmark = [ pytest.mark.slow, pytest.mark.skipif( not (FUNCTIONAL.is_file() and extended_image("sc").is_file()), reason=programs.BUILD_HINT ), ] @pytest.mark.parametrize("row", ROWS, ids=lambda row: row.__name__) def test_extended_opcodes_test_reaches_success(row: type) -> None: image, success = EXTENDED[row] harness = run_to_trap(row, extended_image(image).read_bytes()) assert harness.cpu.pc == success, f"trapped at ${harness.cpu.pc:04X}: see the build's .lst" @pytest.mark.parametrize("row", ROWS, ids=lambda row: row.__name__) def test_nmos_functional_test_reaches_success(row: type) -> None: harness = run_to_trap(row, FUNCTIONAL.read_bytes()) assert harness.cpu.pc == FUNCTIONAL_SUCCESS, f"trapped at ${harness.cpu.pc:04X}" @pytest.mark.parametrize( ("row", "image"), [ (G65SC02, "rockwell"), # the bit instructions are missing (R65C02, "sc"), # `x7`/`xF` are not NOPs (W65C02S, "rockwell"), # `$CB` is WAI, not a NOP ], ids=["G65SC02-rockwell", "R65C02-sc", "W65C02S-rockwell"], ) def test_extended_opcodes_test_tells_the_rows_apart(row: type, image: str) -> None: """Each image on a row it was not built for does not reach its success trap: the test sees the cells in which the rows differ.""" success = {"wdc": 0x24F1, "rockwell": 0x2569, "sc": 0x2434}[image] harness = run_to_trap(row, extended_image(image).read_bytes()) assert harness.cpu.pc != success class WrongCompareCarry(G65SC02): """A seeded fault: CMP, CPX and CPY set C on greater-than, not greater-or-equal.""" def _compare(self, register: int, operand: int) -> None: super()._compare(register, operand) if register == operand: self.p &= ~0x01 def test_a_wrong_cmp_carry_traps() -> None: """The oracle can fail: with a wrong CMP carry neither program reaches its success trap.""" harness = run_to_trap(WrongCompareCarry, extended_image("sc").read_bytes()) assert harness.cpu.pc != EXTENDED[G65SC02][1] harness = run_to_trap(WrongCompareCarry, FUNCTIONAL.read_bytes()) assert harness.cpu.pc != FUNCTIONAL_SUCCESS