Understanding SystemVerilog Structures Data Type

If you are delving into the world of circuit design, you might have heard of SystemVerilog structures data type. Structures provide an efficient way to organize and store complex data types. In this article, we will dive into the details of SystemVerilog structures, including the difference between unpacked and packed structures, and how to assign values to them.

Unpacked Structures

Unpacked structures are used to define a group of variables that have different data types. You can think of them as a collection of variables that are grouped together under a common name. Here is an example of an unpacked structure in SystemVerilog:

struct {
  logic [7:0]  data1;
  logic [15:0] data2;
  bit          enable;
} myStruct;

In this example, we have defined a structure called myStruct that contains three variables: data1, data2, and enable. The variable data1 is an 8-bit logic variable, data2 is a 16-bit logic variable, and enable is a single bit variable.

Packed Structures

Packed structures in SystemVerilog allow you to organize data into a single vector, which can then be divided into subfields. To declare a packed structure, you use the packed keyword, which tells the compiler to allocate the subfields in a contiguous block of memory.

Here's an example of a packed structure with two subfields:

struct packed {
  logic [7:0]  subfield1;
  logic [15:8] subfield2;
} myPackedStruct;

In this example, myPackedStruct contains two subfields: subfield1 and subfield2. The packed keyword tells the compiler to allocate these subfields in a single 16-bit vector. subfield1 occupies the 8 most significant bits, and subfield2 occupies the 8 least significant bits.

You can use arithmetic and logical operators with packed structures just as you would with individual variables. For example, you can add two packed structures together:

struct packed {
  logic [3:0] subfield1;
  logic [7:4] subfield2;
} myPackedStruct1, myPackedStruct2, myPackedStructSum;

myPackedStruct1.subfield1 = 4'b0011;
myPackedStruct1.subfield2 = 4'b1010;

myPackedStruct2.subfield1 = 4'b0101;
myPackedStruct2.subfield2 = 4'b0111;

myPackedStructSum = myPackedStruct1 + myPackedStruct2;

It's worth noting that you can also declare a packed structure as struct packed signed, which allows you to use the structure as a signed vector. Here's an example:

struct packed signed {
  logic [3:0] subfield1;
  logic [7:4] subfield2;
} mySignedPackedStruct;

In this example, mySignedPackedStruct is declared as a struct packed signed with two subfields. This allows you to treat the entire packed structure as a signed vector, which can be useful in certain situations.

Assigning to Structures

You can assign values to structures just like you would assign values to individual variables. Here is an example:

myStruct.data1  = 8'b01100100;
myStruct.data2  = 16'hA1B2;
myStruct.enable = 1'b1;

In this example, we assign the value 01100100 to data1, the value A1B2 to data2, and the value 1 to enable.

You can also assign values to structures using the curly brace syntax. Here is an example:

myPackedStruct = '{8'b01100100, 16'hA1B2, 1'b1};

In this example, we assign the same values to myPackedStruct using the curly brace syntax.


In conclusion, SystemVerilog structures provide an efficient way to organize and store complex data types. You can use either unpacked or packed structures, depending on your needs. Assigning values to structures is straightforward and can be done using either the dot syntax or the curly brace syntax. We hope this article has helped you understand SystemVerilog structures and how to use them in your circuit designs. Happy sailing!