带执行和使能功能的三位计数器给出 X 输出

three bit counter with carry out and enable gives X output

我在 testbench 中遇到此代码的问题,因为它为我提供了 cout 的 X,但我找不到问题所在。

带启用功能的三位计数器 Verilog:

`timescale 1ns/1ns
module three_bit_counter(output cout,input en,clk);
    reg [2:0] w;
    always@(negedge clk)begin
        if (en) 
            w <= w + 1;
    end
    assign cout= w & en;
endmodule

这是我的测试平台:

`timescale 1ns/1ns
module three_bit_counterTB();
reg en;
reg clk=1;
wire cout;
three_bit_counter tbc(cout,en,clk);
always #20 clk=~clk;
initial begin
    #20 en=1;
    #100;
    $stop;
end
endmodule

cout 未知 (X) 因为 w 未知。 w 声明为 reg,它在时间 0 初始化为 X。即使 en=1,w <= w + 1 仍然是 X。

您需要初始化w。出于模拟目的,这可以通过以下方式完成:

reg [2:0] w = 0;

一种常见的设计方法是使用复位信号来初始化寄存器。您将向设计模块添加一个复位输入,然后从测试台驱动它。例如:

module three_bit_counter (output cout, input en, clk, reset);
    reg [2:0] w;
    always @(negedge clk or posedge reset) begin
        if (reset)   w <= 0;
        else if (en) w <= w + 1;
    end
    assign cout= w & en;
endmodule