I see this code written in a testbench. But, I do not understand why we need to do it like this. The clock period is 2.034ns. I did not wrote this code. The person who wrote this is not here.
`timescale 1ps/1ps
module tb();
initial begin
#0
forever begin
for (int x=0; x<95; x++) begin
#(1017) clk_491p52 = ~clk_491p52;
#(1017) clk_491p52 = ~clk_491p52;
#(1017) clk_491p52 = ~clk_491p52;
#(1018) clk_491p52 = ~clk_491p52;
end
#(1017) clk_491p52 = ~clk_491p52;
#(1018) clk_491p52 = ~clk_491p52;
#(1017) clk_491p52 = ~clk_491p52;
#(1018) clk_491p52 = ~clk_491p52;
end
end
endmodule
Why are we inverting the clock with this particular pattern? Can't we just do it like this?
initial clk_491p52=0;
always #(2034/2) clk_491p52=~clk_491p52;
There is a difference between the 2 code examples you showed.
The example with the
alwaysblock produces a 50% duty cycle.The example with the
foreverblock does not produce a 50% duty cycle.In the
alwaysblock, every half-cycle is 1017ps.In the
foreverblock, some half-cycles are 1017ps, whereas other half-cycles are 1018ps: 1017 vs. 1018This all assumes that
clk_491p52is declared as a single bit signal.The
alwaysblock is the traditional way to generate a simple testbench clock. However, the person who used theforeverblock must have seen a need to create a more complex clock waveform.