I am trying to build a 4-bit counter in system Verilog using logical elements only. I am using simple D-flipflops with some XOR and AND logic to achieve this. However, while simulating, the Output values are constantly 0, and I am not sure what's the issue.
Code:
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
TestBench file:
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
Simulation :
Design:

