Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 37 ยท SystemVerilog Features

SystemVerilog Data Types: logic, enum, struct and typedef

Learn the SystemVerilog data types that replace reg and wire: logic, bit, int, typedef, enum, packed struct, unions and arrays, with C stand-ins for packing.

Verilog-2001 has two confusing storage types: reg (which is not always a register) and wire. SystemVerilog (IEEE 1800, a superset of Verilog) fixes that and adds the type features software engineers expect: user-defined types, enumerations, structures and richer arrays. Learning them turns 200-line port lists and magic numbers into readable code.

This lesson covers logic, two-state types, typedef, enum, struct (packed and unpacked), union and arrays. SystemVerilog needs a simulator or a synthesis tool that supports it (Verilator, Questa, VCS, Vivado, Quartus Pro and others), none of which run on this site: the SV code is read-only and the C programs are labelled stand-ins for the bit-level ideas.

logic: one type for variables

logic is a 4-state type (0, 1, x, z) usable almost anywhere a reg or a wire was:

SYSTEMVERILOG
logic        valid;
logic [7:0]  data;          // 8-bit vector

assign valid = (data != 8'h00);     // driven by a continuous assignment
always_comb  data = a & b;          // or by a procedural block, but not both!

A logic variable may have one driver only: a continuous assignment, a single procedural block, or an output of one instance. Two drivers is a compile error, which catches mistakes that old Verilog hides. (If you truly need several drivers, as in a tri-state bus, use wire.)

Rule of thumb: use logic for everything inside a design, and wire only for multi-driver nets.

Two-state types

For testbenches and models, where you do not want x and z at all, SV has 2-state types:

Type Bits Signed? Notes
bit 1 (vectors allowed) unsigned default value 0
byte 8 signed
shortint 16 signed
int 32 signed the loop-counter type
longint 64 signed
integer 32 signed 4-state, legacy Verilog

Two-state variables start at 0 (not x), simulate faster, and hide reset bugs, so do not use them for design signals you want to see go x when uninitialised. Qualifiers signed and unsigned change the interpretation: logic signed [7:0] temperature;.

typedef: naming your types

SYSTEMVERILOG
typedef logic [7:0]  byte_t;
typedef logic [31:0] word_t;

byte_t  a, b;
word_t  bus;

A typedef makes widths a single change and gives intent a name. Conventionally, type names end with _t.

enum: named values

SYSTEMVERILOG
typedef enum logic [1:0] {
  IDLE,       // 0
  RUN,        // 1
  DONE        // 2
} state_t;

state_t state, next;

Enumerations replace the localparam lists in FSMs. Benefits: waveform viewers show RUN instead of 2'd1; the compiler rejects assigning an unrelated number without a cast; and you get helper methods:

  • state.next(), state.prev(): the following or previous value,
  • state.name(): the name as a string (great for $display),
  • state.num(): how many values.

You can set values (RED = 3'b001), which is how you build one-hot encodings: typedef enum logic [2:0] {A = 3'b001, B = 3'b010, C = 3'b100} onehot_t;. See FSM coding styles for why you would.

The first stand-in models an enum with a name table and next() in C, with the wrap-around SystemVerilog gives you:

C Stand-in: an enum with name(), next() and a case lookup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

struct: grouping signals

A packed struct is stored as one contiguous vector, first member in the most significant bits. It behaves like a vector (you can assign it, compare it, slice it, send it through a port), and you can also access members by name:

SYSTEMVERILOG
typedef struct packed {
  logic [3:0] opcode;
  logic [3:0] dest;
  logic [7:0] imm;
} instr_t;                   // 16 bits wide

instr_t instr;
assign instr = 16'hA3_5C;
// instr.opcode == 4'hA, instr.dest == 4'h3, instr.imm == 8'h5C

An unpacked struct (without packed) is like a C struct: members are stored separately, it can hold non-vector members (strings, reals), and the bit layout is not defined. Use packed for hardware signals and unpacked for testbench data.

Because a packed struct is just bits, you can pass it through a single port instead of ten, and change its fields in one place. The stand-in below does the same packing with shifts and masks in C, which is essentially what the hardware wiring does:

C Stand-in: a 16-bit packed struct built from shifts and masks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The printed word is 0xA35C, and writing the field dest changes only bits 11 to 8. In SystemVerilog instr.dest = 4'h7; does the mask and shift for you.

union

A packed union gives several views of the same bits, all of the same width:

SYSTEMVERILOG
typedef union packed {
  logic [31:0] word;
  logic [3:0][7:0] bytes;     // four bytes
  struct packed { logic [15:0] hi, lo; } halves;
} reg32_t;

Writing r.word and then reading r.bytes[0] is legal and well defined for packed unions: the same storage viewed differently. Useful for registers and protocol headers.

Arrays

SV extends arrays in three useful ways:

SYSTEMVERILOG
logic [7:0] mem [256];         // unpacked: shorthand for [0:255]
logic [3:0][7:0] word;         // packed 2D: four bytes in one vector
int queue[$];                  // queue (testbench)
int table[string];             // associative array (testbench)

Packed dimensions go before the name and are bit-vectors; unpacked dimensions go after the name and are collections. Dynamic arrays, queues (push_back, pop_front, size()) and associative arrays are testbench-only features that do not synthesize. They shine in scoreboards, where you store expected values.

You can also initialise whole arrays at once with assignment patterns: logic [7:0] mem [4] = '{8'h1, 8'h2, 8'h3, 8'h4}; and '0 or '1 fill everything with zeros or ones regardless of width.

Handy operators

  • ==? and !=? use x/z in the right operand as wildcards; inside checks membership: if (opcode inside {4'h1, 4'h2, [4'h8:4'hB]}).
  • Cast with type'(value), e.g. state_t'(3), and with signed'(x).
  • $bits(type_or_expr) returns the width in bits: $bits(instr_t) is 16.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main advantage of logic over reg and wire?

    logic can be driven by one continuous assignment or one procedural block. Several drivers become a compile error.
  2. 2. In typedef struct packed { logic [3:0] a; logic [3:0] b; } t; which bits does member a occupy?

    The first member of a packed struct occupies the most significant bits.
  3. 3. Which type is two-state?

    int is a 2-state 32-bit signed type. integer, logic and reg can hold x and z.
  4. 4. What does $bits(instr_t) return for the 16-bit packed struct above?

    $bits returns the total width in bits of a type or expression.

Summary

  • Use logic for design signals, wire only for multi-driver nets; two-state types belong in testbenches.
  • typedef names types; enum replaces localparam lists and gives name(), next() and type safety.
  • Packed structs and unions are bit-vectors with named fields; unpacked structs are like C structs.
  • Packed dimensions come before the name, unpacked after; queues and associative arrays are for testbenches.

Next: always_comb, always_ff and Packages