Friday, March 02, 2012

Structures and Unions

Structures and Unions

UNPACKED STRUCTURES :

A structure in C++ is simply a composite data type consisting of a number of elements of other types.

typedef struct {
bit [7:0] opcode;
bit [23:0] addr;
} instruction; // named structure type
instruction IR; // define variable

PACKED STRUCTURES:

A packed structure is a mechanism for subdividing a vector into subfields that can be conveniently accessed as members. Consequently, a packed structure consists of bit fields, which are packed together in memory without gaps.

struct packed signed {
int a;
shortint b;
byte c;
bit [7:0] d; } pack1

UNIONS:

UnPacked Union:

Unlike structures, the components of a union all refer to the same location in memory. In this way, a union can be used at various times to hold different types of objects, without the need to create a separate object for each new type.

typedef union { // default unsigned
s_atmcell acell;
bit [423:0] bit_slice;
bit [52:0][7:0] byte_slice;
} u_atmcell

Packed Union:

A packed union shall contain members that must be packed structures, or packed arrays or integer data types all of the same size .

typedef union packed { // default unsigned
s_atmcell acell;
bit [423:0] bit_slice;
bit [52:0][7:0] byte_slice;
} u_atmcell;

No comments:

Post a Comment

Popular Posts