Pipeline Interlock on Data Hazard
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:
stallandbubbledefault to 0; internal state resets to idle. - Outputs
stallandbubblemust react combinationally in the exact same cycle thathazard_detectedis 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
stallandbubblemust be combinational with respect tohazard_detectedto stall the pipeline in the current cycle. - The stall must last exactly two clock cycles per hazard event.
- If
hazard_detectedis asserted during the second cycle of an ongoing stall, it must be ignored. - Outputs
stallandbubblemust be 0 on reset.
Topics
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