Skip to content

Commit 11eaa4b

Browse files
committed
Enable c++ model parameterisation, apply to gpio
1 parent 8f5cc26 commit 11eaa4b

File tree

3 files changed

+35
-39
lines changed

3 files changed

+35
-39
lines changed

chipflow_lib/common/sim/models.cc

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -274,39 +274,6 @@ void uart_model::step(unsigned timestamp) {
274274
}
275275
}
276276

277-
// GPIO
278-
279-
void gpio_model::step(unsigned timestamp) {
280-
uint32_t o_value = o.get<uint32_t>();
281-
uint32_t oe_value = oe.get<uint32_t>();
282-
283-
for (auto action : get_pending_actions(name)) {
284-
if (action.event == "set") {
285-
auto bin = std::string(action.payload);
286-
input_data = 0;
287-
for (unsigned i = 0; i < pin_count; i++) {
288-
if (bin.at((pin_count - 1) - i) == '1')
289-
input_data |= (1U << i);
290-
}
291-
}
292-
}
293-
294-
if (o_value != s.o_last || oe_value != s.oe_last) {
295-
std::string formatted_value;
296-
for (int i = pin_count - 1; i >= 0; i--) {
297-
if (oe_value & (1U << unsigned(i)))
298-
formatted_value += (o_value & (1U << unsigned(i))) ? '1' : '0';
299-
else
300-
formatted_value += 'Z';
301-
}
302-
log_event(timestamp, name, "change", json(formatted_value));
303-
}
304-
305-
i.set((input_data & ~oe_value) | (o_value & oe_value));
306-
s.o_last = o_value;
307-
s.oe_last = oe_value;
308-
}
309-
310277
// Generic SPI model
311278
void spi_model::step(unsigned timestamp) {
312279
for (auto action : get_pending_actions(name)) {

chipflow_lib/common/sim/models.h

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,11 @@ struct uart_model {
8282
} s;
8383
};
8484

85+
template<int pin_count>
8586
struct gpio_model {
8687
std::string name;
87-
struct parameters {
88-
unsigned pin_count;
89-
};
90-
parameters parameters;
9188

92-
gpio_model(const std::string &name, parameters, const value<paramters.pin_count> &o, const value<parameters.pin_count> &oe, value<parameters.pin_count> &i) : name(name), parameters(parameters), o(o), oe(oe), i(i) {};
89+
gpio_model(const std::string &name, const value<pin_count> &o, const value<pin_count> &oe, value<pin_count> &i) : name(name), o(o), oe(oe), i(i) {};
9390

9491
void step(unsigned timestamp);
9592

@@ -105,6 +102,39 @@ struct gpio_model {
105102
};
106103

107104

105+
// GPIO
106+
template<int pin_count>
107+
void gpio_model<pin_count>::step(unsigned timestamp) {
108+
uint32_t o_value = o.template get<uint32_t>();
109+
uint32_t oe_value = oe.template get<uint32_t>();
110+
111+
for (auto action : get_pending_actions(name)) {
112+
if (action.event == "set") {
113+
auto bin = std::string(action.payload);
114+
input_data = 0;
115+
for (unsigned i = 0; i < pin_count; i++) {
116+
if (bin.at((pin_count - 1) - i) == '1')
117+
input_data |= (1U << i);
118+
}
119+
}
120+
}
121+
122+
if (o_value != s.o_last || oe_value != s.oe_last) {
123+
std::string formatted_value;
124+
for (int i = pin_count - 1; i >= 0; i--) {
125+
if (oe_value & (1U << unsigned(i)))
126+
formatted_value += (o_value & (1U << unsigned(i))) ? '1' : '0';
127+
else
128+
formatted_value += 'Z';
129+
}
130+
log_event(timestamp, name, "change", json(formatted_value));
131+
}
132+
133+
i.set((input_data & ~oe_value) | (o_value & oe_value));
134+
s.o_last = o_value;
135+
s.oe_last = oe_value;
136+
}
137+
108138
struct spi_model {
109139
std::string name;
110140
spi_model(const std::string &name, const value<1> &clk, const value<1> &copi, value<1> &cipo, const value<1> &csn) :

chipflow_lib/platforms/_signatures.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from amaranth.lib import wiring
88
from amaranth.lib.wiring import Out
99

10-
from .. import ChipFlowError
1110
from ._utils import InputIOSignature, OutputIOSignature, BidirIOSignature, IOModelOptions, _chipflow_schema_uri
1211
from ._annotate import amaranth_annotate
1312

0 commit comments

Comments
 (0)