Clock Generation
Part of the Fundamentals section of Coddy's Verilog journey — lesson 71 of 90.
A clock is a signal that continuously toggles between 0 and 1 at regular intervals. Clocks are essential for sequential logic like flip-flops and counters.
Why Generate a Clock
In testbenches, you need a clock to test sequential circuits. The clock drives the behavior of flip-flops, registers, and state machines.
Methods to Generate a Clock
| Method | Description |
|---|---|
always with # delay | Most common method |
forever loop | Alternative method |
repeat loop | For fixed number of cycles |
Method 1: Always Block with Delay
reg clk;
initial begin
clk = 0;
end
always #5 clk = ~clk;clk = 0at time 0- Every 5 time units,
clktoggles
- Period = 10 time units
- Frequency = 1/10 = 0.1 per time unit
Method 2: Forever Loop
reg clk;
initial begin
clk = 0;
forever begin
#5 clk = ~clk;
end
endSame result as the always method.
Method 3: Repeat for Fixed Cycles
reg clk;
initial begin
clk = 0;
repeat (10) begin
#5 clk = ~clk;
end
endGenerates exactly 10 clock edges (5 complete cycles), then stops.
Challenge
Add the missing code to generate a clock that toggles every 4 time units (period = 8 time units).
What to do:
- Initialize
clkto 0 at time 0 using aninitialblock - Use an
alwaysblock with a delay to toggleclkevery 4 time units
Cheat sheet
A clock toggles between 0 and 1 at regular intervals. Period = 2 × delay.
Method 1: Always block (most common)
reg clk;
initial begin
clk = 0;
end
always #5 clk = ~clk; // Period = 10Method 2: Forever loop
initial begin
clk = 0;
forever #5 clk = ~clk;
endMethod 3: Repeat (fixed number of edges)
initial begin
clk = 0;
repeat(10) #5 clk = ~clk; // 10 edges = 5 cycles
endTry it yourself
module clock_challenge;
reg clk;
// TODO: Step 1 - Add initial block to set clk = 0
// TODO: Step 2 - Add always block to toggle clk every 4 time units
initial begin
$monitor("Time %0t: clk = %b", $time, clk);
#20;
$display("Clock generated for 20 time units");
$finish;
end
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design13Timing And Delays
What Are DelaysGate DelaysAssignment DelaysTimescale DirectiveClock GenerationRecap - Timing Control5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic