// SPDX-License-Identifier: MIT //! Arithmetic, logic, compare and bit test: ADC SBC AND ORA EOR CMP CPX CPY BIT //! TSB TRB (`_alu.py`). //! //! 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 transcribe the reference's, which are written from Bruce //! Clark's *Decimal Mode* tutorial (6502.org, appendices A and C; DOC, facts //! only) and checked there against SingleStepTests' decimal cases, valid and //! invalid BCD alike (its rung 1, TEST ROM). The reference rewrote them //! clean-room on 2026-11-25 (its docs/provenance.md, item 2), replacing an adder //! its audit had found translated from bsnes's GPL-3.0 code; this file carries //! the rewrite down and keeps nothing of the old one. //! //! Differences forced by Rust: Python's integers are unbounded and its `&` on //! a negative number is two's complement; the digit arithmetic here is in //! `i32`, whose `&` is two's complement too and which holds every value these //! sums reach (a decimal subtraction's digit can go below zero before it is //! masked). The byte operations are passed as `fn` pointers where the //! reference passes functions. The closures of //! TSB and TRB are the [`crate::core::Modify`] operations `tsb_operation` and //! `trb_operation`, which read A as the closure captured it (no cycle between //! the capture and the call changes A). use crate::core::{Bus, Modify, C, D, M, N, V, W65C816, X, Z}; use crate::dispatch::Mode; fn nz(result: i64, wide: bool) -> u8 { if wide { crate::core::nz16(result as u32) } else { crate::core::nz8(result as u32) } } /// One byte of decimal ADC: (result, carry out, V) (`_decimal_add_byte`). /// /// 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. fn decimal_add_byte(a: i32, b: i32, carry: i32) -> (i32, i32, bool) { let mut low = (a & 0x0F) + (b & 0x0F) + carry; if low >= 0x0A { // The low digit carries: skip the six codes $A-$F. low = ((low + 0x06) & 0x0F) + 0x10; } let mut total = (a & 0xF0) + (b & 0xF0) + low; // V reads the same sum as two's complement, before the high digit is adjusted. let signed = total - signed_offset(a) - signed_offset(b); let overflow = !(-0x80..=0x7F).contains(&signed); if total >= 0xA0 { // The high digit carries. total += 0x60; } (total & 0xFF, (total >= 0x100) as i32, overflow) } /// What reading a byte as two's complement subtracts from it. fn signed_offset(byte: i32) -> i32 { if byte & 0x80 != 0 { 0x100 } else { 0 } } /// One byte of decimal SBC: (result, carry out, V) (`_decimal_subtract_byte`). /// /// 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"). fn decimal_subtract_byte(a: i32, b: i32, carry: i32) -> (i32, i32, bool) { let mut 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; } let mut total = (a & 0xF0) - (b & 0xF0) + low; if total < 0 { // The high digit borrows. total -= 0x60; } let binary = a - b + carry - 1; let overflow = (a ^ b) & (a ^ binary) & 0x80 != 0; (total & 0xFF, (binary >= 0) as i32, overflow) } /// One byte of a decimal operation: (result, carry out, V). type ByteOperation = fn(i32, i32, i32) -> (i32, i32, bool); /// Decimal ADC or SBC, 8 or 16 bits; return (result, P) (`_decimal`). /// /// 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. fn decimal(a: u16, value: u16, p: u8, wide: bool, byte_operation: ByteOperation) -> (u16, u8) { let mut carry = (p & C) as i32; let mut overflow = false; let mut result = 0; let shifts: &[u32] = if wide { &[0, 8] } else { &[0] }; for &shift in shifts { let (byte, carry_out, byte_overflow) = byte_operation( (a as i32) >> shift & 0xFF, (value as i32) >> shift & 0xFF, carry, ); result |= byte << shift; carry = carry_out; overflow = byte_overflow; } let mut p = (p & !(N | V | Z | C)) | nz(result as i64, wide); if overflow { p |= V; } if carry != 0 { p |= C; } (result as u16, p) } /// A + value + C in binary, 8 or 16 bits; return (result, P) (`_binary_add`). fn binary_add(a: u16, value: u16, p: u8, wide: bool) -> (u16, u8) { let (mask, sign): (u32, u32) = if wide { (0xFFFF, 0x8000) } else { (0xFF, 0x80) }; let (a, value) = (a as u32, value as u32); let total = a + value + (p & C) as u32; let result = total & mask; let mut p = (p & !(N | V | Z | C)) | nz(result as i64, wide); if !(a ^ value) & (a ^ result) & sign != 0 { // Both operands one sign, the result the other. p |= V; } if total > mask { p |= C; } (result as u16, p) } /// A + M + C, binary or decimal, 8 or 16 bits; return (result, P). pub fn add(a: u16, value: u16, p: u8, wide: bool) -> (u16, u8) { if p & D != 0 { return decimal(a, value, p, wide, decimal_add_byte); } binary_add(a, value, p, wide) } /// 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". pub fn subtract(a: u16, value: u16, p: u8, wide: bool) -> (u16, u8) { if p & D != 0 { return decimal(a, value, p, wide, decimal_subtract_byte); } binary_add(a, value ^ if wide { 0xFFFF } else { 0xFF }, p, wide) } impl W65C816 { pub(crate) fn set_a(&mut self, value: u16, wide: bool) { // With M = 1 the high byte B is untouched (U5). self.a = if wide { value } else { (self.a & 0xFF00) | value }; self.p = (self.p & !(N | Z)) | nz(value as i64, wide); } /// ADC -- A + M + C -> A (DS Table 5-5 p. 32). pub(crate) fn op_adc(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); let operand = self.load(address, wide); let a = if wide { self.a } else { self.a & 0xFF }; let (result, p) = add(a, operand, self.p, wide); self.a = if wide { result } else { (self.a & 0xFF00) | result }; self.p = p; } /// SBC -- A - M - (1 - C) -> A (DS Table 5-5 p. 33). pub(crate) fn op_sbc(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); let operand = self.load(address, wide); let a = if wide { self.a } else { self.a & 0xFF }; let (result, p) = subtract(a, operand, self.p, wide); self.a = if wide { result } else { (self.a & 0xFF00) | result }; self.p = p; } /// AND -- A AND M -> A (DS Table 5-5 p. 32). pub(crate) fn op_and(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); let value = self.a & self.load(address, wide) & if wide { 0xFFFF } else { 0xFF }; self.set_a(value, wide); } /// ORA -- A OR M -> A (DS Table 5-5 p. 33). pub(crate) fn op_ora(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); let value = (self.a | self.load(address, wide)) & if wide { 0xFFFF } else { 0xFF }; self.set_a(value, wide); } /// EOR -- A EOR M -> A (DS Table 5-5 p. 32). pub(crate) fn op_eor(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); let value = (self.a ^ self.load(address, wide)) & if wide { 0xFFFF } else { 0xFF }; self.set_a(value, wide); } fn compare(&mut self, register: u16, value: u16, wide: bool) { let mask: i64 = if wide { 0xFFFF } else { 0xFF }; let register = register as i64 & mask; let value = value as i64; let difference = (register - value) & mask; let mut p = (self.p & !(N | Z | C)) | nz(difference, wide); if register >= value { p |= C; } self.p = p; } /// CMP -- A - M, flags only (DS Table 5-5 p. 32). pub(crate) fn op_cmp(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); let value = self.load(address, wide); self.compare(self.a, value, wide); } /// CPX -- X - M, flags only (DS Table 5-5 p. 32). pub(crate) fn op_cpx(&mut self, mode: Mode) { let wide = self.p & X == 0; let address = self.ea(mode); let value = self.load(address, wide); self.compare(self.x, value, wide); } /// CPY -- Y - M, flags only (DS Table 5-5 p. 32). pub(crate) fn op_cpy(&mut self, mode: Mode) { let wide = self.p & X == 0; let address = self.ea(mode); let value = self.load(address, wide); self.compare(self.y, value, wide); } /// BIT -- A AND M sets Z; M's top two bits -> N, V (DS Table 5-5 p. 32). pub(crate) fn op_bit(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); let value = self.load(address, wide); let top = if wide { value >> 8 } else { value }; let mut p = (self.p & !(N | V | Z)) | (top as u8 & (N | V)); if self.a & value & if wide { 0xFFFF } else { 0xFF } == 0 { p |= Z; } self.p = p; } /// BIT # -- A AND M sets Z only; N and V are untouched (DS Table 5-5 p. 32). pub(crate) fn op_bit_imm(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); let value = self.load(address, wide); if self.a & value & if wide { 0xFFFF } else { 0xFF } != 0 { self.p &= !Z; } else { self.p |= Z; } } /// TSB -- A AND M sets Z, then M OR A -> M (DS Table 5-5 p. 34). pub(crate) fn op_tsb(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); self.modify(address, wide, Modify::Tsb); } /// TRB -- A AND M sets Z, then M AND NOT A -> M (DS Table 5-5 p. 34). pub(crate) fn op_trb(&mut self, mode: Mode) { let wide = self.p & M == 0; let address = self.ea(mode); self.modify(address, wide, Modify::Trb); } // TSB's and TRB's operations (the reference's closures; `a` is A masked // to the width, as the closure captured it). pub(crate) fn tsb_operation(&mut self, value: u16) -> u16 { let a = self.a & if self.p & M == 0 { 0xFFFF } else { 0xFF }; self.p = (self.p & !Z) | if a & value != 0 { 0 } else { Z }; value | a } pub(crate) fn trb_operation(&mut self, value: u16) -> u16 { let a = self.a & if self.p & M == 0 { 0xFFFF } else { 0xFF }; self.p = (self.p & !Z) | if a & value != 0 { 0 } else { Z }; value & !a } }