Skip to content

Commit 731c857

Browse files
committed
Namespace simulation models
1 parent 34954d7 commit 731c857

File tree

8 files changed

+45
-53
lines changed

8 files changed

+45
-53
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/_annotate.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from types import MethodType
22
import pydantic
3-
from amaranth.lib import meta
4-
from typing import Any, Annotated, NamedTuple, Self, TypeVar
5-
from typing_extensions import TypedDict, is_typeddict
3+
from typing import TypeVar
4+
from typing_extensions import is_typeddict
65
_T_TypedDict = TypeVar('_T_TypedDict')
76

87
def amaranth_annotate(modeltype: type['_T_TypedDict'], schema_id: str, member='__chipflow_annotation__', decorate_object = False):

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

chipflow_lib/platforms/_utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,17 @@
1313
from enum import Enum, IntEnum, StrEnum, auto
1414
from math import ceil, floor
1515
from typing import (
16-
Any, Annotated, NamedTuple, Self, Type,
17-
TYPE_CHECKING
16+
Any, Annotated, NamedTuple, Self, TYPE_CHECKING
1817
)
1918
from typing_extensions import (
2019
TypedDict, Unpack, NotRequired
2120
)
2221

2322

24-
from amaranth.lib import wiring, io, meta
23+
from amaranth.lib import wiring, io
2524
from amaranth.lib.wiring import In, Out
2625
from pydantic import (
27-
ConfigDict, TypeAdapter, PlainSerializer,
26+
ConfigDict, PlainSerializer,
2827
WrapValidator
2928
)
3029

chipflow_lib/platforms/silicon.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from amaranth import Module, Signal, ClockDomain, ClockSignal, ResetSignal, unsigned
1515
from amaranth.lib import wiring, io, data
1616
from amaranth.lib.cdc import FFSynchronizer
17-
from amaranth.lib.wiring import Component, In, PureInterface
17+
from amaranth.lib.wiring import Component, In
1818
from amaranth.back import rtlil #type: ignore[reportAttributeAccessIssue]
1919
from amaranth.hdl import Fragment
2020
from amaranth.hdl._ir import PortDirection
@@ -333,16 +333,6 @@ def __init__(self,
333333
self._gpio_analog_sel = None # analog mux select
334334
self._gpio_analog_pol = None # analog mux select
335335

336-
def wire(self, m: Module, interface: PureInterface):
337-
super().wire(m, interface)
338-
339-
# wire up drive mode bits
340-
bit = 0
341-
for i in self._dms:
342-
m.d.comb += self._dm0[bit].eq(i[0]) # type: ignore
343-
m.d.comb += self._dm1[bit].eq(i[1]) # type: ignore
344-
m.d.comb += self._dm2[bit].eq(i[2]) # type: ignore
345-
346336
def instantiate_toplevel(self):
347337
ports = super().instantiate_toplevel()
348338
for s, d in self._signals:
@@ -354,6 +344,8 @@ def instantiate_toplevel(self):
354344
def wire_up(self, m, wire):
355345
super().wire_up(m, wire)
356346

347+
# wire up drive mode bits
348+
357349
if hasattr(wire, 'drive_mode'):
358350
m.d.comb += self.drive_mode.eq(wire.drive_mode)
359351

chipflow_lib/platforms/sim.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
import os
55
import sys
66

7-
from collections import defaultdict
87
from dataclasses import dataclass
98
from enum import StrEnum
109
from pathlib import Path
1110
from pprint import pformat
1211
from typing import Dict, List, Optional, Type
1312

14-
from amaranth import Value, ValueCastable, Module, Signal, ClockSignal, ResetSignal, ClockDomain
15-
from amaranth.lib import io, meta, wiring
16-
from amaranth.lib.wiring import In, Out
13+
from amaranth import Module, ClockSignal, ResetSignal, ClockDomain
14+
from amaranth.lib import io, wiring
1715
from amaranth.back import rtlil # type: ignore[reportAttributeAccessIssue]
1816
from amaranth.hdl._ir import PortDirection
1917
from amaranth.lib.cdc import FFSynchronizer
@@ -46,6 +44,7 @@ class SimModel:
4644
capabilities: List of capabilities of the model.
4745
"""
4846
name: str
47+
namespace: str
4948
signature: Type[wiring.Signature]
5049
capabilities: Optional[List[SimModelCapability]] = None
5150

@@ -103,9 +102,6 @@ class BasicCxxBuilder(BaseModel):
103102
def model_post_init(self, *args, **kwargs):
104103
self._table = { getattr(m.signature,'__chipflow_uid__'): m for m in self.models }
105104

106-
def uid_to_c(self, uid: str) -> str:
107-
return uid.replace('.','__')
108-
109105
def instantiate_model(self, interface: str, sim_interface: SimInterface, interface_desc: Interface, ports: Dict[str, io.SimulationPort]) -> str:
110106
uid = sim_interface['uid']
111107
parameters = sim_interface['parameters']
@@ -120,11 +116,10 @@ def instantiate_model(self, interface: str, sim_interface: SimInterface, interfa
120116
sig_names = [ path for path, _, _ in members ]
121117
port_names = { n: interface_desc[n].port_name for n in interface_desc.keys()}
122118

123-
identifier_uid = self.uid_to_c(uid)
124119
names = [f"\\io${port_names[str(n)]}${d}" for n,d in sig_names]
125120
params = [f"top.{cxxrtlmangle(n)}" for n in names]
126121

127-
cpp_class = model.name
122+
cpp_class = f"{model.namespace}::{model.name}"
128123
if len(parameters):
129124
template_params = []
130125
for p,v in parameters:
@@ -146,11 +141,11 @@ def find_builder(builders: List[BasicCxxBuilder], sim_interface: SimInterface):
146141

147142
_COMMON_BUILDER = BasicCxxBuilder(
148143
models=[
149-
SimModel('spi_model', SPISignature),
150-
SimModel('spiflash_model', QSPIFlashSignature, [SimModelCapability.LOAD_DATA]),
151-
SimModel('uart_model', UARTSignature),
152-
SimModel('i2c_model', I2CSignature),
153-
SimModel('gpio_model', GPIOSignature),
144+
SimModel('spi', 'chipflow::models', SPISignature),
145+
SimModel('spiflash', 'chipflow::models', QSPIFlashSignature, [SimModelCapability.LOAD_DATA]),
146+
SimModel('uart', 'chipflow::models', UARTSignature),
147+
SimModel('i2c', 'chipflow::models', I2CSignature),
148+
SimModel('gpio', 'chipflow::models', GPIOSignature),
154149
],
155150
cpp_files=[ Path('{COMMON_DIR}', 'models.cc') ],
156151
hpp_files=[ Path('models.h') ],

0 commit comments

Comments
 (0)