litz-core 0.0.1

Core traits and types for Litz, an open-source, modular firmware simulator that runs natively and in WebAssembly.
Documentation
//! Foundation types shared by every Litz crate.
//!
//! Litz is a firmware simulator split along three seams:
//!
//! * [`Bus`] — what the CPU sees: an address space, peripherals hang off it.
//! * [`Mcu`] — a chip: CPU core plus on-chip peripherals, stepped by time.
//! * [`Part`] — anything on the schematic: LED, button, sensor, display. It only
//!   ever sees pin levels, never the MCU's internals.
//!
//! Keeping those three apart is what lets `litz-esp` and `litz-avr` share the
//! same parts bin, and what lets the same core compile for a host CLI and for
//! `wasm32-unknown-unknown`.
//!
//! Time is a `u64` count of nanoseconds ([`Tick`]). Every peripheral declares
//! its own clock period in ticks; a domain whose period does not divide one
//! nanosecond (a 160 MHz core is 6.25 ns per cycle) carries a fractional
//! remainder, which is exactly what a hardware clock divider does. No floating
//! point anywhere in the schedule, so two runs of one firmware produce the
//! same trace on every host.
#![no_std]
#![forbid(unsafe_code)]

extern crate alloc;

/// Nanoseconds since simulation start.
pub type Tick = u64;

/// Ticks in one second. Time is fixed-point by construction.
pub const TICKS_PER_SEC: Tick = 1_000_000_000;

/// A digital pin level.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Level {
    Low,
    High,
    /// High-impedance: the pin is not driving, a net must resolve it from
    /// other drivers or a pull resistor.
    HighZ,
}

/// Identifier of a device instance in a [`Board`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PartId(pub u16);

/// Identifier of one pin of a [`Part`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PinId(pub u16);

/// The address space a CPU core sees.
///
/// Peripherals are not methods on the bus: a bus owns an ordered table of
/// address ranges and forwards to whichever peripheral claims the range. That
/// keeps the chip definition declarative and lets a peripheral be moved from
/// one chip to another without touching the CPU.
pub trait Bus {
    fn read8(&mut self, addr: u32) -> u8;
    fn read16(&mut self, addr: u32) -> u16 {
        u16::from(self.read8(addr)) | (u16::from(self.read8(addr + 1)) << 8)
    }
    fn read32(&mut self, addr: u32) -> u32 {
        u32::from(self.read16(addr)) | (u32::from(self.read16(addr + 2)) << 16)
    }
    fn write8(&mut self, addr: u32, value: u8);
    fn write16(&mut self, addr: u32, value: u16) {
        self.write8(addr, value as u8);
        self.write8(addr + 1, (value >> 8) as u8);
    }
    fn write32(&mut self, addr: u32, value: u32) {
        self.write16(addr, value as u16);
        self.write16(addr + 2, (value >> 16) as u16);
    }
}

/// A microcontroller: a CPU plus the peripherals on its bus.
///
/// [`Mcu::step`] advances exactly one instruction (or one hardware event,
/// whichever comes first in time) and returns the tick at which it next needs
/// attention. The scheduler runs the MCU whose next tick is smallest.
pub trait Mcu {
    fn reset(&mut self, now: Tick);

    /// Execute one step and return the earliest tick at which this MCU must be
    /// stepped again.
    fn step(&mut self, now: Tick) -> Tick;

    /// Current level driven by an external pin, if the chip drives it.
    fn pin_level(&self, pin: u32) -> Level;

    /// A [`Part`] changed the level it drives onto a chip pin.
    fn set_pin_level(&mut self, pin: u32, level: Level, now: Tick);
}

/// A schematic component. Parts never read MCU registers; they react to pin
/// levels and to time, which is the same contract a real part has.
pub trait Part {
    /// Called for every level change on a pin this part is attached to.
    fn pin_changed(&mut self, pin: PinId, level: Level, now: Tick);

    /// The level this part drives back onto a net, if any.
    fn drive(&self, pin: PinId) -> Level;

    /// Earliest tick at which this part wants [`Part::tick`] called again.
    fn next_tick(&self) -> Option<Tick> {
        None
    }

    fn tick(&mut self, _now: Tick) {}
}