# SPDX-License-Identifier: MIT """Moves: MOV in all its forms, PUSH and POP. MOV into a register sets N Z, except ``MOV SP,X`` (U13); MOV into memory sets none. Stores read their destination first, a real bus read whose value is discarded (U1): on the S-SMP that read clears a timer counter at $FD-$FF. The exceptions are ``MOV dd,ds`` and ``MOV (X)+,A``. """ from __future__ import annotations from spc700_python._core import CoreMixin class MoveMixin(CoreMixin): def _into_a(self, value: int) -> None: self.a = value self._set_nz(value) def _into_x(self, value: int) -> None: self.x = value self._set_nz(value) def _into_y(self, value: int) -> None: self.y = value self._set_nz(value) # ----- immediate ----------------------------------------------------------- def _op_mov_a_imm(self, op: int) -> None: """MOV A,#i -- load an immediate (timing.md immediate).""" self._into_a(self._fetch()) def _op_mov_x_imm(self, op: int) -> None: """MOV X,#i -- load an immediate (timing.md immediate).""" self._into_x(self._fetch()) def _op_mov_y_imm(self, op: int) -> None: """MOV Y,#i -- load an immediate (timing.md immediate).""" self._into_y(self._fetch()) # ----- register to register -------------------------------------------------- def _op_mov_a_x(self, op: int) -> None: """MOV A,X -- N Z from the value (U13).""" self._read_dummy(self.pc) self._into_a(self.x) def _op_mov_a_y(self, op: int) -> None: """MOV A,Y -- N Z from the value (U13).""" self._read_dummy(self.pc) self._into_a(self.y) def _op_mov_x_a(self, op: int) -> None: """MOV X,A -- N Z from the value (U13).""" self._read_dummy(self.pc) self._into_x(self.a) def _op_mov_y_a(self, op: int) -> None: """MOV Y,A -- N Z from the value (U13).""" self._read_dummy(self.pc) self._into_y(self.a) def _op_mov_x_sp(self, op: int) -> None: """MOV X,SP -- N Z from the value (U13).""" self._read_dummy(self.pc) self._into_x(self.sp) def _op_mov_sp_x(self, op: int) -> None: """MOV SP,X -- the one register MOV that sets no flags (U13).""" self._read_dummy(self.pc) self.sp = self.x # ----- loads ----------------------------------------------------------------- def _op_mov_a_d(self, op: int) -> None: """MOV A,d -- load from the direct page (timing.md direct).""" self._into_a(self._load(self._fetch())) def _op_mov_x_d(self, op: int) -> None: """MOV X,d -- load from the direct page (timing.md direct).""" self._into_x(self._load(self._fetch())) def _op_mov_y_d(self, op: int) -> None: """MOV Y,d -- load from the direct page (timing.md direct).""" self._into_y(self._load(self._fetch())) def _op_mov_a_dx(self, op: int) -> None: """MOV A,d+X -- load, indexed in the page (timing.md direct indexed).""" self._into_a(self._load(self._direct_x())) def _op_mov_y_dx(self, op: int) -> None: """MOV Y,d+X -- load, indexed in the page (timing.md direct indexed).""" self._into_y(self._load(self._direct_x())) def _op_mov_x_dy(self, op: int) -> None: """MOV X,d+Y -- load, indexed in the page (timing.md direct indexed).""" offset = self._fetch() self._idle() self._into_x(self._load(offset + self.y)) def _op_mov_a_abs(self, op: int) -> None: """MOV A,!a -- load from an absolute address (timing.md absolute).""" self._into_a(self._read(self._fetch_word())) def _op_mov_x_abs(self, op: int) -> None: """MOV X,!a -- load from an absolute address (timing.md absolute).""" self._into_x(self._read(self._fetch_word())) def _op_mov_y_abs(self, op: int) -> None: """MOV Y,!a -- load from an absolute address (timing.md absolute).""" self._into_y(self._read(self._fetch_word())) def _op_mov_a_absx(self, op: int) -> None: """MOV A,!a+X -- load, indexed (timing.md absolute indexed).""" self._into_a(self._read(self._absolute_indexed(self.x))) def _op_mov_a_absy(self, op: int) -> None: """MOV A,!a+Y -- load, indexed (timing.md absolute indexed).""" self._into_a(self._read(self._absolute_indexed(self.y))) def _op_mov_a_ix(self, op: int) -> None: """MOV A,(X) -- load from X in the direct page (timing.md (X)).""" self._read_dummy(self.pc) self._into_a(self._load(self.x)) def _op_mov_a_ixp(self, op: int) -> None: """MOV A,(X)+ -- load, then X += 1; one internal cycle more (timing.md (X)+ load).""" self._read_dummy(self.pc) value = self._load(self.x) self.x = (self.x + 1) & 0xFF self._idle() self._into_a(value) def _op_mov_a_dxi(self, op: int) -> None: """MOV A,[d+X] -- load through a pointer at d+X (timing.md [d+X]).""" self._into_a(self._read(self._indexed_indirect())) def _op_mov_a_diy(self, op: int) -> None: """MOV A,[d]+Y -- load through a pointer plus Y (timing.md [d]+Y, C4).""" self._into_a(self._read(self._indirect_y_load())) # ----- stores: the destination is read first (U1) -------------------------------- def _read_then_store(self, target: int, value: int) -> None: """The last two cycles of a direct-page store.""" self._read_dummy(self._dp(target)) self._store(target, value) def _read_then_write(self, address: int, value: int) -> None: """The last two cycles of a store to a full address.""" self._read_dummy(address) self._write(address, value) def _op_mov_d_a(self, op: int) -> None: """MOV d,A -- reads the destination first (U1, timing.md store direct).""" self._read_then_store(self._fetch(), self.a) def _op_mov_d_x(self, op: int) -> None: """MOV d,X -- reads the destination first (U1, timing.md store direct).""" self._read_then_store(self._fetch(), self.x) def _op_mov_d_y(self, op: int) -> None: """MOV d,Y -- reads the destination first (U1, timing.md store direct).""" self._read_then_store(self._fetch(), self.y) def _op_mov_dx_a(self, op: int) -> None: """MOV d+X,A -- reads the destination first (U1, timing.md store direct indexed).""" self._read_then_store(self._direct_x(), self.a) def _op_mov_dx_y(self, op: int) -> None: """MOV d+X,Y -- reads the destination first (U1, timing.md store direct indexed).""" self._read_then_store(self._direct_x(), self.y) def _op_mov_dy_x(self, op: int) -> None: """MOV d+Y,X -- reads the destination first (U1, timing.md store direct indexed).""" offset = self._fetch() self._idle() self._read_then_store((offset + self.y) & 0xFF, self.x) def _op_mov_abs_a(self, op: int) -> None: """MOV !a,A -- reads the destination first (U1, timing.md store absolute).""" self._read_then_write(self._fetch_word(), self.a) def _op_mov_abs_x(self, op: int) -> None: """MOV !a,X -- reads the destination first (U1, timing.md store absolute).""" self._read_then_write(self._fetch_word(), self.x) def _op_mov_abs_y(self, op: int) -> None: """MOV !a,Y -- reads the destination first (U1, timing.md store absolute).""" self._read_then_write(self._fetch_word(), self.y) def _op_mov_absx_a(self, op: int) -> None: """MOV !a+X,A -- reads the destination first (U1, timing.md store absolute indexed).""" self._read_then_write(self._absolute_indexed(self.x), self.a) def _op_mov_absy_a(self, op: int) -> None: """MOV !a+Y,A -- reads the destination first (U1, timing.md store absolute indexed).""" self._read_then_write(self._absolute_indexed(self.y), self.a) def _op_mov_ix_a(self, op: int) -> None: """MOV (X),A -- reads the destination first (U1, timing.md store (X)).""" self._read_dummy(self.pc) self._read_then_store(self.x, self.a) def _op_mov_ixp_a(self, op: int) -> None: """MOV (X)+,A -- an internal cycle, no read of the target (U1, timing.md store (X)+).""" self._read_dummy(self.pc) self._idle() # anomie 13b: "No RMW here" self._store(self.x, self.a) self.x = (self.x + 1) & 0xFF def _op_mov_dxi_a(self, op: int) -> None: """MOV [d+X],A -- store through a pointer, read first (U1, timing.md store [d+X]).""" self._read_then_write(self._indexed_indirect(), self.a) def _op_mov_diy_a(self, op: int) -> None: """MOV [d]+Y,A -- store through a pointer plus Y, read first (U1, C4).""" pointer = self._load_pointer(self._fetch()) self._idle() # after the pointer reads, unlike the read forms: C4 self._read_then_write((pointer + self.y) & 0xFFFF, self.a) def _op_mov_d_imm(self, op: int) -> None: """MOV d,#i -- store an immediate, read first (U1, timing.md d,#i RMW).""" immediate = self._fetch() target = self._fetch() self._read_dummy(self._dp(target)) # anomie 10: "Yes, RMW even for MOV" self._store(target, immediate) def _op_mov_dd_ds(self, op: int) -> None: """MOV dd,ds -- no read of the destination: it cannot clear a timer (U1, timing.md).""" value = self._load(self._fetch()) self._store(self._fetch(), value) # ----- the stack ------------------------------------------------------------- def _op_push(self, op: int) -> None: """PUSH PSW/PUSH A/PUSH X/PUSH Y -- a write, then an internal cycle (timing.md PUSH, C6).""" self._read_dummy(self.pc) self._push((self.psw, self.a, self.x, self.y)[op >> 5]) self._idle() # C6: SST and ares; anomie asks "Data-IO-IO or IO-IO-Data?" def _op_pop(self, op: int) -> None: """POP PSW/POP A/POP X/POP Y -- no flags but POP PSW's eight (U13, timing.md POP, C6).""" self._read_dummy(self.pc) self._idle() value = self._pull() match op >> 5: case 4: self.psw = value case 5: self.a = value case 6: self.x = value case 7: self.y = value