# SPDX-License-Identifier: MIT """Decimal-mode ADC and SBC: the worked examples of Bruce Clark's *Decimal Mode*. DOC: 6502.org's tutorial (reference/docs/6502org-decimal_mode.html), sections 3.2.2 and 4 and appendix C, each stated there for the 65816. The volume check (valid and invalid BCD, 8 and 16 bits) is SingleStepTests' (rung 1). """ import pytest from w65c816._alu import add, subtract from w65c816._core import C, D, N, V, Z FLAGS = N | V | Z | C @pytest.mark.parametrize( ("operation", "a", "value", "carry", "wide", "result", "flags"), [ (add, 0x99, 0x01, 0, False, 0x00, Z | C), # 3.2.2: Z valid on the 65816 (add, 0x90, 0x90, 0, False, 0x80, N | V | C), # 3.2.2: V set as in binary (add, 0x12, 0x34, 1, False, 0x47, 0), (subtract, 0x01, 0x01, 1, False, 0x00, Z | C), # 3.2.2 (subtract, 0x90, 0x0F, 1, False, 0x8B, N | C), # 4: invalid BCD, $8B on the 65816 (subtract, 0x00, 0x01, 1, False, 0x99, N), # a borrow out: C clear (add, 0x9999, 0x0001, 0, True, 0x0000, Z | C), # appendix C: four digits chained (add, 0x0950, 0x0050, 0, True, 0x1000, 0), (subtract, 0x1000, 0x0001, 1, True, 0x0999, C), ], ) def test_clark_examples(operation, a, value, carry, wide, result, flags) -> None: got, p = operation(a, value, D | carry, wide) assert (got, p & FLAGS) == (result, flags) assert p & D