第一天与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
目前编译后我只看到标题行。除了一个明显的问题,为什么它不起作用,我也不理解以下内容:
提前致谢。
答案 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
块#
(见下文)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