// SPDX-License-Identifier: MIT //! The interrupt controller (INTC): priorities and vector numbers of the on-chip sources. //! //! sh2-python's `intc.py`. HM chapter 5. NMI and the IRL pins are inputs the board drives //! (``chip.nmi``, ``chip.irl``); the on-chip modules' sources get their level (0-15, 0 = //! masked) from IPRA/IPRB and their vector number from the VCR registers (HM 5.3). The //! CPU compares the highest pending level with SR.I3-I0 between instructions (HM 4.6, //! 5.4); that check is in ``core.rs``, which reads the two plain fields this module keeps //! current: ``chip.onchip_level`` and ``chip.onchip_vector``. //! //! Registers (HM table 5.5, appendix B), all on the 8-bit peripheral bus: //! //! ====== ========== ============================================================= //! IPRB H'FFFFFE60 SCIIP3-0 (bits 15-12), FRTIP3-0 (11-8) //! VCRA H'FFFFFE62 SERV (14-8), SRXV (6-0): SCI receive error, receive full //! VCRB H'FFFFFE64 STXV, STEV: SCI transmit empty, transmit end //! VCRC H'FFFFFE66 FICV, FOCV: FRT input capture, output compare //! VCRD H'FFFFFE68 FOVV: FRT overflow //! ICR H'FFFFFEE0 NMIL (15, the NMI pin, read only), NMIE (8), VECMD (0) //! IPRA H'FFFFFEE2 DIVUIP (15-12), DMACIP (11-8), WDTIP (7-4) //! VCRWDT H'FFFFFEE4 WITV (14-8): WDT interval; BCMV (6-0): BSC compare match //! ====== ========== ============================================================= //! //! VCRDIV (H'FFFFFF0C) belongs to the DIVU and VCRDMA0/1 (H'FFFFFFA0/A8) to the DMAC. //! //! What Rust forces: the reference's ``regs`` is a dict keyed by byte address; here it is //! an array over the same sixteen addresses ([`slot`]). use crate::bus::Bus; use crate::cpu::Sh7604; use crate::module::{reg, value_of, ModuleState, Register}; /// The byte addresses this module answers, with their reset values (HM table 5.5). pub const INITIAL: [(u32, u32); 16] = [ (0xFFFFFE60, 0x00), (0xFFFFFE61, 0x00), (0xFFFFFE62, 0x00), (0xFFFFFE63, 0x00), (0xFFFFFE64, 0x00), (0xFFFFFE65, 0x00), (0xFFFFFE66, 0x00), (0xFFFFFE67, 0x00), (0xFFFFFE68, 0x00), (0xFFFFFE69, 0x00), (0xFFFFFEE0, 0x00), (0xFFFFFEE1, 0x00), (0xFFFFFEE2, 0x00), (0xFFFFFEE3, 0x00), (0xFFFFFEE4, 0x00), (0xFFFFFEE5, 0x00), ]; /// Bits that read 0 whatever is written (HM 5.3, the reserved bits). pub const WRITABLE: [(u32, u32); 16] = [ (0xFFFFFE60, 0xFF), (0xFFFFFE61, 0x00), (0xFFFFFE62, 0x7F), (0xFFFFFE63, 0x7F), (0xFFFFFE64, 0x7F), (0xFFFFFE65, 0x7F), (0xFFFFFE66, 0x7F), (0xFFFFFE67, 0x7F), (0xFFFFFE68, 0x7F), (0xFFFFFE69, 0x00), (0xFFFFFEE0, 0x01), // NMIE; NMIL (bit 7) is the pin (0xFFFFFEE1, 0x01), // VECMD (0xFFFFFEE2, 0xFF), (0xFFFFFEE3, 0xF0), (0xFFFFFEE4, 0x7F), (0xFFFFFEE5, 0x7F), ]; pub const REGISTERS: [Register; 8] = [ reg(0xFFFFFE60, "IPRB", 16, Some(0x0000)), reg(0xFFFFFE62, "VCRA", 16, Some(0x0000)), reg(0xFFFFFE64, "VCRB", 16, Some(0x0000)), reg(0xFFFFFE66, "VCRC", 16, Some(0x0000)), reg(0xFFFFFE68, "VCRD", 16, Some(0x0000)), reg(0xFFFFFEE0, "ICR", 16, None), // NMIL follows the pin reg(0xFFFFFEE2, "IPRA", 16, Some(0x0000)), reg(0xFFFFFEE4, "VCRWDT", 16, Some(0x0000)), ]; /// Where ``address`` sits in [`INITIAL`], or None when the module has no such byte. pub fn slot(address: u32) -> Option { if (0xFFFFFE60..=0xFFFFFE69).contains(&address) { return Some((address - 0xFFFFFE60) as usize); } if (0xFFFFFEE0..=0xFFFFFEE5).contains(&address) { return Some(10 + (address - 0xFFFFFEE0) as usize); } None } #[derive(Clone, Debug)] pub struct Intc { pub regs: [u32; 16], } impl Intc { pub fn new() -> Intc { Intc { regs: INITIAL.map(|(_, v)| v) } } /// ``self.regs.get(address, 0)``. pub fn get(&self, address: u32) -> u32 { match slot(address) { Some(i) => self.regs[i], None => 0, } } /// ICR.NMIE: 1 takes NMI on a rising edge, 0 on a falling edge (HM 5.3.3). pub fn nmi_rising(&self) -> bool { self.get(0xFFFFFEE0) & 1 != 0 } /// ICR.VECMD: IRL vectors from the bus (HM 5.3.3, table 4.3 note 2). pub fn vecmd(&self) -> bool { self.get(0xFFFFFEE1) & 1 != 0 } /// The IPR level of a source group: divu, dmac, wdt, sci, frt (HM 5.3.1, 5.3.2). pub fn level(&self, name: &str) -> u32 { match name { "divu" => self.get(0xFFFFFEE2) >> 4, "dmac" => self.get(0xFFFFFEE2) & 15, "wdt" => self.get(0xFFFFFEE3) >> 4, "sci" => self.get(0xFFFFFE60) >> 4, _ => self.get(0xFFFFFE60) & 15, // frt } } /// A VCR field, by the byte address that holds it (7 bits, HM 5.3.4-5.3.8). pub fn vector(&self, address: u32) -> u32 { self.get(address) & 0x7F } pub fn state(&self) -> ModuleState { let mut out = Vec::new(); for register in REGISTERS.iter() { let high = self.get(register.address); let low = self.get(register.address + 1); out.push((register.name, ((high << 8) | low) as i64)); } out } } impl Default for Intc { fn default() -> Self { Intc::new() } } impl Sh7604 { pub(crate) fn intc_reset(&mut self) { self.intc.regs = INITIAL.map(|(_, v)| v); self.intc_update(); } pub(crate) fn intc_read8(&mut self, address: u32) -> u32 { if address == 0xFFFFFEE0 { // NMIL: "the NMI pin's level" (HM 5.3.3, ICR bit 15). return (if self.nmi != 0 { 0x80 } else { 0 }) | self.intc.get(address); } self.intc.get(address) } pub(crate) fn intc_write8(&mut self, address: u32, value: u32) { if let Some(i) = slot(address) { self.intc.regs[i] = value & WRITABLE[i].1; self.intc_update(); } } /// Recompute the highest on-chip request into ``chip.onchip_level`` / ``_vector``. /// /// Sources at the same IPR level are ordered as HM table 5.4 lists them: DIVU, /// DMAC channel 0, DMAC channel 1, WDT, BSC compare match, SCI (ERI, RXI, TXI, /// TEI), FRT (ICI, OCI, OVI). The first source at the highest level wins. pub(crate) fn intc_update(&mut self) { let (mut best_level, mut best_vector) = (0, 0); for (level, vector) in self.onchip_requests() { if level > best_level { best_level = level; best_vector = vector; } } self.onchip_level = best_level; self.onchip_vector = best_vector; } pub(crate) fn intc_load_state(&mut self, values: &ModuleState) { for register in REGISTERS.iter() { let word = value_of(values, register.name) as u32; self.intc.regs[slot(register.address).unwrap()] = (word >> 8) & 0xFF; self.intc.regs[slot(register.address + 1).unwrap()] = word & 0xFF; } self.intc_update(); } }