如何在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
相关产品推荐
相关产品推荐

