litz-board 0.5.1

Board runtime for Litz: flatten a diagram, resolve nets, and drive parts from pin levels.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! What the runner needs from an MCU, whichever way that MCU is provided.
//!
//! [`Board`] owns the parts and resolves nets; everything above it — tests,
//! the CLI, a UI — talks to a `Chip`. Two implementations exist: the
//! `litz-esp` adapter over esp32sim, and `litz-mcu-wasm`, which loads a
//! WebAssembly component that speaks the `litz:mcu` contract. Neither the
//! board nor the runner knows which of the two it has.
//!
//! The trait is deliberately close to what a chip actually does: load images,
//! boot, run for a while, and exchange pin levels and console bytes. Anything
//! that only makes sense for one backend (a mask ROM, an esp32sim input
//! script) stays out of it.

use crate::Conflict;
use litz_core::{Level, PartState};

/// A piece of non-volatile memory a chip can be asked to load.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ImageKind {
    /// The mask ROM / firmware burned into the chip.
    Rom,
    /// A second-stage bootloader.
    Bootloader,
    /// A partition table.
    PartitionTable,
    /// The application.
    Firmware,
}

/// Where images live, in the chip's own terms.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FlashLayout {
    pub size: u64,
    pub bootloader: u64,
    pub partition_table: u64,
    pub app: u64,
}

/// Why a run came back.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Stop {
    /// The cycle budget ran out; more work remains.
    Budget,
    /// Nothing to do until the host changes an input.
    Idle,
    /// The core stopped on purpose.
    Halted,
    /// The core faulted; the detail was reported through the chip's fault
    /// channel.
    Trap,
    /// The chip asked for its own reset.
    Reset,
    /// The run reached a host-requested breakpoint at this address.
    Breakpoint(u32),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RunResult {
    pub cycles: u64,
    pub stop: Stop,
}

/// Something the host wants to happen at a simulated time.
///
/// Scheduling lives above the trait so each backend can implement it with the
/// clock it already has: esp32sim renders a script, a wasm chip compares
/// against the time it is asked to run to.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Input {
    /// Assert a level on a chip pin (a bare GPIO number, not a part).
    Gpio { at_ns: u64, pin: u8, level: Level },
    /// Deliver bytes to the chip's console (UART0 receive).
    Uart { at_ns: u64, bytes: Vec<u8> },
}

/// The chip's own description.
#[derive(Clone, Debug)]
pub struct ChipInfo {
    pub name: String,
    pub cpu_hz: u64,
    pub flash: FlashLayout,
}

/// An MCU driving a [`crate::Board`].
///
/// Implementations push resolved input levels into the chip and read the drive
/// it holds on each pad back out; the board does the rest.
pub trait Chip {
    fn info(&self) -> ChipInfo;

    fn load_image(&mut self, kind: ImageKind, offset: u64, bytes: &[u8]) -> Result<(), String>;

    /// Make external PSRAM visible to the chip. A board that has none leaves
    /// the default, which reports the absence rather than pretending.
    fn set_psram_size(&mut self, _bytes: usize) -> Result<(), String> {
        Err("this backend has no external PSRAM".into())
    }

    /// Start the firmware and return its entry address.
    fn boot(&mut self) -> Result<u32, String>;

    /// Queue host actions. Replaces anything queued before.
    fn schedule(&mut self, inputs: Vec<Input>) -> Result<(), String>;

    /// Run for at most `budget_cycles` device cycles.
    fn run(&mut self, budget_cycles: u64) -> RunResult;

    /// Simulated seconds since reset.
    fn seconds(&self) -> f64;

    /// Bytes from a host terminal into the chip's console receive path.
    fn write_console(&mut self, bytes: &[u8]);

    /// Console bytes the chip has produced since the last call.
    fn take_console(&mut self) -> Vec<u8>;

    fn state(&self, part: &str) -> Option<PartState>;

    fn press(&mut self, part: &str, pressed: bool) -> bool;

    fn set_control(&mut self, part: &str, value: u16) -> bool;

    /// Set a named control on a part (`pressed`, `temperature`, `lux`). The
    /// default says the backend has no such surface, so a scenario can report
    /// the part rather than silently doing nothing.
    fn set_named_control(&mut self, _part: &str, _name: &str, _value: &str) -> bool {
        false
    }

    fn part_pin_level(&self, part: &str, pin: Option<&str>) -> Option<Level>;

    /// The level this chip sees on one of its own pads, by GPIO number. This
    /// is how a scenario addresses a board pin (`part-id: esp, name: 2`).
    fn gpio_level(&self, _gpio: u8) -> Option<Level> {
        None
    }

    /// Electrical faults the board found, in net order.
    fn conflicts(&self) -> Vec<Conflict>;

    /// Begin recording net transitions for [`Chip::vcd`]. A backend that does
    /// not record leaves the default and returns an empty dump.
    fn start_trace(&mut self) {}

    /// The recorded waveform as a Value Change Dump, empty when nothing was
    /// recorded.
    fn vcd(&self) -> String {
        String::new()
    }

    /// Stop the next run at this program counter, where the backend supports
    /// breakpoints. The stop comes back as [`Stop::Breakpoint`].
    fn break_at(&mut self, _pc: u32) {}

    /// Registers of the stopped core, for a debug print. Empty when the
    /// backend does not expose them.
    fn registers(&self) -> String {
        String::new()
    }

    /// Backend reports collected during the run — coverage, a pc histogram,
    /// a heap summary — one string per observer. Empty when no observer is
    /// enabled or the backend has none.
    fn report(&mut self) -> Vec<String> {
        Vec::new()
    }

    /// Turn on instruction coverage for the next run, where the backend can.
    /// A backend without coverage ignores it.
    fn enable_coverage(&mut self) {}

    /// Why the chip stopped early, when it stopped for its own reasons (a
    /// trap, a fault the guest reported). `None` for a chip that has nothing
    /// to say.
    fn fault(&self) -> Option<String> {
        None
    }

    /// The first display on the board's buses as RGB565, with its width,
    /// height and change counter. `None` when the circuit has no display, or
    /// when this backend cannot reach the board's display devices.
    fn display(&mut self) -> Option<(u32, u32, Vec<u16>, u64)> {
        None
    }

    /// The change counter of the output of [`Chip::display`], without building
    /// a frame. Zero when there is no display to ask.
    fn display_version(&mut self) -> u64 {
        0
    }

    /// Write the simulated display to `path` as a PNG. A backend whose boards
    /// have no framebuffer says so instead of leaving an empty file.
    fn save_display(&mut self, _path: &std::path::Path) -> Result<(), String> {
        Err("this backend has no display to capture".into())
    }

    /// Write one display part's framebuffer to `path` as a PNG, by the part's
    /// diagram id. A backend without a board or a part with no display says so.
    fn save_part_display(&mut self, _part: &str, _path: &std::path::Path) -> Result<(), String> {
        Err("this backend cannot capture a part".into())
    }

    /// Deliver a touch at panel coordinates. A board with a touch controller
    /// sees the press through that part; a board without one says so.
    fn touch(&mut self, _x: u16, _y: u16, _down: bool) -> Result<(), String> {
        Err("this board has no touch controller".into())
    }

    /// Ask a running firmware for its free heap, by calling its own heap
    /// getter. Backends that cannot call into the guest say so.
    fn sample_heap(&mut self) -> Result<u32, String> {
        Err("this backend cannot sample a guest heap".into())
    }

    /// Record every 802.11 frame the radio puts on the air into a pcap with
    /// link type 105, written as the run goes.
    fn start_air_capture(&mut self, _path: &std::path::Path) -> Result<(), String> {
        Err("this backend has no 802.11 radio".into())
    }

    /// Record the Ethernet frames the station's MAC carries into a pcap with
    /// link type 1, written as the run goes.
    fn start_ethernet_capture(&mut self, _path: &std::path::Path) -> Result<(), String> {
        Err("this backend has no Ethernet transport".into())
    }

    /// Put host sockets behind the Wi-Fi gateway, forwarding these host ports
    /// into the guest (`tcp:[host:]port-[guest:]port`). Returns one line per
    /// forward. A run that uses this is not reproducible off the host.
    fn attach_host_gateway(&mut self, _forwards: &[String]) -> Result<Vec<String>, String> {
        Err("this backend has no Wi-Fi radio".into())
    }

    /// The debugger surface, when the backend has one. The protocol lives in
    /// the CLI; handing it a `ChipDebug` keeps every register and memory
    /// access on the backend that owns it.
    fn debug(&mut self) -> Option<&mut dyn ChipDebug> {
        None
    }

    /// Whether this backend can answer the browser-facing debug calls below.
    /// A backend that returns false leaves every other debug method unused.
    fn debug_available(&self) -> bool {
        false
    }

    /// The stopped core's program counter.
    fn debug_pc(&self) -> Option<u32> {
        None
    }

    /// The 32 integer registers followed by the program counter.
    fn debug_register_file(&self) -> Option<[u32; 33]> {
        None
    }

    /// Bytes readable at `addr`, stopping at the first inaccessible word.
    fn debug_read_memory(&mut self, _addr: u32, _len: usize) -> Vec<u8> {
        Vec::new()
    }

    /// `count` instructions at `addr`, in the backend's disassembly format.
    fn debug_disassemble(&mut self, _addr: u32, _count: usize) -> String {
        String::new()
    }

    /// Replace the armed breakpoint set. Empty clears every breakpoint.
    fn debug_set_breakpoints(&mut self, _pcs: &[u32]) {}

    /// Execute exactly one instruction and report why execution ended.
    fn debug_step(&mut self) -> Option<Stop> {
        None
    }
}

/// A GDB server over one chip.
///
/// The trait exists so a boxed [`Chip`] can still hand out its debug surface:
/// the runner knows a session can be served, and the backend knows how to read
/// its own registers and memory.
pub trait ChipDebug {
    /// Serve one client on `127.0.0.1:port`; returns when it disconnects.
    fn serve(&mut self, port: u16) -> Result<(), String>;

    /// The registers of the stopped core, for a run report.
    fn report(&mut self) -> String {
        String::new()
    }
}
/// A boxed chip is still a chip, so callers can hold the backend behind `dyn`
/// without threading a lifetime through every helper.
impl<T: Chip + ?Sized> Chip for Box<T> {
    fn info(&self) -> ChipInfo {
        (**self).info()
    }
    fn load_image(&mut self, kind: ImageKind, offset: u64, bytes: &[u8]) -> Result<(), String> {
        (**self).load_image(kind, offset, bytes)
    }
    fn set_psram_size(&mut self, bytes: usize) -> Result<(), String> {
        (**self).set_psram_size(bytes)
    }
    fn boot(&mut self) -> Result<u32, String> {
        (**self).boot()
    }
    fn schedule(&mut self, inputs: Vec<Input>) -> Result<(), String> {
        (**self).schedule(inputs)
    }
    fn run(&mut self, budget_cycles: u64) -> RunResult {
        (**self).run(budget_cycles)
    }
    fn seconds(&self) -> f64 {
        (**self).seconds()
    }
    fn write_console(&mut self, bytes: &[u8]) {
        (**self).write_console(bytes)
    }
    fn take_console(&mut self) -> Vec<u8> {
        (**self).take_console()
    }
    fn state(&self, part: &str) -> Option<PartState> {
        (**self).state(part)
    }
    fn press(&mut self, part: &str, pressed: bool) -> bool {
        (**self).press(part, pressed)
    }
    fn set_control(&mut self, part: &str, value: u16) -> bool {
        (**self).set_control(part, value)
    }
    fn set_named_control(&mut self, part: &str, name: &str, value: &str) -> bool {
        (**self).set_named_control(part, name, value)
    }
    fn part_pin_level(&self, part: &str, pin: Option<&str>) -> Option<Level> {
        (**self).part_pin_level(part, pin)
    }
    fn gpio_level(&self, gpio: u8) -> Option<Level> {
        (**self).gpio_level(gpio)
    }
    fn conflicts(&self) -> Vec<Conflict> {
        (**self).conflicts()
    }
    fn fault(&self) -> Option<String> {
        (**self).fault()
    }
    fn save_display(&mut self, path: &std::path::Path) -> Result<(), String> {
        (**self).save_display(path)
    }
    fn save_part_display(&mut self, part: &str, path: &std::path::Path) -> Result<(), String> {
        (**self).save_part_display(part, path)
    }
    fn sample_heap(&mut self) -> Result<u32, String> {
        (**self).sample_heap()
    }
    fn touch(&mut self, x: u16, y: u16, down: bool) -> Result<(), String> {
        (**self).touch(x, y, down)
    }
    fn start_air_capture(&mut self, path: &std::path::Path) -> Result<(), String> {
        (**self).start_air_capture(path)
    }
    fn start_ethernet_capture(&mut self, path: &std::path::Path) -> Result<(), String> {
        (**self).start_ethernet_capture(path)
    }
    fn attach_host_gateway(&mut self, forwards: &[String]) -> Result<Vec<String>, String> {
        (**self).attach_host_gateway(forwards)
    }
    fn debug(&mut self) -> Option<&mut dyn ChipDebug> {
        (**self).debug()
    }
    fn debug_available(&self) -> bool {
        (**self).debug_available()
    }
    fn debug_pc(&self) -> Option<u32> {
        (**self).debug_pc()
    }
    fn debug_register_file(&self) -> Option<[u32; 33]> {
        (**self).debug_register_file()
    }
    fn debug_read_memory(&mut self, addr: u32, len: usize) -> Vec<u8> {
        (**self).debug_read_memory(addr, len)
    }
    fn debug_disassemble(&mut self, addr: u32, count: usize) -> String {
        (**self).debug_disassemble(addr, count)
    }
    fn debug_set_breakpoints(&mut self, pcs: &[u32]) {
        (**self).debug_set_breakpoints(pcs)
    }
    fn debug_step(&mut self) -> Option<Stop> {
        (**self).debug_step()
    }
    fn start_trace(&mut self) {
        (**self).start_trace()
    }
    fn vcd(&self) -> String {
        (**self).vcd()
    }
    fn break_at(&mut self, pc: u32) {
        (**self).break_at(pc)
    }
    fn registers(&self) -> String {
        (**self).registers()
    }
    fn report(&mut self) -> Vec<String> {
        (**self).report()
    }
    fn enable_coverage(&mut self) {
        (**self).enable_coverage()
    }
}