我用Verilog做了一个4位计数器模块,我需要在EPWave上表示以下步骤:
module contador_sequencial(
input clk,
input reset,
output reg [3:0] count
);
always@(posedge clk or posedge reset) begin
if(reset) begin
count <= 4'b0000;
end else if (count == 4'b1001) begin
count <= 4'b0000;
end else begin
count <= count + 1'b1;
end
end
endmodule
module testbench;
reg clk;
reg reset;
wire [3:0] count;
contador_sequencial counter(.clk(clk), .reset(reset), .count(count));
initial begin
clk = 0;
forever #1000 clk = ~clk;
end
initial begin
$dumpfile("dump.vcd");
$dumpvars;
reset = 0;
#5000
reset = 1;
#1000
reset = 0;
#12000
$finish;
end
endmodule
计数器最初应该计数 5 秒,但这就是我得到的:
前5秒根本不算数。我做错了什么?
在第一个 5 秒内,
count
未知 (x
),因为您将信号声明为 reg
类型,并且该类型在模拟中初始化为 x
。由于在那段时间 reset
为 0,因此您不执行重置子句:
if(reset) begin
因此,
count
在时间 = 5 秒之前仍然未知。在时间=5秒时,您在测试台中设置reset
=1,这意味着count
设置为0。
通常,测试平台会在时间 0 开始断言复位信号,然后在几个时钟周期后取消断言。