1

Is it possible to cast a packed array to an unpacked array and use the unpacked array as a parameter in a module instantiation? The packed array is defined as a localparam.

Here is an illustration of what I am trying to do

localparam [7:0] packed         = '0;
localparam      unpacked [3:0]  = packed[3:0]; <-- Needs to be casted to an unpacked array

module1 #(unpacked) myModule1(...); <--- The parameter here needs to be of unpacked type
3
  • Have you mixed up you packed and unpacked arrays? localparam packed[7:0] is an unpacked array; localparam [3:0] unpacked is a packed array. Commented Feb 1, 2017 at 15:19
  • And, BTW: packed is a reserved word in System-Verilog, so you cannot use it yourself. Commented Feb 1, 2017 at 15:19
  • There was a mistake in the example. The type of array should match the names. I have corrected the code to reflect this. Commented Feb 2, 2017 at 12:47

1 Answer 1

5

You can convert a packed array to an unpacked array using an assignment pattern, but it's not very flexible/extensible:

unpacked_array = '{ packed_array[0], packed_array[1], ... , packed_array[...]};

eg

localparam [7:0] packed_array   = '{default: 0};
localparam unpacked_array [3:0] = '{ packed_array[0], packed_array[1], packed_array[2], packed_array[3]}; 

module1 #(unpacked_array) myModule1();

https://www.edaplayground.com/x/3v8V

You can convert an unpacked array to a packed array using a streaming operator:

packed_array = { << { unpacked_array }};

eg

localparam       unpacked_array[7:0] = '{default: 0};
localparam [3:0] packed_array        = { << { unpacked_array[3:0] }}; 

module1 #(packed_array) myModule1();

https://www.edaplayground.com/x/5Q5b

Sign up to request clarification or add additional context in comments.

2 Comments

A better solution to the streaming operator when using fixed sized arrays is a bit-stream cast. A little more work because you need to create typedefs for the cast, but safer as it will not pad if the number of bits does not match.
I did a mistake when I wrote the example. I did mean converting from packed to unpacked array, I just placed the brackets on the wrong side of the parameter name. So the question is: is it possible to do the conversion the opposite direction from what you showed me? From packed to unpacked.

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.