6

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

Victor Lyuboslavsky
  • 9,882
  • 25
  • 87
  • 134

2 Answers2

9

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

Tudor Timi
  • 7,453
  • 1
  • 24
  • 53
  • @dave_59 @Tudor VCS is accepting `foreach (array[0][j])` but not accepting `automatic int h = 0; foreach (array[,j])` giving error message as Error-[NYI] Not Yet Implemented bus.sv, 318 Feature is not yet supported: Foreach with omitted loop variables preceding variable-size dimensions – Chandan Choudhury Nov 25 '14 at 07:21
  • Also Questa does not support this for dynamic array: Cannot omit loop variable while iterating over a dynamic array – Chandan Choudhury Nov 25 '14 at 08:46
4

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])

dave_59
  • 39,096
  • 3
  • 24
  • 63
  • Is `foreach (array[,j])` format part of the LRM? If so, where? I could find anything mentioned. Great feature, just hope it is part of the standard. – Greg Apr 17 '14 at 19:53
  • 3
    @Greg: In 18.5.8.1, Syntax 18-7 (Foreach iterative constraint syntax), `loop_variables` can start with a comma, not necessarily an identifier: foreach ( ps_or_hierarchical_array_identifier [ loop_variables ] ) constraint_set loop_variables ::= [ index_variable_identifier ] { , [ index_variable_identifier ] } – Ari Apr 17 '14 at 21:14
  • Ah, I missed it because it was written in BNF (Backus-Naur Form). – Greg Apr 17 '14 at 23:40