29 uint64_t gateDelay(
const Gate& gate)
31 return gate.getPropertyValue<
int>(
"delay").value_or(0);
34 bool gateBitwiseEnabled(
const Gate& gate)
36 return gate.getPropertyValue<
bool>(
"bitwise").value_or(
false);
39 int gateWidth(
const Gate& gate)
41 return gate.getPropertyValue<
int>(
"size").value_or(1);
47 for (
const auto& wire : bus)
52 template <
typename Reducer,
typename Finalizer = std::
identity>
53 void simulateBitwiseGate(
Gate& gate, SILICON::simulation::Simulator& sim,
54 State initialState, Reducer&& reducer,
55 Finalizer&& finalizer = {})
57 const auto delay = gateDelay(gate);
58 const auto inputs =
gate.getInputs();
59 const auto outputs =
gate.getOutputs();
61 if (!gateBitwiseEnabled(gate)) {
62 State result = initialState;
63 for (
const auto& input : inputs) {
64 if (gateWidth(gate) > 1) {
65 for (
const auto& wire : input)
68 result = reducer(result, busTruthValue(input));
72 sim.
updateWire(outputs[0][0], finalizer(result), delay,
gate.weak_from_this());
76 const int width = gateWidth(gate);
77 for (
int bit = 0; bit <
width; ++bit) {
78 State result = initialState;
79 for (
const auto& input : inputs)
82 sim.
updateWire(outputs[0][bit], finalizer(result), delay,
gate.weak_from_this());
100 const bool enableBitwiseProperties)
101 :
Gate(enableBitwiseProperties)
104 throw std::invalid_argument(
"Gate requires at least one input");
107 for (
const auto& input :
inputs)
108 this->inputs.push_back({input});
109 this->outputs = {{output}};
119 if (!enableBitwiseProperties)
126 const bool bitwise = std::get<bool>(value);
147 if (newInputs.size() > 1)
150 const bool reduction = !bitwise && size > 1;
152 newInputs.resize(std::min(newInputs.size(), inputCount));
153 while (newInputs.size() < inputCount)
154 newInputs.emplace_back(1);
155 for (
auto& input : newInputs)
156 input.setSize(
static_cast<unsigned short>(size));
159 for (
auto& output : newOutputs)
160 output.setSize(
static_cast<unsigned short>(bitwise ? size : 1));
172 for (
auto& input : newInputs)
173 input.setSize(
static_cast<unsigned short>(width));
177 for (
auto& output : newOutputs)
178 output.setSize(
static_cast<unsigned short>(width));
188 throw std::invalid_argument(
"AndGate requires at least 2 inputs");
194 [](
const State lhs,
const State rhs) {
return lhs && rhs; });
201 throw std::invalid_argument(
"OrGate requires at least 2 inputs");
207 [](
const State lhs,
const State rhs) {
return lhs || rhs; });
217 initializeNotProperties();
241 throw std::logic_error(
"NOT gate input and output widths must match");
243 for (std::size_t bit = 0; bit <
outputs[0].size(); ++bit) {
245 outputs[0][
static_cast<unsigned short>(bit)],
255 throw std::invalid_argument(
"NandGate requires at least 2 inputs");
263 [](
const State lhs,
const State rhs) {
return lhs && rhs; },
264 [](
const State state) {
return !state; });
271 throw std::invalid_argument(
"NorGate requires at least 2 inputs");
279 [](
const State state) {
return !state; });
285 setProperty(
"delay", 7);
291 [](
const State lhs,
const State rhs) {
return lhs ^ rhs; });
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
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 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::vector< Bus > getInputs() const
Gets all input buses.
Base class for combinational logic gates.
void initializeProperties(bool enableBitwiseProperties)
int setSize(int width)
Resizes every input bus and the output bus to the same width.
Gate(const std::vector< Wire_ptr > &inputs, Wire_ptr output)
Constructs a gate with scalar I/O and bitwise properties enabled.
Gate()
Constructs an empty gate instance for factory/serialization usage.
void configureIO(int size, bool bitwise)
std::size_t scalarInputCount
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
void initializeNotProperties()
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
static State safeGetCurrentState(const std::weak_ptr< Wire > &w)
void simulate(SILICON::simulation::Simulator &sim) override
Executes the component's logic.
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.
int width(RTLIL::Module *module, RTLIL::Cell *cell, RTLIL::IdString key)
std::optional< Gate > gate(Cell *cell, SigMap &signals)
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.