Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SystemVerilog foreach syntax for looping through lower dimension of multidimensional array

What is the standard way of looping through the lower dimension of a multidimensional array? With the higher dimension fixed.

In the following example:

  automatic int i = 2;
  foreach (my_req[i][j]) begin // <-- WARNING
    $display("i:%0d,j:%0d", i, j);
  end

I see the warning:

** Warning: testbench.sv(16): (vlog-LRM-2897) Using non-standard foreach loop variable list syntax.

Full code example on EDA Playground: http://www.edaplayground.com/x/nh

like image 654
Victor Lyuboslavsky Avatar asked Apr 17 '14 14:04

Victor Lyuboslavsky


People also ask

How do you iterate through a multidimensional array?

To loop over two dimensional array in Java you can use two for loops. Each loop uses an index. Index of outer for loop refers to the rows, and inner loop refers to the columns. You can then get each element from the array using the combination of row and column indexes.

What is foreach in SystemVerilog?

In SystemVerilog the foreach statement can be used to iterate over the elements of an array. Special attention should be payed to loop variables (SystemVerilog IEEE 1800-2012 LRM Chapter 12.7. 3, page 281), as their behavior depends on how the array dimensions are specified at declaration.

What is the difference between for and foreach loop in SystemVerilog?

The biggest differences are that a foreach loop processes an instance of each element in a collection in turn, while a for loop can work with any data and is not restricted to collection elements alone. This means that a for loop can modify a collection - which is illegal and will cause an error in a foreach loop.

Is foreach loop used in Verilog?

SystemVerilog arrays are data structures that allow storage of many values in a single variable. A foreach loop is only used to iterate over such arrays and is the easiest and simplest way to do so.


Video Answer


2 Answers

You can do this:

$display("Loop through i=2");
begin
  automatic int i = 2;
  foreach (my_req[,j]) begin // notice the "," before j
    $display("i:%0d,j:%0d", i, j);
  end
end

Working code on EDA Playground: http://www.edaplayground.com/x/2Qn

like image 90
Tudor Timi Avatar answered Oct 21 '22 11:10

Tudor Timi


The warning is to let you know that the code you are writing may not be compatible with other simulators. I do know that at one time, another simulator accepted foreach (array[i][j]) where i was not previously defined to mean the same thing as foreach(array[i,j]), and that syntax would not be compatible with what you are trying to do.

The correct syntax is foreach (my_req[,j])

like image 30
dave_59 Avatar answered Oct 21 '22 12:10

dave_59