wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
ElementSimState.h
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
7
8#pragma once
9
10#include <algorithm>
11
12#include <QVector>
13
14#include "App/Core/Enums.h"
15
16class GraphicElement;
17class InputPort;
18class OutputPort;
19
31{
32public:
38
40 void initVectors(int inputCount, int outputCount, const QVector<OutputPort *> &outputPorts);
41
43 void reset(const QVector<OutputPort *> &outputPorts);
44
46 void connectPredecessor(int inputIndex, GraphicElement *source, int outputPort);
47
57 bool updateInputs(bool allowUnknown, const QVector<InputPort *> &inputPorts);
58
60 int decodeSelectValue(int offset, int count) const;
61
63 Status outputValue(const int index = 0) const
64 {
65 if (index >= m_outputs.size()) { return Status::Unknown; }
66 return m_outputs.at(index);
67 }
68
70 void setOutputValue(const int index, const Status value)
71 {
72 if (index >= m_outputs.size()) { return; }
73 if (m_deferCommit) {
74 // Synchronous sequential element mid-tick: stage the value so peers
75 // still read the old output. commitDeferredOutputs() publishes it.
76 m_staged[index] = value;
77 return;
78 }
79 if (m_outputs[index] != value) { m_outputChanged = true; }
80 m_outputs[index] = value;
81 }
82
84 qsizetype outputSize() const { return m_outputs.size(); }
85
87 bool outputChanged() const { return m_outputChanged; }
88
90 void clearOutputChanged() { m_outputChanged = false; }
91
93 const QVector<Status> &inputs() const { return m_inputs; }
94
99 const QVector<Status> &outputs() const { return m_deferCommit ? m_staged : m_outputs; }
100
104 {
105 // Element-wise copy into persistent storage, deliberately NOT `m_staged =
106 // m_outputs`: assignment CoW-shares the two buffers, so every staged write and
107 // every commit write into m_outputs pays a detach (malloc + deep copy) -- per
108 // sequential element, per simulation tick, even when nothing changed. The copy
109 // keeps both buffers owned and the whole tick allocation-free. The resize is a
110 // no-op in steady state (initVectors() sizes m_staged); it only guards a
111 // mid-run port-count change.
112 m_staged.resize(m_outputs.size());
113 std::copy(m_outputs.cbegin(), m_outputs.cend(), m_staged.begin());
114 m_deferCommit = true;
115 }
116
120 {
121 m_deferCommit = false;
122 for (int i = 0; i < m_staged.size() && i < m_outputs.size(); ++i) {
123 setOutputValue(i, m_staged.at(i));
124 }
125 }
126
127private:
128 QVector<InputConnection> m_connections;
129 QVector<Status> m_inputs;
130 QVector<Status> m_outputs;
134 QVector<Status> m_staged;
135 bool m_outputChanged = false;
136 bool m_deferCommit = false;
137};
Central enumeration types for element types, groups, and signal status.
Enums::Status Status
Definition Enums.h:106
Owns a GraphicElement's simulation runtime state, decoupled from its graphics.
Status outputValue(const int index=0) const
Returns the four-state value on output slot index (Unknown if out of range).
bool updateInputs(bool allowUnknown, const QVector< InputPort * > &inputPorts)
Snapshots each predecessor's output into the input cache, reading inputPorts for unconnected-input de...
void clearOutputChanged()
Clears the output-changed flag.
void reset(const QVector< OutputPort * > &outputPorts)
Resets each output slot to its port's power-on default (Unknown coerced to Inactive).
void setOutputValue(const int index, const Status value)
Sets output slot index to value, flagging a change when it differs.
bool outputChanged() const
Returns true if any output changed since the flag was last cleared.
int decodeSelectValue(int offset, int count) const
Decodes count select-line statuses starting at offset into a binary index.
void initVectors(int inputCount, int outputCount, const QVector< OutputPort * > &outputPorts)
Allocates the I/O vectors and seeds outputs from outputPorts default statuses.
const QVector< Status > & outputs() const
const QVector< Status > & inputs() const
Read-only view of the cached simulation input values.
qsizetype outputSize() const
Returns the number of simulation output slots.
void connectPredecessor(int inputIndex, GraphicElement *source, int outputPort)
Records that simulation input inputIndex is driven by source output outputPort.
Abstract base class for all graphical circuit elements in wiRedPanda.
A port that receives a signal (the destination end of a wire).
Definition Port.h:234
A port that drives a signal (the source end of a wire).
Definition Port.h:266
A single simulation-graph edge: which element/output port feeds one input slot.