【问题标题】:three bit counter with carry out and enable gives X output带执行和启用的三位计数器提供 X 输出
【发布时间】:2020-12-30 19:25:33
【问题描述】:

我在测试台中的这段代码有问题,因为它给了我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

【问题讨论】:

    标签: verilog


    【解决方案1】:

    cout 未知 (X),因为 w 未知。 w 被声明为 reg,它在时间 0 初始化为 X。即使 en=1,w &lt;= 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
    

    【讨论】:

      猜你喜欢
      • 2013-07-23
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 2023-01-11
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多