SystemVerilog Struct

SystemVerilog Struct

The SystemVerilog struct groups the data types of multiple types. The entire group can be referenced as a whole, or the individual data type can be referenced by name.

Struct Syntax

Struct is defined with the Struct keyword followed by variables of multiple data type with in the curly braces.

  typedef struct packed {
    type_1 var_1;
    type_2 var_2;
    type_3 var_3; 
  } struct_name; 

SystemVerilog Packed Struct

Struct defined with the keyword Packed is referred as packed struct
Only packed data types and integer data types are allowed with in the packed struct

Packed Struct example

In the below example, variable of bit type are grouped in the struct.

module struct_tb;
  
  typedef struct packed {
    bit [7:0]  addr;
    bit        valid;
    bit [31:0] data; 
  } mem_pkt; 
  
  mem_pkt pkt;
  
  initial begin 
    
    // Initializing Struct
    pkt = '{8'h6, 1'b1, 32'hC001_0FAB};
    $display ("pkt = %p", pkt);
    
    // Change the struct field value
    pkt.addr = 8'h8;
    $display ("pkt = %p", pkt);
   
    // Change the struct field value
    pkt.data = 32'hFFF0_0FFF;
    $display ("pkt = %p", pkt);
  end  
endmodule 

Simulator Output

pkt = '{addr:'h6, valid:'h1, data:'hc0010fab}
pkt = '{addr:'h8, valid:'h1, data:'hc0010fab}
pkt = '{addr:'h8, valid:'h1, data:'hfff00fff}

Click to execute on   

SystemVerilog UnPacked Struct

By default struct is of Unpacked type, i.e struct without Packed keyword.

Packed Struct example

In the below example, byte, enum and bit types are grouped in the struct.

module struct_tb;
  
  typedef enum logic {INVALID_PKT,VALID_PKT} pkt_type;
  
  typedef struct packed {
    byte       addr;
    pkt_type   valid;
    bit [31:0] data; 
  } mem_pkt; 
  
  mem_pkt pkt;
  
  initial begin 
    
    // Initializing Struct
    pkt = '{8'h6, VALID_PKT, 32'hC001_0FAB};
    $display ("pkt = %p", pkt);
    
    // Change the struct field value
    pkt.addr = 8'h8;
    $display ("pkt = %p", pkt);
   
    // Change the struct field value
    pkt.valid = INVALID_PKT;
    $display ("pkt = %p", pkt);
  end  
endmodule 

Simulator Output

pkt = '{addr:'h6, valid:VALID_PKT, data:'hc0010fab}
pkt = '{addr:'h8, valid:VALID_PKT, data:'hc0010fab}
pkt = '{addr:'h8, valid:INVALID_PKT, data:'hc0010fab}

Click to execute on