Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

SystemVerilog Union

SystemVerilog Data Types

SystemVerilog Union — a ready-to-run SystemVerilog example.

Open in IDE
Related tutorial:
testbench.v
typedef union packed {
  bit [31:0] raw;

  struct packed {
    bit [15:0] high;   // declared first - occupies the upper (most significant) half
    bit [15:0] low;    // declared second - occupies the lower (least significant) half
  } halves;
} data_t;

module tb;
  initial begin
    data_t d;

    d.raw = 32'hDEAD_BEEF;

    $display ("raw  = 0x%0h", d.raw);
    $display ("high = 0x%0h", d.halves.high);
    $display ("low  = 0x%0h", d.halves.low);
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.