rsemu 0.0.2

A multiplatform emulator in pure Rust, built bottom-up on a generic framework.
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
//! The assembled machine: what [`realize`](mod@crate::machine::realize) produces
//! and what a run loop drives (`ROADMAP.md` §4).
//!
//! A [`Machine`] owns the four things a running machine is made of — the
//! address spaces (§4.1), the clock forest and scheduler (§4.2), the wire nets
//! (§4.3) and the device instances (§4.4) — plus the one piece of bookkeeping
//! that ties them to a snapshot: a **stable instance path** per device, which
//! is the chunk key §4.5 keys state by.
//!
//! Everything here is `no_std + alloc` and nothing names `std::sync`,
//! `std::thread` or the host clock: rate control takes a [`HostClock`]
//! injected from above the `std` line (invariant 4).
//!
//! [`HostClock`]: crate::core::sched::HostClock
//!
//! # The run loop
//!
//! ```text
//! run_quantum ─► Scheduler::run_quantum ─► Runnable::run per CPU (budgeted)
//!                                       └► events that came due
//!                    ─► Scheduler::sync_lazy_devices  (catch-up, §4.2)
//!                    ─► Instance::event per fired event
//!                    ─► Deferred::drain  after every handler
//! ```
//!
//! A quantum is bounded by the next event a **lazily advanced** device has of
//! its own, and every such device is caught up at the boundary. That is the
//! *scheduled* half of §4.2's sync-on-access; the *sampled* half fires from
//! inside the device's own `MemOps::read`, through a
//! [`LazyHandle`](crate::core::sched::LazyHandle) the realizer hands it.
//!
//! Only [`ThreadingMode::Deterministic`](crate::core::sched::ThreadingMode) is
//! driven here, which is the mode §4.2 requires for record/replay and for the
//! regression suite. The other two are a `core::sched` concern and report
//! themselves unimplemented.
//!
//! Events are dispatched **after** the quantum that made them due rather than
//! from inside it, because `Scheduler::run_quantum` collects them into its
//! report. That is not a loss of precision: a quantum never runs past the next
//! deadline, so the machine is standing exactly at the event's instant when the
//! handler runs.
//!
//! # Snapshots
//!
//! [`Machine::save`] writes one chunk per device keyed by instance path, plus
//! three chunks of machine-level state: [`CLOCK_PATH`] for the oscillator
//! forest, [`SCHED_PATH`] for virtual time and the event queue, and
//! [`WIRE_PATH`] for the levels every wire source is driving. All three begin
//! with `/`, which no object name can, so they can never collide with a device.
//!
//! The scheduler chunk is there because §4.5 says the scheduler *is*
//! architectural state: the pending events, the front of virtual time and the
//! tie-break sequence counter all have to survive a load, or a restored timer
//! comes back a whole period from firing instead of the forty cycles it was
//! actually at. It is written after the clocks and read back after them too,
//! since the positions it republishes to lazily-advanced devices are derived
//! from the forest's tick counters.

use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;

use crate::core::clock::{ClockForest, DomainId, GlobalTime};
use crate::core::device::{Deferred, Device, DeviceClass, ResetKind};
use crate::core::error::{Error, Result};
use crate::core::sched::{
    Budget, Consumed, Event, EventId, EventTarget, HostClock, LazyDevice, LazyId, QuantumReport,
    Runnable, RunnableId, Scheduler, SchedulerSnapshot,
};
use crate::core::space::{AddressSpace, RequesterId};
use crate::core::state::{MachineShape, Migrations, Sink, Source, StateReader, StateWriter};
use crate::core::wire::{Level, Wire, WireId};
use crate::machine::realize::Instance;

/// The snapshot chunk holding the oscillator forest's tick counters.
pub const CLOCK_PATH: &str = "/clock";

/// The class name recorded on the [`CLOCK_PATH`] chunk.
pub const CLOCK_CLASS: &str = "machine.clock";

/// The snapshot chunk holding every wire source's level.
pub const WIRE_PATH: &str = "/wires";

/// The class name recorded on the [`WIRE_PATH`] chunk.
pub const WIRE_CLASS: &str = "machine.wires";

/// The snapshot chunk holding the scheduler: virtual time and the event queue.
pub const SCHED_PATH: &str = "/sched";

/// The class name recorded on the [`SCHED_PATH`] chunk.
pub const SCHED_CLASS: &str = "machine.sched";

/// The version of the machine-level chunks written by this build.
pub const MACHINE_STATE_VERSION: u32 = 1;

/// One address space, with the name the machine description gave it.
///
/// The space is behind an `Arc` because devices that initiate accesses need to
/// hold their own view of it (§4.4's `Initiator`). The topology is *not* frozen
/// by that: every `AddressSpace` method takes `&self`, and a retopology goes
/// through `AddressSpace::topology()`, so a BAR move or a hot-plug can still
/// remap a space this entry has already handed out.
#[derive(Debug)]
pub struct SpaceEntry {
    name: String,
    space: Arc<AddressSpace>,
}

impl SpaceEntry {
    /// The space's name, as the `space` statement spelled it.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The space itself.
    pub fn space(&self) -> &Arc<AddressSpace> {
        &self.space
    }
}

/// One device instance and everything the machine knows about it.
#[derive(Debug)]
pub struct DeviceEntry {
    pub(crate) path: String,
    pub(crate) class: &'static DeviceClass,
    pub(crate) device: Arc<dyn Device>,
    pub(crate) instance: Option<Arc<dyn Instance>>,
    pub(crate) domain: Option<DomainId>,
    pub(crate) space: Option<usize>,
    pub(crate) requester: RequesterId,
    pub(crate) runnable: Option<RunnableId>,
    pub(crate) lazy: Option<LazyId>,
}

impl DeviceEntry {
    /// The instance path — the snapshot chunk key (§4.5), stable for the life
    /// of the machine.
    pub fn path(&self) -> &str {
        &self.path
    }

    /// The class this instance was built from.
    pub fn class(&self) -> &'static DeviceClass {
        self.class
    }

    /// The device.
    pub fn device(&self) -> &Arc<dyn Device> {
        &self.device
    }

    /// The device's machine-layer view, when its class is bound (see
    /// [`Bindings`](crate::machine::realize::Bindings)).
    pub fn instance(&self) -> Option<&Arc<dyn Instance>> {
        self.instance.as_ref()
    }

    /// The clock domain it runs in, if it declared one.
    pub fn domain(&self) -> Option<DomainId> {
        self.domain
    }

    /// The address space it declared, as an index into [`Machine::spaces`].
    pub fn space_index(&self) -> Option<usize> {
        self.space
    }

    /// Its requester id, as it appears in `MemAttrs` for accesses it initiates.
    pub fn requester(&self) -> RequesterId {
        self.requester
    }

    /// Its scheduler handle, if it takes execution budgets.
    pub fn runnable(&self) -> Option<RunnableId> {
        self.runnable
    }

    /// Its catch-up handle, if it declared itself lazily advanced (§4.2).
    pub fn lazy(&self) -> Option<LazyId> {
        self.lazy
    }
}

/// One wire net: a set of pins that are the same piece of copper.
#[derive(Debug)]
pub struct Net {
    pub(crate) wire: Arc<Wire>,
    pub(crate) sources: Vec<PinRef>,
}

impl Net {
    /// The net itself.
    pub fn wire(&self) -> &Arc<Wire> {
        &self.wire
    }

    /// The pins driving it, in the order their ids were allocated.
    pub fn sources(&self) -> &[PinRef] {
        &self.sources
    }
}

/// One end of a wire: a device and one of its pins.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PinRef {
    /// Index into [`Machine::devices`].
    pub device: usize,
    /// The pin's name, as the device knows it.
    pub port: String,
    /// The id this pin drives the net with, for a source pin.
    pub id: WireId,
}

/// The `Runnable` the scheduler sees, wrapping the `Instance` the machine owns.
///
/// The shim exists because the two halves disagree about mutability: §4.6's
/// `Cpu::run` takes `&self` (a device is shared — it is `Send + Sync` with
/// interior mutability), while [`Runnable::run`] takes `&mut self` and the
/// scheduler takes ownership of the box. Forwarding through an `Arc` is the
/// only way to satisfy both without the machine giving up ownership of its own
/// device.
pub(crate) struct RunAdapter {
    inner: Arc<dyn Instance>,
}

impl RunAdapter {
    /// Wrap `inner` so the scheduler can hand it budgets.
    pub(crate) fn new(inner: Arc<dyn Instance>) -> RunAdapter {
        RunAdapter { inner }
    }
}

impl Runnable for RunAdapter {
    fn run(&mut self, budget: Budget) -> Consumed {
        self.inner.run(budget)
    }
}

/// The [`LazyDevice`] the scheduler sees, wrapping the device the machine owns.
///
/// The same shim as [`RunAdapter`] and for the same reason: a `Device` declares
/// itself lazily advanced through `&self` methods, because a device is shared
/// and holds its state behind interior mutability, while
/// [`LazyDevice::advance_to`] takes `&mut self`. Forwarding through an `Arc` is
/// what satisfies both.
///
/// The `&mut` is not wasted. The scheduler takes the box *out* of its slot for
/// the duration of the call (`core::sched`), so the exclusive borrow is what
/// makes a device that re-enters its own catch-up get
/// [`SchedError::LazyDeviceBusy`](crate::core::sched::SchedError::LazyDeviceBusy)
/// rather than a deadlock.
pub(crate) struct LazyAdapter {
    inner: Arc<dyn Device>,
}

impl LazyAdapter {
    /// Wrap `inner` so the scheduler can catch it up.
    pub(crate) fn new(inner: Arc<dyn Device>) -> LazyAdapter {
        LazyAdapter { inner }
    }
}

impl LazyDevice for LazyAdapter {
    fn current_tick(&self) -> u64 {
        self.inner.current_tick()
    }

    fn advance_to(&mut self, tick: u64) {
        // `&self` on the device side: `&mut self` here is the scheduler's
        // exclusivity, not the device's.
        self.inner.advance_to(tick);
    }

    fn next_event_tick(&self) -> Option<u64> {
        self.inner.next_event_tick()
    }

    fn sampled_every_cycle(&self) -> bool {
        self.inner.sampled_every_cycle()
    }
}

/// A realized machine: spaces, clocks, wires and devices, ready to run.
///
/// Built by [`realize`](crate::machine::realize::realize). Nothing observable
/// happens before that call returns, so a description that fails half way
/// leaves no half-wired machine behind (§4.4).
#[derive(Debug)]
pub struct Machine {
    name: String,
    spaces: Vec<SpaceEntry>,
    sched: Scheduler,
    devices: Vec<DeviceEntry>,
    by_path: BTreeMap<String, usize>,
    nets: Vec<Net>,
    sweep: Vec<PinRef>,
    shape: MachineShape,
    deferred: Deferred,
}

/// The parts a realizer hands to [`Machine::assemble`].
///
/// A struct rather than eight positional arguments: every one of them is a
/// `Vec` or a name, and swapping two at a call site would compile.
#[derive(Debug)]
pub(crate) struct MachineParts {
    pub(crate) name: String,
    pub(crate) spaces: Vec<(String, Arc<AddressSpace>)>,
    pub(crate) sched: Scheduler,
    pub(crate) devices: Vec<DeviceEntry>,
    pub(crate) nets: Vec<Net>,
    pub(crate) sweep: Vec<PinRef>,
    pub(crate) shape: MachineShape,
    pub(crate) deferred: Deferred,
}

impl Machine {
    /// Assemble a machine from parts. Called by the realizer, and by nothing
    /// else — every field has an invariant the realizer establishes.
    pub(crate) fn assemble(parts: MachineParts) -> Machine {
        let by_path = parts
            .devices
            .iter()
            .enumerate()
            .map(|(i, d)| (d.path.clone(), i))
            .collect();
        Machine {
            name: parts.name,
            spaces: parts
                .spaces
                .into_iter()
                .map(|(name, space)| SpaceEntry { name, space })
                .collect(),
            sched: parts.sched,
            devices: parts.devices,
            by_path,
            nets: parts.nets,
            sweep: parts.sweep,
            shape: parts.shape,
            deferred: parts.deferred,
        }
    }

    /// The machine's name, as `machine "nes"` wrote it.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Every address space, in declaration order.
    pub fn spaces(&self) -> &[SpaceEntry] {
        &self.spaces
    }

    /// One address space by name.
    pub fn space(&self, name: &str) -> Option<&Arc<AddressSpace>> {
        self.spaces
            .iter()
            .find(|s| s.name == name)
            .map(SpaceEntry::space)
    }

    /// Every device instance, in declaration order — which is also reset order.
    pub fn devices(&self) -> &[DeviceEntry] {
        &self.devices
    }

    /// One device by instance path.
    pub fn device(&self, path: &str) -> Option<&DeviceEntry> {
        self.by_path.get(path).and_then(|i| self.devices.get(*i))
    }

    /// The index of the device at `path`, for scheduling events against it.
    pub fn device_index(&self, path: &str) -> Option<usize> {
        self.by_path.get(path).copied()
    }

    /// Every wire net, in the order the realizer built them.
    pub fn nets(&self) -> &[Net] {
        &self.nets
    }

    /// The scheduler, which owns virtual time and the clock forest.
    pub fn scheduler(&self) -> &Scheduler {
        &self.sched
    }

    /// The scheduler, mutably — for posting events and changing rate control.
    pub fn scheduler_mut(&mut self) -> &mut Scheduler {
        &mut self.sched
    }

    /// The oscillator forest (§4.2).
    pub fn clocks(&self) -> &ClockForest {
        self.sched.forest()
    }

    /// The current virtual instant.
    pub fn now(&self) -> GlobalTime {
        self.sched.now()
    }

    /// The machine's structural fingerprint, which a snapshot is checked
    /// against (§4.5).
    pub fn shape(&self) -> &MachineShape {
        &self.shape
    }

    /// Inject the host's monotonic clock, for rate control.
    ///
    /// Nothing below `host/` may read a wall clock (invariant 4), so the
    /// machine is handed one rather than reaching for it.
    pub fn set_host_clock(&mut self, clock: Box<dyn HostClock>) {
        self.sched.set_host_clock(clock);
    }

    // -----------------------------------------------------------------
    // reset and the realize sweep
    // -----------------------------------------------------------------

    /// Reset every device, then re-announce every wire source (§4.3).
    ///
    /// Devices are reset in **declaration order** — the order the machine file
    /// names them — because a reset order that depends on a hash or on the
    /// wiring is a reset order that changes between runs, and §0 does not allow
    /// that. A device that must be reset after another says so by being
    /// declared after it.
    ///
    /// The deferred queue is drained after each device, so an action a reset
    /// handler pushes runs before the next device is touched, in the order it
    /// was pushed.
    pub fn reset(&mut self, kind: ResetKind) {
        for i in 0..self.devices.len() {
            let device = Arc::clone(&self.devices[i].device);
            device.reset(kind);
            self.deferred.drain();
        }
        self.sweep();
    }

    /// The realize sweep: walk wire sources in topological order and announce
    /// the level each drives (§4.3).
    ///
    /// Without it a freshly realized — or freshly restored — machine is
    /// inconsistent: an undriven wire sits low, which contradicts an inverter
    /// whose output idles high, and the interrupt line comes up wrong on some
    /// machines and only on some paths. The order is
    /// [`realize_order`](crate::machine::validate::realize_order)'s, so a
    /// source announces before anything that forwards its level.
    pub fn sweep(&mut self) {
        for pin in &self.sweep {
            if let Some(instance) = self.devices[pin.device].instance.as_ref() {
                instance.announce(&pin.port);
            }
        }
        self.deferred.drain();
    }

    // -----------------------------------------------------------------
    // running
    // -----------------------------------------------------------------

    /// Run one scheduler quantum and dispatch whatever came due.
    ///
    /// # Errors
    ///
    /// Whatever the scheduler refuses — an overrun budget, an unimplemented
    /// threading mode — or an event addressed to a device that does not exist.
    pub fn run_quantum(&mut self) -> Result<QuantumReport> {
        let limit = self.quantum_limit(GlobalTime::MAX);
        let report = self.sched.run_quantum_until(limit)?;
        self.sched.sync_lazy_devices()?;
        self.dispatch(&report)?;
        Ok(report)
    }

    /// How far the next quantum may run: the caller's deadline, or the next
    /// instant a lazily-advanced device has an event of its own, whichever
    /// comes first (§4.2).
    ///
    /// This is the *scheduled* half of sync-on-access. Catch-up on access makes
    /// a `$2002` read see the dot it happened on, but nothing makes the PPU
    /// reach the dot it raises vblank on while the CPU is busy elsewhere — and
    /// a game whose main loop spins on a flag its NMI handler sets touches no
    /// PPU register at all. Stopping the CPU at the PPU's own next event, and
    /// catching the PPU up there, is what turns "advanced when read" into
    /// "advanced".
    ///
    /// A deadline that has already gone by is not reported by
    /// [`Scheduler::lazy_deadline`], so this never clamps a quantum to an
    /// instant the machine is already standing on — which would stall it.
    fn quantum_limit(&self, deadline: GlobalTime) -> GlobalTime {
        match self.sched.lazy_deadline() {
            Some(at) if at < deadline => at,
            _ => deadline,
        }
    }

    /// Run until virtual time reaches `deadline`.
    ///
    /// The loop is here rather than in `Scheduler::run_until` because that one
    /// discards the per-quantum report, and the report is where fired events
    /// are: driving it from above is what keeps them from being dropped.
    ///
    /// # Errors
    ///
    /// As [`Machine::run_quantum`], plus a machine whose configuration cannot
    /// advance virtual time at all.
    pub fn run_until(&mut self, deadline: GlobalTime) -> Result<()> {
        while self.sched.now() < deadline {
            let before = self.sched.now();
            let limit = self.quantum_limit(deadline);
            let report = self.sched.run_quantum_until(limit)?;
            // Before the events are dispatched: a handler that reads a lazily
            // advanced device must see it standing on the instant that fired,
            // not on the one the previous quantum ended at.
            self.sched.sync_lazy_devices()?;
            self.dispatch(&report)?;
            if self.sched.now() <= before {
                // A quantum always ends at `min(now + quantum, deadline, next
                // deadline)`, and events due at `now` have already been popped,
                // so this can only mean a zero quantum. Reporting it is the
                // only honest option: spinning would hang, and jumping to the
                // deadline through `Scheduler::run_until` would fire events
                // into a report nobody reads.
                return Err(Error::Config {
                    at: self.name.clone(),
                    message: "virtual time did not advance: the scheduler quantum is zero"
                        .to_string(),
                });
            }
        }
        Ok(())
    }

    /// Run for `span` of virtual time from wherever the machine is now.
    ///
    /// # Errors
    ///
    /// As [`Machine::run_quantum`].
    pub fn run_for(&mut self, span: GlobalTime) -> Result<()> {
        let deadline = self.sched.now().saturating_add(span);
        self.run_until(deadline)
    }

    /// Post an event for the device at `path`, `ticks` of its own clock domain
    /// from now.
    ///
    /// `token` is handed back to the device untouched: it is a timer index, a
    /// channel number, whatever the device put there.
    ///
    /// # Errors
    ///
    /// If no device is at `path`, if it has no clock domain, or if the clock
    /// conversion overflows.
    pub fn schedule_after_ticks(&mut self, path: &str, ticks: u64, token: u64) -> Result<EventId> {
        let index = self.device_index(path).ok_or_else(|| Error::Config {
            at: path.to_string(),
            message: "no device at this instance path".to_string(),
        })?;
        let domain = self.devices[index].domain.ok_or_else(|| Error::Config {
            at: path.to_string(),
            message: "cannot post an event for a device with no clock domain".to_string(),
        })?;
        let target = EventTarget(u32::try_from(index).unwrap_or(u32::MAX));
        Ok(self
            .sched
            .schedule_after_ticks(domain, ticks, target, token)?)
    }

    /// Deliver every event in `report` to its device, draining the deferred
    /// queue after each handler.
    ///
    /// The drain is per handler, not per quantum: an action a device defers is
    /// meant to run *after that handler returns* and before anything else
    /// observes the machine (§4.7's re-entrancy contract). Batching them to the
    /// end of the quantum would reorder them against the next event.
    fn dispatch(&mut self, report: &QuantumReport) -> Result<()> {
        for event in &report.fired {
            let index = event.target.0 as usize;
            let Some(instance) = self
                .devices
                .get(index)
                .map(|d| d.instance.clone())
                .ok_or_else(|| Error::Config {
                    at: self.name.clone(),
                    message: format!(
                        "event {} is addressed to device {index}, which does not exist",
                        event.id.seq()
                    ),
                })?
            else {
                // A device with no machine-layer view cannot have posted an
                // event, so this is a stale target rather than a lost handler.
                continue;
            };
            instance.event(event.token, &mut self.deferred);
            self.deferred.drain();
        }
        Ok(())
    }

    // -----------------------------------------------------------------
    // snapshots
    // -----------------------------------------------------------------

    /// Serialize the whole machine: one chunk per device, keyed by instance
    /// path, plus the clock forest, the scheduler and the wire levels (§4.5).
    ///
    /// # Errors
    ///
    /// Whatever a device's `save` reports, or a duplicate instance path.
    pub fn save(&self) -> Result<Vec<u8>> {
        let mut w = StateWriter::new(self.shape.clone());
        for entry in &self.devices {
            let mut chunk = w.chunk(&entry.path, entry.class.name, entry.class.version)?;
            entry.device.save(&mut chunk)?;
        }
        {
            let mut chunk = w.chunk(CLOCK_PATH, CLOCK_CLASS, MACHINE_STATE_VERSION)?;
            save_clocks(self.sched.forest(), &mut chunk)?;
        }
        {
            let mut chunk = w.chunk(SCHED_PATH, SCHED_CLASS, MACHINE_STATE_VERSION)?;
            save_sched(&self.sched, &mut chunk)?;
        }
        {
            let mut chunk = w.chunk(WIRE_PATH, WIRE_CLASS, MACHINE_STATE_VERSION)?;
            save_wires(&self.nets, &mut chunk)?;
        }
        w.to_vec()
    }

    /// Restore what [`Machine::save`] wrote, with no class migrations.
    ///
    /// # Errors
    ///
    /// As [`Machine::load_with`].
    pub fn load(&mut self, bytes: &[u8]) -> Result<()> {
        self.load_with(bytes, &Migrations::new())
    }

    /// Restore a snapshot, migrating device chunks through `migrations`.
    ///
    /// The machine's shape is checked first, so a snapshot taken from a
    /// differently-shaped machine fails with a diff naming what moved rather
    /// than by loading nonsense into the wrong device (§4.5).
    ///
    /// The realize sweep runs afterwards: a restored machine is as inconsistent
    /// as a fresh one until every gate drives what its inputs imply.
    ///
    /// # Errors
    ///
    /// A shape mismatch, a missing or mis-classed chunk, a migration hole, or
    /// whatever a device's `load` reports.
    pub fn load_with(&mut self, bytes: &[u8], migrations: &Migrations) -> Result<()> {
        let reader = StateReader::new(bytes)?;
        reader.check_shape(&self.shape)?;
        for entry in &self.devices {
            let chunk = reader.load(
                &entry.path,
                entry.class.name,
                entry.class.version,
                migrations,
            )?;
            let mut r = chunk.reader();
            entry.device.load(&mut r)?;
        }
        let clocks = reader.load(CLOCK_PATH, CLOCK_CLASS, MACHINE_STATE_VERSION, migrations)?;
        load_clocks(self.sched.forest_mut(), &mut clocks.reader())?;
        // After the clocks: the scheduler's restore republishes every lazily
        // advanced device's domain position, which is only right once the tick
        // counters those positions come from are back.
        let sched = reader.load(SCHED_PATH, SCHED_CLASS, MACHINE_STATE_VERSION, migrations)?;
        load_sched(&mut self.sched, &mut sched.reader())?;
        let wires = reader.load(WIRE_PATH, WIRE_CLASS, MACHINE_STATE_VERSION, migrations)?;
        load_wires(&self.nets, &mut wires.reader())?;
        self.deferred.drain();
        self.sweep();
        Ok(())
    }

    /// A hash of the machine's serialized state.
    ///
    /// The regression method of §0 in one call: run deterministically for N
    /// virtual units and compare this number. It is a hash of [`Machine::save`]
    /// output, which `core::state` guarantees is byte-identical for identical
    /// state, so equal hashes mean equal state and not merely equal-looking
    /// state.
    ///
    /// # Errors
    ///
    /// As [`Machine::save`].
    pub fn state_hash(&self) -> Result<u64> {
        Ok(fnv1a(&self.save()?))
    }
}

/// FNV-1a over the snapshot bytes.
///
/// Not a cryptographic hash and not meant to be: `purecrypto`'s BLAKE3 is the
/// integrity seam (§4.5), and this is a test and regression comparison that has
/// to work in a dependency-free `no_std` build.
fn fnv1a(bytes: &[u8]) -> u64 {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    for b in bytes {
        h ^= u64::from(*b);
        h = h.wrapping_mul(0x0000_0100_0000_01b3);
    }
    h
}

/// Write every oscillator's unit position and every domain's tick counter.
///
/// The tick counters are the authoritative architectural state (§4.2); the
/// global timeline is derived from them and is recomputed on restore.
///
/// Both sequences come from the forest's own enumeration, in creation order, so
/// the writer and the reader agree without either of them having to have kept a
/// list of handles.
fn save_clocks(forest: &ClockForest, sink: &mut impl Sink) -> Result<()> {
    let oscillators: Vec<_> = forest.oscillators().collect();
    sink.write_seq_len(oscillators.len() as u64)?;
    for osc in oscillators {
        sink.write_u64(forest.unit_position(osc)?)?;
    }
    let domains: Vec<_> = forest.domains().collect();
    sink.write_seq_len(domains.len() as u64)?;
    for id in domains {
        sink.write_u64(forest.ticks(id)?)?;
    }
    Ok(())
}

/// Restore what [`save_clocks`] wrote.
fn load_clocks<'a>(forest: &mut ClockForest, src: &mut impl Source<'a>) -> Result<()> {
    let domains: Vec<_> = forest.domains().collect();
    let count = src.read_seq_len(8)? as usize;
    let oscillators: Vec<_> = forest.oscillators().collect();
    if count != oscillators.len() {
        return Err(Error::State(format!(
            "snapshot has {count} oscillators, this machine has {}",
            oscillators.len()
        )));
    }
    // Unit positions first: §4.2's tick counters are anchored to them, so
    // restoring in the other order rebases every counter onto the old front.
    for osc in oscillators {
        forest.restore_unit_position(osc, src.read_u64()?)?;
    }
    let count = src.read_seq_len(8)? as usize;
    if count != domains.len() {
        return Err(Error::State(format!(
            "snapshot has {count} clock domains, this machine has {}",
            domains.len()
        )));
    }
    for id in domains {
        forest.restore_ticks(id, src.read_u64()?)?;
    }
    Ok(())
}

/// Write the scheduler's own architectural state (§4.5).
///
/// Virtual time, every pending event in fire order, the tie-break sequence
/// counter and the round-robin cursor. Re-deriving the queue by asking devices
/// to re-register would lose sub-tick phase — a timer 40 cycles from firing
/// would come back a whole period from firing — so the queue is written
/// verbatim.
fn save_sched(sched: &Scheduler, sink: &mut impl Sink) -> Result<()> {
    let snapshot = sched.snapshot();
    sink.write_u128(snapshot.now.raw())?;
    sink.write_u64(snapshot.next_seq)?;
    sink.write_u64(snapshot.cursor as u64)?;
    sink.write_seq_len(snapshot.events.len() as u64)?;
    for event in &snapshot.events {
        sink.write_u128(event.time.raw())?;
        sink.write_u64(event.id.seq())?;
        sink.write_u32(event.target.0)?;
        sink.write_u64(event.token)?;
    }
    Ok(())
}

/// Restore what [`save_sched`] wrote.
fn load_sched<'a>(sched: &mut Scheduler, src: &mut impl Source<'a>) -> Result<()> {
    let now = GlobalTime::from_raw(src.read_u128()?);
    let next_seq = src.read_u64()?;
    let cursor = usize::try_from(src.read_u64()?)
        .map_err(|_| Error::State(String::from("scheduler cursor does not fit this host")))?;
    // Sixteen bytes of instant, eight of sequence, four of target, eight of
    // token: an event cannot encode in fewer, so a corrupt count is caught
    // before anything is reserved.
    let count = src.read_seq_len(36)? as usize;
    let mut events = Vec::with_capacity(count.min(src.remaining()));
    for _ in 0..count {
        events.push(Event {
            time: GlobalTime::from_raw(src.read_u128()?),
            id: EventId::from_seq(src.read_u64()?),
            target: EventTarget(src.read_u32()?),
            token: src.read_u64()?,
        });
    }
    sched.restore(&SchedulerSnapshot {
        now,
        next_seq,
        cursor,
        events,
    })?;
    Ok(())
}

/// Write each net's per-source levels.
fn save_wires(nets: &[Net], sink: &mut impl Sink) -> Result<()> {
    sink.write_seq_len(nets.len() as u64)?;
    for net in nets {
        let levels = net.wire.snapshot();
        sink.write_seq_len(levels.len() as u64)?;
        for (id, level) in levels {
            sink.write_u64(id.raw())?;
            sink.write_u8(u8::from(level.is_high()))?;
        }
    }
    Ok(())
}

/// Restore what [`save_wires`] wrote.
fn load_wires<'a>(nets: &[Net], src: &mut impl Source<'a>) -> Result<()> {
    // A net encodes at least its own source count.
    let count = src.read_seq_len(8)? as usize;
    if count != nets.len() {
        return Err(Error::State(format!(
            "snapshot has {count} wire nets, this machine has {}",
            nets.len()
        )));
    }
    for net in nets {
        // Nine bytes per source: a `u64` id and a level byte.
        let sources = src.read_seq_len(9)? as usize;
        let mut levels = Vec::with_capacity(sources.min(src.remaining()));
        for _ in 0..sources {
            let id = WireId::new(src.read_u64()?);
            let level = match src.read_u8()? {
                0 => Level::Low,
                1 => Level::High,
                other => {
                    return Err(Error::State(format!("wire level {other} is not 0 or 1")));
                }
            };
            levels.push((id, level));
        }
        net.wire.restore(&levels);
    }
    // Restoring a level does not *deliver* it, and a sink's own fan-in is
    // derived state that nothing else rebuilds — so a re-announce is what
    // makes the sinks agree with the wires again. It has to come after every
    // net is restored: a sink that re-drives its output would otherwise write
    // over a net whose saved levels had not been put back yet.
    for net in nets {
        net.wire.refresh();
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_machine_chunk_paths_cannot_collide_with_a_device() {
        // Object names come from the resolver's identifier grammar, which has
        // no `/`. That is the whole reason the machine's own chunks are spelled
        // with one.
        assert!(CLOCK_PATH.starts_with('/'));
        assert!(WIRE_PATH.starts_with('/'));
    }

    #[test]
    fn the_state_hash_is_a_function_of_the_bytes() {
        assert_eq!(fnv1a(b"abc"), fnv1a(b"abc"));
        assert_ne!(fnv1a(b"abc"), fnv1a(b"abd"));
        // An empty snapshot still hashes to the FNV offset basis rather than 0,
        // so "no state" and "hash not computed" are distinguishable.
        assert_eq!(fnv1a(b""), 0xcbf2_9ce4_8422_2325);
    }
}