George
George

Reputation: 157

Converting an array of one type to another - system verilog

I am looking to convert an array of one type to another.

As an example, let's say we have an array of type byte and would like a new array of type logic [5:0].

I am having difficulty doing this while not trying to lose data.

Thanks,

George

Upvotes: 0

Views: 2217

Answers (2)

nick_g
nick_g

Reputation: 489

A data type could be converted by using a cast (') operation.

For your example, you could do the following:

typedef byte byte_arr_t[];
byte_arr_t byte_arr;

typedef logic logic_arr_t[5:0];
logic_arr_t logic_arr;

logic_arr = logic_arr_t'(byte_arr);

remembering that you must also be aware of the size of the arrays and its elements, or data could be lost.

Upvotes: 1

dave_59
dave_59

Reputation: 42698

If you want to convert an array of one type to another, you can easily do that with a bit-stream cast if they have the exact same number of total bits. Dynamically sized arrays would need to be sized to have the exact number of bits.

An array of 60 bytes can be cast to an array of 80 6-bits because they both contain 480 bits.

typedef logic [7:0] array_8_60_t[60];
typedef logic [5:0] array_6_80_t[60];
array_8_60_t a1;
array_6_80_t a2;

...
a2 = array_6_80_t'(a1);

If the target array(a2) has more bits than required, or you have some special formatting, you will need to look at the streaming operators, or revert to a foreach loop;

Upvotes: 1

Related Questions