// SPDX-License-Identifier: MIT //! Arithmetic, logic and compare instructions: ADC SBC AND ORA EOR CMP CPX CPY //! BIT (`_alu.py`). //! //! 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. //! //! [`decimal_add`] and [`decimal_subtract`] are pure functions, kept public //! for another core that needs the 65C02's decimal results (the reference's //! docs/handoff-brief.md, "Recommendation"). //! //! Differences forced by Rust: a handler receives its operand's value (the //! dispatch has run the `rd_` mode) where the Python one calls `mode(self)` //! as its first statement; the order of bus cycles is the same. The Python //! integers are unbounded, so the arithmetic here is done in `i32`/`u32`. use crate::core::{Bus, Cmos65c02, C, D, N, NZ, V, Z}; // 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). /// Decimal `a + operand + carry` on a 65C02: (accumulator, carry out). /// /// BC Appendix A, sequence 1, line by line. pub fn decimal_add(a: u8, operand: u8, carry: u8) -> (u8, u8) { // DOC (BC); MEASURED on two parts (CAP, Clark's test); U11 for the others. let (a, operand, carry) = (a as u32, operand as u32, carry as u32); let mut low = (a & 0x0F) + (operand & 0x0F) + carry; // BC 1a if low >= 0x0A { low = ((low + 0x06) & 0x0F) + 0x10; // BC 1b } let mut total = (a & 0xF0) + (operand & 0xF0) + low; // BC 1c if total >= 0xA0 { total += 0x60; // BC 1e } ((total & 0xFF) as u8, (total >= 0x100) as u8) // BC 1f, 1g } /// 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). pub fn decimal_add_overflow(a: u8, operand: u8, carry: u8) -> bool { // DOC (BC, CL section 8); MEASURED on two parts (CAP: Clark's test checks V). let (a, operand, carry) = (a as u32, operand as u32, carry as u32); let mut low = (a & 0x0F) + (operand & 0x0F) + carry; // BC 2a if low >= 0x0A { low = ((low + 0x06) & 0x0F) + 0x10; // BC 2b } let total = (a & 0xF0) + (operand & 0xF0) + low; // BC 2c, here unsigned !(a ^ operand) & (a ^ total) & 0x80 != 0 // BC 2f: operands agree in sign, sum differs } /// 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. pub fn decimal_subtract(a: u8, operand: u8, carry: u8) -> (u8, u8) { // DOC (BC); MEASURED on two parts (CAP, Clark's test); U11 for the others. let (a, operand, carry) = (a as i32, operand as i32, carry as i32); let low = (a & 0x0F) - (operand & 0x0F) + carry - 1; // BC 4a let mut total = a - operand + carry - 1; // BC 4b let carry_out = (total >= 0) as u8; if total < 0 { total -= 0x60; // BC 4c } if low < 0 { total -= 0x06; // BC 4d } ((total & 0xFF) as u8, carry_out) // BC 4e } impl Cmos65c02 { /// 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). fn decimal_cycle(&mut self) { // MEASURED (CAP: the cycle and its address); DOC (DS Table 7-1: the cycle). self.fetch_discard(); self.extra += 1; } /// ADC -- add memory and carry to A (DS Table 6-4). pub(crate) fn op_adc(&mut self, operand: u8) { // Binary: DOC (DS Table 6-4), MEASURED (CAP, Dormann). Decimal: see the module note. let a = self.a; let p = self.p; let mut carry = p & C; let result; let overflow; if p & D != 0 { self.decimal_cycle(); (result, carry) = decimal_add(a, operand, carry); overflow = decimal_add_overflow(a, operand, p & C); } else { let total = a as u32 + operand as u32 + carry as u32; result = (total & 0xFF) as u8; carry = (total >> 8) as u8; overflow = !(a ^ operand) & (a ^ result) & 0x80 != 0; } 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 as usize] | if overflow { V } else { 0 } | carry; } /// SBC -- subtract memory and borrow from A (DS Table 6-4). pub(crate) fn op_sbc(&mut self, operand: u8) { // 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). let a = self.a; let p = self.p; let mut carry = p & C; let total = a as i32 - operand as i32 + carry as i32 - 1; // V and C are the binary subtraction's in both modes (BC Appendix A). let overflow = (a as i32 ^ operand as i32) & (a as i32 ^ total) & 0x80 != 0; let result; if p & D != 0 { self.decimal_cycle(); (result, carry) = decimal_subtract(a, operand, carry); } else { result = (total & 0xFF) as u8; carry = (total >= 0) as u8; } self.a = result; self.p = (p & !(N | V | Z | C)) | NZ[result as usize] | if overflow { V } 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). /// AND -- A AND memory (DS Table 6-4). pub(crate) fn op_and(&mut self, operand: u8) { self.a = self.set_nz(self.a & operand); } /// ORA -- A OR memory (DS Table 6-4). pub(crate) fn op_ora(&mut self, operand: u8) { self.a = self.set_nz(self.a | operand); } /// EOR -- A exclusive-OR memory (DS Table 6-4). pub(crate) fn op_eor(&mut self, operand: u8) { self.a = self.set_nz(self.a ^ operand); } /// N, Z and C from `register - operand`; nothing is stored. fn compare(&mut self, register: u8, operand: u8) { let difference = register.wrapping_sub(operand); self.p = (self.p & !(N | Z | C)) | NZ[difference as usize] | if register >= operand { C } else { 0 }; } /// CMP -- compare memory with A (DS Table 6-4). pub(crate) fn op_cmp(&mut self, operand: u8) { self.compare(self.a, operand); } /// CPX -- compare memory with X (DS Table 6-4). pub(crate) fn op_cpx(&mut self, operand: u8) { self.compare(self.x, operand); } /// CPY -- compare memory with Y (DS Table 6-4). pub(crate) fn op_cpy(&mut self, operand: u8) { self.compare(self.y, operand); } /// BIT -- Z from A AND memory, N and V from memory bits 7 and 6 (DS Table 6-4 note 2). pub(crate) fn op_bit(&mut self, operand: u8) { self.p = (self.p & !(N | V | Z)) | (operand & (N | V)) | if self.a & operand != 0 { 0 } else { Z }; } /// BIT #imm -- Z from A AND the operand; N and V are untouched (CL section 2). pub(crate) fn op_bit_imm(&mut self) { // DOC (CL section 2); MEASURED (CAP: Dormann's 65C02 test checks it on both parts). let operand = self.fetch_byte(); self.p = (self.p & !Z) | if self.a & operand != 0 { 0 } else { Z }; } }