Loop statements provide a means of modeling blocks of procedural statements.
repeat (expression) statement;
while (expression) statement;
for (assignment; expression; assignment) statement;
There are four types of loop statements: forever, repeat, while, and for statements.
The forever instruction (Example 1) continuously repeats the statement that follows it. Therefore, it should be used with procedural timing controls (otherwise it hangs the simulation).
The repeat instruction (Example 2) executes a given statement a fixed number of times. The number of executions is set by the expression, which follows the repeat keyword. If the expression evaluates to unknown, high-impedance, or a zero value, then no statement will be executed.
The while instruction (Example 3) executes a given statement until the expression is true. If a while statement starts with a false value, then no statement will be executed.
The for instruction (Example 4) executes a given statement until the expression is true. At the initial step, the first assignment will be executed. At the second step, the expression will be evaluated. If the expression evaluates to an unknown, high-impedance, or zero value, then the for statement will be terminated. Otherwise, the statement and second assignment will be executed. After that, the second step is repeated.
counter = 0;
forever #10 counter = counter + 1;
repeat (10) a = a + ~b;
parameter MSB = 8;
reg [MSB-1:0] Vector;
t = 0;
while (t < MSB)
//Initializes vector elelments
Vector[t] = 1'b0;
t = t + 1;
for (index=0; index < 10; index = index + 2)
mem[index] = index;
· The forever statement should contain at least one procedural timing control.
Post a Comment
Please provide valuable comments and suggestions for our motivation. Feel free to write down any query if you have regarding this post.