S2.2.2 Build the Immediate Generator

MediumImplement35 minIcarus Verilog · serverReview the topic

Write the block that takes an instruction word and gives its immediate, 32 bits wide, for all five formats that have one.

Hardware context: Real RV32I instruction words from the assembler, cut at the six fixed field positions by test code; one 12-bit field widened by zero extension and by sign extension. Reading instruction words in a trace or a memory dump, and specifying an instruction decoder

Every RISC-V processor has a block that reads the instruction word and builds its immediate. The rest of the datapath then never looks at the format again.

Write that block. It has no clock. The bench feeds it real instruction words from an assembler.

Signals in the Timeline

SignalWidthRole
trial10input of each check
Full specification
The format of each opcode
Opcode, bits 6 to 0InstructionsFormat
0010011, 0000011, 1100111arithmetic with an immediate, loads, jalrI
0100011storesS
1100011branchesB
0110111, 0010111lui, auipcU
1101111jalJ
every other opcodefor example 0110011, the R formatnone: the output is 0
What the output is
The lesson gives the place of every bit. For a shift such as slli, give the plain I immediate.
FormatThe 32-bit output
I, Sthe immediate of 12 bits, sign-extended
Bthe offset of 13 bits, sign-extended. Its bit 0 is 0
Uthe 20 bits in the upper part, and 0 in bits 11 to 0
Jthe offset of 21 bits, sign-extended. Its bit 0 is 0

How to read a result: a public case shows the output in hex. A private case feeds many words of one kind, and all_match is 1 when every output was right.

Examples

Example 1

An I Word
Input
the word fff28293: addi x5, x5, -1
Output
imm = 0xffffffff

An I word. Bits 31 to 20 are twelve ones, and the sign fills the upper 20 bits.

Example 2

An S Word
Input
the word 00752423: sw x7, 8(x10)
Output
imm = 0x8

An S word. The immediate is bits 31 to 25, then bits 11 to 7.

Example 3

A B Word
Input
the word fe029ce3: bne x5, x0, 0x8, at address 10 in hex
Output
imm = 0xfffffff8

A B word. The offset is -8: its bit 0 is 0 and is not in the word.

Constraints and Contract

  • The output depends only on the word of the same moment. The bench waits 1 ns after it sets a word.
  • The format is chosen by bits 6 to 0 alone. Do not look at funct3 or funct7.
  • For an opcode that is not in the first table, the output is 0.

Hints

Hint 1

Start with a case statement on the opcode. Give each format one line.

Hint 2

For B and J, write the offset bits in order from the sign down, and say for each where it comes from.

Hint 3

Count the bits of each line. Every line must be 32 bits wide.

What the grader checks: For each format: the immediates 0, 1 and -1, the largest and the smallest. Then every single bit set alone, positive and negative, with random register numbers. Every funct3 of the base instructions, the three shifts, the R format, ten words with other opcodes, and 120 random words.

14 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 3 problems on this path

Report a problem with this page

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

An I Word

Input
the word fff28293: addi x5, x5, -1
Expected output
imm = 0xffffffff

An I word. Bits 31 to 20 are twelve ones, and the sign fills the upper 20 bits.

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

Icarus Verilog · server