"""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, Z and C reflect the decimal result, 16-bit operands chain four digits, no extra cycle (U7). V and the results for invalid digits are undocumented; the digit-by-digit adder below is bsnes's (``processor/wdc65816/algorithms.cpp``, emulator tier), checked against SingleStepTests' decimal cases (rung 1). """ from w65c816._core import C, M, N, V, X, Z, nz8, nz16 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).""" carry = p & C top = 0x8000 if wide else 0x80 if not p & 0x08: result = a + value + carry else: # One digit at a time, each adjusted before the next sees its carry; # V is taken from the sum before the top digit's adjustment. result = 0 for shift in range(0, 16 if wide else 8, 4): mask = 0xF << shift digit = (a & mask) + (value & mask) + (carry << shift) + result if shift < (12 if wide else 4) and digit > (9 << shift) | ((1 << shift) - 1): digit += 6 << shift carry = 1 if digit > (0x10 << shift) - 1 else 0 result = digit & ((0x10 << shift) - 1) if shift < (12 if wide else 4) else digit p &= ~(N | V | Z | C) if ~(a ^ value) & (a ^ result) & top: p |= V if p & 0x08 and result > (0x9FFF if wide else 0x9F): result += 0x6000 if wide else 0x60 if result > (0xFFFF if wide else 0xFF): p |= C result &= 0xFFFF if wide else 0xFF return result, p | (nz16(result) if wide else nz8(result)) def subtract(a: int, value: int, p: int, wide: bool) -> tuple[int, int]: """A - M - (1 - C): A + ~M + C, with decimal digits adjusted downwards.""" mask = 0xFFFF if wide else 0xFF value = ~value & mask top = 0x8000 if wide else 0x80 carry = p & C if not p & 0x08: result = a + value + carry else: result = 0 last = 12 if wide else 4 for shift in range(0, last + 4, 4): digit_mask = 0xF << shift digit = (a & digit_mask) + (value & digit_mask) + (carry << shift) + result if shift < last: if digit <= (0x10 << shift) - 1: digit -= 6 << shift carry = 1 if digit > (0x10 << shift) - 1 else 0 result = digit & ((0x10 << shift) - 1) else: result = digit p &= ~(N | V | Z | C) if ~(a ^ value) & (a ^ result) & top: p |= V if p & 0x08 and result <= mask: result -= 0x6000 if wide else 0x60 if result > mask: p |= C result &= mask return result, p | (nz16(result) if wide else nz8(result)) 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)