-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_vivado_bd.py
485 lines (412 loc) · 16.2 KB
/
gen_vivado_bd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
"""Generates Vivado block designs to run RTL simulation and generate bitstreams.
As of 2023.2, Vitis only supports HLS kernels to use Versal NoC for memory (LPDDR/DDR).
This module enables HLS kernels to use Versal NoC between their sub-modules.
It dumps tcl files to create Vivado block designs for running simulation and generating
bitstreams. This module avoids the Vitis platform flow, and uses Vivado block designs
for directly instantiating NoC IPs. The HLS kernel should use top-level ports to infer
NoC connection between sub-modules. This module will use the AXI Stream NoC to connect
the sub-modules.
The Microblaze block design uses the 64-bit Microblaze as the host. Users should export
the pre-synthesis hardware to Vitis, and writes the bare-metal C program to send test
inputs to DUT and memory.
The ARM block design can be simulated by writing a CIPS VIP testbench,
and can be directly synthesized and implemented to run on the board.
"""
__copyright__ = """
Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors.
All rights reserved. The contributor(s) of this file has/have agreed to the
RapidStream Contributor License Agreement.
"""
from ir_helper import VALID_TDATA_NUM_BYTES, round_up_to_noc_tdata
from vivado_bd_helper import (
arm_ddr_tcl,
arm_hbm_tcl,
arm_tcl,
assign_arm_bd_address,
proc_tcl,
)
def dut_hbm_mmap_tcl(
mmap_ports: dict[str, dict[str, int]],
hbm_init_file: str,
) -> list[str]:
"""Adds DUT's HBM-MMAP related tcl commands to the block design.
Returns a list of tcl commands.
"""
tcl = []
num_hbm_nmu = sum(1 for v in mmap_ports.values() if v.get("noc") is None)
tcl += [
f"""
startgroup
set_property -dict [list \
CONFIG.NUM_HBM_BLI {{{num_hbm_nmu}}} \
CONFIG.NUM_SI {{{len(mmap_ports) - num_hbm_nmu}}} \
CONFIG.NUM_CLKS {{1}} \
CONFIG.HBM_MEM_BACKDOOR_WRITE {{true}} \
CONFIG.HBM_MEM_INIT_FILE {{{hbm_init_file}}} \
] $axi_noc_dut
"""
]
# counters for each HBM PC to balance the usage of the HBM ports
# initialize with CIPS connections
def get_hbm_noc_port(bank: int) -> str:
# bank 0 = HBM PC 0 port 0 = port_idx 0
# bank 0 = HBM PC 0 port 1 = port_idx 1
# bank 1 = HBM PC 1 port 0 = port_idx 2
# bank 1 = HBM PC 1 port 1 = port_idx 3
port_idx = bank % 2 * 2 + hbm_bank_cnt[bank] % 2
# Note: hbm_bank_cnt is STATIC!
hbm_bank_cnt[bank] += 1
return f"HBM{bank // 2}_PORT{port_idx}"
hbm_bank_cnt = {i: 1 for i in range(32)}
# Configure and connect mmap noc
all_busif_ports = []
nmu_port_cnt = {"reg": 0, "hbm": 0}
for i, (port, attr) in enumerate(mmap_ports.items()):
# for HBM, mmap ports use dedicated HBM NMU nodes if attr["noc"] is not set
# else, use regular NMU nodes
if attr.get("noc") is None:
noc_s_port = f"HBM{nmu_port_cnt['hbm']:02d}_AXI" # HBM NMU
nmu_port_cnt["hbm"] += 1
else:
noc_s_port = f"S{(nmu_port_cnt['reg'] + 8):02d}_AXI" # regular NMU
nmu_port_cnt["reg"] += 1
noc_m_port = get_hbm_noc_port(attr["bank"])
tcl += [
f"""
set_property -dict [list CONFIG.CONNECTIONS {{{noc_m_port} {{ \
read_bw {{{attr['read_bw'] - 100}}} \
write_bw {{{attr['write_bw'] - 100}}} \
read_avg_burst {{8}} \
write_avg_burst {{8}} \\
# excl_group {{{'' if attr.get("noc") is None else i}}} \\
sep_rt_group {{{i}}} \
}}}}] [get_bd_intf_pins /axi_noc_dut/{noc_s_port}]
connect_bd_intf_net [get_bd_intf_pins $dut/{port}] \
[get_bd_intf_pins /axi_noc_dut/{noc_s_port}]
"""
]
all_busif_ports.append(noc_s_port)
s_busif_ports = ":".join(all_busif_ports)
tcl += [
f"set_property -dict [list CONFIG.ASSOCIATED_BUSIF {{{s_busif_ports}}}] \
[get_bd_pins /axi_noc_dut/aclk0]"
]
tcl += ["endgroup"]
return tcl
def dut_ddr_mmap_tcl(mmap_ports: dict[str, dict[str, int]]) -> list[str]:
"""Adds DUT's MMAP-DDR related tcl commands to the block design.
Returns a list of tcl commands.
"""
tcl = []
ddr_mc_cnt = [0, 0, 0]
for attr in mmap_ports.values():
mc = attr["bank"] // 4
ddr_mc_cnt[mc] += 1
for i, cnt in enumerate(ddr_mc_cnt):
tcl += [
f"""
# Create mmap noc
startgroup
set_property -dict [list \
CONFIG.NUM_SI {{{cnt}}} \
] $axi_noc_dut_{i + 1}
"""
]
# Configure and connect mmap noc
all_busif_ports: list[list[str]] = [[], [], []]
ddr_mc_cnt = [0, 0, 0]
for i, (port, attr) in enumerate(mmap_ports.items()):
mc = attr["bank"] // 4
noc_s_port = f"S{ddr_mc_cnt[mc]:02d}_AXI" # DDR
noc_m_port = f"MC_{attr['bank'] % 4}"
ddr_mc_cnt[mc] += 1
tcl += [
f"""
set_property -dict [list CONFIG.CONNECTIONS {{{noc_m_port} {{ \
read_bw {{{attr['read_bw'] - 100}}} \
write_bw {{{attr['write_bw'] - 100}}} \
read_avg_burst {{8}} \
write_avg_burst {{8}} \\
# excl_group {{{'' if attr.get("noc") is None else i}}} \\
sep_rt_group {{{i}}} \\
}}}}] [get_bd_intf_pins /axi_noc_dut_{mc + 1}/{noc_s_port}]
connect_bd_intf_net [get_bd_intf_pins $dut/{port}] \
[get_bd_intf_pins /axi_noc_dut_{mc + 1}/{noc_s_port}]
"""
]
all_busif_ports[mc].append(noc_s_port)
for i, busif in enumerate(all_busif_ports):
if not busif:
continue
s_busif_ports = ":".join(busif)
tcl += [
f"set_property -dict [list CONFIG.ASSOCIATED_BUSIF {{{s_busif_ports}}}] \
[get_bd_pins /axi_noc_dut_{i + 1}/aclk0]"
]
tcl += ["endgroup"]
return tcl
def dut_stream_noc_tcl(stream_attr: dict[str, dict[str, str]]) -> list[str]:
"""Adds DUT's AXIS NoC related tcl commands to the block design.
Return a list of tcl commands
"""
tcl = [
f"""
# Create stream noc
startgroup
set axis_noc_dut [ create_bd_cell -type ip -vlnv \
xilinx.com:ip:axis_noc:1.0 axis_noc_dut ]
set_property -dict [list \
CONFIG.MI_TDEST_VALS {{}} \
CONFIG.NUM_MI {{{len(stream_attr)}}} \
CONFIG.NUM_SI {{{len(stream_attr)}}} \
CONFIG.SI_DESTID_PINS {{}} \
CONFIG.TDEST_WIDTH {{0}} \
] $axis_noc_dut
set_property CONFIG.ASSOCIATED_BUSIF [concat_axi_pins $axis_noc_dut] \
[get_bd_pins axis_noc_dut/aclk0]
"""
]
for i, (src, attr) in enumerate(stream_attr.items()):
noc_m_port = f"M{i:02d}_AXIS"
noc_s_port = f"S{i:02d}_AXIS"
tcl += [
f"""
set_property -dict [list CONFIG.CONNECTIONS {{{noc_m_port} \
{{ write_bw {{{float(attr["bandwidth"]) - 100}}} write_avg_burst {{8}}}}}}] \
[get_bd_intf_pins /axis_noc_dut/{noc_s_port}]
"""
]
# rounds the width up to the nearest supported TDATA_NUM_BYTES
if ((int(attr["width"]) + 7) // 8) not in VALID_TDATA_NUM_BYTES:
roundup_num_bytes = round_up_to_noc_tdata(attr["width"], True)
tcl += [
f"""
create_bd_cell -type ip -vlnv xilinx.com:ip:axis_dwidth_converter:1.1 \
axis_dwidth_converter_to_noc_{i}
set_property CONFIG.M_TDATA_NUM_BYTES {{{roundup_num_bytes}}} \
[get_bd_cells axis_dwidth_converter_to_noc_{i}]
# connect_bd_net [get_bd_pins axis_dwidth_converter_to_noc_{i}/aclk] \
# [get_bd_pins clk_wizard_0/clk_out1]
connect_bd_net [get_bd_pins axis_dwidth_converter_to_noc_{i}/aclk] \
[get_bd_pins CIPS_0/pl0_ref_clk]
connect_bd_net [get_bd_pins axis_dwidth_converter_to_noc_{i}/aresetn] \
[get_bd_pins proc_sys_reset_0/peripheral_aresetn]
connect_bd_intf_net [get_bd_intf_pins $dut/{src}] \
[get_bd_intf_pins axis_dwidth_converter_to_noc_{i}/S_AXIS]
connect_bd_intf_net [get_bd_intf_pins axis_dwidth_converter_to_noc_{i}/M_AXIS] \
[get_bd_intf_pins axis_noc_dut/{noc_s_port}]
create_bd_cell -type ip -vlnv xilinx.com:ip:axis_dwidth_converter:1.1 \
axis_dwidth_converter_to_dut_{i}
set_property -dict [list \
CONFIG.S_TDATA_NUM_BYTES {{{roundup_num_bytes}}} \
CONFIG.M_TDATA_NUM_BYTES {{{(int(attr["width"]) + 7) // 8}}} \
] [get_bd_cells axis_dwidth_converter_to_dut_{i}]
# connect_bd_net [get_bd_pins axis_dwidth_converter_to_dut_{i}/aclk] \
# [get_bd_pins clk_wizard_0/clk_out1]
connect_bd_net [get_bd_pins axis_dwidth_converter_to_dut_{i}/aclk] \
[get_bd_pins CIPS_0/pl0_ref_clk]
connect_bd_net [get_bd_pins axis_dwidth_converter_to_dut_{i}/aresetn] \
[get_bd_pins proc_sys_reset_0/peripheral_aresetn]
connect_bd_intf_net [get_bd_intf_pins axis_noc_dut/{noc_m_port}]\
[get_bd_intf_pins axis_dwidth_converter_to_dut_{i}/S_AXIS]
connect_bd_intf_net [get_bd_intf_pins axis_dwidth_converter_to_dut_{i}/M_AXIS] \
[get_bd_intf_pins $dut/{attr["dest"]}]
"""
]
else:
tcl += [
f"""
connect_bd_intf_net [get_bd_intf_pins $dut/{attr["dest"]}] \
[get_bd_intf_pins axis_noc_dut/{noc_m_port}]
connect_bd_intf_net [get_bd_intf_pins $dut/{src}] \
[get_bd_intf_pins axis_noc_dut/{noc_s_port}]
"""
]
tcl += ["endgroup"]
return tcl
def dut_tcl(
top_mod: str,
mmap_ports: dict[str, dict[str, int]],
stream_attr: dict[str, dict[str, str]],
hbm: bool,
hbm_init_file: str,
) -> list[str]:
"""Adds the design-under-test (DUT) to the block diagram.
It assumes DUT uses one clock for all top-level AXI ports.
It assumes the configurations registers use "s_axi_control".
The mmap ports are connected to the DDR through AXI-NoC.
The stream ports, if any, are connected to each other through AXIS-NoC.
Returns a list of tcl commands.
"""
tcl = [
f"""
# ======================= Adding DUT =======================
startgroup
# Add RTL module to BD
set dut [create_bd_cell -type module -reference {top_mod} dut_0]
# Associate AXI interfaces to clock
# Assumes there is one clock and all AXI pins use the same clock
set_property CONFIG.ASSOCIATED_BUSIF [concat_axi_pins $dut] [get_bd_clk_pins $dut]
endgroup
"""
]
if hbm:
tcl += dut_hbm_mmap_tcl(mmap_ports, hbm_init_file)
else:
tcl += dut_ddr_mmap_tcl(mmap_ports)
if stream_attr:
tcl += dut_stream_noc_tcl(stream_attr)
return tcl
def connect_dut_arm_ddr_tcl(stream_attr: dict[str, dict[str, str]]) -> list[str]:
"""Connects dut in the ARM-DDR block design.
Returns a list of tcl commands.
"""
tcl = [
"""
# Create external clk port for simulation
set pl0_ref_clk_0 [ create_bd_port -dir O -type clk pl0_ref_clk_0 ]
connect_bd_net [get_bd_pins CIPS_0/pl0_ref_clk] [get_bd_ports pl0_ref_clk_0]
# connect_bd_net [get_bd_pins clk_wizard_0/clk_out1] [get_bd_clk_pins $dut] \
# [get_bd_pins axi_noc_dut/aclk0]
connect_bd_net [get_bd_pins CIPS_0/pl0_ref_clk] [get_bd_clk_pins $dut] \
[get_bd_pins axi_noc_dut_1/aclk0] [get_bd_pins axi_noc_dut_2/aclk0] \
[get_bd_pins axi_noc_dut_3/aclk0]
connect_bd_net [get_bd_pins proc_sys_reset_0/peripheral_aresetn] [get_bd_rst_pins $dut]
connect_bd_intf_net [get_bd_intf_pins icn_ctrl/M01_AXI] \
[get_bd_intf_pins dut_0/s_axi_control]
connect_bd_net [get_bd_pins dut_0/interrupt] [get_bd_pins axi_intc_0/intr]
"""
]
if stream_attr:
tcl += [
"connect_bd_net [get_bd_pins axis_noc_dut/aclk0] \
[get_bd_pins CIPS_0/pl0_ref_clk]"
# "connect_bd_net [get_bd_pins axis_noc_dut/aclk0] \
# [get_bd_pins clk_wizard_0/clk_out1]"
]
return tcl
def connect_dut_arm_hbm_tcl(stream_attr: dict[str, dict[str, str]]) -> list[str]:
"""Connects dut in the ARM-HBM block design.
Returns a list of tcl commands.
"""
tcl = [
"""
# Create external clk and reset ports for simulation
set pl0_ref_clk_0 [ create_bd_port -dir O -type clk pl0_ref_clk_0 ]
connect_bd_net [get_bd_pins CIPS_0/pl0_ref_clk] [get_bd_ports pl0_ref_clk_0]
# connect_bd_net [get_bd_pins clk_wizard_0/clk_out1] [get_bd_clk_pins $dut] \
# [get_bd_pins $axi_noc_dut/aclk0]
connect_bd_net [get_bd_pins CIPS_0/pl0_ref_clk] [get_bd_clk_pins $dut] \
[get_bd_pins $axi_noc_dut/aclk0]
connect_bd_net [get_bd_pins proc_sys_reset_0/peripheral_aresetn] [get_bd_rst_pins $dut]
connect_bd_intf_net [get_bd_intf_pins icn_ctrl/M01_AXI] \
[get_bd_intf_pins dut_0/s_axi_control]
connect_bd_net [get_bd_pins dut_0/interrupt] [get_bd_pins axi_intc_0/intr]
"""
]
if stream_attr:
tcl += [
"connect_bd_net [get_bd_pins axis_noc_dut/aclk0] \
[get_bd_pins CIPS_0/pl0_ref_clk]"
# "connect_bd_net [get_bd_pins axis_noc_dut/aclk0] \
# [get_bd_pins clk_wizard_0/clk_out1]"
]
return tcl
def gen_arm_bd_ddr(
bd_attr: dict[str, str],
mmap_ports: dict[str, dict[str, int]],
stream_attr: dict[str, dict[str, str]],
fpd: bool,
) -> list[str]:
"""Generates Vivado block design with ARM and LPDDR.
Merges the tcl commands from the helper functions and dumps to a file.
Args:
bd_attr: bd_name: name of the block design
top_mod: name of the top-level module.
frequency: frequency of the CIPS pl ref clk.
mmap_ports: list of top-level mmap ports connected to the memory.
stream_attr: dictionary of top-level stream ports. Keys are "src" name.
Values are "dest" name, "bandwidth", and "width".
fpd: True if using CIPS M_AXI_FPD for kernel's control_s_axi,
else uses CIPS LPD_AXI_NOC_0 with NoC.
Returns a list of tcl commands.
"""
tcl = []
tcl += proc_tcl()
tcl += arm_tcl(bd_attr["bd_name"], bd_attr["frequency"], False, fpd)
tcl += arm_ddr_tcl(fpd)
tcl += dut_tcl(
bd_attr["top_mod"],
mmap_ports,
stream_attr,
False,
bd_attr["hbm_init_file"],
)
tcl += connect_dut_arm_ddr_tcl(stream_attr)
tcl += assign_arm_bd_address(False)
return tcl
def gen_arm_bd_hbm(
bd_attr: dict[str, str],
mmap_ports: dict[str, dict[str, int]],
stream_attr: dict[str, dict[str, str]],
fpd: bool,
) -> list[str]:
"""Generates Vivado block design with ARM and HBM.
Merges the tcl commands from the helper functions and dumps to a file.
Args:
bd_attr: bd_name: name of the block design
top_mod: name of the top-level module.
hbm_init_file: backdoor HBM initialization mem file.
frequency: frequency of the CIPS pl ref clk.
mmap_ports: list of top-level mmap ports connected to the memory.
stream_attr: dictionary of top-level stream ports. Keys are "src" name.
Values are "dest" name, "bandwidth", and "width".
fpd: True if using CIPS M_AXI_FPD for kernel's control_s_axi,
else uses CIPS LPD_AXI_NOC_0 with NoC.
Returns a list of tcl commands.
"""
tcl = []
tcl += proc_tcl()
tcl += arm_tcl(bd_attr["bd_name"], bd_attr["frequency"], True, fpd)
tcl += arm_hbm_tcl(mmap_ports, fpd)
tcl += dut_tcl(
bd_attr["top_mod"],
mmap_ports,
stream_attr,
True,
bd_attr["hbm_init_file"],
)
tcl += connect_dut_arm_hbm_tcl(stream_attr)
tcl += assign_arm_bd_address(True)
return tcl
if __name__ == "__main__":
import json
# manually set the following
TEST_DIR = "/home/jakeke/rapidstream-noc/test/tmp2"
TOP_MOD_NAME = "Serpens"
HBM_BD = True
I_MMAP_PORT_JSON = "mmap_port.json"
NOC_STREAM_ATTR_JSON = "noc_streams_attr.json"
VIVADO_BD_TCL = "arm_bd.tcl"
BD_NAME = "top_arm"
USE_M_AXI_FPD = False
IMPL_FREQUENCY = "300.0"
HBM_INIT_FILE = "/home/jakeke/rapidstream-noc/test/serpens_hbm48_nasa4704.mem"
with open(f"{TEST_DIR}/{I_MMAP_PORT_JSON}", "r", encoding="utf-8") as file:
test_mmap = json.load(file)
with open(f"{TEST_DIR}/{NOC_STREAM_ATTR_JSON}", "r", encoding="utf-8") as file:
test_stream_attr = json.load(file)
worker = gen_arm_bd_hbm if HBM_BD else gen_arm_bd_ddr
arm_bd_tcl = worker(
{
"bd_name": BD_NAME,
"top_mod": TOP_MOD_NAME,
"hbm_init_file": HBM_INIT_FILE,
"frequency": IMPL_FREQUENCY,
},
test_mmap,
test_stream_attr,
USE_M_AXI_FPD,
)
with open(f"{TEST_DIR}/{VIVADO_BD_TCL}", "w", encoding="utf-8") as file:
file.write("\n".join(arm_bd_tcl))