RTL Design / Module 8: Pipelining for Performance
Lesson 8.3

Stalls, bubbles & flush

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.

Vocabulary

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.

1

Stall signals โ€” who freezes?

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

SignalEffect on registerUpstream
stall_if_idIF/ID holds current instrPC does not advance
stall_id_exID/EX holds; IF/ID must stall tooInsert bubble into EX if needed
stall_ex_memEX/MEM holds; all prior stages stallMemory 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).

Global stall

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.

Stall propagation diagram
stall_logic.v
// 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;
stall_chain.v
// 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;
2

Inserting a bubble (NOP)

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.

  • Stall + bubble: upstream frozen, current stage receives forced NOP on next edge
  • Flush to NOP: overwrite register fields with safe defaults
nop_insert.v
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;
Trap

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.

3

Flush on redirect

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.

Flush on branch redirect
Flush stall priority encoder

Same priority in every stage register โ€” inconsistent ordering across IF/ID vs ID/EX causes subtle sim/synth mismatches.

flush_control.v
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;
Trap

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

4

Enable / clear pipeline registers โ€” full pattern

The canonical pipeline register template handles reset, flush (clear), stall (hold), and normal advance in one always block.

pipeline_reg_en_clear.v
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_pipe.v
// 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
rst_nโ†’clear all flushโ†’NOP + kill flags stallโ†’hold elseโ†’capture d_in
5

Stall vs bubble vs flush โ€” decision table

SituationActionPC advances?
Load-use hazardStall IF/ID/ID/EX + bubble EXNo
Structural mem conflictStall IF onlyNo
Branch mispredictFlush IF/ID, redirect PCYes (to target)
InterruptFlush all in-flight, save CSRYes (to vector)
Debug haltGlobal stall all stagesNo

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.

โ–ถ Test stall/flush in simulator
6

Control checklist

  • Stall propagates backward from the stage that cannot advance
  • Bubble path zeros reg_write, mem_write, and branch flags
  • Flush priority documented (irq > branch > normal)
  • Pipeline regs use if/else-if: flush before stall before capture
  • TB covers load-use stall and taken-branch flush in same run
  • Multi-hazard priority tested (stall during flush edge case)
7

Combining stall and flush in one cycle

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.

hazard_priority.v
// 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;
Verification angle

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.

๐ŸŽฏ What to remember

  • Stall freezes registers; bubble inserts harmless NOP; flush kills wrong-path work.
  • Load-use needs stall + bubble โ€” forwarding alone cannot fix it.
  • Pipeline register template: reset โ†’ flush โ†’ stall โ†’ enable, in that priority.
  • Always clear side-effect flags on flush, not just the instruction field.
Advertisement