Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Looping over a 3d Systemverilog array

Status
Not open for further replies.

shaiko

Advanced Member level 5
Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,302
Hello,

I have a 3d array defined as :
Code:
logic [1:0] [2:0] [3:0] some_3d_array  ;
I want to write a "foreach" loop that iterates over all dimensions.
Is this the correct syntax ?
Code:
foreach (some_3d_array[i])
begin
    foreach (some_3d_array[i][j])
    begin
        foreach (some_3d_array[i][j][k])
        begin
        // do something
        end
    end
end
 

No. This syntax is not legal event if some tools allow it. You'll wind up iterating over the first dimension 3 times.

If you only want to do something at the inner most loop, you can do
Code:
foreach (some_3d_array[i,j,k])
      begin
      //do something 
     end

But if you really need 3 separate loops, you can do
Code:
foreach (some_3d_array[i])
begin
    // do something over i
    foreach (some_3d_array[,j])
    begin
        // do sometthing over j
        foreach (some_3d_array[,,k])
        begin
        // do something over k
        end
    end
end
 

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Thanks dave_59.
What if my loop was defined as :
Code:
logic [1:0] [2:0] some_3d_array [3:0] ; // one unpacked dimension
How would the code change ?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top