SILICON
Loading...
Searching...
No Matches
netlist.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 <cstddef>
21#include <cstdint>
22#include <optional>
23#include <string>
24#include <string_view>
25#include <unordered_map>
26#include <vector>
27
28#include <boost/graph/adjacency_list.hpp>
29
30#include <nlohmann/json.hpp>
31
32#include <core/wire.hpp>
34
35namespace SILICON::core {
36class Circuit;
37class CircuitResolver;
38class Component;
39} // namespace SILICON::core
40
41namespace SILICON::yosys {
42
43
44using Json = nlohmann::ordered_json;
45
59public:
61 using ModuleNameList = std::vector<std::string>;
62
65 std::string name;
66 };
67
69 using Graph =
70 boost::adjacency_list<boost::setS, boost::vecS, boost::directedS, VertexProperty>;
72 using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
73
76 std::unordered_map<std::string, Vertex, core::TransparentStringHash,
78
84 void addModule(std::string_view moduleName);
85
95 void addDependency(std::string_view from, std::string_view to);
96
104 [[nodiscard]] ModuleNameList dependenciesOf(std::string_view moduleName) const;
105
115 [[nodiscard]] ModuleNameList dependencyOrder(const ModuleNameList& roots) const;
116
122 [[nodiscard]] ModuleNameList modules() const;
123
130 [[nodiscard]] bool containsModule(std::string_view moduleName) const;
131
133 [[nodiscard]] const Graph& boost() const { return graph; }
134
135private:
140
141 [[nodiscard]] std::optional<Vertex> findVertex(std::string_view moduleName) const;
142
149 Vertex ensureVertex(std::string_view moduleName);
150
156 void rebuildNameIndex();
157};
158
165[[nodiscard]] ModuleDependencyGraph moduleDependencyGraph(std::string_view json);
166
175public:
176 struct Impl;
177
179 [[nodiscard]] Json bits(const core::Bus& bus, std::string_view nullValue = "x") const;
180
187 [[nodiscard]] Json inputBits(const core::Component& component, std::size_t index,
188 std::size_t expectedWidth) const;
189
191 [[nodiscard]] Json allocateBits(std::size_t width);
192
194 [[nodiscard]] static Json concatenate(const std::vector<Json>& vectors);
195
197 [[nodiscard]] static std::string parameter(std::uint64_t value, std::size_t width = 32);
198
200 [[nodiscard]] std::string componentIdentifier() const;
201
203 void addCell(std::string_view suffix, std::string_view type, Json parameters,
204 Json portDirections, Json connections, Json attributes = Json::object());
205
207 void addPort(std::string name, std::string_view direction, const core::Bus& bus);
208
210 void addSubcircuitInstance(std::string_view slug, const std::vector<core::Bus>& inputs,
211 const std::vector<core::Bus>& outputs,
212 const core::CircuitResolver* resolver);
213
216
217private:
219
220 friend std::string serialize(const core::Circuit& circuit, std::string_view moduleName);
221};
222
231[[nodiscard]] std::string serialize(const core::Circuit& circuit,
232 std::string_view moduleName);
233
243[[nodiscard]] core::Circuit
244deserialize(std::string_view json,
245 std::optional<std::string_view> moduleName = std::nullopt);
246
247} // namespace SILICON::yosys
Represents a digital circuit as a directed graph of components.
Definition circuit.hpp:125
Base class for all digital circuit components.
Module-instantiation dependency graph for a Yosys JSON design.
Definition netlist.hpp:58
std::vector< std::string > ModuleNameList
List of module names.
Definition netlist.hpp:61
NameToVertex nameToVertex
Name-to-vertex index kept in sync with graph.
Definition netlist.hpp:139
void rebuildNameIndex()
Rebuilds nameToVertex from the current graph.
Graph graph
The dependency graph itself.
Definition netlist.hpp:137
bool containsModule(std::string_view moduleName) const
Checks whether a module vertex is present in the graph.
boost::adjacency_list< boost::setS, boost::vecS, boost::directedS, VertexProperty > Graph
Boost graph type: set out-edges, vector vertices, directed.
Definition netlist.hpp:69
ModuleNameList dependencyOrder(const ModuleNameList &roots) const
Returns the unique dependency closure of roots in dependency-first order.
std::unordered_map< std::string, Vertex, core::TransparentStringHash, core::TransparentStringEqual > NameToVertex
O(1) index mapping a module name to its graph vertex.
Definition netlist.hpp:75
const Graph & boost() const
Read-only access to the underlying Boost graph.
Definition netlist.hpp:133
void addDependency(std::string_view from, std::string_view to)
Records that from instantiates to.
ModuleNameList dependenciesOf(std::string_view moduleName) const
Returns the sorted, unique modules moduleName instantiates.
Vertex ensureVertex(std::string_view moduleName)
Ensures a module vertex exists in this graph (O(1)).
boost::graph_traits< Graph >::vertex_descriptor Vertex
Boost vertex descriptor (an integer index under vecS).
Definition netlist.hpp:72
void addModule(std::string_view moduleName)
Ensures a module vertex exists, creating it if necessary.
ModuleNameList modules() const
Returns every declared module name.
std::optional< Vertex > findVertex(std::string_view moduleName) const
void addPort(std::string name, std::string_view direction, const core::Bus &bus)
Register a named top-level input or output port.
Definition netlist.cpp:402
void addSubcircuitInstance(std::string_view slug, const std::vector< core::Bus > &inputs, const std::vector< core::Bus > &outputs, const core::CircuitResolver *resolver)
Emit a hierarchical instance and recursively serialize its definition.
Definition netlist.cpp:415
Json bits(const core::Bus &bus, std::string_view nullValue="x") const
Convert a Silicon bus to a Yosys least-significant-bit-first vector.
Definition netlist.cpp:276
Json allocateBits(std::size_t width)
Allocate module-local temporary signal bits.
Definition netlist.cpp:314
friend std::string serialize(const core::Circuit &circuit, std::string_view moduleName)
Serialize a complete Silicon circuit as Yosys write_json-compatible JSON.
Definition netlist.cpp:443
static std::string parameter(std::uint64_t value, std::size_t width=32)
Encode an unsigned Yosys parameter as a fixed-width binary string.
Definition netlist.cpp:331
std::string componentIdentifier() const
Stable name of the component being serialized within its module.
Definition netlist.cpp:341
Json inputBits(const core::Component &component, std::size_t index, std::size_t expectedWidth) const
Encode a component input, applying its declared unconnected-input default.
Definition netlist.cpp:290
void addCell(std::string_view suffix, std::string_view type, Json parameters, Json portDirections, Json connections, Json attributes=Json::object())
Emit one native Yosys cell for the current Silicon component.
Definition netlist.cpp:346
static Json concatenate(const std::vector< Json > &vectors)
Concatenate bit vectors in their existing LSB-first order.
Definition netlist.cpp:322
Circuit deserialize(const std::string_view json, const std::optional< std::string_view > moduleName)
Deserialize one supported Yosys write_json module into a Silicon circuit.
ModuleDependencyGraph moduleDependencyGraph(const std::string_view json)
Build a module dependency graph from a Yosys write_json document.
std::string serialize(const Circuit &circuit, const std::string_view moduleName)
Serialize a complete Silicon circuit as Yosys write_json-compatible JSON.
Definition netlist.cpp:443
nlohmann::ordered_json Json
Definition netlist.hpp:44
Transparent equality functor for string-like keys.
Transparent hash functor for string-like keys.
Per-vertex payload holding the declared module name.
Definition netlist.hpp:64