BitFlow

RISC-V Step 3: Immediate Generator

advanced · riscv · 240 XP

Sign in to write your solution, run it in the simulator and submit it for grading.

Sign in or create a free account →

Problem

RISC-V Step 3: Immediate Generator Many instructions carry a constant (an **immediate**) inside the 32-bit instruction word. RISC-V scatters its bits differently per instruction format so that the register fields always stay in the same place. Your job: rebuild the full **sign-extended 32-bit immediate** for every format. **Interface:** `input [31:0] instr`, `output [31:0] imm`. The format is chosen by the opcode, `instr[6:0]`: | Format | Opcodes | `imm` | |--------|---------|-------| | I | 0010011 (OP-IMM), 0000011 (LOAD), 1100111 (JALR) | `instr[31:20]`, sign-extended | | S | 0100011 (STORE) | `{instr[31:25], instr[11:7]}`, sign-extended | | B | 1100011 (BRANCH) | `{instr[31], instr[7], instr[30:25], instr[11:8], 0}`, sign-extended (13 bits) | | U | 0110111 (LUI), 0010111 (AUIPC) | `{instr[31:12], 12'b0}` | | J | 1101111 (JAL) | `{instr[31], instr[19:12], instr[20], instr[30:21], 0}`, sign-extended (21 bits) | "Sign-extended" means the top bit of the immediate (always `instr[31]`) is copied into all the higher bits. B and J immediates are byte offsets that are always even, so their lowest bit is a constant 0. Other opcodes are never tested.

Constraints

  • Purely combinational.
  • The sign bit of every immediate is instr[31].
  • B and J immediates have bit 0 = 0.

Examples

Input: addi x1, x2, -5 (0xFFB10093)

Output: imm = 0xFFFFFFFB

I-type, negative

Input: sw x3, 40(x4) (0x02322423)

Output: imm = 40

S-type: the offset is split in two fields

Input: beq x1, x2, -16

Output: imm = 0xFFFFFFF0

B-type branch offset

Input: lui x5, 0xABCDE

Output: imm = 0xABCDE000

U-type: upper 20 bits

Input: jal x1, 2048

Output: imm = 2048

J-type: bit 11 lives in instr[20]

Common questions

What does the RISC-V Step 3: Immediate Generator problem ask you to build?
RISC-V Step 3: Immediate Generator Many instructions carry a constant (an **immediate**) inside the 32-bit instruction word. RISC-V scatters its bits differently per instruction format so that the register fields always stay in the same place.
What constraints does RISC-V Step 3: Immediate Generator have?
Purely combinational.; The sign bit of every immediate is instr[31].; B and J immediates have bit 0 = 0.
What is an example input and output for RISC-V Step 3: Immediate Generator?
Input addi x1, x2, -5 (0xFFB10093) gives output imm = 0xFFFFFFFB. I-type, negative
Which Verilog concepts does RISC-V Step 3: Immediate Generator practise?
riscv, risc-v, decoder, combinational, cpu.