Friday, March 02, 2012

Loop statements

Loop Statements

The Loop Statements in SystemVerilog are as Follows.

» for
» while
» do --- while
» forever
» repeat
» foreach

Verilog provides for, while, repeat and forever loops. SystemVerilog enhances the Verilog for loop, and adds a do...while loop and a foreach loop.

#1ns r = a;
r = #1ns a;
r <= #1ns a;

The do...while loop:

do statement while(condition) // as C

The condition can be any expression which can be treated as a boolean. It is evaluated after the statement.

NOTE : First Prints and then Check.

Enhanced for loop:

In Verilog, the variable used to control a for loop must be declared prior to the loop. If loops in two or more parallel procedures use the same loop control variable, there is a potential of one loop modifying the variable while other loops are still using it. SystemVerilog adds the ability to declare the for loop control variable within the for loop. This creates a local variable within the loop. Other parallel loops cannot inadvertently affect the loop control variable.

For Example:

module for;
initial begin
for (int i = 0; i <= 100; i++)
...
end
initial begin
loop2: for (int i = 10; i >= 0; i--)
...
end
endmodule

Verilog only permits a single initial statement and a single step assignment within a for loop. SystemVerilog allows the initial declaration or assignment statement to be one or more comma-separated statements. The step assignment can also be one or more comma-separated assignment statements.

for ( int count = 0; count < 3; count++ )
value = value +((a[count]) * (count+1));
for ( int count = 0, done = 0, int j = 0; j * count < 125; j++ )
$display("Value j = %d\n", j );

The foreach loop:

The foreach construct specifies iteration over the elements of an array. Its argument is an identifier that designates any type of array (fixed-size, dynamic, or associative) followed by a list of loop variables enclosed in square brackets. Each loop variable corresponds to one of the dimensions of the array. The foreach construct is similar to a repeat loop that uses the array bounds to specify the repeat count instead of an expression.

string words [2] = { "hello", "world" };
int prod [1:8] [1:3];
foreach( words [ j ] )
$display( j , words[j] ); // print each index and value
foreach( prod[ k, m ] )
prod[k][m] = k * m; // initialize


No comments:

Post a Comment

Popular Posts