32using namespace SILICON::core;
48 const int width = std::get<int>(
requireValidSize(
"Extender outSize", value));
57 if (in.
size() == 0 || out.
size() == 0)
58 throw std::invalid_argument(
"Extender buses must not be empty");
69 if (width < 1 || width > std::numeric_limits<unsigned short>::max())
73 if (newInputs.empty())
75 newInputs[0].setSize(
static_cast<unsigned short>(width));
82 if (width < 1 || width > std::numeric_limits<unsigned short>::max())
86 if (newOutputs.empty())
88 newOutputs[0].setSize(
static_cast<unsigned short>(width));
98 auto value =
inputs[0].getCurrentValue();
100 const bool signedMode =
119 const int size = std::get<int>(
requireValidSize(
"Complementer size", value));
131 throw std::invalid_argument(
132 "Complementer: input bus width must match non-empty output bus width");
141 if (width < 1 || width > std::numeric_limits<unsigned short>::max())
145 if (newInputs.empty())
147 newInputs[0].setSize(
static_cast<unsigned short>(width));
151 if (newOutputs.empty())
152 newOutputs.resize(1);
153 newOutputs[0].setSize(
static_cast<unsigned short>(width));
182 outputs = {{std::move(sum)}, {std::move(cout)}};
213 outputs = {{std::move(sum)}, {std::move(cout)}};
224 const State sum = a ^ b ^ cin;
225 const State cout = (a && b) || (cin && (a ^ b));
255 throw std::invalid_argument(
"AdderNBits: input bus width must match sum bus width");
258 inputs = {inputBuses[busIndex(Inputs::A)], inputBuses[busIndex(Inputs::B)]};
259 outputs = {sum, {std::move(cout)}};
260 setProperty(
"size",
static_cast<int>(sum.size()));
269 if (newInputs.size() < 2)
276 if (newOutputs.size() < 2)
277 newOutputs.resize(2);
297 const State carry = result[sumWidth];
298 result.resize(sumWidth);
318 const int width = std::get<int>(
requireValidSize(
"Shifter amountSize", value));
330 if (value.size() == 0 || amount.
size() == 0 || result.
size() != value.size())
331 throw std::invalid_argument(
332 "Shifter: value and result must have the same non-zero width, and amount must be non-empty");
333 inputs = {std::move(value), std::move(amount)};
341 if (width < 1 || width > std::numeric_limits<unsigned short>::max())
344 if (newInputs.size() < 2)
346 newInputs[0].setSize(
static_cast<unsigned short>(width));
349 if (newOutputs.empty())
350 newOutputs.resize(1);
351 newOutputs[0].setSize(
static_cast<unsigned short>(width));
358 if (width < 1 || width > std::numeric_limits<unsigned short>::max())
361 if (newInputs.size() < 2)
363 newInputs[1].setSize(
static_cast<unsigned short>(width));
377 if (std::ranges::find(amount,
State::ERROR) != amount.end()) {
379 }
else if (std::ranges::find(amount,
State::UNKNOWN) != amount.end()) {
383 std::size_t count = 0;
384 for (std::size_t bit = amount.size(); bit-- > 0;) {
385 count = std::min(value.size(), count * 2 + (amount[bit] ==
State::HIGH));
391 result =
shift(value, left ? -
static_cast<ptrdiff_t
>(count)
392 :
static_cast<ptrdiff_t
>(count), signedness);
399int resizeBinaryArithmetic(
Component& component,
const int width,
400 const std::size_t outputCount)
402 if (width < 1 || width > std::numeric_limits<unsigned short>::max())
406 for (
auto& bus : inputs)
407 bus.setSize(
static_cast<unsigned short>(width));
410 outputs.resize(outputCount);
411 for (
auto& bus : outputs)
412 bus.setSize(
static_cast<unsigned short>(width));
424 const int width = std::get<int>(
requireValidSize(
"Multiplier size", value));
435 if (width == 0 ||
inputBuses[1].size() != width || product.
size() != width)
436 throw std::invalid_argument(
437 "Multiplier: all buses must have the same non-zero width");
438 inputs = {std::move(inputBuses[0]), std::move(inputBuses[1])};
439 outputs = {std::move(product)};
445 return resizeBinaryArithmetic(*
this, width, 1);
465 if (width == 0 ||
inputBuses[1].size() != width || quotient.
size() != width
466 || remainder.
size() != width)
467 throw std::invalid_argument(
"Divider: all buses must have the same non-zero width");
468 inputs = {std::move(inputBuses[0]), std::move(inputBuses[1])};
469 outputs = {std::move(quotient), std::move(remainder)};
475 return resizeBinaryArithmetic(*
this, width, 2);
498 const auto dividend =
inputs[0].getCurrentValue();
499 const auto divisor =
inputs[1].getCurrentValue();
501 if (std::ranges::all_of(divisor, [](
State state) {
return state ==
State::LOW; })) {
508 std::tie(quotient, remainder) =
divide(dividend, divisor, sign);
539 throw std::invalid_argument(
540 "Comparator: input buses must have the same non-zero width");
543 this->outputs = {{std::move(output)}};
545 const int size =
static_cast<int>(
inputs[0].size());
558 const auto isError = [](
const BusValue& value) {
559 return std::ranges::any_of(value,
563 if (isError(a) || isError(b)) {
569 const auto compareResult =
572 if (compareResult == std::partial_ordering::unordered) {
577 const auto checkForOrdering = [compareResult](std::string_view mode) {
578 const auto modeWantEquivalent = {
"==",
"<=",
">="};
579 const auto modeWantLess = {
"<",
"<="};
580 const auto modeWantGreat = {
">",
">="};
583 if ((std::ranges::contains(modeWantEquivalent, mode) && compareResult == std::partial_ordering::equivalent) ||
584 (std::ranges::contains(modeWantLess, mode) && compareResult == std::partial_ordering::less) ||
585 (std::ranges::contains(modeWantGreat, mode) && compareResult == std::partial_ordering::greater))
605 newInputs[0].setSize(width);
606 newInputs[1].setSize(width);
608 this->inputs = newInputs;
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.
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.
void setOutput(unsigned int index, const Bus &bus)
Sets an output bus at a specific index.
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 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.
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
BusValue shift(const BusValue &v, const ptrdiff_t amount, const Signedness signedness)
std::partial_ordering compare(const BusValue &lhs, const BusValue &rhs, const Signedness signedness)
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.
std::pair< BusValue, BusValue > divide(const BusValue &a, const BusValue &b, Signedness signedness)
PropertyValue requireNonNegative(const std::string_view name, const PropertyValue &value)
Validates that an integer property value is non-negative.
BusValue twosComplement(const BusValue &n)
BusValue multiply(const BusValue &a, const BusValue &b, const Signedness signedness)
std::vector< State > BusValue
BusValue normalizeBusValue(const BusValue &value, const std::size_t width, const State extension)
Resize an LSB-first value to an exact bus width.