29#include <unordered_map>
104 return previous.has_value() && wire && *previous != wire->getCurrentState();
134 { T::Type } -> std::convertible_to<std::string_view>;
199class Component :
public std::enable_shared_from_this<Component> {
247 template <
typename T>
253 cb = std::move(callback);
256 using Decayed = std::decay_t<T>;
258 if constexpr (std::is_constructible_v<std::string, T>
259 && !std::is_same_v<Decayed, bool>) {
260 defaultVal = std::string(std::forward<T>(defaultValue));
264 PropertyValue finalValue = cb ? cb(defaultVal) : std::move(defaultVal);
265 properties.insert_or_assign(std::move(key), std::move(finalValue));
306 template <
typename Enum>
307 requires std::is_enum_v<Enum>
310 return std::to_underlying(value);
333 if (input >=
inputs.size() || bit >=
inputs[input].size())
336 return inputs[input][bit];
339 template <
typename Enum>
340 requires std::is_enum_v<Enum>
353 template <
typename Enum>
354 requires std::is_enum_v<Enum>
375 template <
typename Enum>
376 requires std::is_enum_v<Enum>
393 template <
typename Enum>
394 requires std::is_enum_v<Enum>
459 void replaceBus(std::vector<Bus>& busCollection,
unsigned int index,
Bus newBus);
473 [[nodiscard]] std::optional<PropertyValue>
getProperty(std::string_view key)
const;
484 [[nodiscard]] std::optional<std::reference_wrapper<const StringPropertyOptions>>
508 if (
const T* val = std::get_if<T>(&it->second)) {
528 void setInput(
unsigned int index,
const Bus& bus,
bool checkSize =
false);
534 void setInputs(
const std::vector<Bus>& newInputs);
547 void setOutputs(
const std::vector<Bus>& newOutputs);
611 return std::array{std::span<const Bus>(
inputs), std::span<const Bus>(
outputs)}
static constexpr unsigned int busIndex(Enum value)
Converts a component-local bus enum to its storage index.
Wire_ptr outputWire(unsigned int output, unsigned short bit=0) const
const std::vector< Bus > & inputBuses() const
Gets all input buses without copying.
std::function< void(Component *)> IOObserver
Callback type for I/O change notifications.
bool isConnectedTo(const Bus &b) const
Checks if this component is connected to a bus.
void removeIOListener(uint64_t id)
Removes a previously added I/O listener.
std::optional< PropertyValue > getProperty(std::string_view key) const
Gets a property value.
virtual std::string_view typeName() const =0
Gets the component type name.
auto allBuses() const
Borrows all buses, inputs first, without copying or deduplicating.
StringPropertyOptionsMap stringPropertyOptions
Allowed values for constrained string properties.
State inputState(unsigned int input, unsigned short bit=0) const
void setOutputs(const std::vector< Bus > &newOutputs)
Replaces all output buses.
void setInput(unsigned int index, const Bus &bus, bool checkSize=false)
Sets an input bus at a specific index.
const PropertyMap & getProperties() const
Gets all properties.
std::optional< T > getPropertyValue(std::string_view key) const
Gets a property value with type deduction.
std::vector< Bus > outputs
Output buses produced by this component.
void setInputs(const std::vector< Bus > &newInputs)
Replaces all input buses.
void setProperty(std::string_view key, const PropertyValue &value)
Sets a property value.
std::size_t outputBusSize(Enum output) const
Returns the size of an output bus, or zero when it is missing.
std::vector< Bus > getOutputs() const
Gets all output buses.
State inputState(Enum input, unsigned short bit=0) const
Reads an input bit, returning ERROR when the bus or bit is missing.
const std::vector< Bus > & outputBuses() const
Gets all output buses without copying.
void defineUnconnectedInputDefault(unsigned int input, State value)
Defines the state read from an input when its wire is unconnected.
std::unordered_map< unsigned int, State > unconnectedInputDefaults
Per-input state used when a bus bit has no connected wire.
std::vector< Bus > inputs
Input buses connected to this component.
std::optional< State > unconnectedInputDefault(unsigned int input) const
Gets the state used when an input has no connected wire.
void defineStringListProperty(std::string key, std::string defaultValue, StringPropertyOptions allowedValues, PropertyCallback callback=nullptr)
Defines a string property constrained to a fixed set of values.
virtual void simulate(SILICON::simulation::Simulator &sim, const SILICON::simulation::Context &context)
Executes the component's logic with simulator scheduling context.
virtual void simulate(SILICON::simulation::Simulator &sim)
Executes the component's logic.
PropertyMap properties
Map of configurable properties for this component.
void setOutput(unsigned int index, const Bus &bus)
Sets an output bus at a specific index.
void replaceBus(std::vector< Bus > &busCollection, unsigned int index, Bus newBus)
Replaces a bus in the input or output collection.
void setPropertyCallback(std::string_view key, PropertyCallback callback)
Sets a callback for property validation/transformation.
virtual void clearWires()
Clears all wire connections.
PropertyCallbackMap propertyCallbacks
Map of callbacks for property validation/transformation.
virtual ~Component()=default
Wire_ptr outputWire(Enum output, unsigned short bit=0) const
Returns an output wire, or null when the bus or bit is missing.
uint64_t addIOListener(IOObserver cb)
Adds a listener for I/O changes.
virtual ComponentMetadata metadata() const
Gets catalog metadata for this component type.
Wire_ptr inputWire(unsigned int input, unsigned short bit=0) const
uint64_t nextIoListenerId
Counter for generating unique I/O listener IDs.
virtual bool usesStagedSequentialOutputs() const
Whether zero-delay output writes should be staged during reactive passes.
std::optional< std::reference_wrapper< const StringPropertyOptions > > getStringPropertyOptions(std::string_view key) const
Gets allowed values for a constrained string property.
void defineProperty(std::string key, T &&defaultValue, PropertyCallback callback=nullptr)
Defines a property with a default value and optional callback.
virtual void serializeYosys(SILICON::yosys::SerializationContext &context) const
Lower this component into native Yosys netlist cells.
void notifyIOListeners()
Notifies all registered I/O listeners of a change.
std::unordered_map< uint64_t, IOObserver > ioListeners
Map of I/O change listeners indexed by ID.
void setPropertyValue(std::string_view key, T &&value)
Sets a property value with type deduction.
void handleInputSizeChange(const unsigned int index, const unsigned int newSize)
Handles input size changes. By default input size shouldn't change automatically.
std::size_t outputBusSize(unsigned int output) const
Wire_ptr inputWire(Enum input, unsigned short bit=0) const
std::vector< Bus > getInputs() const
Gets all input buses.
void validatePropertyValue(std::string_view key, const PropertyValue ¤tValue, const PropertyValue &newValue) const
Validates a property value against the declared type and metadata.
static State safeGetCurrentState(const std::weak_ptr< Wire > &w)
Executes event-driven simulation of a Circuit.
Mutable module-building interface passed to component serializers.
Concept to check if a type has a Type static member.
std::shared_ptr< Wire > Wire_ptr
std::function< PropertyValue(const PropertyValue &)> PropertyCallback
Callback function for property validation/transformation.
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
PropertyValue requireValidSize(const std::string_view name, const PropertyValue &value)
Validates that an integer property value is a valid bus size.
PropertyValue requireNonNegative(const std::string_view name, const PropertyValue &value)
Validates that an integer property value is non-negative.
std::unordered_map< std::string, PropertyValue, TransparentStringHash, TransparentStringEqual > PropertyMap
Map from property names to their values.
std::vector< std::string > StringPropertyOptions
Allowed values for a constrained string property.
PortRole
Declares whether a component represents a public circuit boundary port.
std::unordered_map< std::string, PropertyCallback, TransparentStringHash, TransparentStringEqual > PropertyCallbackMap
Map from property names to their callback functions.
std::unordered_map< std::string, StringPropertyOptions, TransparentStringHash, TransparentStringEqual > StringPropertyOptionsMap
Map from property names to constrained string values.
Transparent equality functor for string-like keys.
Transparent hash functor for string-like keys.
Context describing why a component is being evaluated.
bool changed(const core::Wire_ptr &wire) const
Returns true when a wire has a captured previous state different from now.
std::unordered_map< uint64_t, core::State > previousWireStates
Wire states captured before the reactive mutation that caused this pass.
bool initialEvaluation
True for initial or otherwise full-plan evaluation.
std::optional< core::State > previousState(const core::Wire_ptr &wire) const
Returns the state a wire had before this reactive evaluation, if captured.
std::span< const core::Bus > changedBuses
Buses that caused a reactive forward-cone evaluation.