SILICON
Loading...
Searching...
No Matches
multiplexer.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 "multiplexer.hpp"
19
20#include <algorithm>
21#include <charconv>
22#include <cstddef>
23#include <limits>
24#include <optional>
25#include <stdexcept>
26#include <string>
27#include <string_view>
28#include <utility>
29#include <variant>
30#include <vector>
31
32#include <core/simulator.hpp>
33
34namespace SILICON::extra {
35using namespace SILICON::core;
36
37namespace {
38 constexpr int MAX_SELECTION_SIZE = 15;
39
40 // Converts an N-bit selection bus into the width of a packed one-bit mux/demux bus.
41 unsigned short dataWidthForSelectionSize(const int selectionSize)
42 {
43 return static_cast<unsigned short>(1u << selectionSize);
44 }
45
46 // Converts an N-bit selection bus into the number of exposed data/output buses.
47 std::size_t dataBusCountForSelectionSize(const int selectionSize)
48 {
49 return std::size_t{1} << selectionSize;
50 }
51
52 void validateSelectionSize(const std::string_view componentName,
53 const int selectionSize)
54 {
55 if (selectionSize < 1)
56 throw std::invalid_argument(std::string(componentName)
57 + " selectionSize must be at least 1");
58 if (selectionSize > MAX_SELECTION_SIZE)
59 throw std::invalid_argument(std::string(componentName)
60 + " selectionSize is too large");
61 }
62
63 void validateSelectionBus(const std::string_view componentName, const Bus& selection)
64 {
65 if (selection.size() == 0)
66 throw std::invalid_argument(std::string(componentName)
67 + " selection bus must not be empty");
68 if (selection.size() > MAX_SELECTION_SIZE)
69 throw std::invalid_argument(std::string(componentName)
70 + " selection bus is too large");
71 }
72
73 struct MuxShapeProperties {
74 int selectionSize;
75 int busSize;
76 };
77
78 MuxShapeProperties
79 checkedShapeProperties(const Component& component, const std::string_view componentName,
80 const std::optional<int> selectionSizeOverride = std::nullopt,
81 const std::optional<int> busSizeOverride = std::nullopt)
82 {
83 // Property callbacks validate one changed value at a time; reshaping needs the
84 // candidate value paired with the component's current value for the other axis.
85 const int selectionSize = selectionSizeOverride.value_or(
86 component.getPropertyValue<int>("selectionSize").value_or(1));
87 const int rawBusSize =
88 busSizeOverride.value_or(component.getPropertyValue<int>("busSize").value_or(1));
89 const int busSize = std::get<int>(
90 requireValidSize(std::string(componentName) + " busSize",
91 PropertyValue{rawBusSize}));
92
93 validateSelectionSize(componentName, selectionSize);
94 return {selectionSize, busSize};
95 }
96
97 void applyShape(Component& component, std::vector<Bus> inputs, std::vector<Bus> outputs)
98 {
99 // Keep input/output replacement paired so property reshapes cannot update only
100 // one side of the component topology.
101 // Note: setInputs/setOutputs take std::vector<Bus>&, so we pass lvalues directly
102 component.setInputs(inputs);
103 component.setOutputs(outputs);
104 }
105
106 template <typename InputStateForBit>
107 State selectedIndexFromSelection(InputStateForBit inputStateForBit,
108 const unsigned short selectionSize,
109 std::size_t& selectedIndex)
110 {
111 // Selection bits are little-endian: bit 0 contributes the low bit of the index.
112 // UNKNOWN/ERROR short-circuit because the selected lane cannot be known.
113 selectedIndex = 0;
114 for (unsigned short bit = 0; bit < selectionSize; ++bit) {
115 const State selectionBit = inputStateForBit(bit);
116 if (selectionBit == State::ERROR)
117 return State::ERROR;
118 if (selectionBit == State::UNKNOWN)
119 return State::UNKNOWN;
120 if (selectionBit == State::HIGH)
121 selectedIndex |= std::size_t{1} << bit;
122 }
123
124 return State::LOW;
125 }
126
127 template <typename UpdateOutputBit>
128 void updateAllOutputs(const std::size_t outputSize, UpdateOutputBit updateOutputBit,
129 const State state)
130 {
131 for (unsigned short bit = 0; bit < outputSize; ++bit)
132 updateOutputBit(bit, state);
133 }
134
135 template <typename UpdateOutputBit>
136 void updateAllOutputBuses(const std::vector<Bus>& outputs,
137 UpdateOutputBit updateOutputBit, const State state)
138 {
139 // Used by multi-bus demux paths where a single state must fan out over every
140 // bit of every destination bus.
141 for (std::size_t output = 0; output < outputs.size(); ++output)
142 updateAllOutputs(
143 outputs[output].size(),
144 [output, &updateOutputBit](const unsigned short bit, const State bitState) {
145 updateOutputBit(output, bit, bitState);
146 },
147 state);
148 }
149
150 bool propagatesAsBusState(const State state)
151 {
152 // ERROR and UNKNOWN describe the whole selected value, not just one data bit.
153 return state == State::ERROR || state == State::UNKNOWN;
154 }
155
156 std::size_t multiplexerSelectionInputIndex(const std::vector<Bus>& inputs)
157 {
158 // Every mux stores one bus per selectable lane and appends the selector.
159 return inputs.empty() ? 0 : inputs.size() - 1;
160 }
161
162 std::size_t demultiplexerSelectionInputIndex(const int busSize)
163 {
164 // Demux input count is stable, but this helper mirrors the mux shape helpers
165 // and keeps bus-size-dependent selection lookup in one place.
166 return busSize == 1 ? std::to_underlying(Demultiplexer::Inputs::Selection) : 1;
167 }
168
169 std::vector<Bus> resizedPorts(const std::vector<Bus>& currentPorts,
170 const std::size_t portCount,
171 const unsigned short portWidth)
172 {
173 // Preserve existing wire identities where possible so resizing does not discard
174 // established connections for ports that still exist after the shape change.
175 std::vector<Bus> newPorts(portCount);
176 for (std::size_t port = 0; port < portCount; ++port) {
177 if (port < currentPorts.size())
178 newPorts[port] = currentPorts[port];
179 newPorts[port].setSize(portWidth);
180 }
181 return newPorts;
182 }
183
184 std::vector<Bus> shapedMultiplexerInputs(const std::vector<Bus>& currentInputs,
185 const int selectionSize, const int busSize,
186 const std::vector<unsigned short>& table)
187 {
188 // The canonical shape is one equally-sized bus per lane followed by the
189 // selector. Preserve existing lane wires and the final selector where possible.
190 Bus selectionBus;
191 if (!currentInputs.empty())
192 selectionBus = currentInputs.back();
193 const std::size_t dataBusCount =
194 table.empty() ? dataBusCountForSelectionSize(selectionSize)
195 : static_cast<std::size_t>(*std::ranges::max_element(table)) + 1;
196 std::vector<Bus> newInputs(dataBusCount + 1);
197
198 const std::size_t oldDataBusCount =
199 currentInputs.empty() ? 0 : currentInputs.size() - 1;
200 const std::size_t lanesToCopy = std::min(dataBusCount, oldDataBusCount);
201
202 for (std::size_t i = 0; i < lanesToCopy; ++i) {
203 newInputs[i] = currentInputs[i];
204 }
205 for (std::size_t i = 0; i < dataBusCount; ++i) {
206 newInputs[i].setSize(static_cast<unsigned short>(busSize));
207 }
208
209 newInputs[dataBusCount] = std::move(selectionBus);
210 newInputs[dataBusCount].setSize(static_cast<unsigned short>(selectionSize));
211
212 return newInputs;
213 }
214
215 std::vector<Bus> shapedMultiplexerOutputs(const std::vector<Bus>& currentOutputs,
216 const int busSize)
217 {
218 // A mux always has one output bus; busSize only changes its width.
219 auto newOutputs = currentOutputs;
220 if (newOutputs.empty())
221 newOutputs.resize(1);
222 newOutputs[std::to_underlying(Multiplexer::Outputs::Out)].setSize(
223 static_cast<unsigned short>(busSize));
224 return newOutputs;
225 }
226
227 std::vector<Bus> shapedDemultiplexerInputs(const std::vector<Bus>& currentInputs,
228 const int selectionSize, const int busSize)
229 {
230 // A demux always has data plus selection inputs; busSize changes only the data
231 // width while selectionSize changes only the selector width.
232 auto newInputs = currentInputs;
233 if (newInputs.size() < 2)
234 newInputs.resize(2);
235 newInputs[std::to_underlying(Demultiplexer::Inputs::Data)].setSize(
236 static_cast<unsigned short>(busSize));
237 newInputs[demultiplexerSelectionInputIndex(busSize)].setSize(
238 static_cast<unsigned short>(selectionSize));
239 return newInputs;
240 }
241
242 std::vector<Bus> shapedDemultiplexerOutputs(const std::vector<Bus>& currentOutputs,
243 const int selectionSize, const int busSize)
244 {
245 if (busSize == 1) {
246 // Single-bit demuxes expose the selectable destinations as bits of one bus.
247 auto newOutputs = currentOutputs;
248 newOutputs.resize(1);
249 newOutputs[std::to_underlying(Demultiplexer::Outputs::Out)].setSize(
250 dataWidthForSelectionSize(selectionSize));
251 return newOutputs;
252 }
253
254 const std::size_t outputBusCount = dataBusCountForSelectionSize(selectionSize);
255 // Multi-bit demuxes expose one output bus per selectable destination.
256 return resizedPorts(currentOutputs, outputBusCount,
257 static_cast<unsigned short>(busSize));
258 }
259} // namespace
260
262{
263 defineProperty("selectionSize", 1);
264 defineProperty("busSize", 1);
265 defineProperty("delay", 0);
266 defineProperty("selectionTable", std::string{});
267
268 setPropertyCallback("selectionSize", [this](const PropertyValue& value) {
269 const int selectionSize = std::get<int>(value);
270 validateSelectionSize("Multiplexer", selectionSize);
271 if (!selectionTable.empty()
272 && selectionTable.size() != dataBusCountForSelectionSize(selectionSize))
273 throw std::invalid_argument(
274 "Clear the multiplexer selectionTable before changing selectionSize");
275
276 if (!inputs.empty() && !outputs.empty())
277 setSelectionSize(selectionSize);
278
279 return value;
280 });
281
282 setPropertyCallback("busSize", [this](const PropertyValue& value) {
283 const int busSize = std::get<int>(requireValidSize("Multiplexer busSize", value));
284
285 if (!inputs.empty() && !outputs.empty())
286 setBusSize(busSize);
287
288 return value;
289 });
290
291 setPropertyCallback("delay", [](const PropertyValue& value) {
292 return requireNonNegative("Multiplexer delay", value);
293 });
294 setPropertyCallback("selectionTable", [this](const PropertyValue& value) {
295 return setSelectionTable(std::get<std::string>(value));
296 });
297}
298
300{
301 validateSelectionBus("Multiplexer", selection);
302
303 const auto expectedDataWidth =
304 dataWidthForSelectionSize(static_cast<int>(selection.size()));
305 if (data.size() != expectedDataWidth)
306 throw std::invalid_argument("Multiplexer data bus width must be 2^selection width");
307
308 inputs.reserve(static_cast<std::size_t>(expectedDataWidth) + 1);
309 for (unsigned short lane = 0; lane < expectedDataWidth; ++lane)
310 inputs.emplace_back(Bus{data[lane]});
311 inputs.emplace_back(std::move(selection));
312 outputs = {{std::move(output)}};
313 setProperty("selectionSize", static_cast<int>(inputs.back().size()));
314 setProperty("busSize", 1);
315}
316
317int Multiplexer::setSelectionSize(const int selectionSize)
318{
319 const auto shape = checkedShapeProperties(*this, "Multiplexer", selectionSize);
320 if (!selectionTable.empty()
321 && selectionTable.size() != dataBusCountForSelectionSize(selectionSize))
322 throw std::invalid_argument(
323 "Clear the multiplexer selectionTable before changing selectionSize");
324 applyShape(*this,
325 shapedMultiplexerInputs(getInputs(), shape.selectionSize, shape.busSize,
327 shapedMultiplexerOutputs(getOutputs(), shape.busSize));
328
329 return selectionSize;
330}
331
332int Multiplexer::setBusSize(const int busSize)
333{
334 const auto shape = checkedShapeProperties(*this, "Multiplexer", std::nullopt, busSize);
335 applyShape(*this,
336 shapedMultiplexerInputs(getInputs(), shape.selectionSize, shape.busSize,
338 shapedMultiplexerOutputs(getOutputs(), shape.busSize));
339
340 return busSize;
341}
342
343std::string Multiplexer::setSelectionTable(const std::string& specification)
344{
345 const auto laneCount =
346 dataBusCountForSelectionSize(getPropertyValue<int>("selectionSize").value_or(1));
347 std::vector<unsigned short> parsed;
348 std::string_view text = specification;
349 while (!text.empty()) {
350 const auto comma = text.find(',');
351 const auto item = text.substr(0, comma);
352 unsigned int index = 0;
353 const auto [end, error] =
354 std::from_chars(item.data(), item.data() + item.size(), index);
355 if (item.empty() || error != std::errc{} || end != item.data() + item.size()
356 || index >= laneCount || parsed.size() >= laneCount)
357 throw std::invalid_argument("Invalid multiplexer selectionTable data-port index");
358 parsed.push_back(static_cast<unsigned short>(index));
359 if (comma == std::string_view::npos)
360 break;
361 text.remove_prefix(comma + 1);
362 if (text.empty())
363 throw std::invalid_argument("Multiplexer selectionTable has a trailing comma");
364 }
365 if (!parsed.empty()) {
366 if (parsed.size() != laneCount)
367 throw std::invalid_argument(
368 "Multiplexer selectionTable must cover every selector value");
369 std::vector<bool> seen(static_cast<std::size_t>(*std::ranges::max_element(parsed))
370 + 1);
371 for (const auto index : parsed)
372 seen[index] = true;
373 if (std::ranges::find(seen, false) != seen.end())
374 throw std::invalid_argument(
375 "Multiplexer selectionTable data-port indices must be contiguous from zero");
376 }
377 selectionTable = std::move(parsed);
378 if (!inputs.empty() && !outputs.empty())
379 setSelectionSize(getPropertyValue<int>("selectionSize").value_or(1));
380 return specification;
381}
382
384{
385 if (outputBuses().empty() || outputBusSize(Outputs::Out) == 0)
386 return;
387
388 // Note: Consider caching delay, busSize, and selectionSize in class members via
389 // setPropertyCallback to avoid hash map lookups on the simulation hot path.
390 const std::size_t selectionIndex = multiplexerSelectionInputIndex(inputBuses());
391 const auto selectionSize =
392 inputBuses().size() > selectionIndex ? inputBuses()[selectionIndex].size() : 0;
393 const std::size_t laneCount = selectionIndex;
394 const int delay = getPropertyValue<int>("delay").value_or(0);
395
396 if (selectionSize == 0 || laneCount == 0) {
397 updateAllOutputs(
399 [this, &sim, delay](const unsigned short bit, const State state) {
400 sim.updateWire(outputWire(Outputs::Out, bit), state, delay, weak_from_this());
401 },
403 return;
404 }
405
406 std::size_t selectedIndex = 0;
407 const State selectionState = selectedIndexFromSelection(
408 [this, selectionIndex](const unsigned short bit) {
409 return inputState(static_cast<unsigned int>(selectionIndex), bit);
410 },
411 selectionSize, selectedIndex);
412
413 if (propagatesAsBusState(selectionState)) {
414 // Ambiguous selection means every output bit is ambiguous; no data lane can
415 // be chosen deterministically.
416 updateAllOutputs(
418 [this, &sim, delay](const unsigned short bit, const State state) {
419 sim.updateWire(outputWire(Outputs::Out, bit), state, delay, weak_from_this());
420 },
421 selectionState);
422 return;
423 }
424
425 if (!selectionTable.empty())
426 selectedIndex =
427 selectedIndex < selectionTable.size() ? selectionTable[selectedIndex] : laneCount;
428
429 for (unsigned short bit = 0; bit < outputBusSize(Outputs::Out); ++bit) {
430 const State selectedState =
431 selectedIndex < laneCount && bit < inputBuses()[selectedIndex].size()
432 ? inputState(static_cast<unsigned int>(selectedIndex), bit)
433 : State::ERROR;
434 sim.updateWire(outputWire(Outputs::Out, bit), selectedState, delay, weak_from_this());
435 }
436}
437
439{
440 defineProperty("selectionSize", 1);
441 defineProperty("busSize", 1);
442 defineProperty("delay", 0);
443
444 setPropertyCallback("selectionSize", [this](const PropertyValue& value) {
445 const int selectionSize = std::get<int>(value);
446 validateSelectionSize("Demultiplexer", selectionSize);
447
448 if (!inputs.empty() && !outputs.empty())
449 setSelectionSize(selectionSize);
450
451 return value;
452 });
453
454 setPropertyCallback("busSize", [this](const PropertyValue& value) {
455 const int busSize =
456 std::get<int>(requireValidSize("Demultiplexer busSize", value));
457
458 if (!inputs.empty() && !outputs.empty())
459 setBusSize(busSize);
460
461 return value;
462 });
463
464 setPropertyCallback("delay", [](const PropertyValue& value) {
465 return requireNonNegative("Demultiplexer delay", value);
466 });
467}
468
470{
471 validateSelectionBus("Demultiplexer", selection);
472 if (data.size() != 1)
473 throw std::invalid_argument("Demultiplexer data bus must be single-bit");
474
475 const auto expectedOutputWidth =
476 dataWidthForSelectionSize(static_cast<int>(selection.size()));
477 if (output.size() != expectedOutputWidth)
478 throw std::invalid_argument(
479 "Demultiplexer output bus width must be 2^selection width");
480
481 inputs = {std::move(data), std::move(selection)};
482 outputs = {std::move(output)};
483 setProperty("selectionSize",
484 static_cast<int>(inputs[busIndex(Inputs::Selection)].size()));
485 setProperty("busSize", 1);
486}
487
488int Demultiplexer::setSelectionSize(const int selectionSize)
489{
490 const auto shape = checkedShapeProperties(*this, "Demultiplexer", selectionSize);
491 applyShape(
492 *this, shapedDemultiplexerInputs(getInputs(), shape.selectionSize, shape.busSize),
493 shapedDemultiplexerOutputs(getOutputs(), shape.selectionSize, shape.busSize));
494
495 return selectionSize;
496}
497
498int Demultiplexer::setBusSize(const int busSize)
499{
500 const auto shape =
501 checkedShapeProperties(*this, "Demultiplexer", std::nullopt, busSize);
502 applyShape(
503 *this, shapedDemultiplexerInputs(getInputs(), shape.selectionSize, shape.busSize),
504 shapedDemultiplexerOutputs(getOutputs(), shape.selectionSize, shape.busSize));
505
506 return busSize;
507}
508
510{
511 if (outputBuses().empty())
512 return;
513
514 const int busSize = getPropertyValue<int>("busSize").value_or(1);
515 const std::size_t selectionIndex = demultiplexerSelectionInputIndex(busSize);
516 const auto selectionSize =
517 inputBuses().size() > selectionIndex ? inputBuses()[selectionIndex].size() : 0;
518 const int delay = getPropertyValue<int>("delay").value_or(0);
519
520 if (selectionSize == 0) {
521 if (busSize == 1) {
522 updateAllOutputs(
524 [this, &sim, delay](const unsigned short bit, const State state) {
525 sim.updateWire(outputWire(Outputs::Out, bit), state, delay, weak_from_this());
526 },
528 } else {
529 // With no usable selection bus, every bit of every destination bus is invalid.
530 updateAllOutputBuses(
531 outputBuses(),
532 [this, &sim, delay](const std::size_t output, const unsigned short bit,
533 const State state) {
534 sim.updateWire(outputWire(output, bit), state, delay, weak_from_this());
535 },
537 }
538 return;
539 }
540
541 std::size_t selectedIndex = 0;
542 const State selectionState = selectedIndexFromSelection(
543 [this, selectionIndex](const unsigned short bit) {
544 return inputState(static_cast<unsigned int>(selectionIndex), bit);
545 },
546 selectionSize, selectedIndex);
547
548 if (propagatesAsBusState(selectionState)) {
549 if (busSize == 1) {
550 updateAllOutputs(
552 [this, &sim, delay](const unsigned short bit, const State state) {
553 sim.updateWire(outputWire(Outputs::Out, bit), state, delay, weak_from_this());
554 },
555 selectionState);
556 } else {
557 // UNKNOWN/ERROR selection fans out to every destination because no single
558 // selected output can be trusted.
559 updateAllOutputBuses(
560 outputBuses(),
561 [this, &sim, delay](const std::size_t output, const unsigned short bit,
562 const State state) {
563 sim.updateWire(outputWire(output, bit), state, delay, weak_from_this());
564 },
565 selectionState);
566 }
567 return;
568 }
569
570 if (busSize == 1) {
571 const State dataState = inputState(Inputs::Data);
572 for (unsigned short bit = 0; bit < outputBusSize(Outputs::Out); ++bit) {
573 const State outputState =
574 static_cast<std::size_t>(bit) == selectedIndex ? dataState : State::LOW;
575 sim.updateWire(outputWire(Outputs::Out, bit), outputState, delay, weak_from_this());
576 }
577 return;
578 }
579
580 // Loop unswitched to prevent checking equality during hot bitwise iteration
581 for (std::size_t output = 0; output < outputBuses().size(); ++output) {
582 if (output == selectedIndex) {
583 for (unsigned short bit = 0; bit < outputBuses()[output].size(); ++bit) {
584 sim.updateWire(outputWire(output, bit), inputState(Inputs::Data, bit), delay,
585 weak_from_this());
586 }
587 } else {
588 for (unsigned short bit = 0; bit < outputBuses()[output].size(); ++bit) {
589 sim.updateWire(outputWire(output, bit), State::LOW, delay, weak_from_this());
590 }
591 }
592 }
593}
594
596{
597 defineProperty("selectionSize", 1);
598 defineProperty("delay", 0);
599
600 setPropertyCallback("selectionSize", [this](const PropertyValue& value) {
601 const int selectionSize = std::get<int>(value);
602 validateSelectionSize("Decoder", selectionSize);
603
604 if (!inputs.empty() && !outputs.empty())
605 setSelectionSize(selectionSize);
606
607 return value;
608 });
609
610 setPropertyCallback("delay", [](const PropertyValue& value) {
611 return requireNonNegative("Decoder delay", value);
612 });
613}
614
615Decoder::Decoder(Bus enable, Bus selection, Bus output) : Decoder()
616{
617 validateSelectionBus("Decoder", selection);
618 if (enable.size() != 1)
619 throw std::invalid_argument("Decoder enable bus must be single-bit");
620
621 const auto expectedOutputWidth =
622 dataWidthForSelectionSize(static_cast<int>(selection.size()));
623 if (output.size() != expectedOutputWidth)
624 throw std::invalid_argument("Decoder output bus width must be 2^selection width");
625
626 inputs = {std::move(enable), std::move(selection)};
627 outputs = {std::move(output)};
628 setProperty("selectionSize",
629 static_cast<int>(inputs[busIndex(Inputs::Selection)].size()));
630}
631
632int Decoder::setSelectionSize(const int selectionSize)
633{
634 validateSelectionSize("Decoder", selectionSize);
635
636 auto newInputs = getInputs();
637 if (newInputs.size() < 2)
638 newInputs.resize(2);
639 if (newInputs[busIndex(Inputs::Enable)].size() != 1)
640 newInputs[busIndex(Inputs::Enable)].setSize(1);
641 newInputs[busIndex(Inputs::Selection)].setSize(
642 static_cast<unsigned short>(selectionSize));
643
644 auto newOutputs = getOutputs();
645 if (newOutputs.empty())
646 newOutputs.resize(1);
647 newOutputs[busIndex(Outputs::Out)].setSize(dataWidthForSelectionSize(selectionSize));
648
649 applyShape(*this, newInputs, newOutputs);
650
651 return selectionSize;
652}
653
655{
656 if (outputBusSize(Outputs::Out) == 0)
657 return;
658
659 const auto selectionSize = inputBuses().size() > busIndex(Inputs::Selection)
661 : 0;
662 const int delay = getPropertyValue<int>("delay").value_or(0);
663 const State enable = inputState(Inputs::Enable);
664
665 if (propagatesAsBusState(enable)) {
666 updateAllOutputs(
668 [this, &sim, delay](const unsigned short bit, const State state) {
669 sim.updateWire(outputWire(Outputs::Out, bit), state, delay, weak_from_this());
670 },
671 enable);
672 return;
673 }
674
675 if (selectionSize == 0) {
676 updateAllOutputs(
678 [this, &sim, delay](const unsigned short bit, const State state) {
679 sim.updateWire(outputWire(Outputs::Out, bit), state, delay, weak_from_this());
680 },
682 return;
683 }
684
685 if (enable == State::LOW) {
686 updateAllOutputs(
688 [this, &sim, delay](const unsigned short bit, const State state) {
689 sim.updateWire(outputWire(Outputs::Out, bit), state, delay, weak_from_this());
690 },
691 State::LOW);
692 return;
693 }
694
695 std::size_t selectedIndex = 0;
696 const State selectionState = selectedIndexFromSelection(
697 [this](const unsigned short bit) { return inputState(Inputs::Selection, bit); },
698 selectionSize, selectedIndex);
699
700 if (propagatesAsBusState(selectionState)) {
701 updateAllOutputs(
703 [this, &sim, delay](const unsigned short bit, const State state) {
704 sim.updateWire(outputWire(Outputs::Out, bit), state, delay, weak_from_this());
705 },
706 selectionState);
707 return;
708 }
709
710 for (unsigned short bit = 0; bit < outputBusSize(Outputs::Out); ++bit) {
711 const State outputState =
712 (static_cast<std::size_t>(bit) == selectedIndex) ? State::HIGH : State::LOW;
713 sim.updateWire(outputWire(Outputs::Out, bit), outputState, delay, weak_from_this());
714 }
715}
716
717} // namespace SILICON::extra
Base class for all digital circuit components.
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
const std::vector< Bus > & inputBuses() const
Gets all input buses without copying.
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.
std::vector< Bus > getOutputs() const
Gets all output buses.
const std::vector< Bus > & outputBuses() const
Gets all output buses without copying.
std::vector< Bus > inputs
Input buses connected to this component.
void setPropertyCallback(std::string_view key, PropertyCallback callback)
Sets a callback for property validation/transformation.
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::vector< Bus > getInputs() const
Gets all input buses.
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
int setSelectionSize(int selectionSize)
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
int setSelectionSize(int selectionSize)
std::vector< unsigned short > selectionTable
std::string setSelectionTable(const std::string &specification)
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
int setSelectionSize(int selectionSize)
Executes event-driven simulation of a Circuit.
Definition simulator.hpp:74
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.
RTLIL::SigSpec port(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key, int expected)
std::shared_ptr< Wire > Wire_ptr
Definition wire.hpp:116
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
PropertyValue requireValidSize(const std::string_view name, const PropertyValue &value)
Validates that an integer property value is a valid bus size.
Definition component.cpp:38
PropertyValue requireNonNegative(const std::string_view name, const PropertyValue &value)
Validates that an integer property value is non-negative.
Definition component.cpp:29