1
int c[][$][int];

I am trying to understand memories in depth. I was just wondering whether a 3D dynamic array with queue and associative array is possible or not?

I am able to make 2D dynamic array with queue like this:

int a[][$]; 

and it's working. I am able to write data in each location and retrieve as well.

I am stuck with int c[][$][$] and int c[][$][int].

I used this logic for 2D dynamic array with queue.

initial
    a=new[4];
         foreach(a[i])
            begin
                a[i].push_front(i*2);
                a[i].push_front(i*3);
                a[i].push_front(i*4);
            end

        $display("arr %0p size=%0d",a,$size(a));            
        
        foreach(a[i])
            for(int j=0; j<3;j++)
            begin
                b[l] =a[i].pop_front();
                l++;
            end
        $display("%0p",b);
end

For 3D, I was trying this:

initial
    begin
        c=new[4];
        foreach(c[i])
             a[i].push_front(i*2);
        $display("%0p",c);
    end

2 Answers 2

0

In your code, you didn't add anything to c in your foreach loop.

If c is an array of queues of queues, you can push multiple copies of the a[i] queue, for example:

module tb;

int a[][$];
int c[][$][$];

initial begin
    a = new[4];
    foreach (a[i]) begin
        a[i].push_front(i*2);
        a[i].push_front(i*3);
        a[i].push_front(i*4);
    end
    $display;
    $display("a %p size=%0d", a, $size(a));
    $display;

    c = new[4];
    foreach (c[i]) begin
        c[i].push_front( a[i] );
        c[i].push_front( a[i] );
    end
    $display("c %p", c);
    $display;
end
endmodule

Output:

a '{'{0, 0, 0}, '{4, 3, 2}, '{8, 6, 4}, '{12, 9, 6}} size=4

c '{'{'{0, 0, 0}, '{0, 0, 0}}, '{'{4, 3, 2}, '{4, 3, 2}}, '{'{8, 6, 4}, '{8, 6, 4}}, '{'{12, 9, 6}, '{12, 9, 6}}}

With an associative array:

module tb;

int c[][$][int];

initial begin
    c = new[4];
    foreach (c[i]) begin
        c[i].push_front( '{ 3 : i+4 } );
        c[i].push_front( '{ 7 : i+1 } );
    end
    $display("c %p", c);
end
endmodule

Output:

c '{'{'{7:1}, '{3:4}}, '{'{7:2}, '{3:5}}, '{'{7:3}, '{3:6}}, '{'{7:4}, '{3:7}}}
Sign up to request clarification or add additional context in comments.

Comments

0

Think of this way: SystemVerilog only has single dimensional arrays, but each element can be of any data type, including another array. This is known as arrays_of_arrays. For dynamically-sized arrays, like queues, dynamic, and associative arrays, you need to make sure each array element gets sized before accessing the next layer.

int c[][$][int];
initial begin
  c= new[4]; // creates an array of 4 empty queues
  foreach(c[i])
    repeat(i+1) 
      c[i].push_back('{default:0})
end

This creates a triangular shaped dynamic array of queues, where each element is an empty associative array. You can an assignment

 c[3][3][234] = 42; 

but the element c[1][3] does not exist yet.

Comments

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.