// SPDX-License-Identifier: MIT //! What every on-chip module shares: its register map and how sized accesses reach it. //! //! sh2-python's `_module.py`. The SH7604's modules sit on two internal buses (HM 7.11.2): //! the 8-bit peripheral bus at H'FFFFFE00-H'FFFFFEFF (SCI, FRT, INTC, WDT, the DMAC's //! DRCRs, SBYCR, CCR) and the 16/32-bit side at H'FFFFFF00-H'FFFFFFFF (DIVU, UBC, DMAC, //! BSC); HM appendix B lists every register. A module declares ``REGISTERS`` (its map, //! for the docs, the debugger and later rungs) and implements ``read`` and ``write`` for //! the sizes its registers take. //! //! What Rust forces: the reference's modules are objects holding a reference to their //! chip, and the base classes' ``read``/``write`` split an access into bytes (``Module``) //! or select part of a longword (``LongwordModule``) before calling the subclass. Here //! each module's registers are a struct beside the chip, its methods that reach the chip //! are chip methods named after it (``frt_read8``), and [`ModuleId`] names the module an //! address belongs to; the two base-class splits are [`split_read`]/[`split_write`] and //! the longword helpers, called by the chip's ``module_read``/``module_write`` (cpu.rs) //! for the modules that use them, as the reference's class hierarchy does. /// One row of a module's register map (HM appendix B). #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Register { pub address: u32, pub name: &'static str, /// Register width: 8, 16 or 32. pub bits: u32, /// Value after a power-on reset; None when undefined. pub initial: Option, } pub const fn reg(address: u32, name: &'static str, bits: u32, initial: Option) -> Register { Register { address, name, bits, initial } } /// The modules, in the reference's ``chip.modules`` order. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ModuleId { Cache, Bsc, Intc, Ubc, Dmac, Divu, Frt, Wdt, Sci, Power, } /// ``chip.modules``' order (cpu.py): resets, states and loads go through it. pub const MODULES: [ModuleId; 10] = [ ModuleId::Cache, ModuleId::Bsc, ModuleId::Intc, ModuleId::Ubc, ModuleId::Dmac, ModuleId::Divu, ModuleId::Frt, ModuleId::Wdt, ModuleId::Sci, ModuleId::Power, ]; impl ModuleId { /// The module's ``NAME``. pub fn name(self) -> &'static str { match self { ModuleId::Cache => "cache", ModuleId::Bsc => "bsc", ModuleId::Intc => "intc", ModuleId::Ubc => "ubc", ModuleId::Dmac => "dmac", ModuleId::Divu => "divu", ModuleId::Frt => "frt", ModuleId::Wdt => "wdt", ModuleId::Sci => "sci", ModuleId::Power => "power", } } } /// ``Module.read``: a CPU (or DMAC) read of ``width`` bits at ``address``, split into /// bytes. pub fn split_read(address: u32, width: u32, mut read8: impl FnMut(u32) -> u32) -> u32 { let mut value: u32 = 0; for offset in 0..(width >> 3) { value = (value << 8) | read8(address.wrapping_add(offset)); } value } /// ``Module.write``: a CPU (or DMAC) write, split into bytes, high byte first. pub fn split_write(address: u32, width: u32, value: u32, mut write8: impl FnMut(u32, u32)) { let count = width >> 3; for offset in 0..count { write8(address.wrapping_add(offset), (((value as u64) >> (8 * (count - 1 - offset))) & 0xFF) as u32); } } /// ``LongwordModule.read``'s shift: the part of the longword a byte or word access /// selects. The reference computes it with Python's ``>>``, which raises on a negative /// count (a word at an address that is 3 mod 4); this panics the same way. pub fn longword_shift(address: u32, width: u32) -> u32 { let shift = (4 - (address & 3) as i64 - (width >> 3) as i64) * 8; if shift < 0 { panic!("negative shift count"); } shift as u32 } /// A value's state in a module's ``state()`` mapping: name -> value, in insertion order. pub type ModuleState = Vec<(&'static str, i64)>; /// A value in a [`ModuleState`] by name (the reference's ``values[name]``). pub fn value_of(values: &[(&'static str, i64)], name: &str) -> i64 { match values.iter().find(|(key, _)| *key == name) { Some((_, value)) => *value, None => panic!("no {name} in the state"), } }