35using namespace SILICON::core;
38 constexpr int MAX_SELECTION_SIZE = 15;
41 unsigned short dataWidthForSelectionSize(
const int selectionSize)
43 return static_cast<unsigned short>(1u << selectionSize);
47 std::size_t dataBusCountForSelectionSize(
const int selectionSize)
49 return std::size_t{1} << selectionSize;
52 void validateSelectionSize(
const std::string_view componentName,
53 const int selectionSize)
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");
63 void validateSelectionBus(
const std::string_view componentName,
const Bus& selection)
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");
73 struct 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)
85 const int selectionSize = selectionSizeOverride.value_or(
87 const int rawBusSize =
88 busSizeOverride.value_or(component.
getPropertyValue<
int>(
"busSize").value_or(1));
89 const int busSize = std::get<int>(
93 validateSelectionSize(componentName, selectionSize);
94 return {selectionSize, busSize};
97 void applyShape(
Component& component, std::vector<Bus> inputs, std::vector<Bus> outputs)
106 template <
typename InputStateForBit>
107 State selectedIndexFromSelection(InputStateForBit inputStateForBit,
108 const unsigned short selectionSize,
109 std::size_t& selectedIndex)
114 for (
unsigned short bit = 0; bit < selectionSize; ++bit) {
115 const State selectionBit = inputStateForBit(bit);
116 if (selectionBit == State::ERROR)
118 if (selectionBit == State::UNKNOWN)
119 return State::UNKNOWN;
120 if (selectionBit == State::HIGH)
121 selectedIndex |= std::size_t{1} << bit;
127 template <
typename UpdateOutputBit>
128 void updateAllOutputs(
const std::size_t outputSize, UpdateOutputBit updateOutputBit,
131 for (
unsigned short bit = 0; bit < outputSize; ++bit)
132 updateOutputBit(bit, state);
135 template <
typename UpdateOutputBit>
136 void updateAllOutputBuses(
const std::vector<Bus>& outputs,
137 UpdateOutputBit updateOutputBit,
const State state)
141 for (std::size_t output = 0; output < outputs.size(); ++output)
143 outputs[output].size(),
144 [output, &updateOutputBit](
const unsigned short bit,
const State bitState) {
145 updateOutputBit(output, bit, bitState);
150 bool propagatesAsBusState(
const State state)
153 return state == State::ERROR || state == State::UNKNOWN;
156 std::size_t multiplexerSelectionInputIndex(
const std::vector<Bus>& inputs)
159 return inputs.empty() ? 0 : inputs.size() - 1;
162 std::size_t demultiplexerSelectionInputIndex(
const int busSize)
169 std::vector<Bus> resizedPorts(
const std::vector<Bus>& currentPorts,
170 const std::size_t portCount,
171 const unsigned short portWidth)
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);
184 std::vector<Bus> shapedMultiplexerInputs(
const std::vector<Bus>& currentInputs,
185 const int selectionSize,
const int busSize,
186 const std::vector<unsigned short>& table)
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);
198 const std::size_t oldDataBusCount =
199 currentInputs.empty() ? 0 : currentInputs.size() - 1;
200 const std::size_t lanesToCopy = std::min(dataBusCount, oldDataBusCount);
202 for (std::size_t i = 0; i < lanesToCopy; ++i) {
203 newInputs[i] = currentInputs[i];
205 for (std::size_t i = 0; i < dataBusCount; ++i) {
206 newInputs[i].setSize(
static_cast<unsigned short>(busSize));
209 newInputs[dataBusCount] = std::move(selectionBus);
210 newInputs[dataBusCount].setSize(
static_cast<unsigned short>(selectionSize));
215 std::vector<Bus> shapedMultiplexerOutputs(
const std::vector<Bus>& currentOutputs,
219 auto newOutputs = currentOutputs;
220 if (newOutputs.empty())
221 newOutputs.resize(1);
223 static_cast<unsigned short>(busSize));
227 std::vector<Bus> shapedDemultiplexerInputs(
const std::vector<Bus>& currentInputs,
228 const int selectionSize,
const int busSize)
232 auto newInputs = currentInputs;
233 if (newInputs.size() < 2)
236 static_cast<unsigned short>(busSize));
237 newInputs[demultiplexerSelectionInputIndex(busSize)].setSize(
238 static_cast<unsigned short>(selectionSize));
242 std::vector<Bus> shapedDemultiplexerOutputs(
const std::vector<Bus>& currentOutputs,
243 const int selectionSize,
const int busSize)
247 auto newOutputs = currentOutputs;
248 newOutputs.resize(1);
250 dataWidthForSelectionSize(selectionSize));
254 const std::size_t outputBusCount = dataBusCountForSelectionSize(selectionSize);
256 return resizedPorts(currentOutputs, outputBusCount,
257 static_cast<unsigned short>(busSize));
269 const int selectionSize = std::get<int>(value);
270 validateSelectionSize(
"Multiplexer", selectionSize);
272 &&
selectionTable.size() != dataBusCountForSelectionSize(selectionSize))
273 throw std::invalid_argument(
274 "Clear the multiplexer selectionTable before changing selectionSize");
283 const int busSize = std::get<int>(
requireValidSize(
"Multiplexer busSize", value));
301 validateSelectionBus(
"Multiplexer", selection);
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");
308 inputs.reserve(
static_cast<std::size_t
>(expectedDataWidth) + 1);
309 for (
unsigned short lane = 0; lane < expectedDataWidth; ++lane)
311 inputs.emplace_back(std::move(selection));
312 outputs = {{std::move(output)}};
319 const auto shape = checkedShapeProperties(*
this,
"Multiplexer", selectionSize);
321 &&
selectionTable.size() != dataBusCountForSelectionSize(selectionSize))
322 throw std::invalid_argument(
323 "Clear the multiplexer selectionTable before changing selectionSize");
325 shapedMultiplexerInputs(
getInputs(), shape.selectionSize, shape.busSize,
327 shapedMultiplexerOutputs(
getOutputs(), shape.busSize));
329 return selectionSize;
334 const auto shape = checkedShapeProperties(*
this,
"Multiplexer", std::nullopt, busSize);
336 shapedMultiplexerInputs(
getInputs(), shape.selectionSize, shape.busSize,
338 shapedMultiplexerOutputs(
getOutputs(), shape.busSize));
345 const auto laneCount =
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)
361 text.remove_prefix(comma + 1);
363 throw std::invalid_argument(
"Multiplexer selectionTable has a trailing comma");
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))
371 for (
const auto index : parsed)
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");
380 return specification;
390 const std::size_t selectionIndex = multiplexerSelectionInputIndex(
inputBuses());
391 const auto selectionSize =
393 const std::size_t laneCount = selectionIndex;
396 if (selectionSize == 0 || laneCount == 0) {
399 [
this, &sim, delay](
const unsigned short bit,
const State state) {
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);
411 selectionSize, selectedIndex);
413 if (propagatesAsBusState(selectionState)) {
418 [
this, &sim, delay](
const unsigned short bit,
const State state) {
430 const State selectedState =
431 selectedIndex < laneCount && bit <
inputBuses()[selectedIndex].size()
432 ?
inputState(
static_cast<unsigned int>(selectedIndex), bit)
445 const int selectionSize = std::get<int>(value);
446 validateSelectionSize(
"Demultiplexer", selectionSize);
471 validateSelectionBus(
"Demultiplexer", selection);
472 if (data.
size() != 1)
473 throw std::invalid_argument(
"Demultiplexer data bus must be single-bit");
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");
481 inputs = {std::move(data), std::move(selection)};
490 const auto shape = checkedShapeProperties(*
this,
"Demultiplexer", selectionSize);
492 *
this, shapedDemultiplexerInputs(
getInputs(), shape.selectionSize, shape.busSize),
493 shapedDemultiplexerOutputs(
getOutputs(), shape.selectionSize, shape.busSize));
495 return selectionSize;
501 checkedShapeProperties(*
this,
"Demultiplexer", std::nullopt, busSize);
503 *
this, shapedDemultiplexerInputs(
getInputs(), shape.selectionSize, shape.busSize),
504 shapedDemultiplexerOutputs(
getOutputs(), shape.selectionSize, shape.busSize));
515 const std::size_t selectionIndex = demultiplexerSelectionInputIndex(busSize);
516 const auto selectionSize =
520 if (selectionSize == 0) {
524 [
this, &sim, delay](
const unsigned short bit,
const State state) {
530 updateAllOutputBuses(
532 [
this, &sim, delay](
const std::size_t output,
const unsigned short bit,
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);
546 selectionSize, selectedIndex);
548 if (propagatesAsBusState(selectionState)) {
552 [
this, &sim, delay](
const unsigned short bit,
const State state) {
559 updateAllOutputBuses(
561 [
this, &sim, delay](
const std::size_t output,
const unsigned short bit,
573 const State outputState =
574 static_cast<std::size_t
>(bit) == selectedIndex ? dataState :
State::LOW;
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) {
588 for (
unsigned short bit = 0; bit <
outputBuses()[output].size(); ++bit) {
601 const int selectionSize = std::get<int>(value);
602 validateSelectionSize(
"Decoder", selectionSize);
617 validateSelectionBus(
"Decoder", selection);
618 if (enable.
size() != 1)
619 throw std::invalid_argument(
"Decoder enable bus must be single-bit");
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");
626 inputs = {std::move(enable), std::move(selection)};
634 validateSelectionSize(
"Decoder", selectionSize);
637 if (newInputs.size() < 2)
642 static_cast<unsigned short>(selectionSize));
645 if (newOutputs.empty())
646 newOutputs.resize(1);
649 applyShape(*
this, newInputs, newOutputs);
651 return selectionSize;
665 if (propagatesAsBusState(enable)) {
668 [
this, &sim, delay](
const unsigned short bit,
const State state) {
675 if (selectionSize == 0) {
678 [
this, &sim, delay](
const unsigned short bit,
const State state) {
688 [
this, &sim, delay](
const unsigned short bit,
const State state) {
695 std::size_t selectedIndex = 0;
696 const State selectionState = selectedIndexFromSelection(
698 selectionSize, selectedIndex);
700 if (propagatesAsBusState(selectionState)) {
703 [
this, &sim, delay](
const unsigned short bit,
const State state) {
711 const State outputState =
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.
Executes event-driven simulation of a Circuit.
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
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.
PropertyValue requireNonNegative(const std::string_view name, const PropertyValue &value)
Validates that an integer property value is non-negative.