// SPDX-License-Identifier: MIT //! Moves: MOV in all its forms, PUSH and POP (`_moves.py`). //! //! 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`. //! //! Differences forced by Rust: none beyond the integer types; POP's `match` on //! `op >> 5` keeps the reference's four arms (any other value is not decoded to //! this handler). use crate::core::{Bus, Spc700}; // The reference's names `_into_a`, `_into_x`, `_into_y`, kept. #[allow(clippy::wrong_self_convention)] impl Spc700 { fn into_a(&mut self, value: u8) { self.a = value; self.set_nz(value); } fn into_x(&mut self, value: u8) { self.x = value; self.set_nz(value); } fn into_y(&mut self, value: u8) { self.y = value; self.set_nz(value); } // ----- immediate ----------------------------------------------------------- /// MOV A,#i -- load an immediate (timing.md immediate). pub(crate) fn op_mov_a_imm(&mut self, _op: u8) { let value = self.fetch(); self.into_a(value); } /// MOV X,#i -- load an immediate (timing.md immediate). pub(crate) fn op_mov_x_imm(&mut self, _op: u8) { let value = self.fetch(); self.into_x(value); } /// MOV Y,#i -- load an immediate (timing.md immediate). pub(crate) fn op_mov_y_imm(&mut self, _op: u8) { let value = self.fetch(); self.into_y(value); } // ----- register to register -------------------------------------------------- /// MOV A,X -- N Z from the value (U13). pub(crate) fn op_mov_a_x(&mut self, _op: u8) { self.read_dummy(self.pc); self.into_a(self.x); } /// MOV A,Y -- N Z from the value (U13). pub(crate) fn op_mov_a_y(&mut self, _op: u8) { self.read_dummy(self.pc); self.into_a(self.y); } /// MOV X,A -- N Z from the value (U13). pub(crate) fn op_mov_x_a(&mut self, _op: u8) { self.read_dummy(self.pc); self.into_x(self.a); } /// MOV Y,A -- N Z from the value (U13). pub(crate) fn op_mov_y_a(&mut self, _op: u8) { self.read_dummy(self.pc); self.into_y(self.a); } /// MOV X,SP -- N Z from the value (U13). pub(crate) fn op_mov_x_sp(&mut self, _op: u8) { self.read_dummy(self.pc); self.into_x(self.sp); } /// MOV SP,X -- the one register MOV that sets no flags (U13). pub(crate) fn op_mov_sp_x(&mut self, _op: u8) { self.read_dummy(self.pc); self.sp = self.x; } // ----- loads ----------------------------------------------------------------- /// MOV A,d -- load from the direct page (timing.md direct). pub(crate) fn op_mov_a_d(&mut self, _op: u8) { let offset = self.fetch() as u32; let value = self.load(offset); self.into_a(value); } /// MOV X,d -- load from the direct page (timing.md direct). pub(crate) fn op_mov_x_d(&mut self, _op: u8) { let offset = self.fetch() as u32; let value = self.load(offset); self.into_x(value); } /// MOV Y,d -- load from the direct page (timing.md direct). pub(crate) fn op_mov_y_d(&mut self, _op: u8) { let offset = self.fetch() as u32; let value = self.load(offset); self.into_y(value); } /// MOV A,d+X -- load, indexed in the page (timing.md direct indexed). pub(crate) fn op_mov_a_dx(&mut self, _op: u8) { let offset = self.direct_x(); let value = self.load(offset); self.into_a(value); } /// MOV Y,d+X -- load, indexed in the page (timing.md direct indexed). pub(crate) fn op_mov_y_dx(&mut self, _op: u8) { let offset = self.direct_x(); let value = self.load(offset); self.into_y(value); } /// MOV X,d+Y -- load, indexed in the page (timing.md direct indexed). pub(crate) fn op_mov_x_dy(&mut self, _op: u8) { let offset = self.fetch() as u32; self.idle(); let value = self.load(offset + self.y as u32); self.into_x(value); } /// MOV A,!a -- load from an absolute address (timing.md absolute). pub(crate) fn op_mov_a_abs(&mut self, _op: u8) { let address = self.fetch_word(); let value = self.read(address); self.into_a(value); } /// MOV X,!a -- load from an absolute address (timing.md absolute). pub(crate) fn op_mov_x_abs(&mut self, _op: u8) { let address = self.fetch_word(); let value = self.read(address); self.into_x(value); } /// MOV Y,!a -- load from an absolute address (timing.md absolute). pub(crate) fn op_mov_y_abs(&mut self, _op: u8) { let address = self.fetch_word(); let value = self.read(address); self.into_y(value); } /// MOV A,!a+X -- load, indexed (timing.md absolute indexed). pub(crate) fn op_mov_a_absx(&mut self, _op: u8) { let address = self.absolute_indexed(self.x); let value = self.read(address); self.into_a(value); } /// MOV A,!a+Y -- load, indexed (timing.md absolute indexed). pub(crate) fn op_mov_a_absy(&mut self, _op: u8) { let address = self.absolute_indexed(self.y); let value = self.read(address); self.into_a(value); } /// MOV A,(X) -- load from X in the direct page (timing.md (X)). pub(crate) fn op_mov_a_ix(&mut self, _op: u8) { self.read_dummy(self.pc); let value = self.load(self.x as u32); self.into_a(value); } /// MOV A,(X)+ -- load, then X += 1; one internal cycle more (timing.md (X)+ load). pub(crate) fn op_mov_a_ixp(&mut self, _op: u8) { self.read_dummy(self.pc); let value = self.load(self.x as u32); self.x = self.x.wrapping_add(1); self.idle(); self.into_a(value); } /// MOV A,[d+X] -- load through a pointer at d+X (timing.md [d+X]). pub(crate) fn op_mov_a_dxi(&mut self, _op: u8) { let address = self.indexed_indirect(); let value = self.read(address); self.into_a(value); } /// MOV A,[d]+Y -- load through a pointer plus Y (timing.md [d]+Y, C4). pub(crate) fn op_mov_a_diy(&mut self, _op: u8) { let address = self.indirect_y_load(); let value = self.read(address); self.into_a(value); } // ----- stores: the destination is read first (U1) -------------------------------- /// The last two cycles of a direct-page store. fn read_then_store(&mut self, target: u32, value: u8) { self.read_dummy(self.dp(target)); self.store(target, value); } /// The last two cycles of a store to a full address. fn read_then_write(&mut self, address: u16, value: u8) { self.read_dummy(address); self.write(address, value); } /// MOV d,A -- reads the destination first (U1, timing.md store direct). pub(crate) fn op_mov_d_a(&mut self, _op: u8) { let target = self.fetch() as u32; self.read_then_store(target, self.a); } /// MOV d,X -- reads the destination first (U1, timing.md store direct). pub(crate) fn op_mov_d_x(&mut self, _op: u8) { let target = self.fetch() as u32; self.read_then_store(target, self.x); } /// MOV d,Y -- reads the destination first (U1, timing.md store direct). pub(crate) fn op_mov_d_y(&mut self, _op: u8) { let target = self.fetch() as u32; self.read_then_store(target, self.y); } /// MOV d+X,A -- reads the destination first (U1, timing.md store direct indexed). pub(crate) fn op_mov_dx_a(&mut self, _op: u8) { let target = self.direct_x(); self.read_then_store(target, self.a); } /// MOV d+X,Y -- reads the destination first (U1, timing.md store direct indexed). pub(crate) fn op_mov_dx_y(&mut self, _op: u8) { let target = self.direct_x(); self.read_then_store(target, self.y); } /// MOV d+Y,X -- reads the destination first (U1, timing.md store direct indexed). pub(crate) fn op_mov_dy_x(&mut self, _op: u8) { let offset = self.fetch() as u32; self.idle(); self.read_then_store((offset + self.y as u32) & 0xFF, self.x); } /// MOV !a,A -- reads the destination first (U1, timing.md store absolute). pub(crate) fn op_mov_abs_a(&mut self, _op: u8) { let address = self.fetch_word(); self.read_then_write(address, self.a); } /// MOV !a,X -- reads the destination first (U1, timing.md store absolute). pub(crate) fn op_mov_abs_x(&mut self, _op: u8) { let address = self.fetch_word(); self.read_then_write(address, self.x); } /// MOV !a,Y -- reads the destination first (U1, timing.md store absolute). pub(crate) fn op_mov_abs_y(&mut self, _op: u8) { let address = self.fetch_word(); self.read_then_write(address, self.y); } /// MOV !a+X,A -- reads the destination first (U1, timing.md store absolute indexed). pub(crate) fn op_mov_absx_a(&mut self, _op: u8) { let address = self.absolute_indexed(self.x); self.read_then_write(address, self.a); } /// MOV !a+Y,A -- reads the destination first (U1, timing.md store absolute indexed). pub(crate) fn op_mov_absy_a(&mut self, _op: u8) { let address = self.absolute_indexed(self.y); self.read_then_write(address, self.a); } /// MOV (X),A -- reads the destination first (U1, timing.md store (X)). pub(crate) fn op_mov_ix_a(&mut self, _op: u8) { self.read_dummy(self.pc); self.read_then_store(self.x as u32, self.a); } /// MOV (X)+,A -- an internal cycle, no read of the target (U1, timing.md store (X)+). pub(crate) fn op_mov_ixp_a(&mut self, _op: u8) { self.read_dummy(self.pc); self.idle(); // anomie 13b: "No RMW here" self.store(self.x as u32, self.a); self.x = self.x.wrapping_add(1); } /// MOV [d+X],A -- store through a pointer, read first (U1, timing.md store [d+X]). pub(crate) fn op_mov_dxi_a(&mut self, _op: u8) { let address = self.indexed_indirect(); self.read_then_write(address, self.a); } /// MOV [d]+Y,A -- store through a pointer plus Y, read first (U1, C4). pub(crate) fn op_mov_diy_a(&mut self, _op: u8) { let offset = self.fetch() as u32; let pointer = self.load_pointer(offset); self.idle(); // after the pointer reads, unlike the read forms: C4 self.read_then_write(pointer.wrapping_add(self.y as u16), self.a); } /// MOV d,#i -- store an immediate, read first (U1, timing.md d,#i RMW). pub(crate) fn op_mov_d_imm(&mut self, _op: u8) { let immediate = self.fetch(); let target = self.fetch() as u32; self.read_dummy(self.dp(target)); // anomie 10: "Yes, RMW even for MOV" self.store(target, immediate); } /// MOV dd,ds -- no read of the destination: it cannot clear a timer (U1, timing.md). pub(crate) fn op_mov_dd_ds(&mut self, _op: u8) { let source = self.fetch() as u32; let value = self.load(source); let target = self.fetch() as u32; self.store(target, value); } // ----- the stack ------------------------------------------------------------- /// PUSH PSW/PUSH A/PUSH X/PUSH Y -- a write, then an internal cycle (timing.md PUSH, C6). pub(crate) fn op_push(&mut self, op: u8) { self.read_dummy(self.pc); let value = [self.psw, self.a, self.x, self.y][(op >> 5) as usize]; self.push(value); self.idle(); // C6: SST and ares; anomie asks "Data-IO-IO or IO-IO-Data?" } /// POP PSW/POP A/POP X/POP Y -- no flags but POP PSW's eight (U13, timing.md POP, C6). pub(crate) fn op_pop(&mut self, op: u8) { self.read_dummy(self.pc); self.idle(); let value = self.pull(); match op >> 5 { 4 => self.psw = value, 5 => self.a = value, 6 => self.x = value, 7 => self.y = value, _ => {} } } }