在Verilog测试平台中循环测试模式

时间:2015-11-02 15:48:34

标签: loops verilog

第一天与verilog斗争,这是一个简单的模块:

module example4_23 (x, f);
  input [0:6] x;
  output f;

  assign f = x[0] & x[1] & x[2] & x[3] & x[4] & x[5] & x[6];
endmodule

然后我想为它编写一个测试平台。首先,我使用7位寄存器用于x(无数组) - 一切正常,但现在我想用几种测试模式运行它,所以我宣布一个数组,并希望循环它并每次运行带有新输入的模块。 / p>

module test;
  reg [0:6] arr [0:1] = {7'b0011111,7'b1100000}; // all patterns
  reg [0:1] arr_size;                            // number of patterns
  wire [0:6] x;                                  // current pattern
  wire f;                                        // output

  initial begin
    $write("| TIME | x0 | x1 | x2 | x3 | x4 | x5 | x6 |   f   |"); // header
    $display;
    arr_size = $size(arr);                                         // 2
  end

// go through all test patterns, assign it to x and run the module   
  genvar i;  
  generate
    for (i=0; i<2; i=i+1)         // if use 'i<arr_size' gives an error
    begin
      assign x = arr[i];  // change to '..= arr[1]' or '..= arr[0]' successfully makes one test
      example4_23 dut (x,f);
    end
  endgenerate

// anytime there is a change - output on the screen   
  always @(x,f)
  begin
    $strobe("%6d %4d %4d %4d %4d %4d %4d %4d %5d", $time, x[0],x[1],x[2],x[3],x[4],x[5],x[6], f);
  end

endmodule 

目前编译后我只看到标题行。除了一个明显的问题,为什么它不起作用,我也不理解以下内容:

  1. 我应该将什么用于x-wire或注册?
  2. 我可以在没有'生成'''循环的情况下获得吗?可能有一种简化代码的方法。
  3. 是否可以在FOR循环中使用变量arr_size而不是数字?
  4. 提前致谢。

1 个答案:

答案 0 :(得分:0)

我很惊讶您没有收到有关为f分配多个值的错误。

请记住,verilog(或任何其他HDL)会创建物理电路,而生成块会创建给定电路的多个版本。在这种情况下,您要创建example4_23模块的两个实例,并同时驱动相同的输出。 Generate循环以空间方式运行,而always块中的循环在添加延迟时是暂时的。

要回答您的问题:1)使用reg,并在always块中指定延迟值; 2)是的! (见下例); 3)是的!使用参数!

因此,您可以进行一些更改:

  • wire arr_size替换为localparam ARR_SIZE = 2(或视情况而定)
  • reg [0:6] arr [0:1]更改为reg [0:6] arr [0:ARR_SIZE1-]
  • generate循环替换为initial
  • 中的基本for循环
  • 测试向量数组可以是参数而不是wire或reg
  • 加入延迟。现在,您的测试台不会随时间变化,只能在太空中变化。使用#(见下文)
  • 注明延迟
  • 使用always @(*)代替always @(x,f)

以下是我要实施的内容:

module test;
  localparam ARR_SIZE = 2;                            // number of patterns
  localparam [0:6] arr [0:ARR_SIZE-1] = {7'b0011111,7'b1100000}; // all patterns
  reg[0:6] x;                                  // current pattern
  wire f;                                        // output
  integer i;

  example4_23 dut (x,f);  

  initial begin
    $write("| TIME | x0 | x1 | x2 | x3 | x4 | x5 | x6 |   f   |"); // header
    $display;
    for (i=0; i< ARR_SIZE; i=i+1) begin
      x = arr[i];
      #5;    // delay 5 timesteps
    end
  end

// anytime there is a change - output on the screen   
  always @(*)
  begin
    $strobe("%6d %4d %4d %4d %4d %4d %4d %4d %5d", $time, x[0],x[1],x[2],x[3],x[4],x[5],x[6], f);
  end

endmodule