// SPDX-License-Identifier: MIT //! The user break controller (UBC): two break channels (A and B). //! //! sh2-python's `ubc.py`. HM chapter 6. Each channel compares the CPU's or DMAC's //! address (and, for channel B, data) against BARx/BAMRx (BDRB/BDMRB) under BBRx's cycle //! conditions, and raises the user break interrupt (level 15, vector 12) on a match; //! BRCR sets sequential mode and the PC-break timing. //! //! **Stub for rungs 0-2**: the register map below, readable and writable as HM appendix //! B gives it, with no break detection. 32X software does not use the UBC; the //! reference's debugger gives breakpoints and watchpoints without it. Break detection is //! a later rung's work, extending this module (its registers already hold what it //! needs). //! //! What Rust forces: the reference's ``bytes`` dict, keyed by byte address, is an array //! over H'FFFFFF40-H'FFFFFF79 with a flag for the bytes the dict holds. use crate::module::{reg, value_of, ModuleState, Register}; /// (address, name, initial): 16-bit registers, HM appendix B and table 6.2. pub const MAP: [(u32, &str, u32); 15] = [ (0xFFFFFF40, "BARAH", 0x0000), (0xFFFFFF42, "BARAL", 0x0000), (0xFFFFFF44, "BAMRAH", 0x0000), (0xFFFFFF46, "BAMRAL", 0x0000), (0xFFFFFF48, "BBRA", 0x0000), (0xFFFFFF60, "BARBH", 0x0000), (0xFFFFFF62, "BARBL", 0x0000), (0xFFFFFF64, "BAMRBH", 0x0000), (0xFFFFFF66, "BAMRBL", 0x0000), (0xFFFFFF68, "BBRB", 0x0000), (0xFFFFFF70, "BDRBH", 0x0000), (0xFFFFFF72, "BDRBL", 0x0000), (0xFFFFFF74, "BDMRBH", 0x0000), (0xFFFFFF76, "BDMRBL", 0x0000), (0xFFFFFF78, "BRCR", 0x0000), ]; pub fn registers() -> Vec { MAP.iter().map(|(address, name, initial)| reg(*address, name, 16, Some(*initial))).collect() } const BASE: u32 = 0xFFFFFF40; const SPAN: usize = 0x3A; #[derive(Clone, Debug)] pub struct Ubc { pub bytes: [u32; SPAN], pub present: [bool; SPAN], } impl Ubc { pub fn new() -> Ubc { let mut ubc = Ubc { bytes: [0; SPAN], present: [false; SPAN] }; ubc.reset(); ubc } fn index(address: u32) -> Option { let offset = address.wrapping_sub(BASE) as usize; if offset < SPAN { Some(offset) } else { None } } pub fn reset(&mut self) { for (address, _name, initial) in MAP.iter() { let i = Ubc::index(*address).unwrap(); self.bytes[i] = initial >> 8; self.bytes[i + 1] = initial & 0xFF; self.present[i] = true; self.present[i + 1] = true; } } pub fn read8(&self, address: u32) -> u32 { match Ubc::index(address) { Some(i) if self.present[i] => self.bytes[i], _ => 0, } } pub fn write8(&mut self, address: u32, value: u32) { if let Some(i) = Ubc::index(address) { if self.present[i] { self.bytes[i] = value; } } } pub fn state(&self) -> ModuleState { MAP.iter() .map(|(address, name, _)| { let i = Ubc::index(*address).unwrap(); (*name, ((self.bytes[i] << 8) | self.bytes[i + 1]) as i64) }) .collect() } pub fn load_state(&mut self, values: &ModuleState) { for (address, name, _) in MAP.iter() { let i = Ubc::index(*address).unwrap(); let value = value_of(values, name) as u32; self.bytes[i] = value >> 8; self.bytes[i + 1] = value & 0xFF; } } } impl Default for Ubc { fn default() -> Self { Ubc::new() } }