You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

cocotb与Verilator使用问题:首次运行崩溃及.dat文件生成失败

Cocotb + Verilator 常见问题解决

问题描述

我正在学习使用cocotb,遇到两个技术问题:

  1. 首次执行make SIM=verilator时因11个警告导致崩溃,但后续运行正常,想知道怎么避免首次崩溃。
  2. 用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不够:

  1. 确认Verilator版本在v4.200以上,低版本可能不支持完整覆盖率功能。
  2. 在Makefile里添加覆盖率相关编译参数:
    EXTRA_ARGS += --coverage --trace --trace-structs
    VERILATOR_COVERAGE_FLAGS = --annotate coverage_report --xml coverage.xml
    
  3. 按以下流程操作:
    • 先清理缓存:make clean
    • 带参数编译运行:make SIM=verilator COVERAGE=1
    • 测试结束后,去sim_build目录找coverage.dat,如果没自动生成,手动执行:
      verilator_coverage *.dat -o coverage.dat
      
  4. 另外要确保测试代码确实覆盖了设计逻辑,测试路径太少也可能导致.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 09:15:22