RTL Studio Open IDE

4.4 · RV SYS CLASS

Data hazard detect

Implement a hazard detection unit to detect read-after-write hazards and generate stall/flush signals for a pipelined RISC-V core.

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

What you practice

Data hazard detect (4.4). Implement a hazard detection unit to detect read-after-write hazards and generate stall/flush signals for a pipelined RISC-V core. It is in Data hazard detect — 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

You will learn to detect read-after-write hazards between the ID and EX pipeline stages. This logic prevents incorrect execution when an instruction reads data before a previous load has written it.

Learning objectives

  • Implement raw hazard detection for source registers rs1 and rs2.
  • Generate a stall signal when a load instruction creates a hazard.
  • Generate a flush signal when a branch is taken.
  • Verify hazard detection using a structured testbench.

Theory

A data hazard occurs when an instruction depends on data that has not yet been written by a preceding instruction. In a pipelined RISC-V core, the ID stage reads registers while the EX stage might be writing to them. A load-use hazard is the most critical case because the data is not available until the end of the EX stage. The hazard unit compares the destination register of the instruction in EX (ex_rd) with the source registers in ID (id_rs1, id_rs2). If they match and the EX instruction is a load (ex_memread), the pipeline must stall. The hazard unit also handles control hazards by flushing the pipeline if a branch is taken.

SignalDirectionDescription
ex_memreadInputHigh if the EX stage instruction is a load.
ex_rdInputDestination register number from the EX stage.
id_rs1InputFirst source register number from the ID stage.
id_rs2InputSecond source register number from the ID stage.
branch_takenInputHigh if a branch decision was made and taken.
stallOutputHigh to pause the pipeline when a load-use hazard exists.
flushOutputHigh to clear the pipeline when a branch is taken.

Practice

  1. Open RtlStudio and select this chapter from the Examples list.
  2. Open student/rtl/ip/riscv/rv32i_hazard_unit.sv in the editor.
  3. Implement the logic to detect raw hazards for id_rs1 and id_rs2.
  4. Implement the logic to generate the stall signal based on ex_memread and the raw hazards.
  5. Implement the logic to generate the flush signal based on branch_taken.
  6. Open student/tb/tb_top.sv to review the testbench stimulus.
  7. Run Simulation from the sidebar and check the Log panel.

Summary

You implemented the hazard detection unit to identify read-after-write dependencies between pipeline stages. The logic correctly generates stall signals for load-use hazards and flush signals for taken branches. This ensures data integrity by preventing the execution of instructions with stale data. You verified the design using the provided simulation testbench.

© 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 system lesson in the IDE. The RV32I core and the mini-SoC fabric simulate together.
  2. Run Verilator and trace the program through MMIO: address, load/store data, and the peripheral response.
  3. Interrupts and DMA belong on the waveform next to the instruction stream, not only in a pass/fail line.
  4. 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 Data hazard detect. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.