Codiode

Pipeline Interlock on Data Hazard

MediumVerilog / SystemVerilogBuild

The simplest answer to a data hazard is to wait: freeze the front of the pipeline and feed bubbles in until the result reaches writeback.

This module monitors a hazard detection flag from the decode stage. When a hazard is detected, it must immediately assert a stall signal to freeze the fetch and decode stages, and assert a bubble signal to insert a NOP into the execute stage. Because the data takes two additional clock cycles to reach the writeback stage, the stall and bubble signals must remain asserted for exactly two consecutive cycles. During the second cycle of the stall, the pipeline is already frozen, so any hazard detection inputs must be ignored.

Timing and Reset Rules:

  • Clock edge: posedge clk
  • Reset: Asynchronous, active-low (rst_n)
  • Reset values: stall and bubble default to 0; internal state resets to idle.
  • Outputs stall and bubble must react combinationally in the exact same cycle that hazard_detected is asserted.
  • The second cycle of the stall is driven by internal sequential logic.

Worked Trace:

  • Cycle 1: rst_n=0 → stall=0, bubble=0
  • Cycle 2: rst_n=1, hazard_detected=0 → stall=0, bubble=0
  • Cycle 3: hazard_detected=1 → stall=1, bubble=1 (Cycle 1 of stall)
  • Cycle 4: hazard_detected=1 (ignored) → stall=1, bubble=1 (Cycle 2 of stall)
  • Cycle 5: hazard_detected=0 → stall=0, bubble=0 (Stall complete)
  • Cycle 6: hazard_detected=1 → stall=1, bubble=1 (Cycle 1 of new stall)
  • Cycle 7: hazard_detected=0 → stall=1, bubble=1 (Cycle 2 of new stall)
  • Cycle 8: hazard_detected=0 → stall=0, bubble=0
Signal Direction Width Description
clk input 1 Positive-edge triggered clock
rst_n input 1 Asynchronous active-low reset; outputs go to 0
hazard_detected input 1 Asserted when a RAW hazard is identified
stall output 1 Freezes the early pipeline stages
bubble output 1 Inserts a NOP into the later pipeline stages

Constraints

  • Clock edge: posedge clk
  • Reset: Asynchronous, active-low (rst_n)
  • Outputs stall and bubble must be combinational with respect to hazard_detected to stall the pipeline in the current cycle.
  • The stall must last exactly two clock cycles per hazard event.
  • If hazard_detected is asserted during the second cycle of an ongoing stall, it must be ignored.
  • Outputs stall and bubble must be 0 on reset.

Topics

FSMCombinational LogicPipelining

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 13 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.