30 [[nodiscard]] std::size_t packedWordCount(
const std::size_t bytes,
31 const std::size_t width)
35 if (bytes > std::numeric_limits<std::size_t>::max() / 8)
36 throw std::invalid_argument(
"ROM binaryContents is too large");
37 const std::size_t bits = bytes * 8;
38 return bits /
width +
static_cast<std::size_t
>(bits %
width != 0);
41 [[nodiscard]]
unsigned short addressWidthFor(
const std::size_t words)
45 return static_cast<unsigned short>(std::bit_width(words - 1));
61 const auto& slug = std::get<std::string>(value);
71 if (address.
size() == 0)
72 throw std::invalid_argument(
"ROM address bus must not be empty");
74 throw std::invalid_argument(
"ROM data bus must not be empty");
76 inputs = {std::move(address), {std::move(chipSelect)}, {std::move(outputEnable)}};
77 outputs = {std::move(data)};
78 setProperty(
"dataWidth",
static_cast<int>(outputs[0].size()));
91void ROM::configure(
const int dataWidth, std::shared_ptr<const std::string> nextContent,
92 const bool rejectInvalid,
const bool reshape,
93 const bool preferAddressDepth,
const std::size_t explicitWords)
97 bool nextResolved =
false;
98 std::size_t nextWords = 0;
100 nextWords = packedWordCount(nextContent->size(),
static_cast<std::size_t
>(width));
101 if (explicitWords != 0) {
102 if (!std::has_single_bit(explicitWords)
103 || explicitWords > std::numeric_limits<std::size_t>::max() /
static_cast<std::size_t
>(width)
104 || nextContent->size() != (explicitWords *
static_cast<std::size_t
>(width) + 7) / 8)
105 throw std::invalid_argument(
"ROM image does not match its explicit word count");
106 nextWords = explicitWords;
107 }
else if (preferAddressDepth && !nextContent->empty() && !
inputs.empty()) {
109 const auto addressBits = address.size();
110 if (addressBits < std::numeric_limits<std::size_t>::digits) {
111 const auto candidate = std::size_t{1} << addressBits;
112 if (candidate <= std::numeric_limits<std::size_t>::max() /
static_cast<std::size_t
>(width)
113 && nextContent->size() == (candidate *
static_cast<std::size_t
>(width) + 7) / 8)
114 nextWords = candidate;
117 nextResolved = nextWords != 0 && std::has_single_bit(nextWords);
118 if (nextWords != 0 && !nextResolved && rejectInvalid)
119 throw std::invalid_argument(
120 "ROM binaryContents must contain a power-of-two number of packed words");
123 content = std::move(nextContent);
125 wordCount = nextResolved ? nextWords : 0;
140 newOutputs.resize(1);
152 const std::size_t words =
153 packedWordCount(contents->size(),
static_cast<std::size_t
>(width));
154 if (words != 0 && !std::has_single_bit(words))
155 throw std::invalid_argument(
156 "ROM binaryContents must contain a power-of-two number of packed words");
167 configure(width, std::move(contents),
true,
true);
176 const std::size_t words)
209 std::size_t address = 0;
214 for (std::size_t bit = 0; bit < addressSize; ++bit) {
225 address |= std::size_t{1} << bit;
231 const std::size_t firstBit = address * dataWidth;
232 for (std::size_t bit = 0; bit < dataWidth; ++bit) {
233 const std::size_t sourceBit = firstBit + bit;
234 const std::size_t byteIndex = sourceBit / 8;
237 const auto byte =
static_cast<unsigned char>((*content)[byteIndex]);
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.
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
bool settingBinaryDocument
void configure(int dataWidth, std::shared_ptr< const std::string > contents, bool rejectInvalid, bool reshape, bool preferAddressDepth=false, std::size_t explicitWords=0)
int configuredDataWidth() const
std::string configuredBinaryContents() const
bool initializingProperties
void reshapeBuses(int dataWidth, std::size_t words)
void driveState(SILICON::simulation::Simulator &sim, State state)
std::shared_ptr< const std::string > binaryContentsSnapshot() const noexcept
std::shared_ptr< const std::string > content
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
void refreshBinaryContents(std::shared_ptr< const std::string > contents)
void setBinaryDocument(std::string slug, std::shared_ptr< const std::string > contents)
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.
int width(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key)
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.
std::vector< State > BusValue