14 ic.m_sortedInternalElements.clear();
15 ic.m_boundaryInputElements.clear();
16 ic.m_internalHasFeedback =
false;
18 if (ic.m_internalElements.isEmpty()) {
23 for (
auto *elm : std::as_const(ic.m_internalElements)) {
24 elm->initSimulationVectors(elm->inputSize(), elm->outputSize());
32 for (
auto *elm : std::as_const(ic.m_internalElements)) {
33 if (elm->elementType() == ElementType::IC) {
39 for (
auto *port : std::as_const(ic.m_internalInputs)) {
40 if (
auto *elm = port->graphicElement()) {
41 ic.m_boundaryInputElements.insert(elm);
49 ic.m_internalHasFeedback = !result.feedbackNodes.isEmpty();
53 ic.m_sortedInternalElements = result.sorted;
54 ic.m_sortedInternalElements.erase(
55 std::remove_if(ic.m_sortedInternalElements.begin(), ic.m_sortedInternalElements.end(),
56 [&ic](
auto *elm) { return ic.m_boundaryInputElements.contains(elm); }),
57 ic.m_sortedInternalElements.end());
60void ICSimulation::pushInputsToBoundary(
IC &ic)
62 for (
int i = 0; i < ic.
inputSize() && i < ic.m_internalInputs.size(); ++i) {
63 auto *boundaryElement = ic.m_internalInputs.at(i)->graphicElement();
64 if (boundaryElement) {
65 boundaryElement->setOutputValue(0, ic.
simInputs().at(i));
70void ICSimulation::pullOutputsFromBoundary(
IC &ic)
72 for (
int i = 0; i < ic.
outputSize() && i < ic.m_internalOutputs.size(); ++i) {
73 auto *boundaryElement = ic.m_internalOutputs.at(i)->graphicElement();
74 if (boundaryElement) {
82 if (ic.m_sortedInternalElements.isEmpty()) {
91 pushInputsToBoundary(ic);
95 if (ic.m_internalHasFeedback) {
98 for (
auto *element : std::as_const(ic.m_sortedInternalElements)) {
100 element->updateLogic();
105 pullOutputsFromBoundary(ic);
110 if (ic.m_sortedInternalElements.isEmpty()) {
118 pushInputsToBoundary(ic);
129 for (
int pass = 0; pass < maxPasses; ++pass) {
130 bool changed =
false;
131 for (
auto *element : std::as_const(ic.m_sortedInternalElements)) {
132 if (element && element->elementGroup() != ElementGroup::Memory) {
133 element->clearOutputChanged();
134 element->resettleCombinational();
135 changed = changed || element->outputChanged();
143 pullOutputsFromBoundary(ic);
ICSimulation: builds and runs an IC's internal simulation graph.
Integrated Circuit (IC) graphic element that encapsulates a sub-circuit file.
Port classes: Port (base), InputPort, and OutputPort.
Synchronous cycle-based simulation engine with event-driven clock support.
const QVector< Status > & simInputs() const
Read-only view of the cached simulation input values.
int inputSize() const
Returns the current number of input ports.
bool simUpdateInputsAllowUnknown()
Like simUpdateInputs(), but allows Unknown/Error values through.
void setOutputValue(const int index, const Status value)
Sets simulation output port index to value.
int outputSize() const
Returns the current number of output ports.
static void update(IC &ic)
Simulates ic's internal circuit and propagates results to its boundary.
static void resettle(IC &ic)
static void initialize(IC &ic)
Builds the internal simulation graph (connection graph + topological sort) for ic.
Graphic element representing an Integrated Circuit (sub-circuit) box.
static SortResult topologicalSort(const QVector< GraphicElement * > &elements, const QHash< GraphicElement *, QVector< GraphicElement * > > &successors)
Topologically sorts elements using the successor graph, detects feedback loops.
static bool iterativeSettle(const QVector< GraphicElement * > &elements, int maxIterations=kMaxSettleIterations)
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 buildConnectionGraph(const QVector< GraphicElement * > &elements)
static void connectWirelessElements(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 constexpr int kMaxSettleIterations