Doubt in Verilog using always and forever

Status
Not open for further replies.

cnu4u

Newbie level 5
Joined
Feb 25, 2014
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
50
What is the difference in generating clock using 'always' and 'forever'? Please gimme detailed answer. Thanks in advance
 

For most simulation usage, there is no functional difference between
Code:
always 
  begin 
    #10 clk = 1; #5 clk = 0;
  end
and
Code:
initial forever
  begin 
    #10 clk = 1; #5 clk = 0;
  end
Semantically, the two forms are different even though they have the same end result.

An always construct declares a permanent thread of execution. At time 0, it starts the thread by executing the procedural statement that is part of the construct (in this case the begin/end block). When the procedural statement finishes, it executes it again. This thread of execution exists for the entire simulation and can never be terminated.

An initial construct declares a thread of execution that exists as long as there are procedural statements to execute. At time 0, it starts the thread by executing the procedural statement that is part of the construct (in this case the forever statement). When the procedural statement finishes, the thread terminates. In this case the thread does not terminated because the looping statement never ends.

More specifically, there are a few more things you can do with a forever statement that you cannot easily do with an always construct. By adding a delay before executing the forever statement you can introduce a phase shift.
Code:
initial #2 forever
  begin 
    #10 clk2 = 1; #5 clk2 = 0;
  end
clk and clk2 will have the same period, but clk2 is shifted by 2 timeunits.

As a looping statement, you can break out of a forever loop, and if you name the statement, you can disable it. So you can terminate the process created by an initial block. There is no way to terminate the process created by an always block.
 
Thank you Dave.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…