When multiple operations overlap in a pipeline, hazards threaten correctness or stall progress. Every pipelined block needs a hazard plan: detect, forward, stall, or flush.
You will implement pieces of this plan across the next three lessons — stalls and bubbles (8.3), forwarding (8.4), then the pipelined ALU lab (8.5) where they all connect.
A condition where the pipeline would produce the wrong result, or two stages need the same hardware at the same time, unless you intervene with control logic.
| Type | Cause | Typical fix |
|---|---|---|
| Structural | Two stages need same resource (ALU, memory port) | Duplicate hardware, schedule, or stall |
| Data | Operand not ready when consumer needs it | Forwarding, stall, or compiler scheduling |
| Control | Branch/jump changes PC before pipe drains | Flush wrong-path ops, predict branch |
Hazards are not bugs — they are the price of overlapping operations. Your job is to pick the cheapest fix per case: duplicate hardware beats stall if area allows; forward beats stall if timing allows; predict beats flush if accuracy is high.
Design order: Eliminate structural conflicts first (memory ports, single ALU), then add forwarding for RAW, then stall logic for load-use, then branch flush/predict for control. Each layer builds on the previous — skipping structural fixes makes stall logic a band-aid.
Occurs when two pipeline stages need the same non-pipelined resource in the same cycle — e.g. instruction fetch and data load both hitting single-port memory.
Modern SoCs rarely share one physical port between IF and MEM — but the pattern repeats everywhere: one DMA engine vs CPU, one AXI port vs two masters, one multiplier vs two MAC lanes. The RTL pattern is identical: arbitrate, stall the lower-priority client, or duplicate the resource.
Dual-port SRAM costs ~1.6–2× the area of single-port at the same depth. If structural stalls happen <5% of cycles, a cheap arbiter + stall is often the right call. If stalls dominate, pay for the second port.
// Simple structural hazard arbiter — MEM wins over IF wire mem_req = mem_stage_active && mem_read_en; wire if_req = if_stage_active && !mem_req; assign mem_port_en = mem_req; assign mem_port_addr = mem_req ? mem_addr : if_pc; assign if_stall = mem_req && if_req; // IF waits for port
Data hazards appear when instruction B needs a value that instruction A has not finished writing. In RTL you detect them by comparing destination registers in later stages against source registers in EX.
| Name | Pattern | In-order pipe? |
|---|---|---|
| RAW (Read After Write) | Consumer reads before producer writes | Yes — most common; needs forward or stall |
| WAR (Write After Read) | Writer overwrites before reader finishes | Rare in classic 5-stage; issue in OoO rename |
| WAW (Write After Write) | Two writers, wrong commit order | Rare in-order; rename handles in OoO |
// RAW detect: EX-stage consumer needs register written by prior op
wire raw_hazard =
ex_reg_read_en && ex_rs_addr != 5'd0 && (
(mem_reg_write && mem_rd_addr == ex_rs_addr) ||
(wb_reg_write && wb_rd_addr == ex_rs_addr)
);
// Action: assert forward mux select OR stall EX/ID
// Combine hazard outputs — priority: structural stall overrides forward wire stall_from_struct = mem_port_busy && if_mem_conflict; wire stall_from_load = load_use_hazard; wire stall_if_id = stall_from_struct | stall_from_load; wire stall_id_ex = stall_from_load; wire use_fwd_a = raw_hazard_rs1 && !stall_from_load; wire use_fwd_b = raw_hazard_rs2 && !stall_from_load; // load-use: stall wins — forwarding cannot supply data yet
In a simple in-order pipeline with one writeback per cycle, WAR and WAW rarely appear because reads happen in ID and writes in WB — strictly ordered. They matter when you add bypass paths, dual-issue, or out-of-order retirement. Know the names; focus RTL effort on RAW.
Detecting RAW on x0 as destination causes false stalls — RISC-V x0 is not a real register. Always gate with rd != 5'd0 on both producer and consumer sides.
Branch outcome is known in EX (or later). Meanwhile IF/ID already fetched fall-through or wrong-target instructions. You must flush the wrong path.
The penalty is measured in lost fetch slots: a 5-stage pipe with branch in EX typically wastes 2 instructions (IF + ID) per taken branch. Branch prediction moves the penalty to mispredicts only — covered in advanced CPU design; for now implement correct flush-on-taken.
wire branch_taken = ex_branch && ex_cond_true; wire flush_if_id = branch_taken; wire flush_id_ex = branch_taken; // optional if branch in EX assign next_pc = branch_taken ? ex_target_pc : pc_plus4; // IF/ID registers load NOP when flush_if_id asserted
Forgetting to squash reg_write on flushed ops can corrupt the register file. Flush must zero side effects: no writeback, no memory store, no CSR update.
Unconditional jumps also need PC redirect + flush, but there is no "not taken" path — every jump is a control hazard. Link register write (rd=ra) must still commit from the jump instruction in EX while IF/ID flush.
Use this table as your implementation checklist. When sim fails on a dependent instruction pair, identify which row applies before randomly adding stalls.
| Hazard | Detect | Fix priority |
|---|---|---|
| Structural | Resource busy signal | Stall lower-priority stage; add port if frequent |
| RAW (EX needs WB) | Addr match + reg_write | Forward from MEM/WB; else stall 1 cycle |
| Load-use RAW | Load in MEM, use in EX | Must stall — data not ready until WB |
| Control | Branch resolved taken | Flush IF/ID; redirect PC; predict to hide |
Pipeline bugs rarely show on isolated instructions. Build directed tests that exercise each hazard row in the summary table — one test per row, run back-to-back in the same sim.
// Directed hazard tests — run sequentially in one sim task test_raw_alu_alu; // ADD then SUB on same reg — expect forward reset_pipe(); inject_instr(ADD, rd=1, rs1=2, rs2=3); inject_instr(SUB, rd=4, rs1=1, rs2=5); @(posedge clk); @(posedge clk); @(posedge clk); check_reg(4, expected_sub_result); endtask task test_load_use; // LW then ADD — expect 1-cycle stall reset_pipe(); inject_instr(LW, rd=1, rs1=2); inject_instr(ADD, rd=3, rs1=1, rs2=4); monitor_stall(1); // exactly one stall cycle endtask task test_branch_taken; // branch + wrong-path ops — expect flush reset_pipe(); inject_instr(BEQ, taken=1, target=0x100); inject_instr(ADD, ...); // wrong path — must not write @(posedge clk); check_no_reg_write_from_wrong_path(); endtask
Debug tip: Waveform markers at stall_if_id, flush_if_id, and fwd_a_sel let you correlate a wrong register value with the exact cycle the hazard unit failed.