wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
SRFlipFlop.cpp
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
5
8#include "App/Wiring/Port.h"
9
10template<>
13 .type = ElementType::SRFlipFlop,
14 .group = ElementGroup::Memory,
15 .minInputSize = 5,
16 .maxInputSize = 5,
17 .minOutputSize = 2,
18 .maxOutputSize = 2,
19 .canChangeAppearance = true,
20 };
21 static_assert(validate(constraints));
22
24 {
26 meta.pixmapPath = []{ return SRFlipFlop::pixmapPath(); };
27 meta.titleText = QT_TRANSLATE_NOOP("SRFlipFlop", "SR-FLIP-FLOP");
28 meta.translatedName = QT_TRANSLATE_NOOP("SRFlipFlop", "SR-Flip-Flop");
29 meta.trContext = "SRFlipFlop";
30 return meta;
31 }
32
33 static inline const bool registered = []() {
35 ElementFactory::registerCreator(constraints.type, [] { return new SRFlipFlop(); });
36 return true;
37 }();
38};
39
40SRFlipFlop::SRFlipFlop(QGraphicsItem *parent)
42{
43 // Call the most-derived override explicitly to guarantee correct port positions
44 // even if a subclass overrides updatePortsProperties() further.
46}
47
49{
50 // SR flip-flop: S (Set) and R (Reset) are clocked inputs; Clock is required.
51 // ~Preset and ~Clear provide asynchronous overrides (active-low, same as DFlipFlop).
52 inputPort(0)->setPos( 0, 16); inputPort(0)->setName("S");
53 inputPort(1)->setPos( 0, 32); inputPort(1)->setName("Clock");
54 inputPort(2)->setPos( 0, 48); inputPort(2)->setName("R");
55 inputPort(3)->setPos(32, 0); inputPort(3)->setName("~Preset");
56 inputPort(4)->setPos(32, 64); inputPort(4)->setName("~Clear");
57
58 outputPort(0)->setPos(64, 16); outputPort(0)->setName("Q");
59 outputPort(1)->setPos(64, 48); outputPort(1)->setName("~Q");
60
61 // S, R, ~Preset, ~Clear are optional; only Clock is always required.
62 // S and R unconnected default to Inactive (no set/reset action on clock edge).
63 // ~Preset and ~Clear unconnected default to Active (HIGH = not asserted).
64 inputPort(0)->setRequired(false);
65 inputPort(2)->setRequired(false);
66 inputPort(3)->setRequired(false);
67 inputPort(4)->setRequired(false);
68
69 inputPort(3)->setDefaultStatus(Status::Active);
70 inputPort(4)->setDefaultStatus(Status::Active);
71
72 // Initial state: Q=0, ~Q=1
73 outputPort(0)->setDefaultStatus(Status::Inactive);
74 outputPort(1)->setDefaultStatus(Status::Active);
75}
76
78{
79 // Reload the pixmap before delegating to the base class so the correct
80 // theme-specific SVG is used before ports and colours are updated.
83}
84
86{
87 if (!simUpdateInputs()) {
88 // Forget the last clock level while inputs are invalid (see DFlipFlop).
89 m_simLastClk = Status::Unknown;
90 return;
91 }
92 Status q0 = simOutputs().at(0);
93 Status q1 = simOutputs().at(1);
94 const Status s = simInputs().at(0);
95 const Status clk = simInputs().at(1);
96 const Status r = simInputs().at(2);
97 const Status prst = simInputs().at(3);
98 const Status clr = simInputs().at(4);
99
100 if (clk == Status::Active && m_simLastClk == Status::Inactive) {
101 if (m_simLastS == Status::Active && m_simLastR == Status::Active) {
102 q0 = Status::Active;
103 q1 = Status::Active;
104 } else if (m_simLastS != m_simLastR) {
105 q0 = m_simLastS;
106 q1 = m_simLastR;
107 }
108 }
109
110 if (prst == Status::Inactive || clr == Status::Inactive) {
111 q0 = (prst == Status::Inactive) ? Status::Active : Status::Inactive;
112 q1 = (clr == Status::Inactive) ? Status::Active : Status::Inactive;
113 }
114 m_simLastClk = clk;
115 m_simLastS = s;
116 m_simLastR = r;
117 setOutputValue(0, q0);
118 setOutputValue(1, q1);
119}
120
122{
124 m_simLastClk = Status::Inactive;
125 m_simLastS = Status::Inactive;
126 m_simLastR = Status::Inactive;
127}
Singleton factory for all circuit element types.
Self-registering element trait template and compile-time constraint validation.
ElementMetadata metadataFromConstraints(const ElementConstraints &c)
Converts ElementConstraints to an ElementMetadata with all constraint-derived fields set.
Definition ElementInfo.h:80
constexpr bool validate(const ElementConstraints &c)
Validates element constraints at compile time.
Definition ElementInfo.h:48
Enums::ElementType ElementType
Definition Enums.h:107
Enums::Status Status
Definition Enums.h:106
Port classes: Port (base), InputPort, and OutputPort.
Graphic element for the clocked SR flip-flop.
static void registerCreator(ElementType type, std::function< GraphicElement *()> creator)
Registers a creator lambda for type, used by buildElement().
static void registerMetadata(const ElementMetadata &meta)
Registers meta in the global map (called once per element type at startup).
GraphicElement(ElementType type, QGraphicsItem *parent=nullptr)
Constructs a graphic element of the given type, fetching all properties from the metadata registry.
const QVector< Status > & simInputs() const
Read-only view of the cached simulation input values.
virtual void updateTheme()
Updates the element's visual theme according to the current dark/light palette.
void setOutputValue(const int index, const Status value)
Sets simulation output port index to value.
InputPort * inputPort(const int index=0) const
Returns the input port at index (default 0).
OutputPort * outputPort(const int index=0) const
Returns the output port at index (default 0).
bool simUpdateInputs()
Snapshots each predecessor's output into the simulation input cache.
const QVector< Status > & simOutputs() const
Read-only view of the current simulation output values.
void setPixmap(const QString &pixmapPath)
Loads and applies the pixmap located at pixmapPath.
virtual void resetSimState()
Resets all simulation-visible state to power-on defaults.
void setRequired(const bool required)
Marks whether a wire to this port is mandatory.
Definition Port.cpp:199
void setDefaultStatus(const Status defaultStatus)
Sets the status applied when the port has no connection.
Definition Port.cpp:172
void setName(const QString &name)
Sets the label text shown next to the port.
Definition Port.cpp:166
Graphical representation of an edge-triggered SR flip-flop.
Definition SRFlipFlop.h:20
SRFlipFlop(QGraphicsItem *parent=nullptr)
Constructs the element with optional parent.
void updatePortsProperties() override
Updates port names for this flip-flop.
static QString pixmapPath()
Definition SRFlipFlop.h:31
void resetSimState() override
Resets Q/~Q outputs and edge-detection state to power-on defaults.
void updateLogic() override
Updates output state on each rising clock edge.
void updateTheme() override
Refreshes the pixmap when the application theme changes.
Compile-time-validatable subset of ElementMetadata.
Definition ElementInfo.h:21
static ElementMetadata metadata()
static constexpr ElementConstraints constraints
static const bool registered
Self-registering element information trait.
Compile-time-registered properties for one element type.