// SPDX-License-Identifier: MIT //! The SH7604: the SH-2 CPU and its on-chip modules, composed into one chip. //! //! sh2-python's `cpu.py`. The board implements [`Bus`]: ``read(address, width, kind) -> //! int`` and ``write(address, width, value, kind)``, one call per external bus cycle //! (``bus.rs``; sh2-python's docs/handoff-brief.md, "The bus contract"), and calls //! [`Sh7604::step`], which runs one instruction or one exception sequence and returns //! its states. The chip keeps its own clock, ``chip.clock``, in states of φ since //! construction. //! //! Pins in are plain fields the board sets, also from inside a callback (through the //! callback's [`crate::bus::BusContext`]): ``irl`` (0-15, the inverted IRL3-IRL0 level), //! ``nmi`` (the pin's level; the edge is detected), ``dreq0``/``dreq1``, ``ftci``/``fti``. //! Pins out are the [`Bus`] methods ``on_ftoa(level, clock)``, ``on_ftob``, //! ``on_dack(channel, clock)``, ``on_txd(byte, clock)``, ``on_wdtovf(clock)``. //! ``observe(address, width, kind)``, when the ``observe`` flag is set, sees every CPU //! access, cache hits included (``kind`` has ``HIT``); default off, no cost. //! //! What Rust forces (the rest is the reference's structure, names and order): the //! reference composes the chip from mixins (one per instruction family, the core, the //! scheduler); here they are ``impl`` blocks of one struct in the same files. Python's //! private ``_name`` is a private ``name``. Its dispatch table of bound handlers is the //! generated match in ``dispatch.rs``; its per-opcode tables are built once per process //! ([`tables`]) instead of once per class. Its two test and replay seams that replace //! methods (``chip._place``, ``chip.dmac.run``) are the flags ``free_fetch`` and //! ``dmac_run_disabled``. use std::sync::OnceLock; use crate::bsc::Bsc; use crate::bus::{Bus, BusContext, Pins}; use crate::cache::Cache; use crate::decode::{decode_index, Form, FORMS}; use crate::dispatch::ILLEGAL; use crate::divu::Divu; use crate::dmac::Dmac; use crate::frt::Frt; use crate::intc::Intc; use crate::module::{split_read, split_write, ModuleId, ModuleState, MODULES}; use crate::pipeline::{plan_index, plans, Event, Plan, PR_BIT, RING}; use crate::power::Power; use crate::sci::Sci; use crate::state::ChipState; use crate::system::SR_MASK; use crate::ubc::Ubc; use crate::wdt::Wdt; pub const M32: u32 = 0xFFFF_FFFF; /// HM table 4.9: an instruction that changes PC in a delay slot is a slot illegal. pub const PC_CHANGING: [&str; 13] = ["JMP", "JSR", "BRA", "BSR", "RTS", "RTE", "BT", "BF", "TRAPA", "BF/S", "BT/S", "BSRF", "BRAF"]; /// HM 4.6.2, table 4.10: no interrupt is accepted after these. pub const INTERRUPT_DISABLING: [&str; 8] = ["LDC", "LDC.L", "STC", "STC.L", "LDS", "LDS.L", "STS", "STS.L"]; /// Instructions that read or write PR without naming it (PM 6.10, 6.11, 6.26, 6.51). const IMPLICIT_PR: [&str; 4] = ["RTS", "BSR", "BSRF", "JSR"]; /// The register bits (``PR_BIT`` for PR) named in an operand text: the words ``Rm``, /// ``Rn``, ``R0`` and ``PR`` (the reference's ``\bR[mn0]\b|\bPR\b``). fn register_bits(form: &Form, opcode: u32, text: &str) -> u32 { let mut bits = 0; for token in text.split(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) { if token == "PR" { bits |= PR_BIT; } else if token == "R0" { bits |= 1; } else if token == "Rm" || token == "Rn" { let letter = token.as_bytes()[1] as char; let field = form.code.rfind(letter).unwrap() as u32; // the n or m field's last bit position bits |= 1 << ((opcode >> (15 - field)) & 15); } } bits } /// Python's ``str.rpartition(",")``: (before, after) the last comma, or ("", text). fn rpartition(text: &str) -> (&str, &str) { match text.rfind(',') { Some(i) => (&text[..i], &text[i + 1..]), None => ("", text), } } /// (the register a load writes in WB, the registers the load-use rule sees used). /// /// PM 7.5: a load's WB comes after the next instruction's EX, so that EX splits the /// slot when it uses the loaded register as a source or a destination; not when the /// next instruction is a load to the same register (its destination is not a use) nor /// a ``MAC @Rm+,@Rn+``. Loads are table 7.2's memory loads, ``LDS.L @Rm+,PR`` and ``STS /// MACH/MACL,Rn`` (WB to Rn); LDC.L's two-slot ID stall keeps its successor's EX out of /// its MA's slot. fn load_use(form: &Form, opcode: u32) -> (u32, u32) { let pipeline = form.pipeline; let operands = form.operands; if pipeline == "mac_w" || pipeline == "mac_l" { return (0, 0); } if pipeline == "load" || pipeline == "mac_to_reg" { let (sources, dest) = rpartition(operands); return (register_bits(form, opcode, dest), register_bits(form, opcode, sources)); } if pipeline == "lds_l_pr" { return (PR_BIT, register_bits(form, opcode, rpartition(operands).0)); } let mut uses = register_bits(form, opcode, operands); if IMPLICIT_PR.contains(&form.mnemonic) { uses |= PR_BIT; } (0, uses) } /// -fast: one opcode's decode, everything ``step`` needs in one record (one cache line /// instead of six tables). #[derive(Clone, Copy, Debug)] pub struct Op { pub plan: &'static Plan, pub load_dest: u32, pub uses: u32, pub handler: u16, pub slot_illegal: bool, pub blocks: bool, } /// Per-opcode handler numbers, pipeline plans, load-use registers, and the rule tables /// (the reference's ``_build_tables``). pub struct Tables { /// -fast: the same tables packed per opcode (``Op``), for ``step``. pub ops: Vec, pub index: Vec>, pub handler: Vec, pub plan: Vec<&'static Plan>, pub load_dest: Vec, pub uses: Vec, pub slot_illegal: Vec, pub blocks: Vec, } /// The tables, built on first use. pub fn tables() -> &'static Tables { static TABLES: OnceLock = OnceLock::new(); TABLES.get_or_init(|| { let index = decode_index(); let plans = plans(); let illegal = &plans[plan_index("illegal")]; let mut t = Tables { ops: Vec::with_capacity(0x10000), index: index.clone(), handler: Vec::with_capacity(0x10000), plan: Vec::with_capacity(0x10000), load_dest: Vec::with_capacity(0x10000), uses: Vec::with_capacity(0x10000), slot_illegal: vec![0; 0x10000], blocks: vec![0; 0x10000], }; for (opcode, entry) in index.iter().enumerate() { match entry { None => { // Undefined code: the illegal instruction sequence replaces it in its // ID (PM figure 7.97). t.handler.push(ILLEGAL); t.plan.push(illegal); t.load_dest.push(0); t.uses.push(0); t.slot_illegal[opcode] = 1; } Some(number) => { let form = &FORMS[*number as usize]; t.handler.push(*number); t.plan.push(&plans[plan_index(form.pipeline)]); let (dest, used) = load_use(form, opcode as u32); t.load_dest.push(dest); t.uses.push(used); t.slot_illegal[opcode] = PC_CHANGING.contains(&form.mnemonic) as u8; t.blocks[opcode] = INTERRUPT_DISABLING.contains(&form.mnemonic) as u8; } } } for opcode in 0..0x10000 { t.ops.push(Op { plan: t.plan[opcode], load_dest: t.load_dest[opcode], uses: t.uses[opcode], handler: t.handler[opcode], slot_illegal: t.slot_illegal[opcode] != 0, blocks: t.blocks[opcode] != 0, }); } t }) } /// ``"master"`` or ``"slave"``, the MD5 pin (BCR1.MASTER; HM 7.10). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Mode { Master, Slave, } /// The constructor's keyword options. #[derive(Clone, Copy, Debug)] pub struct Options { /// The MD5 pin (BCR1.MASTER; HM 7.10). pub mode: Mode, /// 8, 16 or 32, the area 0 port width set by the MD3/MD4 pins (HM 7.2.2 note). pub area0_width: u32, /// The value given at reset to the registers HM table 2.1 calls undefined (R0-R14, /// GBR, MACH, MACL, PR, SR's M Q S T). Default 0 (U19). pub undefined: u32, /// Call ``Bus::observe`` on every CPU access (the reference's ``observe=fn``). pub observe: bool, } impl Default for Options { fn default() -> Self { Options { mode: Mode::Master, area0_width: 32, undefined: 0, observe: false } } } /// The Hitachi SH7604: an SH-2 CPU with cache, DMAC, DIVU, FRT, WDT, SCI, BSC, INTC, UBC. /// /// Registers are plain fields: ``r`` (the reference's ``R``, sixteen 32-bit values), /// ``sr``, ``gbr``, ``vbr``, ``mach``, ``macl``, ``pr``, ``pc`` (the next instruction), /// and ``delay_target`` (-1, or the target of a delayed branch whose slot runs next). /// Modules are fields too: ``cache``, ``bsc``, ``intc``, ``ubc``, ``dmac``, ``divu``, /// ``frt``, ``wdt``, ``sci``, ``power``. /// /// The constructor does not reset the chip: call [`Sh7604::reset`], which reads PC and /// R15 through ``read``, when the board is ready to answer. pub struct Sh7604 { pub bus: B, pub observe: bool, pub mode: Mode, pub undefined: u32, // Pins in. pub irl: u32, pub nmi: u32, pub dreq0: u32, pub dreq1: u32, pub ftci: u32, pub fti: u32, /// Inside a bus callback: the board adds WAIT-pin states here (0 on entry). pub wait: i64, pub clock: i64, // CPU registers (HM 2.1; values before reset are the "undefined" choice). pub r: [u32; 16], pub sr: u32, pub gbr: u32, pub mach: u32, pub macl: u32, pub pr: u32, pub vbr: u32, pub pc: u32, pub delay_target: i64, pub sleeping: bool, // Internal state of the stepper. pub(crate) no_interrupt: bool, pub(crate) address_error: bool, pub(crate) dma_address_error: bool, pub(crate) nmi_seen: u32, pub(crate) nmi_pending: bool, pub(crate) onchip_level: u32, pub(crate) onchip_vector: u32, pub(crate) next_event: i64, /// Replay hosts only: see [`Sh7604::inject_interrupt`]. pub replay_interrupts: bool, pub(crate) injected: Option<(u32, i64)>, // The modules (HM 1.1), each beside the CPU. pub cache: Cache, pub bsc: Bsc, pub intc: Intc, pub ubc: Ubc, pub dmac: Dmac, pub divu: Divu, pub frt: Frt, pub wdt: Wdt, pub sci: Sci, pub power: Power, pub(crate) onchip: [Option; 512], pub(crate) tables: &'static Tables, pub(crate) exception_plans: [&'static Plan; 3], // interrupt, address_error, illegal /// A list to receive the scheduler's events (tests, tools), or None. pub pipeline_log: Option>, /// The reference's tests' ``place_free``: every fetch an ``if`` (pipeline.rs). pub free_fetch: bool, /// The rung-2 replay's ``chip.dmac.run = lambda: None``: the DMAC never transfers. pub dmac_run_disabled: bool, // The stage scheduler (pipeline.rs). pub(crate) p_if: [i64; RING], pub(crate) p_ma: [i64; RING], pub(crate) p_lu: [u8; RING], pub(crate) p_slot: i64, pub(crate) p_start: i64, pub(crate) p_next_ex: i64, pub(crate) p_ex: i64, pub(crate) p_fetch_pc: u32, pub(crate) p_fetch_line: i64, pub(crate) p_mm_end: i64, pub(crate) p_lu_dest: u32, pub(crate) p_lu_slot: i64, pub(crate) p_plan: Option<&'static Plan>, pub(crate) p_ma_i: usize, pub(crate) p_ma_left: usize, pub(crate) p_ma_open: i64, pub(crate) p_ma_t0: i64, pub(crate) p_mul: i64, pub(crate) p_mm: i64, pub(crate) p_ma_is_mul: bool, pub(crate) p_pc: u32, pub(crate) fl0: i64, pub(crate) fl1: i64, pub(crate) fv0: u32, pub(crate) fv1: u32, pub(crate) wb_end: i64, pub(crate) ccr_at: i64, pub(crate) ccr_value: u32, pub(crate) refresh_end: i64, pub(crate) bsc_due: i64, /// The reference's ``hasattr(chip, "_bsc_due")``: false while the constructor runs. pub(crate) bsc_due_exists: bool, } /// Which module answers each address of H'FFFFFE00-H'FFFFFFFF (HM appendix B). fn onchip_map() -> [Option; 512] { let mut table = [None; 512]; let mut claim = |first: u32, last: u32, module: ModuleId| { for address in first..=last { table[(address - 0xFFFFFE00) as usize] = Some(module); } }; claim(0xFFFFFE00, 0xFFFFFE05, ModuleId::Sci); claim(0xFFFFFE10, 0xFFFFFE19, ModuleId::Frt); claim(0xFFFFFE60, 0xFFFFFE69, ModuleId::Intc); claim(0xFFFFFE71, 0xFFFFFE72, ModuleId::Dmac); claim(0xFFFFFE80, 0xFFFFFE83, ModuleId::Wdt); claim(0xFFFFFE91, 0xFFFFFE91, ModuleId::Power); claim(0xFFFFFE92, 0xFFFFFE92, ModuleId::Cache); claim(0xFFFFFEE0, 0xFFFFFEE5, ModuleId::Intc); claim(0xFFFFFF00, 0xFFFFFF1F, ModuleId::Divu); claim(0xFFFFFF40, 0xFFFFFF79, ModuleId::Ubc); claim(0xFFFFFF80, 0xFFFFFFB3, ModuleId::Dmac); claim(0xFFFFFFE0, 0xFFFFFFFB, ModuleId::Bsc); table } impl Sh7604 { /// ``SH7604(read, write, **options)``. pub fn new(bus: B, options: Options) -> Sh7604 { if ![8, 16, 32].contains(&options.area0_width) { panic!("area0_width must be 8, 16 or 32"); } let u = options.undefined; let clock = 0; let tables = tables(); let plans = plans(); let mut chip = Sh7604 { bus, observe: options.observe, mode: options.mode, undefined: u, irl: 0, nmi: 1, // the NMI pin idles high; NMIE = 0 takes the falling edge dreq0: 0, dreq1: 0, ftci: 0, fti: 0, wait: 0, clock, r: [u; 16], sr: 0xF0 | (u & 0x303), gbr: u, mach: u, macl: u, pr: u, vbr: 0, pc: 0, delay_target: -1, sleeping: false, no_interrupt: false, address_error: false, dma_address_error: false, nmi_seen: 1, nmi_pending: false, onchip_level: 0, onchip_vector: 0, next_event: 1 << 62, replay_interrupts: false, injected: None, cache: Cache::new(), bsc: Bsc::new(options.mode == Mode::Slave, options.area0_width, clock), intc: Intc::new(), ubc: Ubc::new(), dmac: Dmac::new(), divu: Divu::new(), frt: Frt::new(clock), wdt: Wdt::new(clock), sci: Sci::new(), power: Power::new(), onchip: onchip_map(), tables, exception_plans: [ &plans[plan_index("interrupt")], &plans[plan_index("address_error")], &plans[plan_index("illegal")], ], pipeline_log: None, free_fetch: false, dmac_run_disabled: false, p_if: [-1; RING], p_ma: [0; RING], p_lu: [0; RING], p_slot: 0, p_start: 0, p_next_ex: 0, p_ex: 0, p_fetch_pc: 0, p_fetch_line: -1, p_mm_end: 0, p_lu_dest: 0, p_lu_slot: -1, p_plan: None, p_ma_i: 0, p_ma_left: 0, p_ma_open: -1, p_ma_t0: 0, p_mul: -1, p_mm: 0, p_ma_is_mul: false, p_pc: 0, fl0: -1, fl1: -1, fv0: 0, fv1: 0, wb_end: 0, ccr_at: 0, ccr_value: 0, refresh_end: 0, bsc_due: 0, bsc_due_exists: false, }; // The stage scheduler (pipeline.rs). chip.pipeline_reset(); chip } /// The board back, the chip dropped (a host that builds a fresh chip on it). pub fn into_bus(self) -> B { self.bus } // -- the board's callbacks (bus.rs: what Rust forces) ------------------------------ pub fn pins(&self) -> Pins { Pins { irl: self.irl, nmi: self.nmi, dreq0: self.dreq0, dreq1: self.dreq1, ftci: self.ftci, fti: self.fti } } pub fn set_pins(&mut self, pins: Pins) { self.irl = pins.irl; self.nmi = pins.nmi; self.dreq0 = pins.dreq0; self.dreq1 = pins.dreq1; self.ftci = pins.ftci; self.fti = pins.fti; } /// ``self.read(address, width, kind)``: the board's read callback, with the chip's /// clock, wait and pins visible to it. pub(crate) fn call_read(&mut self, address: u32, width: u32, kind: u32) -> u32 { let mut cx = BusContext { clock: self.clock, wait: self.wait, cycle_states: self.bsc.cycle_states, pins: self.pins() }; let value = self.bus.read(address, width, kind, &mut cx); self.wait = cx.wait; self.set_pins(cx.pins); value } /// ``self.write(address, width, value, kind)``. pub(crate) fn call_write(&mut self, address: u32, width: u32, value: u32, kind: u32) { let mut cx = BusContext { clock: self.clock, wait: self.wait, cycle_states: self.bsc.cycle_states, pins: self.pins() }; self.bus.write(address, width, value, kind, &mut cx); self.wait = cx.wait; self.set_pins(cx.pins); } /// A pin-out callable (``on_ftoa`` and the others), with the pins visible to it. pub(crate) fn pin_out(&mut self, f: impl FnOnce(&mut B, &mut Pins)) { let mut pins = self.pins(); f(&mut self.bus, &mut pins); self.set_pins(pins); } // -- the on-chip register map ------------------------------------------------------ /// Every asserted on-chip interrupt source, in HM table 5.4's default order. pub(crate) fn onchip_requests(&self) -> crate::intc::Requests { let intc = &self.intc; let mut out = crate::intc::Requests::default(); self.divu.requests(intc, &mut out); self.dmac.requests(intc, &mut out); self.wdt.requests(intc, &mut out); self.bsc.requests(intc, &mut out); self.sci.requests(intc, &mut out); self.frt.requests(intc, &mut out); out } /// The module ``id``'s ``read(address, width)``. pub(crate) fn module_read(&mut self, id: ModuleId, address: u32, width: u32) -> u32 { match id { ModuleId::Cache => split_read(address, width, |a| self.cache_read8(a)), ModuleId::Bsc => self.longword_read(id, address, width), ModuleId::Intc => split_read(address, width, |a| self.intc_read8(a)), ModuleId::Ubc => split_read(address, width, |a| self.ubc.read8(a)), ModuleId::Dmac => self.dmac.read(address, width), ModuleId::Divu => self.longword_read(id, address, width), ModuleId::Frt => split_read(address, width, |a| self.frt_read8(a)), ModuleId::Wdt => split_read(address, width, |a| self.wdt_read8(a)), ModuleId::Sci => split_read(address, width, |a| self.sci.read8(a)), ModuleId::Power => split_read(address, width, |a| self.power.read8(a)), } } /// The module ``id``'s ``write(address, width, value)``. pub(crate) fn module_write(&mut self, id: ModuleId, address: u32, width: u32, value: u32) { match id { ModuleId::Cache => split_write(address, width, value, |a, v| self.cache_write8(a, v)), ModuleId::Bsc => self.bsc_write(address, width, value), ModuleId::Intc => split_write(address, width, value, |a, v| self.intc_write8(a, v)), ModuleId::Ubc => split_write(address, width, value, |a, v| self.ubc.write8(a, v)), ModuleId::Dmac => self.dmac_write(address, width, value), ModuleId::Divu => self.longword_write(id, address, width, value), ModuleId::Frt => split_write(address, width, value, |a, v| self.frt_write8(a, v)), ModuleId::Wdt => self.wdt_write(address, width, value), ModuleId::Sci => split_write(address, width, value, |a, v| self.sci_write8(a, v)), ModuleId::Power => split_write(address, width, value, |a, v| self.power.write8(a, v)), } } /// ``LongwordModule.read`` for the BSC and the DIVU. fn longword_read(&mut self, id: ModuleId, address: u32, width: u32) -> u32 { let base = address & !3; let value = if id == ModuleId::Bsc { self.bsc_read32(base) } else { self.divu_read32(base) }; if width == 32 { return value; } let shift = crate::module::longword_shift(address, width); (value >> shift) & ((1u32 << width) - 1) } /// ``LongwordModule.write`` for the DIVU (the BSC has its own ``write``). fn longword_write(&mut self, _id: ModuleId, address: u32, width: u32, value: u32) { let base = address & !3; if width == 32 { self.divu_write32(base, value, 0xFFFF_FFFF); return; } let shift = crate::module::longword_shift(address, width); let mask = ((1u32 << width) - 1) << shift; self.divu_write32(base, (value << shift) & mask, mask); } /// ``read_onchip(module, address, width)``: the board's hook, then the module. pub(crate) fn read_onchip(&mut self, id: ModuleId, address: u32, width: u32) -> u32 { if let Some(value) = self.bus.read_onchip_instead(address, width) { return value; } let value = self.module_read(id, address, width); self.bus.read_onchip_seen(address, width, value) } /// ``write_onchip(module, address, width, value)``. pub(crate) fn write_onchip(&mut self, id: ModuleId, address: u32, width: u32, value: u32) { let value = self.bus.write_onchip(address, width, value); self.module_write(id, address, width, value); } /// The module that answers ``address`` (H'FFFFFE00-), for hosts and tools. pub fn module_at(&self, address: u32) -> Option { if address < 0xFFFFFE00 { return None; } self.onchip[(address - 0xFFFFFE00) as usize] } /// A module's ``write`` called directly (as the reference's boards call /// ``chip.bsc.write``): no size check, no bus hook. pub fn module_write_direct(&mut self, address: u32, width: u32, value: u32) { if let Some(id) = self.module_at(address) { self.module_write(id, address, width, value); } } /// A module's ``read`` called directly. pub fn module_read_direct(&mut self, address: u32, width: u32) -> u32 { match self.module_at(address) { Some(id) => self.module_read(id, address, width), None => 0, } } // -- reset ------------------------------------------------------------------------- fn module_reset(&mut self, id: ModuleId) { let clock = self.clock; match id { ModuleId::Cache => self.cache_reset(), ModuleId::Bsc => self.bsc_reset(), ModuleId::Intc => self.intc_reset(), ModuleId::Ubc => self.ubc.reset(), ModuleId::Dmac => self.dmac.reset(), ModuleId::Divu => self.divu.reset(), ModuleId::Frt => self.frt.reset(clock), ModuleId::Wdt => self.wdt.reset(clock), ModuleId::Sci => self.sci.reset(), ModuleId::Power => self.power.reset(), } } /// Power-on (vectors 0/1) or manual (vectors 2/3) reset (HM 4.2, table 2.1). /// /// PC and R15 are read through ``read`` at absolute addresses (VBR is not applied); /// VBR = 0 and SR.I3-I0 = 15. Registers HM calls undefined take the ``undefined`` /// constructor value at a power-on reset (U19). The modules return to their initial /// values; CCR.CE is cleared and the cache contents are kept (HM 8.5.1). pub fn reset(&mut self, power_on: bool) { for id in MODULES { self.module_reset(id); } self.onchip_level = 0; self.onchip_vector = 0; if power_on { let u = self.undefined; self.r = [u; 16]; self.gbr = u; self.mach = u; self.macl = u; self.pr = u; self.sr = 0xF0 | (u & 0x303); } else { self.sr = (self.sr & SR_MASK) | 0xF0; } self.vbr = 0; self.delay_target = -1; self.sleeping = false; self.no_interrupt = false; self.address_error = false; self.dma_address_error = false; self.nmi_pending = false; self.nmi_seen = self.nmi; let base = if power_on { 0 } else { 8 }; self.p_ma_left = 0; self.pc = self.read32(base); self.r[15] = self.read32(base + 4); self.pipeline_restart(); self.reschedule(); } // -- state ------------------------------------------------------------------------- fn module_state(&mut self, id: ModuleId) -> ModuleState { match id { ModuleId::Cache => self.cache.state(), ModuleId::Bsc => self.bsc_state(), ModuleId::Intc => self.intc.state(), ModuleId::Ubc => self.ubc.state(), ModuleId::Dmac => self.dmac.state(), ModuleId::Divu => self.divu.state(), ModuleId::Frt => self.frt.state(), ModuleId::Wdt => self.wdt.state(), ModuleId::Sci => self.sci.state(), ModuleId::Power => self.power.state(), } } fn module_load_state(&mut self, id: ModuleId, values: &ModuleState) { match id { ModuleId::Cache => self.cache.load_state(values), ModuleId::Bsc => self.bsc_load_state(values), ModuleId::Intc => self.intc_load_state(values), ModuleId::Ubc => self.ubc.load_state(values), ModuleId::Dmac => self.dmac.load_state(values), ModuleId::Divu => self.divu.load_state(values), ModuleId::Frt => self.frt_load_state(values), ModuleId::Wdt => self.wdt_load_state(values), ModuleId::Sci => self.sci.load_state(values), ModuleId::Power => self.power.load_state(values), } } /// A module's ``internal()`` (module.rs: empty for a module whose registers are all it /// holds). fn module_internal(&self, id: ModuleId) -> Vec { match id { ModuleId::Cache => self.cache.internal(), ModuleId::Bsc => self.bsc_internal(), ModuleId::Dmac => self.dmac.internal(), ModuleId::Frt => self.frt.internal(), ModuleId::Wdt => self.wdt.internal(), ModuleId::Sci => self.sci.internal(), ModuleId::Intc | ModuleId::Ubc | ModuleId::Divu | ModuleId::Power => Vec::new(), } } /// A module's ``load_internal()`` (after its ``load_state``). fn module_load_internal(&mut self, id: ModuleId, values: &[i64]) { match id { ModuleId::Cache => self.cache.load_internal(values), ModuleId::Bsc => self.bsc_load_internal(values), ModuleId::Dmac => self.dmac.load_internal(values), ModuleId::Frt => self.frt.load_internal(values), ModuleId::Wdt => self.wdt.load_internal(values), ModuleId::Sci => self.sci.load_internal(values), ModuleId::Intc | ModuleId::Ubc | ModuleId::Divu | ModuleId::Power => {} } } /// A snapshot of the chip at a step boundary (no bus reads). pub fn capture_state(&mut self) -> ChipState { let pipeline = self.pipeline_state(); let mut modules = Vec::new(); for id in MODULES { let values = self.module_state(id); modules.push((id.name(), values)); } let mut internal = vec![("chip", vec![self.nmi_seen as i64])]; for id in MODULES { let values = self.module_internal(id); if !values.is_empty() { internal.push((id.name(), values)); } } ChipState { r: self.r, sr: self.sr, gbr: self.gbr, vbr: self.vbr, mach: self.mach, macl: self.macl, pr: self.pr, pc: self.pc, delay_target: self.delay_target, sleeping: self.sleeping, no_interrupt: self.no_interrupt, address_error: self.address_error, nmi_pending: self.nmi_pending, clock: self.clock, pipeline, modules, dma_address_error: self.dma_address_error, internal, } } /// Load a [`ChipState`] (registers, in-flight state, every module). pub fn restore_state(&mut self, state: &ChipState) { self.r = state.r; self.sr = state.sr & SR_MASK; self.gbr = state.gbr; self.vbr = state.vbr; self.mach = state.mach; self.macl = state.macl; self.pr = state.pr; self.pc = state.pc; self.delay_target = state.delay_target; self.sleeping = state.sleeping; self.no_interrupt = state.no_interrupt; self.address_error = state.address_error; self.nmi_pending = state.nmi_pending; self.clock = state.clock; if !state.pipeline.is_empty() { self.load_pipeline_state(&state.pipeline); } else { self.p_slot = 0; self.pipeline_restart(); } self.dma_address_error = state.dma_address_error; for id in MODULES { if let Some((_, values)) = state.modules.iter().find(|(name, _)| *name == id.name()) { if !values.is_empty() { self.module_load_state(id, values); } } } let internal = &state.internal; for id in MODULES { if let Some((_, values)) = internal.iter().find(|(name, _)| *name == id.name()) { if !values.is_empty() { self.module_load_internal(id, values); } } } if let Some((_, values)) = internal.iter().find(|(name, _)| *name == "chip") { self.nmi_seen = values[0] as u32; } self.intc_update(); self.reschedule(); } // -- for the debugger and the disassembler ---------------------------------------- /// The decode-table form of ``opcode``, or ``None``. pub fn form(opcode: u32) -> Option<&'static Form> { tables().index[(opcode & 0xFFFF) as usize].map(|n| &FORMS[n as usize]) } /// The on-chip interrupt request the INTC holds (level, vector). pub fn onchip_request(&self) -> (u32, u32) { (self.onchip_level, self.onchip_vector) } /// ``chip._no_interrupt`` (a replay host reads and clears it). pub fn no_interrupt(&self) -> bool { self.no_interrupt } pub fn set_no_interrupt(&mut self, value: bool) { self.no_interrupt = value; } }