RTL Studio Open IDE

1.4 · RISC-V CLASS

Immediate generator for all five formats

Implement immediate extraction and sign-extension for RISC-V I, S, B, U, and J formats in rv32i_imm.sv.

Video coming soon. This lesson page stays at the same URL when the lecture publishes.

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.

FormatBits ExtractedSign-Extended?PaddingNotes
Iinst[31:20]YesNoneUsed for ADDI, LOAD
Sinst[31:25], inst[11:7]YesNoneUsed for STORE
Binst[31], inst[7], inst[30:25], inst[11:8]Yes1'b0Used for BEQ
Uinst[31:12]No12'b0Used for LUI, AUIPC
Jinst[31], inst[19:12], inst[20], inst[30:21]Yes1'b0Used for JAL

Practice

  1. Open RtlStudio and select this chapter from the Examples or Lecture list.
  2. Navigate to Explore and open student/rtl/ip/riscv/rv32i_imm.sv.
  3. Replace the __TODO__ placeholders with the correct concatenation expressions for imm_i, imm_s, imm_b, and imm_u.
  4. Ensure you replace the entire __TODO__ token with the signal expression for each port.
  5. Open student/tb/tb_top.sv to review the testbench stimulus.
  6. Click Simulation in the left sidebar and run the simulation.
  7. Check the Log panel for INFO: messages and INFO: 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

  1. Open the RV32I lesson in the IDE. Fetch, decode, and execute are source you can simulate.
  2. Run Verilator and watch PC, the instruction, and the register write on the waveform.
  3. Treat a program printout as done only after the ISA step this lesson names matches the wave.
  4. 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.