29using namespace SILICON::core;
32 unsigned int parseWidth(
const std::string_view text,
const bool allowZero =
false)
34 unsigned int value = 0;
35 const auto [end, error] =
36 std::from_chars(text.data(), text.data() + text.size(), value);
37 if (text.empty() || error != std::errc{} || end != text.data() + text.size()
38 || (!allowZero && value == 0)
39 || value > std::numeric_limits<unsigned short>::max())
40 throw std::invalid_argument(
"Invalid splitter/merger bit range or width");
44 template <
typename Callback>
void parseList(std::string_view text, Callback callback)
46 while (!text.empty()) {
47 const auto comma = text.find(
',');
48 callback(text.substr(0, comma));
49 if (comma == std::string_view::npos)
51 text.remove_prefix(comma + 1);
53 throw std::invalid_argument(
"Splitter/merger list has a trailing comma");
57 std::vector<Bus> resizedBuses(
const std::vector<Bus>& buses,
58 const std::vector<unsigned int>& widths)
60 std::vector<Bus> result;
61 result.reserve(widths.size());
62 for (std::size_t i = 0; i < widths.size(); ++i)
63 result.push_back(i < buses.size() && buses[i].size() == widths[i]
65 :
Bus(
static_cast<unsigned short>(widths[i])));
77 this->
inputs = {std::move(input)};
80 this->
inputs[0].size() == 0 ? 2 :
static_cast<int>(this->
inputs[0].size()));
96 const int newSize = std::get<int>(
requireValidSize(
"WireSplitter size", value));
97 for (
const auto& [offset, width] :
slices)
98 if (offset + width >
static_cast<unsigned int>(newSize))
99 throw std::invalid_argument(
"WireSplitter slice exceeds its input width");
100 if (!this->
inputs.empty())
105 return setSlices(std::get<std::string>(value));
112 std::vector<unsigned int> outputWidths;
114 outputWidths.assign(newSize, 1);
116 for (
const auto& [offset, width] :
slices) {
117 if (offset + width >
static_cast<unsigned int>(newSize))
118 throw std::invalid_argument(
"WireSplitter slice exceeds its input width");
119 outputWidths.push_back(width);
130 std::vector<std::pair<unsigned int, unsigned int>> parsed;
132 parseList(specification, [&](
const std::string_view item) {
133 const auto colon = item.find(
':');
134 if (colon == std::string_view::npos)
135 throw std::invalid_argument(
"WireSplitter slices use offset:width pairs");
136 const auto offset = parseWidth(item.substr(0, colon),
true);
137 const auto width = parseWidth(item.substr(colon + 1));
138 if (offset + width >
static_cast<unsigned int>(size))
139 throw std::invalid_argument(
"WireSplitter slice exceeds its input width");
140 parsed.emplace_back(offset, width);
142 slices = std::move(parsed);
145 return specification;
152 std::vector<std::pair<unsigned int, unsigned int>> result;
154 :
static_cast<int>(
inputs[0].size());
155 for (
int i = 0; i < size; ++i)
156 result.emplace_back(i, 1);
166 for (std::size_t port = 0; port <
outputs.size() && port < ranges.size(); ++port)
167 for (
unsigned int bit = 0; bit <
outputs[port].size() && bit < ranges[port].second;
171 delay, weak_from_this());
182 this->
outputs = {std::move(output)};
185 :
static_cast<int>(this->
outputs[0].size()));
201 const int newSize = std::get<int>(
requireValidSize(
"WireMerger size", value));
204 !=
static_cast<unsigned int>(newSize))
205 throw std::invalid_argument(
"WireMerger input widths must sum to its output width");
211 return setWidths(std::get<std::string>(value));
220 !=
static_cast<unsigned int>(newSize))
221 throw std::invalid_argument(
"WireMerger input widths must sum to its output width");
230 std::vector<unsigned int> parsed;
231 parseList(specification,
232 [&](
const std::string_view item) { parsed.push_back(parseWidth(item)); });
235 && std::accumulate(parsed.begin(), parsed.end(), std::uint64_t{0})
236 !=
static_cast<unsigned int>(size))
237 throw std::invalid_argument(
"WireMerger input widths must sum to its output width");
238 widths = std::move(parsed);
241 return specification;
249 const auto inputWidths =
251 unsigned int offset = 0;
252 for (std::size_t port = 0; port < inputWidths.size(); ++port)
253 for (
unsigned int bit = 0; bit < inputWidths[port]; ++bit, ++offset) {
258 if (offset <
outputs[0].size())
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 > 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.
static State safeGetCurrentState(const std::weak_ptr< Wire > &w)
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.
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.
QVariant value(const QSettings &settings, const Definition &setting)
Reads a setting value, falling back to its declared default when unset.