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
55 bool updateInputs(bool allowUnknown, const QVector<InputPort *> &inputPorts);
56
58 int decodeSelectValue(int offset, int count) const;
59
61 Status outputValue(const int index = 0) const
62 {
63 if (index >= m_outputs.size()) { return Status::Unknown; }
64 return m_outputs.at(index);
65 }
66
68 void setOutputValue(const int index, const Status value)
69 {
70 if (index >= m_outputs.size()) { return; }
71 if (m_deferCommit) {
72 // Synchronous sequential element mid-tick: stage the value so peers
73 // still read the old output. commitDeferredOutputs() publishes it.
74 m_staged[index] = value;
75 return;
76 }
77 if (m_outputs[index] != value) { m_outputChanged = true; }
78 m_outputs[index] = value;
79 }
80
82 qsizetype outputSize() const { return m_outputs.size(); }
83
85 bool outputChanged() const { return m_outputChanged; }
86
88 void clearOutputChanged() { m_outputChanged = false; }
89
91 const QVector<Status> &inputs() const { return m_inputs; }
92
97 const QVector<Status> &outputs() const { return m_deferCommit ? m_staged : m_outputs; }
98
102 {
103 // Element-wise copy into persistent storage, deliberately NOT `m_staged =
104 // m_outputs`: assignment CoW-shares the two buffers, so every staged write and
105 // every commit write into m_outputs pays a detach (malloc + deep copy) -- per
106 // sequential element, per simulation tick, even when nothing changed. The copy
107 // keeps both buffers owned and the whole tick allocation-free. The resize is a
108 // no-op in steady state (initVectors() sizes m_staged); it only guards a
109 // mid-run port-count change.
110 m_staged.resize(m_outputs.size());
111 std::copy(m_outputs.cbegin(), m_outputs.cend(), m_staged.begin());
112 m_deferCommit = true;
113 }
114
118 {
119 m_deferCommit = false;
120 for (int i = 0; i < m_staged.size() && i < m_outputs.size(); ++i) {
121 setOutputValue(i, m_staged.at(i));
122 }
123 }
124
125private:
126 QVector<InputConnection> m_connections;
127 QVector<Status> m_inputs;
128 QVector<Status> m_outputs;
132 QVector<Status> m_staged;
133 bool m_outputChanged = false;
134 bool m_deferCommit = false;
135};
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:229
A port that drives a signal (the source end of a wire).
Definition Port.h:261
A single simulation-graph edge: which element/output port feeds one input slot.