SILICON
Loading...
Searching...
No Matches
io.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 "io.hpp"
19
20#include <algorithm>
21#include <array>
22#include <atomic>
23#include <limits>
24#include <stdexcept>
25#include <utility>
26
27#include <core/simulator.hpp>
28#include <core/wireUtils.hpp>
30
31namespace SILICON::core {
32
33namespace {
34
35 std::atomic<uint64_t> nextPortIdentity{1};
36
37 constexpr std::string_view PORT_ORIENTATION_PROPERTY = "portOrientation";
38 constexpr std::array<std::string_view, 4> PORT_ORIENTATIONS = {"UP", "DOWN", "LEFT",
39 "RIGHT"};
40 [[nodiscard]] StringPropertyOptions orientationOptions()
41 {
43 options.reserve(PORT_ORIENTATIONS.size());
44 for (const auto orientation : PORT_ORIENTATIONS)
45 options.emplace_back(orientation);
46 return options;
47 }
48
49 [[nodiscard]] int normalizedBusSize(const int size)
50 {
51 return std::clamp(size, 1,
52 static_cast<int>(std::numeric_limits<unsigned short>::max()));
53 }
54
55 [[nodiscard]] BusValue normalizedEditableValue(const BusValue& value,
56 const std::size_t width,
57 const bool fillScalarUnknown)
58 {
59 if (value.empty())
60 throw std::invalid_argument("Bus value must not be empty");
61 if (std::ranges::contains(value, State::ERROR))
62 throw std::invalid_argument("State::ERROR cannot be used as an editable value");
63
64 const State extension =
65 fillScalarUnknown && value.size() == 1 && value.front() == State::UNKNOWN
67 : State::LOW;
68 return SILICON::wireUtils::normalizeBusValue(value, width, extension);
69 }
70
71} // namespace
72
74{
75 defineProperty("size", 1, [this](const PropertyValue& value) {
76 return setSize(std::get<int>(value));
77 });
78 defineProperty("value", BusValue{State::LOW}, [this](const PropertyValue& value) {
79 return setValue(std::get<BusValue>(value));
80 });
81}
82
84 : ConstantComponent(Bus{std::move(output)}, std::move(value))
85{
86}
87
89{
90 if (output.size() == 0)
91 throw std::invalid_argument("Constant output bus must not be empty");
92 outputs = {std::move(output)};
93 setProperty("size", static_cast<int>(outputs[0].size()));
94 setProperty("value", std::move(value));
95}
96
97int ConstantComponent::setSize(const int newSize)
98{
99 const int normalizedSize = std::clamp(
100 newSize, 1, static_cast<int>(std::numeric_limits<unsigned short>::max()));
101 if (!outputs.empty()) {
102 outputs[0].setSize(static_cast<unsigned short>(normalizedSize));
104 }
105 if (const auto value = getPropertyValue<BusValue>("value"))
106 setProperty("value", *value);
107 return normalizedSize;
108}
109
111{
112 const auto width =
113 outputs.empty()
114 ? static_cast<std::size_t>(getPropertyValue<int>("size").value_or(1))
115 : outputs[0].size();
116 return normalizedEditableValue(value, width, true);
117}
118
120{
121 if (outputs.empty())
122 return;
123
124 sim.updateBus(outputs[0],
125 getPropertyValue<BusValue>("value").value_or(
126 BusValue(outputs[0].size(), State::UNKNOWN)),
127 0, weak_from_this());
128}
129
131 std::vector<Bus> outputs, std::string name)
132 : Component(std::move(inputs), std::move(outputs))
133{
134 resetPortId();
135 defineProperty("name", std::move(name));
136 defineStringListProperty(std::string(PORT_ORIENTATION_PROPERTY),
137 this->outputs.empty() ? "LEFT" : "RIGHT",
138 orientationOptions());
139}
140
142{
143 if (id == 0 || id == std::numeric_limits<uint64_t>::max())
144 throw std::invalid_argument("Boundary port identity must be a nonzero integer");
145 // Identities only need to be unique within one circuit. Reserve loaded numeric
146 // IDs so newly created or pasted boundary ports cannot reuse them.
147 auto next = nextPortIdentity.load(std::memory_order_relaxed);
148 while (next <= id
149 && !nextPortIdentity.compare_exchange_weak(next, id + 1,
150 std::memory_order_relaxed)) {}
151 portIdentity = id;
152}
153
155{
156 portIdentity = nextPortIdentity.fetch_add(1, std::memory_order_relaxed);
157}
158
160 : BoundaryIoComponent({}, {std::move(bus)}, std::move(name))
161{
162 defineProperty("startValue", BusValue{State::LOW}, [](const PropertyValue& value) {
163 return normalizedEditableValue(std::get<BusValue>(value), 1, true);
164 });
165}
166
171
173 : BoundaryIoComponent({}, {std::move(bus)}, std::move(name))
174{
175 defineProperty(
176 "size", static_cast<int>(outputs[0].size()),
177 [this](const PropertyValue& value) { return setSize(std::get<int>(value)); });
178 defineProperty("startValue", BusValue(outputs[0].size(), State::LOW),
179 [this](const PropertyValue& value) {
180 const auto width = outputs.empty() ? 1 : outputs[0].size();
181 return normalizedEditableValue(std::get<BusValue>(value), width, true);
182 });
183}
184
189
191{
192 const int size = normalizedBusSize(newSize);
193 Bus bus = outputs.empty() ? Bus(size) : outputs[0];
194 bus.setSize(static_cast<unsigned short>(size));
195 setOutput(0, bus);
196 if (const auto startValue = getPropertyValue<BusValue>("startValue"))
197 setProperty("startValue", *startValue);
198 return size;
199}
200
202 : BoundaryIoComponent({std::move(bus)}, {}, std::move(name))
203{
204}
205
210
212 : BoundaryIoComponent({std::move(bus)}, {}, std::move(name))
213{
214 defineProperty(
215 "size", static_cast<int>(inputs[0].size()),
216 [this](const PropertyValue& value) { return setSize(std::get<int>(value)); });
217}
218
224
226{
227 const int size = normalizedBusSize(newSize);
228 Bus bus = inputs.empty() ? Bus(size) : inputs[0];
229 bus.setSize(static_cast<unsigned short>(size));
230 setInput(0, bus);
231 return size;
232}
233
234} // namespace SILICON::core
BoundaryIoComponent(std::vector< Bus > inputs, std::vector< Bus > outputs, std::string name)
Definition io.cpp:130
void setPortId(uint64_t id)
Definition io.cpp:141
void setSize(unsigned short size)
Definition wire.cpp:463
void setInput(unsigned int index, const Bus &bus, bool checkSize=false)
Sets an input bus at a specific index.
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 setProperty(std::string_view key, const PropertyValue &value)
Sets a property value.
std::vector< Bus > inputs
Input buses connected to this component.
void defineStringListProperty(std::string key, std::string defaultValue, StringPropertyOptions allowedValues, PropertyCallback callback=nullptr)
Defines a string property constrained to a fixed set of values.
Definition component.cpp:79
void setOutput(unsigned int index, const Bus &bus)
Sets an output bus at a specific index.
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
int setSize(int newSize)
Definition io.cpp:97
BusValue setValue(const BusValue &value) const
Definition io.cpp:110
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
Definition io.cpp:119
Executes event-driven simulation of a Circuit.
Definition simulator.hpp:74
void updateBus(const Bus &bus, const BusValue &value, uint64_t delay, const Component_weakPtr &source)
Updates a bus to a new value through the normal wire-update path.
std::shared_ptr< Wire > Wire_ptr
Definition wire.hpp:116
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::vector< std::string > StringPropertyOptions
Allowed values for a constrained string property.
std::vector< State > BusValue
Definition wire.hpp:120
QVariant value(const QSettings &settings, const Definition &setting)
Reads a setting value, falling back to its declared default when unset.
BusValue normalizeBusValue(const BusValue &value, const std::size_t width, const State extension)
Resize an LSB-first value to an exact bus width.
Definition wire.cpp:121