# SPDX-License-Identifier: MIT """Rung 3: Bruce Clark's decimal test (``cputype = 1``) on each row. Every A, operand and carry through ``ADC`` and ``SBC`` in decimal mode, invalid BCD digits included (131,072 additions and as many subtractions), checking A, N, V, Z and C as a 65C02 gives them. The program, its image and its tier are tests/programs.py's. Slow: 18 million instructions a run. """ import programs import pytest from programs import DECIMAL, DECIMAL_ERROR, ROWS, decimal_image, run_to_trap from w65c02_python import G65SC02, _alu pytestmark = [ pytest.mark.slow, pytest.mark.skipif(not DECIMAL.is_file(), reason=programs.BUILD_HINT), ] @pytest.mark.parametrize("row", ROWS, ids=lambda row: row.__name__) def test_decimal_test_reports_no_error(row: type) -> None: harness = run_to_trap(row, decimal_image()) assert harness.cpu.pc == 0x044B # DONE: its `JMP *` assert harness.memory[DECIMAL_ERROR] == 0, "ERROR is set: a result or flag differed" def test_the_error_byte_is_written_by_the_test() -> None: """The pre-fill check: ERROR starts as `$A5` here, so a run that never reached the test's own store would not read as success.""" assert decimal_image()[DECIMAL_ERROR] == 0xA5 assert DECIMAL.read_bytes()[DECIMAL_ERROR] == 0x00 def test_one_wrong_bcd_sum_fails_the_decimal_test(monkeypatch: pytest.MonkeyPatch) -> None: """The oracle can fail: one wrong sum out of 131,072 leaves ERROR set.""" right = _alu.decimal_add def wrong(a: int, operand: int, carry: int) -> tuple[int, int]: result, carry_out = right(a, operand, carry) if (a, operand, carry) == (0x45, 0x38, 1): result ^= 0x01 return result, carry_out monkeypatch.setattr(_alu, "decimal_add", wrong) harness = run_to_trap(G65SC02, decimal_image()) assert harness.memory[DECIMAL_ERROR] == 1 def test_one_wrong_decimal_flag_fails_the_decimal_test(monkeypatch: pytest.MonkeyPatch) -> None: """The oracle can fail: V taken from the binary sum instead of BC's sequence 2.""" monkeypatch.setattr( _alu, "decimal_add_overflow", lambda a, operand, carry: bool(~(a ^ operand) & (a ^ (a + operand + carry)) & 0x80), ) harness = run_to_trap(G65SC02, decimal_image()) assert harness.memory[DECIMAL_ERROR] == 1