SILICON
Loading...
Searching...
No Matches
register.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 "register.hpp"
19
20#include <algorithm>
21#include <stdexcept>
22#include <string>
23#include <utility>
24#include <variant>
25
26#include <core/wireUtils.hpp>
27
28namespace SILICON::core {
29
30namespace {
31
32 void validateSize(const int size)
33 {
34 if (size <= 1)
35 throw std::invalid_argument("Register size must be greater than 1");
36 }
37
38 State normalizedInputState(const State state)
39 {
41 }
42
43 unsigned short dataBusSize(const std::string& inputType, const int size)
44 {
45 return inputType == Register::PARALLEL_TYPE ? static_cast<unsigned short>(size) : 1;
46 }
47
48 unsigned short registerOutputBusSize(const std::string& outputType, const int size)
49 {
50 return outputType == Register::PARALLEL_TYPE ? static_cast<unsigned short>(size) : 1;
51 }
52
53} // namespace
54
59
60Register::Register(Bus data, Wire_ptr clock, Wire_ptr enable, Wire_ptr clear, Bus output)
61 : Register()
62{
63 const auto initialDataSize = data.size();
64 const auto initialOutputSize = output.size();
65
66 inputs = {std::move(data), {std::move(clock)}, {std::move(enable)}, {std::move(clear)}};
67 outputs = {std::move(output)};
68
69 const int inferredSize = static_cast<int>(std::max(initialDataSize, initialOutputSize));
70 setProperty("size", std::max(2, inferredSize));
71 setProperty("inputType", initialDataSize == 1 && initialOutputSize > 1
72 ? std::string(SERIAL_TYPE)
73 : std::string(PARALLEL_TYPE));
74 setProperty("outputType", initialOutputSize == 1 && initialDataSize > 1
75 ? std::string(SERIAL_TYPE)
76 : std::string(PARALLEL_TYPE));
77}
78
80{
81 return getPropertyValue<int>("size").value_or(2);
82}
83
85{
86 return getPropertyValue<std::string>("inputType").value_or(std::string(PARALLEL_TYPE));
87}
88
90{
91 return getPropertyValue<std::string>("outputType").value_or(std::string(PARALLEL_TYPE));
92}
93
95{
97}
98
100{
102}
103
105{
107 defineProperty("delay", 0);
108 defineStringListProperty("inputType", std::string(PARALLEL_TYPE),
109 {std::string(PARALLEL_TYPE), std::string(SERIAL_TYPE)});
110 defineStringListProperty("outputType", std::string(PARALLEL_TYPE),
111 {std::string(PARALLEL_TYPE), std::string(SERIAL_TYPE)});
112 defineProperty("size", 2);
113
114 setPropertyCallback("delay", [](const PropertyValue& value) {
115 return requireNonNegative("Register delay", value);
116 });
117 setPropertyCallback("size", [this](const PropertyValue& value) {
118 const int size = std::get<int>(value);
119 validateSize(size);
120 if (!inputs.empty() && !outputs.empty())
121 setSize(size);
122 return value;
123 });
124 setPropertyCallback("inputType", [this](const PropertyValue& value) {
125 const std::string inputType = std::get<std::string>(value);
126 if (!inputs.empty() && !outputs.empty())
127 setInputType(inputType);
128 return value;
129 });
130 setPropertyCallback("outputType", [this](const PropertyValue& value) {
131 const std::string outputType = std::get<std::string>(value);
132 if (!inputs.empty() && !outputs.empty())
133 setOutputType(outputType);
134 return value;
135 });
136}
137
138int Register::setSize(const int size)
139{
140 validateSize(size);
141 if (state.size() != static_cast<std::size_t>(size))
142 state.assign(static_cast<std::size_t>(size), State::UNKNOWN);
144 return size;
145}
146
147std::string Register::setInputType(std::string inputType)
148{
150 return inputType;
151}
152
153std::string Register::setOutputType(std::string outputType)
154{
156 return outputType;
157}
158
163
164void Register::reshapeBuses(const int size, const std::string& inputType,
165 const std::string& outputType)
166{
167 std::vector<Bus> newInputs = inputs;
168 newInputs.resize(inputType == PARALLEL_TYPE && outputType == SERIAL_TYPE ? 5 : 4);
169 newInputs[busIndex(Inputs::Data)].setSize(dataBusSize(inputType, size));
170 newInputs[busIndex(Inputs::Clock)].setSize(1);
171 newInputs[busIndex(Inputs::Enable)].setSize(1);
172 newInputs[busIndex(Inputs::Clear)].setSize(1);
173 if (newInputs.size() > busIndex(Inputs::Load))
174 newInputs[busIndex(Inputs::Load)].setSize(1);
175 setInputs(newInputs);
176
177 std::vector<Bus> newOutputs = outputs;
178 newOutputs.resize(1);
179 newOutputs[busIndex(Outputs::Out)].setSize(registerOutputBusSize(outputType, size));
180 setOutputs(newOutputs);
181
182 if (state.size() != static_cast<std::size_t>(size))
183 state.assign(static_cast<std::size_t>(size), State::UNKNOWN);
184}
185
187{
188 state.assign(static_cast<std::size_t>(configuredSize()), State::LOW);
189}
190
192{
193 if (outputs.empty() || outputBusSize(Outputs::Out) == 0)
194 return;
195
196 const auto delay = static_cast<uint64_t>(getPropertyValue<int>("delay").value_or(0));
197
198 if (parallelOutput()) {
199 for (unsigned short bit = 0; bit < outputBusSize(Outputs::Out); ++bit) {
200 const State bitState = bit < state.size() ? state[bit] : State::ERROR;
201 sim.updateWire(outputWire(Outputs::Out, bit), bitState, delay, weak_from_this());
202 }
203 return;
204 }
205
206 const State bitState = state.empty() ? State::ERROR : state.front();
207 sim.updateWire(outputWire(Outputs::Out), bitState, delay, weak_from_this());
208}
209
211 const SILICON::simulation::Context& context)
212{
213 if (state.size() != static_cast<std::size_t>(configuredSize()))
214 state.assign(static_cast<std::size_t>(configuredSize()), State::UNKNOWN);
215
216 const State clear = inputState(Inputs::Clear);
217 if (clear == State::HIGH) {
218 clearState();
219 driveOutput(sim);
220 return;
221 }
222 if (clear == State::UNKNOWN || clear == State::ERROR) {
223 // assign() does not reallocate memory if the size remains the same
224 state.assign(state.size(), State::UNKNOWN);
225 driveOutput(sim);
226 return;
227 }
228
229 const State enable = inputState(Inputs::Enable);
230 if (enable == State::UNKNOWN || enable == State::ERROR) {
231 state.assign(state.size(), State::UNKNOWN);
232 driveOutput(sim);
233 return;
234 }
235 if (enable == State::LOW) {
236 if (context.initialEvaluation)
237 driveOutput(sim);
238 return;
239 }
240
241 const Wire_ptr clockWire = inputWire(Inputs::Clock);
242 if (SILICON::simulation::Simulator::edgeType(context, clockWire)
244 if (parallelInput()) {
245 if (parallelOutput()) {
246 // PIPO: Overwrite state directly from inputs
247 for (std::size_t bit = 0; bit < state.size(); ++bit)
248 state[bit] = normalizedInputState(inputState(Inputs::Data, bit));
249 } else {
250 // PISO
251 const State load = inputState(Inputs::Load);
252 if (load == State::UNKNOWN || load == State::ERROR) {
253 state.assign(state.size(), State::UNKNOWN);
254 } else if (load == State::HIGH) {
255 // Load Parallel Data directly into state
256 for (std::size_t bit = 0; bit < state.size(); ++bit)
257 state[bit] = normalizedInputState(inputState(Inputs::Data, bit));
258 } else {
259 // Shift Right In-Place
260 for (std::size_t bit = 0; bit + 1 < state.size(); ++bit)
261 state[bit] = state[bit + 1];
262 if (!state.empty())
263 state.back() = State::LOW;
264 }
265 }
266 } else {
267 // Serial Input (SISO / SIPO): Shift Right In-Place
268 if (!state.empty()) {
269 for (std::size_t bit = 0; bit + 1 < state.size(); ++bit)
270 state[bit] = state[bit + 1];
271 state.back() = normalizedInputState(inputState(Inputs::Data));
272 }
273 }
274
275 driveOutput(sim);
276 return;
277 }
278
279 if (context.initialEvaluation)
280 driveOutput(sim);
281}
282
283} // namespace SILICON::core
static constexpr unsigned int busIndex(Enum value)
Converts a component-local bus enum to its storage index.
Wire_ptr outputWire(unsigned int output, unsigned short bit=0) const
State inputState(unsigned int input, unsigned short bit=0) const
void setOutputs(const std::vector< Bus > &newOutputs)
Replaces all output buses.
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 setInputs(const std::vector< Bus > &newInputs)
Replaces all input buses.
void setProperty(std::string_view key, const PropertyValue &value)
Sets a property value.
void defineUnconnectedInputDefault(unsigned int input, State value)
Defines the state read from an input when its wire is unconnected.
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 setPropertyCallback(std::string_view key, PropertyCallback callback)
Sets a callback for property validation/transformation.
Wire_ptr inputWire(unsigned int input, unsigned short bit=0) const
void defineProperty(std::string key, T &&defaultValue, PropertyCallback callback=nullptr)
Defines a property with a default value and optional callback.
std::size_t outputBusSize(unsigned int output) const
std::string setInputType(std::string inputType)
Definition register.cpp:147
std::vector< State > state
Definition register.hpp:81
void driveOutput(SILICON::simulation::Simulator &sim)
Definition register.cpp:191
int configuredSize() const
Definition register.cpp:79
void simulate(SILICON::simulation::Simulator &sim, const SILICON::simulation::Context &context) override
Executes the component's logic with simulator scheduling context.
Definition register.cpp:210
static constexpr std::string_view SERIAL_TYPE
Definition register.hpp:33
std::string setOutputType(std::string outputType)
Definition register.cpp:153
bool parallelOutput() const
Definition register.cpp:99
bool parallelInput() const
Definition register.cpp:94
int setSize(int size)
Definition register.cpp:138
static constexpr std::string_view PARALLEL_TYPE
Definition register.hpp:32
std::string configuredOutputType() const
Definition register.cpp:89
std::string configuredInputType() const
Definition register.cpp:84
Executes event-driven simulation of a Circuit.
Definition simulator.hpp:74
static EdgeType edgeType(const Context &context, const Wire_ptr &wire)
Classifies one wire's transition in a simulation context.
void updateWire(const Wire_ptr &target, State newState, uint64_t delay, const Component_weakPtr &source)
Updates a wire with a new state and optional delay.
std::shared_ptr< Wire > Wire_ptr
Definition wire.hpp:116
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
PropertyValue requireNonNegative(const std::string_view name, const PropertyValue &value)
Validates that an integer property value is non-negative.
Definition component.cpp:29
State normalizeBinaryOrUnknown(const State state)
Normalizes non-binary states for stored binary elements.
Definition wire.cpp:146
Context describing why a component is being evaluated.
Definition component.hpp:90
bool initialEvaluation
True for initial or otherwise full-plan evaluation.
Definition component.hpp:92