Hazard fixes boil down to three control actions: stall (freeze a stage), bubble (insert a NOP), and flush (squash wrong-path ops). This lesson wires them into pipeline register enable/clear logic.
Stall = hold register values, block upstream advance. Bubble = let a stage advance but force NOP semantics. Flush = overwrite in-flight ops with harmless NOPs on redirect.
A global stall is rare. Typical design: per-stage stall_X derived from hazard logic. When ID/EX stalls, IF/ID must also stall (otherwise you overwrite the instruction waiting in ID).
| Signal | Effect on register | Upstream |
|---|---|---|
stall_if_id | IF/ID holds current instr | PC does not advance |
stall_id_ex | ID/EX holds; IF/ID must stall too | Insert bubble into EX if needed |
stall_ex_mem | EX/MEM holds; all prior stages stall | Memory port stays busy |
The key rule: stall propagates backward. If ID/EX cannot advance, IF/ID must hold too โ otherwise the instruction in ID gets overwritten by the next fetch. Only stages downstream of the hazard may continue (often with a bubble inserted).
Debug halt, reset sequencing, or clock-gate handshakes sometimes assert stall_all. Every PC and pipeline register freezes. Do not mix global stall with per-stage stall without OR-combining into each stage's final stall signal.
// Load-use: LW in EX/MEM, dependent op in ID/EX
wire load_use_stall =
id_ex_mem_read &&
(id_ex_rs == ex_mem_rd || id_ex_rt == ex_mem_rd) &&
ex_mem_rd != 5'd0;
assign stall_if_id = load_use_stall;
assign stall_id_ex = load_use_stall;
assign stall_pc = load_use_stall;
// Bubble into EX/MEM (advance ID/EX into EX with NOP)
assign bubble_ex_mem = load_use_stall;
// Backward stall propagation โ one source of truth wire hazard_stall_id_ex = load_use_stall | mul_stall; // example assign stall_id_ex = hazard_stall_id_ex; assign stall_if_id = hazard_stall_id_ex; // must match ID/EX assign stall_pc = hazard_stall_id_ex; // PC frozen with IF/ID // EX/MEM and MEM/WB keep advancing unless separately stalled assign stall_ex_mem = mem_wait | div_stall;
A bubble is a pipeline slot that looks like a valid instruction but has no side effects. In RISC-V, addi x0, x0, 0 is the canonical NOP. Two mechanisms:
Bubbles differ from stalls: with a stall the pipeline register holds its value; with a bubble the register advances but the payload is forced to NOP semantics. Load-use needs both โ stall upstream stages so the dependent op waits, and bubble into EX so the ALU does not re-execute garbage.
localparam NOP = 32'h00000013; // addi x0,x0,0 wire [31:0] id_ex_instr_next = bubble_ex_mem ? NOP : id_ex_instr_in; wire id_ex_reg_write_next = bubble_ex_mem ? 1'b0 : id_ex_reg_write_in; wire id_ex_mem_read_next = bubble_ex_mem ? 1'b0 : id_ex_mem_read_in;
Inserting only an instruction NOP without clearing mem_write or branch flags still causes side effects. A bubble must zero all control signals, not just the opcode field.
When a branch resolves taken (or interrupt fires), wrong-path instructions in IF and ID must not commit. Flush overwrites pipeline registers with NOP and clears control signals.
Same priority in every stage register โ inconsistent ordering across IF/ID vs ID/EX causes subtle sim/synth mismatches.
wire branch_flush = ex_branch_taken;
wire irq_flush = irq_pending && ex_allow_irq;
// Priority: irq > branch > normal
wire flush_if_id = irq_flush | branch_flush;
wire flush_id_ex = irq_flush; // branch may only flush IF/ID
assign pc_next = irq_flush ? irq_vector :
branch_flush ? ex_target :
pc_plus4;
If flush and stall assert together on the same register without priority, synthesis may produce X in simulation or glitch enables. Define explicit priority: if (flush) ... else if (!stall) ...
The canonical pipeline register template handles reset, flush (clear), stall (hold), and normal advance in one always block.
module pipeline_reg #(
parameter WIDTH = 32
) (
input wire clk,
input wire rst_n,
input wire stall, // 1 = hold
input wire flush, // 1 = load zero/NOP
input wire [WIDTH-1:0] d_in,
output reg [WIDTH-1:0] q_out
);
localparam [WIDTH-1:0] CLR = {WIDTH{1'b0}};
always @(posedge clk or negedge rst_n) begin
if (!rst_n)
q_out <= CLR;
else if (flush)
q_out <= CLR; // clear / NOP payload
else if (!stall)
q_out <= d_in; // enable: capture
// else hold โ implicit stall
end
endmodule
// ID/EX bundle โ flush kills all side-effect flags
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
id_ex_valid <= 1'b0;
id_ex_reg_write <= 1'b0;
id_ex_mem_write <= 1'b0;
id_ex_alu_src_a <= 32'h0;
end else if (flush_id_ex) begin
id_ex_valid <= 1'b0;
id_ex_reg_write <= 1'b0;
id_ex_mem_write <= 1'b0;
id_ex_instr <= NOP;
end else if (!stall_id_ex) begin
id_ex_valid <= 1'b1;
id_ex_reg_write <= id_reg_write;
id_ex_mem_write <= id_mem_write;
id_ex_alu_src_a <= rs1_data;
id_ex_instr <= if_id_instr;
end
end
| Situation | Action | PC advances? |
|---|---|---|
| Load-use hazard | Stall IF/ID/ID/EX + bubble EX | No |
| Structural mem conflict | Stall IF only | No |
| Branch mispredict | Flush IF/ID, redirect PC | Yes (to target) |
| Interrupt | Flush all in-flight, save CSR | Yes (to vector) |
| Debug halt | Global stall all stages | No |
When multiple conditions can fire in the same cycle (e.g. load-use stall during a taken branch), define explicit priority in your hazard unit. Typical choice: interrupt flush > branch flush > structural stall > load-use stall โ document it in the spec.
Simulation tip: Log stall, flush, and pc every cycle in your testbench. Most pipeline bugs show up as a missing stall or a flush that doesn't clear mem_write.
Real designs see overlapping conditions โ e.g. a load-use stall on the same cycle a debug interrupt fires. Your hazard unit must resolve them without ambiguous register behavior.
// Final stall/flush per stage โ single assignment each wire stall_if_id_raw = load_use_stall | struct_stall; wire flush_if_id_raw = irq_flush | branch_flush; // Flush wins over stall on IF/ID (redirect kills fetch anyway) wire stall_if_id = stall_if_id_raw && !flush_if_id_raw; wire flush_if_id = flush_if_id_raw; // ID/EX: irq may flush; load-use stalls without flush wire stall_id_ex = load_use_stall && !irq_flush; wire flush_id_ex = irq_flush;
Constrained-random instruction streams catch priority bugs that directed tests miss. Seed your RNG and assert $stable(pc) whenever stall_pc is high โ PC must not advance during stall.