// SPDX-License-Identifier: MIT //! Chip state: what [`crate::Sh7604::capture_state`] returns and ``restore_state`` loads. //! //! sh2-python's `state.py`. The reference's ``ChipState`` is a frozen dataclass whose //! ``__post_init__`` validates its fields; Rust's types do most of that (``u32`` //! registers), and [`ChipState::validate`] does the rest. use crate::module::ModuleState; /// The SH7604 at a step boundary. /// /// ``r`` is R0-R15; ``pc`` the address of the next instruction; ``delay_target`` -1 or /// the target of a delayed branch whose slot (at ``pc``) runs next: that and /// ``no_interrupt`` (the last instruction blocks interrupts, HM 4.6.2), /// ``address_error`` (one is pending), ``dma_address_error`` (a DMA address error /// waiting for its exception, vector 10) and ``nmi_pending`` are the in-flight state a /// step boundary needs to resume exactly, with ``pipeline``, the stage scheduler's /// in-flight slots and fetched longwords (opaque integers; empty means "refetch from /// ``pc``", which is what a state built by hand gets). ``modules`` maps each module's name /// to its registers. ``sr`` holds only M, Q, I3-I0, S, T (mask H'3F3). /// /// ``internal`` is what the chip holds beyond its registers, by module name (and /// ``"chip"`` for what the chip keeps for its modules), as opaque integers like /// ``pipeline``: the cache's lines, tags, valid bits and LRU; the BSC's SDRAM banks and /// refresh; the flags a "read 1, then write 0" clears; the timers' catch-up clocks; the /// NMI edge detector. With it a state loaded into a fresh chip on the same memory /// continues exactly as the chip it was captured from (tests/test_state.py). A module /// missing from it (a state built by hand) keeps what the chip holds. #[derive(Clone, Debug, PartialEq, Eq)] pub struct ChipState { pub r: [u32; 16], pub sr: u32, pub gbr: u32, pub vbr: u32, pub mach: u32, pub macl: u32, pub pr: u32, pub pc: u32, pub delay_target: i64, pub sleeping: bool, pub no_interrupt: bool, pub address_error: bool, pub nmi_pending: bool, pub clock: i64, pub pipeline: Vec, pub modules: Vec<(&'static str, ModuleState)>, pub dma_address_error: bool, pub internal: Vec<(&'static str, Vec)>, } impl Default for ChipState { fn default() -> Self { ChipState { r: [0; 16], sr: 0xF0, gbr: 0, vbr: 0, mach: 0, macl: 0, pr: 0, pc: 0, delay_target: -1, sleeping: false, no_interrupt: false, address_error: false, nmi_pending: false, clock: 0, pipeline: Vec::new(), modules: Vec::new(), dma_address_error: false, internal: Vec::new(), } } } impl ChipState { /// The reference's ``__post_init__`` checks that Rust's types do not make. pub fn validate(&self) -> Result<(), String> { if self.sr & !0x3F3 != 0 { return Err("sr must use only M, Q, I3-I0, S and T (mask 0x3F3)".into()); } if !(-1..=0xFFFF_FFFF).contains(&self.delay_target) { return Err("delay_target must be -1 or an address".into()); } Ok(()) } pub fn t(&self) -> u32 { self.sr & 1 } pub fn interrupt_mask(&self) -> u32 { (self.sr >> 4) & 15 } /// The CPU registers by name, as the rung-1 corpus and MAME's trace name them. pub fn registers(&self) -> Vec<(String, u32)> { let mut out: Vec<(String, u32)> = self.r.iter().enumerate().map(|(i, v)| (format!("r{i}"), *v)).collect(); for (name, value) in [ ("sr", self.sr), ("gbr", self.gbr), ("vbr", self.vbr), ("mach", self.mach), ("macl", self.macl), ("pr", self.pr), ("pc", self.pc), ] { out.push((name.to_string(), value)); } out } }