cocotb与Verilator使用问题:首次运行崩溃及.dat文件生成失败
Cocotb + Verilator 常见问题解决
问题描述
我正在学习使用cocotb,遇到两个技术问题:
- 首次执行
make SIM=verilator时因11个警告导致崩溃,但后续运行正常,想知道怎么避免首次崩溃。 - 用verilator_coverage分析测试覆盖率时,需要的.dat文件没生成,执行
make SIM=verilator COVERAGE=1也没用。
测试代码(test.py)
@cocotb.test() async def add_test(dut): dut._log.info("Running add test!") c = Clock(dut.clk, 10, 'ns') await cocotb.start(c.start()) a = 1 b = 2 for cycle in range(10): a += 1 b += 1 await RisingEdge(dut.clk) dut.exu2ialu_main_op1_i <= a dut.exu2ialu_main_op2_i <= b dut.exu2ialu_cmd_i <= 4 await RisingEdge(dut.clk) assert dut.ialu2exu_main_res_o == (a+b)
Makefile内容
VERILOG_INCLUDE_DIRS = $(PWD)/include VERILOG_SOURCES = $(PWD)/myalu.sv TOPLEVEL=scr1_pipe_ialu # the module name in your Verilog or VHDL file MODULE=test # the name of the Python test file include $(shell cocotb-config --makefiles)/Makefile.sim
问题1:首次编译因警告崩溃的解决办法
Verilator首次编译时没有缓存,默认会把警告当作错误处理导致崩溃,后续运行因为有编译缓存所以正常。可以这么解决:
- 在Makefile里添加
EXTRA_ARGS += -Wno-fatal,让Verilator不把警告触发为错误。 - 如果知道具体警告类型,也可以针对性屏蔽,比如未使用信号警告加
-Wno-unused,根据实际警告调整。 - 首次运行前先执行
make clean清理缓存,确保新参数生效。
修改后的Makefile示例:
VERILOG_INCLUDE_DIRS = $(PWD)/include VERILOG_SOURCES = $(PWD)/myalu.sv TOPLEVEL=scr1_pipe_ialu # the module name in your Verilog or VHDL file MODULE=test # the name of the Python test file EXTRA_ARGS += -Wno-fatal # 禁止将警告视为错误 include $(shell cocotb-config --makefiles)/Makefile.sim
问题2:覆盖率.dat文件未生成的解决办法
要生成Verilator的覆盖率.dat文件,得把配置做全,只加COVERAGE=1不够:
- 确认Verilator版本在v4.200以上,低版本可能不支持完整覆盖率功能。
- 在Makefile里添加覆盖率相关编译参数:
EXTRA_ARGS += --coverage --trace --trace-structs VERILATOR_COVERAGE_FLAGS = --annotate coverage_report --xml coverage.xml - 按以下流程操作:
- 先清理缓存:
make clean - 带参数编译运行:
make SIM=verilator COVERAGE=1 - 测试结束后,去
sim_build目录找coverage.dat,如果没自动生成,手动执行:verilator_coverage *.dat -o coverage.dat
- 先清理缓存:
- 另外要确保测试代码确实覆盖了设计逻辑,测试路径太少也可能导致.dat内容为空,但不会完全不生成。
修改后的完整Makefile(包含两个问题的修复):
VERILOG_INCLUDE_DIRS = $(PWD)/include VERILOG_SOURCES = $(PWD)/myalu.sv TOPLEVEL=scr1_pipe_ialu # the module name in your Verilog or VHDL file MODULE=test # the name of the Python test file EXTRA_ARGS += -Wno-fatal --coverage --trace --trace-structs VERILATOR_COVERAGE_FLAGS = --annotate coverage_report --xml coverage.xml include $(shell cocotb-config --makefiles)/Makefile.sim
内容的提问来源于stack exchange,提问作者s345
相关产品推荐
相关产品推荐

