What you practice
Immediate generator for all five formats (1.4). Implement immediate extraction and sign-extension for RISC-V I, S, B, U, and J formats in rv32i_imm.sv. It is in Immediate I/S/B/U/J — Part 1 · Fetch & Decode of the RISC-V RV32I Core Masterclass.
The RV32I core is Verilog you simulate in the browser IDE. Verilator runs the program; the waveform shows PC, the instruction, and the register write.
What you will learn
This lesson teaches you how to extract and sign-extend immediates from RISC-V instructions. You will implement the logic for I, S, B, U, and J formats in a single module.
Learning objectives
- Understand the bit-field extraction rules for each RISC-V immediate format.
- Implement sign-extension using concatenation for signed immediates.
- Construct the U and J immediate formats with zero-padding.
- Verify the implementation using a testbench with ADDI and BEQ examples.
Theory
RISC-V instructions encode immediate values in various bit fields within the 32-bit instruction word. The processor must extract these bits and often sign-extend them to 32 bits for arithmetic operations. Sign-extension replicates the most significant bit (the sign bit) into the higher bits of the result. For example, an 8-bit signed value must be extended to 32 bits by copying bit 7 into bits 8 through 31. The U format places the immediate in the upper 20 bits and pads the lower 12 bits with zeros. The J format is a complex 20-bit immediate that requires specific bit reordering before sign-extension and zero-padding. The B format is an 11-bit signed immediate that also requires bit reordering and zero-padding at the least significant bit position.
| Format | Bits Extracted | Sign-Extended? | Padding | Notes |
|---|---|---|---|---|
| I | inst[31:20] | Yes | None | Used for ADDI, LOAD |
| S | inst[31:25], inst[11:7] | Yes | None | Used for STORE |
| B | inst[31], inst[7], inst[30:25], inst[11:8] | Yes | 1'b0 | Used for BEQ |
| U | inst[31:12] | No | 12'b0 | Used for LUI, AUIPC |
| J | inst[31], inst[19:12], inst[20], inst[30:21] | Yes | 1'b0 | Used for JAL |
Practice
- Open RtlStudio and select this chapter from the Examples or Lecture list.
- Navigate to Explore and open
student/rtl/ip/riscv/rv32i_imm.sv. - Replace the
__TODO__placeholders with the correct concatenation expressions forimm_i,imm_s,imm_b, andimm_u. - Ensure you replace the entire
__TODO__token with the signal expression for each port. - Open
student/tb/tb_top.svto review the testbench stimulus. - Click Simulation in the left sidebar and run the simulation.
- Check the Log panel for
INFO:messages andINFO: PASS.
Summary
You implemented the immediate generation logic for all five RISC-V instruction formats in the rv32i_imm module. This involved correctly extracting bit fields and applying sign-extension or zero-extension to produce 32-bit values. The exercise reinforced how different formats like I, S, B, U, and J encode immediate data within the instruction word. Successfully passing the simulation confirms your understanding of these critical fetch and decode operations.
© 2026 RtlStudio. Licensed under CC BY-NC-ND 4.0. Commercial use and modifications by third parties are not permitted.
Run it in the Verilog IDE
- Open the RV32I lesson in the IDE. Fetch, decode, and execute are source you can simulate.
- Run Verilator and watch PC, the instruction, and the register write on the waveform.
- Treat a program printout as done only after the ISA step this lesson names matches the wave.
- Change the RTL in the editor, rerun Verilator, and read the waveform again in the same tab.
The lesson is done when the simulation matches Immediate generator for all five formats. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.