// SPDX-License-Identifier: MIT //! Registers, the bus, addressing helpers and the shape of `step()` (`_core.py`). //! //! The bus contract (the reference's docs/handoff-brief.md "The bus contract"): //! every SPC700 cycle is one callback, in chip order, dummy reads and internal //! cycles included. A step starts with the opcode fetch and ends after the //! instruction's last cycle, the order SingleStepTests records. //! //! Differences forced by Rust: //! //! - A Python callback reads `cpu.psw`, `cpu.opcode`, `cpu.cycle`, //! `cpu.stopped` and `cpu.dummy` 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. //! - The class is one struct, [`Spc700`]; the mixins of the six instruction //! modules are `impl` blocks in the files of the same names. //! - `cycle` starts each step at -1, so it is an `i32`. //! - `_read` masks the host's value with `& 0xFF`; the Rust host returns a `u8`. use crate::flags::{FLAG_N, FLAG_P, FLAG_Z}; pub const STACK_PAGE: u16 = 0x0100; /// A 16-bit value from its two bytes. pub fn pack(high: u8, low: u8) -> u16 { (high as u16) << 8 | low as u16 } /// What a host may read of the CPU inside a bus callback. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct Signals { /// PSW as it stands (with P: U9). pub psw: u8, /// The opcode of the instruction in progress; during the fetch cycle itself, the previous one. pub opcode: u8, /// The index of this cycle within the instruction, 0 = the fetch. pub cycle: i32, pub stopped: bool, /// This read's value will be discarded (tooling). pub dummy: bool, } /// The host's side of the bus: one call per cycle, in the chip's order. /// /// `read(address)` is one cycle that reads, `write(address, value)` one that /// writes, and `idle()` one internal cycle, which has no address (C1); the /// default `idle` touches nothing (the reference's `_no_idle`). pub trait Bus { fn read(&mut self, address: u16, signals: Signals) -> u8; fn write(&mut self, address: u16, value: u8, signals: Signals); fn idle(&mut self, _signals: Signals) {} } /// A Sony SPC700 core whose memory is a [`Bus`] supplied by a host (`SPC700`). /// /// There is no interrupt input (U11). A newly constructed CPU has the state /// [`crate::CpuState`] defaults to: ares' power-on values (U12), all settable. pub struct Spc700 { pub a: u8, pub x: u8, pub y: u8, pub sp: u8, pub pc: u16, pub psw: u8, pub opcode: u8, pub cycle: i32, pub dummy: bool, pub stopped: bool, pub bus: T, } impl Spc700 { /// Y:A, not a register of its own: the two bytes paired (fullsnes "Registers"). pub fn ya(&self) -> u16 { (self.y as u16) << 8 | self.a as u16 } pub fn set_ya(&mut self, value: u16) { self.y = (value >> 8 & 0xFF) as u8; self.a = (value & 0xFF) as u8; } #[inline] pub(crate) fn signals(&self) -> Signals { Signals { psw: self.psw, opcode: self.opcode, cycle: self.cycle, stopped: self.stopped, dummy: self.dummy, } } // ----- the bus: one callback per cycle -------------------------------- /// One cycle that reads. #[inline] pub(crate) fn read(&mut self, address: u16) -> u8 { self.cycle += 1; let signals = self.signals(); self.bus.read(address, signals) } /// One cycle that reads and discards: the read still happens on the bus, so a /// dummy read of $FD-$FF clears a timer on the board (U1, U10). `dummy` is /// true during it. #[inline] pub(crate) fn read_dummy(&mut self, address: u16) { self.cycle += 1; self.dummy = true; let signals = self.signals(); self.bus.read(address, signals); self.dummy = false; } /// One cycle that writes. #[inline] pub(crate) fn write(&mut self, address: u16, value: u8) { self.cycle += 1; let signals = self.signals(); self.bus.write(address, value, signals); } /// One internal cycle: no address (C1); the board prices it from opcode and cycle (C3). #[inline] pub(crate) fn idle(&mut self) { self.cycle += 1; let signals = self.signals(); self.bus.idle(signals); } // ----- operands ------------------------------------------------------- /// Operand fetch: read at PC, which wraps $FFFF to $0000. #[inline] pub(crate) fn fetch(&mut self) -> u8 { let value = self.read(self.pc); self.pc = self.pc.wrapping_add(1); value } /// Two operand fetches, low byte first. pub(crate) fn fetch_word(&mut self) -> u16 { let low = self.fetch(); pack(self.fetch(), low) } /// Direct-page address: page 0 or 1 by P, and the offset wraps within the page (U2). pub(crate) fn dp(&self, offset: u32) -> u16 { ((self.psw & FLAG_P) as u16) << 3 | (offset & 0xFF) as u16 } /// Read a direct-page byte. pub(crate) fn load(&mut self, offset: u32) -> u8 { self.read(self.dp(offset)) } /// Write a direct-page byte. pub(crate) fn store(&mut self, offset: u32, value: u8) { self.write(self.dp(offset), value); } /// Read a little-endian word from the direct page; the high byte wraps in the page (U2). pub(crate) fn load_pointer(&mut self, offset: u32) -> u16 { let low = self.load(offset); pack(self.load(offset + 1), low) } /// `d+X`: the operand, then an internal cycle for the index add; returns the offset. pub(crate) fn direct_x(&mut self) -> u32 { let offset = self.fetch() as u32; self.idle(); (offset + self.x as u32) & 0xFF } /// `!a+X` / `!a+Y`: the operand, an internal cycle, no page-wrap quirk (U2). pub(crate) fn absolute_indexed(&mut self, index: u8) -> u16 { let base = self.fetch_word(); self.idle(); base.wrapping_add(index as u16) } /// `[d+X]`: the pointer at `d+X` in the direct page; returns the address it holds. pub(crate) fn indexed_indirect(&mut self) -> u16 { let offset = self.direct_x(); self.load_pointer(offset) } /// `[d]+Y` as the read forms do it: the internal cycle comes before the pointer reads. /// /// C4: SingleStepTests and ares; anomie group 7 ("most likely") puts it after /// them, as the store does. SST is followed; the count is the same either way. pub(crate) fn indirect_y_load(&mut self) -> u16 { let offset = self.fetch() as u32; self.idle(); self.load_pointer(offset).wrapping_add(self.y as u16) } /// `m.b`: 13 address bits and a bit number in the top three (U15). pub(crate) fn absolute_bit(&mut self) -> (u16, u32) { let operand = self.fetch_word(); (operand & 0x1FFF, (operand >> 13) as u32) } // ----- the stack: page 1, SP wraps within it ---------------------------- pub(crate) fn push(&mut self, value: u8) { self.write(STACK_PAGE | self.sp as u16, value); self.sp = self.sp.wrapping_sub(1); } pub(crate) fn pull(&mut self) -> u8 { self.sp = self.sp.wrapping_add(1); self.read(STACK_PAGE | self.sp as u16) } /// Calls push the next instruction's address, high byte first (U14). pub(crate) fn push_pc(&mut self) { self.push((self.pc >> 8) as u8); self.push((self.pc & 0xFF) as u8); } pub(crate) fn pull_pc(&mut self) { let low = self.pull(); self.pc = pack(self.pull(), low); } // ----- flags ---------------------------------------------------------- /// N and Z from an 8-bit result. pub(crate) fn set_nz(&mut self, value: u8) { self.psw = self.psw & !(FLAG_N | FLAG_Z) | (value & FLAG_N) | if value == 0 { FLAG_Z } else { 0 }; } /// N from bit 15 and Z from all sixteen bits. pub(crate) fn set_nz16(&mut self, value: u16) { let negative = if value & 0x8000 != 0 { FLAG_N } else { 0 }; self.psw = self.psw & !(FLAG_N | FLAG_Z) | negative | if value == 0 { FLAG_Z } else { 0 }; } // ----- one step --------------------------------------------------------- /// Execute one instruction, or one stopped cycle. Returns the SPC700 cycles /// consumed, which is the number of bus callbacks made. A stopped CPU makes /// exactly one `idle()` and returns 1 (C5): the board's clock keeps running /// and only `reset()` leaves the state. /// /// `cycle` is the index of the current cycle, 0 = the fetch. `opcode` still /// holds the previous opcode during the fetch cycle itself; it is current from /// cycle 1 on. pub fn step(&mut self) -> u32 { if self.stopped { self.cycle = 0; let signals = self.signals(); self.bus.idle(signals); return 1; } self.cycle = -1; let opcode = self.fetch(); self.opcode = opcode; self.dispatch(opcode); (self.cycle + 1) as u32 } }