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.
| Signal | Direction | Description |
|---|---|---|
ex_memread | Input | High if the EX stage instruction is a load. |
ex_rd | Input | Destination register number from the EX stage. |
id_rs1 | Input | First source register number from the ID stage. |
id_rs2 | Input | Second source register number from the ID stage. |
branch_taken | Input | High if a branch decision was made and taken. |
stall | Output | High to pause the pipeline when a load-use hazard exists. |
flush | Output | High to clear the pipeline when a branch is taken. |
Practice
- Open RtlStudio and select this chapter from the Examples list.
- Open
student/rtl/ip/riscv/rv32i_hazard_unit.svin the editor. - Implement the logic to detect raw hazards for
id_rs1andid_rs2. - Implement the logic to generate the
stallsignal based onex_memreadand the raw hazards. - Implement the logic to generate the
flushsignal based onbranch_taken. - Open
student/tb/tb_top.svto review the testbench stimulus. - 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
- 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 Data hazard detect. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.