Skip to content
BIASED

The datapath and pipelining

A single-cycle datapath for add/lw/sw/beq, five IF–WB stages, throughput vs latency, data/control hazards, forwarding, stalls and branch prediction.

The single-cycle datapath

Open the processor. A datapath moves values through the PC, instruction memory, register file, ALU and data memory; control sets muxes from the opcode and funct fields. Beside ALUOp the classic design needs RegWrite, MemRead, MemWrite, MemtoReg, ALUSrc, RegDst, Branch and Jump.

Single-cycle: every instruction finishes in one long clock. With IM 250 + regs 150 + ALU 200 + DM 250 + write 100 ps, lw sets Tc=950T_c=950 ps (f≈1.05f\approx1.05 GHz) and CPI =1=1. An add was ready at 700 ps — the common case pays for the slowest path, and most units idle most of the cycle. Raising frequency means shortening that path or overlapping different instructions’ work in time.

Worked example. A loop of 10 iterations, run 100 times; its closing branch goes taken 9 times and then not taken once. Always-not-taken is right 10 % of the time and backward-taken 90 %. The one-bit predictor is right 80 %: it mispredicts the exit, stores “not taken”, and mispredicts again on the first iteration of the next run. The two-bit counter is right 90 %: the exit moves it from 11 to 10, which still predicts taken. The static rule ties here, but put the same pattern on a forward branch (an if that is usually true) and backward-taken drops to 10 % while the counter still gets 90 %: the counter learns the bias of each branch, whatever its direction. Modern predictors combine the histories of many branches and reach far higher accuracy on real programs; the next section returns to them, because deeper pipelines make each misprediction cost many more cycles than 2.

Five stages

Cut at unit boundaries: IF (fetch), ID (decode and register read), EX (ALU), MEM (data memory), WB (register write), with a pipeline register between each pair carrying both data and the control signals that still apply to that instruction. Five instructions can be in flight; each cycle every stage advances one step. sw and beq still walk empty stages so architectural updates stay in program order.

Pipelining raises throughput, not one-instruction latency: T_{pipe}(n)=(k+n-1)T_c. Example stage clock ~280 ps (slowest stage plus pipeline-register overhead). Ideal speedup 950/280≈3.4, less than k=5 because stages are uneven and hazards insert stalls. After a few instructions the pipe fills and, when hazards cooperate, retires about one instruction per short cycle.

Worked example. The four instructions take 5+3=85 + 3 = 85+3=8 cycles if nothing stalls, 9 with forwarding (one load-use stall) and 14 without (six stalls). Forwarding removed five of the six stall cycles; the one that remains is the price of loads that are used at once.

Hazards and forwarding

Hazards stop the next instruction from starting on schedule.

  • Structural: one memory for IF and MEM — fix with separate IM/DM or a dual-ported cache. Writing the register file in the first half of WB and reading it in the second half of ID removes the usual register-port clash.
  • Data: an instruction needs a result still in the pipe. Forwarding muxes from EX/MEM and MEM/WB into ALU inputs clear ALU→ALU dependences with no stall; if both stages can supply a source, EX/MEM wins as the more recent definition. A store whose data comes from the previous ALU op forwards into the MEM store-data path. Load-use still needs one stall: the load’s data appears at the end of MEM, too late for the next EX — detect by comparing the load’s rd in ID/EX with the next instruction’s rs1/rs2 in IF/ID, hold IF/ID, insert a bubble (control zeros) into ID/EX, and freeze PC for one cycle.
  • Control: branch target and takenness unknown while younger instructions have already been fetched.

CPI=1+∑fipi\mathrm{CPI}=1+\sum f_i p_i. Worked. 25% loads, 10% of them load-use (f=0.025f=0.025, p=1p=1); 20% branches, 30% taken under predict-not-taken with a 2-cycle flush (fp=0.12fp=0.12): CPI ≈1.145\approx1.145.

Worked timing. One lw alone takes 5×280=14005\times280=1400 ps in the pipeline against 950 ps single-cycle — latency got worse because of pipeline registers and unbalanced stages. Four instructions take (5+3)×280=2240(5+3)\times280=2240 ps against 4×950=38004\times950=3800 ps (speedup 1.7). For n=1000n=1000, Tpipe=1004×280≈281T_{\mathrm{pipe}}=1004\times280\approx281 ns against 950950 µs-scale single-cycle time for the same nn, approaching 950/280≈3.39950/280\approx3.39 as n→∞n\to\infty. It is not 5 because stages are unbalanced (WB’s 100 ps of work still occupies a 250 ps slot here) and pipeline registers add delay.

Branches

In the classic five-stage design branches resolve in EX, so two wrong-path fetches may already sit in IF/ID. Predict not-taken: fall through; on a taken branch flush those stages (2-cycle penalty here). Predict taken needs an early target adder. A 2-bit saturating counter per branch (or BTB entry) keeps bias through a single surprise: strongly/weakly taken/not-taken; only two wrong outcomes flip the direction. A BTB supplies the target without waiting for full decode. Moving the compare into ID cuts the flush to one cycle at the cost of an early comparator.

MIPS delayed branches exposed one instruction after the branch as always executed; modern RISC-V cores speculate with predictors and squash on mispredict instead. Timing: with CPI 1.15 and a 280 ps stage clock versus 950 ps single-cycle, time per instruction falls from 950 ps to about 1.15×280≈322 ps — roughly 3× in this toy model before cache misses re-enter as MEM stalls.

Drill: add t0,t1,t2; sub t3,t0,t4 forwards from EX/MEM; lw t0,0(a0); addi t1,t0,1 stalls then forwards from MEM/WB; a taken beq under predict-not-taken flushes two younger fetches.

Without forwarding, a dependent instruction reads operands in ID while the producer writes in WB — three stages later. With the split-cycle register file, distance 1 stalls 2 cycles, distance 2 stalls 1, and from distance 3 the value is ready. A four-instruction chain with three such dependences needs 8+6=148+6=14 cycles instead of 8 if nothing forwards.

Next: Exceptions and interrupts — stop five in-flight instructions precisely.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Hazards: forwarding vs stalloptional

    On a 5-stage pipeline for 3 instruction pairs choose forward, stall, or none; count bubbles.

    ~20 min · easier on a computer · The matching course topic has been read.

    Self-check in the notebook — not part of mastery or the certificate.

Full notes

Answers are saved on the server and tied to this browser until you sign in.