# SPDX-License-Identifier: MIT """Arithmetic, logic, compare and bit test: ADC SBC AND ORA EOR CMP CPX CPY BIT TSB TRB. The accumulator instructions take M's width and the compares of X and Y take X's (DS note 1). Decimal mode is the 65C816's, not the NMOS 6502's: N and Z reflect the decimal result and there is no extra cycle (DS Table 7-1; U7). V and the results for invalid digits are undocumented in the datasheet; the decimal paths below are written from Bruce Clark's *Decimal Mode* tutorial (6502.org, appendices A and C; DOC, facts only) and checked against SingleStepTests' decimal cases, valid and invalid BCD alike (rung 1, TEST ROM). They were rewritten clean-room on 2026-11-25 (docs/provenance.md, item 2). """ from w65c816._core import C, D, M, N, V, X, Z, nz8, nz16 def _decimal_add_byte(a: int, b: int, carry: int) -> tuple[int, int, bool]: """One byte of decimal ADC: (result, carry out, V). DOC: Clark appendix A, "Seq. 1" for the result and C, "Seq. 2" for V, which the 65C02 and the 65816 share. Any nibble values, valid BCD or not. """ low = (a & 0x0F) + (b & 0x0F) + carry if low >= 0x0A: # the low digit carries: skip the six codes $A-$F low = ((low + 0x06) & 0x0F) + 0x10 total = (a & 0xF0) + (b & 0xF0) + low # V reads the same sum as two's complement, before the high digit is adjusted. signed = total - (0x100 if a & 0x80 else 0) - (0x100 if b & 0x80 else 0) overflow = not -0x80 <= signed <= 0x7F if total >= 0xA0: # the high digit carries total += 0x60 return total & 0xFF, int(total >= 0x100), overflow def _decimal_subtract_byte(a: int, b: int, carry: int) -> tuple[int, int, bool]: """One byte of decimal SBC: (result, carry out, V). DOC: Clark appendix A, "Seq. 3" for the 65816's result (the 65C02 differs, "Seq. 4"); C and V are the binary subtraction's ("bin"). """ low = (a & 0x0F) - (b & 0x0F) + carry - 1 if low < 0: # the low digit borrows: skip the six codes $A-$F downwards low = ((low - 0x06) & 0x0F) - 0x10 total = (a & 0xF0) - (b & 0xF0) + low if total < 0: # the high digit borrows total -= 0x60 binary = a - b + carry - 1 overflow = bool((a ^ b) & (a ^ binary) & 0x80) return total & 0xFF, int(binary >= 0), overflow def _decimal(a: int, value: int, p: int, wide: bool, byte_operation) -> tuple[int, int]: """Decimal ADC or SBC, 8 or 16 bits; return (result, P). DOC: Clark appendix C: with M = 0 the 65816 behaves as the low bytes' 8-bit operation followed by the high bytes', the carry passing between them; N, V and C are the high byte's, Z is the whole 16-bit result's. """ carry = p & C result = 0 for shift in (0, 8) if wide else (0,): byte, carry, overflow = byte_operation(a >> shift & 0xFF, value >> shift & 0xFF, carry) result |= byte << shift p = (p & ~(N | V | Z | C)) | (nz16(result) if wide else nz8(result)) if overflow: p |= V if carry: p |= C return result, p def _binary_add(a: int, value: int, p: int, wide: bool) -> tuple[int, int]: """A + value + C in binary, 8 or 16 bits; return (result, P).""" mask, sign = (0xFFFF, 0x8000) if wide else (0xFF, 0x80) total = a + value + (p & C) result = total & mask p = (p & ~(N | V | Z | C)) | (nz16(result) if wide else nz8(result)) if ~(a ^ value) & (a ^ result) & sign: # both operands one sign, the result the other p |= V if total > mask: p |= C return result, p def add(a: int, value: int, p: int, wide: bool) -> tuple[int, int]: """A + M + C, binary or decimal, 8 or 16 bits; return (result, P).""" if p & D: return _decimal(a, value, p, wide, _decimal_add_byte) return _binary_add(a, value, p, wide) def subtract(a: int, value: int, p: int, wide: bool) -> tuple[int, int]: """A - M - (1 - C), binary or decimal, 8 or 16 bits; return (result, P). In binary this is A + NOT M + C, so C is "no borrow". """ if p & D: return _decimal(a, value, p, wide, _decimal_subtract_byte) return _binary_add(a, value ^ (0xFFFF if wide else 0xFF), p, wide) class ALUMixin: """Private arithmetic, logic and compare implementation.""" def _set_a(self, value: int, wide: bool) -> None: # With M = 1 the high byte B is untouched (U5). self.a = value if wide else (self.a & 0xFF00) | value self.p = (self.p & ~(N | Z)) | (nz16(value) if wide else nz8(value)) def _op_adc(self, mode) -> None: """ADC -- A + M + C -> A (DS Table 5-5 p. 32).""" wide = not self.p & M operand = self._load(mode(self), wide) a = self.a if wide else self.a & 0xFF result, p = add(a, operand, self.p, wide) self.a = result if wide else (self.a & 0xFF00) | result self.p = p def _op_sbc(self, mode) -> None: """SBC -- A - M - (1 - C) -> A (DS Table 5-5 p. 33).""" wide = not self.p & M operand = self._load(mode(self), wide) a = self.a if wide else self.a & 0xFF result, p = subtract(a, operand, self.p, wide) self.a = result if wide else (self.a & 0xFF00) | result self.p = p def _op_and(self, mode) -> None: """AND -- A AND M -> A (DS Table 5-5 p. 32).""" wide = not self.p & M self._set_a(self.a & self._load(mode(self), wide) & (0xFFFF if wide else 0xFF), wide) def _op_ora(self, mode) -> None: """ORA -- A OR M -> A (DS Table 5-5 p. 33).""" wide = not self.p & M self._set_a((self.a | self._load(mode(self), wide)) & (0xFFFF if wide else 0xFF), wide) def _op_eor(self, mode) -> None: """EOR -- A EOR M -> A (DS Table 5-5 p. 32).""" wide = not self.p & M self._set_a((self.a ^ self._load(mode(self), wide)) & (0xFFFF if wide else 0xFF), wide) def _compare(self, register: int, value: int, wide: bool) -> None: mask = 0xFFFF if wide else 0xFF register &= mask difference = (register - value) & mask p = (self.p & ~(N | Z | C)) | (nz16(difference) if wide else nz8(difference)) if register >= value: p |= C self.p = p def _op_cmp(self, mode) -> None: """CMP -- A - M, flags only (DS Table 5-5 p. 32).""" wide = not self.p & M self._compare(self.a, self._load(mode(self), wide), wide) def _op_cpx(self, mode) -> None: """CPX -- X - M, flags only (DS Table 5-5 p. 32).""" wide = not self.p & X self._compare(self.x, self._load(mode(self), wide), wide) def _op_cpy(self, mode) -> None: """CPY -- Y - M, flags only (DS Table 5-5 p. 32).""" wide = not self.p & X self._compare(self.y, self._load(mode(self), wide), wide) def _op_bit(self, mode) -> None: """BIT -- A AND M sets Z; M's top two bits -> N, V (DS Table 5-5 p. 32).""" wide = not self.p & M value = self._load(mode(self), wide) top = value >> 8 if wide else value p = (self.p & ~(N | V | Z)) | (top & (N | V)) if not self.a & value & (0xFFFF if wide else 0xFF): p |= Z self.p = p def _op_bit_imm(self, mode) -> None: """BIT # -- A AND M sets Z only; N and V are untouched (DS Table 5-5 p. 32).""" wide = not self.p & M value = self._load(mode(self), wide) if self.a & value & (0xFFFF if wide else 0xFF): self.p &= ~Z else: self.p |= Z def _op_tsb(self, mode) -> None: """TSB -- A AND M sets Z, then M OR A -> M (DS Table 5-5 p. 34).""" wide = not self.p & M a = self.a & (0xFFFF if wide else 0xFF) def operation(value: int) -> int: self.p = (self.p & ~Z) | (0 if a & value else Z) return value | a self._modify(mode(self), wide, operation) def _op_trb(self, mode) -> None: """TRB -- A AND M sets Z, then M AND NOT A -> M (DS Table 5-5 p. 34).""" wide = not self.p & M a = self.a & (0xFFFF if wide else 0xFF) def operation(value: int) -> int: self.p = (self.p & ~Z) | (0 if a & value else Z) return value & ~a self._modify(mode(self), wide, operation)