Codiode

Load Use Hazard Detection

MediumVerilog / SystemVerilogBuild

Forwarding fixes most read-after-write hazards, but not a load followed straight away by an instruction that uses the loaded value. The data arrives one stage too late, so the pipeline has to stall for a cycle.

The hazard detection unit monitors the pipeline registers to identify this specific condition. It compares the destination register of the instruction currently in the Execute stage against the source registers of the instruction in the Instruction Decode stage. If the Execute stage instruction is a memory load, its destination register is not register zero, and it matches either of the Decode stage source registers, a load-use hazard is detected and a stall is issued.

  • Clock edge: posedge clk

  • Reset type: Asynchronous active-low rst_n

  • Reset polarity: All outputs go to 0 when rst_n is 0

  • Priority: Reset has the highest priority. Matches on register 0 are always ignored.

  • The stall output is registered and updates on the positive edge of clk.

  • Cycle 1: rst_n=0 → stall=0

  • Cycle 2: rst_n=1, id_ex_mem_read=1, id_ex_rd=1, if_id_rs1=2, if_id_rs2=3 → stall=0 (no hazard detected)

  • Cycle 3: id_ex_mem_read=1, id_ex_rd=5, if_id_rs1=5, if_id_rs2=2 → stall=0 (hazard on rs1 detected; output updates on next edge)

  • Cycle 4: id_ex_mem_read=0, id_ex_rd=0, if_id_rs1=0, if_id_rs2=0 → stall=1 (pipeline is stalled)

  • Cycle 5: id_ex_mem_read=1, id_ex_rd=0, if_id_rs1=0, if_id_rs2=0 → stall=0 (hazard resolved; match on register 0 is ignored)

  • Cycle 6: id_ex_mem_read=1, id_ex_rd=9, if_id_rs1=2, if_id_rs2=9 → stall=0 (hazard on rs2 detected)

  • Cycle 7: id_ex_mem_read=0, id_ex_rd=0, if_id_rs1=0, if_id_rs2=0 → stall=1 (pipeline is stalled)

  • Cycle 8: id_ex_mem_read=0, id_ex_rd=0, if_id_rs1=0, if_id_rs2=0 → stall=0 (hazard resolved)

Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; stall goes to 0 when asserted
id_ex_mem_read input 1 Asserts to 1 if the instruction in the EX stage is a memory load
id_ex_rd input 5 Destination register address of the instruction in the EX stage
if_id_rs1 input 5 First source register address of the instruction in the ID stage
if_id_rs2 input 5 Second source register address of the instruction in the ID stage
stall output 1 Registered output; asserts to 1 if a hazard is detected

Constraints

  • The stall output must be registered and updated on the positive edge of clk.
  • Register 0 (5'b00000) is hardwired to zero in this architecture and never causes a data hazard. A match on register 0 must not trigger a stall.
  • Reset is asynchronous and active-low.

Topics

RegistersPipeliningHazard Detection

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Pipelining

Problem 16 of 20 in the Pipelining track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.