Your pipelined core is complete: it runs every test program, in a predictable number of cycles, faster than the single-cycle core it came from. Real processors go much further, and every step further is a change of the kind you have just made: a new structure that saves time in one place and costs it, or hardware, in another. This closing article explains, without new RTL, what the next steps would be: more pipeline stages, the stalls a realistic memory forces, branch prediction, caches, exceptions and interrupts, and issuing more than one instruction per cycle. For each, it says what would change in your core and where the first half of this tutorial explains it in depth.
10 min read | Intermediate
What You'll Learn
- Explain what a five-stage pipeline changes compared with your three stages
- Describe the load-use stall a synchronous memory would require and how it is detected
- Explain why deeper pipelines need branch prediction, and how caches change the memory interface
- Identify where traps, interrupts and wider issue would fit into the core you built
Before You Start
This article has no code; it builds on the core you finished in A Control Hazard: Flushing After a Taken Branch and the measurements in Single-Cycle vs Pipelined: Cycles and Clock Period. If you extend your core along the lines below, you need the same Verilog as for the rest of the section (Verilog Tutorial). Each topic links the article of the first half that teaches it.
Five Stages
The classic RISC pipeline has five stages: IF (fetch), ID (decode and register read), EX (execute), MEM (data memory) and WB (write-back). Compared with your core, X is split into ID and EX, and W into MEM and WB:
Each stage does less, so the clock can be faster. Everything else gets a little harder:
- Forwarding from two places. A result now spends two cycles in flight after EX (in MEM and in WB), so an instruction in EX may need a value from either. Each operand multiplexer gets a third input, and the select logic must prefer the newer value when both match.
- A load-use stall. A load's data appears at the end of MEM, but the next instruction needs it at the start of EX, one cycle earlier. Forwarding cannot go back in time, so the pipeline must hold the dependent instruction in ID for one cycle and insert an empty slot, a bubble, into EX.
- Two flushed instructions per taken branch, if the branch is decided in EX, because IF and ID both hold instructions fetched after it.
The 5-Stage Pipeline, Data Hazards and Forwarding and Load-Use Stalls and Control Hazards explain all three in detail.
Stalls and a Realistic Memory
Your core's memory answers in the same cycle. A real on-chip SRAM registers the address at a clock edge and returns the data after it. Even in three stages, that changes the design: the instruction arrives one cycle after the PC is sent, which adds a fetch stage or a cycle of latency, and a load's data arrives too late to forward to the next instruction.
The fix for the second problem is a stall: hazard detection logic in the stage that reads registers compares its rs1 and rs2 with the rd of a load one stage ahead, and if they match, it freezes the PC and the first pipeline register for one cycle (their enable inputs, which your PC module does not have yet) and sends a bubble forward. The idea is the same as your flush: an invalid slot that does nothing. In the bubble sort, 2,016 instructions use the result of the load just before them, so the stall would cost about 2,016 cycles in that program, and it would remove the long W-to-X forwarding path that limits your pipeline's clock. Whether that is a good trade is exactly the kind of question Performance Modeling answers before anything is built.
Quick Check
Q: In a five-stage pipeline, lw a0, 0(a1) is followed by addi a2, a0, 1 and then by add a3, a0, a2. Which of the two needs a stall, and how does the second one get a0 and a2?
Show Answer
The addi needs a one-cycle stall: it uses a0 in EX one cycle after the lw is in EX, but the data arrives only at the end of MEM. After the stall, a0 is forwarded from WB. The add then gets a2 forwarded from the addi in MEM, and a0 from the register file or from WB, depending on the timing; no further stall is needed.
Branch Prediction
Your pipeline loses one cycle per taken branch or jump, which costs the test programs 13 to 25 percent more cycles. A five-stage pipeline would lose two, and deeper pipelines more. A branch predictor lets fetch guess both whether a branch is taken and where it goes, before the branch is even decoded:
- A branch target buffer (BTB) remembers, by PC, the targets of recent taken branches and jumps, so that fetch can go to the target in the very next cycle.
- A direction predictor guesses taken or not taken from each branch's history: a 2-bit counter per branch gets a loop branch right on all but its last iteration.
- A return address stack predicts where
retgoes, by remembering the return addresses of recent calls.
The flush you built stays: it is how a wrong guess is undone. A predictor only makes the guesses better. Why Branches Hurt, Static Prediction, BTBs and Return Address Stacks, Dynamic Predictors: Bimodal and gshare and Predictor Accuracy and Sizing cover predictors from the simplest to measured sizing studies.
Caches
Your memory is small, fast and fixed. Real programs need megabytes or gigabytes, and large memories are slow: tens to hundreds of cycles away. A cache keeps recently used data in a small fast memory next to the core, so that most accesses take a cycle or two. For your core, a cache changes one thing at the interface: an access can now take an unknown number of cycles. The imem_ready and dmem_ready inputs you have been ignoring exist for exactly that: when a cache misses, ready goes low, and the core must stall until it rises. The Memory Hierarchy and Locality and Cache Organization start the chapter on caches. In the SoC Design tutorial, Your Core Learns to Wait has you add exactly that stall and put your core on a real bus.
Where Is This Used?
Each of these steps is a product category. Small microcontroller cores stop around where you are, with two or three stages, a simple prediction scheme and tightly coupled memory instead of caches. Application cores in phones and laptops have deep pipelines, large predictors, several levels of cache, and issue several instructions per cycle out of order. Between them is a whole range of embedded and real-time cores that pick the features their workload needs, measured the way you measured your two cores.
Traps and Interrupts
Your core assumes nothing goes wrong. A real core must stop on an illegal instruction or a misaligned access, call the operating system on ecall, and react to interrupts from timers and devices. All of these are traps: the core saves the PC of the instruction it was executing and the cause in control and status registers (CSRs), and jumps to a handler. In a pipeline the hard part is making traps precise: every instruction before the trapping one must complete and none after it may change anything, which uses the same valid bits and flushes you built. Privilege Levels and CSRs, Traps and Precise Exceptions and Interrupts and Interrupt Latency explain them.
More Than One Instruction per Cycle
A pipeline completes at most one instruction per cycle. A superscalar core fetches, decodes and executes two or more per cycle, which needs a register file with more ports, more forwarding paths, and logic to check that the instructions issued together do not depend on each other. Out-of-order cores go further and execute instructions as soon as their operands are ready, not in program order, using register renaming to remove false dependences. Superscalar Issue and Out-of-Order Execution and Register Renaming explain both, with limit studies on the same kind of programs your core runs.
Quick Check
Q: Your pipeline ignores dmem_ready. A data cache is added that sometimes holds dmem_ready low for several cycles. What must the core do while it is low, and which state must not change?
Show Answer
The instruction in W must wait for its data, so the whole pipeline must stall: the PC and both pipeline registers keep their values, no register is written and no new instruction enters. Only when dmem_ready rises does the load complete and the pipeline move on. The instructions behind it must not overtake it, or the program order breaks.
Common Mistakes
Mistake #1: Adding stages without counting the hazards
Each new stage between reading and writing a register adds a forwarding source, and each new stage before the branch decision adds a flushed instruction. More stages are only faster if the clock gain outweighs the extra cycles, which must be measured.
Mistake #2: Treating prediction as a replacement for the flush
A predictor makes the guess better, but every guess can still be wrong. The flush, or its equivalent in a deeper pipeline, is what keeps the core correct.
Mistake #3: Ignoring the ready signals once memory gets real
With any memory that can be slow, a core that does not stall on ready low reads data that is not there yet. Plan the stall logic when you plan the memory, not after.
Your Turn in the Lab
There is no graded exercise for this article. If you want to keep going, try one extension in your own project, test it with your directed tests and the seven programs, and measure it as in Single-Cycle vs Pipelined: Cycles and Clock Period:
- Give your PC module an enable input and add a load-use stall, then replace the data memory read with one that answers a cycle later.
- Add static prediction: predict backward branches as taken (loops) and fetch their target early, and count how many flushes disappear.
Then compare the cycle counts with the formula you know, instructions + redirects + 2, and see which of your changes moved which term.
Open the course Computer Architecture: Build a RISC-V Core in ChipVerify LabSummary
- A five-stage pipeline shortens each stage but adds a second forwarding source, a load-use stall and a second flushed instruction per taken branch.
- Realistic memories answer a cycle later, which turns loads followed by their use into stalls and adds latency to fetch.
- Branch prediction lets fetch follow taken branches early; the flush remains for wrong guesses.
- Caches make memory accesses take a variable number of cycles, handled with the ready signals and pipeline stalls.
- Traps, interrupts and superscalar or out-of-order issue build on the same valid bits, flushes and forwarding you have built; the first half of this tutorial covers each.