|
wiRedPanda
Logic Circuit Simulator
|
Manages the digital circuit simulation loop. More...
#include <Simulation.h>
Classes | |
| struct | SortResult |
| Result of topological sort with feedback detection. More... | |
Public Member Functions | |
| bool | initialize () |
| Builds the simulation graph from the current scene elements. | |
| bool | isInFeedbackLoop (const GraphicElement *element) const |
Returns true if element is part of a combinational feedback loop. | |
| bool | isRunning () |
Returns true if the simulation timer is currently running. | |
| bool | isUserMuted () const |
Returns true if the user has explicitly muted audio. | |
| void | restart () |
| void | setUserMuted (bool muted) |
| Sets whether the user has explicitly muted audio; persists across stop/start cycles. | |
| void | setVisualThrottleEnabled (bool enabled) |
| Simulation (SimulationHost *host, QObject *parent=nullptr) | |
| Constructs a Simulation bound to host. | |
| void | simulationWarning (const QString &message) |
| Emitted (at most once per initialize()) when a feedback circuit fails to converge. | |
| void | start () |
| Starts the 1 ms simulation timer. | |
| void | stop () |
| Stops the simulation timer. | |
| void | update () |
| Executes one simulation step (used by tests to advance the simulation manually). | |
| ~Simulation () override=default | |
| Destructor; stops the simulation timer. | |
Static Public Member Functions | |
| static void | buildConnectionGraph (const QVector< GraphicElement * > &elements) |
| static QHash< GraphicElement *, QVector< GraphicElement * > > | buildSuccessorGraph (const QVector< GraphicElement * > &elements, const QHash< QString, GraphicElement * > &txMap) |
| Builds a successor adjacency list from connection graph + wireless Tx→Rx edges. | |
| static QHash< QString, GraphicElement * > | buildTxMap (const QVector< GraphicElement * > &elements) |
| Builds a label→element map for wireless Tx nodes. First Tx per label wins. | |
| static void | connectWirelessElements (const QVector< GraphicElement * > &elements) |
| static bool | iterativeSettle (const QVector< GraphicElement * > &elements, int maxIterations=kMaxSettleIterations) |
| static SortResult | topologicalSort (const QVector< GraphicElement * > &elements, const QHash< GraphicElement *, QVector< GraphicElement * > > &successors) |
| Topologically sorts elements using the successor graph, detects feedback loops. | |
Static Public Attributes | |
| static constexpr int | kMaxSettleIterations = 10 |
Friends | |
| class | TestDanglingPointer |
Manages the digital circuit simulation loop.
The simulation runs a 1 ms periodic QTimer. On each tick it:
Feedback loops are detected during initialization and handled with an iterative settling algorithm.
Definition at line 37 of file Simulation.h.
|
explicit |
Constructs a Simulation bound to host.
| host | Scene (via the narrow SimulationHost interface) whose elements will be simulated. |
| parent | QObject parent (the same Scene as host, passed separately since SimulationHost is not itself a QObject). |
Definition at line 24 of file Simulation.cpp.
References update().
Referenced by simulationWarning().
|
overridedefault |
Destructor; stops the simulation timer.
|
static |
Definition at line 490 of file Simulation.cpp.
Referenced by ICSimulation::initialize().
|
static |
Builds a successor adjacency list from connection graph + wireless Tx→Rx edges.
Definition at line 543 of file Simulation.cpp.
Referenced by ICSimulation::initialize().
|
static |
Builds a label→element map for wireless Tx nodes. First Tx per label wins.
Definition at line 530 of file Simulation.cpp.
Referenced by connectWirelessElements(), and ICSimulation::initialize().
|
static |
Overrides physical predecessors on Rx nodes with their matching Tx node. Must be called after buildConnectionGraph() so wireless always wins, and before sort().
Definition at line 513 of file Simulation.cpp.
References buildTxMap().
Referenced by ICSimulation::initialize().
| bool Simulation::initialize | ( | ) |
Builds the simulation graph from the current scene elements.
true if initialization succeeded (all elements are valid). Definition at line 377 of file Simulation.cpp.
| bool Simulation::isInFeedbackLoop | ( | const GraphicElement * | element | ) | const |
Returns true if element is part of a combinational feedback loop.
Definition at line 316 of file Simulation.cpp.
| bool Simulation::isRunning | ( | ) |
Returns true if the simulation timer is currently running.
Definition at line 311 of file Simulation.cpp.
| bool Simulation::isUserMuted | ( | ) | const |
Returns true if the user has explicitly muted audio.
Definition at line 361 of file Simulation.cpp.
|
static |
Runs updateLogic() iteratively on elements until outputs converge or maxIterations is reached.
true if the circuit converged. Definition at line 605 of file Simulation.cpp.
Referenced by ICSimulation::update().
| void Simulation::restart | ( | ) |
Invalidates the cached simulation topology: clears m_initialized and the hot-path element/connection vectors so no stale reference can be dereferenced on subsequent ticks. The next update() call re-runs initialize(). The QTimer's run state (running/stopped) is preserved — callers who also want to pause should use SimulationBlocker.
Definition at line 285 of file Simulation.cpp.
| void Simulation::setUserMuted | ( | bool | muted | ) |
Sets whether the user has explicitly muted audio; persists across stop/start cycles.
Definition at line 353 of file Simulation.cpp.
| void Simulation::setVisualThrottleEnabled | ( | bool | enabled | ) |
Enables or disables the visual refresh throttle. When disabled, phases 3–4 run on every update() call regardless of tick count.
Definition at line 43 of file Simulation.cpp.
| void Simulation::simulationWarning | ( | const QString & | message | ) |
Emitted (at most once per initialize()) when a feedback circuit fails to converge.
References Simulation().
Referenced by SceneUiBinder::bind(), and SceneUiBinder::unbind().
| void Simulation::start | ( | ) |
Starts the 1 ms simulation timer.
Definition at line 329 of file Simulation.cpp.
References initialize(), and qCDebug.
| void Simulation::stop | ( | ) |
Stops the simulation timer.
Definition at line 321 of file Simulation.cpp.
|
static |
Topologically sorts elements using the successor graph, detects feedback loops.
Definition at line 585 of file Simulation.cpp.
References calculatePriorities(), Simulation::SortResult::feedbackNodes, findFeedbackNodes(), Simulation::SortResult::priorities, and Simulation::SortResult::sorted.
Referenced by ICSimulation::initialize().
| void Simulation::update | ( | ) |
Executes one simulation step (used by tests to advance the simulation manually).
Definition at line 51 of file Simulation.cpp.
References initialize(), Application::interactiveMode, and kMaxSettleIterations.
Referenced by Simulation().
|
friend |
Definition at line 41 of file Simulation.h.
References TestDanglingPointer.
Referenced by TestDanglingPointer.
|
staticconstexpr |
Maximum iteration count for post-commit combinational re-settle passes. Matches iterativeSettle()'s default and the "did not converge" warning threshold.
Definition at line 46 of file Simulation.h.
Referenced by ArduinoCodeGen::generateTestbench(), ICSimulation::resettle(), and update().