Skip to content

Commit ebfc7cc

Browse files
committed
Namespace simulation models
1 parent 11eaa4b commit ebfc7cc

File tree

6 files changed

+39
-32
lines changed

6 files changed

+39
-32
lines changed

chipflow_lib/common/sim/main.cc.jinja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
using namespace cxxrtl::time_literals;
1414
using namespace cxxrtl_design;
15+
using namespace chipflow;
1516

1617
int main(int argc, char **argv) {
1718
p_sim__top top;

chipflow_lib/common/sim/models.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <unordered_map>
1010
#include "models.h"
1111

12-
namespace cxxrtl_design {
12+
namespace chipflow {
1313

1414
// Helper functions
1515

@@ -134,8 +134,10 @@ void close_event_log() {
134134
}
135135
}
136136

137+
namespace models {
138+
137139
// SPI flash
138-
void spiflash_model::load_data(const std::string &filename, unsigned offset) {
140+
void spiflash::load_data(const std::string &filename, unsigned offset) {
139141
std::ifstream in(filename, std::ifstream::binary);
140142
if (offset >= data.size()) {
141143
throw std::out_of_range("flash: offset beyond end");
@@ -145,7 +147,7 @@ void spiflash_model::load_data(const std::string &filename, unsigned offset) {
145147
}
146148
in.read(reinterpret_cast<char*>(data.data() + offset), (data.size() - offset));
147149
}
148-
void spiflash_model::step(unsigned timestamp) {
150+
void spiflash::step(unsigned timestamp) {
149151
auto process_byte = [&]() {
150152
s.out_buffer = 0;
151153
if (s.byte_count == 0) {
@@ -221,7 +223,7 @@ void spiflash_model::step(unsigned timestamp) {
221223

222224
// UART
223225

224-
void uart_model::step(unsigned timestamp) {
226+
void uart::step(unsigned timestamp) {
225227

226228
for (auto action : get_pending_actions(name)) {
227229
if (action.event == "tx") {
@@ -275,7 +277,7 @@ void uart_model::step(unsigned timestamp) {
275277
}
276278

277279
// Generic SPI model
278-
void spi_model::step(unsigned timestamp) {
280+
void spi::step(unsigned timestamp) {
279281
for (auto action : get_pending_actions(name)) {
280282
if (action.event == "set_data") {
281283
s.out_buffer = s.send_data = uint32_t(action.payload);
@@ -308,7 +310,7 @@ void spi_model::step(unsigned timestamp) {
308310
}
309311

310312
// Generic I2C model
311-
void i2c_model::step(unsigned timestamp) {
313+
void i2c::step(unsigned timestamp) {
312314
bool sda = !bool(sda_oe), scl = !bool(scl_oe);
313315

314316
for (auto action : get_pending_actions(name)) {
@@ -370,4 +372,5 @@ void i2c_model::step(unsigned timestamp) {
370372
scl_i.set(scl);
371373
}
372374

373-
}
375+
} //chipflow::models
376+
} //chipflow

chipflow_lib/common/sim/models.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "vendor/nlohmann/json.hpp"
1111

1212

13-
namespace cxxrtl_design {
13+
namespace chipflow {
1414

1515
using namespace cxxrtl;
1616

@@ -30,9 +30,12 @@ void log_event(unsigned timestamp, const std::string &peripheral, const std::str
3030
std::vector<action> get_pending_actions(const std::string &peripheral);
3131
void close_event_log();
3232

33-
struct spiflash_model {
33+
namespace models {
34+
35+
36+
struct spiflash {
3437
std::string name;
35-
spiflash_model(const std::string &name, const value<1> &clk, const value<1> &csn, const value<4> &d_o, const value<4> &d_oe, value<4> &d_i) :
38+
spiflash(const std::string &name, const value<1> &clk, const value<1> &csn, const value<4> &d_o, const value<4> &d_oe, value<4> &d_i) :
3639
name(name), clk(clk), csn(csn), d_o(d_o), d_oe(d_oe), d_i(d_i) {
3740
data.resize(16*1024*1024);
3841
std::fill(data.begin(), data.end(), 0xFF); // flash starting value
@@ -61,9 +64,9 @@ struct spiflash_model {
6164
} s;
6265
};
6366

64-
struct uart_model {
67+
struct uart {
6568
std::string name;
66-
uart_model(const std::string &name, const value<1> &tx, value<1> &rx, unsigned baud_div = 25000000/115200) : name(name), tx(tx), rx(rx), baud_div(baud_div) {};
69+
uart(const std::string &name, const value<1> &tx, value<1> &rx, unsigned baud_div = 25000000/115200) : name(name), tx(tx), rx(rx), baud_div(baud_div) {};
6770

6871
void step(unsigned timestamp);
6972
private:
@@ -83,10 +86,10 @@ struct uart_model {
8386
};
8487

8588
template<int pin_count>
86-
struct gpio_model {
89+
struct gpio {
8790
std::string name;
8891

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) {};
92+
gpio(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) {};
9093

9194
void step(unsigned timestamp);
9295

@@ -104,7 +107,7 @@ struct gpio_model {
104107

105108
// GPIO
106109
template<int pin_count>
107-
void gpio_model<pin_count>::step(unsigned timestamp) {
110+
void gpio<pin_count>::step(unsigned timestamp) {
108111
uint32_t o_value = o.template get<uint32_t>();
109112
uint32_t oe_value = oe.template get<uint32_t>();
110113

@@ -135,9 +138,9 @@ void gpio_model<pin_count>::step(unsigned timestamp) {
135138
s.oe_last = oe_value;
136139
}
137140

138-
struct spi_model {
141+
struct spi {
139142
std::string name;
140-
spi_model(const std::string &name, const value<1> &clk, const value<1> &copi, value<1> &cipo, const value<1> &csn) :
143+
spi(const std::string &name, const value<1> &clk, const value<1> &copi, value<1> &cipo, const value<1> &csn) :
141144
name(name), clk(clk), csn(csn), copi(copi), cipo(cipo) {
142145
};
143146

@@ -159,9 +162,9 @@ struct spi_model {
159162
} s;
160163
};
161164

162-
struct i2c_model {
165+
struct i2c {
163166
std::string name;
164-
i2c_model(const std::string &name, const value<1> &scl_o, const value<1> &scl_oe, value<1> &scl_i, const value<1> &sda_o, const value<1> &sda_oe, value<1> &sda_i) : name(name), sda_oe(sda_oe), sda_i(sda_i), scl_oe(scl_oe), scl_i(scl_i) {};
167+
i2c(const std::string &name, const value<1> &scl_o, const value<1> &scl_oe, value<1> &scl_i, const value<1> &sda_o, const value<1> &sda_oe, value<1> &sda_i) : name(name), sda_oe(sda_oe), sda_i(sda_i), scl_oe(scl_oe), scl_i(scl_i) {};
165168

166169
void step(unsigned timestamp);
167170
private:
@@ -184,6 +187,7 @@ struct i2c_model {
184187
};
185188

186189

187-
}
190+
} //chipflow::simulation
191+
} //chipflow
188192

189193
#endif

chipflow_lib/platforms/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
)
2222

2323

24-
from amaranth.lib import wiring, io, meta
24+
from amaranth.lib import wiring, io
2525
from amaranth.lib.wiring import In, Out
2626
from pydantic import (
27-
ConfigDict, TypeAdapter, PlainSerializer,
27+
ConfigDict, PlainSerializer,
2828
WrapValidator
2929
)
3030

chipflow_lib/platforms/silicon.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ def instantiate_toplevel(self):
303303
def wire_up(self, m, wire):
304304
super().wire_up(m, wire)
305305

306+
# wire up drive mode bits
307+
306308
if hasattr(wire, 'drive_mode'):
307309
m.d.comb += self.drive_mode.eq(wire.drive_mode)
308310

chipflow_lib/platforms/sim.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class SimModel:
4343
capabilities: List of capabilities of the model.
4444
"""
4545
name: str
46+
namespace: str
4647
signature: Type[wiring.Signature]
4748
capabilities: Optional[List[SimModelCapability]] = None
4849

@@ -100,9 +101,6 @@ class BasicCxxBuilder(BaseModel):
100101
def model_post_init(self, *args, **kwargs):
101102
self._table = { getattr(m.signature,'__chipflow_uid__'): m for m in self.models }
102103

103-
def uid_to_c(self, uid: str) -> str:
104-
return uid.replace('.','__')
105-
106104
def instantiate_model(self, interface: str, sim_interface: SimInterface, interface_desc: Interface, ports: Dict[str, io.SimulationPort]) -> str:
107105
uid = sim_interface['uid']
108106
parameters = sim_interface['parameters']
@@ -115,11 +113,10 @@ def instantiate_model(self, interface: str, sim_interface: SimInterface, interfa
115113
sig_names = [ path for path, _, _ in members ]
116114
port_names = { n: interface_desc[n].port_name for n in interface_desc.keys()}
117115

118-
identifier_uid = self.uid_to_c(uid)
119116
names = [f"\\io${port_names[str(n)]}${d}" for n,d in sig_names]
120117
params = [f"top.{cxxrtlmangle(n)}" for n in names]
121118

122-
cpp_class = model.name
119+
cpp_class = f"{model.namespace}::{model.name}"
123120
if len(parameters):
124121
template_params = []
125122
for p,v in parameters:
@@ -141,11 +138,11 @@ def find_builder(builders: List[BasicCxxBuilder], sim_interface: SimInterface):
141138

142139
_COMMON_BUILDER = BasicCxxBuilder(
143140
models=[
144-
SimModel('spi_model', SPISignature),
145-
SimModel('spiflash_model', QSPIFlashSignature, [SimModelCapability.LOAD_DATA]),
146-
SimModel('uart_model', UARTSignature),
147-
SimModel('i2c_model', I2CSignature),
148-
SimModel('gpio_model', GPIOSignature),
141+
SimModel('spi', 'chipflow::models', SPISignature),
142+
SimModel('spiflash', 'chipflow::models', QSPIFlashSignature, [SimModelCapability.LOAD_DATA]),
143+
SimModel('uart', 'chipflow::models', UARTSignature),
144+
SimModel('i2c', 'chipflow::models', I2CSignature),
145+
SimModel('gpio', 'chipflow::models', GPIOSignature),
149146
],
150147
cpp_files=[ Path('{COMMON_DIR}', 'models.cc') ],
151148
hpp_files=[ Path('models.h') ],

0 commit comments

Comments
 (0)