// SPDX-License-Identifier: MIT //! The 8-bit ALU: OR AND EOR CMP ADC SBC, ASL ROL LSR ROR INC DEC, XCN, DAA, DAS //! (`_alu.py`). //! //! Each handler runs after the opcode fetch and performs the cycles the //! reference's docs/timing.md lists for its addressing-mode group. `op >> 5` //! picks the operation: the map repeats by rows. //! //! Differences forced by Rust: the reference's operation tables `_alu` and //! `_shift` (tuples of bound methods indexed by `op >> 5`) are //! [`Spc700::alu`] and [`Spc700::shift`], a `match` on the same index. The //! arithmetic is in `i32` where Python's integers go below zero or past $FF //! before a mask. use crate::core::{Bus, Spc700}; use crate::flags::{FLAG_C, FLAG_H, FLAG_N, FLAG_V, FLAG_Z}; impl Spc700 { /// `self._alu[index](x, y)`: OR, AND, EOR, CMP, ADC, SBC. pub(crate) fn alu(&mut self, index: u8, x: u8, y: u8) -> u8 { match index { 0 => self.or(x, y), 1 => self.and(x, y), 2 => self.eor(x, y), 3 => self.cmp(x, y), 4 => self.adc(x, y), 5 => self.sbc(x, y), _ => panic!("no ALU operation {index}"), // an IndexError in the reference } } /// `self._shift[index](value)`: ASL, ROL, LSR, ROR, DEC, INC. pub(crate) fn shift(&mut self, index: u8, value: u8) -> u8 { match index { 0 => self.asl(value), 1 => self.rol(value), 2 => self.lsr(value), 3 => self.ror(value), 4 => self.dec(value), 5 => self.inc(value), _ => panic!("no shift operation {index}"), } } // ----- two-operand operations: x op y; the result is what is stored ---------- fn or(&mut self, x: u8, y: u8) -> u8 { let result = x | y; self.set_nz(result); result } fn and(&mut self, x: u8, y: u8) -> u8 { let result = x & y; self.set_nz(result); result } fn eor(&mut self, x: u8, y: u8) -> u8 { let result = x ^ y; self.set_nz(result); result } /// N Z C only, C = no borrow; V and H stay. Returns `x`: nothing changes. pub(crate) fn cmp(&mut self, x: u8, y: u8) -> u8 { let difference = x as i32 - y as i32; let carry = if difference >= 0 { FLAG_C } else { 0 }; self.psw = self.psw & !FLAG_C | carry; self.set_nz((difference & 0xFF) as u8); x } /// C out of bit 7, H out of bit 3, V signed overflow (the reference's docs/start-here.md flags). pub(crate) fn adc(&mut self, x: u8, y: u8) -> u8 { let (x, y) = (x as i32, y as i32); let total = x + y + (self.psw & FLAG_C) as i32; let result = (total & 0xFF) as u8; let mut psw = self.psw & !(FLAG_N | FLAG_V | FLAG_H | FLAG_Z | FLAG_C) | (result & FLAG_N); if total > 0xFF { psw |= FLAG_C; } if (x ^ y ^ total) & 0x10 != 0 { psw |= FLAG_H; } if !(x ^ y) & (x ^ total) & 0x80 != 0 { psw |= FLAG_V; } if result == 0 { psw |= FLAG_Z; } self.psw = psw; result } /// `ADC x, ~y`: C = no borrow, H = no half-borrow. pub(crate) fn sbc(&mut self, x: u8, y: u8) -> u8 { self.adc(x, y ^ 0xFF) } // ----- one-operand operations ------------------------------------------------ fn asl(&mut self, value: u8) -> u8 { let carry = if value & 0x80 != 0 { FLAG_C } else { 0 }; let result = value << 1; self.psw = self.psw & !FLAG_C | carry; self.set_nz(result); result } fn rol(&mut self, value: u8) -> u8 { let carry = if value & 0x80 != 0 { FLAG_C } else { 0 }; let result = value << 1 | self.psw & FLAG_C; self.psw = self.psw & !FLAG_C | carry; self.set_nz(result); result } fn lsr(&mut self, value: u8) -> u8 { let result = value >> 1; self.psw = self.psw & !FLAG_C | (value & FLAG_C); self.set_nz(result); result } fn ror(&mut self, value: u8) -> u8 { let result = (self.psw & FLAG_C) << 7 | value >> 1; self.psw = self.psw & !FLAG_C | (value & FLAG_C); self.set_nz(result); result } pub(crate) fn dec(&mut self, value: u8) -> u8 { let result = value.wrapping_sub(1); self.set_nz(result); result } pub(crate) fn inc(&mut self, value: u8) -> u8 { let result = value.wrapping_add(1); self.set_nz(result); result } // ----- the ALU block: columns $4-$9 of rows $0-$B ---------------------------- /// OR/AND/EOR/CMP/ADC/SBC A,d -- direct page operand (timing.md direct). pub(crate) fn op_alu_a_d(&mut self, op: u8) { let a = self.a; let offset = self.fetch() as u32; let value = self.load(offset); self.a = self.alu(op >> 5, a, value); } /// OR/AND/EOR/CMP/ADC/SBC A,!a -- absolute operand (timing.md absolute). pub(crate) fn op_alu_a_abs(&mut self, op: u8) { let a = self.a; let address = self.fetch_word(); let value = self.read(address); self.a = self.alu(op >> 5, a, value); } /// OR/AND/EOR/CMP/ADC/SBC A,(X) -- operand at X in the direct page (timing.md (X)). pub(crate) fn op_alu_a_ix(&mut self, op: u8) { self.read_dummy(self.pc); let a = self.a; let value = self.load(self.x as u32); self.a = self.alu(op >> 5, a, value); } /// OR/AND/EOR/CMP/ADC/SBC A,[d+X] -- through a pointer at d+X (timing.md [d+X]). pub(crate) fn op_alu_a_dxi(&mut self, op: u8) { let a = self.a; let address = self.indexed_indirect(); let value = self.read(address); self.a = self.alu(op >> 5, a, value); } /// OR/AND/EOR/CMP/ADC/SBC A,#i -- immediate operand (timing.md immediate). pub(crate) fn op_alu_a_imm(&mut self, op: u8) { let a = self.a; let value = self.fetch(); self.a = self.alu(op >> 5, a, value); } /// OR/AND/EOR/CMP/ADC/SBC A,d+X -- indexed direct operand (timing.md direct indexed). pub(crate) fn op_alu_a_dx(&mut self, op: u8) { let a = self.a; let offset = self.direct_x(); let value = self.load(offset); self.a = self.alu(op >> 5, a, value); } /// OR/AND/EOR/CMP/ADC/SBC A,!a+X -- indexed absolute (timing.md absolute indexed). pub(crate) fn op_alu_a_absx(&mut self, op: u8) { let a = self.a; let address = self.absolute_indexed(self.x); let value = self.read(address); self.a = self.alu(op >> 5, a, value); } /// OR/AND/EOR/CMP/ADC/SBC A,!a+Y -- indexed absolute (timing.md absolute indexed). pub(crate) fn op_alu_a_absy(&mut self, op: u8) { let a = self.a; let address = self.absolute_indexed(self.y); let value = self.read(address); self.a = self.alu(op >> 5, a, value); } /// OR/AND/EOR/CMP/ADC/SBC A,[d]+Y -- pointer in the page, plus Y (timing.md [d]+Y, C4). pub(crate) fn op_alu_a_diy(&mut self, op: u8) { let a = self.a; let address = self.indirect_y_load(); let value = self.read(address); self.a = self.alu(op >> 5, a, value); } /// OR/AND/EOR/CMP/ADC/SBC dd,ds -- memory to memory (timing.md dd,ds RMW). pub(crate) fn op_alu_dd_ds(&mut self, op: u8) { let source = self.fetch() as u32; // the source is stored first let right = self.load(source); let target = self.fetch() as u32; let left = self.load(target); let result = self.alu(op >> 5, left, right); if op >> 5 == 3 { self.idle(); // C8: CMP does not write; anomie asks "IO or read again?", SST says IO } else { self.store(target, result); } } /// OR/AND/EOR/CMP/ADC/SBC d,#i -- immediate onto memory (timing.md d,#i RMW). pub(crate) fn op_alu_d_imm(&mut self, op: u8) { let immediate = self.fetch(); // the immediate is stored first let target = self.fetch() as u32; let value = self.load(target); let result = self.alu(op >> 5, value, immediate); if op >> 5 == 3 { self.idle(); // C8 } else { self.store(target, result); } } /// OR/AND/EOR/CMP/ADC/SBC (X),(Y) -- memory at Y onto memory at X (timing.md (X),(Y)). pub(crate) fn op_alu_ix_iy(&mut self, op: u8) { self.read_dummy(self.pc); let right = self.load(self.y as u32); let left = self.load(self.x as u32); let result = self.alu(op >> 5, left, right); if op >> 5 == 3 { self.idle(); // C8: anomie "it is an IO cycle" } else { self.store(self.x as u32, result); } } // ----- CMP X and CMP Y ------------------------------------------------------ /// CMP X,#i -- compare X with an immediate (timing.md immediate). pub(crate) fn op_cmp_x_imm(&mut self, _op: u8) { let x = self.x; let value = self.fetch(); self.cmp(x, value); } /// CMP X,d -- compare X with a direct-page byte (timing.md direct). pub(crate) fn op_cmp_x_d(&mut self, _op: u8) { let x = self.x; let offset = self.fetch() as u32; let value = self.load(offset); self.cmp(x, value); } /// CMP X,!a -- compare X with an absolute byte (timing.md absolute). pub(crate) fn op_cmp_x_abs(&mut self, _op: u8) { let x = self.x; let address = self.fetch_word(); let value = self.read(address); self.cmp(x, value); } /// CMP Y,#i -- compare Y with an immediate (timing.md immediate). pub(crate) fn op_cmp_y_imm(&mut self, _op: u8) { let y = self.y; let value = self.fetch(); self.cmp(y, value); } /// CMP Y,d -- compare Y with a direct-page byte (timing.md direct). pub(crate) fn op_cmp_y_d(&mut self, _op: u8) { let y = self.y; let offset = self.fetch() as u32; let value = self.load(offset); self.cmp(y, value); } /// CMP Y,!a -- compare Y with an absolute byte (timing.md absolute). pub(crate) fn op_cmp_y_abs(&mut self, _op: u8) { let y = self.y; let address = self.fetch_word(); let value = self.read(address); self.cmp(y, value); } // ----- shifts, INC and DEC on memory and registers --------------------------- /// ASL/ROL/LSR/ROR/DEC/INC d -- read, modify, write (timing.md RMW direct). pub(crate) fn op_shift_d(&mut self, op: u8) { let target = self.fetch() as u32; let value = self.load(target); let result = self.shift(op >> 5, value); self.store(target, result); } /// ASL/ROL/LSR/ROR/DEC/INC d+X -- read, modify, write (timing.md RMW direct indexed). pub(crate) fn op_shift_dx(&mut self, op: u8) { let target = self.direct_x(); let value = self.load(target); let result = self.shift(op >> 5, value); self.store(target, result); } /// ASL/ROL/LSR/ROR/DEC/INC !a -- read, modify, write (timing.md RMW absolute). pub(crate) fn op_shift_abs(&mut self, op: u8) { let address = self.fetch_word(); let value = self.read(address); let result = self.shift(op >> 5, value); self.write(address, result); } /// ASL/ROL/LSR/ROR/DEC/INC A -- the accumulator (timing.md implied, register). pub(crate) fn op_shift_a(&mut self, op: u8) { self.read_dummy(self.pc); self.a = self.shift(op >> 5, self.a); } /// INC X -- N Z from the result (timing.md implied, register). pub(crate) fn op_inc_x(&mut self, _op: u8) { self.read_dummy(self.pc); self.x = self.inc(self.x); } /// DEC X -- N Z from the result (timing.md implied, register). pub(crate) fn op_dec_x(&mut self, _op: u8) { self.read_dummy(self.pc); self.x = self.dec(self.x); } /// INC Y -- N Z from the result (timing.md implied, register). pub(crate) fn op_inc_y(&mut self, _op: u8) { self.read_dummy(self.pc); self.y = self.inc(self.y); } /// DEC Y -- N Z from the result (timing.md implied, register). pub(crate) fn op_dec_y(&mut self, _op: u8) { self.read_dummy(self.pc); self.y = self.dec(self.y); } // ----- A alone --------------------------------------------------------------- /// XCN A -- swap the nibbles of A; N Z from the result (timing.md XCN). pub(crate) fn op_xcn(&mut self, _op: u8) { self.read_dummy(self.pc); self.idle(); self.idle(); self.idle(); self.a = self.a.rotate_left(4); self.set_nz(self.a); } /// DAA A -- BCD adjust after an addition; H stays (U4). pub(crate) fn op_daa(&mut self, _op: u8) { self.read_dummy(self.pc); self.idle(); if self.psw & FLAG_C != 0 || self.a > 0x99 { self.a = self.a.wrapping_add(0x60); self.psw |= FLAG_C; } // U4: this test sees the A the first adjustment left if self.psw & FLAG_H != 0 || self.a & 0x0F > 9 { self.a = self.a.wrapping_add(0x06); } self.set_nz(self.a); } /// DAS A -- BCD adjust after a subtraction; H stays (U4). pub(crate) fn op_das(&mut self, _op: u8) { self.read_dummy(self.pc); self.idle(); if self.psw & FLAG_C == 0 || self.a > 0x99 { self.a = self.a.wrapping_sub(0x60); self.psw &= !FLAG_C; } // U4: this test sees the A the first adjustment left if self.psw & FLAG_H == 0 || self.a & 0x0F > 9 { self.a = self.a.wrapping_sub(0x06); } self.set_nz(self.a); } }