packed_array

virtual class packed_array #(type T =  bit,
int WIDTH =  1)

A parameterized class that manages a packed array.

Parameters

Toptional The type of a packed array.  The type T must be the single bit data types (bit, logic, or reg), enumerated types, or other packed arrays or packed structures.  The default type is bit.
WIDTHoptional The width of a packed array.  The default is 1.
Summary
packed_arrayA parameterized class that manages a packed array.
Types
pa_typeThe shorthand of the packed array type of type T.
ua_typeThe shorthand of the unpacked array type of type T.
da_typeThe shorthand of the dynamic array type of type T.
q_typeThe shorthand of the queue type of type T.
Functions
from_unpacked_arraystatic Converts an unpacked array of type T to a packed array of the same type.
to_unpacked_arraystatic Converts a packed array of type T to an unpacked array of the same type.
from_dynamic_arraystatic Converts a dynamic array of type T to a packed array of the same type.
to_dynamic_arraystatic Converts a packed array of type T to a dynamic array of the same type.
from_queuestatic Converts a queue of type T to a packed array of the same type.
to_queuestatic Converts a packed array of type T to a queue of the same type.
ua_to_pastatic Converts an unpacked array of type T to a packed array of the same type.
pa_to_uastatic Converts a packed array of type T to an unpacked array of the same type.
da_to_pastatic Converts a dynamic array of type T to a packed array of the same type.
pa_to_dastatic Converts a packed array of type T to a dynamic array of the same type.
q_to_pastatic Converts a queue of type T to a packed array of the same type.
pa_to_qstatic Converts a packed array of type T to a queue of the same type.
initstatic Initializes the each element of the given packed array to the specified value.
reversestatic Reverses the order of the elements of the given packed array.
count_onesstatic Counts the number of bits having value 1.
count_zerosstatic Counts the number of bits having value 0.
count_unknownsstatic Counts the number of bits having value X.
count_hizsstatic Counts the number of bits having value Z.

Types

pa_type

typedef T [WIDTH-1:0] pa_type

The shorthand of the packed array type of type T.

ua_type

typedef T ua_type[WIDTH]

The shorthand of the unpacked array type of type T.

da_type

typedef T da_type[]

The shorthand of the dynamic array type of type T.

q_type

typedef T q_type[$]

The shorthand of the queue type of type T.

Functions

from_unpacked_array

static function pa_type from_unpacked_array(const ref ua_type ua,  
input bit reverse =  0)

static Converts an unpacked array of type T to a packed array of the same type.

Arguments

uaAn unpacked array to be converted.
reverseoptional If 0, the element at the index 0 of ua is positioned to the index 0 of the packed array.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

A packed array converted from ua.

Example

bit ua[8] = '{ 0, 0, 0, 1, 1, 0, 1, 1 }; // assigned to ua[0:7]
assert( packed_array#(bit,8)::from_unpacked_array( ua                ) == 8'hD8 ); // bit[7:0]
assert( packed_array#(bit,8)::from_unpacked_array( ua, .reverse( 1 ) ) == 8'h1B );

See Also

ua_to_pa

to_unpacked_array

static function ua_type to_unpacked_array(const ref pa_type pa,  
input bit reverse =  0)

static Converts a packed array of type T to an unpacked array of the same type.

Arguments

paA packed array to be converted.
reverseoptional If 0, the element at the index 0 of pa is positioned to the index 0 of the unpacked array.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

An unpacked array converted from pa.

Example

bit[7:0] pa = 8'hD8;
bit ua0[8] = '{ 0, 0, 0, 1, 1, 0, 1, 1 };
bit ua1[8] = '{ 1, 1, 0, 1, 1, 0, 0, 0 };

assert( packed_array#(bit,8)::to_unpacked_array( pa                ) == ua0 );
assert( packed_array#(bit,8)::to_unpacked_array( pa, .reverse( 1 ) ) == ua1 );

See Also

pa_to_ua

from_dynamic_array

static function pa_type from_dynamic_array(const ref da_type da,  
input bit reverse =  0)

static Converts a dynamic array of type T to a packed array of the same type.  If the size of the dynamic array is larger than WIDTH, the excess elements are ignored.  If the size of the dynamic array is smaller than WIDTH, the default value of type T is used for the missing elements.

Arguments

daA dynamic array to be converted.
reverseoptional If 0, the element at the index 0 of da is positioned to the index 0 of the packed array.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

A packed array converted from da.

Example

bit da[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } ); // da[0] to da[7]
assert( packed_array#(bit,8)::from_dynamic_array( da                ) == 8'hD8 ); // bit[7:0]
assert( packed_array#(bit,8)::from_dynamic_array( da, .reverse( 1 ) ) == 8'h1B );

See Also

da_to_pa

to_dynamic_array

static function da_type to_dynamic_array(const ref pa_type pa,  
input bit reverse =  0)

static Converts a packed array of type T to a dynamic array of the same type.

Arguments

paA packed array to be converted.
reverseoptional If 0, the element at the index 0 of pa is positioned to the index 0 of the dynamic array.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

A dynamic array converted from pa.

Example

bit[7:0] pa = 8'hD8;
bit da0[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit da1[] = new[8]( '{ 1, 1, 0, 1, 1, 0, 0, 0 } );

assert( packed_array#(bit,8)::to_dynamic_array( pa                ) == da0 );
assert( packed_array#(bit,8)::to_dynamic_array( pa, .reverse( 1 ) ) == da1 );

See Also

pa_to_da

from_queue

static function pa_type from_queue(const ref q_type q,  
input bit reverse =  0)

static Converts a queue of type T to a packed array of the same type.  If the size of the queue is larger than WIDTH, the excess elements are ignored.  If the size of the queue is smaller than WIDTH, the default valus of type T is used for the missing elements.

Arguments

qA queue to be converted.
reverseoptional If 0, the element at the index 0 of q is positioned to the index 0 of the packed array.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

A packed array converted from q.

Example

bit q[$] = { 0, 0, 0, 1, 1, 0, 1, 1 }; // q[0] to q[7]
assert( packed_array#(bit,8)::from_queue( q                ) == 8'hD8 ); // bit[7:0]
assert( packed_array#(bit,8)::from_queue( q, .reverse( 1 ) ) == 8'h1B );

See Also

q_to_pa

to_queue

static function q_type to_queue(const ref pa_type pa,  
input bit reverse =  0)

static Converts a packed array of type T to a queue of the same type.

Arguments

paA packed array to be converted.
reverseoptional If 0, the element at the index 0 of pa is positioned to the index 0 of the queue.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

A queue converted from pa.

Example

bit[7:0] pa = 8'hD8;
bit q0[$] = { 0, 0, 0, 1, 1, 0, 1, 1 };
bit q1[$] = { 1, 1, 0, 1, 1, 0, 0, 0 };

assert( packed_array#(bit,8)::to_queue( pa                ) == q0 );
assert( packed_array#(bit,8)::to_queue( pa, .reverse( 1 ) ) == q1 );

See Also

pa_to_q

ua_to_pa

static function void ua_to_pa(const ref ua_type ua,  
ref pa_type pa,  
input bit reverse =  0)

static Converts an unpacked array of type T to a packed array of the same type.  Unlike from_unpacked_array, this function populates the packed array passed by reference, instead of returning a new packed array.

Arguments

uaAn unpacked array to be converted.
paA packed array reference to be populated.
reverseoptional If 0, the element at the index 0 of ua is positioned to the index 0 of pa.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

None.

Example

bit ua[8] = '{ 0, 0, 0, 1, 1, 0, 1, 1 }; // assigned to ua[0:7]
bit[7:0] pa;

packed_array#(bit,8)::ua_to_pa( ua, pa );
assert( pa == 8'hD8 ); // bit[7:0]

packed_array#(bit,8)::ua_to_pa( ua, pa, .reverse( 1 ) );
assert( pa == 8'h1B );

See Also

from_unpacked_array

pa_to_ua

static function void pa_to_ua(const ref pa_type pa,  
ref ua_type ua,  
input bit reverse =  0)

static Converts a packed array of type T to an unpacked array of the same type.  Unlike to_unpacked_array, this function populates the unpacked array passed by reference, instead of returning a new unpacked array.

Arguments

paA packed array to be converted.
uaAn unpacked array reference to be populated.
reverseoptional If 0, the element at the index 0 of pa is positioned to the index 0 of ua.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

None.

Example

bit[7:0] pa = 8'hD8;
bit ua[8];
bit ua0[8] = '{ 0, 0, 0, 1, 1, 0, 1, 1 };
bit ua1[8] = '{ 1, 1, 0, 1, 1, 0, 0, 0 };

packed_array#(bit,8)::pa_to_ua( pa, ua );
assert( ua == ua0 );

packed_array#(bit,8)::pa_to_ua( pa, ua, .reverse( 1 ) );
assert( ua == ua1 );

See Also

to_unpacked_array

da_to_pa

static function void da_to_pa(const ref da_type da,  
ref pa_type pa,  
input bit reverse =  0)

static Converts a dynamic array of type T to a packed array of the same type.  Unlike from_dynamic_array, this function populates the packed array passed by reference, instead of returning a new packed array.  If the size of the dynamic array is larger than WIDTH, the excess elements are ignored.  If the size of the dynamic array is smaller than WIDTH, the default value of type T is used for the missing elements.

Arguments

daA dynamic array to be converted.
paA packed array to be populated.
reverseoptional If 0, the element at the index 0 of da is positioned to the index 0 of pa.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

None.

Example

bit da[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } ); // da[0] to da[7]
bit[7:0] pa;

packed_array#(bit,8)::da_to_pa( da, pa );
assert( pa == 8'hD8 ); // bit[7:0]

packed_array#(bit,8)::da_to_pa( da, pa, .reverse( 1 ) );
assert( pa == 8'h1B );

See Also

from_dynamic_array

pa_to_da

static function void pa_to_da(const ref pa_type pa,  
ref da_type da,  
input bit reverse =  0)

static Converts a packed array of type T to a dynamic array of the same type.  Unlike to_dynamic_array, this function populates the dynamic array passed by reference, instead of returning a new dynamic array.

Arguments

paA packed array to be converted.
daA dynamic array to be populated.  This function does not resize da.  Make sure to set the size of the dynamic array to accommodate the elements of pa before calling this function.
reverseoptional If 0, the element at the index 0 of pa is positioned to the index 0 of da.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

None.

Example

bit[7:0] pa = 8'hD8;
bit da [] = new[8]; // set the size of da[]
bit da0[] = new[8]( '{ 0, 0, 0, 1, 1, 0, 1, 1 } );
bit da1[] = new[8]( '{ 1, 1, 0, 1, 1, 0, 0, 0 } );

packed_array#(bit,8)::pa_to_da( pa, da );
assert( da == da0 );

packed_array#(bit,8)::pa_to_da( pa, da, .reverse( 1 ) );
assert( da == da1 );

See Also

to_dynamic_array

q_to_pa

static function void q_to_pa(const ref q_type q,  
ref pa_type pa,  
input bit reverse =  0)

static Converts a queue of type T to a packed array of the same type.  Unlike from_queue, this function populates the packed array passed by reference, instead of returning a new packed array.  If the size of the queue is larger than WIDTH, the excess elements are ignored.  If the size of the queue is smaller than WIDTH, the default valus of type T is used for the missing elements.

Arguments

qA queue to be converted.
paA packed array to be populated.
reverseoptional If 0, the element at the index 0 of q is positioned to the index 0 of pa.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

None.

Example

bit q[$] = { 0, 0, 0, 1, 1, 0, 1, 1 }; // q[0] to q[7]
bit[7:0] pa;

packed_array#(bit,8)::q_to_pa( q, pa );
assert( pa == 8'hD8 ); // bit[7:0]

packed_array#(bit,8)::q_to_pa( q, pa, .reverse( 1 ) );
assert( pa == 8'h1B );

See Also

from_queue

pa_to_q

static function void pa_to_q(const ref pa_type pa,  
ref q_type q,  
input bit reverse =  0)

static Converts a packed array of type T to a queue of the same type.  Unlike to_queue, this function populates the queue passed by reference instead of returning a new queue.

Arguments

paA packed array to be converted.
qA queue to be populated.
reverseoptional If 0, the element at the index 0 of pa is positioned to the index 0 of q.  If 1, the elements are positioned in the reverse order.  The default is 0.

Returns

None.

Example

bit[7:0] pa = 8'hD8;
bit q [$];
bit q0[$] = { 0, 0, 0, 1, 1, 0, 1, 1 };
bit q1[$] = { 1, 1, 0, 1, 1, 0, 0, 0 };

packed_array#(bit,8)::pa_to_q( pa, q );
assert( q == q0 );

q.delete();
packed_array#(bit,8)::pa_to_q( pa, q, .reverse( 1 ) );
assert( q == q1 );

See Also

to_queue

init

static function void init(ref pa_type pa,
input val)

static Initializes the each element of the given packed array to the specified value.

Arguments

paA packed array to be initialized.
valA value to initialize the elements of pa.

Returns

None.

Example

bit[7:0] pa;
packed_array#(bit,8)::init( pa, 1'b1 );
assert( pa == 8'hFF );

reverse

static function void reverse(ref pa_type pa)

static Reverses the order of the elements of the given packed array.

Argument

paA packed array to be reversed.

Returns

None.

Example

bit[7:0] pa = 8'h0F;
packed_array#(bit,8)::reverse( pa );
assert( pa == 8'hF0 );

count_ones

static function int count_ones(pa_type pa)

static Counts the number of bits having value 1.

Argument

paA packed array.

Returns

The number of bits having value 1.  If the type T is not a single-bit data type, -1 is returned.

Example

bit[15:0] pa = 16'h1234; // 16'b0001_0010_0011_0100
assert( packed_array#(bit,16)::count_ones( pa ) == 5 );

count_zeros

static function int count_zeros(pa_type pa)

static Counts the number of bits having value 0.

Argument

paA packed array.

Returns

The number of bits having value 0.  If the type T is not a single-bit data type, -1 is returned.

Example

bit[15:0] pa = 16'h1234; // 16'b0001_0010_0011_0100
assert( packed_array#(bit,16)::count_zeros( pa ) == 11 );

count_unknowns

static function int count_unknowns(pa_type pa)

static Counts the number of bits having value X.

Argument

paA packed array.

Returns

The number of bits having value X.  If the type T is not a single-bit data type, -1 is returned.

Example

logic[15:0] pa = 16'b0000_1111_xxxx_zzzz;
assert( packed_array#(logic,16)::count_unknowns( pa ) == 4 );

count_hizs

static function int count_hizs(pa_type pa)

static Counts the number of bits having value Z.

Argument

paA packed array.

Returns

The number of bits having value Z.  If the type T is not a single-bit data type, -1 is returned.

Example

logic[15:0] pa = 16'b0000_1111_xxxx_zzzz;
assert( packed_array#(logic,16)::count_hizs( pa ) == 4 );
virtual class packed_array #(type T =  bit,
int WIDTH =  1)
A parameterized class that manages a packed array.
typedef T [WIDTH-1:0] pa_type
The shorthand of the packed array type of type T.
typedef T ua_type[WIDTH]
The shorthand of the unpacked array type of type T.
typedef T da_type[]
The shorthand of the dynamic array type of type T.
typedef T q_type[$]
The shorthand of the queue type of type T.
static function pa_type from_unpacked_array(const ref ua_type ua,  
input bit reverse =  0)
static Converts an unpacked array of type T to a packed array of the same type.
static function ua_type to_unpacked_array(const ref pa_type pa,  
input bit reverse =  0)
static Converts a packed array of type T to an unpacked array of the same type.
static function pa_type from_dynamic_array(const ref da_type da,  
input bit reverse =  0)
static Converts a dynamic array of type T to a packed array of the same type.
static function da_type to_dynamic_array(const ref pa_type pa,  
input bit reverse =  0)
static Converts a packed array of type T to a dynamic array of the same type.
static function pa_type from_queue(const ref q_type q,  
input bit reverse =  0)
static Converts a queue of type T to a packed array of the same type.
static function q_type to_queue(const ref pa_type pa,  
input bit reverse =  0)
static Converts a packed array of type T to a queue of the same type.
static function void ua_to_pa(const ref ua_type ua,  
ref pa_type pa,  
input bit reverse =  0)
static Converts an unpacked array of type T to a packed array of the same type.
static function void pa_to_ua(const ref pa_type pa,  
ref ua_type ua,  
input bit reverse =  0)
static Converts a packed array of type T to an unpacked array of the same type.
static function void da_to_pa(const ref da_type da,  
ref pa_type pa,  
input bit reverse =  0)
static Converts a dynamic array of type T to a packed array of the same type.
static function void pa_to_da(const ref pa_type pa,  
ref da_type da,  
input bit reverse =  0)
static Converts a packed array of type T to a dynamic array of the same type.
static function void q_to_pa(const ref q_type q,  
ref pa_type pa,  
input bit reverse =  0)
static Converts a queue of type T to a packed array of the same type.
static function void pa_to_q(const ref pa_type pa,  
ref q_type q,  
input bit reverse =  0)
static Converts a packed array of type T to a queue of the same type.
static function void init(ref pa_type pa,
input val)
static Initializes the each element of the given packed array to the specified value.
static function void reverse(ref pa_type pa)
static Reverses the order of the elements of the given packed array.
static function int count_ones(pa_type pa)
static Counts the number of bits having value 1.
static function int count_zeros(pa_type pa)
static Counts the number of bits having value 0.
static function int count_unknowns(pa_type pa)
static Counts the number of bits having value X.
static function int count_hizs(pa_type pa)
static Counts the number of bits having value Z.