SILICON
Loading...
Searching...
No Matches
subcircuit.cpp
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#include "subcircuit.hpp"
19
20#include <algorithm>
21#include <ranges>
22#include <stdexcept>
23#include <unordered_set>
24#include <vector>
25
28
29namespace SILICON::core {
30
31namespace {
32
33 [[nodiscard]] std::vector<uint64_t> portIds(const std::vector<CircuitPort>& ports)
34 {
35 if (std::ranges::any_of(ports, [](const CircuitPort& port) { return port.id == 0; }))
36 return {}; // Topology-only interfaces have no persistent identity.
37
38 return ports | std::views::transform(&CircuitPort::id)
39 | std::ranges::to<std::vector>();
40 }
41
42 void preserveConnection(Bus& target, const Bus& source)
43 {
44 auto wires = static_cast<std::vector<Wire_ptr>>(target);
45 const auto saved = static_cast<std::vector<Wire_ptr>>(source);
46 for (auto [destination, original] : std::views::zip(wires, saved))
47 destination = original;
48 target = Bus(std::move(wires));
49 }
50
51 [[nodiscard]] std::vector<Bus>
52 remapConnections(const std::vector<Bus>& expected,
53 const std::vector<uint64_t>& expectedIds,
54 const std::vector<Bus>& saved, const std::vector<uint64_t>& savedIds)
55 {
56 if (!savedIds.empty()) {
57 std::unordered_set<uint64_t> seen;
58 if (savedIds.size() != saved.size())
59 throw std::runtime_error(
60 "Subcircuit port identity count does not match its buses");
61 for (const auto& id : savedIds)
62 if (id == 0 || !seen.insert(id).second)
63 throw std::runtime_error(
64 "Subcircuit port identities must be nonzero and unique");
65 }
66
67 // Start with the current interface widths and fresh wires. Reuse only the
68 // overlapping bits from a matching saved port; any added bits stay fresh.
69 auto result = expected;
70 for (std::size_t portIndex = 0; portIndex < result.size(); ++portIndex) {
71 auto savedPortIndex = portIndex;
72 if (!savedIds.empty()) {
73 if (portIndex >= expectedIds.size())
74 continue; // An implicit topology port cannot inherit a removed boundary ID.
75 const auto match = std::ranges::find(savedIds, expectedIds.at(portIndex));
76 if (match == savedIds.end())
77 continue;
78 savedPortIndex = static_cast<std::size_t>(match - savedIds.begin());
79 }
80 if (savedPortIndex >= saved.size())
81 continue;
82 preserveConnection(result[portIndex], saved[savedPortIndex]);
83 }
84 return result;
85 }
86
87 [[nodiscard]] std::vector<Bus> externalBuses(const std::vector<CircuitPort>& ports)
88 {
89 return ports | std::views::transform([](const CircuitPort& port) {
90 return Bus(static_cast<unsigned short>(port.bus.size()));
91 })
92 | std::ranges::to<std::vector>();
93 }
94
95} // namespace
96
99{
100 defineProperty(std::string("slug"), std::string(), [this](const PropertyValue& value) {
101 configureFromSlug(std::get<std::string>(value));
102 return value;
103 });
104}
105
106std::shared_ptr<SubcircuitComponent> SubcircuitComponent::imported(
107 std::string slug, std::vector<std::string> inputNames, std::vector<Bus> inputs,
108 std::vector<std::string> outputNames, std::vector<Bus> outputs)
109{
110 auto component = std::make_shared<SubcircuitComponent>();
111 component->properties["slug"] = std::move(slug);
112 component->transientInputNames = std::move(inputNames);
113 component->transientOutputNames = std::move(outputNames);
114 component->setInputs(inputs);
115 component->setOutputs(outputs);
116 return component;
117}
118
120{
121 inputIds.clear();
122 outputIds.clear();
123 inputs.clear();
124 outputs.clear();
126}
127
128void SubcircuitComponent::configureFromSlug(std::string_view slug)
129{
130 if (slug.empty()) {
132 return;
133 }
134
135 if (!resolver) {
137 return;
138 }
139
140 auto definition = resolver->resolve(slug);
141 const bool sameDefinition = getPropertyValue<std::string>("slug") == slug;
142 auto newInputs = externalBuses(definition.inputs);
143 auto newOutputs = externalBuses(definition.outputs);
144 if (sameDefinition) {
145 newInputs = remapConnections(newInputs, portIds(definition.inputs), inputs, inputIds);
146 newOutputs =
147 remapConnections(newOutputs, portIds(definition.outputs), outputs, outputIds);
148 }
149 inputIds = portIds(definition.inputs);
150 outputIds = portIds(definition.outputs);
151 // Update both directions before notifying observers of the new interface.
152 inputs = std::move(newInputs);
153 outputs = std::move(newOutputs);
155}
156
157void SubcircuitComponent::restoreConnections(const std::vector<Bus>& savedInputs,
158 const std::vector<Bus>& savedOutputs,
159 const std::vector<uint64_t>& savedInputIds,
160 const std::vector<uint64_t>& savedOutputIds)
161{
162 auto newInputs = remapConnections(inputs, inputIds, savedInputs, savedInputIds);
163 auto newOutputs = remapConnections(outputs, outputIds, savedOutputs, savedOutputIds);
164 inputs = std::move(newInputs);
165 outputs = std::move(newOutputs);
167}
168
169std::vector<Bus>
170SubcircuitComponent::inputsForPorts(const std::vector<CircuitPort>& ports) const
171{
172 return remapConnections(externalBuses(ports), portIds(ports), inputs, inputIds);
173}
174
175std::vector<Bus>
176SubcircuitComponent::outputsForPorts(const std::vector<CircuitPort>& ports) const
177{
178 return remapConnections(externalBuses(ports), portIds(ports), outputs, outputIds);
179}
180
182{
183 if (resolver == newResolver)
184 return;
185 resolver = newResolver;
187}
188
190{
191 return resolver;
192}
193
198
200{
201 (void)sim;
202 throw std::logic_error("Unprocessed SubcircuitComponent reached "
203 "SILICON::simulation::Simulator; preprocess the circuit "
204 "with CircuitElaborator or Session before simulation");
205}
206
207} // namespace SILICON::core
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.
std::vector< Bus > inputs
Input buses connected to this component.
void defineProperty(std::string key, T &&defaultValue, PropertyCallback callback=nullptr)
Defines a property with a default value and optional callback.
void notifyIOListeners()
Notifies all registered I/O listeners of a change.
Definition component.cpp:72
const CircuitResolver * circuitResolver() const noexcept
void setCircuitResolver(const CircuitResolver *resolver)
SubcircuitComponent(const CircuitResolver *resolver=nullptr)
std::vector< Bus > outputsForPorts(const std::vector< CircuitPort > &ports) const
static std::shared_ptr< SubcircuitComponent > imported(std::string slug, std::vector< std::string > inputNames, std::vector< Bus > inputs, std::vector< std::string > outputNames, std::vector< Bus > outputs)
Creates an imported module instance before its project document exists.
std::vector< Bus > inputsForPorts(const std::vector< CircuitPort > &ports) const
void restoreConnections(const std::vector< Bus > &savedInputs, const std::vector< Bus > &savedOutputs, const std::vector< uint64_t > &savedInputIds, const std::vector< uint64_t > &savedOutputIds)
const CircuitResolver * resolver
std::vector< uint64_t > outputIds
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
std::vector< uint64_t > inputIds
void configureFromSlug(std::string_view slug)
Executes event-driven simulation of a Circuit.
Definition simulator.hpp:74
RTLIL::SigSpec port(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key, int expected)
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
Named bus exposed by a circuit boundary component or topology fallback.
Definition circuit.hpp:100