如何访问多个模块中的子模块中的信号?

huangapple go评论74阅读模式
英文:

How to access signals in submodules with multiple modules?

问题

I have the following Verilog file named main.v:

module m1(input a, b, output wire c);

assign c = a & b;

endmodule

module main(input x, y, output wire z);

wire k;
m1 m1_inst(.a(x), .b(y), .c(k));
assign z = x ^ k;

endmodule

After that, I have a test bench named tb_main.v which has an instantiation as shown below:

`include "main.v"
reg a, b;
wire c;
main main_dut(a, b, c);

$display("%b", main_dut.m1_inst.a);
$display("%b", main_dut.y);
$display("%b", main_dut.m1_inst.a);

When I simulate that testbench, I get the error "unresolved reference to m1" and "unresolved reference to main".

How do I solve this problem? I do not want to separate modules in the main.v file into different files.
Any help will be appreciated.

英文:

I have the following Verilog file named main.v:

module m1(input a, b, output wire c);

assign c = a & b;

endmodule

module main(input x, y, output wire z);

wire k;
m1 m1_inst(.a(x), .b(y), .c(k));
assign z = x ^ k;

endmodule

After that, I have a test bench named tb_main.v which has an instantiation as shown below:

`include "main.v"
reg a, b;
wire b;
main main_dut(a, b, c);

$display("%b", main_dut.m1.a);
$display("%b", main_dut.main.y);
$display("%b", m1.a);

When I simulate that testbench, I get the error unresolved reference to m1 and unresolved reference to main.

How do I solve this problem? I do not want to separate modules in the main.v file into different files.
Any help will be appreciated.

答案1

得分: 1

问题在于您在分层规范中使用了一些模块名称,而不是模块实例名称。

例如,在 main_dut.m1.a 中:

  • main_dut 是模块实例名称
  • m1 是模块的名称,而不是实例

您必须只使用模块实例名称。 这是一个可以编译而不会出错的完整代码示例(也可在 EDA Playground 上找到):

module m1 (input a, b, output wire c);
    assign c = a & b;
endmodule

module main (input x, y, output wire z);
    wire k;
    m1 m1_inst (.a(x), .b(y), .c(k));
    assign z = x ^ k;
endmodule

module tb_main;
    reg a, b;
    main main_dut (a, b, c);
    initial $display("%b", main_dut.m1_inst.a);
    initial $display("%b", main_dut.y);
endmodule
英文:

The problem is that you are using some module names instead of module instance names in your hierarchical specifiers.

For example, in main_dut.m1.a:

  • main_dut is a module instance name
  • m1 is the name of the module, not the instance

You must only use module instance names. This is a complete code example that compiles without errors (also on EDA Playground):

module m1 (input a, b, output wire c);
    assign c = a & b;
endmodule

module main (input x, y, output wire z);
    wire k;
    m1 m1_inst (.a(x), .b(y), .c(k));
    assign z = x ^ k;
endmodule

module tb_main;
    reg a, b;
    main main_dut (a, b, c);
    initial $display("%b", main_dut.m1_inst.a);
    initial $display("%b", main_dut.y);
endmodule

答案2

得分: -1

总是更好地保持明确的连接。因此,不要这样做:

reg a, b;
wire b;
main main_dut(a, b, c);

而应该这样做:

reg a, b;
wire b;
main main_dut(
   .a(a),
   .b(b),
   .c(c)
);
英文:

It is always better to maintain explicit connectivity. So instead of

reg a, b;
wire b;
main main_dut(a, b, c);

, do something like

reg a, b;
wire b;
main main_dut(
               .a(a),
               .b(b),
               .c(c)
             );

huangapple
  • 本文由 发表于 2023年2月27日 00:04:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定