SILICON
Loading...
Searching...
No Matches
circuit.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 <cstdint>
21#include <functional>
22#include <memory>
23#include <optional>
24#include <set>
25#include <string>
26#include <unordered_map>
27#include <utility>
28#include <vector>
29
30#include <boost/graph/adjacency_list.hpp>
31
32#include <core/wire.hpp>
33
34namespace SILICON::core {
35
37enum class PortRole;
38
39} // namespace SILICON::core
40
41namespace SILICON::core {
42
43class CircuitResolver;
44
50
55};
56
67
70
72 unsigned int sourceBusIndex = 0;
73
75 unsigned int targetBusIndex = 0;
76
79};
80
90 boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperty,
92
94using VertexDescriptor = boost::graph_traits<CircuitGraph>::vertex_descriptor;
95
97using EdgeDescriptor = boost::graph_traits<CircuitGraph>::edge_descriptor;
98
101 std::string name;
107 std::optional<VertexDescriptor> boundaryVertex = std::nullopt;
109 uint64_t id = 0;
110};
111
125class Circuit {
126private:
129
131 std::unordered_map<const Component*, VertexDescriptor> componentToVertex;
132
134 std::unordered_map<uint64_t, std::vector<Component_ptr>> wireListeners;
135
137 std::vector<Component_ptr> ownedComponents;
138
140 std::vector<Wire_ptr> ownedWires;
141
148
159
168 std::pair<std::vector<Bus>, std::vector<Bus>> getComponentIOs() const;
169 [[nodiscard]] std::vector<CircuitPort> interfacePorts(PortRole role) const;
170
176 void addComponentRecursive(const Component_ptr& component,
177 std::vector<VertexDescriptor>& newlyAdded);
178
185 [[nodiscard]] std::vector<VertexDescriptor> topologicalOrder() const;
186
187public:
188 struct SimulationBlock;
189
190 Circuit() = default;
191
197 explicit Circuit(const Component_set& components, bool explore = false);
198
205 explicit Circuit(const std::vector<Component_ptr>& components, bool explore = false);
206
212 explicit Circuit(const Component_ptr& component, bool explore = true);
213
221 void addComponent(const Component_ptr& component);
222
231 void removeComponent(const Component_ptr& component);
232
238 [[nodiscard]] std::optional<VertexDescriptor>
239 getVertexId(const Component* component) const;
240
245 [[nodiscard]] const std::unordered_map<const Component*, VertexDescriptor>&
247 {
248 return componentToVertex;
249 }
250
256 [[nodiscard]] Component_ptr getComponentByVertexId(VertexDescriptor vertexId) const;
257
264 void buildTopologyMap();
265
274 [[nodiscard]] std::vector<Bus> getInputs() const;
275
277 [[nodiscard]] std::vector<CircuitPort> getInputPorts() const;
278
287 [[nodiscard]] std::vector<Bus> getOutputs() const;
288
290 [[nodiscard]] std::vector<CircuitPort> getOutputPorts() const;
291
301 [[nodiscard]] Component_set getComponentsForBus(Bus b) const;
302
312 [[nodiscard]] std::vector<CircuitConnection> getConnections() const;
313
320 [[nodiscard]] std::vector<Component_weakPtr> getListenersForWire(uint64_t wireId) const;
321
332 [[nodiscard]] Circuit getBackwardsSubgraph(const Bus& targetOutput) const;
333
343 [[nodiscard]] Circuit getForwardSubgraph(const Bus& sourceInput) const;
344
349 [[nodiscard]] const CircuitGraph& getGraph() const { return graph; }
350
360 [[nodiscard]] std::vector<SimulationBlock> splitCyclic() const;
361
362 using LevelMap =
363 std::map<Component_weakPtr, unsigned int, std::owner_less<Component_weakPtr>>;
364
366 [[nodiscard]] Circuit::LevelMap getLevelMap() const;
367
392 [[nodiscard]] std::string serialize() const;
393
405 [[nodiscard]] static Circuit deserialize(const std::string& jsonStr,
406 const ComponentRegistry& reg,
407 const CircuitResolver* resolver = nullptr);
408};
409
414
417
419 std::vector<Component_weakPtr> executionOrder;
420};
421
422} // namespace SILICON::core
Represents a digital circuit as a directed graph of components.
Definition circuit.hpp:125
VertexDescriptor getOrAddVertex(const Component_ptr &component)
Gets or adds a vertex for a component.
Definition circuit.cpp:122
static Circuit deserialize(const std::string &jsonStr, const ComponentRegistry &reg, const CircuitResolver *resolver=nullptr)
Deserializes the core circuit model from a JSON string.
Definition circuit.cpp:749
std::optional< VertexDescriptor > getVertexId(const Component *component) const
Gets the vertex ID for a component.
Definition circuit.cpp:330
std::pair< std::vector< Bus >, std::vector< Bus > > getComponentIOs() const
Collects all unique input and output buses from every component in the circuit.
Definition circuit.cpp:177
Circuit getBackwardsSubgraph(const Bus &targetOutput) const
Extracts the cone of influence (COI) for a target output bus.
Definition circuit.cpp:464
std::vector< Component_weakPtr > getListenersForWire(uint64_t wireId) const
Gets all components listening to changes on a specific wire.
Definition circuit.cpp:223
Component_ptr getComponentByVertexId(VertexDescriptor vertexId) const
Gets a component by its vertex ID.
Definition circuit.cpp:113
std::vector< VertexDescriptor > topologicalOrder() const
Computes a valid execution order for all vertices in the circuit.
Definition circuit.cpp:644
std::map< Component_weakPtr, unsigned int, std::owner_less< Component_weakPtr > > LevelMap
Definition circuit.hpp:362
std::vector< CircuitPort > getOutputPorts() const
Gets named output ports using boundary names or deterministic fallbacks.
Definition circuit.cpp:385
void buildTopologyMap()
Builds a reverse mapping from wires to components that use them.
Definition circuit.cpp:205
const CircuitGraph & getGraph() const
Gets the underlying circuit graph.
Definition circuit.hpp:349
const std::unordered_map< const Component *, VertexDescriptor > & getComponentToVertex() const
Gets all component-to-vertex mappings.
Definition circuit.hpp:246
Circuit getForwardSubgraph(const Bus &sourceInput) const
Extracts the forward cone of influence for a source input bus.
Definition circuit.cpp:514
std::vector< SimulationBlock > splitCyclic() const
Splits the circuit into simulation blocks separating cyclic from acyclic parts.
Definition circuit.cpp:557
std::string serialize() const
Serializes the core circuit model to a JSON string.
Definition circuit.cpp:694
std::vector< CircuitPort > interfacePorts(PortRole role) const
Definition circuit.cpp:353
Circuit::LevelMap getLevelMap() const
Get a levelized map between the components and their level number.
Definition circuit.cpp:658
std::vector< Bus > getOutputs() const
Gets all output buses of the circuit.
Definition circuit.cpp:378
void addComponent(const Component_ptr &component)
Adds a component to the circuit recursively.
Definition circuit.cpp:294
void addComponentRecursive(const Component_ptr &component, std::vector< VertexDescriptor > &newlyAdded)
Recursively adds a component and all connected components.
Definition circuit.cpp:278
void rebuildEdges(VertexDescriptor v)
Rebuilds directed edges in the graph based on wire connections.
Definition circuit.cpp:139
std::vector< CircuitPort > getInputPorts() const
Gets named input ports using boundary names or deterministic fallbacks.
Definition circuit.cpp:348
std::unordered_map< uint64_t, std::vector< Component_ptr > > wireListeners
Maps wire IDs to components that listen to changes on that wire.
Definition circuit.hpp:134
std::vector< Wire_ptr > ownedWires
Wires owned by this circuit (for deserialization).
Definition circuit.hpp:140
CircuitGraph graph
The Boost graph representing the circuit topology.
Definition circuit.hpp:128
Component_set getComponentsForBus(Bus b) const
Finds all components that are connected to a given bus.
Definition circuit.cpp:390
std::vector< CircuitConnection > getConnections() const
Gets concrete output-to-input logical connections for routing.
Definition circuit.cpp:423
void removeComponent(const Component_ptr &component)
Removes a component from the circuit.
Definition circuit.cpp:307
std::vector< Component_ptr > ownedComponents
Components owned by this circuit (for deserialization).
Definition circuit.hpp:137
std::unordered_map< const Component *, VertexDescriptor > componentToVertex
Maps components to their vertex descriptors in the graph.
Definition circuit.hpp:131
std::vector< Bus > getInputs() const
Gets all input buses of the circuit.
Definition circuit.cpp:341
Base class for all digital circuit components.
std::shared_ptr< Component > Component_ptr
boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperty, EdgeProperty > CircuitGraph
Boost graph type representing the circuit topology.
Definition circuit.hpp:89
boost::graph_traits< CircuitGraph >::vertex_descriptor VertexDescriptor
Descriptor type for vertices in the circuit graph.
Definition circuit.hpp:94
std::set< Component_ptr, std::owner_less< Component_ptr > > Component_set
Definition wire.hpp:54
boost::graph_traits< CircuitGraph >::edge_descriptor EdgeDescriptor
Descriptor type for edges in the circuit graph.
Definition circuit.hpp:97
PortRole
Declares whether a component represents a public circuit boundary port.
Definition component.hpp:66
Directed logical connection between one component output bus and one input bus.
Definition circuit.hpp:64
unsigned int sourceBusIndex
Output bus index on source.
Definition circuit.hpp:72
Component_ptr target
Component that owns the input bus.
Definition circuit.hpp:69
Bus bus
Logical bus carried by this connection.
Definition circuit.hpp:78
Component_ptr source
Component that owns the output bus.
Definition circuit.hpp:66
unsigned int targetBusIndex
Input bus index on target.
Definition circuit.hpp:75
Named bus exposed by a circuit boundary component or topology fallback.
Definition circuit.hpp:100
std::optional< VertexDescriptor > boundaryVertex
Definition circuit.hpp:107
Represents a block of components for simulation execution.
Definition circuit.hpp:411
std::vector< Component_weakPtr > executionOrder
Pre-computed execution order for acyclic blocks.
Definition circuit.hpp:419
Circuit circuit
The sub-circuit containing the components in this block.
Definition circuit.hpp:416
bool isCyclic
Whether this block contains cyclic dependencies.
Definition circuit.hpp:413
Property stored in each edge of the circuit graph.
Definition circuit.hpp:52
Bus bus
The bus of wires this edge represents.
Definition circuit.hpp:54
Property stored in each vertex of the circuit graph.
Definition circuit.hpp:46
Component_ptr component
The component this vertex represents.
Definition circuit.hpp:48