// SPDX-License-Identifier: MIT //! The public CPU: one instruction core, one row per part (`cpu.py`). //! //! The host owns memory and every device: it gives the CPU a [`Bus`], calls //! [`Cmos65c02::step`], and runs its devices inside the callbacks for as long //! as each cycle lasts on its board. See the reference's //! docs/handoff-brief.md, "The bus contract". //! //! The differences between makers' parts are data (the reference's //! docs/start-here.md, "The rows"): each part below is [`Row`], a value, and //! the core is [`Cmos65c02`] with its row. //! //! Differences forced by Rust: a Python row is a subclass of `CMOS65C02` whose //! class attributes are the settings; Rust has no class attributes, so a row //! is a `Row` value given to the constructor, with the same fields, and the //! four classes are the constants [`W65C02S`], [`R65C02`], [`G65SC02`] and //! [`LYNX2_CELL`] (`Lynx2Cell`; a constant is upper case in Rust). //! `CMOS65C02`'s own defaults are [`CMOS65C02`]. A board makes its own row by //! building a `Row`. The Python constructor `Row(read_byte, write_byte)` is //! `Cmos65c02::new(row, bus)`. The frame's four hooks are the [`Part`] trait's. use crate::core::{Bus, Cmos65c02}; use crate::nops::{Nop5cReads, NopDcFcReads}; use crate::state::CpuState; use sixfiveohtwo::frame::{Frame, Part}; /// A part's row of settings: the class attributes of a `CMOS65C02` subclass. /// /// - `bit_instructions`: `RMB SMB BBR BBS` at `x7` and `xF`, else one-byte, /// one-cycle NOPs; /// - `wai_stp`: `WAI` and `STP` at `$CB` and `$DB`, else one-byte, one-cycle /// NOPs; /// - `reset_writes_stack`: reset's three stack cycles write PCH, PCL, P /// (U1, U2: the G65SC12 does, CAP, GDS Table V; the R65C02 only reads, CAP); /// - `one_cycle_nops_poll`, `nop_5c_reads`, `nop_dc_fc_reads`: the defaults /// of U7, U9 and C10 (the reference's docs/undocumented-behavior.md). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Row { /// The class name, as the reference's traces and harness name the row. pub name: &'static str, pub bit_instructions: bool, pub wai_stp: bool, pub reset_writes_stack: bool, /// U7: whether an interrupt may be taken directly after a one-byte, /// one-cycle NOP. False: it never is; the NOP "attaches or merges with" /// the following instruction (CL section 9, Laughton's bench; LT `cpu` /// test 8 on real Lynxes). // DOC (CL section 9, LK's bench report) and TEST ROM (LT `cpu` test 8); not // MEASURED by CAP (U7 stays open for the parts not covered). pub one_cycle_nops_poll: bool, pub nop_5c_reads: Nop5cReads, pub nop_dc_fc_reads: NopDcFcReads, } /// `CMOS65C02`'s own settings: the instruction core's defaults. // // Evidence tags (docs/methodology.md in emulator-lineage) are given beside // each rule in src/: MEASURED is CAP (logic-analyser captures of real // chips); DOC is a data sheet (DS RDS GDS VDS NDS LS) or community research // (CL LK GF BC GW); TEST ROM is LT; OBSERVED is SST or MAME (emulators); // HYPOTHESIS is a default with no direct evidence (its U entry names what // would settle it). pub const CMOS65C02: Row = Row { name: "CMOS65C02", // DOC: DS Table 5-2 (the W65C02S has both); a part's own row overrides. bit_instructions: true, wai_stp: true, // MEASURED for the two captured parts; the default of a new row (False) is // HYPOTHESIS (U1). reset_writes_stack: false, one_cycle_nops_poll: false, nop_5c_reads: Nop5cReads::FfPage, nop_dc_fc_reads: NopDcFcReads::Absolute, }; /// WDC W65C02S: the bit instructions, `WAI` and `STP` (DS Table 5-2: 212 opcodes). /// /// No capture of this part's reset was found; it is taken to read the stack /// (U1). Its only tier-1 evidence is Foot's data-only capture (rung 4b). pub const W65C02S: Row = Row { name: "W65C02S", // DOC: DS Table 5-2 (bit instructions, WAI, STP); MEASURED only through // Foot's data-bus capture of a part with bit instructions (rung 4b, which // cannot tell this row from R65C02). Reset: HYPOTHESIS (U1; a reset capture // of a W65C02S would settle it). bit_instructions: true, wai_stp: true, reset_writes_stack: false, // U1: unknown; the default ..CMOS65C02 }; /// Rockwell R65C02, R65C102, R65C112: the bit instructions, no `WAI` or `STP` (RDS; C5). /// /// Captured (CAP, a BBC B fitted with one): reset reads the stack. pub const R65C02: Row = Row { name: "R65C02", // MEASURED (CAP: bit instructions run, reset reads the stack); DOC (RDS). // `$CB`/`$DB` as NOPs: DOC (RDS has no WAI/STP; CL section 9), not // captured (U3); MAME's r65c02 differs (C5, OBSERVED). bit_instructions: true, wai_stp: false, // U3: `$CB` and `$DB` are one-byte, one-cycle NOPs reset_writes_stack: false, // CAP ..CMOS65C02 }; /// GTE/CMD G65SC02 and G65SC12, NCR65C02, VLSI VL65NC02, and the Lynx I's cell: 178 opcodes. /// /// No bit instructions (CAP on a BBC Master's G65SC12: `x7` and `xF` take one /// cycle; GDS Table V, VDS Table 2, NDS), no `WAI` or `STP`. Captured: /// reset writes PCH, PCL and P to the stack. pub const G65SC02: Row = Row { name: "G65SC02", // MEASURED (CAP on a G65SC12: `x7`/`xF` one cycle; reset writes the // stack); DOC (GDS Table V, VDS Table 2, NDS). `$CB`/`$DB`: DOC (GDS, VDS, // NDS), not captured (U3). bit_instructions: false, // C2 wai_stp: false, // U3 reset_writes_stack: true, // CAP; GDS Table V ..CMOS65C02 }; /// The 65C02 cell of the Atari Lynx II's Mikey: the `G65SC02` row with the bit /// instructions (`Lynx2Cell`). /// /// Real Lynx IIs execute `RMB SMB BBR BBS` and Lynx Is do not (LT `cpu` /// test 7; C12). No part number is known for the later cell, and nothing /// else about it is: its reset is taken to be the Lynx I's (U2), and `WAI` /// and `STP` to be absent (C8). The Lynx I's cell is [`G65SC02`]. pub const LYNX2_CELL: Row = Row { name: "Lynx2Cell", // Bit instructions: TEST ROM (LT `cpu` test 7, expectations taken from // real Lynx IIs; not yet run on our board, rung 9). WAI/STP absent: DOC // (42Bastian's post, start-here.md; C8). Reset: HYPOTHESIS (U2: copied from // the Lynx I row; a bus capture of a Lynx II's reset would settle it). bit_instructions: true, // C12: LT, tier 3 with hardware-derived expectations wai_stp: false, // C8 reset_writes_stack: true, // U2: no evidence; as the Lynx I row ..CMOS65C02 }; /// The four parts, as the reference's harness lists them (`ROWS`). pub const ROWS: [Row; 4] = [W65C02S, R65C02, G65SC02, LYNX2_CELL]; /// The row of this name, as `python -m w65c02_python --row` takes it (any case). pub fn row_named(name: &str) -> Option { ROWS.into_iter() .find(|row| row.name.eq_ignore_ascii_case(name)) } impl Part for Cmos65c02 { fn frame(&self) -> &Frame { &self.frame } fn frame_mut(&mut self) -> &mut Frame { &mut self.frame } fn status(&self) -> u8 { self.p } fn accept_reset(&mut self) -> u32 { self.reset_sequence() } fn halted_cycle(&mut self) -> u32 { self.halted_step() } fn enter_interrupt(&mut self, kind: u8) -> u32 { self.enter_interrupt_sequence(kind) } fn execute(&mut self) -> u32 { let pc = self.pc; // MEASURED: CAP records SYNC high in exactly the opcode-fetch cycle. self.sync = true; self.fetch = true; let opcode = self.read(pc); self.sync = false; self.fetch = false; self.pc = pc.wrapping_add(1); self.opcode = opcode; let (handler, mode, cycles) = self.table[opcode as usize]; self.dispatch(handler, mode); let extra = self.extra; if extra != 0 { self.extra = 0; return cycles + extra; } cycles } } impl Cmos65c02 { /// A 65C02 of `row` on `bus` (the reference's `Row(read_byte, write_byte)`). pub fn new(row: Row, bus: T) -> Self { Self::init_registers(row, bus) } /// The part this CPU is. pub fn row(&self) -> Row { self.row } /// Execute one instruction, interrupt entry, reset or waiting cycle; return /// the number of bus callbacks it made ([`Part::step`]). #[inline] pub fn step(&mut self) -> u32 { Part::step(self) } /// True while `WAI` waits or `STP` has stopped the CPU. pub fn halted(&self) -> bool { self.frame.halted } // -- state ----------------------------------------------------------- /// Return a snapshot of all CPU-owned state at a boundary. /// /// No host reads, no side effects. Memory and devices are the host's and /// are not included. pub fn capture_state(&self) -> CpuState { let poll = self.frame.poll_fields(); CpuState { a: self.a, x: self.x, y: self.y, s: self.s, pc: self.pc, p: self.p, waiting: self.waiting, stopped: self.stopped, halted: poll.halted, reset_pending: poll.reset_pending, maskable_interrupt_pending: poll.maskable_interrupt_pending, non_maskable_interrupt_pending: poll.non_maskable_interrupt_pending, polled_i: poll.polled_i, polled_maskable_interrupt: poll.polled_maskable_interrupt, polled_non_maskable_interrupt: poll.polled_non_maskable_interrupt, skipped_poll: poll.skipped_poll, } } /// Restore a captured CPU state without touching the host. pub fn restore_state(&mut self, state: &CpuState) -> Result<(), String> { state.validate()?; self.a = state.a; self.x = state.x; self.y = state.y; self.s = state.s; self.pc = state.pc; self.p = state.p; self.waiting = state.waiting; self.stopped = state.stopped; self.frame.restore_poll(&state.poll_fields()); Ok(()) } }