// SPDX-License-Identifier: MIT //! What a bus callback is told about the cycle it makes: the ``kind`` bits. //! //! sh2-python's `bus.py`. The board implements [`Bus`] (`read(address, width, kind)` and //! `write(address, width, value, kind)`, the reference's two callables). Each call is //! **one external bus cycle** the bus state controller (BSC) starts: the core has already //! removed the cache-space bits (A31-A29), split the access into port-width cycles (HM //! 7.4.1, a longword on a 16-bit area is two calls, high word first) and decided the //! cycle is external. Cache hits, on-chip module accesses, and the address- and //! data-array spaces never reach the callbacks (HM 7.11.2: they use the cache bus or the //! peripheral bus). //! //! ``kind`` is a sum of these bits (docs/handoff-brief.md, "The bus contract"; decided by //! Aubrey 2026-10-23: misses appear as ``FILL`` cycles, hits only on ``observe``). //! //! What Rust forces: the reference's callables are plain attributes a host may replace, //! and inside a callback the board reads and writes `chip.clock`, `chip.wait`, //! `chip.bsc.cycle_states` and the pins. Here the chip owns its board (`chip.bus`) and //! hands each callback a [`BusContext`] holding those values; what the board changes in //! it (`wait`, the pins) is copied back when the callback returns. The reference's //! optional callables (`observe`, `on_ftoa`, ...) are [`Bus`] methods that do nothing by //! default; `observe` is called only when the chip's `observe` flag is set (the //! reference's `observe is not None`). The replaceable `read_onchip`/`write_onchip` are //! the three `*_onchip` methods. /// An ordinary data access (no bit set). pub const DATA: u32 = 0; /// An instruction fetch: always a longword at the CPU (HM 7.11.2), split by the port. pub const FETCH: u32 = 1; /// One longword of a cache line fill after a miss: four per line, in HM's wrap order /// (address + 4, + 8, + 12, then the missed longword; HM 8.4.1, figure 8.4). pub const FILL: u32 = 2; /// Set on the first cycle of a line fill. pub const FIRST: u32 = 4; /// A cache-through access (A31-A29 = 001), or a cache-area access with the cache off. pub const THROUGH: u32 = 8; /// A DMAC cycle; ``DMA1`` is also set for channel 1. pub const DMA: u32 = 16; pub const DMA1: u32 = 32; /// An external vector fetch (ICR.VECMD = 1; HM table 4.3 note 2). pub const VECTOR: u32 = 64; /// A BSC refresh cycle (no data; made through ``read``, value ignored). pub const REFRESH: u32 = 128; /// ``observe`` only: the access hit the cache and made no bus cycle. pub const HIT: u32 = 256; /// ``observe`` only: the access is a write. pub const WRITE: u32 = 512; pub const KIND_NAMES: [(u32, &str); 10] = [ (FETCH, "FETCH"), (FILL, "FILL"), (FIRST, "FIRST"), (THROUGH, "THROUGH"), (DMA, "DMA"), (DMA1, "DMA1"), (VECTOR, "VECTOR"), (REFRESH, "REFRESH"), (HIT, "HIT"), (WRITE, "WRITE"), ]; /// ``FETCH|FILL|FIRST`` style text for a kind value (``DATA`` when no bit is set). pub fn kind_text(kind: u32) -> String { let names: Vec<&str> = KIND_NAMES.iter().filter(|(bit, _)| kind & bit != 0).map(|(_, name)| *name).collect(); if names.is_empty() { "DATA".to_string() } else { names.join("|") } } /// The pins in, as the board drives them (the reference's `chip.irl`, `chip.nmi`, ...). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct Pins { /// 0-15, the inverted IRL3-IRL0 level. pub irl: u32, /// The NMI pin's level; the edge is detected. pub nmi: u32, pub dreq0: u32, pub dreq1: u32, pub ftci: u32, pub fti: u32, } /// What a callback sees of the chip and may change: the reference's `chip.clock`, /// `chip.wait`, `chip.bsc.cycle_states`, `chip.bsc.waits`, `chip.bsc.wait_pin` and the /// pins in. (`waits` and `wait_pin` were added for the 32X board, sega32x-rust, whose /// reference reads them inside its callbacks to decide the WAIT it drives; read only.) #[derive(Clone, Copy, Debug)] pub struct BusContext { /// The state on which this bus cycle starts (φ since construction). pub clock: i64, /// 0 on entry; the board adds WAIT-pin states here (arbitration, slow devices). pub wait: i64, /// The BSC's states for each area: for this call's area, the states of this call. pub cycle_states: [i64; 4], /// The BSC's programmed waits for each area (ordinary space) and whether it samples /// WAIT there: read only, a copy of `chip.bsc.waits` and `chip.bsc.wait_pin`. pub waits: [i64; 4], pub wait_pin: [bool; 4], /// The pins in; the board may change them inside any callback. pub pins: Pins, } /// The board: one call per external bus cycle, and the optional callables. pub trait Bus { /// One external read cycle. fn read(&mut self, address: u32, width: u32, kind: u32, cx: &mut BusContext) -> u32; /// One external write cycle. fn write(&mut self, address: u32, width: u32, value: u32, kind: u32, cx: &mut BusContext); /// Every CPU access, cache hits included (``kind`` has ``HIT``), when the chip's /// `observe` flag is set. fn observe(&mut self, _address: u32, _width: u32, _kind: u32) {} /// The FTOA pin changed (the 32X ties it to IRL; the pins may be changed here). fn on_ftoa(&mut self, _level: u32, _clock: i64, _pins: &mut Pins) {} fn on_ftob(&mut self, _level: u32, _clock: i64, _pins: &mut Pins) {} fn on_dack(&mut self, _channel: u32, _clock: i64, _pins: &mut Pins) {} fn on_txd(&mut self, _byte: u32, _clock: i64, _pins: &mut Pins) {} fn on_wdtovf(&mut self, _clock: i64, _pins: &mut Pins) {} /// The reference's replaceable ``read_onchip``, first half: `Some(value)` answers /// instead of the module (which is then not read); `None` lets the module answer. fn read_onchip_instead(&mut self, _address: u32, _width: u32) -> Option { None } /// Its second half: what the read returns, given the module's answer. fn read_onchip_seen(&mut self, _address: u32, _width: u32, value: u32) -> u32 { value } /// The reference's replaceable ``write_onchip``: the value the module is given. fn write_onchip(&mut self, _address: u32, _width: u32, value: u32) -> u32 { value } }