SystemVerilog Arrays

So, I was talking with my colleague the other day about how to pass a multi-dimension array of parameters. It started with that he could not get his code with a multi-dimension array of parameters compiled.  Since I’m the one who should be familiar with SystemVerilog structs in our group, he referred to me for help. Actually, I don’t remember the syntax that well and I always do some trials every time I need to use array structures.

So, the first problem is that he encountered compile error when he was trying to have a 2D dynamic array of parameters.


parameter int STATE_ARRAY [2] [] = '{'{30, 100}, {24,155}};

The error message is:

Unsupported element datatype for array parameter.

But when I delete “parameter”, make it a regular 2D dynamic array, everything is fine. So, I think NCVerilog, (the simulator I’m using at this moment), doesn’t support 2D dynamic parameter. Then I talked to my colleague Kevin, a senior engineer, he pointed out it is a bad idea to use those “fancy” data structures in your design, which can be a pain when debugging. So, the solution here is simply to flatten the 2D parameter array into 2 individual arrays.

 
parameter int STATE_UP [] = '{30, 100}; 
parameter int STATE_DOWN [] = '{24, 155}; 

The other thing we were talking about how to use the array method sum() when we instantiated another array. Basically, we wanted to do something like this:

 
parameter int PULSE_ARRAY [4] = '{1, 2, 4, 1}; 
parameter STATE_ARRAY[PULSE_ARRAY.sum()] = '{24, 12, 53, 14, 15, 62, 35, 16};

Apparently, it failed. I talked to Kevin again and realized I made a big mistake here. Since for fixed array, the memory is allocated at the very beginning. But it takes some time to execute the sum() method. For fixed array, the index should be a constant, which cannot be a result from array method. We use a dynamic array instead then.

We were talking about how the index works when we are trying to instantiate the unpacked and packed array, which is pretty interesting.


logic [3:0] a = 4'b0011;
logic [0:3] b = 4'b0011;

It turns out that the LSB of a and b are totally different. It actually maps directly to how you instantiate the index.
a[0] = 1;
b[0] = 0;

Also, you cannot do one index instantiation for a packed array.

 
logic [4] a = 4'b0011; 

This is wrong and only works for an unpacked array.

Above is what I learned, or reviewed again, from our discussion. Thanks Kevin for his constant help. I guess I still have a long way to go, a lot to learn. Prolly still need to do some trials again next time I use array structures haha.

 

Leave me a comment if you find this article interesting or want to share your opinions with you. 🙂

 

 

 

Leave a Reply

Your email address will not be published.