# SPDX-License-Identifier: MIT """The 8-bit ALU: OR AND EOR CMP ADC SBC, ASL ROL LSR ROR INC DEC, XCN, DAA, DAS. Each handler runs after the opcode fetch and performs the cycles docs/timing.md lists for its addressing-mode group. ``op >> 5`` picks the operation: the map repeats by rows. """ from __future__ import annotations from spc700_python._core import CoreMixin from spc700_python._flags import FLAG_C, FLAG_H, FLAG_N, FLAG_V, FLAG_Z class ALUMixin(CoreMixin): def _init_alu(self) -> None: """The operation tables, indexed by ``op >> 5``.""" self._alu = (self._or, self._and, self._eor, self._cmp, self._adc, self._sbc) self._shift = (self._asl, self._rol, self._lsr, self._ror, self._dec, self._inc) # ----- two-operand operations: x op y; the result is what is stored ---------- def _or(self, x: int, y: int) -> int: result = x | y self._set_nz(result) return result def _and(self, x: int, y: int) -> int: result = x & y self._set_nz(result) return result def _eor(self, x: int, y: int) -> int: result = x ^ y self._set_nz(result) return result def _cmp(self, x: int, y: int) -> int: """N Z C only, C = no borrow; V and H stay. Returns ``x``: nothing changes.""" difference = x - y carry = FLAG_C if difference >= 0 else 0 self.psw = self.psw & ~FLAG_C | carry self._set_nz(difference & 0xFF) return x def _adc(self, x: int, y: int) -> int: """C out of bit 7, H out of bit 3, V signed overflow (docs/start-here.md flags).""" total = x + y + (self.psw & FLAG_C) result = total & 0xFF 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: psw |= FLAG_H if ~(x ^ y) & (x ^ total) & 0x80: psw |= FLAG_V if result == 0: psw |= FLAG_Z self.psw = psw return result def _sbc(self, x: int, y: int) -> int: """``ADC x, ~y``: C = no borrow, H = no half-borrow.""" return self._adc(x, y ^ 0xFF) # ----- one-operand operations ------------------------------------------------ def _asl(self, value: int) -> int: carry = FLAG_C if value & 0x80 else 0 result = value << 1 & 0xFF self.psw = self.psw & ~FLAG_C | carry self._set_nz(result) return result def _rol(self, value: int) -> int: carry = FLAG_C if value & 0x80 else 0 result = (value << 1 | self.psw & FLAG_C) & 0xFF self.psw = self.psw & ~FLAG_C | carry self._set_nz(result) return result def _lsr(self, value: int) -> int: result = value >> 1 self.psw = self.psw & ~FLAG_C | (value & FLAG_C) self._set_nz(result) return result def _ror(self, value: int) -> int: result = (self.psw & FLAG_C) << 7 | value >> 1 self.psw = self.psw & ~FLAG_C | (value & FLAG_C) self._set_nz(result) return result def _dec(self, value: int) -> int: result = (value - 1) & 0xFF self._set_nz(result) return result def _inc(self, value: int) -> int: result = (value + 1) & 0xFF self._set_nz(result) return result # ----- the ALU block: columns $4-$9 of rows $0-$B ---------------------------- def _op_alu_a_d(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC A,d -- direct page operand (timing.md direct).""" self.a = self._alu[op >> 5](self.a, self._load(self._fetch())) def _op_alu_a_abs(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC A,!a -- absolute operand (timing.md absolute).""" self.a = self._alu[op >> 5](self.a, self._read(self._fetch_word())) def _op_alu_a_ix(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC A,(X) -- operand at X in the direct page (timing.md (X)).""" self._read_dummy(self.pc) self.a = self._alu[op >> 5](self.a, self._load(self.x)) def _op_alu_a_dxi(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC A,[d+X] -- through a pointer at d+X (timing.md [d+X]).""" self.a = self._alu[op >> 5](self.a, self._read(self._indexed_indirect())) def _op_alu_a_imm(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC A,#i -- immediate operand (timing.md immediate).""" self.a = self._alu[op >> 5](self.a, self._fetch()) def _op_alu_a_dx(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC A,d+X -- indexed direct operand (timing.md direct indexed).""" self.a = self._alu[op >> 5](self.a, self._load(self._direct_x())) def _op_alu_a_absx(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC A,!a+X -- indexed absolute (timing.md absolute indexed).""" self.a = self._alu[op >> 5](self.a, self._read(self._absolute_indexed(self.x))) def _op_alu_a_absy(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC A,!a+Y -- indexed absolute (timing.md absolute indexed).""" self.a = self._alu[op >> 5](self.a, self._read(self._absolute_indexed(self.y))) def _op_alu_a_diy(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC A,[d]+Y -- pointer in the page, plus Y (timing.md [d]+Y, C4).""" self.a = self._alu[op >> 5](self.a, self._read(self._indirect_y_load())) def _op_alu_dd_ds(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC dd,ds -- memory to memory (timing.md dd,ds RMW).""" source = self._fetch() # the source is stored first right = self._load(source) target = self._fetch() left = self._load(target) 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) def _op_alu_d_imm(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC d,#i -- immediate onto memory (timing.md d,#i RMW).""" immediate = self._fetch() # the immediate is stored first target = self._fetch() result = self._alu[op >> 5](self._load(target), immediate) if op >> 5 == 3: self._idle() # C8 else: self._store(target, result) def _op_alu_ix_iy(self, op: int) -> None: """OR/AND/EOR/CMP/ADC/SBC (X),(Y) -- memory at Y onto memory at X (timing.md (X),(Y)).""" self._read_dummy(self.pc) right = self._load(self.y) result = self._alu[op >> 5](self._load(self.x), right) if op >> 5 == 3: self._idle() # C8: anomie "it is an IO cycle" else: self._store(self.x, result) # ----- CMP X and CMP Y ------------------------------------------------------ def _op_cmp_x_imm(self, op: int) -> None: """CMP X,#i -- compare X with an immediate (timing.md immediate).""" self._cmp(self.x, self._fetch()) def _op_cmp_x_d(self, op: int) -> None: """CMP X,d -- compare X with a direct-page byte (timing.md direct).""" self._cmp(self.x, self._load(self._fetch())) def _op_cmp_x_abs(self, op: int) -> None: """CMP X,!a -- compare X with an absolute byte (timing.md absolute).""" self._cmp(self.x, self._read(self._fetch_word())) def _op_cmp_y_imm(self, op: int) -> None: """CMP Y,#i -- compare Y with an immediate (timing.md immediate).""" self._cmp(self.y, self._fetch()) def _op_cmp_y_d(self, op: int) -> None: """CMP Y,d -- compare Y with a direct-page byte (timing.md direct).""" self._cmp(self.y, self._load(self._fetch())) def _op_cmp_y_abs(self, op: int) -> None: """CMP Y,!a -- compare Y with an absolute byte (timing.md absolute).""" self._cmp(self.y, self._read(self._fetch_word())) # ----- shifts, INC and DEC on memory and registers --------------------------- def _op_shift_d(self, op: int) -> None: """ASL/ROL/LSR/ROR/DEC/INC d -- read, modify, write (timing.md RMW direct).""" target = self._fetch() self._store(target, self._shift[op >> 5](self._load(target))) def _op_shift_dx(self, op: int) -> None: """ASL/ROL/LSR/ROR/DEC/INC d+X -- read, modify, write (timing.md RMW direct indexed).""" target = self._direct_x() self._store(target, self._shift[op >> 5](self._load(target))) def _op_shift_abs(self, op: int) -> None: """ASL/ROL/LSR/ROR/DEC/INC !a -- read, modify, write (timing.md RMW absolute).""" address = self._fetch_word() self._write(address, self._shift[op >> 5](self._read(address))) def _op_shift_a(self, op: int) -> None: """ASL/ROL/LSR/ROR/DEC/INC A -- the accumulator (timing.md implied, register).""" self._read_dummy(self.pc) self.a = self._shift[op >> 5](self.a) def _op_inc_x(self, op: int) -> None: """INC X -- N Z from the result (timing.md implied, register).""" self._read_dummy(self.pc) self.x = self._inc(self.x) def _op_dec_x(self, op: int) -> None: """DEC X -- N Z from the result (timing.md implied, register).""" self._read_dummy(self.pc) self.x = self._dec(self.x) def _op_inc_y(self, op: int) -> None: """INC Y -- N Z from the result (timing.md implied, register).""" self._read_dummy(self.pc) self.y = self._inc(self.y) def _op_dec_y(self, op: int) -> None: """DEC Y -- N Z from the result (timing.md implied, register).""" self._read_dummy(self.pc) self.y = self._dec(self.y) # ----- A alone --------------------------------------------------------------- def _op_xcn(self, op: int) -> None: """XCN A -- swap the nibbles of A; N Z from the result (timing.md XCN).""" self._read_dummy(self.pc) self._idle() self._idle() self._idle() self.a = (self.a >> 4 | self.a << 4) & 0xFF self._set_nz(self.a) def _op_daa(self, op: int) -> None: """DAA A -- BCD adjust after an addition; H stays (U4).""" self._read_dummy(self.pc) self._idle() if self.psw & FLAG_C or self.a > 0x99: self.a = (self.a + 0x60) & 0xFF self.psw |= FLAG_C # U4: this test sees the A the first adjustment left if self.psw & FLAG_H or self.a & 0x0F > 9: self.a = (self.a + 0x06) & 0xFF self._set_nz(self.a) def _op_das(self, op: int) -> None: """DAS A -- BCD adjust after a subtraction; H stays (U4).""" self._read_dummy(self.pc) self._idle() if not self.psw & FLAG_C or self.a > 0x99: self.a = (self.a - 0x60) & 0xFF self.psw &= ~FLAG_C # U4: this test sees the A the first adjustment left if not self.psw & FLAG_H or self.a & 0x0F > 9: self.a = (self.a - 0x06) & 0xFF self._set_nz(self.a)