PAYATHON 2026

SystemVerilog 顶层模块的 interface 端口未连接,如何修复?

数据小许

增加一个无端口的顶层模块,在其中生成时钟并实例化 dut_if,再通过 .tb_if(vif) 将接口实例传给 test。仿真时选择该模块作为顶层,就能保留通过 interface 端口封装 testbench 的方式。

为什么内部有 vif,仍然提示端口未连接?

这两处声明的作用不同:

module test (dut_if.tb tb_if);

这里的 tb_if 是 interface 端口,实例化 test 时需要为它连接一个实际的接口实例。dut_if.tb 表示通过 dut_if 的 tb modport 访问该接口。

dut_if vif(.clk(clk));

这里在 test 内部创建了另一个接口实例。它不会自动连接到 tb_if,名称不同也不是报错的原因。

当 test 直接作为仿真顶层时,没有上层实例为 tb_if 提供连接,因此会报错。严格来说,这里传入的是 interface 实例,不是通过 parameter 声明的参数。

原代码还混用了两个接口:驱动和采样使用 tb_if.cb,等待两个时钟事件却使用 vif.cb。应统一使用传入的 tb_if.cb。

完整修改示例

下面的示例将时钟、接口实例和仿真控制放在 top 中,test 只负责测试激励。ready 固定为高电平,用来演示接口连接;接入真实 DUT 时,应移除这个固定驱动,由 DUT 驱动 ready。

`timescale 1ns/1ps

interface dut_if(input logic clk);
    logic [7:0] data;
    logic valid;
    logic ready;

    default clocking cb @(posedge clk);
        default input #1step output #2ns;
        output data, valid;
        input ready;
    endclocking

    modport tb (clocking cb);
endinterface

module test(dut_if.tb tb_if);
    initial begin
        @(tb_if.cb);
        tb_if.cb.data <= 8'hA5;
        tb_if.cb.valid <= 1'b1;

        repeat (2) @(tb_if.cb);

        if (tb_if.cb.ready)
            $display("DUT is ready at time %t", $time);

        tb_if.cb.valid <= 1'b0;
    end
endmodule

module top;
    logic clk = 1'b0;

    always #5 clk = ~clk;

    dut_if vif(.clk(clk));

    test u_test(.tb_if(vif));

    // Demonstration only; replace with the DUT connection.
    assign vif.ready = 1'b1;

    initial begin
        $dumpfile("dump.vcd");
        $dumpvars(0, top);
    end

    initial begin
        #50;
        $finish;
    end
endmodule

连接语句是:

test u_test(.tb_if(vif));

vif 是 top 创建的接口实例,tb_if 是 test 接收该实例的端口。端口声明中的 dut_if.tb 指定了 test 使用的 modport,连接时可以直接传入 vif。

仿真时需要注意什么?

  • 选择 top 作为仿真顶层。如果仍然单独对 test 进行 elaboration,它的 interface 端口依旧没有连接。
  • 按 SystemVerilog 编译。代码依赖 interface、modport 和 clocking block,仿真器需要支持这些特性。具体的编译和顶层选择命令取决于使用的工具。
  • 等待和访问时使用同一个接口。repeat (2) @(tb_if.cb); 等待传入接口的两个 clocking event,避免原代码中等待内部 vif、却操作外部 tb_if 的混用。
  • 明确时间单位。示例中的 `timescale 1ns/1ps 使 #5 表示 5 ns,时钟周期为 10 ns。原代码没有给出时间单位,不能仅凭 #5 确定实际时长。
  • 保留 clocking block 的时序含义。input #1step 指定在时钟事件前一个仿真精度步长采样,output #2ns 指定相对该事件延后 2 ns 驱动。修复端口连接不会取消这些偏移。

原始代码没有展示 DUT,也没有给 ready 提供驱动。修复连接后,仍需连接 DUT 或提供明确的测试驱动,才能有意义地判断 ready。

备注:内容仅供参考。