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

Base class for all digital circuit components. More...

#include <component.hpp>

Inheritance diagram for Component:
Collaboration diagram for Component:

Public Types

using IOObserver = std::function<void(Component*)>
 Callback type for I/O change notifications.

Public Member Functions

 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.
virtual void simulate (SILICON::simulation::Simulator &sim, const SILICON::simulation::Context &context)
 Executes the component's logic with simulator scheduling context.
virtual bool usesStagedSequentialOutputs () const
 Whether zero-delay output writes should be staged during reactive passes.
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

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.

Static Protected Member Functions

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

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 all digital circuit components.

The Component class represents a fundamental building block in a digital circuit. It manages input and output buses for data connectivity, maintains configurable properties, and implements an observer pattern for I/O changes.

Components operate in a reactive simulation environment where their simulate() method is called to process inputs and produce outputs.

See also
Circuit
Simulator

Definition at line 199 of file component.hpp.

Member Typedef Documentation

◆ IOObserver

using IOObserver = std::function<void(Component*)>

Callback type for I/O change notifications.

IOObservers are invoked when the component's input or output buses change, allowing dependent components or circuits to update accordingly.

Parameters
componentPointer to the component whose I/O changed

Definition at line 209 of file component.hpp.

Constructor & Destructor Documentation

◆ Component() [1/2]

Component ( )
default

◆ Component() [2/2]

Component ( std::vector< Bus > inputs,
std::vector< Bus > outputs )

Constructs a component with input and output buses.

Parameters
inputsVector of input buses
outputsVector of output buses

Definition at line 49 of file component.cpp.

◆ ~Component()

virtual ~Component ( )
virtualdefault

Member Function Documentation

◆ addIOListener()

uint64_t addIOListener ( IOObserver cb)

Adds a listener for I/O changes.

Parameters
cbThe callback function
Returns
Unique ID for later removing the listener

Definition at line 60 of file component.cpp.

◆ allBuses()

auto allBuses ( ) const
inlinenodiscard

Borrows all buses, inputs first, without copying or deduplicating.

Note
Replacing buses invalidates the view. The component must outlive it.

Definition at line 609 of file component.hpp.

◆ busIndex()

template<typename Enum>
requires std::is_enum_v<Enum>
constexpr unsigned int busIndex ( Enum value)
inlinestaticconstexprprotected

Converts a component-local bus enum to its storage index.

Template Parameters
EnumComponent input/output enum type
Parameters
valueEnum value to convert
Returns
Numeric bus index

Definition at line 308 of file component.hpp.

◆ clearWires()

void clearWires ( )
virtual

Clears all wire connections.

Removes references to all wires in input and output buses, disconnecting this component from the circuit.

Derived components may override this when some pins have component-specific unconnected semantics.

Definition at line 243 of file component.cpp.

◆ defineProperty()

template<typename T>
void defineProperty ( std::string key,
T && defaultValue,
PropertyCallback callback = nullptr )
inlineprotected

Defines a property with a default value and optional callback.

Used by derived components to declare their configurable properties during construction.

Template Parameters
TThe property value type
Parameters
keyThe property name
defaultValueThe default property value
callbackOptional callback for value validation/transformation

Definition at line 248 of file component.hpp.

◆ defineStringListProperty()

void defineStringListProperty ( std::string key,
std::string defaultValue,
StringPropertyOptions allowedValues,
PropertyCallback callback = nullptr )
protected

Defines a string property constrained to a fixed set of values.

Parameters
keyThe property name
defaultValueThe default string value
allowedValuesNon-empty list of accepted values
callbackOptional callback for validation/transformation

Definition at line 79 of file component.cpp.

◆ defineUnconnectedInputDefault()

void defineUnconnectedInputDefault ( unsigned int input,
State value )
inlineprotected

Defines the state read from an input when its wire is unconnected.

Parameters
inputInput bus index
valueState used for every unconnected bit on that input bus

Definition at line 318 of file component.hpp.

◆ getInputs()

std::vector< Bus > getInputs ( ) const
inlinenodiscard

Gets all input buses.

Returns
Vector of input buses

Definition at line 571 of file component.hpp.

◆ getOutputs()

std::vector< Bus > getOutputs ( ) const
inlinenodiscard

Gets all output buses.

Returns
Vector of output buses

Definition at line 597 of file component.hpp.

◆ getProperties()

const PropertyMap & getProperties ( ) const
inlinenodiscard

Gets all properties.

Returns
Reference to the property map

Definition at line 478 of file component.hpp.

◆ getProperty()

std::optional< PropertyValue > getProperty ( std::string_view key) const
nodiscard

Gets a property value.

Parameters
keyThe property name
Returns
The property value if present

Definition at line 165 of file component.cpp.

◆ getPropertyValue()

template<typename T>
std::optional< T > getPropertyValue ( std::string_view key) const
inline

Gets a property value with type deduction.

Template Parameters
TThe expected property type
Parameters
keyThe property name
Returns
The property value if present and of correct type

Definition at line 504 of file component.hpp.

◆ getStringPropertyOptions()

std::optional< std::reference_wrapper< const StringPropertyOptions > > getStringPropertyOptions ( std::string_view key) const
nodiscard

Gets allowed values for a constrained string property.

Returns
The allowed values if the property is constrained

Definition at line 174 of file component.cpp.

◆ handleInputSizeChange()

void handleInputSizeChange ( const unsigned int index,
const unsigned int newSize )
protected

Handles input size changes. By default input size shouldn't change automatically.

Parameters
indexThe index of the input being changed
newSizeThe new size of the input

Definition at line 122 of file component.cpp.

◆ inputBuses()

const std::vector< Bus > & inputBuses ( ) const
inlinenodiscard

Gets all input buses without copying.

Returns
Const reference to the input bus vector

Definition at line 577 of file component.hpp.

◆ inputState() [1/2]

template<typename Enum>
requires std::is_enum_v<Enum>
State inputState ( Enum input,
unsigned short bit = 0 ) const
inlinenodiscardprotected

Reads an input bit, returning ERROR when the bus or bit is missing.

Template Parameters
EnumComponent input enum type
Parameters
inputInput bus enum value
bitBit index inside the bus
Returns
Current wire state, or ERROR for missing wiring

Definition at line 355 of file component.hpp.

◆ inputState() [2/2]

State inputState ( unsigned int input,
unsigned short bit = 0 ) const
inlinenodiscardprotected

Definition at line 323 of file component.hpp.

◆ inputWire() [1/2]

template<typename Enum>
requires std::is_enum_v<Enum>
Wire_ptr inputWire ( Enum input,
unsigned short bit = 0 ) const
inlinenodiscardprotected

Definition at line 341 of file component.hpp.

◆ inputWire() [2/2]

Wire_ptr inputWire ( unsigned int input,
unsigned short bit = 0 ) const
inlinenodiscardprotected

Definition at line 331 of file component.hpp.

◆ isConnectedTo()

bool isConnectedTo ( const Bus & b) const

Checks if this component is connected to a bus.

Parameters
bThe bus to check
Returns
True if connected

Definition at line 238 of file component.cpp.

◆ metadata()

ComponentMetadata metadata ( ) const
nodiscardvirtual

◆ notifyIOListeners()

void notifyIOListeners ( )
protected

Notifies all registered I/O listeners of a change.

Called when the component's input or output configuration changes, typically after setInput/setOutput is called.

Definition at line 72 of file component.cpp.

◆ outputBuses()

const std::vector< Bus > & outputBuses ( ) const
inlinenodiscard

Gets all output buses without copying.

Returns
Const reference to the output bus vector

Definition at line 603 of file component.hpp.

◆ outputBusSize() [1/2]

template<typename Enum>
requires std::is_enum_v<Enum>
std::size_t outputBusSize ( Enum output) const
inlinenodiscardprotected

Returns the size of an output bus, or zero when it is missing.

Template Parameters
EnumComponent output enum type
Parameters
outputOutput bus enum value
Returns
Output bus width

Definition at line 395 of file component.hpp.

◆ outputBusSize() [2/2]

std::size_t outputBusSize ( unsigned int output) const
inlinenodiscardprotected

Definition at line 382 of file component.hpp.

◆ outputWire() [1/2]

template<typename Enum>
requires std::is_enum_v<Enum>
Wire_ptr outputWire ( Enum output,
unsigned short bit = 0 ) const
inlinenodiscardprotected

Returns an output wire, or null when the bus or bit is missing.

Template Parameters
EnumComponent output enum type
Parameters
outputOutput bus enum value
bitBit index inside the bus
Returns
Output wire pointer, or null

Definition at line 377 of file component.hpp.

◆ outputWire() [2/2]

Wire_ptr outputWire ( unsigned int output,
unsigned short bit = 0 ) const
inlinenodiscardprotected

Definition at line 360 of file component.hpp.

◆ removeIOListener()

void removeIOListener ( uint64_t id)

Removes a previously added I/O listener.

Parameters
idThe ID returned by addIOListener

Definition at line 67 of file component.cpp.

◆ replaceBus()

void replaceBus ( std::vector< Bus > & busCollection,
unsigned int index,
Bus newBus )

Replaces a bus in the input or output collection.

Parameters
busCollectionThe collection to modify (inputs or outputs)
indexThe index of the bus to replace
newBusThe new bus to set

Definition at line 131 of file component.cpp.

◆ serializeYosys()

void serializeYosys ( SILICON::yosys::SerializationContext & context) const
virtual

Lower this component into native Yosys netlist cells.

Third-party components that do not override this method fail export with a descriptive error instead of silently producing an incomplete netlist.

Reimplemented in AndGate, BoundaryIoComponent, ConstantComponent, DFlipFlop, DLatch, EFlipFlop, JKFlipFlop, NandGate, NorGate, NotGate, OrGate, Register, ROM, SubcircuitComponent, XorGate, AdderNBits, Comparator, Complementer, Decoder, Demultiplexer, Divider, Extender, FullAdder, HalfAdder, Multiplexer, Multiplier, Shifter, WireMerger, and WireSplitter.

Definition at line 105 of file components.cpp.

◆ setInput()

void setInput ( unsigned int index,
const Bus & bus,
bool checkSize = false )

Sets an input bus at a specific index.

Parameters
indexThe input index
busThe bus to connect
checkSizeWhether to call handleInputSizeChange

Definition at line 197 of file component.cpp.

◆ setInputs()

void setInputs ( const std::vector< Bus > & newInputs)

Replaces all input buses.

Parameters
newInputsThe new input buses

Definition at line 216 of file component.cpp.

◆ setOutput()

void setOutput ( unsigned int index,
const Bus & bus )

Sets an output bus at a specific index.

Parameters
indexThe output index
busThe bus to connect

Definition at line 208 of file component.cpp.

◆ setOutputs()

void setOutputs ( const std::vector< Bus > & newOutputs)

Replaces all output buses.

Parameters
newOutputsThe new output buses

Definition at line 227 of file component.cpp.

◆ setProperty()

void setProperty ( std::string_view key,
const PropertyValue & value )

Sets a property value.

Parameters
keyThe property name
valueThe property value

Definition at line 143 of file component.cpp.

◆ setPropertyCallback()

void setPropertyCallback ( std::string_view key,
PropertyCallback callback )

Sets a callback for property validation/transformation.

Parameters
keyThe property name
callbackThe callback function

Definition at line 183 of file component.cpp.

◆ setPropertyValue()

template<typename T>
void setPropertyValue ( std::string_view key,
T && value )
inline

Sets a property value with type deduction.

Template Parameters
TThe property value type
Parameters
keyThe property name
valueThe property value

Definition at line 493 of file component.hpp.

◆ simulate() [1/2]

virtual void simulate ( SILICON::simulation::Simulator & sim)
inlinevirtual

Executes the component's logic.

Called by the Simulator during circuit evaluation. Each component implements this to read input bus states and produce output bus states.

Parameters
simThe simulator executing this component

Reimplemented in AndGate, ConstantComponent, DummyBusInputComponent, DummyBusOutputComponent, DummyInputComponent, DummyOutputComponent, NandGate, NorGate, NotGate, OrGate, ROM, SubcircuitComponent, XorGate, AdderNBits, Comparator, Complementer, Decoder, Demultiplexer, Divider, Extender, FullAdder, HalfAdder, Multiplexer, Multiplier, Shifter, WireMerger, and WireSplitter.

Definition at line 421 of file component.hpp.

◆ simulate() [2/2]

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

Executes the component's logic with simulator scheduling context.

The default implementation preserves backwards compatibility by delegating to simulate(Simulator&). Components can override this overload only when they need to inspect the evaluation context.

Parameters
simThe simulator executing this component
contextEvaluation metadata for this simulation pass

Reimplemented in DLatch, FlipFlop, and Register.

Definition at line 436 of file component.hpp.

◆ typeName()

◆ unconnectedInputDefault()

std::optional< State > unconnectedInputDefault ( unsigned int input) const
inlinenodiscard

Gets the state used when an input has no connected wire.

Parameters
inputInput bus index
Returns
Declared default, or no value when the input is required

Definition at line 584 of file component.hpp.

◆ usesStagedSequentialOutputs()

virtual bool usesStagedSequentialOutputs ( ) const
inlinenodiscardvirtual

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 in DLatch, FlipFlop, and Register.

Definition at line 450 of file component.hpp.

◆ validatePropertyValue()

void validatePropertyValue ( std::string_view key,
const PropertyValue & currentValue,
const PropertyValue & newValue ) const
protected

Validates a property value against the declared type and metadata.

Definition at line 103 of file component.cpp.

Member Data Documentation

◆ inputs

std::vector<Bus> inputs
protected

Input buses connected to this component.

Definition at line 213 of file component.hpp.

◆ ioListeners

std::unordered_map<uint64_t, IOObserver> ioListeners
protected

Map of I/O change listeners indexed by ID.

Definition at line 234 of file component.hpp.

◆ nextIoListenerId

uint64_t nextIoListenerId = 0
protected

Counter for generating unique I/O listener IDs.

Definition at line 231 of file component.hpp.

◆ outputs

std::vector<Bus> outputs
protected

Output buses produced by this component.

Definition at line 216 of file component.hpp.

◆ properties

PropertyMap properties
protected

Map of configurable properties for this component.

Definition at line 222 of file component.hpp.

◆ propertyCallbacks

PropertyCallbackMap propertyCallbacks
protected

Map of callbacks for property validation/transformation.

Definition at line 225 of file component.hpp.

◆ stringPropertyOptions

StringPropertyOptionsMap stringPropertyOptions
protected

Allowed values for constrained string properties.

Definition at line 228 of file component.hpp.

◆ unconnectedInputDefaults

std::unordered_map<unsigned int, State> unconnectedInputDefaults
protected

Per-input state used when a bus bit has no connected wire.

Definition at line 219 of file component.hpp.


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