为什么顶层模块的端口没有连接?

编程语言 2026-07-09

我尝试把一个参数传给测试基准(testbench),但出现了错误:

顶层模块 'test' 的端口 'tb_if',其类型为接口 'dut_if',未连接。 将接口端口保持未连接是非法的。
请确保所有接口端口都已连接。

下面是我的代码:

interface dut_if(input logic clk);
  logic [7:0] data;
  logic valid;
  logic ready;
  // Clocking block for the testbench (Driver/Monitor)
  default clocking cb @(posedge clk);
    default input #1step output #2ns;
    output data, valid;
    input  ready;
  endclocking
  // Modport to restrict the testbench to using the clocking block
  modport tb (clocking cb);
endinterface  

module test (dut_if.tb tb_if);
  logic clk;

  dut_if vif(.clk(clk)); 

  initial begin
    clk = 0;
    forever #5 clk = ~clk;    
  end 

  initial begin
    // Synchronize to the clocking event
    @(tb_if.cb);  
    // Drive signals through the clocking block
    tb_if.cb.data  <= 8'hA5;
    tb_if.cb.valid <= 1;  
    // Wait for 2 clock cycles using the cycle delay operator
    //##2;   
    repeat (2) @(vif.cb);  
    // Sample a signal
    if (tb_if.cb.ready) 
      $display("DUT is ready at time %t", $time);     
    tb_if.cb.valid <= 0;
  end 
  initial #50 $finish; 
  initial begin
    $dumpfile("dump.vcd"); 
    $dumpvars;
  end
endmodule

我当然可以用另一种风格来编写测试基准,在其中不传递参数以避免这个问题。然而,为了更好地实现代码的封装,请问有人能给出解决这个问题的方法,使所给出的代码能够正常工作吗?

解决方案

当我在VCS模拟器上运行代码时,得到与你贴出的相同错误信息。以下是对这个错误信息的解释。

你编译了一个 interface 和一个 module。模拟器期望其中一个模块作为“顶层”模块,由于你只编译了一个,它将 test 设为顶层模块。问题在于模拟器不支持顶层模块的端口。

你在说给测试基准传递一个“参数”时也使用了不正确的术语。Verilog有一个 parameter 关键字,用来声明常量类型。在这行代码:

module test (dut_if.tb tb_if);

你并没有把参数传递给 test 模块。tb_if 是一个模块端口,而不是参数。

我确实可以用另一种风格编写测试基准,使其不传递参数来避免这个问题。

你必须换一种方式来解决问题。

具体怎么做取决于你要实现的目标。通常,你会有一个没有模块端口的顶层模块。这就是我们通常所说的测试基准模块。如果你希望 test 成为顶层,那么它应这样声明:

module test;

如果 test 被实例化在另一个模块中,那么你可以用端口列表来声明它。但随后你必须把一个接口连接到 test 的实例。当模拟器消息提到:

请确保所有接口端口都已连接

时,这正是它在指的内容。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章