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
The main difference between always and forever is that always usually means at all times or on all occasions whereas forever usually means for an endless time. 1. Meaning and Usage of Always
forever is a procedural statement that can only be used in a procedural context. So it is legal to write initial forever or always forever, but not just forever. The situation where forever becomes quite important is within tasks, which are procedural contexts, so use of always is not allowed.
Most of us use “Always” and “Never” statements rhetorically - it’s also wise to use them sparingly, lest our own credibility be called into question. “Always” and “Never” statements are popular delivery vehicles of FOG - Fear, Obligation and Guilt.
This meaning is similar to always and forever can be replaced by always in sentences that carry this meaning. For example, He is forever asking questions. = He is always asking questions. We’ve been waiting forever.
Your second code snippet is actually a syntax error. The difference between forever
and always
is that always
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, whereas initial
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 write initial forever
or always forever
, but not just forever
.
The situation where forever
becomes quite important is within tasks, which are procedural contexts, so use of always
is not allowed. (Functions are procedural contexts as well, but may not contain delays, which makes it unlikely that forever
will come in useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With