// SPDX-License-Identifier: MIT //! Loads, stores, transfers, register increments, XBA and XCE (`_loads.py`). //! //! Loads and stores take the width of the register they name (DS note 1). A //! transfer takes its destination's width; TCD, TDC, TCS and TSC move all 16 //! bits whatever M says; TCS and TXS keep S in page one in emulation mode (U5). //! //! Differences forced by Rust: `_set_index(name, value)` names its register by //! a string; here it is [`Index`]. Its `value` may be below zero or above //! $FFFF in Python (DEX of 0, INX of $FFFF) before the mask; here it is a //! `u32` computed with wrapping arithmetic, which the same mask reduces to the //! same value. use crate::core::{nz16, nz8, Bus, C, M, N, W65C816, X, Z}; use crate::dispatch::Mode; /// Which index register `set_index` writes (the reference's `name`). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum Index { X, Y, } impl W65C816 { pub(crate) fn set_index(&mut self, name: Index, value: u32) { // With X = 1 the high bytes of X and Y are 0 (DS section 2.7). let value = if self.p & X != 0 { let value = value & 0xFF; self.p = (self.p & !(N | Z)) | nz8(value); value } else { let value = value & 0xFFFF; self.p = (self.p & !(N | Z)) | nz16(value); value }; match name { Index::X => self.x = value as u16, Index::Y => self.y = value as u16, } } pub(crate) fn set_accumulator(&mut self, value: u32) { if self.p & M != 0 { let value = value & 0xFF; self.a = (self.a & 0xFF00) | value as u16; self.p = (self.p & !(N | Z)) | nz8(value); } else { self.a = (value & 0xFFFF) as u16; self.p = (self.p & !(N | Z)) | nz16(self.a as u32); } } // -- loads and stores ------------------------------------------------- /// LDA -- M -> A (DS Table 5-5 p. 32). pub(crate) fn op_lda(&mut self, mode: Mode) { let address = self.ea(mode); let value = self.load(address, self.p & M == 0); self.set_accumulator(value as u32); } /// LDX -- M -> X (DS Table 5-5 p. 33). pub(crate) fn op_ldx(&mut self, mode: Mode) { let address = self.ea(mode); let value = self.load(address, self.p & X == 0); self.set_index(Index::X, value as u32); } /// LDY -- M -> Y (DS Table 5-5 p. 33). pub(crate) fn op_ldy(&mut self, mode: Mode) { let address = self.ea(mode); let value = self.load(address, self.p & X == 0); self.set_index(Index::Y, value as u32); } /// STA -- A -> M (DS Table 5-5 p. 33). pub(crate) fn op_sta(&mut self, mode: Mode) { let address = self.ea(mode); self.store(address, self.a, self.p & M == 0); } /// STX -- X -> M (DS Table 5-5 p. 33). pub(crate) fn op_stx(&mut self, mode: Mode) { let address = self.ea(mode); self.store(address, self.x, self.p & X == 0); } /// STY -- Y -> M (DS Table 5-5 p. 34). pub(crate) fn op_sty(&mut self, mode: Mode) { let address = self.ea(mode); self.store(address, self.y, self.p & X == 0); } /// STZ -- 0 -> M (DS Table 5-5 p. 34). pub(crate) fn op_stz(&mut self, mode: Mode) { let address = self.ea(mode); self.store(address, 0, self.p & M == 0); } // -- transfers (row 19a: the opcode, then an IO cycle at PC) ------------ /// TAX -- A -> X (DS Table 5-5 p. 34). pub(crate) fn op_tax(&mut self) { self.io_pc(); self.set_index(Index::X, self.a as u32); } /// TAY -- A -> Y (DS Table 5-5 p. 34). pub(crate) fn op_tay(&mut self) { self.io_pc(); self.set_index(Index::Y, self.a as u32); } /// TXA -- X -> A (DS Table 5-5 p. 34). pub(crate) fn op_txa(&mut self) { self.io_pc(); self.set_accumulator(self.x as u32); } /// TYA -- Y -> A (DS Table 5-5 p. 34). pub(crate) fn op_tya(&mut self) { self.io_pc(); self.set_accumulator(self.y as u32); } /// TXY -- X -> Y (DS Table 5-5 p. 34). pub(crate) fn op_txy(&mut self) { self.io_pc(); self.set_index(Index::Y, self.x as u32); } /// TYX -- Y -> X (DS Table 5-5 p. 34). pub(crate) fn op_tyx(&mut self) { self.io_pc(); self.set_index(Index::X, self.y as u32); } /// TSX -- S -> X (DS Table 5-5 p. 34). pub(crate) fn op_tsx(&mut self) { self.io_pc(); self.set_index(Index::X, self.s as u32); } /// TXS -- X -> S, no flags (DS Table 5-5 p. 34). pub(crate) fn op_txs(&mut self) { self.io_pc(); self.s = if self.e != 0 { 0x0100 | (self.x & 0xFF) } else { self.x }; } /// TCS -- C -> S, all 16 bits, no flags (DS Table 5-5 p. 34). pub(crate) fn op_tcs(&mut self) { self.io_pc(); self.s = if self.e != 0 { 0x0100 | (self.a & 0xFF) } else { self.a }; } /// TSC -- S -> C, all 16 bits (DS Table 5-5 p. 34). pub(crate) fn op_tsc(&mut self) { self.io_pc(); self.a = self.s; self.p = (self.p & !(N | Z)) | nz16(self.s as u32); } /// TCD -- C -> D, all 16 bits (DS Table 5-5 p. 34). pub(crate) fn op_tcd(&mut self) { self.io_pc(); self.d = self.a; self.p = (self.p & !(N | Z)) | nz16(self.a as u32); } /// TDC -- D -> C, all 16 bits (DS Table 5-5 p. 34). pub(crate) fn op_tdc(&mut self) { self.io_pc(); self.a = self.d; self.p = (self.p & !(N | Z)) | nz16(self.d as u32); } /// XBA -- swap A and B; N and Z from the new A (DS Table 5-5 p. 34). pub(crate) fn op_xba(&mut self) { self.io_pc(); self.io_pc(); // row 19b: two IO cycles self.a = (self.a >> 8) | (self.a & 0xFF) << 8; self.p = (self.p & !(N | Z)) | nz8((self.a & 0xFF) as u32); } /// XCE -- exchange C and E (DS Table 5-5 p. 34). pub(crate) fn op_xce(&mut self) { self.io_pc(); let carry = self.p & C; self.p = (self.p & !C) | self.e; self.e = carry; if carry != 0 { // Into emulation mode: M = X = 1, XH = YH = 0, SH = $01 (U5). self.p |= M | X; self.x &= 0xFF; self.y &= 0xFF; self.s = 0x0100 | (self.s & 0xFF); } } // -- register increments ------------------------------------------------ /// INX -- X + 1 -> X (DS Table 5-5 p. 32). pub(crate) fn op_inx(&mut self) { self.io_pc(); self.set_index(Index::X, (self.x as u32).wrapping_add(1)); } /// INY -- Y + 1 -> Y (DS Table 5-5 p. 32). pub(crate) fn op_iny(&mut self) { self.io_pc(); self.set_index(Index::Y, (self.y as u32).wrapping_add(1)); } /// DEX -- X - 1 -> X (DS Table 5-5 p. 32). pub(crate) fn op_dex(&mut self) { self.io_pc(); self.set_index(Index::X, (self.x as u32).wrapping_sub(1)); } /// DEY -- Y - 1 -> Y (DS Table 5-5 p. 32). pub(crate) fn op_dey(&mut self) { self.io_pc(); self.set_index(Index::Y, (self.y as u32).wrapping_sub(1)); } }