SILICON
Loading...
Searching...
No Matches
FlipFlop Class Referenceabstract

Base class for edge-triggered storage elements with async clear/preset. More...

#include <flipflops.hpp>

Inheritance diagram for FlipFlop:
Collaboration diagram for FlipFlop:

Public Member Functions

 FlipFlop (unsigned int clockIndex, unsigned int clearIndex, unsigned int presetIndex)
 Constructs a flip-flop base with the input indices used for control pins.
 FlipFlop (std::vector< Bus > inputs, std::vector< Bus > outputs, unsigned int clockIndex, unsigned int clearIndex, unsigned int presetIndex)
 Constructs a flip-flop base with predefined buses and control pin indices.
void clearState ()
 Clear FF internal state (state, last transitions time, ...).
void simulate (SILICON::simulation::Simulator &sim, const SILICON::simulation::Context &context) override
 Evaluates async controls and selected clock edges.
bool usesStagedSequentialOutputs () const override
 Whether zero-delay output writes should be staged during reactive passes.
Public Member Functions inherited from Component
 Component ()=default
 Component (std::vector< Bus > inputs, std::vector< Bus > outputs)
 Constructs a component with input and output buses.
virtual ~Component ()=default
virtual void simulate (SILICON::simulation::Simulator &sim)
 Executes the component's logic.
void replaceBus (std::vector< Bus > &busCollection, unsigned int index, Bus newBus)
 Replaces a bus in the input or output collection.
void setProperty (std::string_view key, const PropertyValue &value)
 Sets a property value.
std::optional< PropertyValue > getProperty (std::string_view key) const
 Gets a property value.
const PropertyMap & getProperties () const
 Gets all properties.
std::optional< std::reference_wrapper< const StringPropertyOptions > > getStringPropertyOptions (std::string_view key) const
 Gets allowed values for a constrained string property.
template<typename T>
void setPropertyValue (std::string_view key, T &&value)
 Sets a property value with type deduction.
template<typename T>
std::optional< T > getPropertyValue (std::string_view key) const
 Gets a property value with type deduction.
void setPropertyCallback (std::string_view key, PropertyCallback callback)
 Sets a callback for property validation/transformation.
void setInput (unsigned int index, const Bus &bus, bool checkSize=false)
 Sets an input bus at a specific index.
void setInputs (const std::vector< Bus > &newInputs)
 Replaces all input buses.
void setOutput (unsigned int index, const Bus &bus)
 Sets an output bus at a specific index.
void setOutputs (const std::vector< Bus > &newOutputs)
 Replaces all output buses.
bool isConnectedTo (const Bus &b) const
 Checks if this component is connected to a bus.
virtual void clearWires ()
 Clears all wire connections.
std::vector< Bus > getInputs () const
 Gets all input buses.
const std::vector< Bus > & inputBuses () const
 Gets all input buses without copying.
std::optional< State > unconnectedInputDefault (unsigned int input) const
 Gets the state used when an input has no connected wire.
std::vector< Bus > getOutputs () const
 Gets all output buses.
const std::vector< Bus > & outputBuses () const
 Gets all output buses without copying.
auto allBuses () const
 Borrows all buses, inputs first, without copying or deduplicating.
virtual std::string_view typeName () const =0
 Gets the component type name.
virtual ComponentMetadata metadata () const
 Gets catalog metadata for this component type.
virtual void serializeYosys (SILICON::yosys::SerializationContext &context) const
 Lower this component into native Yosys netlist cells.
uint64_t addIOListener (IOObserver cb)
 Adds a listener for I/O changes.
void removeIOListener (uint64_t id)
 Removes a previously added I/O listener.

Protected Member Functions

State latchedState () const
 Returns the currently stored state.
virtual State captureState () const =0
 Computes the state captured on the selected clock edge.
virtual bool isTimingSensitiveInput (unsigned int inputIndex) const =0
 Returns true for synchronous pins governed by setup/hold timing.
Protected Member Functions inherited from Component
template<typename T>
void defineProperty (std::string key, T &&defaultValue, PropertyCallback callback=nullptr)
 Defines a property with a default value and optional callback.
void defineStringListProperty (std::string key, std::string defaultValue, StringPropertyOptions allowedValues, PropertyCallback callback=nullptr)
 Defines a string property constrained to a fixed set of values.
void notifyIOListeners ()
 Notifies all registered I/O listeners of a change.
void validatePropertyValue (std::string_view key, const PropertyValue &currentValue, const PropertyValue &newValue) const
 Validates a property value against the declared type and metadata.
void handleInputSizeChange (const unsigned int index, const unsigned int newSize)
 Handles input size changes. By default input size shouldn't change automatically.
void defineUnconnectedInputDefault (unsigned int input, State value)
 Defines the state read from an input when its wire is unconnected.
State inputState (unsigned int input, unsigned short bit=0) const
Wire_ptr inputWire (unsigned int input, unsigned short bit=0) const
template<typename Enum>
requires std::is_enum_v<Enum>
Wire_ptr inputWire (Enum input, unsigned short bit=0) const
template<typename Enum>
requires std::is_enum_v<Enum>
State inputState (Enum input, unsigned short bit=0) const
 Reads an input bit, returning ERROR when the bus or bit is missing.
Wire_ptr outputWire (unsigned int output, unsigned short bit=0) const
template<typename Enum>
requires std::is_enum_v<Enum>
Wire_ptr outputWire (Enum output, unsigned short bit=0) const
 Returns an output wire, or null when the bus or bit is missing.
std::size_t outputBusSize (unsigned int output) const
template<typename Enum>
requires std::is_enum_v<Enum>
std::size_t outputBusSize (Enum output) const
 Returns the size of an output bus, or zero when it is missing.

Private Member Functions

void initializeProperties ()
void driveOutput (SILICON::simulation::Simulator &sim, State newState)
bool hasTimingSensitiveInputChange (const SILICON::simulation::Context &context) const
bool violatesSetupTime (uint64_t currentTime, bool timingInputChanged) const
bool violatesHoldTime (uint64_t currentTime, bool timingInputChanged) const
bool mayHaveSelectedEdge (State previousClock, State currentClock) const
 Checks if an ambiguous clock transition occurred that might be the triggering edge.

Private Attributes

State state = State::UNKNOWN
unsigned int clockIndex
unsigned int clearIndex
unsigned int presetIndex
uint64_t propagationDelay = 0
uint64_t setupTime = 0
uint64_t holdTime = 0
std::optional< uint64_t > lastTimingInputChangeTime
std::optional< uint64_t > lastSelectedClockEdgeTime
SILICON::simulation::Simulator::EdgeType edgeType

Additional Inherited Members

Public Types inherited from Component
using IOObserver = std::function<void(Component*)>
 Callback type for I/O change notifications.
Static Protected Member Functions inherited from Component
template<typename Enum>
requires std::is_enum_v<Enum>
static constexpr unsigned int busIndex (Enum value)
 Converts a component-local bus enum to its storage index.
Protected Attributes inherited from Component
std::vector< Bus > inputs
 Input buses connected to this component.
std::vector< Bus > outputs
 Output buses produced by this component.
std::unordered_map< unsigned int, State > unconnectedInputDefaults
 Per-input state used when a bus bit has no connected wire.
PropertyMap properties
 Map of configurable properties for this component.
PropertyCallbackMap propertyCallbacks
 Map of callbacks for property validation/transformation.
StringPropertyOptionsMap stringPropertyOptions
 Allowed values for constrained string properties.
uint64_t nextIoListenerId = 0
 Counter for generating unique I/O listener IDs.
std::unordered_map< uint64_t, IOObserver > ioListeners
 Map of I/O change listeners indexed by ID.

Detailed Description

Base class for edge-triggered storage elements with async clear/preset.

FlipFlop centralizes common sequential behavior: selected clock-edge detection, asynchronous CLR/PRE handling, output driving with optional delay, and context-aware reactive evaluation. Derived classes provide only the synchronous capture rule.

Note
CLR and PRE are active-high. Unconnected CLR/PRE pins are treated as LOW. When both are active, the stored state becomes UNKNOWN. Unknown async control states also drive UNKNOWN.

Definition at line 42 of file flipflops.hpp.

Constructor & Destructor Documentation

◆ FlipFlop() [1/2]

FlipFlop ( unsigned int clockIndex,
unsigned int clearIndex,
unsigned int presetIndex )

Constructs a flip-flop base with the input indices used for control pins.

Parameters
clockIndexIndex of the clock input bus
clearIndexIndex of the asynchronous clear input bus
presetIndexIndex of the asynchronous preset input bus

Definition at line 30 of file flipflops.cpp.

◆ FlipFlop() [2/2]

FlipFlop ( std::vector< Bus > inputs,
std::vector< Bus > outputs,
unsigned int clockIndex,
unsigned int clearIndex,
unsigned int presetIndex )

Constructs a flip-flop base with predefined buses and control pin indices.

Parameters
inputsInput buses
outputsOutput buses, ordered as Q and not-Q
clockIndexIndex of the clock input bus
clearIndexIndex of the asynchronous clear input bus
presetIndexIndex of the asynchronous preset input bus

Definition at line 39 of file flipflops.cpp.

Member Function Documentation

◆ captureState()

virtual State captureState ( ) const
protectedpure virtual

Computes the state captured on the selected clock edge.

Returns
New storage state for the concrete flip-flop type

Implemented in DFlipFlop, EFlipFlop, and JKFlipFlop.

◆ clearState()

void clearState ( )

Clear FF internal state (state, last transitions time, ...).

Definition at line 48 of file flipflops.cpp.

◆ driveOutput()

void driveOutput ( SILICON::simulation::Simulator & sim,
State newState )
private

Definition at line 143 of file flipflops.cpp.

◆ hasTimingSensitiveInputChange()

bool hasTimingSensitiveInputChange ( const SILICON::simulation::Context & context) const
nodiscardprivate

Definition at line 156 of file flipflops.cpp.

◆ initializeProperties()

void initializeProperties ( )
private

Definition at line 113 of file flipflops.cpp.

◆ isTimingSensitiveInput()

virtual bool isTimingSensitiveInput ( unsigned int inputIndex) const
nodiscardprotectedpure virtual

Returns true for synchronous pins governed by setup/hold timing.

Implemented in DFlipFlop, EFlipFlop, and JKFlipFlop.

◆ latchedState()

State latchedState ( ) const
inlinenodiscardprotected

Returns the currently stored state.

Returns
Latched storage state

Definition at line 88 of file flipflops.hpp.

◆ mayHaveSelectedEdge()

bool mayHaveSelectedEdge ( State previousClock,
State currentClock ) const
nodiscardprivate

Checks if an ambiguous clock transition occurred that might be the triggering edge.

This is used to propagate UNKNOWN (X) states when a clock edge cannot be guaranteed. It specifically prevents false positives and negatives that a simple clock == UNKNOWN check would cause. For a Positive-Edge Triggered (PET) flip-flop:

  • LOW -> UNKNOWN: Could be a rising edge. (Returns true)
  • UNKNOWN -> HIGH: Could be a rising edge. (Returns true)
  • HIGH -> UNKNOWN: Cannot be a rising edge; it is either holding HIGH or falling. (Returns false)
Parameters
previousClockThe previous state of the clock pin
currentClockThe current state of the clock pin
Returns
true if the transition from previousClock to currentClock could be the active edge

Definition at line 192 of file flipflops.cpp.

◆ simulate()

void simulate ( SILICON::simulation::Simulator & sim,
const SILICON::simulation::Context & context )
overridevirtual

Evaluates async controls and selected clock edges.

Initial/full evaluations drive the initial stored state when needed. Reactive evaluations use SILICON::simulation::Context::previousState for edge detection and timing; async CLR/PRE are evaluated first on every call.

Parameters
simSILICON::simulation::Simulator used to drive output wires
contextEvaluation context supplied by the simulator

Reimplemented from Component.

Definition at line 55 of file flipflops.cpp.

◆ usesStagedSequentialOutputs()

bool usesStagedSequentialOutputs ( ) const
inlinenodiscardoverridevirtual

Whether zero-delay output writes should be staged during reactive passes.

Edge-triggered sequential components return true so all triggered outputs are committed from the same pre-edge snapshot. Combinational components keep the default immediate zero-delay behavior.

Reimplemented from Component.

Definition at line 81 of file flipflops.hpp.

◆ violatesHoldTime()

bool violatesHoldTime ( uint64_t currentTime,
bool timingInputChanged ) const
nodiscardprivate

Definition at line 185 of file flipflops.cpp.

◆ violatesSetupTime()

bool violatesSetupTime ( uint64_t currentTime,
bool timingInputChanged ) const
nodiscardprivate

Definition at line 172 of file flipflops.cpp.

Member Data Documentation

◆ clearIndex

unsigned int clearIndex
private

Definition at line 132 of file flipflops.hpp.

◆ clockIndex

unsigned int clockIndex
private

Definition at line 131 of file flipflops.hpp.

◆ edgeType

◆ holdTime

uint64_t holdTime = 0
private

Definition at line 136 of file flipflops.hpp.

◆ lastSelectedClockEdgeTime

std::optional<uint64_t> lastSelectedClockEdgeTime
private

Definition at line 139 of file flipflops.hpp.

◆ lastTimingInputChangeTime

std::optional<uint64_t> lastTimingInputChangeTime
private

Definition at line 138 of file flipflops.hpp.

◆ presetIndex

unsigned int presetIndex
private

Definition at line 133 of file flipflops.hpp.

◆ propagationDelay

uint64_t propagationDelay = 0
private

Definition at line 134 of file flipflops.hpp.

◆ setupTime

uint64_t setupTime = 0
private

Definition at line 135 of file flipflops.hpp.

◆ state

State state = State::UNKNOWN
private

Definition at line 130 of file flipflops.hpp.


The documentation for this class was generated from the following files: