Scaling Clock frequency for faster simulation in Verilog
If you have a faster system clock, say, at 50 MHz that needs to generate counter at a much slower speed, say 1 Hz, the simulation may not be efficient. The counter or clock divider used will be very slow and will generate a very big file to generate waveform.
Consider the simple case where we are generating a 1 Hz time from a 50 MHz clock. The time period corresponding to the 50 MHz clock will be 20 ns ( 1/ 50 MHz = 20 ns). The half clock period will be 10 ns. A typical timescale directive and the statement to generate clock will be
`timescale 10ns/10ns . . initial clk=1'b0; always #1 clk=~clk;
If the code uses a counter for 1 second, then typical code will be
reg [26:0] count; reg [3:0] counter; always @(negedge clk or negedge reset) begin if (reset ==0) begin count<=27'd0; counter <= 4'b0; end else if (count==27'd50000000) begin count <=27'd0; if (counter < 9) counter <= counter +1; else counter <= 0; end else count<=count+1; end
This will increase the "counter" value in 1 second interval. However, if you try to simulate this, the test bench need to run for a very long count value to display values of counter. Under such scenario, it me a be better idea to scale down the input clock. For example we may assume an input clock to be 50KHz in place of 50 MHz ( a factor of 1000).
The count value in this case can be changed from
else if (count==27'd50000000) begin count <=27'd0;
to
else if (count==27'd50000) begin count <=27'd0;
At simulation, we change the timescale from
`timescale 10ns/10ns
to
`timescale 10us/10us
Again applying a factor of 1000. The result of this exercise is that our end result is same. When we scale the clock from 50 KHz to 50 MHz, we scale up the actual counter factor to 1000.
Notice the period of clk used in this simulation.
It generated a vcd file that had a size of 6 MB. If we had not scaled it, its size would have been 6000 MB ( approx). The simulation time will be 1000 times more.
As an exercise, we ask the reader to write a verilog code that has 50 MHz clock input and it will count in interval of, say, 1 second. Try to simulate the code.
This exercise and its solution will be published with a Xilinx evaluation board to be soon released. The full code will be published shortly. For a complete verilog tutorial, please see here .