Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • RTL Foundations57
  • Combinational Design58
  • Sequential Design61
    • Flip-Flops and Registers
    • Counters and Timers
    • Shift Registers and LFSRs
    • Pipelining
    • Stalls, Bubbles and Hazards
      • retiming and hazard management
      • Structural Hazard and Stall Logic
      • Pipeline Flush on Branch Resolve
      • Retiming-Friendly Coding Notes
      • Data Hazards and Forwarding
      • A Round-Robin Arbiter from a Priority Encoder
      • Key takeaways
      • Common pitfalls
      • Worked Examples and Waveforms
      • Corner Cases and Stress Tests
      • Integration and Sign-off Notes
    • Retiming
  • FSM & Control66
  • Timing & Clocking64
  • Reset & CDC58
  • Memory & Buses94
  • Advanced RTL60
  • Senior & Interview175
  • Glossary and Notation
Loading lesson

‹Stalls, Bubbles and Hazards
  1. Topics
  2. RTL Design
  3. Sequential Design
  4. Stalls, Bubbles and Hazards
  5. Pipeline Flush on Branch Resolve

Part 3 · Sequential · Intermediate

Pipeline Flush on Branch Resolve

A taken branch flushes two wrong-path stages and resumes fetch at the target.

On this page

    Why this matters

    A taken branch resolves two stages after the wrong-path instructions entered the pipe. Clear only their valid bits and a store or register write may still reach commit.

    After this lesson you can: write the redirect and flush for a branch resolved in execute, and estimate the average CPI penalty from the branch and taken rates.

    Fetching before you know

    A pipeline fetches the next sequential instruction every cycle. A taken branch resolves a couple of stages later, after two wrong-path instructions have entered the pipe.

    The fix is a flush: redirect the PC to the target and clear the valid bits of the younger instructions.

    A three-stage pipeline with a redirect and flush from execute back to fetch and decode.
    The execute stage resolves the branch. If taken, it redirects fetch and flushes the two younger stages.
    The page's three-stage front end around a taken branch at PC 4: the wrong-path instructions 5 and 6 are flushed and execution resumes at 0x40 two cycles later

    Each column is one cycle and each cell holds the PC in that stage, as traced by the testbench below. A struck-through cell is still in the register, but its valid bit is 0.

    • Cycle 2: the branch at PC 4 resolves taken in Execute while 5 and 6 are already in flight.

    • Cycle 3: Fetch is redirected to 0x40, and both younger instructions lose their valid bits.

    • Cycles 3 and 4: Execute holds only flushed work, so two cycles are lost; 0x40 executes in cycle 5.

    The full listing sits under Testbench below. It redirects the PC on a taken branch and clears both younger valid bits.

    Branch penalty

    Each flushed slot is a lost cycle in this single-issue sketch: fetch is stage 1, one item enters per cycle, fetch predicts sequential (not taken), redirect is immediate, and there is no extra fetch delay or overlapping stall. Baseline CPI is near 1. With the branch resolved in stage k, a taken branch discards k − 1 instructions, which equals wasted cycles here. A discarded count is not generally the same as elapsed recovery time.

    If a fraction b of instructions are branches and a fraction t of those are taken, the average cost is:

    CPI≈1+b⋅t⋅(k−1)
    • Resolve earlier (move comparison into decode) to cut k.

    • Predict the outcome and fetch from the predicted target; flush only on a misprediction.

    • Delay slots expose the bubble to software (rare in modern ISAs).

    Common mistakes

    Common pitfall

    Flush must kill side effects, not only the valid bit. If a wrong-path instruction could already write a register or store to memory, the design must stop it before its commit stage. Valid bits that gate writes are the usual mechanism.

    Interview

    “What happens on a branch misprediction?” Detect in execute, redirect PC, clear valid bits in all younger stages, drain nothing with side effects. In this sketch the penalty equals the number of stages between fetch and resolve.

    Check yourself If branches are 20% of instructions, 60% are taken, and resolve happens in stage 3 (penalty 2 cycles), what is the average CPI?

    1 + 0.20 × 0.60 × 2 = 1.24.

    Testbench

    The three-stage front end plus the testbench that takes a branch and checks the resume address.

    mini_frontend.sv
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    module mini_frontend (
      input  logic        clk, rst_n,
      input  logic        taken,              // execute: a taken branch is resolved this cycle
      input  logic [7:0]  target,
      output logic [7:0]  exec_pc,
      output logic        exec_valid,         // instruction in execute is real
      output logic [7:0]  fetch_pc
    );
      logic [7:0] pc, d_pc;
      logic       d_valid;
    
      always_ff @(posedge clk or negedge rst_n)
        if (!rst_n) begin
          pc <= 8'd0;  d_valid <= 1'b0;  exec_valid <= 1'b0;
        end else begin
          // Fetch: redirect on a taken branch, otherwise sequential
          pc <= taken ? target : pc + 8'd1;
          // Decode: flush (drop) the instruction fetched on the wrong path
          d_pc    <= pc;
          d_valid <= !taken;
          // Execute: the instruction behind the branch is also wrong-path, so flush it too
          exec_pc    <= d_pc;
          exec_valid <= d_valid && !taken;
        end
      assign fetch_pc = pc;
    endmodule
    

    Remember

    • On a taken branch, redirect the PC and clear the valid bits of all younger stages.

    • With the branch resolved in stage k, the sketch discards k - 1 instructions.

    • Flush must also stop side effects such as register writes and stores before commit.

    Keep going

    • Practice: Branch misprediction flush logic

    • Interview: run a mock interview on this topic

    Related topics

    • Retiming-friendly coding

    Previous

    159 / 694

    Next: Retiming-Friendly Coding Notes

    Up next

    Up nextRetiming-Friendly Coding Notes

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • retiming and hazard management
    • Data Hazards and Forwarding
    • A Round-Robin Arbiter from a Priority Encoder