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

如何在SystemVerilog绑定模块中打印绑定源模块信息?

解决SystemVerilog绑定模块断言触发时打印绑定源的问题

方法1:正确使用%m打印完整层次路径

你提到%m未生效,大概率是格式书写错误。在断言的else块中,%m会打印当前断言实例的完整层次路径,其中包含绑定的上层模块(A1或B1)。修改$display语句如下:

module main(
    input a,
    input b
);
assert_unknown: assert property(a)
else begin
    $display("Assertion failed in hierarchy: %m");
    // 输出示例:Assertion failed in hierarchy: A1.main.assert_unknown
end
endmodule

如果只需要提取绑定源模块名(A1/B1),可以结合$basename函数截取层次路径的最上层:

else begin
    string full_path;
    $sformat(full_path, "%m");
    $display("Assertion triggered from: %s", $basename(full_path));
    // 输出示例:Assertion triggered from: A1
end

方法2:通过参数传递绑定源标识(推荐)

这种方法更直观,直接在绑定模块时传递绑定源的名称参数,在main模块中保存并打印:

修改绑定文件(文件1):

bind A1 main #(.BIND_SOURCE("A1")) (.sour(a), dest(b));
bind B1 main #(.BIND_SOURCE("B1")) (.sour(c), dest(d));

修改main模块(文件2):

module main #(
    parameter string BIND_SOURCE = "UNKNOWN" // 默认值防止未指定参数
)(
    input a,
    input b
);
assert_unknown: assert property(a)
else begin
    $display("Assertion failed from binding source: %s", BIND_SOURCE);
    // 输出示例:Assertion failed from binding source: A1
end
endmodule

方法3:利用层次查询函数获取上层模块

通过$parent函数可以直接获取当前模块的父模块(即绑定的A1/B1),再结合$name函数获取模块名:

else begin
    $display("Assertion triggered by parent module: %s", $name($parent(this)));
    // 输出示例:Assertion triggered by parent module: A1
end

注意:部分仿真器对$parent(this)的支持可能有差异,建议优先使用方法1或2。

内容的提问来源于stack exchange,提问作者Hank

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 12:12:13