RTL Studio Open IDE

1.3 · SYSTEMVERILOG CLASS

struct (Structures)

Define packed structs to group control and payload fields into a single packet type.

Watch on YouTube

What you practice

struct (Structures) (1.3). Define packed structs to group control and payload fields into a single packet type. It is in Aggregate Data Types — Part 1 · Design in SV of the SystemVerilog Masterclass. Synthesizable SystemVerilog that removes Verilog ambiguity and cuts boilerplate.

The same IDE is the SystemVerilog simulator. Slang lints the file, Verilator runs it, and the waveform viewer sits next to the source.

What you will learn

Today you will learn how to group related signals into a single struct packed type in SystemVerilog to simplify port declarations and improve code readability.

Learning objectives

  • Define a typedef struct packed with named fields.
  • Declare struct-typed ports and assign struct variables.
  • Use struct-typed signals to organize control and payload data.
  • Reduce port count by bundling related signals into one structured type.

Theory

A struct packed in SystemVerilog groups multiple signals into a single contiguous bit vector, preserving bit positions for hardware synthesis. It is ideal for protocol packets where control and payload bits must be accessed by name.

Syntax ElementPurpose
typedef struct packed { ... } type_name;Define a new packed struct type
logic [msb:lsb] field_name;Declare a field with fixed bit width
packet.ctrl, packet.payloadAccess struct fields by name
struct_type variable;Declare a variable of the struct type

Packed structs map 1:1 to hardware wires and synthesize to flat buses, making them safe for synthesis and simulation.

Practice

Open this chapter in RtlStudio; files appear in Explore. Edit only student/rtl/design.sv to complete the struct definition. Run Simulation and verify the Log output matches expected values.

  1. In student/rtl/design.sv, replace each __TODO__ placeholder with the correct bit range.
  2. Ensure the typedef struct packed declares ctrl as 2 bits and payload as 8 bits.
  3. Declare packet_in and packet_out as logic [9:0] and cast them to/from the struct type.
  4. Keep the rest of the module unchanged; the testbench already provides stimulus.
  5. Run Simulation → Run → check the Log panel for INFO: lines.
  6. Confirm INFO: PASS appears after all test cases.

Summary

You practiced defining a packed structure type with named control and payload fields, then used it to simplify port declarations in the DUT and testbench. By assigning full struct variables instead of individual bit slices, you reduced port complexity and improved code readability. The simulation confirmed correct field-level behavior with expected control and payload values propagating through the design. This exercise reinforced how packed structures enable clean abstraction of protocol packets for synthesis and verification.

Run it in the Verilog IDE

  1. Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
  2. Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
  3. Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
  4. Run Yosys when the lesson is synthesizable RTL, and keep testbench-only constructs out of that module.

The lesson is done when the simulation matches struct (Structures). Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.