// SPDX-License-Identifier: MIT //! Registers, the cycle primitives, operand access and the addressing modes //! (`_core.py`). //! //! Every cycle of the 65C816 is one callback, IO cycles included, each with the //! 24-bit address the chip drives and its kind (VDA, VPA, VPB, MLB), which the //! callback receives in [`Signals`]. The addressing modes below are written //! cycle by cycle from the datasheet's Table 5-7 (DS, WDC W65C816S, 2024-03-13, //! pp. 36-42, rows cited as "row 10a"), with the emulation-mode wrapping rules //! of the reference's docs/undocumented-behavior.md U1-U4 layered on top. //! //! Differences forced by Rust: //! //! - A Python callback reads `cpu.vda`, `cpu.vpa`, `cpu.vpb`, `cpu.mlb`, //! `cpu.e`, `cpu.m` and `cpu.x_flag` off the CPU object while the CPU is //! calling it. A Rust callback cannot borrow the CPU then, so every callback //! receives [`Signals`], a copy of exactly those, with `&mut Frame` for its //! interrupt requests (and `note_stall`), as in 6502-rust and w65c02-rust. //! - The Python frame swaps its bus callables for confirming wrappers when a //! request changes; here every read and write calls [`Frame::access`] first, //! which is the same check (6502-rust's frame). An IO cycle confirms the same //! way, as the reference's `_io` calls `_confirm()` when `_deferred` is set. //! - Python's integers have no width; here the registers are `u16` and `u8`, //! addresses `u32`, and the reference's masks are kept where it writes them //! (a sum that Python masks is computed in `u32` and masked the same way). //! - `_modify` takes the operation as a [`Modify`] value (the Python one a //! bound method or a closure). use crate::dispatch::Mode; use sixfiveohtwo::frame::Frame; // P (DS section 2.9, Figure 2-2). In emulation mode bits 5 and 4 are not M // and X: the core keeps both set there, so that M and X read as 1 (8-bit) // without a test of E in every handler. pub const N: u8 = 0x80; pub const V: u8 = 0x40; pub const M: u8 = 0x20; // accumulator and memory width: 1 = 8-bit pub const X: u8 = 0x10; // index width: 1 = 8-bit (emulation mode: the B bit of a pushed P) pub const D: u8 = 0x08; pub const I: u8 = sixfiveohtwo::frame::I; pub const Z: u8 = 0x02; pub const C: u8 = 0x01; // A cycle's kind: which of VDA, VPA, VPB and MLB the chip asserts (DS // section 2.19-2.22). VPB and MLB are active-low pins; here 1 means asserted. pub const VDA: u8 = 0x01; pub const VPA: u8 = 0x02; pub const VPB: u8 = 0x04; pub const MLB: u8 = 0x08; pub const OPCODE: u8 = VDA | VPA; // the opcode fetch: both, as the 6502's SYNC pub const PROGRAM: u8 = VPA; // an operand byte from the instruction stream pub const DATA: u8 = VDA; pub const VECTOR: u8 = VDA | VPB; pub const IO: u8 = 0; // an internal cycle: neither, the address "invalid" (DS section 7.5) pub fn nz8(value: u32) -> u8 { (value as u8 & N) | if value & 0xFF != 0 { 0 } else { Z } } pub fn nz16(value: u32) -> u8 { ((value >> 8) as u8 & N) | if value != 0 { 0 } else { Z } } /// What a host may read of the CPU inside a bus callback: the reference's /// `vda`, `vpa`, `vpb`, `mlb` (from `kind`), `e`, `m` and `x_flag`. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct Signals { /// The cycle's kind: a combination of [`VDA`], [`VPA`], [`VPB`], [`MLB`]. pub kind: u8, /// The E pin: emulation mode. pub e: bool, /// The M flag as the M/X pin shows it: true for an 8-bit accumulator. pub m: bool, /// The X flag as the M/X pin shows it: true for 8-bit index registers. pub x_flag: bool, } impl Signals { /// Valid data address: the cycle reads or writes data. pub fn vda(&self) -> bool { self.kind & VDA != 0 } /// Valid program address: the cycle reads the instruction stream. pub fn vpa(&self) -> bool { self.kind & VPA != 0 } /// Vector pull: the cycle reads an interrupt vector. pub fn vpb(&self) -> bool { self.kind & VPB != 0 } /// Memory lock: the cycle is part of a read-modify-write. pub fn mlb(&self) -> bool { self.kind & MLB != 0 } } /// The host's side of the bus: one call per cycle, in the chip's order. /// /// `read(address)` and `write(address, value)` are the bus, with 24-bit /// addresses (bank << 16 | offset); `idle(address)` is called for each IO /// cycle, when the chip drives an address and asserts neither VDA nor VPA /// (the default touches nothing, as the reference's `_no_idle`). A callback /// may change interrupt requests through `frame`. pub trait Bus { fn read(&mut self, address: u32, frame: &mut Frame, signals: Signals) -> u8; fn write(&mut self, address: u32, value: u8, frame: &mut Frame, signals: Signals); fn idle(&mut self, _address: u32, _frame: &mut Frame, _signals: Signals) {} } /// The operation of a read-modify-write (`_modify`'s `operation`). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum Modify { Asl, Lsr, Rol, Ror, Inc, Dec, Tsb, Trb, } /// A WDC 65C816 core whose bus is a [`Bus`] supplied by a host (`W65C816`). /// /// Every cycle is exactly one callback, in the chip's order, and `step()` /// returns how many it made. Registers are plain fields: `a` (C, 16 bits), /// `x`, `y`, `s`, `d`, `pc` (16 bits), `dbr`, `pbr`, `p` (8 bits) and `e`. A /// new CPU is in emulation mode; [`Frame::request_reset`] runs the start /// sequence. The interrupt inputs, their poll and `step()`'s skeleton are /// 6502-rust's [`Frame`] and [`sixfiveohtwo::frame::Part`]. pub struct W65C816 { pub a: u16, // C, all 16 bits; A is the low byte, B the high pub x: u16, pub y: u16, pub s: u16, pub d: u16, pub dbr: u8, pub pbr: u8, pub pc: u16, pub p: u8, pub e: u8, pub waiting: bool, // WAI has run; an interrupt request ends it pub stopped: bool, // STP has run; only reset ends it pub bus: T, pub frame: Frame, pub(crate) kind: u8, pub(crate) cycles: u32, // How the second byte of a 16-bit operand follows the first: set by // every addressing mode (row notes). In the bank: the offset wraps at // $FFFF and the bank stays (program bytes, direct page and stack in // bank 0). Not in the bank: the carry reaches the bank (DS section // 7.3, U4). pub(crate) in_bank: bool, pub(crate) operand_kind: u8, } impl W65C816 { pub(crate) fn init_registers(bus: T) -> Self { // The registers hold whatever they held until reset; zero makes // traces reproducible. Reset itself sets only what DS section 2.25 // lists (see accept_reset). W65C816 { a: 0, x: 0, y: 0, s: 0x01FF, d: 0, dbr: 0, pbr: 0, pc: 0, p: M | X | I, e: 1, waiting: false, stopped: false, bus, frame: Frame::default(), kind: IO, cycles: 0, in_bank: false, operand_kind: DATA, } } // -- the pins as a callback sees them ------------------------------- #[inline] pub(crate) fn signals(&self) -> Signals { Signals { kind: self.kind, e: self.e != 0, m: self.p & M != 0, x_flag: self.p & X != 0, } } // -- cycles ---------------------------------------------------------- #[inline] pub(crate) fn cycle_read(&mut self, address: u32, kind: u8) -> u8 { self.kind = kind; self.cycles += 1; self.frame.access(); let signals = self.signals(); self.bus.read(address, &mut self.frame, signals) } #[inline] pub(crate) fn cycle_write(&mut self, address: u32, value: u8, kind: u8) { self.kind = kind; self.cycles += 1; self.frame.access(); let signals = self.signals(); self.bus.write(address, value, &mut self.frame, signals); } /// An internal cycle: the address is driven, nothing is read or written. #[inline] pub(crate) fn io(&mut self, address: u32, kind: u8) { self.kind = kind; self.cycles += 1; if self.frame.deferred.is_some() { self.frame.access(); } let signals = self.signals(); self.bus.idle(address, &mut self.frame, signals); } /// One operand byte at PBR,PC; PC wraps within the bank (U4). #[inline] pub(crate) fn fetch(&mut self) -> u8 { let pc = self.pc; self.pc = pc.wrapping_add(1); self.cycle_read((self.pbr as u32) << 16 | pc as u32, PROGRAM) } pub(crate) fn fetch_word(&mut self) -> u16 { let low = self.fetch() as u16; (self.fetch() as u16) << 8 | low } /// The IO cycle Table 5-7 drives at the last operand byte's address (PBR,PC-1). pub(crate) fn io_operand(&mut self) { self.io((self.pbr as u32) << 16 | self.pc.wrapping_sub(1) as u32, IO); } /// The IO cycle at PBR,PC: an implied instruction's second cycle (row 19a). pub(crate) fn io_pc(&mut self) { self.io((self.pbr as u32) << 16 | self.pc as u32, IO); } // -- operands of either width --------------------------------------- /// The address of a 16-bit operand's high byte, as the mode left it. pub(crate) fn next(&self, address: u32) -> u32 { if self.in_bank { return (address & 0xFF0000) | ((address + 1) & 0xFFFF); } (address + 1) & 0xFFFFFF } /// Data low, then with 16-bit data the high byte at the next address (note 1). pub(crate) fn load(&mut self, address: u32, wide: bool) -> u16 { let kind = self.operand_kind; let low = self.cycle_read(address, kind) as u16; if !wide { return low; } let high = self.next(address); (self.cycle_read(high, kind) as u16) << 8 | low } pub(crate) fn store(&mut self, address: u32, value: u16, wide: bool) { self.cycle_write(address, value as u8, DATA); if wide { let high = self.next(address); self.cycle_write(high, (value >> 8) as u8, DATA); } } /// A read-modify-write (row 1d): read, modify cycle, write, with MLB throughout. /// /// 16-bit data reads low then high and writes high then low. The modify /// cycle is an IO cycle in native mode; in emulation mode RWB is low and /// the old value is on the bus, with VDA low (note 17; contested, C3): /// the core reports it as a write and the board decides whether it /// reaches anything. pub(crate) fn modify(&mut self, address: u32, wide: bool, operation: Modify) { let high_address = self.next(address); let mut value = self.cycle_read(address, DATA | MLB) as u16; if wide { value |= (self.cycle_read(high_address, DATA | MLB) as u16) << 8; } if self.e != 0 { self.cycle_write(address, value as u8, MLB); } else { self.io(if wide { high_address } else { address }, MLB); } let result = self.operate(operation, value); if wide { self.cycle_write(high_address, (result >> 8) as u8, DATA | MLB); } self.cycle_write(address, result as u8, DATA | MLB); } fn operate(&mut self, operation: Modify, value: u16) -> u16 { match operation { Modify::Asl => self.asl(value), Modify::Lsr => self.lsr(value), Modify::Rol => self.rol(value), Modify::Ror => self.ror(value), Modify::Inc => self.inc(value), Modify::Dec => self.dec(value), Modify::Tsb => self.tsb_operation(value), Modify::Trb => self.trb_operation(value), } } // -- addressing modes (Table 5-7) ----------------------------------- // Each makes its addressing cycles and returns the 24-bit address of the // operand's low byte, having set in_bank and operand_kind for its high // byte. The `_w` variants are for stores and read-modify-writes, which // spend note 4's IO cycle whatever the index. /// The mode's addressing cycles (the reference calls `mode(self)`). #[inline] pub(crate) fn ea(&mut self, mode: Mode) -> u32 { match mode { Mode::ImmM => self.ea_imm_m(), Mode::ImmX => self.ea_imm_x(), Mode::Abs => self.ea_abs(), Mode::Abx => self.ea_abx(), Mode::Aby => self.ea_aby(), Mode::AbxW => self.ea_abx_w(), Mode::AbyW => self.ea_aby_w(), Mode::Long => self.ea_long(), Mode::LongX => self.ea_long_x(), Mode::Dp => self.ea_dp(), Mode::Dpx => self.ea_dpx(), Mode::Dpy => self.ea_dpy(), Mode::Idx => self.ea_idx(), Mode::Ind => self.ea_ind(), Mode::Idy => self.ea_idy(), Mode::IdyW => self.ea_idy_w(), Mode::IndLong => self.ea_ind_long(), Mode::IndLongY => self.ea_ind_long_y(), Mode::Sr => self.ea_sr(), Mode::SrY => self.ea_sr_y(), } } /// `#` with the accumulator's width (row 18). fn ea_imm_m(&mut self) -> u32 { self.immediate(self.p & M == 0) } /// `#` with the index registers' width (row 18). fn ea_imm_x(&mut self) -> u32 { self.immediate(self.p & X == 0) } fn immediate(&mut self, wide: bool) -> u32 { let pc = self.pc; self.pc = pc.wrapping_add(if wide { 2 } else { 1 }); self.in_bank = true; self.operand_kind = PROGRAM; (self.pbr as u32) << 16 | pc as u32 } fn data_mode(&mut self, in_bank: bool) { self.in_bank = in_bank; self.operand_kind = DATA; } /// `a`: DBR,AA (row 1a). fn ea_abs(&mut self) -> u32 { let address = self.fetch_word(); self.data_mode(false); (self.dbr as u32) << 16 | address as u32 } fn indexed(&mut self, base: u32, index: u32, write: bool) -> u32 { // Note 4: an IO cycle at DBR,AAH,(AAL+index) for a page crossed, a // store, or 16-bit index registers; the index carries into the bank. if write || self.p & X == 0 || (base & 0xFF) + index > 0xFF { self.io( (self.dbr as u32) << 16 | (base & 0xFF00) | ((base + index) & 0xFF), IO, ); } self.data_mode(false); (((self.dbr as u32) << 16) + base + index) & 0xFFFFFF } /// `a,x` (row 6a). fn ea_abx(&mut self) -> u32 { let base = self.fetch_word() as u32; self.indexed(base, self.x as u32, false) } /// `a,y` (row 7). fn ea_aby(&mut self) -> u32 { let base = self.fetch_word() as u32; self.indexed(base, self.y as u32, false) } /// `a,x` for a store or read-modify-write (rows 6a, 6b). fn ea_abx_w(&mut self) -> u32 { let base = self.fetch_word() as u32; self.indexed(base, self.x as u32, true) } /// `a,y` for a store (row 7). fn ea_aby_w(&mut self) -> u32 { let base = self.fetch_word() as u32; self.indexed(base, self.y as u32, true) } /// `al`: AAB,AA (row 4a). fn ea_long(&mut self) -> u32 { let address = self.fetch_word() as u32; let bank = self.fetch() as u32; self.data_mode(false); bank << 16 | address } /// `al,x`: AAB,AA+X, the index carrying into the bank (row 5). fn ea_long_x(&mut self) -> u32 { let address = self.fetch_word() as u32; let bank = self.fetch() as u32; self.data_mode(false); ((bank << 16) + address + self.x as u32) & 0xFFFFFF } /// The direct-page operand, with note 2's IO cycle when D.l is not 0. pub(crate) fn direct_offset(&mut self) -> u32 { let offset = self.fetch() as u32; if self.d & 0xFF != 0 { self.io_operand(); } offset } /// Bank 0, D + offset. U1: in emulation mode with D.l = 0 the 6502's /// modes stay in page D.h; otherwise the sum wraps at $FFFF. fn direct(&self, offset: u32) -> u32 { if self.e != 0 && self.d & 0xFF == 0 { return (self.d as u32 & 0xFF00) | (offset & 0xFF); } (self.d as u32 + offset) & 0xFFFF } /// `d` (row 10a). fn ea_dp(&mut self) -> u32 { let offset = self.direct_offset(); let address = self.direct(offset); self.data_mode(true); address } /// `d,x` (row 16a). fn ea_dpx(&mut self) -> u32 { let offset = self.direct_offset(); self.io_operand(); let address = self.direct(offset + self.x as u32); self.data_mode(true); address } /// `d,y` (row 17). fn ea_dpy(&mut self) -> u32 { let offset = self.direct_offset(); self.io_operand(); let address = self.direct(offset + self.y as u32); self.data_mode(true); address } fn pointer16(&mut self, low_address: u32, high_address: u32) -> u32 { let low = self.cycle_read(low_address, DATA) as u32; (self.cycle_read(high_address, DATA) as u32) << 8 | low } /// A two-byte pointer in the direct page; U1's page wrap applies to both bytes. fn direct_pointer(&mut self, offset: u32) -> u32 { let low_address = self.direct(offset); let high_address = self.direct(offset + 1); self.pointer16(low_address, high_address) } /// `(d,x)` (row 11). /// /// U3 (C4): in emulation mode with D.l not 0 the pointer's high byte comes /// from the low byte's page, as gilyon's cputest, bsnes, ares, Mesen2 and /// MAME have it; the corpus disagrees and those cases are excluded. fn ea_idx(&mut self) -> u32 { let offset = self.direct_offset(); self.io_operand(); let pointer = if self.e != 0 && self.d & 0xFF != 0 { let low_address = (self.d as u32 + offset + self.x as u32) & 0xFFFF; let high_address = (low_address & 0xFF00) | ((low_address + 1) & 0xFF); self.pointer16(low_address, high_address) } else { self.direct_pointer(offset + self.x as u32) }; self.data_mode(false); (self.dbr as u32) << 16 | pointer } /// `(d)` (row 12). fn ea_ind(&mut self) -> u32 { let offset = self.direct_offset(); let pointer = self.direct_pointer(offset); self.data_mode(false); (self.dbr as u32) << 16 | pointer } fn indirect_y(&mut self, write: bool) -> u32 { let offset = self.direct_offset(); let pointer = self.direct_pointer(offset); self.indexed(pointer, self.y as u32, write) } /// `(d),y` (row 13). fn ea_idy(&mut self) -> u32 { self.indirect_y(false) } /// `(d),y` for a store (row 13). fn ea_idy_w(&mut self) -> u32 { self.indirect_y(true) } fn long_pointer(&mut self) -> u32 { // [d] is a "new" mode: no page wrap in emulation mode (U1), bank 0. let base = self.d as u32 + self.direct_offset(); let low = self.cycle_read(base & 0xFFFF, DATA) as u32; let high = self.cycle_read((base + 1) & 0xFFFF, DATA) as u32; let bank = self.cycle_read((base + 2) & 0xFFFF, DATA) as u32; bank << 16 | high << 8 | low } /// `[d]` (row 15). fn ea_ind_long(&mut self) -> u32 { let address = self.long_pointer(); self.data_mode(false); address } /// `[d],y` (row 14). fn ea_ind_long_y(&mut self) -> u32 { let address = (self.long_pointer() + self.y as u32) & 0xFFFFFF; self.data_mode(false); address } /// `d,s`: bank 0, S + offset (row 23). fn ea_sr(&mut self) -> u32 { let offset = self.fetch() as u32; self.io_operand(); self.data_mode(true); (self.s as u32 + offset) & 0xFFFF } /// `(d,s),y` (row 24). fn ea_sr_y(&mut self) -> u32 { let offset = self.fetch() as u32; self.io_operand(); let base = self.s as u32 + offset; let pointer = self.pointer16(base & 0xFFFF, (base + 1) & 0xFFFF); self.io((base + 1) & 0xFFFF, IO); self.data_mode(false); (((self.dbr as u32) << 16) + pointer + self.y as u32) & 0xFFFFFF } // -- the stack (bank 0; S points at the next free byte) ------------- /// Push for the 6502's instructions: in emulation mode S wraps in page one (U2). pub(crate) fn push(&mut self, value: u8) { let s = self.s; self.cycle_write(s as u32, value, DATA); self.s = if self.e != 0 { (s & 0xFF00) | (s.wrapping_sub(1) & 0xFF) } else { s.wrapping_sub(1) }; } pub(crate) fn pull(&mut self) -> u8 { let mut s = self.s; s = if self.e != 0 { (s & 0xFF00) | (s.wrapping_add(1) & 0xFF) } else { s.wrapping_add(1) }; self.s = s; self.cycle_read(s as u32, DATA) } /// Push for the new instructions: S steps in 16 bits even in emulation mode (U2). pub(crate) fn push_new(&mut self, value: u8) { let s = self.s; self.cycle_write(s as u32, value, DATA); self.s = s.wrapping_sub(1); } pub(crate) fn pull_new(&mut self) -> u8 { let s = self.s.wrapping_add(1); self.s = s; self.cycle_read(s as u32, DATA) } /// After a new instruction in emulation mode, SH is forced back to $01 (U2). pub(crate) fn fix_stack(&mut self) { if self.e != 0 { self.s = 0x0100 | (self.s & 0xFF); } } }