// ---- from src/alu.rs lines 1-147 //! Arithmetic, logic, compare and bit test: ADC SBC AND ORA EOR CMP CPX CPY //! BIT (`_alu.py`). //! //! `ORA`, `AND`, `EOR` and `ADC` honour the T flag (U4): with T set the //! accumulator is replaced by the zero-page byte at X (logical `$2000 + X`), //! the result goes back there, A is untouched and the instruction takes three //! more cycles (read, internal, write). `SBC` does not (C5: SW's entry has no //! T clause; MAME's yes is a guess), unless `sbc_honours_t` is set. //! //! Decimal mode (U14, HW HB-4, SW `ADC`): one more cycle, N, Z and C taken //! from the decimal result, **V left alone**. How invalid digits adjust is //! C13: neither manual says. `decimal_formula` picks the digit adjust. The //! default, `Mednafen`, gives the results Mednafen's HuC6280 gives //! ("corrected ... per tests on a HuC6280", its ChangeLog, 2015-04-29: the //! one author statement of hardware testing); `Mesen` gives Mesen's, the //! results the 640,000-case SingleStepTests corpus was made with. They //! differ only with an invalid BCD digit (the reference's docs/issues/0004). //! Both are transcribed from the reference's own routines, which were //! written from the rules stated in words there and checked against recorded //! result tables; neither emulator's code is copied. //! //! Differences forced by Rust: the digit arithmetic is done in `i32`, with //! `rem_euclid` where the reference's `%` meets a negative number (Python's //! `%` takes the sign of the divisor); an unknown formula cannot be named, //! so the reference's `ValueError` has no counterpart. use crate::core::{Bus, HuC6280, C, D, N, V, Z, ZERO_PAGE}; use crate::dispatch::Mode; /// C13: the decimal digit adjust of ADC and SBC (`decimal_formula`). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum DecimalFormula { /// The default: the results Mednafen gives. Mednafen, /// The results Mesen gives, which the SingleStepTests corpus records. Mesen, } /// N and Z for a result that has not been masked to 8 bits yet. fn nz(value: i32) -> u8 { (value as u8 & N) | if value & 0xFF != 0 { 0 } else { Z } } /// The decimal sum before masking: 256 and up is the carry out. /// /// Written from the rule each emulator's result table follows (the /// reference's docs/issues/0004 states both in words), digit by digit; no /// emulator's source is reproduced here. fn decimal_add(a: i32, operand: i32, carry: i32, formula: DecimalFormula) -> i32 { let mut ones = a % 16 + operand % 16 + carry; let sixteens = a / 16 + operand / 16; if ones >= 10 { ones += 6; // a digit past 9 skips the six unused codes } let mut total = match formula { // Everything the ones digit overflowed by is carried, however much that is. DecimalFormula::Mednafen => 16 * sixteens + ones, // One carry at most, and only the low four bits of the ones digit are kept. DecimalFormula::Mesen => 16 * (sixteens + if ones >= 16 { 1 } else { 0 }) + ones % 16, }; if total >= 0xA0 { total += 0x60; } total } /// The decimal difference: 256 and up means no borrow (carry out). Written as above. fn decimal_subtract(a: i32, operand: i32, carry: i32, formula: DecimalFormula) -> i32 { match formula { DecimalFormula::Mednafen => { // Digit differences with a borrow between them; each digit that // borrowed is corrected (by 6, by $60) on the assembled byte, // modulo 256. let ones = a % 16 - operand % 16 - (1 - carry); let sixteens = a / 16 - operand / 16 - if ones < 0 { 1 } else { 0 }; let mut byte = 16 * sixteens.rem_euclid(16) + ones.rem_euclid(16); if ones < 0 { byte -= 0x06; } if sixteens < 0 { byte -= 0x60; } byte.rem_euclid(256) + if sixteens < 0 { 0 } else { 256 } } DecimalFormula::Mesen => { // The sum with the operand's complement; a digit that produced no // carry is corrected before the next is formed, and the // correction is not reduced modulo 256 here. let complement = 255 - operand; let mut ones = a % 16 + complement % 16 + carry; if ones < 16 { ones -= 6; } let mut total = 16 * (a / 16 + complement / 16 + if ones >= 16 { 1 } else { 0 }) + ones.rem_euclid(16); if total < 256 { total -= 0x60; } total } } } /// A + operand + C, binary or decimal; return (result, P). pub fn add(a: u8, operand: u8, p: u8, formula: DecimalFormula) -> (u8, u8) { let (a, operand) = (a as i32, operand as i32); let carry = (p & C) as i32; let mut p = p; let result; if p & D != 0 { result = decimal_add(a, operand, carry, formula); p &= !(N | Z | C); // V is unchanged in decimal mode (U14) } else { result = a + operand + carry; p &= !(N | V | Z | C); if !(a ^ operand) & (a ^ result) & 0x80 != 0 { p |= V; } } if result > 0xFF { p |= C; } (result as u8, p | nz(result)) } /// A - operand - (1 - C), binary or decimal; return (result, P). pub fn subtract(a: u8, operand: u8, p: u8, formula: DecimalFormula) -> (u8, u8) { let (a, operand) = (a as i32, operand as i32); let carry = (p & C) as i32; let mut p = p; let result; if p & D != 0 { result = decimal_subtract(a, operand, carry, formula); p &= !(N | Z | C); } else { let inverted = !operand & 0xFF; result = a + inverted + carry; p &= !(N | V | Z | C); if !(a ^ inverted) & (a ^ result) & 0x80 != 0 { p |= V; } } if result > 0xFF { p |= C; } (result as u8, p | nz(result)) }