R9.4.1 Decode Three Formats

MediumImplementVerilator + Yosys · serverReview the topic

Lay three packed structs over one instruction word. Then build the scrambled B, U and J immediates of RV32I from their named pieces.

Hardware context: An RV32I immediate decoder that reads one word through a packed union of B, U and J views; and a table of four descriptor records. Finish the three packed structs and decode the B, U and J immediates.

A branch predictor needs the target offset of every branch and jump before the rest of the decoder has run. RV32I stores those offsets in pieces, out of order, so that other fields stay on the same bits in every format. A decoder written with bare indexes for these pieces was wrong twice in review.

This is an Implement problem in two parts. First finish the package: three packed structs, one per format, whose members land on the bits the format tables give. The starter holds every member with its width, in the wrong order. Then write imm_dec, which reads the word through the union and builds the immediate from the named pieces.

The tests check both parts. They load each word into a variable of your instr_t and read the pieces by name. They also compare kind and imm with a model.

Brief: input ins (32 bits, typed instr_t); outputs kind[1:0] and imm[31:0]; combinational. B and J immediates are sign-extended and their bit 0 is 0. A U immediate fills bits 31:12 and bits 11:0 are 0. Any other opcode gives kind 0 and imm 0. Synthesis check: the design must build on the site's synthesis flow with no latch. The cell and level counts come from an open-source flow and are a proxy for area and depth, not a foundry result.

Signals in the Timeline

SignalWidthRole
word32input of each check
Full specification
Format3130:2524:212019:1514:1211:876:0
Bimm[12]imm[10:5]rs2 (24:20)rs1funct3imm[4:1]imm[11]opcode
Uimm[31:12] (31:12)rd (11:7)opcode
Jimm[20]imm[10:1] (30:21)imm[11]imm[19:12] (19:12)rd (11:7)opcode
MemberViewWord bitsHolds
imm12, imm10_5, imm4_1, imm11b31, 30:25, 11:8, 7bits 12, 10:5, 4:1 and 11 of the branch offset
rs2, rs1, funct3b24:20, 19:15, 14:12as in every format
imm31_12u31:12the upper 20 bits of the result
imm20, imm10_1, imm11, imm19_12j31, 30:21, 20, 19:12bits 20, 10:1, 11 and 19:12 of the jump offset
rdu, j11:7the destination register
opcodeb, u, j6:0the format
opcodekindimm
1100011 (branch)1the 13-bit offset {imm[12:1], 0}, sign-extended to 32 bits
0110111 (lui), 0010111 (auipc)2{imm[31:12], 12 zero bits}
1101111 (jal)3the 21-bit offset {imm[20:1], 0}, sign-extended to 32 bits
anything else00
The union and the module header (keep them as they are)
  typedef union packed {
    logic [31:0] raw;
    b_type_t     b;
    u_type_t     u;
    j_type_t     j;
  } instr_t;
endpackage

module imm_dec
  import imm_pkg::*;
(
  input  instr_t      ins,
  output logic [1:0]  kind,        // 1 = B, 2 = U, 3 = J, 0 = anything else
  output logic [31:0] imm
);
  // your decode
endmodule

Result fields: imm and kind come from your decoder. b_view is {imm12, imm11, imm10_5, imm4_1}, read from your struct by name. j_view is {imm20, imm19_12, imm11, imm10_1}, read the same way. u_view is {rd, opcode} of the U view.

Examples

Example 1

A Branch Forward
Input
beq x1, x2, +16: word = 0x00208863
Output
imm = 16, kind = B (1), b_view = 0x8, j_view = 0x4001, u_view = 0x863

The offset 16 has only bit 4 set. That bit sits in imm4_1, word bits 11:8, as the value 8.

Example 2

A Branch Backward
Input
bne x5, x6, -8: word = 0xfe629ce3
Output
imm = -8, kind = B (1), b_view = 0xffc, j_view = 0x94bf3, u_view = 0xce3

The offset -8 sets imm12, imm11 and all of imm10_5, and imm4_1 is 1100. Sign extension fills bits 31:13 of imm.

Example 3

A Long Jump Backward
Input
jal x1, -4096: word = 0x800ff0ef
Output
imm = -4096, kind = J (3), b_view = 0xc00, j_view = 0xff800, u_view = 0xef

The offset -4096 sets imm20 and every bit of imm19_12. imm11 and imm10_1 are 0.

Constraints and Contract

  • Keep the package name, the type names, the member names and widths, and the module ports.
  • The word holds known 0/1 values. Each case applies one word and reads the outputs 1 ns later.

Hints

Hint 1

Do the structs first. A packed struct lists its members from bit 31 down, so read each row of the format table from left to right.

Hint 2

Check a struct by its widths. The members of each view must add up to 32 bits, or the union will not compile.

Hint 3

Build each offset by joining the named pieces in offset order, top bit first. Put a 0 at the bottom. Then sign-extend: a cast to signed and then to 32 bits does it.

What the grader checks: Sixteen directed and fourteen seeded instruction words are applied one at a time. The decoder's kind and imm are compared with an independent model, and the B and J immediate pieces and the rd and opcode of the U view are read from your structs by name. Passing shows the three views and the decoder agree with the format tables on these words. The synthesis check adds the latch count.

The submitted design must synthesize with 0 latches and no combinational loop. Counts come from the pinned open-source synthesis flow. They are proxies for logic size and depth, not physical area or timing.

30 checks on Verilator + Yosys · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

A Branch Forward

Input
beq x1, x2, +16: word = 0x00208863
Expected output
imm = 16, kind = B (1), b_view = 0x8, j_view = 0x4001, u_view = 0x863

The offset 16 has only bit 4 set. That bit sits in imm4_1, word bits 11:8, as the value 8.

Run samples executes all 3 public checks. Check all tests runs 30 checks on the server.

Verilator + Yosys · server