Both the following codes generate a clock. I need to know if there is any use of forever loop other than clock generation? I have only come across forever in clock generation. If it only serves this purpose, isn't it useless?
initial begin
clk = 0;
forever begin
#5 clk = ~clk;
end
end
initial begin
clk = 0 ;
always begin
# 5 clk = ~clk;
end
end
Yes,
forever
loops have widespread usage in testbenches for design verification, particulalry in the industry standard methodology, UVM, and its predecessors (such as VMM). They are used in verification components such as drivers and monitors which make extensive use of tasks in SystemVerilog classes.Your second code snippet is actually a syntax error. The difference between
forever
andalways
is thatalways
can exist as a "module item", which is the name that the Verilog spec gives to constructs that may be written directly within a module, not contained within some other construct.initial
is also a module item.always
blocks are repeated, whereasinitial
blocks are run once at the start of the simulation.forever
is a procedural statement that can only be used in a procedural context. So it is legal to writeinitial forever
oralways forever
, but not justforever
.The situation where
forever
becomes quite important is within tasks, which are procedural contexts, so use ofalways
is not allowed. (Functions are procedural contexts as well, but may not contain delays, which makes it unlikely thatforever
will come in useful.//i will show the difference;;;;;;; //we cannot use the forever block inside the initial block,but we can use forever block inside the intial block //code with error :