85 throw std::logic_error(
"Unhandled State enum value");
99 return value.size() <= width
100 || std::ranges::all_of(value.begin() +
static_cast<std::ptrdiff_t
>(width),
102 [](
const State state) { return state == State::LOW; });
107 if (value.empty() || width == 0)
109 if (value.size() <= width)
112 const State sign = value[width - 1];
116 return std::ranges::all_of(value.begin() +
static_cast<std::ptrdiff_t
>(width),
118 [sign](
const State state) { return state == sign; });
122 const State extension)
126 +
static_cast<std::ptrdiff_t
>(std::min(value.size(), width)));
127 normalized.resize(width, extension);
157 this->
id =
nextId.fetch_add(1, std::memory_order_relaxed);
163 this->
id =
nextId.fetch_add(1, std::memory_order_relaxed);
169 auto next =
nextId.load(std::memory_order_relaxed);
171 && !
nextId.compare_exchange_weak(next,
id + 1, std::memory_order_relaxed)) {}
198 return this->
currentState.load(std::memory_order_relaxed);
203 this->
currentState.store(newState, std::memory_order_relaxed);
215 this->
currentState.store(newState, std::memory_order_relaxed);
229 if (!changeIsAuthorized) {
231 log.
error(
"Unauthorized wire state change detected");
240 if (
const auto lockedWire = w.lock()) {
241 lockedWire->setCurrentState(newState, requestedBy);
247 const auto lockedWire = w.lock();
248 return lockedWire ? lockedWire->getCurrentState() :
State::ERROR;
254 const auto width = std::max(a.size(), b.size()) + 1;
261 for (
auto&& [idx, s] : res | SILICON::views::enumerate) {
262 const State valA = s;
263 const State valB = extB[idx];
265 s = valA ^ valB ^ carry;
266 carry = (valA && valB) || (carry && (valA ^ valB));
274 BusValue res = n | std::views::transform([](
const State s) {
return !s; })
275 | std::ranges::to<BusValue>();
279 for (
State& s : res) {
280 const State currentBit = s;
282 s = currentBit ^ carry;
283 carry = currentBit && carry;
292 const auto isError = [](
const BusValue& value) {
293 return std::ranges::any_of(value,
297 if (isError(lhs) || isError(rhs) || lhs.empty() || rhs.empty())
298 return std::partial_ordering::unordered;
301 const auto bounds = [signedness](
const BusValue& value) {
305 for (std::size_t i = 0; i < value.size(); ++i) {
315 return std::pair{std::move(min), std::move(max)};
319 const auto compareKnown = [signedness](
const BusValue& lhs,
const BusValue& rhs) {
321 return lhs.back() ==
State::HIGH ? std::partial_ordering::less
322 : std::partial_ordering::greater;
325 for (std::size_t i = lhs.size(); i-- > 0;) {
326 if (lhs[i] == rhs[i])
329 return lhs[i] ==
State::HIGH ? std::partial_ordering::greater
330 : std::partial_ordering::less;
333 return std::partial_ordering::equivalent;
336 auto [aMin, aMax] = bounds(lhs);
337 auto [bMin, bMax] = bounds(rhs);
339 const auto width = std::max(lhs.size(), rhs.size());
341 const auto extend = [signedness, width](
const BusValue& value) {
352 if (compareKnown(aMin, bMax) == std::partial_ordering::greater)
353 return std::partial_ordering::greater;
355 if (compareKnown(aMax, bMin) == std::partial_ordering::less)
356 return std::partial_ordering::less;
359 [[unlikely]]
if (aMin == aMax && bMin == bMax
360 && compareKnown(aMin, bMin) == std::partial_ordering::equivalent) {
361 return std::partial_ordering::equivalent;
364 return std::partial_ordering::unordered;
369 if (v.empty() || amount == 0)
377 for (
auto&& [index, value] : SILICON::views::enumerate(res)) {
378 const ptrdiff_t source = index + amount;
380 const bool sourceInBounds = source >= 0 && source < std::ssize(v);
382 value = sourceInBounds ? v[source] : filler;
390 if (a.empty() || b.empty())
391 throw std::invalid_argument(
"BusValue multiplication arguments can't be empty");
393 const auto width = a.size() + b.size();
399 for (
auto [index, s] : b | SILICON::views::enumerate) {
405 const auto intermediateResult = aExtended
406 | std::views::transform([s](
const State bit) {
409 | std::ranges::to<BusValue>();
412 auto partial =
shift(intermediateResult, -index);
415 const bool isSignBit = index ==
static_cast<long>(b.size() - 1);
428 if (a.empty() || b.empty())
429 throw std::invalid_argument(
"BusValue division arguments can't be empty");
431 if (std::ranges::all_of(b, [](
const auto s) {
return s ==
State::LOW; })) {
432 throw std::invalid_argument(
"BusValue division by 0");
436 return std::ranges::contains(v, s);
450 boost::multiprecision::divide_qr(dividend, divisor, quotient, remainder);
459 for (
unsigned short i = 0; i <
size; i++)
465 const size_t oldSize = this->
busData.size();
467 for (
size_t i = oldSize; i <
size; i++)
472{ this->busData = std::move(
busData); }
482 for (
const auto& val : initList) {
483 busData[i++] = std::make_shared<Wire>(val);
490 for (
auto i = 0uz; i <
size(); ++i) {
491 const auto currentWire = this->
busData[i];
496 currentWire->forceSetCurrentState(normalized[i]);
505 for (
auto i = 0uz; i <
size(); ++i) {
506 const auto currentWire = this->
busData[i];
511 currentWire->forceSetCurrentState(normalized[i], authorizedBy);
519 for (
auto i = 0uz; i <
size(); ++i) {
520 const auto currentWire = this->
busData[i];
532 return this->
busData | std::views::transform([](
const auto& wire) {
535 | std::ranges::to<BusValue>();
540 return std::ranges::any_of(
busData, [](
const auto& el) {
547 return std::ranges::any_of(
busData, [](
const auto& el) {
554 for (
const auto& localWire :
busData) {
558 for (
const auto& otherWire : other.
busData) {
559 if (otherWire && localWire.get() == otherWire.get())
568 return std::lexicographical_compare_three_way(
570 [](
const auto& a,
const auto& b) {
572 return std::strong_ordering::equal;
574 return std::strong_ordering::less;
576 return std::strong_ordering::greater;
578 return a->getId() <=> b->getId();
584 return std::ranges::equal(
busData, other.
busData, [](
const auto& a,
const auto& b) {
585 return a.get() == b.get();
bool setCurrentValue(const BusValue &value, const Component_weakPtr &requestedBy)
bool forceSetCurrentValue(const BusValue &value)
bool operator==(const Bus &other) const
std::vector< Wire_ptr > busData
bool isInErrorState() const
bool sharesWireWith(const Bus &other) const
void setSize(unsigned short size)
std::strong_ordering operator<=>(const Bus &other) const
BusValue getCurrentValue() const
State getCurrentState() const
Reads the current state safely while simulation may update it.
static State safeGetCurrentState(const std::weak_ptr< Wire > &w)
void clearAuthorizedComponent()
Clears the component currently authorized to drive this wire.
static std::atomic< uint64_t > nextId
Component_weakPtr authorizedComponent
void setCurrentState(State newState, const Component_weakPtr &requestedBy)
static void safeSetCurrentState(const std::weak_ptr< Wire > &w, State newState, const Component_weakPtr &requestedBy)
std::atomic< State > currentState
State shared between the simulation worker and UI readers.
Wire & operator=(const Wire &other)
Assigns state, authorization, and identifier from another wire.
void forceSetCurrentState(State newState)
static void error(std::string_view category, std::string_view message)
BusValue shift(const BusValue &v, const ptrdiff_t amount, const Signedness signedness)
State operator!(const State &a)
BusValue operator+(const BusValue &a, const BusValue &b)
std::partial_ordering compare(const BusValue &lhs, const BusValue &rhs, const Signedness signedness)
State operator||(const State &a, const State &b)
boost::multiprecision::cpp_int BigInt
std::pair< BusValue, BusValue > divide(const BusValue &a, const BusValue &b, Signedness signedness)
State operator&&(const State &a, const State &b)
BigInt integerFromBusValue(const BusValue &value, const Signedness signedness)
Decodes an LSB-first BusValue as an unsigned arbitrary-precision integer. Empty values yield zero....
State operator^(const State &a, const State &b)
BusValue twosComplement(const BusValue &n)
std::weak_ptr< Component > Component_weakPtr
std::string to_str(State s)
BusValue busValueFromInteger(const BigInt &value, const std::size_t width)
Builds an LSB-first BusValue from the low width bits of an integer. Negative values use two's complem...
BusValue multiply(const BusValue &a, const BusValue &b, const Signedness signedness)
std::vector< State > BusValue
bool busWillChangeToValue(const Bus &bus, const BusValue &value)
Checks whether assigning a binary value would change any driven bus wire.
BusValue normalizeBusValue(const BusValue &value, const std::size_t width, const State extension)
Resize an LSB-first value to an exact bus width.
State normalizeBinaryOrUnknown(const State state)
Normalizes non-binary states for stored binary elements.
bool fitsUnsigned(const BusValue &value, const std::size_t width)
Tests whether an LSB-first value is representable as an unsigned value.
bool fitsSigned(const BusValue &value, const std::size_t width)
Tests whether an LSB-first two's-complement value fits a signed width.
bool busValueOverflowsWidth(const BusValue &value, const std::size_t width)
True when truncating to width would discard a non-low bit.
bool mayBe(const State actual, const State expected)
Tests whether an actual state could represent an expected binary state.
bool isKnownBinary(const State state)
Tests whether a state is a concrete binary logic value.