// 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, 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 the reference's, checked there against //! SingleStepTests' decimal cases (its rung 1). The reference names bsnes's //! `processor/wdc65816/algorithms.cpp` as the adder's model (emulator tier); //! this file is a transcription of the reference only. //! //! **Provenance open, inherited:** the reference's own audit (its //! docs/provenance.md, item 2) finds its `add` and `subtract` a restructured //! translation of bsnes's GPL-3.0 `algorithmADC8/16` and `SBC8/16`; the same //! algorithm is in ares (ISC). Whether to re-attribute it to ares with the ISC //! notice or rewrite it from the datasheet and SingleStepTests is Aubrey's call, //! made in the reference first; this file follows the reference by merge. //! //! 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 //! `i64`, which behaves the same for every value these sums reach (a decimal //! subtraction's digit can go below zero before it is masked). 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, 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) } } /// 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) { let (a, value) = (a as i64, value as i64); let mut p = p; let mut carry = (p & C) as i64; let top: i64 = if wide { 0x8000 } else { 0x80 }; let mut result: i64; if p & 0x08 == 0 { 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; let last = if wide { 12 } else { 4 }; let mut shift = 0; while shift < if wide { 16 } else { 8 } { let mask = 0xF_i64 << shift; let mut digit = (a & mask) + (value & mask) + (carry << shift) + result; if shift < last && digit > ((9_i64 << shift) | ((1_i64 << shift) - 1)) { digit += 6_i64 << shift; } carry = if digit > (0x10_i64 << shift) - 1 { 1 } else { 0 }; result = if shift < last { digit & ((0x10_i64 << shift) - 1) } else { digit }; shift += 4; } } p &= !(N | V | Z | C); if !(a ^ value) & (a ^ result) & top != 0 { p |= V; } if p & 0x08 != 0 && result > if wide { 0x9FFF } else { 0x9F } { result += if wide { 0x6000 } else { 0x60 }; } if result > if wide { 0xFFFF } else { 0xFF } { p |= C; } result &= if wide { 0xFFFF } else { 0xFF }; (result as u16, p | nz(result, wide)) } /// A - M - (1 - C): A + ~M + C, with decimal digits adjusted downwards. // The reference writes `digit <= (0x10 << shift) - 1`; kept as written. #[allow(clippy::int_plus_one)] pub fn subtract(a: u16, value: u16, p: u8, wide: bool) -> (u16, u8) { let mut p = p; let mask: i64 = if wide { 0xFFFF } else { 0xFF }; let a = a as i64; let value = !(value as i64) & mask; let top: i64 = if wide { 0x8000 } else { 0x80 }; let mut carry = (p & C) as i64; let mut result: i64; if p & 0x08 == 0 { result = a + value + carry; } else { result = 0; let last = if wide { 12 } else { 4 }; let mut shift = 0; while shift < last + 4 { let digit_mask = 0xF_i64 << shift; let mut digit = (a & digit_mask) + (value & digit_mask) + (carry << shift) + result; if shift < last { if digit <= (0x10_i64 << shift) - 1 { digit -= 6_i64 << shift; } carry = if digit > (0x10_i64 << shift) - 1 { 1 } else { 0 }; result = digit & ((0x10_i64 << shift) - 1); } else { result = digit; } shift += 4; } } p &= !(N | V | Z | C); if !(a ^ value) & (a ^ result) & top != 0 { p |= V; } if p & 0x08 != 0 && result <= mask { result -= if wide { 0x6000 } else { 0x60 }; } if result > mask { p |= C; } result &= mask; (result as u16, p | nz(result, 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 } }