// SPDX-License-Identifier: MIT //! The minimal harness: a host that is not a 32X, for tests, the benchmark and the CLI. //! //! sh2-python's `harness.py` (its docs/handoff-brief.md, "The minimal harness"). Big-endian //! RAM at H'00000000 and H'06000000 (4 MB each, mirrored through their area; the //! cache-through alias at +H'20000000 reaches the same bytes because the core passes //! the board 29-bit addresses), a ROM image loaded at H'00000000 that supplies PC and SP //! from its first two longwords at reset, and a fixed-latency bus: each external cycle //! lasts what the BSC makes it (2 states plus WCR's waits, HM 7.4.1) plus ``wait`` states //! from the board's WAIT pin. FTOA changes are logged; IRL can be driven from a script of //! ``(clock, level)`` pairs. //! //! A run stops when the next instruction branches to itself (``BRA`` with displacement //! -2, or a ``BT``/``BF``/``BT/S``/``BF/S`` to itself whose condition holds), on ``SLEEP`` //! with SR.I3-I0 = 15 (no interrupt can wake it), or at a state budget. R0 is the //! program's result. //! //! What Rust forces: the reference's ``Harness`` holds the chip and is its board; here //! the board ([`Memory`]) is owned by the chip (``chip.bus``) and [`Harness`] holds the //! chip. use crate::bus::{Bus, BusContext, Pins}; use crate::cpu::{Options, Sh7604}; pub const AREA_SIZE: usize = 4 << 20; pub const MASK: usize = AREA_SIZE - 1; /// How a harness run ended. #[derive(Clone, Debug, PartialEq, Eq)] pub struct RunResult { /// "branch-to-self", "sleep", "state-budget", "step-budget" pub reason: &'static str, pub r0: u32, pub pc: u32, pub states: i64, pub steps: u64, pub ftoa: Vec<(i64, u32)>, } /// The harness's board: the two regions, the fixed wait and the FTOA log. pub struct Memory { pub low: Vec, // area 0, H'00000000- pub high: Vec, // area 3, H'06000000- pub wait_states: i64, pub ftoa_log: Vec<(i64, u32)>, } impl Memory { fn region(&mut self, address: u32) -> Option<(&mut Vec, usize)> { let area = (address >> 25) & 3; if area == 0 { return Some((&mut self.low, address as usize & MASK)); } if area == 3 { return Some((&mut self.high, address as usize & MASK)); } None } } impl Bus for Memory { fn read(&mut self, address: u32, width: u32, _kind: u32, cx: &mut BusContext) -> u32 { cx.wait += self.wait_states; let Some((memory, offset)) = self.region(address) else { return 0 }; if width == 32 { return u32::from_be_bytes([memory[offset], memory[offset + 1], memory[offset + 2], memory[offset + 3]]); } if width == 16 { return ((memory[offset] as u32) << 8) | memory[offset + 1] as u32; } memory[offset] as u32 } fn write(&mut self, address: u32, width: u32, value: u32, _kind: u32, cx: &mut BusContext) { cx.wait += self.wait_states; let Some((memory, offset)) = self.region(address) else { return }; let bytes = value.to_be_bytes(); let count = (width >> 3) as usize; memory[offset..offset + count].copy_from_slice(&bytes[4 - count..]); } fn on_ftoa(&mut self, level: u32, clock: i64, _pins: &mut Pins) { self.ftoa_log.push((clock, level)); } } /// A flat two-region memory around an [`Sh7604`]. pub struct Harness { pub chip: Sh7604, pub irl_script: Vec<(i64, u32)>, } impl Harness { pub fn new(image: &[u8], wait: i64, irl_script: Vec<(i64, u32)>, options: Options) -> Harness { let mut low = vec![0; AREA_SIZE]; low[..image.len()].copy_from_slice(image); let memory = Memory { low, high: vec![0; AREA_SIZE], wait_states: wait, ftoa_log: Vec::new() }; let mut script = irl_script; script.sort(); Harness { chip: Sh7604::new(memory, options), irl_script: script } } /// The word at ``address`` (cache-area or cache-through address), side-effect free. pub fn peek16(&mut self, address: u32) -> u32 { match self.chip.bus.region(address & 0x1FFFFFFF) { None => 0, Some((memory, offset)) => ((memory[offset] as u32) << 8) | memory[(offset + 1) & MASK] as u32, } } pub fn load(&mut self, address: u32, data: &[u8]) { match self.chip.bus.region(address & 0x1FFFFFFF) { None => panic!("no memory at {address:#010x}"), Some((memory, offset)) => memory[offset..offset + data.len()].copy_from_slice(data), } } /// Why the next step would never leave this instruction, or ``None``. pub fn stopped(&mut self) -> Option<&'static str> { if self.chip.delay_target >= 0 { return None; } let pc = self.chip.pc; let op = self.peek16(pc); let chip = &self.chip; if op == 0x001B && (chip.sr >> 4) & 15 == 15 { return Some("sleep"); } if chip.sleeping && (chip.sr >> 4) & 15 == 15 { return Some("sleep"); } if op & 0xFF == 0xFE { let high = op >> 8; let t = chip.sr & 1; if ((op >> 12) == 0xA && op & 0xFFF == 0xFFE) // BRA $ || (high == 0x89 && t != 0) // BT $ || (high == 0x8B && t == 0) // BF $ || (high == 0x8D && t != 0) // BT/S $ || (high == 0x8F && t == 0) // BF/S $ { return Some("branch-to-self"); } } None } /// Reset-free run from the current state until a stop condition or a budget. pub fn run(&mut self, max_states: i64, max_steps: Option) -> RunResult { let start = self.chip.clock; let mut steps = 0; let reason; loop { while !self.irl_script.is_empty() && self.irl_script[0].0 <= self.chip.clock { self.chip.irl = self.irl_script.remove(0).1; } if let Some(why) = self.stopped() { reason = why; break; } if self.chip.clock - start >= max_states { reason = "state-budget"; break; } if let Some(limit) = max_steps { if steps >= limit { reason = "step-budget"; break; } } self.chip.step(); steps += 1; } let chip = &self.chip; RunResult { reason, r0: chip.r[0], pc: chip.pc, states: chip.clock - start, steps, ftoa: chip.bus.ftoa_log.clone(), } } }