What you practice
Forwarding unit (4.5). Implement the forwarding unit logic to select EX operands from MEM or WB stages, handling priority and x0 exceptions. It is in Forwarding unit — Part 4 · Pipeline of the RISC-V on SoC & System Masterclass.
The core and the mini-SoC simulate as one workspace in the IDE. Follow MMIO, interrupts, and the program on the Verilator waveform, not only a pass line.
What you will learn
Today you will implement the forwarding unit logic for the RV32I pipeline. This unit selects operands from the MEM or WB stage instead of the register file. Forwarding prevents pipeline stalls when data dependencies exist between adjacent instructions.
Learning objectives
- Understand how to detect data hazards between EX, MEM, and WB stages.
- Implement logic to select the correct source for
fwd_aandfwd_b. - Handle priority rules where MEM stage forwarding takes precedence over WB stage forwarding.
- Ensure that register
x0is never forwarded.
Theory
Forwarding bypasses the register file by routing data directly from pipeline registers to the ALU inputs. This technique reduces the number of cycles required for dependent instructions. The forwarding unit must compare destination registers with source registers to identify hazards.
The forwarding unit outputs two 2-bit signals: fwd_a and fwd_b. These signals select the source for each ALU operand.
| Value | Source |
|---|---|
| 2'd0 | Register File |
| 2'd1 | MEM Stage Result |
| 2'd2 | WB Stage Result |
The logic follows a strict priority order to resolve conflicts. The MEM stage result has higher priority than the WB stage result. This ensures that the most recent data is used. The register x0 is hardcoded to zero and must never be forwarded. If a hazard involves x0, the forwarding unit should default to the register file value.
Practice
- Open RtlStudio and select this chapter from the Examples list.
- Confirm that
student/rtl/ip/riscv/rv32i_forward_unit.svandstudent/tb/tb_top.svappear in the Explore tree. - Open
student/rtl/ip/riscv/rv32i_forward_unit.svin the editor. - Replace the
__TODO__placeholders with the correct logic to select forwarding sources. - Ensure
fwd_aandfwd_bdefault to2'd0(register file) at the start of eachalways_combblock. - Implement the condition for MEM stage forwarding: check
mem_we, ensuremem_rdis notx0, and matchmem_rdwith the source register. - Implement the condition for WB stage forwarding: check
wb_we, ensurewb_rdis notx0, and matchwb_rdwith the source register. - Save the file and run Simulation from the sidebar.
Summary
You implemented the forwarding unit logic to resolve data hazards by selecting operands from the MEM or WB pipeline stages. The design prioritizes MEM stage results over WB stage results to ensure correct timing and data validity. You also ensured that register x0 is never forwarded to prevent incorrect zero propagation.
© 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 system lesson in the IDE. The RV32I core and the mini-SoC fabric simulate together.
- Run Verilator and trace the program through MMIO: address, load/store data, and the peripheral response.
- Interrupts and DMA belong on the waveform next to the instruction stream, not only in a pass/fail line.
- Edit the integration RTL, resimulate, and run Yosys on the structural block when you want a netlist.
The lesson is done when the simulation matches Forwarding unit. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.