1

I'm new to systemverilog; I need to run over the elements of an array that I don't know its size. I need to read 2 elements in each iteration so I cant use foreach (can I ? ). I need to do something like that :

for(int i = 0 ; i < arraySize ; i+=2 ){
foo(data[i],data[i+1]);
} 

but I don't have arraySize in hand.

Adi
  • 81
  • 3
  • 4
  • 12
  • Everyone that uses SystemVerilog should have a copy of the [IEEE 1800-2012 Language Reference Manual] (http://standards.ieee.org/findstds/standard/1800-2012.html). You could easily find your answer there. – dave_59 Nov 11 '15 at 18:00

2 Answers2

5

While using SystemVerilog, this must be one of the very first thing to learn. This is very common and important method in SystemVerilog. Either use $size or arrayname.size method.

Following is your sample code, I have used arrayname.size method, the same can be accomplished by $size(arrayname).

for(int i = 0 ; i < data.size ; i+=2 )
begin // no curly braces here, except constraints
     foo(data[i],data[i+1]); 
end

There is also a foreach loop for which the size of array need not to be known. The above mentioned task can be called as follows. Here you want i+1th element to exist, so you must use size method. Henceforth there is no extra benefits of this method.

foreach(data[i])
begin
if(i%2 == 0 && i+1<data.size)
  foo(data[i],data[i+1]);
end 

Note that i is an internal variable here, no need to declare i.

More information can be available at this link. SystemVerilog LRM 1800-2012 must be very helpful.

sharvil111
  • 4,301
  • 1
  • 14
  • 29
-1

Is it Java? If so

for(int i = 0 ; i < data.length - 1 ; i+=2 ){
    foo(data[i],data[i+1]);
} 

If not, it would probably be same or at least similar

This -1 in data.length - 1 is important, because you are accessing data[i+1] and you have to be sure to not be out of bounds.


Ach sorry, I am blind, it is system-verilog... Well I put systemverilog array size into google and it looks like data.size() should work

libik
  • 22,239
  • 9
  • 44
  • 87