SILICON
Loading...
Searching...
No Matches
flipflops.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <core/component.hpp>
21#include <core/simulator.hpp>
22#include <core/wire.hpp>
23
24#include <optional>
25#include <string_view>
26#include <vector>
27
28namespace SILICON::core {
29
42class FlipFlop : public Component {
43public:
50 FlipFlop(unsigned int clockIndex, unsigned int clearIndex, unsigned int presetIndex);
51
60 FlipFlop(std::vector<Bus> inputs, std::vector<Bus> outputs, unsigned int clockIndex,
61 unsigned int clearIndex, unsigned int presetIndex);
62
66 void clearState();
67
79 const SILICON::simulation::Context& context) override;
80
81 [[nodiscard]] bool usesStagedSequentialOutputs() const override { return true; }
82
83protected:
88 [[nodiscard]] State latchedState() const { return state; }
89
94 virtual State captureState() const = 0;
95
99 [[nodiscard]] virtual bool isTimingSensitiveInput(unsigned int inputIndex) const = 0;
100
101private:
104 [[nodiscard]] bool
106 [[nodiscard]] bool violatesSetupTime(uint64_t currentTime,
107 bool timingInputChanged) const;
108 [[nodiscard]] bool violatesHoldTime(uint64_t currentTime,
109 bool timingInputChanged) const;
110
128 [[nodiscard]] bool mayHaveSelectedEdge(State previousClock, State currentClock) const;
129
131 unsigned int clockIndex;
132 unsigned int clearIndex;
133 unsigned int presetIndex;
134 uint64_t propagationDelay = 0;
135 uint64_t setupTime = 0;
136 uint64_t holdTime = 0;
137
138 std::optional<uint64_t> lastTimingInputChangeTime;
139 std::optional<uint64_t> lastSelectedClockEdgeTime;
140
141 // Cached properties to avoid costly map lookups per delta cycle.
144};
145
153class DFlipFlop : public FlipFlop {
154public:
156 static constexpr std::string_view Type = "DFlipFlop";
157
162 std::string_view typeName() const override { return Type; }
164 {
165 return {"D Flip Flop", "Captures D on the configured clock edge.",
167 }
168
170 enum class Inputs : unsigned int {
172 D = 0,
174 Clock = 1,
176 Clear = 2,
179 };
180
186
196 DFlipFlop(Wire_ptr d, Wire_ptr clock, Wire_ptr clear, Wire_ptr preset, Wire_ptr q,
197 Wire_ptr notQ);
198
199 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
200
201protected:
203 State captureState() const override;
204 bool isTimingSensitiveInput(unsigned int inputIndex) const override;
205};
206
215class EFlipFlop : public FlipFlop {
216public:
218 static constexpr std::string_view Type = "EFlipFlop";
219
224 std::string_view typeName() const override { return Type; }
226 {
227 return {"Enabled D Flip Flop",
228 "Captures D on the configured clock edge while enable is HIGH.",
230 }
231
233 enum class Inputs : unsigned int {
235 D = 0,
239 Clock = 2,
241 Clear = 3,
244 };
245
251
262 EFlipFlop(Wire_ptr d, Wire_ptr enable, Wire_ptr clock, Wire_ptr clear, Wire_ptr preset,
263 Wire_ptr q, Wire_ptr notQ);
264
265 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
266
267protected:
269 State captureState() const override;
270 bool isTimingSensitiveInput(unsigned int inputIndex) const override;
271};
272
281class DLatch : public Component {
282public:
284 static constexpr std::string_view Type = "DLatch";
285
286 std::string_view typeName() const override { return Type; }
288 {
289 return {"D Latch", "Follows D while enable is HIGH and holds while disabled.",
291 }
292
294 enum class Inputs : unsigned int {
296 D = 0,
299 };
300
302 DLatch();
303
311 DLatch(Wire_ptr d, Wire_ptr enable, Wire_ptr q, Wire_ptr notQ);
312
314 const SILICON::simulation::Context& context) override;
315 [[nodiscard]] bool usesStagedSequentialOutputs() const override { return true; }
316 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
317
318private:
321
323 uint64_t propagationDelay = 0;
324};
325
334class JKFlipFlop : public FlipFlop {
335public:
337 static constexpr std::string_view Type = "JKFlipFlop";
338
343 std::string_view typeName() const override { return Type; }
345 {
346 return {"JK Flip Flop", "Stores, sets, resets, or toggles state from J and K.",
348 }
349
351 enum class Inputs : unsigned int {
353 J = 0,
355 K = 1,
357 Clock = 2,
359 Clear = 3,
362 };
363
369
380 JKFlipFlop(Wire_ptr j, Wire_ptr k, Wire_ptr clock, Wire_ptr clear, Wire_ptr preset,
381 Wire_ptr q, Wire_ptr notQ);
382
383 void serializeYosys(SILICON::yosys::SerializationContext& context) const override;
384
385protected:
387 State captureState() const override;
388 bool isTimingSensitiveInput(unsigned int inputIndex) const override;
389};
390
391} // namespace SILICON::core
static constexpr unsigned int busIndex(Enum value)
Converts a component-local bus enum to its storage index.
std::vector< Bus > outputs
Output buses produced by this component.
std::vector< Bus > inputs
Input buses connected to this component.
static constexpr std::string_view Type
Serialization/type-registry identifier.
Inputs
Input bus indices for DFlipFlop.
@ Preset
Active-high asynchronous preset.
@ Clear
Active-high asynchronous clear.
@ D
Data input captured on the selected clock edge.
bool isTimingSensitiveInput(unsigned int inputIndex) const override
Returns true for synchronous pins governed by setup/hold timing.
DFlipFlop()
Constructs an unconnected D flip-flop.
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Returns the component type name.
State captureState() const override
Computes the state captured on the selected clock edge.
static constexpr std::string_view Type
Serialization/type-registry identifier.
bool usesStagedSequentialOutputs() const override
Whether zero-delay output writes should be staged during reactive passes.
Inputs
Input bus indices for DLatch.
@ Enable
Active-high transparency enable.
@ D
Data input followed while the latch is transparent.
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
void driveOutput(SILICON::simulation::Simulator &sim, State newState)
DLatch()
Constructs an unconnected D latch.
std::string_view typeName() const override
Gets the component type name.
void simulate(SILICON::simulation::Simulator &sim, const SILICON::simulation::Context &context) override
Executes the component's logic with simulator scheduling context.
static constexpr std::string_view Type
Serialization/type-registry identifier.
Inputs
Input bus indices for EFlipFlop.
@ Enable
Capture enable input.
@ Preset
Active-high asynchronous preset.
@ Clear
Active-high asynchronous clear.
@ D
Data input captured when Enable is HIGH.
EFlipFlop()
Constructs an unconnected enabled D flip-flop.
bool isTimingSensitiveInput(unsigned int inputIndex) const override
Returns true for synchronous pins governed by setup/hold timing.
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Returns the component type name.
State captureState() const override
Computes the state captured on the selected clock edge.
FlipFlop(unsigned int clockIndex, unsigned int clearIndex, unsigned int presetIndex)
Constructs a flip-flop base with the input indices used for control pins.
Definition flipflops.cpp:30
bool violatesSetupTime(uint64_t currentTime, bool timingInputChanged) const
bool usesStagedSequentialOutputs() const override
Whether zero-delay output writes should be staged during reactive passes.
Definition flipflops.hpp:81
bool mayHaveSelectedEdge(State previousClock, State currentClock) const
Checks if an ambiguous clock transition occurred that might be the triggering edge.
std::optional< uint64_t > lastSelectedClockEdgeTime
bool hasTimingSensitiveInputChange(const SILICON::simulation::Context &context) const
void driveOutput(SILICON::simulation::Simulator &sim, State newState)
bool violatesHoldTime(uint64_t currentTime, bool timingInputChanged) const
void simulate(SILICON::simulation::Simulator &sim, const SILICON::simulation::Context &context) override
Evaluates async controls and selected clock edges.
Definition flipflops.cpp:55
virtual State captureState() const =0
Computes the state captured on the selected clock edge.
State latchedState() const
Returns the currently stored state.
Definition flipflops.hpp:88
std::optional< uint64_t > lastTimingInputChangeTime
virtual bool isTimingSensitiveInput(unsigned int inputIndex) const =0
Returns true for synchronous pins governed by setup/hold timing.
SILICON::simulation::Simulator::EdgeType edgeType
void clearState()
Clear FF internal state (state, last transitions time, ...).
Definition flipflops.cpp:48
static constexpr std::string_view Type
Serialization/type-registry identifier.
Inputs
Input bus indices for JKFlipFlop.
@ Preset
Active-high asynchronous preset.
@ Clear
Active-high asynchronous clear.
bool isTimingSensitiveInput(unsigned int inputIndex) const override
Returns true for synchronous pins governed by setup/hold timing.
ComponentMetadata metadata() const override
Gets catalog metadata for this component type.
void serializeYosys(SILICON::yosys::SerializationContext &context) const override
Lower this component into native Yosys netlist cells.
std::string_view typeName() const override
Returns the component type name.
JKFlipFlop()
Constructs an unconnected JK flip-flop.
State captureState() const override
Computes the state captured on the selected clock edge.
Executes event-driven simulation of a Circuit.
Definition simulator.hpp:74
EdgeType
Logical edge observed for one wire during a reactive evaluation.
Definition simulator.hpp:99
Mutable module-building interface passed to component serializers.
Definition netlist.hpp:174
std::shared_ptr< Wire > Wire_ptr
Definition wire.hpp:116
Context describing why a component is being evaluated.
Definition component.hpp:90