# SPDX-License-Identifier: MIT """Arithmetic, logic and compare instructions: ADC SBC AND ORA EOR CMP CPX CPY BIT. Binary arithmetic is the NMOS 6502's. Decimal arithmetic is not: the 65C02 gives valid N, V and Z and takes one more cycle (DS Table 7-1), and for invalid BCD digits its accumulator differs from the NMOS part's. The formulas are Bruce Clark's ("Decimal Mode", 6502.org, Appendix A; BC, tier 3), which CAP confirms at tier 1 for the G65SC12 and the R65C02: both real parts ran his exhaustive test (every A, operand and carry, 131,072 additions and as many subtractions) to its success address. :func:`decimal_add` and :func:`decimal_subtract` are pure functions, kept importable for another core that needs the 65C02's decimal results (docs/handoff-brief.md, "Recommendation"). """ from collections.abc import Callable from w65c02_python._core import NZ, C, D, N, V, Z Mode = Callable[..., int] # Evidence tags (emulator-lineage docs/methodology.md): the decimal formulas # are DOC (BC Appendix A) and MEASURED for the G65SC12 and the R65C02 (CAP: # both ran Clark's exhaustive test, 65C02 build, to success in lockstep); for # the W65C02S and the Lynx cells they are DOC only (U11). The binary results # and flags are DOC (DS Table 6-4) and MEASURED (CAP: Dormann's tests). def decimal_add(a: int, operand: int, carry: int) -> tuple[int, int]: """Decimal ``a + operand + carry`` on a 65C02: (accumulator, carry out). BC Appendix A, sequence 1, line by line. """ # DOC (BC); MEASURED on two parts (CAP, Clark's test); U11 for the others. low = (a & 0x0F) + (operand & 0x0F) + carry # BC 1a if low >= 0x0A: low = ((low + 0x06) & 0x0F) + 0x10 # BC 1b total = (a & 0xF0) + (operand & 0xF0) + low # BC 1c if total >= 0xA0: total += 0x60 # BC 1e return total & 0xFF, int(total >= 0x100) # BC 1f, 1g def decimal_add_overflow(a: int, operand: int, carry: int) -> bool: """V after a decimal ADC: the signed overflow of the sum before its high digit is adjusted. BC Appendix A, sequence 2 (2a-2c, 2f), which the 65C02 keeps from the NMOS part: "the V flag result is the same on the 6502, 65C02, and 65816 in decimal mode" (CL section 8). """ # DOC (BC, CL section 8); MEASURED on two parts (CAP: Clark's test checks V). low = (a & 0x0F) + (operand & 0x0F) + carry # BC 2a if low >= 0x0A: low = ((low + 0x06) & 0x0F) + 0x10 # BC 2b total = (a & 0xF0) + (operand & 0xF0) + low # BC 2c, here unsigned return bool(~(a ^ operand) & (a ^ total) & 0x80) # BC 2f: operands agree in sign, sum differs def decimal_subtract(a: int, operand: int, carry: int) -> tuple[int, int]: """Decimal ``a - operand - (1 - carry)`` on a 65C02: (accumulator, carry out). BC Appendix A, sequence 4, line by line; the carry is the binary subtraction's. """ # DOC (BC); MEASURED on two parts (CAP, Clark's test); U11 for the others. low = (a & 0x0F) - (operand & 0x0F) + carry - 1 # BC 4a total = a - operand + carry - 1 # BC 4b carry_out = int(total >= 0) if total < 0: total -= 0x60 # BC 4c if low < 0: total -= 0x06 # BC 4d return total & 0xFF, carry_out # BC 4e class ALUMixin: """ADC SBC AND ORA EOR CMP CPX CPY BIT.""" def _decimal_cycle(self) -> None: """The cycle decimal mode adds to ADC and SBC (DS Table 7-1; CAP: `69` 2/3, `65` 3/4). A read at PC, the next opcode's address: CAP, both parts (rung 4: the captured byte is the opcode fetched next, in every decimal ADC and SBC of Clark's test). SST re-reads the operand instead (C13). """ # MEASURED (CAP: the cycle and its address); DOC (DS Table 7-1: the cycle). self._fetch_discard() self._extra += 1 def _op_adc(self, mode: Mode) -> None: """ADC -- add memory and carry to A (DS Table 6-4).""" # Binary: DOC (DS Table 6-4), MEASURED (CAP, Dormann). Decimal: see the module note. operand = mode(self) a = self.a p = self.p carry = p & C if p & D: self._decimal_cycle() result, carry = decimal_add(a, operand, carry) overflow = decimal_add_overflow(a, operand, p & C) else: total = a + operand + carry result = total & 0xFF carry = total >> 8 overflow = ~(a ^ operand) & (a ^ result) & 0x80 self.a = result # N and Z are the result's in decimal mode too: "valid flags" (DS Table 7-1). self.p = (p & ~(N | V | Z | C)) | NZ[result] | (V if overflow else 0) | carry def _op_sbc(self, mode: Mode) -> None: """SBC -- subtract memory and borrow from A (DS Table 6-4).""" # Binary: DOC (DS Table 6-4), MEASURED (CAP, Dormann); V and C in decimal # mode: DOC (BC), MEASURED on two parts (CAP, Clark's test). operand = mode(self) a = self.a p = self.p carry = p & C total = a - operand + carry - 1 # V and C are the binary subtraction's in both modes (BC Appendix A). overflow = (a ^ operand) & (a ^ total) & 0x80 if p & D: self._decimal_cycle() result, carry = decimal_subtract(a, operand, carry) else: result = total & 0xFF carry = int(total >= 0) self.a = result self.p = (p & ~(N | V | Z | C)) | NZ[result] | (V if overflow else 0) | carry # AND ORA EOR, the compares and BIT: DOC (DS Table 6-4); MEASURED (CAP: # Dormann's tests check every result and flag, in lockstep on both parts). def _op_and(self, mode: Mode) -> None: """AND -- A AND memory (DS Table 6-4).""" self.a = self._set_nz(self.a & mode(self)) def _op_ora(self, mode: Mode) -> None: """ORA -- A OR memory (DS Table 6-4).""" self.a = self._set_nz(self.a | mode(self)) def _op_eor(self, mode: Mode) -> None: """EOR -- A exclusive-OR memory (DS Table 6-4).""" self.a = self._set_nz(self.a ^ mode(self)) def _compare(self, register: int, operand: int) -> None: """N, Z and C from ``register - operand``; nothing is stored.""" difference = (register - operand) & 0xFF self.p = (self.p & ~(N | Z | C)) | NZ[difference] | (C if register >= operand else 0) def _op_cmp(self, mode: Mode) -> None: """CMP -- compare memory with A (DS Table 6-4).""" self._compare(self.a, mode(self)) def _op_cpx(self, mode: Mode) -> None: """CPX -- compare memory with X (DS Table 6-4).""" self._compare(self.x, mode(self)) def _op_cpy(self, mode: Mode) -> None: """CPY -- compare memory with Y (DS Table 6-4).""" self._compare(self.y, mode(self)) def _op_bit(self, mode: Mode) -> None: """BIT -- Z from A AND memory, N and V from memory bits 7 and 6 (DS Table 6-4 note 2).""" operand = mode(self) self.p = (self.p & ~(N | V | Z)) | (operand & (N | V)) | (0 if self.a & operand else Z) def _op_bit_imm(self) -> None: """BIT #imm -- Z from A AND the operand; N and V are untouched (CL section 2).""" # DOC (CL section 2); MEASURED (CAP: Dormann's 65C02 test checks it on both parts). operand = self._fetch() self.p = (self.p & ~Z) | (0 if self.a & operand else Z)