Skip to content

Commit 4f917c2

Browse files
committed
Support attaching metadata during elaboration
1 parent 5e367d4 commit 4f917c2

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

chipflow_lib/platforms/sim.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ def __init__(self, config):
168168
self._clocks = {}
169169
self._resets = {}
170170
self._builders: List[BasicCxxBuilder] = [ _COMMON_BUILDER ]
171-
self._sim_data = {}
172171
self._top_sim = {}
172+
self._sim_data = {}
173173

174174
def add_file(self, filename, content):
175175
if not isinstance(content, (str, bytes)):
176176
content = content.read()
177177
self.extra_files[filename] = content
178178

179-
def build(self, e):
179+
def build(self, e, top):
180180
Path(self.build_dir).mkdir(parents=True, exist_ok=True)
181181

182182
ports = []
@@ -213,18 +213,31 @@ def build(self, e):
213213
print("write_cxxrtl -header sim_soc.cc", file=yosys_file)
214214
main = Path(self.build_dir) / "main.cc"
215215

216-
env = Environment(
217-
loader=PackageLoader("chipflow_lib", "common/sim"),
218-
autoescape=select_autoescape()
219-
)
220-
template = env.get_template("main.cc.jinja")
216+
metadata = {}
217+
for key in top.keys():
218+
metadata[key] = getattr(e.submodules, key).metadata.as_json()
219+
for component, iface in metadata.items():
220+
for interface, interface_desc in iface['interface']['members'].items():
221+
annotations = interface_desc['annotations']
222+
223+
if SIM_DATA_SCHEMA in annotations:
224+
self._sim_data[interface] = annotations[SIM_DATA_SCHEMA]
225+
221226
data_load = []
222227
for i,d in self._sim_data.items():
223228
args = [f"0x{d['offset']:X}U"]
224229
p = Path(d['file_name'])
225230
if not p.is_absolute():
226231
p = _ensure_chipflow_root() / p
227232
data_load.append({'model_name': i, 'file_name': p, 'args': args})
233+
234+
235+
env = Environment(
236+
loader=PackageLoader("chipflow_lib", "common/sim"),
237+
autoescape=select_autoescape()
238+
)
239+
template = env.get_template("main.cc.jinja")
240+
228241
with main.open("w") as main_file:
229242
print(template.render(
230243
includes = [hpp for b in self._builders if b.hpp_files for hpp in b.hpp_files ],
@@ -258,9 +271,6 @@ def instantiate_ports(self, m: Module):
258271
if builder:
259272
self._top_sim[interface] = builder.instantiate_model(interface, sim_interface, interface_desc, self._ports)
260273

261-
if SIM_DATA_SCHEMA in annotations:
262-
self._sim_data[interface] = annotations[SIM_DATA_SCHEMA]
263-
264274
print(f"ports = {pformat(self._ports)}")
265275
for clock in pinlock.port_map.get_clocks():
266276
assert 'clock_domain' in clock.iomodel

chipflow_lib/steps/sim.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def build(self, *args):
9595
_wire_up_ports(m, top, self._platform)
9696

9797
#FIXME: common source for build dir
98-
self._platform.build(m)
98+
self._platform.build(m, top)
9999
with common() as common_dir, source() as source_dir, runtime() as runtime_dir:
100100
context = {
101101
"COMMON_DIR": common_dir,

0 commit comments

Comments
 (0)