Next: , Previous: , Up: Structs   [Contents][Index]


25.6.14 Integral Unions

Integral unions are useful to cover cases where we have variants of data encoded in integers. One example could be CPU instructions encoded as 16-bit or 32-bit numbers.

type InstructionFormat_1 =
  struct uint<32>
  {
    uint<20> imm31_12;
    uint<5>  rd;
    uint<7>  opcode : valid_opcode_for_format_1 (opcode);
  };

type InstructionFormat_2 =
  struct uint<32>
  {
    uint<7> funct7;
    uint<5> rs2;
    uint<5> rs1;
    uint<3> funct3;
    uint<5>  rd;
    uint<7>  opcode : valid_opcode_for_format_2 (opcode);
  };

type Instruction =
  union uint<32>
  {
    InstructionFormat_1 f1;
    InstructionFomrat_2 f2;
  };

Like integral structs, integral unions can be casted to/from integers; e.g., 3211411U as Instruction will create an Instruction union.

Like integral structs, integral unions can have fields of the following types: integral, offset, integral struct or union.