2D Array issue

I have my enum type defined as:

typedef enum {
chain_master,
A_master,
B_master,
ip1_slave,
ip2_slave,
master,
slave
} ip_chain;

I have a heirarchy which i have defined as following:

tree[chain_master].master=chain_master;
tree[A_master].master=chain_master;
tree[B_master].master=chain_master;
tree[ip1_slave].master=A_master;
tree[ip2_slave].master=B_master;

In my ovm test sequence I am doing the following:

ip_chain test='{[ip1_slave][slave], [B_master][master]};
enable_func (test);

First question is that, Is it the correct way of defining 2D array?
2nd Question.
I want to define my function ‘enable_func()’ in such a way that it gives me the 2D array. I want the following 2D array:

X[i][j]='{[ip1_slave][slave], [A_master][master], [B_master][master] [master_chain][master]};

I tried to do the following but it didnt work:

function enable_func(ip_chain array[][]);
foreach (array[][])
begin
ip_chain X[][];
do
X[i][j]=tree[ip_chain].master;
while(X[i][j]=X[i][j]);
end

I think I am making some stupid mistake… Can anyone help?
Kindly correct me.

You declare a 2D dynamic array by

ip_chain X;

You have to construct a dynamic array either by using new[…] or copy by assignment. You can only new one dimension of a dynamic array at a time. The second dimension would have to be a loop. You cannot assign individual array elements before they are constructed.

X = new[N];
foreach(X[i]) X[i] = new[M];

or

X = '{{ip1_slave,slave}, {A_master,master}, {B_master,master},{master_chain,master}};

Array names are not pointers. By default, function arguments are copied on input. You you either need to make this argument an output, or passed by reference.

In reply to dave_59:

thanks dave. You are right. I was defining 2D array in a wrong way.

So this is how I am declaring a 2D array now:

test = '{{ip1_slave,slave}, {B_master,master}};
enable_func (test);

Now I want to define my function ‘enable_func()’ in such a way that it gives me the 2D array.

function enable_func(ip_chain array[][]);
foreach (array[][])
begin
ip_chain X[][];
do
X[i][j]=tree[ip_chain].master;
while(X[i][j]=X[i][j]);
end

Can you please tell me how to write above function correctly so that I get the following output:

X = '{{ip1_slave,slave}, {A_master,master}, {B_master,master},{master_chain,master}};

Sorry, I am a beginner in System Verilog.

In reply to debuggerzc:

Actually from the heirarchy that I defined, I want an array of masters and slaves that I need to enable. Example in the above case, I am passing ip1_slave and B_master to the function. In the function I want an array that contains all the slave and masters that I need to enable. The reason I am using 2D array is because both the master and slave have a different configuration registers. Kindly modify my function definition.

In reply to debuggerzc:

It is going to be very difficult to teach you SystemVerilog in a forum a question at a time. I suggest you search for some books or on-line tutorials first. Once you familiarize yourself with SystemVerilog, then you can start learning OVM.

In reply to dave_59:

hi dave,

I am not going into 2D array now. Now I want to create a 1D array using the same function

typedef enum {
chain_master,
A_master,
B_master,
ip1_slave,
ip2_slave,
master,
slave
} ip_chain;

I have a heirarchy which i have defined as following:

`define tree \
begin \
tree[chain_master].master=chain_master; \
tree[A_master].master=chain_master; \
tree[B_master].master=chain_master; \
tree[ip1_slave].master=A_master; \
tree[ip2_slave].master=B_master; \
end

In my testcase I am doing the following:

ip_chain test='{[ip1_slave], [B_master]};
enable_func (test);

The output I want from my function is following:

X='{[ip1_slave], [A_master], [B_master], [master_chain]};

I want the above array because I want to enable ip1_slave and to enable it I first have to enable A_master and master_chain. Similarly to enable B_master I first have to enable master_chain.

I did the following but it didnt work:

function enable_func(ip_chain array[]);
foreach (array[i])
begin
ip_chain X[];
do
X[i]=tree[ip_chain].master;
while(X[i]=X[i]);
end

Any idea how my function should be?