Eivind
Eivind

Reputation: 33

Casting packed array to unpacked array for arrays that are used as parameters

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

Upvotes: 1

Views: 16482

Answers (1)

Matthew
Matthew

Reputation: 14007

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

Upvotes: 5

Related Questions