Generating Clocks
In physical integrated circuits, clock networks receive signals from crystal oscillators or phase-locked loops (PLLs). In digital simulation, testbenches must generate all clock waveforms procedurally. Defining clocks with parameterised periods ensures that related timing parameters scale automatically across simulation environments.
The Standard Clock Generator Pattern
The standard Verilog clock generator requires three concise lines:
parameter CLK_PERIOD = 10; // Clock period in nanoseconds (100 MHz)
reg clk;
initial clk = 0;
always #(CLK_PERIOD / 2) clk = ~clk;
This pattern incorporates critical design rules:
CLK_PERIODis defined as a parameter. Changing this constant scales dependent delays, reset widths, and timeouts across the entire file.- The
initial clk = 0;statement is mandatory. In Verilog, uninitializedregvariables default to unknown (x). The bitwise inversion~xevaluates tox. Without explicit initialization, the clock remainsxindefinitely. - The
alwaysblock contains no sensitivity list. It waits half a period, toggles state, and repeats indefinitely, creating a symmetrical 50% duty cycle.
If timescale precision allows (for example, `timescale 1ns / 1ps), fractional delays such as #3.5 evaluate accurately to 3.5 ns.
The forever Loop Alternative
You can also implement clock generation inside an initial block using a forever loop:
initial begin
clk = 0;
forever #(CLK_PERIOD / 2) clk = ~clk;
end
Use the forever construct when you need to introduce an initial startup delay before clock toggling begins.
Custom Duty Cycles
A symmetrical clock uses identical high and low durations. To generate asymmetric duty cycles, assign high and low durations explicitly:
parameter CLK_PERIOD = 20; // 50 MHz clock
parameter CLK_HIGH_PCT = 25; // 25% high duty cycle
parameter CLK_HIGH = (CLK_PERIOD * CLK_HIGH_PCT) / 100; // 5 ns high
parameter CLK_LOW = CLK_PERIOD - CLK_HIGH; // 15 ns low
reg clk;
always begin
clk = 1;
#(CLK_HIGH);
clk = 0;
#(CLK_LOW);
end
The signal remains high for 5 ns and low for 15 ns, providing a 25% duty cycle.
Multi-Clock Verification Domains
Complex systems integrate multiple clock domains. Model independent clock domains using dedicated reg declarations and independent generators:
parameter SYS_CLK_PERIOD = 10; // 100 MHz system clock
parameter PERI_CLK_PERIOD = 40; // 25 MHz peripheral clock
reg sys_clk, peri_clk;
initial sys_clk = 0;
always #(SYS_CLK_PERIOD / 2) sys_clk = ~sys_clk;
initial peri_clk = 0;
always #(PERI_CLK_PERIOD / 2) peri_clk = ~peri_clk;
Both clocks initialize to zero at simulation time zero and toggle independently at their configured frequencies.
Phase Offsets
To model phase-shifted clocks, introduce an initial offset delay before starting the oscillation loop:
parameter CLK_A_PERIOD = 10;
parameter CLK_B_PERIOD = 10;
reg clk_a, clk_b;
initial clk_a = 0;
always #(CLK_A_PERIOD / 2) clk_a = ~clk_a;
initial begin
clk_b = 0;
#(CLK_A_PERIOD / 4); // 90-degree phase shift (quarter period)
clk_b = 1; // First active edge occurs at 2.5 ns
forever #(CLK_B_PERIOD / 2) clk_b = ~clk_b;
end
Figure 1 illustrates multi-frequency clocks and phase-shifted waveforms.
Complete Multi-Clock Testbench Skeleton
`timescale 1ns / 1ps
module tb_multi_clock();
parameter SYS_CLK_PERIOD = 10;
parameter PERI_CLK_PERIOD = 40;
reg sys_clk, peri_clk;
initial sys_clk = 0;
always #(SYS_CLK_PERIOD / 2) sys_clk = ~sys_clk;
initial peri_clk = 0;
always #(PERI_CLK_PERIOD / 2) peri_clk = ~peri_clk;
initial begin
$dumpfile("tb_multi_clock.vcd");
$dumpvars(0, tb_multi_clock);
end
initial begin
#200;
$display("Completed at %0t ns", $time);
$finish;
end
endmodule
Compile and view the simulation waveforms:
iverilog -o tb_multi_clock tb_multi_clock.v
vvp tb_multi_clock
gtkwave tb_multi_clock.vcd
The waveform confirms four system clock cycles for each peripheral clock cycle.
The next article examines reset assertion protocols, minimum pulse widths, and deassertion timing.