19#include <boost/dynamic_bitset.hpp>
32#include <unordered_map>
38using namespace SILICON::core;
39using namespace SILICON::waveform;
40using namespace SILICON::waveform::fst;
77 using TraceSink = std::function<void(uint64_t,
const std::vector<BusValue>&)>;
124 explicit Simulator(std::shared_ptr<const Circuit> c, uint64_t initialSimulationTime = 0,
125 std::unique_ptr<CircuitWriter>
fstWriter =
nullptr,
166 std::span<const WaveformInputDriver> inputDrivers,
182 void setFstWriter(std::unique_ptr<CircuitWriter> writer);
190 void setTraceBuses(std::vector<CircuitWriter::NamedBus> buses);
308 std::unordered_map<uint64_t, std::size_t>
indices;
341 std::priority_queue<TimedEvent, std::vector<TimedEvent>, std::greater<>>
eventQueue;
344 std::unordered_map<PendingTransitionKey, PendingTransition, PendingTransitionKeyHash>
397 std::unordered_map<uint64_t, State>& previousWireStates);
401 std::unordered_map<uint64_t, State> previousWireStates,
403 bool enableSequentialStaging =
true);
407 [[nodiscard]]
static std::vector<ExecutionStep>
415 const Context& context,
419 const Context& context,
427 std::span<const WaveformInputDriver> inputDrivers,
437 const Context& context,
std::pair< uint64_t, std::uintptr_t > PendingTransitionKey
std::vector< ExecutionStep > executionPlan
Compiled component/SCC execution plan in topological order.
StagedTransitions stagedSequentialTransitions
Zero-delay sequential writes waiting for the active pass to finish.
std::unordered_map< uint64_t, ExecutionStepSet > forwardExecutionStepsByWire
Cached downstream execution-step membership for each input wire ID.
bool shouldStageSequentialOutput(const Component_weakPtr &source) const
RunResult processNextEventBatch(const CancellationCheck &isCancelled)
boost::dynamic_bitset<> ExecutionStepSet
void emitTraceSnapshot()
Emits a waveform snapshot when tracing is enabled.
std::priority_queue< TimedEvent, std::vector< TimedEvent >, std::greater<> > eventQueue
Priority queue of timed events sorted by time.
static uint64_t getMaxSimulationSteps()
void setFstWriter(std::unique_ptr< CircuitWriter > writer)
Installs a caller-created FST writer.
static EdgeType edgeType(const Context &context, const Wire_ptr &wire)
Classifies one wire's transition in a simulation context.
bool commitStagedCyclicTransitions(CyclicStaging &staging)
void discardStagedCyclicTransitions(CyclicStaging &staging)
EdgeType
Logical edge observed for one wire during a reactive evaluation.
RunResult evaluateForwardConeAndTrace(std::span< const Bus > changedBuses, std::unordered_map< uint64_t, State > previousWireStates, const CancellationCheck &isCancelled={}, bool enableSequentialStaging=true)
static std::vector< ExecutionStep > compileExecutionPlan(std::span< const Circuit::SimulationBlock > blocks)
std::unordered_map< uint64_t, State > * activePreviousWireStates
Previous-state index being populated during the active reactive pass.
uint64_t getCurrentTime() const
Gets the current simulation time.
void scheduleDelayedWireUpdate(const Wire_ptr &target, State newState, uint64_t delay, const Component_weakPtr &source)
void stageCyclicWireUpdate(const Wire_ptr &target, State newState, const Component_weakPtr &source)
RunResult applyWaveformInputSample(std::span< const BusValue > values, std::span< const WaveformInputDriver > inputDrivers, 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 int maxTransitionsPerDeltaCycle
std::unordered_map< PendingTransitionKey, PendingTransition, PendingTransitionKeyHash > pendingTransitions
Latest valid delayed transition for each component-output wire.
RunResult setBus(Bus bus, BusValue value, CancellationCheck isCancelled={})
Forces a bus to a specific value and propagates the change through the circuit.
std::vector< Bus > commitStagedSequentialTransitions(std::unordered_map< uint64_t, State > &previousWireStates)
RunResult run(uint64_t duration, CancellationCheck isCancelled={})
Executes the simulation for a specified duration.
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.
TraceSink traceSink
Optional callback for live waveform viewers.
void stageSequentialWireUpdate(const Wire_ptr &target, State newState, const Component_weakPtr &source)
RunResult runUntilIdle(CancellationCheck isCancelled={})
Runs pending timed events until no further propagation is scheduled.
std::function< bool()> CancellationCheck
Framework-independent cooperative cancellation callback.
static void setMaxSimulationSteps(uint64_t value)
RunResult simulateBus(const Bus &bus, CancellationCheck isCancelled={})
Propagates changes from a bus through the circuit in reverse direction.
static void setMaxTransitionsPerDeltaCycle(int value)
void enableFstTracing(std::string_view fileName, CircuitWriter::Options options={})
Enables FST tracing for this simulator.
std::unique_ptr< CircuitWriter > fstWriter
Optional waveform writer for simulation tracing.
std::unordered_map< PendingTransitionKey, StagedWireTransition, PendingTransitionKeyHash > StagedTransitions
void setTraceBuses(std::vector< CircuitWriter::NamedBus > buses)
Configures bus-level waveform snapshots emitted by the simulator.
bool evaluateExecutionSteps(const ExecutionStepSet &steps, const Context &context, const CancellationCheck &isCancelled={})
std::vector< CircuitWriter::NamedBus > traceBuses
Bus list observed by waveform tracing.
ExecutionStepSet getForwardExecutionSteps(std::span< const Bus > changedBuses) const
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.
CyclicStaging * activeCyclicStaging
Active SCC's reusable staging buffer; null outside a cyclic step.
void setTraceSink(TraceSink sink)
bool evaluateExecutionPlan(std::span< const ExecutionStep > steps, const Context &context, const CancellationCheck &isCancelled={})
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.
static uint64_t maxSimulationSteps
std::function< void(uint64_t, const std::vector< BusValue > &)> TraceSink
Receives encoded waveform values for a simulation timestamp.
bool isDelayedEventPending(const TimedEvent &event) const
void initializeExecutionPlan()
Builds execution and reachability caches once, during construction.
bool stageSequentialOutputs
True while reactive evaluation should stage opted-in zero-delay writes.
static int getMaxTransitionsPerDeltaCycle()
RunResult
Outcome of a cancellable simulator operation.
uint64_t currentTime
Current simulation time.
std::shared_ptr< const Circuit > circuit
The circuit being simulated.
static PendingTransitionKey pendingTransitionKey(const Wire_ptr &target, const Component_weakPtr &source)
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.
bool evaluateExecutionStep(const ExecutionStep &step, const Context &context, const CancellationCheck &isCancelled={})
std::shared_ptr< Wire > Wire_ptr
std::weak_ptr< Component > Component_weakPtr
std::vector< State > BusValue
Context describing why a component is being evaluated.
std::vector< std::size_t > dirtyWires
std::vector< Wire_ptr > wires
std::vector< StagedCyclicWire > writes
std::unordered_map< uint64_t, std::size_t > indices
std::unique_ptr< CyclicStaging > cyclicStaging
ExecutionStep(bool cyclic, std::vector< Component_ptr > orderedComponents)
std::vector< Component_ptr > components
std::size_t operator()(const PendingTransitionKey &key) const noexcept
Component_weakPtr conflictingSource
Represents a timed event in the simulation.
Component_weakPtr source
The component that is the source of this state change.
uint64_t time
Simulation time at which the event occurs.
bool operator>(const TimedEvent &other) const
Comparison operator for priority queue ordering.
State newState
The new state to set on the wire.
Wire_ptr targetWire
The wire to update.