首页 > 解决方案 > 如何在 Verilog 中实例化具有 reg 端口的模块?

问题描述

我有一个这种类型的模块:

module rgst(
   input d,...
   output reg [3:0]);

...

endmodule

我想为它写一个测试平台:

module rgst_tb(
   output reg d,
   ...
   output reg [3:0]q);
   rgst uut(.d(d),...,q(q));
   ...

问题是我不能以这种方式在测试台中实例化我的模块,因为qreg类型。

标签: veriloginstantiation

解决方案


通常,包含测试平台代码的模块可以有两种样式之一:被测模块 (DUT) 在测试平台模块 (TB) 本身中实例化,或者 DUT 和 TB 在将它们连接在一起的顶级模块中单独实例化. 你只做一个,而不是两个;许多不熟悉该语言的人倾向于将它们混淆。

+-----------------+         +--------------------------------+
|  module tb      |         | module top                     |
|  +-------------+|         |  +------------+  +------------+|
|  | module dut  ||    or   |  | module tb  |==| module dut ||
|  +-------------+|         |  +------------+  +------------+|
+-----------------+         +--------------------------------+

在第一种样式中,TB 模块不需要任何输入或输出,它只需要本地声明的 DUT 端口的接线/寄存器,并在本地进行操作/监控:

module tb;
   // Declare ports of DUT as locals inside TB
   wire outval;
   reg inval;

   // Instantiate DUT inside TB module
   dut u1(.in(inval), .out(outval));

   // Stimulus and monitor here
   initial begin
     $monitor(outval);
     inval <= ...;
   end
endmodule

在第二种风格中,TB 模块是独立的,不实例化 DUT,因此需要将输入和输出端口连接到第三个更高级别模块中的 DUT:

module tb(input outval, output reg inval); // TB has ports mirroring DUT ports
  // Stimulus and monitor here
  initial begin
    $monitor(outval);
    inval <= ...;
  end
endmodule

module top;
  // Declare connecting lines here
  wire outval;
  wire inval;

  // Instantiate both TB and DUT here, connecting them together
  tb t1(.outval(outval), .inval(inval));
  dut u1(.in(inval), .out(outval));
endmodule

推荐阅读