Verilog for loop |
Make use of for loop freely in test benches. Instead of linearly specifying the stimulus, use for loop to go through a set of values.
We will now see how the us of for loop simplifies the test bench. Let us take a look at the priority encoder example.
|
Note that the always statement
always @(x[4], x[3],x[2], x[1])
Could be written as
always @ *
We now suggest that you write a test bench for this code and verify that it works. If you have sifficulty, you can check it with following test bench
|
The test bench could be written as
|
Notice that the code gets simplified. Few things to be careful about in this code are 1. If instead of
for (x=0; x < 15; x= x+1)
we write
for (x=0; x <= 15; x= x+1)
it will create an infinite loop as x wraps from 15 to 0 ( since it is defined as 5 bit). Also notice that we need to explicitly declare and statement using ;.
Finally, we could omit the begin and end since we have only one statement to execute. So instead of
for (x=0; x < 15; x= x+1)
begin
#20 ;
end // end of for loop
We could write
for (x=0; x < 15; x= x+1)
#20 ; // end of for loop
Care must be taken when for loop is used in the synthesisable verilog code. We will cover it as in comes in other examples. For now we are just learning the for loop
constructs.