SILICON
Loading...
Searching...
No Matches
Circuit Class Reference

Represents a digital circuit as a directed graph of components. More...

#include <circuit.hpp>

Classes

struct  SimulationBlock
 Represents a block of components for simulation execution. More...

Public Types

using LevelMap

Public Member Functions

 Circuit ()=default
 Circuit (const Component_set &components, bool explore=false)
 Constructs a circuit from a set of components.
 Circuit (const std::vector< Component_ptr > &components, bool explore=false)
 Constructs a circuit preserving the supplied component and boundary port order.
 Circuit (const Component_ptr &component, bool explore=true)
 Constructs a circuit from a single component.
void addComponent (const Component_ptr &component)
 Adds a component to the circuit recursively.
void removeComponent (const Component_ptr &component)
 Removes a component from the circuit.
std::optional< VertexDescriptor > getVertexId (const Component *component) const
 Gets the vertex ID for a component.
const std::unordered_map< const Component *, VertexDescriptor > & getComponentToVertex () const
 Gets all component-to-vertex mappings.
Component_ptr getComponentByVertexId (VertexDescriptor vertexId) const
 Gets a component by its vertex ID.
void buildTopologyMap ()
 Builds a reverse mapping from wires to components that use them.
std::vector< Bus > getInputs () const
 Gets all input buses of the circuit.
std::vector< CircuitPort > getInputPorts () const
 Gets named input ports using boundary names or deterministic fallbacks.
std::vector< Bus > getOutputs () const
 Gets all output buses of the circuit.
std::vector< CircuitPort > getOutputPorts () const
 Gets named output ports using boundary names or deterministic fallbacks.
Component_set getComponentsForBus (Bus b) const
 Finds all components that are connected to a given bus.
std::vector< CircuitConnection > getConnections () const
 Gets concrete output-to-input logical connections for routing.
std::vector< Component_weakPtr > getListenersForWire (uint64_t wireId) const
 Gets all components listening to changes on a specific wire.
Circuit getBackwardsSubgraph (const Bus &targetOutput) const
 Extracts the cone of influence (COI) for a target output bus.
Circuit getForwardSubgraph (const Bus &sourceInput) const
 Extracts the forward cone of influence for a source input bus.
const CircuitGraph & getGraph () const
 Gets the underlying circuit graph.
std::vector< SimulationBlock > splitCyclic () const
 Splits the circuit into simulation blocks separating cyclic from acyclic parts.
Circuit::LevelMap getLevelMap () const
 Get a levelized map between the components and their level number.
std::string serialize () const
 Serializes the core circuit model to a JSON string.

Static Public Member Functions

static Circuit deserialize (const std::string &jsonStr, const ComponentRegistry &reg, const CircuitResolver *resolver=nullptr)
 Deserializes the core circuit model from a JSON string.

Private Member Functions

VertexDescriptor getOrAddVertex (const Component_ptr &component)
 Gets or adds a vertex for a component.
void rebuildEdges (VertexDescriptor v)
 Rebuilds directed edges in the graph based on wire connections.
std::pair< std::vector< Bus >, std::vector< Bus > > getComponentIOs () const
 Collects all unique input and output buses from every component in the circuit.
std::vector< CircuitPort > interfacePorts (PortRole role) const
void addComponentRecursive (const Component_ptr &component, std::vector< VertexDescriptor > &newlyAdded)
 Recursively adds a component and all connected components.
std::vector< VertexDescriptor > topologicalOrder () const
 Computes a valid execution order for all vertices in the circuit.

Private Attributes

CircuitGraph graph
 The Boost graph representing the circuit topology.
std::unordered_map< const Component *, VertexDescriptor > componentToVertex
 Maps components to their vertex descriptors in the graph.
std::unordered_map< uint64_t, std::vector< Component_ptr > > wireListeners
 Maps wire IDs to components that listen to changes on that wire.
std::vector< Component_ptr > ownedComponents
 Components owned by this circuit (for deserialization).
std::vector< Wire_ptr > ownedWires
 Wires owned by this circuit (for deserialization).

Detailed Description

Represents a digital circuit as a directed graph of components.

The Circuit class manages a collection of components and their connections through wires. It uses a Boost Graph-based internal representation to track component relationships and enable operations like topological sorting, subgraph extraction, and reactive simulation.

Note
The circuit tracks connectivity through shared wires between component input/output buses. Reconstruct the graph after rewiring components; a circuit passed to a Simulator must retain its topology for that simulator's lifetime.

Definition at line 125 of file circuit.hpp.

Member Typedef Documentation

◆ LevelMap

using LevelMap
Initial value:
std::map<Component_weakPtr, unsigned int, std::owner_less<Component_weakPtr>>

Definition at line 362 of file circuit.hpp.

Constructor & Destructor Documentation

◆ Circuit() [1/4]

Circuit ( )
default

◆ Circuit() [2/4]

Circuit ( const Component_set & components,
bool explore = false )
explicit

Constructs a circuit from a set of components.

Parameters
componentsThe set of components to include
exploreIf true, recursively explore and add connected components

Definition at line 242 of file circuit.cpp.

◆ Circuit() [3/4]

Circuit ( const std::vector< Component_ptr > & components,
bool explore = false )
explicit

Constructs a circuit preserving the supplied component and boundary port order.

Parameters
componentsThe ordered components to include
exploreIf true, recursively explore and add connected components

Definition at line 247 of file circuit.cpp.

◆ Circuit() [4/4]

Circuit ( const Component_ptr & component,
bool explore = true )
explicit

Constructs a circuit from a single component.

Parameters
componentThe root component to add
exploreIf true, recursively explore and add connected components

Definition at line 265 of file circuit.cpp.

Member Function Documentation

◆ addComponent()

void addComponent ( const Component_ptr & component)

Adds a component to the circuit recursively.

Adds the component and all components reachable through shared buses.

Parameters
componentThe component to add

Definition at line 294 of file circuit.cpp.

◆ addComponentRecursive()

void addComponentRecursive ( const Component_ptr & component,
std::vector< VertexDescriptor > & newlyAdded )
private

Recursively adds a component and all connected components.

Parameters
componentThe component to add
newlyAddedVector to collect newly added vertex descriptors

Definition at line 278 of file circuit.cpp.

◆ buildTopologyMap()

void buildTopologyMap ( )

Builds a reverse mapping from wires to components that use them.

This enables O(1) lookup of components connected to a specific wire, used for subgraph extraction and reactive updates.

Definition at line 205 of file circuit.cpp.

◆ deserialize()

Circuit deserialize ( const std::string & jsonStr,
const ComponentRegistry & reg,
const CircuitResolver * resolver = nullptr )
staticnodiscard

Deserializes the core circuit model from a JSON string.

Expects the payload documented in core_serialization_format. The registry is used to recreate component instances by serialized type name.

Parameters
jsonStrThe JSON string to deserialize
regThe component registry to create components
resolverOptional non-owning resolver that must outlive the returned circuit.
Returns
The deserialized circuit

Definition at line 749 of file circuit.cpp.

◆ getBackwardsSubgraph()

Circuit getBackwardsSubgraph ( const Bus & targetOutput) const
nodiscard

Extracts the cone of influence (COI) for a target output bus.

Returns a new Circuit containing all components that can affect the target bus through dataflow dependencies (reading from sources that eventually drive the target).

Parameters
targetOutputThe output bus to get the backwards subgraph for
Returns
A new Circuit containing all affecting components

Definition at line 464 of file circuit.cpp.

◆ getComponentByVertexId()

Component_ptr getComponentByVertexId ( VertexDescriptor vertexId) const
nodiscard

Gets a component by its vertex ID.

Parameters
vertexIdThe vertex ID
Returns
Component pointer, or nullptr if not found

Definition at line 113 of file circuit.cpp.

◆ getComponentIOs()

std::pair< std::vector< Bus >, std::vector< Bus > > getComponentIOs ( ) const
private

Collects all unique input and output buses from every component in the circuit.

Used to determine circuit interface.

Returns
A pair of vectors: first is input buses, second is output buses

Definition at line 177 of file circuit.cpp.

◆ getComponentsForBus()

Component_set getComponentsForBus ( Bus b) const
nodiscard

Finds all components that are connected to a given bus.

A component is connected if any of its input or output buses share wires with the target bus.

Parameters
bThe bus to find components for
Returns
Set of components connected to the bus

Definition at line 390 of file circuit.cpp.

◆ getComponentToVertex()

const std::unordered_map< const Component *, VertexDescriptor > & getComponentToVertex ( ) const
inlinenodiscard

Gets all component-to-vertex mappings.

Returns
Map from component pointers to vertex IDs

Definition at line 246 of file circuit.hpp.

◆ getConnections()

std::vector< CircuitConnection > getConnections ( ) const
nodiscard

Gets concrete output-to-input logical connections for routing.

Each returned item represents one source output bus connected to one target input bus. Fan-out is expanded into multiple connections, while multi-wire buses are deduplicated so each source/target/bus-index tuple appears once.

Returns
Vector of directed logical connections

Definition at line 423 of file circuit.cpp.

◆ getForwardSubgraph()

Circuit getForwardSubgraph ( const Bus & sourceInput) const
nodiscard

Extracts the forward cone of influence for a source input bus.

Returns a new Circuit containing all components that can be affected by changes to the source bus (directly or indirectly through dataflow).

Parameters
sourceInputThe input bus to get the forward subgraph for
Returns
A new Circuit containing all affected components

Definition at line 514 of file circuit.cpp.

◆ getGraph()

const CircuitGraph & getGraph ( ) const
inlinenodiscard

Gets the underlying circuit graph.

Returns
Reference to the Boost graph

Definition at line 349 of file circuit.hpp.

◆ getInputPorts()

std::vector< CircuitPort > getInputPorts ( ) const
nodiscard

Gets named input ports using boundary names or deterministic fallbacks.

Definition at line 348 of file circuit.cpp.

◆ getInputs()

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

Gets all input buses of the circuit.

Boundary components with PortRole::Input define the interface when present. Otherwise topology-derived inputs are returned.

Returns
Vector of input buses

Definition at line 341 of file circuit.cpp.

◆ getLevelMap()

Circuit::LevelMap getLevelMap ( ) const
nodiscard

Get a levelized map between the components and their level number.

Definition at line 658 of file circuit.cpp.

◆ getListenersForWire()

std::vector< Component_weakPtr > getListenersForWire ( uint64_t wireId) const
nodiscard

Gets all components listening to changes on a specific wire.

Parameters
wireIdThe ID of the wire
Returns
Vector of weak pointers to components

Definition at line 223 of file circuit.cpp.

◆ getOrAddVertex()

VertexDescriptor getOrAddVertex ( const Component_ptr & component)
private

Gets or adds a vertex for a component.

Parameters
componentThe component to get or add
Returns
The vertex descriptor

Definition at line 122 of file circuit.cpp.

◆ getOutputPorts()

std::vector< CircuitPort > getOutputPorts ( ) const
nodiscard

Gets named output ports using boundary names or deterministic fallbacks.

Definition at line 385 of file circuit.cpp.

◆ getOutputs()

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

Gets all output buses of the circuit.

Boundary components with PortRole::Output define the interface when present. Otherwise topology-derived outputs are returned.

Returns
Vector of output buses

Definition at line 378 of file circuit.cpp.

◆ getVertexId()

std::optional< VertexDescriptor > getVertexId ( const Component * component) const
nodiscard

Gets the vertex ID for a component.

Parameters
componentThe component
Returns
Vertex ID, or -1 if not found

Definition at line 330 of file circuit.cpp.

◆ interfacePorts()

std::vector< CircuitPort > interfacePorts ( PortRole role) const
nodiscardprivate

Definition at line 353 of file circuit.cpp.

◆ rebuildEdges()

void rebuildEdges ( VertexDescriptor v)
private

Rebuilds directed edges in the graph based on wire connections.

A directed edge (u -> v) is added when component u's output bus shares at least one wire with component v's input bus. This creates the dataflow graph used for topological sorting and subgraph extraction.

Parameters
vThe vertex whose edges should be rebuilt

Definition at line 139 of file circuit.cpp.

◆ removeComponent()

void removeComponent ( const Component_ptr & component)

Removes a component from the circuit.

Removes the vertex and all incident edges from the graph, and removes the component from componentToVertex map.

Parameters
componentThe component to remove

Definition at line 307 of file circuit.cpp.

◆ serialize()

std::string serialize ( ) const
nodiscard

Serializes the core circuit model to a JSON string.

This is the canonical serialization for the logical circuit only. It stores the component graph, not the editor layout.

The payload contains:

  • version
  • components[]
  • for each component: its serialized graph id, registered type, properties, and inputs / outputs bus wiring

Wire connectivity is represented by shared wire IDs inside the serialized bus lists. During deserialization, components are reinserted in serialized id order so their Boost vertex descriptors match the saved graph IDs again.

This format intentionally does not store any UI state such as graphical positions, rotations, wire segment geometry, or runtime scene item IDs. Those belong to the DiagramScene serialization format documented in diagramscene_serialization_format.

Returns
JSON string representation of the logical circuit model

Definition at line 694 of file circuit.cpp.

◆ splitCyclic()

std::vector< Circuit::SimulationBlock > splitCyclic ( ) const
nodiscard

Splits the circuit into simulation blocks separating cyclic from acyclic parts.

This is essential for proper simulation: acyclic parts execute once in topological order, while cyclic parts require iterative delta cycles to converge.

Returns
Vector of SimulationBlocks ordered for proper execution

Definition at line 557 of file circuit.cpp.

◆ topologicalOrder()

std::vector< VertexDescriptor > topologicalOrder ( ) const
nodiscardprivate

Computes a valid execution order for all vertices in the circuit.

Note
Uses Boost's topological_sort algorithm which requires a DAG (Directed Acyclic Graph).

Definition at line 644 of file circuit.cpp.

Member Data Documentation

◆ componentToVertex

std::unordered_map<const Component*, VertexDescriptor> componentToVertex
private

Maps components to their vertex descriptors in the graph.

Definition at line 131 of file circuit.hpp.

◆ graph

CircuitGraph graph
private

The Boost graph representing the circuit topology.

Definition at line 128 of file circuit.hpp.

◆ ownedComponents

std::vector<Component_ptr> ownedComponents
private

Components owned by this circuit (for deserialization).

Definition at line 137 of file circuit.hpp.

◆ ownedWires

std::vector<Wire_ptr> ownedWires
private

Wires owned by this circuit (for deserialization).

Definition at line 140 of file circuit.hpp.

◆ wireListeners

std::unordered_map<uint64_t, std::vector<Component_ptr> > wireListeners
private

Maps wire IDs to components that listen to changes on that wire.

Definition at line 134 of file circuit.hpp.


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