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

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

How to access signals in submodules with multiple modules?

问题

I have the following Verilog file named main.v:

  1. module m1(input a, b, output wire c);
  2. assign c = a & b;
  3. endmodule
  4. module main(input x, y, output wire z);
  5. wire k;
  6. m1 m1_inst(.a(x), .b(y), .c(k));
  7. assign z = x ^ k;
  8. endmodule

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

  1. `include "main.v"
  2. reg a, b;
  3. wire c;
  4. main main_dut(a, b, c);
  5. $display("%b", main_dut.m1_inst.a);
  6. $display("%b", main_dut.y);
  7. $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:

  1. module m1(input a, b, output wire c);
  2. assign c = a & b;
  3. endmodule
  4. module main(input x, y, output wire z);
  5. wire k;
  6. m1 m1_inst(.a(x), .b(y), .c(k));
  7. assign z = x ^ k;
  8. endmodule

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

  1. `include "main.v"
  2. reg a, b;
  3. wire b;
  4. main main_dut(a, b, c);
  5. $display("%b", main_dut.m1.a);
  6. $display("%b", main_dut.main.y);
  7. $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 上找到):

  1. module m1 (input a, b, output wire c);
  2. assign c = a & b;
  3. endmodule
  4. module main (input x, y, output wire z);
  5. wire k;
  6. m1 m1_inst (.a(x), .b(y), .c(k));
  7. assign z = x ^ k;
  8. endmodule
  9. module tb_main;
  10. reg a, b;
  11. main main_dut (a, b, c);
  12. initial $display("%b", main_dut.m1_inst.a);
  13. initial $display("%b", main_dut.y);
  14. 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):

  1. module m1 (input a, b, output wire c);
  2. assign c = a & b;
  3. endmodule
  4. module main (input x, y, output wire z);
  5. wire k;
  6. m1 m1_inst (.a(x), .b(y), .c(k));
  7. assign z = x ^ k;
  8. endmodule
  9. module tb_main;
  10. reg a, b;
  11. main main_dut (a, b, c);
  12. initial $display("%b", main_dut.m1_inst.a);
  13. initial $display("%b", main_dut.y);
  14. endmodule

答案2

得分: -1

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

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

而应该这样做:

  1. reg a, b;
  2. wire b;
  3. main main_dut(
  4. .a(a),
  5. .b(b),
  6. .c(c)
  7. );
英文:

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

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

, do something like

  1. reg a, b;
  2. wire b;
  3. main main_dut(
  4. .a(a),
  5. .b(b),
  6. .c(c)
  7. );

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:

确定