使用逻辑元素的 4 位计数器:输出始终为 0
4-bit counter using logic elements: output is always 0
我正在尝试仅使用逻辑元素在系统 Verilog 中构建一个 4 位计数器。我使用简单的 D 触发器和一些 XOR 和 AND 逻辑来实现这一点。
但是在模拟的时候,输出值一直是0,不知道是什么问题。
代码:
module d_flipflop (
input reg Enable,
input logic clk,
output reg Out
);
always @(posedge clk)
begin
Out <= Enable;
end
endmodule
module Four_bitCounter (
input reg Enable,
input logic clk,
output reg out0, out1, out2, out3
);
logic ffInput0;
logic ffInput1;
logic ffInput2;
logic ffInput3;
always @(*) begin
ffInput0 <= out0 ^ Enable;
ffInput1 <= out1 ^ (Enable & out0);
ffInput2 <= out2 ^ (Enable & out1);
ffInput3 <= out3 ^ (Enable & out2);
end
d_flipflop dff0 (
.Enable(ffInput0),
.clk (clk),
.Out (out0)
);
d_flipflop dff1 (
.Enable(ffInput1),
.clk (clk),
.Out (out1)
);
d_flipflop dff2 (
.Enable(ffInput2),
.clk (clk),
.Out (out2)
);
d_flipflop dff3 (
.Enable(ffInput3),
.clk (clk),
.Out (out3)
);
endmodule
测试台文件:
module Four_bitCounter_tb();
reg out0, out1, out2, out3;
logic clk;
reg Enable;
Four_bitCounter F_bc0 (
.Enable(Enable),
.clk (clk),
.out0 (out0),
.out1 (out1),
.out2 (out2),
.out3 (out3)
);
initial begin
clk = 0;
forever #10 clk = ~clk;
end
initial begin
out0 <= 1 'b0;
out1 <= 1 'b0;
out2 <= 1 'b0;
out3 <= 1 'b0;
Enable <= 1 'b0;
#20
Enable <= 1 'b1;
#300
$finish;
end
endmodule
模拟:
设计:
当我尝试编译您的代码时,我在使用多个模拟器时遇到错误。
在测试台中,out0
信号有多个驱动程序。其他 3 个“输出”信号也是如此。由于它们是设计的输出,因此您不应在测试台中对它们进行分配。您还应该将它们声明为 wire
,而不是 reg
。这是修改后的测试台:
module Four_bitCounter_tb();
wire out0, out1, out2, out3;
logic clk;
reg Enable;
Four_bitCounter F_bc0 (
.Enable(Enable),
.clk (clk),
.out0 (out0),
.out1 (out1),
.out2 (out2),
.out3 (out3)
);
initial begin
clk = 0;
forever #10 clk = ~clk;
end
initial begin
Enable <= 1'b0;
#20
Enable <= 1'b1;
#300
$finish;
end
endmodule
更改后,我在输出中看到未知数 (X)。发生这种情况是因为触发器 Out
在时间 0 是 X
。所有 reg
类型都被初始化为 X。您需要一种方法将它们初始化为 0。对于模拟,更改:
output reg Out
至:
output reg Out = 0
或者,要在模拟开始时去除未知值,您可以使用 reset signal for your design.
您应该在 edaplayground 上注册一个免费帐户;这将使您能够访问其他模拟器,在那里您可能会更好地了解您的模拟行为异常的原因。
我正在尝试仅使用逻辑元素在系统 Verilog 中构建一个 4 位计数器。我使用简单的 D 触发器和一些 XOR 和 AND 逻辑来实现这一点。 但是在模拟的时候,输出值一直是0,不知道是什么问题。
代码:
module d_flipflop (
input reg Enable,
input logic clk,
output reg Out
);
always @(posedge clk)
begin
Out <= Enable;
end
endmodule
module Four_bitCounter (
input reg Enable,
input logic clk,
output reg out0, out1, out2, out3
);
logic ffInput0;
logic ffInput1;
logic ffInput2;
logic ffInput3;
always @(*) begin
ffInput0 <= out0 ^ Enable;
ffInput1 <= out1 ^ (Enable & out0);
ffInput2 <= out2 ^ (Enable & out1);
ffInput3 <= out3 ^ (Enable & out2);
end
d_flipflop dff0 (
.Enable(ffInput0),
.clk (clk),
.Out (out0)
);
d_flipflop dff1 (
.Enable(ffInput1),
.clk (clk),
.Out (out1)
);
d_flipflop dff2 (
.Enable(ffInput2),
.clk (clk),
.Out (out2)
);
d_flipflop dff3 (
.Enable(ffInput3),
.clk (clk),
.Out (out3)
);
endmodule
测试台文件:
module Four_bitCounter_tb();
reg out0, out1, out2, out3;
logic clk;
reg Enable;
Four_bitCounter F_bc0 (
.Enable(Enable),
.clk (clk),
.out0 (out0),
.out1 (out1),
.out2 (out2),
.out3 (out3)
);
initial begin
clk = 0;
forever #10 clk = ~clk;
end
initial begin
out0 <= 1 'b0;
out1 <= 1 'b0;
out2 <= 1 'b0;
out3 <= 1 'b0;
Enable <= 1 'b0;
#20
Enable <= 1 'b1;
#300
$finish;
end
endmodule
模拟:
设计:
当我尝试编译您的代码时,我在使用多个模拟器时遇到错误。
在测试台中,out0
信号有多个驱动程序。其他 3 个“输出”信号也是如此。由于它们是设计的输出,因此您不应在测试台中对它们进行分配。您还应该将它们声明为 wire
,而不是 reg
。这是修改后的测试台:
module Four_bitCounter_tb();
wire out0, out1, out2, out3;
logic clk;
reg Enable;
Four_bitCounter F_bc0 (
.Enable(Enable),
.clk (clk),
.out0 (out0),
.out1 (out1),
.out2 (out2),
.out3 (out3)
);
initial begin
clk = 0;
forever #10 clk = ~clk;
end
initial begin
Enable <= 1'b0;
#20
Enable <= 1'b1;
#300
$finish;
end
endmodule
更改后,我在输出中看到未知数 (X)。发生这种情况是因为触发器 Out
在时间 0 是 X
。所有 reg
类型都被初始化为 X。您需要一种方法将它们初始化为 0。对于模拟,更改:
output reg Out
至:
output reg Out = 0
或者,要在模拟开始时去除未知值,您可以使用 reset signal for your design.
您应该在 edaplayground 上注册一个免费帐户;这将使您能够访问其他模拟器,在那里您可能会更好地了解您的模拟行为异常的原因。