Verilog Examples - Clock Divide by 4
Our previous example of cock divide by 2 seemed trivial, so let us extend it to make a divide by 4.So for example if the frequency of the clock input is 50 MHz, the frequency of the output will be 12.5 MHz. In other words the time period of the outout clock will be 4 times the time perioud of the clock input.
The figure shows the example of a clock divider.

Problem - Write verilog code that has a clock and a reset as input. It has an output that can be called out_clk. The out_clk is also a clock that has a frequency one forth the frequency of the input clock. It has synchronous reset and if there if the reset is 1, the output clock resets to 0. Write test bench to verify it.
Solution -
This is the main code clock.v
|
Here is the test bench clocktb.v
|
Explanation
The counter r_nxt counts to 2 and then becomes 0.
if (r_nxt == 2'b10) begin r_reg <= 0; |
Rest of the code is simple to understand. In the next example we will write a code for divide the clock by any even Number.