all about vlsi DV
Structures & Unions in SystemVerilog — Grouping Data the Right Way
1. Introduction
Up to now, Alice has been sending Bob:
-
Single values (logic, bit)
-
Multiple values (arrays, queues)
But real designs don’t work with loose values.
A real operation usually looks like:
-
address
-
data
-
control
-
status
Beginners usually ask:
How do I send all of this together without losing meaning?
This is exactly why structures and unions exist.
2. What Are Structures and Unions in System Verilog?
(Featured Snippet Target)
In SystemVerilog, a struct groups related fields into a single data object, while a union allows multiple fields to share the same memory, with only one field valid at a time.
In short:
-
struct → many related things together
-
union → different views of the same thing
Now let’s make this intuitive.
3. Alice & Bob Setup (Keep This Picture)
-
Alice produces an operation
-
Bob checks the operation
-
Both agree on what the operation contains
Without agreement, bugs happen.
Structures and unions exist to force agreement.
4. Structure (struct) — Alice Sends ONE Meaningful Object
Mental Picture (Memorize This)
Alice wants to send Bob a parcel.
Inside the parcel:
-
Address
-
Data
-
Valid bit
She does not want to send these separately.
She packs them into one box and labels each item.
That box is a struct.
Runnable Example — Basic struct
module struct_example;
typedef struct {
logic [7:0] addr;
logic [7:0] data;
logic valid;
} packet_t;
packet_t pkt;
initial begin
// Alice prepares packet
pkt.addr = 8'h12;
pkt.data = 8'hA5;
pkt.valid = 1'b1;
// Bob checks packet
$display("addr=%h data=%h valid=%b",
pkt.addr, pkt.data, pkt.valid);
end
endmodule
Why This Matters
-
Fields belong together
-
Impossible to forget one field accidentally
-
Code becomes self-documenting
👉 Struct = one real-world object.
5. Why struct Is Better Than Separate Signals
Without struct
logic [7:0] addr;
logic [7:0] data;
logic valid;
Nothing enforces:
-
These belong together
-
They are used consistently
With struct
packet_t pkt;
Now:
-
One name
-
One meaning
-
One unit
This removes entire classes of bugs.
6. Array of Structures — Alice Sends MANY Packets
Mental Picture
Alice now sends:
-
Packet 0
-
Packet 1
-
Packet 2
Each packet has the same format.
So Alice keeps a row of parcels.
Runnable Example — Array of Structs
module struct_array_example;
typedef struct {
logic [7:0] addr;
logic [7:0] data;
logic valid;
} packet_t;
packet_t fifo [0:2]; // three packets
initial begin
fifo[0] = '{8'h10, 8'hAA, 1'b1};
fifo[1] = '{8'h20, 8'hBB, 1'b1};
fifo[2] = '{8'h30, 8'hCC, 1'b0};
// Bob checks packet 1
$display("Packet1 addr=%h data=%h valid=%b",
fifo[1].addr, fifo[1].data, fifo[1].valid);
end
endmodule
👉 This is transaction-level modeling.
7. Union (union) — Alice Sends ONE Thing, Bob Sees It Differently
Mental Picture (Very Important)
Alice sends Bob one value.
Bob might interpret it as:
-
A number
-
Or bytes
-
Or individual bits
But only one interpretation is valid at a time.
That is a union.
8. union Example — Same Data, Different Views
module union_example;
typedef union {
logic [7:0] whole;
logic [3:0] low_nibble;
} data_u;
data_u u;
initial begin
u.whole = 8'hAB;
// Bob looks at same data differently
$display("whole=%h", u.whole);
$display("low nibble=%h", u.low_nibble);
end
endmodule
Key Rule
-
Memory is shared
-
Writing one field overwrites others
-
Only one meaning at a time
👉 Union = different views of same storage.
9. struct vs union (One Clear Comparison)
Feature | struct | union
Fields All fields valid together Only one field valid at a time
Memory Separate memory per field Shared memory
Use case Group related data Multiple interpretations
Common usage Very common Less common
10. Common Beginner Mistakes
-
Using union when struct is needed
-
Assuming all union fields are valid at once
-
Forgetting field names inside structs
Sending loose signals instead of one object
11. Interview Perspective (What They Look For)
Interviewers often ask:
-
“Why use struct?”
-
“Difference between struct and union?”
-
“Give a real use case”
Strong One-Line Answers
-
Struct groups related signals
-
Union shares memory between fields
-
Struct models transactions
Clear and confident = pass.
12. One Sentence to Remember (Interview Gold)
Struct groups meaning, union shares storage.
If you remember this, you won’t confuse them.
13. Knowledge Check
-
Why is struct safer than loose signals?
-
When is union appropriate?
Can all fields of a union be used at once?
14. What’s Next
Now Alice can send complete objects.
Next, she wants to connect components cleanly without messy wiring.
👉 Next Article:
Interfaces in System Verilog — Why They Matter