|
SILICON
|
Executes event-driven simulation of a Circuit. More...
#include <simulator.hpp>
Classes | |
| struct | WaveformInputDriver |
| Bus driven by one column of an input waveform. More... | |
| struct | PendingTransitionKeyHash |
| struct | PendingTransition |
| struct | StagedWireTransition |
| struct | StagedCyclicWire |
| struct | CyclicStaging |
| struct | ExecutionStep |
| class | EvaluationStateGuard |
Public Types | |
| enum class | RunResult { Completed , Cancelled , StepLimitReached } |
| Outcome of a cancellable simulator operation. More... | |
| enum class | EdgeType { RISE , FALL , UNKNOWN , NO_CHANGE } |
| Logical edge observed for one wire during a reactive evaluation. More... | |
| using | TraceSink = std::function<void(uint64_t, const std::vector<BusValue>&)> |
| Receives encoded waveform values for a simulation timestamp. | |
| using | CancellationCheck = std::function<bool()> |
| Framework-independent cooperative cancellation callback. | |
Public Member Functions | |
| Simulator (std::shared_ptr< const Circuit > c, uint64_t initialSimulationTime=0, std::unique_ptr< CircuitWriter > fstWriter=nullptr, CancellationCheck isCancelled={}) | |
| Constructs a Simulator for the given circuit. | |
| RunResult | run (uint64_t duration, CancellationCheck isCancelled={}) |
| Executes the simulation for a specified duration. | |
| RunResult | runUntilIdle (CancellationCheck isCancelled={}) |
| Runs pending timed events until no further propagation is scheduled. | |
| RunResult | simulateWaveform (uint64_t duration, std::span< const Sample > inputSnapshots, std::span< const WaveformInputDriver > inputDrivers, CancellationCheck isCancelled={}) |
| Drives input buses from a sampled internal waveform and runs to duration. | |
| void | enableFstTracing (std::string_view fileName, CircuitWriter::Options options={}) |
| Enables FST tracing for this simulator. | |
| void | setFstWriter (std::unique_ptr< CircuitWriter > writer) |
| Installs a caller-created FST writer. | |
| void | setTraceBuses (std::vector< CircuitWriter::NamedBus > buses) |
| Configures bus-level waveform snapshots emitted by the simulator. | |
| void | setTraceSink (TraceSink sink) |
| RunResult | setBus (Bus bus, BusValue value, CancellationCheck isCancelled={}) |
| Forces a bus to a specific value and propagates the change through the circuit. | |
| RunResult | setBus (Bus bus, BusValue value, const Component_weakPtr &source, CancellationCheck isCancelled={}) |
| Forces a bus to a specific value with a source component and propagates the change through the circuit. | |
| RunResult | simulateBus (const Bus &bus, CancellationCheck isCancelled={}) |
| Propagates changes from a bus through the circuit in reverse direction. | |
| void | updateWire (const Wire_ptr &target, State newState, uint64_t delay, const Component_weakPtr &source) |
| Updates a wire with a new state and optional delay. | |
| void | updateBus (const Bus &bus, const BusValue &value, uint64_t delay, const Component_weakPtr &source) |
| Updates a bus to a new value through the normal wire-update path. | |
| uint64_t | getCurrentTime () const |
| Gets the current simulation time. | |
Static Public Member Functions | |
| static EdgeType | edgeType (const Context &context, const Wire_ptr &wire) |
| Classifies one wire's transition in a simulation context. | |
| static void | setMaxSimulationSteps (uint64_t value) |
| static void | setMaxTransitionsPerDeltaCycle (int value) |
| static uint64_t | getMaxSimulationSteps () |
| static int | getMaxTransitionsPerDeltaCycle () |
Private Types | |
| using | ExecutionStepSet = boost::dynamic_bitset<> |
| using | PendingTransitionKey = std::pair<uint64_t, std::uintptr_t> |
| using | StagedTransitions |
Private Member Functions | |
| void | initializeExecutionPlan () |
| Builds execution and reachability caches once, during construction. | |
| void | emitTraceSnapshot () |
| Emits a waveform snapshot when tracing is enabled. | |
| void | scheduleDelayedWireUpdate (const Wire_ptr &target, State newState, uint64_t delay, const Component_weakPtr &source) |
| bool | shouldStageSequentialOutput (const Component_weakPtr &source) const |
| void | stageSequentialWireUpdate (const Wire_ptr &target, State newState, const Component_weakPtr &source) |
| void | stageCyclicWireUpdate (const Wire_ptr &target, State newState, const Component_weakPtr &source) |
| bool | commitStagedCyclicTransitions (CyclicStaging &staging) |
| void | discardStagedCyclicTransitions (CyclicStaging &staging) |
| std::vector< Bus > | commitStagedSequentialTransitions (std::unordered_map< uint64_t, State > &previousWireStates) |
| RunResult | evaluateForwardConeAndTrace (std::span< const Bus > changedBuses, std::unordered_map< uint64_t, State > previousWireStates, const CancellationCheck &isCancelled={}, bool enableSequentialStaging=true) |
| bool | isDelayedEventPending (const TimedEvent &event) const |
| bool | evaluateExecutionStep (const ExecutionStep &step, const Context &context, const CancellationCheck &isCancelled={}) |
| bool | evaluateExecutionPlan (std::span< const ExecutionStep > steps, const Context &context, const CancellationCheck &isCancelled={}) |
| bool | evaluateExecutionSteps (const ExecutionStepSet &steps, const Context &context, const CancellationCheck &isCancelled={}) |
| ExecutionStepSet | getForwardExecutionSteps (std::span< const Bus > changedBuses) const |
| RunResult | applyWaveformInputSample (std::span< const BusValue > values, std::span< const WaveformInputDriver > inputDrivers, const CancellationCheck &isCancelled) |
| RunResult | processNextEventBatch (const CancellationCheck &isCancelled) |
| RunResult | evaluateExecutionPlanAndTrace (std::span< const ExecutionStep > steps, const Context &context, const CancellationCheck &isCancelled={}) |
| Evaluates a plan and emits a trace snapshot on success. | |
Static Private Member Functions | |
| static PendingTransitionKey | pendingTransitionKey (const Wire_ptr &target, const Component_weakPtr &source) |
| static std::vector< ExecutionStep > | compileExecutionPlan (std::span< const Circuit::SimulationBlock > blocks) |
Private Attributes | |
| std::shared_ptr< const Circuit > | circuit |
| The circuit being simulated. | |
| std::vector< ExecutionStep > | executionPlan |
| Compiled component/SCC execution plan in topological order. | |
| std::unordered_map< uint64_t, ExecutionStepSet > | forwardExecutionStepsByWire |
| Cached downstream execution-step membership for each input wire ID. | |
| std::priority_queue< TimedEvent, std::vector< TimedEvent >, std::greater<> > | eventQueue |
| Priority queue of timed events sorted by time. | |
| std::unordered_map< PendingTransitionKey, PendingTransition, PendingTransitionKeyHash > | pendingTransitions |
| Latest valid delayed transition for each component-output wire. | |
| StagedTransitions | stagedSequentialTransitions |
| Zero-delay sequential writes waiting for the active pass to finish. | |
| CyclicStaging * | activeCyclicStaging = nullptr |
| Active SCC's reusable staging buffer; null outside a cyclic step. | |
| uint64_t | currentTime = 0 |
| Current simulation time. | |
| std::unique_ptr< CircuitWriter > | fstWriter |
| Optional waveform writer for simulation tracing. | |
| std::vector< CircuitWriter::NamedBus > | traceBuses |
| Bus list observed by waveform tracing. | |
| TraceSink | traceSink |
| Optional callback for live waveform viewers. | |
| std::unordered_map< uint64_t, State > * | activePreviousWireStates = nullptr |
| Previous-state index being populated during the active reactive pass. | |
| bool | stageSequentialOutputs = false |
| True while reactive evaluation should stage opted-in zero-delay writes. | |
Static Private Attributes | |
| static uint64_t | maxSimulationSteps = 100000 |
| static int | maxTransitionsPerDeltaCycle = 1000 |
Executes event-driven simulation of a Circuit.
The Simulator manages the execution of a digital circuit using an event-driven simulation paradigm. It handles both acyclic circuit parts (executed once in topological order) and cyclic parts (executed iteratively using delta cycles until convergence).
Definition at line 74 of file simulator.hpp.
| using CancellationCheck = std::function<bool()> |
Framework-independent cooperative cancellation callback.
Definition at line 89 of file simulator.hpp.
|
private |
Definition at line 274 of file simulator.hpp.
|
private |
Definition at line 276 of file simulator.hpp.
|
private |
Definition at line 293 of file simulator.hpp.
Receives encoded waveform values for a simulation timestamp.
Definition at line 77 of file simulator.hpp.
|
strong |
Logical edge observed for one wire during a reactive evaluation.
| Enumerator | |
|---|---|
| RISE | LOW -> HIGH |
| FALL | HIGH -> LOW |
| UNKNOWN | A changed transition involving UNKNOWN or ERROR state |
| NO_CHANGE | No captured change for this evaluation |
Definition at line 99 of file simulator.hpp.
|
strong |
Outcome of a cancellable simulator operation.
| Enumerator | |
|---|---|
| Completed | The requested operation completed normally |
| Cancelled | The caller requested cancellation |
| StepLimitReached | The configured simulation step limit was reached |
Definition at line 92 of file simulator.hpp.
|
explicit |
Constructs a Simulator for the given circuit.
| c | The circuit to simulate |
| initialSimulationTime | If != 0 also simulate the circuit using initialSimulationTime as duration |
| fstWriter | Optional waveform writer used to trace simulation snapshots |
| isCancelled | Optional cancellation callback used during initial evaluation |
| std::invalid_argument | If c is null |
Definition at line 144 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 852 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 427 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 458 of file simulator.cpp.
|
staticnodiscardprivate |
Definition at line 490 of file simulator.cpp.
|
private |
Definition at line 415 of file simulator.cpp.
|
staticnodiscard |
Classifies one wire's transition in a simulation context.
| context | Reactive evaluation context with previous wire states |
| wire | Wire to inspect |
Definition at line 125 of file simulator.cpp.
|
private |
Emits a waveform snapshot when tracing is enabled.
Definition at line 297 of file simulator.cpp.
| void enableFstTracing | ( | std::string_view | fileName, |
| CircuitWriter::Options | options = {} ) |
Enables FST tracing for this simulator.
The writer is initialized from the simulator's circuit and immediately receives a snapshot for the current simulation time.
Definition at line 268 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 582 of file simulator.cpp.
|
nodiscardprivate |
Evaluates a plan and emits a trace snapshot on success.
Definition at line 680 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 536 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 593 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 691 of file simulator.cpp.
|
inlinenodiscard |
Gets the current simulation time.
Definition at line 265 of file simulator.hpp.
|
nodiscardprivate |
Definition at line 610 of file simulator.cpp.
|
staticnodiscard |
Definition at line 893 of file simulator.cpp.
|
staticnodiscard |
Definition at line 898 of file simulator.cpp.
|
private |
Builds execution and reachability caches once, during construction.
Definition at line 164 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 478 of file simulator.cpp.
|
staticnodiscardprivate |
Definition at line 316 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 784 of file simulator.cpp.
| Simulator::RunResult run | ( | uint64_t | duration, |
| CancellationCheck | isCancelled = {} ) |
Executes the simulation for a specified duration.
Uses an event-driven simulation paradigm with a priority queue sorted by time. Processes events in chronological order, re-evaluating blocks when wire states change.
| duration | The simulation duration in time units |
| isCancelled | Optional cooperative cancellation callback |
Definition at line 733 of file simulator.cpp.
| Simulator::RunResult runUntilIdle | ( | CancellationCheck | isCancelled = {} | ) |
Runs pending timed events until no further propagation is scheduled.
Unlike run(duration), this is not bounded by a caller-provided time window. It is intended for interactive settling after direct input changes, where the full propagation delay depends on the circuit. Unstable circuits are still bounded by maxSimulationSteps.
| isCancelled | Optional cooperative cancellation callback |
Definition at line 761 of file simulator.cpp.
|
private |
Definition at line 322 of file simulator.cpp.
| Simulator::RunResult setBus | ( | Bus | bus, |
| BusValue | value, | ||
| CancellationCheck | isCancelled = {} ) |
Forces a bus to a specific value and propagates the change through the circuit.
Optimized for pass-by-value buses (copies the bus). Evaluates the cached forward execution steps affected by the bus.
| bus | The bus to set |
| value | The value to set |
| isCancelled | Optional cooperative cancellation callback |
Definition at line 903 of file simulator.cpp.
| Simulator::RunResult setBus | ( | Bus | bus, |
| BusValue | value, | ||
| const Component_weakPtr & | source, | ||
| CancellationCheck | isCancelled = {} ) |
Forces a bus to a specific value with a source component and propagates the change through the circuit.
Tracks the source component that authorized this state change for proper event propagation. Evaluates the cached forward execution steps affected by the bus.
| bus | The bus to set |
| value | The value to set |
| source | The component that authorized this change |
| isCancelled | Optional cooperative cancellation callback |
Definition at line 909 of file simulator.cpp.
| void setFstWriter | ( | std::unique_ptr< CircuitWriter > | writer | ) |
Installs a caller-created FST writer.
Ownership transfers to the simulator. Passing nullptr disables tracing.
Definition at line 279 of file simulator.cpp.
|
static |
Definition at line 883 of file simulator.cpp.
|
static |
Definition at line 888 of file simulator.cpp.
| void setTraceBuses | ( | std::vector< CircuitWriter::NamedBus > | buses | ) |
Configures bus-level waveform snapshots emitted by the simulator.
The same registered buses are used for UI waveform snapshots and optional FST export, ensuring both observe exactly the same simulation timing.
Definition at line 285 of file simulator.cpp.
| void setTraceSink | ( | TraceSink | sink | ) |
Definition at line 291 of file simulator.cpp.
|
nodiscardprivate |
Definition at line 343 of file simulator.cpp.
| Simulator::RunResult simulateBus | ( | const Bus & | bus, |
| CancellationCheck | isCancelled = {} ) |
Propagates changes from a bus through the circuit in reverse direction.
Used when the bus value has changed externally and needs to update upstream components. Extracts backwards subgraph and evaluates each block.
| bus | The bus that changed |
| isCancelled | Optional cooperative cancellation callback |
Definition at line 935 of file simulator.cpp.
| Simulator::RunResult simulateWaveform | ( | uint64_t | duration, |
| std::span< const Sample > | inputSnapshots, | ||
| std::span< const WaveformInputDriver > | inputDrivers, | ||
| CancellationCheck | isCancelled = {} ) |
Drives input buses from a sampled internal waveform and runs to duration.
Snapshot values are matched to input drivers by index. Missing values and extra values are ignored, mirroring the viewer's input-only waveform behavior.
| duration | Total simulation duration in time units |
| inputSnapshots | Timestamped input values ordered like inputDrivers |
| inputDrivers | Ordered buses to drive from each snapshot |
| isCancelled | Optional cooperative cancellation callback |
Definition at line 817 of file simulator.cpp.
|
private |
Definition at line 364 of file simulator.cpp.
|
private |
Definition at line 350 of file simulator.cpp.
| void updateBus | ( | const Bus & | bus, |
| const BusValue & | value, | ||
| uint64_t | delay, | ||
| const Component_weakPtr & | source ) |
Updates a bus to a new value through the normal wire-update path.
Normalizes value to the bus width, then delegates each bit to updateWire() so immediate and delayed updates share the usual simulator bookkeeping.
| bus | The bus to update |
| value | The new value |
| delay | |
| source | The component that authorized this change |
Definition at line 665 of file simulator.cpp.
| void updateWire | ( | const Wire_ptr & | target, |
| State | newState, | ||
| uint64_t | delay, | ||
| const Component_weakPtr & | source ) |
Updates a wire with a new state and optional delay.
| target | The wire to update |
| newState | The new state |
| delay | Delay in time units (0 for immediate) |
| source | The component that is the source of this change |
Definition at line 633 of file simulator.cpp.
|
private |
Active SCC's reusable staging buffer; null outside a cyclic step.
Definition at line 351 of file simulator.hpp.
Previous-state index being populated during the active reactive pass.
Definition at line 366 of file simulator.hpp.
|
private |
The circuit being simulated.
Definition at line 329 of file simulator.hpp.
|
private |
Current simulation time.
Definition at line 354 of file simulator.hpp.
|
private |
Priority queue of timed events sorted by time.
Definition at line 341 of file simulator.hpp.
|
private |
Compiled component/SCC execution plan in topological order.
Definition at line 335 of file simulator.hpp.
|
private |
Cached downstream execution-step membership for each input wire ID.
Definition at line 338 of file simulator.hpp.
|
private |
Optional waveform writer for simulation tracing.
Definition at line 357 of file simulator.hpp.
|
staticprivate |
Definition at line 371 of file simulator.hpp.
|
staticprivate |
Definition at line 372 of file simulator.hpp.
|
private |
Latest valid delayed transition for each component-output wire.
Definition at line 345 of file simulator.hpp.
|
private |
Zero-delay sequential writes waiting for the active pass to finish.
Definition at line 348 of file simulator.hpp.
|
private |
True while reactive evaluation should stage opted-in zero-delay writes.
Definition at line 369 of file simulator.hpp.
|
private |
Bus list observed by waveform tracing.
Definition at line 360 of file simulator.hpp.
|
private |
Optional callback for live waveform viewers.
Definition at line 363 of file simulator.hpp.