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.

1

2 Answers 2

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.

-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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.