litz_core/lib.rs
1//! Foundation types shared by every Litz crate.
2//!
3//! Litz is a firmware simulator split along three seams:
4//!
5//! * [`Bus`] — what the CPU sees: an address space, peripherals hang off it.
6//! * [`Mcu`] — a chip: CPU core plus on-chip peripherals, stepped by time.
7//! * [`Part`] — anything on the schematic: LED, button, sensor, display. It only
8//! ever sees pin levels, never the MCU's internals.
9//!
10//! Keeping those three apart is what lets `litz-esp` and `litz-avr` share the
11//! same parts bin, and what lets the same core compile for a host CLI and for
12//! `wasm32-unknown-unknown`.
13//!
14//! Time is a `u64` count of nanoseconds ([`Tick`]). Every peripheral declares
15//! its own clock period in ticks; a domain whose period does not divide one
16//! nanosecond (a 160 MHz core is 6.25 ns per cycle) carries a fractional
17//! remainder, which is exactly what a hardware clock divider does. No floating
18//! point anywhere in the schedule, so two runs of one firmware produce the
19//! same trace on every host.
20#![no_std]
21#![forbid(unsafe_code)]
22
23extern crate alloc;
24
25/// Nanoseconds since simulation start.
26pub type Tick = u64;
27
28/// Ticks in one second. Time is fixed-point by construction.
29pub const TICKS_PER_SEC: Tick = 1_000_000_000;
30
31/// A digital pin level.
32#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
33pub enum Level {
34 Low,
35 High,
36 /// High-impedance: the pin is not driving, a net must resolve it from
37 /// other drivers or a pull resistor.
38 HighZ,
39}
40
41/// Identifier of a device instance in a [`Board`].
42#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
43pub struct PartId(pub u16);
44
45/// Identifier of one pin of a [`Part`].
46#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
47pub struct PinId(pub u16);
48
49/// The address space a CPU core sees.
50///
51/// Peripherals are not methods on the bus: a bus owns an ordered table of
52/// address ranges and forwards to whichever peripheral claims the range. That
53/// keeps the chip definition declarative and lets a peripheral be moved from
54/// one chip to another without touching the CPU.
55pub trait Bus {
56 fn read8(&mut self, addr: u32) -> u8;
57 fn read16(&mut self, addr: u32) -> u16 {
58 u16::from(self.read8(addr)) | (u16::from(self.read8(addr + 1)) << 8)
59 }
60 fn read32(&mut self, addr: u32) -> u32 {
61 u32::from(self.read16(addr)) | (u32::from(self.read16(addr + 2)) << 16)
62 }
63 fn write8(&mut self, addr: u32, value: u8);
64 fn write16(&mut self, addr: u32, value: u16) {
65 self.write8(addr, value as u8);
66 self.write8(addr + 1, (value >> 8) as u8);
67 }
68 fn write32(&mut self, addr: u32, value: u32) {
69 self.write16(addr, value as u16);
70 self.write16(addr + 2, (value >> 16) as u16);
71 }
72}
73
74/// A microcontroller: a CPU plus the peripherals on its bus.
75///
76/// [`Mcu::step`] advances exactly one instruction (or one hardware event,
77/// whichever comes first in time) and returns the tick at which it next needs
78/// attention. The scheduler runs the MCU whose next tick is smallest.
79pub trait Mcu {
80 fn reset(&mut self, now: Tick);
81
82 /// Execute one step and return the earliest tick at which this MCU must be
83 /// stepped again.
84 fn step(&mut self, now: Tick) -> Tick;
85
86 /// Current level driven by an external pin, if the chip drives it.
87 fn pin_level(&self, pin: u32) -> Level;
88
89 /// A [`Part`] changed the level it drives onto a chip pin.
90 fn set_pin_level(&mut self, pin: u32, level: Level, now: Tick);
91}
92
93/// A schematic component. Parts never read MCU registers; they react to pin
94/// levels and to time, which is the same contract a real part has.
95pub trait Part {
96 /// Called for every level change on a pin this part is attached to.
97 fn pin_changed(&mut self, pin: PinId, level: Level, now: Tick);
98
99 /// The level this part drives back onto a net, if any.
100 fn drive(&self, pin: PinId) -> Level;
101
102 /// Earliest tick at which this part wants [`Part::tick`] called again.
103 fn next_tick(&self) -> Option<Tick> {
104 None
105 }
106
107 fn tick(&mut self, _now: Tick) {}
108}