Forever Loop
Part of the Fundamentals section of Coddy's Verilog journey — lesson 61 of 90.
The forever loop repeats a block of code continuously, forever. It never stops on its own.
A forever loop executes repeatedly without end. It is useful for generating clocks and other continuous signals in testbenches.
Syntax:
forever begin
// Code that repeats forever
endSimple Example
forever begin
$display("This prints forever");
endThis will print endlessly and crash your simulation. Always add a delay or a stopping condition.
Generating a Clock (Common Use)
The most common use of forever is to generate a clock:
initial begin
clk = 0;
forever begin
#5 clk = ~clk; // Toggle every 5 time units
end
endThis creates a clock that runs for the entire simulation.
Forever with Disable
You can stop a forever loop using a disable statement:
initial begin : clock_gen // Name added here
clk = 0;
forever begin
#5 clk = ~clk;
end
endinitial begin
#100;
disable clock_gen; // Now this works
endForever vs Other Loops
| Loop | Stops? | When to Use |
|---|---|---|
for | Yes (after fixed iterations) | Known number of repeats |
while | Yes (when condition false) | Unknown stop condition |
repeat | Yes (after fixed iterations) | Known number of repeats |
forever | No (never) | Continuous signals (clock) |
Important Rules
| Rule | Explanation |
|---|---|
| Must include a delay | #10 or @(posedge clk) |
| Without delay, simulation hangs | Infinite loop with no time advance |
Use with disable to stop | Or simulation never ends |
| Best used in testbenches | Not synthesizable |
Challenge
What to do:
Add the missing forever loop to generate a clock that toggles every 10 time units.
Cheat sheet
The forever loop repeats a block of code continuously without stopping. Always include a delay to prevent simulation hang.
initial begin
clk = 0;
forever begin
#5 clk = ~clk; // Toggle every 5 time units
end
endStop a forever loop using disable with a named block:
initial begin : clock_gen
clk = 0;
forever begin
#5 clk = ~clk;
end
end
initial begin
#100;
disable clock_gen;
endKey rules:
- Must include a delay (
#10or@(posedge clk)), otherwise simulation hangs - Use
disableto stop, or simulation never ends - Not synthesizable — testbench use only
Try it yourself
module forever_challenge;
reg clk;
initial begin
clk = 0;
// TODO: Add forever loop to toggle clk every 10 time units
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 Design5Operators 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