Your single-cycle core does everything for an instruction in one clock cycle, so its clock can be no faster than its slowest instruction. Pipelining splits that work into stages that run at the same time on different instructions: while one instruction is being executed, the next one is already being fetched. In this chapter you turn your core into a 3-stage pipeline, and in exercise E13 you take the first step: cut the datapath into three stages, add the two pipeline registers between them, and give each stage a valid bit. This article explains the split, what the pipeline registers must hold, and why the result runs some programs perfectly and others not at all, yet.

12 min read | Intermediate

What You'll Learn

  • Explain why the core is split into F, X and W, and what each stage does
  • List what the F/X and X/W pipeline registers must carry, and why every signal W uses must travel with its instruction
  • Use valid bits to start the pipeline after reset and to keep empty stages from writing
  • Predict the cycle count of a program without hazards, and explain why real C does not run yet

Before You Start

You need Verilog registers and a careful eye for which signal belongs to which instruction; the Verilog Tutorial covers the language. The ideas behind this chapter are in the first half of the tutorial: Single-Cycle and Multi-Cycle Processors explains what limits a single-cycle clock, and The 5-Stage Pipeline explains pipelining with five stages. Pass E12 first: E13 starts from your finished single-cycle core and all its blocks.

Why Pipeline

In the single-cycle core, a load's path crosses the instruction memory, the decoder, the register file, the ALU, the data memory and the write-back multiplexer within one cycle. In a placed version of the reference core, with each memory read assumed to take 2 ns, that path needs 14.373 ns, and every instruction pays for it. Meanwhile each part of the hardware is busy for only a part of the cycle: the instruction memory has finished its work long before the data memory starts.

Pipelining puts registers into that path. The work of one instruction is spread over several cycles, one stage per cycle, and in each cycle every stage works on a different instruction. If the work were split into three equal parts, each cycle would need a third of the time, and after the pipeline fills, one instruction would still complete every cycle. The split is never perfectly equal, and the pipeline creates two new problems, but the gain is real: Single-Cycle vs Pipelined: Cycles and Clock Period measures it on your design.

The Three Stages

The 3-stage core: stage F holds the PC and fetches; the F/X register keeps the instruction, its PC and a valid bit; stage X decodes, reads registers, runs the ALU and decides branches; the X/W register keeps the ALU result, store data, rd, control, PC + 4 and a valid bit; stage W accesses data memory and writes the register file. Redirects go from X back to F, register writes from W back to the register file.
  • F, fetch: your PC module and the instruction memory. At the end of the cycle the instruction and its address go into the F/X register.
  • X, decode and execute: the decoder, the immediate generator, the register file's read ports, the operand multiplexers, the ALU, the branch comparator and the target logic, all working on the instruction in F/X. Branches and jumps are decided here.
  • W, memory and write-back: the load/store unit and the data memory, the write-back multiplexer and the register file's write port, working on the instruction in X/W.

This split keeps your blocks whole: you cut the single-cycle datapath in two places and change nothing inside the modules. It also gives exactly one data hazard and one control hazard, each with a simple fix, which A Data Hazard and Its Forwarding Path and A Control Hazard: Flushing After a Taken Branch add. The register file now belongs to two stages: X reads it, W writes it. That is normal, and it is the source of the data hazard.

The Pipeline Registers

A pipeline register is a set of ordinary flip-flops between two stages. At each clock edge it captures what the stage before it produced, and during the next cycle it feeds the stage after it. The rule that governs what goes in it is simple, and breaking it is the most common pipeline bug: every signal a stage uses must belong to the instruction that is in that stage.

F/X carries the instruction word and its PC (X needs the PC for auipc, branch targets and PC + 4), plus a valid bit.

X/W carries everything W needs about its instruction: the ALU result (the value to write, or the memory address), the store data (rs2, read in X), the destination register rd, the control signals W uses (reg_write, mem_read, mem_write, wb_sel and funct3 for the access size), PC + 4 for the link of jal and jalr, and a valid bit. If W takes any of these from X instead, it uses the values of the next instruction, not its own.

Here is that mistake, from the test: a core whose register file write address is the rd of the instruction in X instead of the one carried in X/W. The first instruction, lui x1, 0x1, writes its correct value into the wrong register, x2, which is the destination of the instruction behind it:

Output
FAIL: register write 1: expected x1 = 0x00001000, got x2 = 0x00001000

Quick Check

Q: A store sw x2, 0(x1) is in W. Which of these does W take from X/W and which from somewhere else: the address, the data, the byte enables, the write enable?

Show Answer

All four come from X/W, directly or through the load/store unit: the address is the ALU result in X/W, the data is the rs2 value read in X and carried in X/W, and the byte enables and write enable come from the store's own funct3 and mem_write, also in X/W, together with its valid bit. Nothing about the store is read from X, where the next instruction already is.

Valid Bits

After reset there are no instructions in X or W yet. A valid bit in each pipeline register says whether the stage behind it holds a real instruction. Both are reset to 0. At each clock edge the F/X valid bit becomes 1 (F always fetches something), and the X/W valid bit takes the F/X valid bit, so the pipeline fills from the front: the first instruction is in F in cycle 1, in X in cycle 2 and in W in cycle 3.

An instruction whose valid bit is 0 must not change anything: W must not write the register file or the memory for it. The simplest way is to gate the two write enables, the register file's and dmem_we, with the X/W valid bit. retire is now simply the X/W valid bit: an instruction completes when it is valid in W.

The valid bits are the only pipeline register bits that need a reset; the data parts may hold anything while their valid bit is 0. Keep them in separate always_ff blocks, the reset ones and the others, as The Core's Specification asks. In the control-hazard step the F/X valid bit gets a second job: clearing it is how a wrongly fetched instruction is thrown away.

Timing in the Pipeline

Pipeline diagram of four independent instructions: each spends one cycle in F, one in X and one in W; the first completes in cycle 3, and one completes every cycle after that.

The pipeline diagram shows each instruction's stage in each cycle. The first instruction completes in cycle 3, after two cycles of filling, and from then on one completes every cycle. A program of n instructions with no hazards therefore takes n + 2 cycles. The test program for E13 has 25 instructions and takes 27 cycles; the single-cycle core takes 25. The two extra cycles are paid once per program, not per instruction.

What Does Not Work Yet

Run a real C program on this pipeline and it fails, for two reasons you can see in a waveform:

  • An instruction that needs the result of the one just before it reads the old value. When addi x2, x2, 1 is in X, the instruction that wrote x2 is in W, and its value reaches the register file only at the end of the cycle. Your register file has no write-through, so X reads the old x2. This is the data hazard; A Data Hazard and Its Forwarding Path fixes it.
  • The instruction after a taken branch runs anyway. When a branch is decided in X, F has already fetched the next instruction in memory. Without help, it continues down the pipeline and executes. This is the control hazard; A Control Hazard: Flushing After a Taken Branch fixes it.

That is why E13 is tested with a program written to have neither: no instruction reads a register written by the instruction just before it, and no branch is taken. It is enough to prove the pipeline registers carry the right things.

Where Is This Used?

Pipeline registers and valid bits are in every pipelined processor, and the rule that each stage must only see its own instruction's signals is the first thing processor designers check. Real cores usually have a few more bits per stage: an exception flag, the instruction's PC for debugging, and bits that say why a stage is empty. The structure is the same as yours.

How It Is Tested

The test runs pipe_nohaz.S: 25 instructions with 19 register writes and 4 stores, a mix of ALU instructions, loads and stores and two branches that are not taken, arranged so that no instruction depends on the one just before it. Every register write and store is compared with the reference, and at the end the cycle count must be 27, instructions plus two. That last check is what proves the core is really pipelined: a correct single-cycle core passes every write check and fails here:

Output
FAIL: the program took 25 cycles; the 3-stage pipeline needs 27: cycles = instructions + 2 (two cycles to fill the pipeline)

A core that does not carry the store data into X/W fails at its first store, which writes the value of a later instruction's rs2.

Quick Check

Q: A hazard-free program of 1,000 instructions runs on the 3-stage core. How many cycles does it take, and in which cycle does its last instruction complete?

Show Answer

1,002 cycles. The first instruction completes in cycle 3 and one completes every cycle after that, so the 1,000th completes in cycle 1,002.

Common Mistakes

Mistake #1: Using a signal from the wrong stage

Any signal W uses must come from X/W: rd, the write enable, the store data, the access size, PC + 4. Taking one from X uses the next instruction's value. Go through W's inputs one by one and check where each comes from.

Mistake #2: Forgetting the valid bits

Without valid bits, the pipeline registers hold whatever they hold after reset, and the first two cycles may write registers or memory with garbage. Reset both valid bits and gate every write in W with the X/W valid bit.

Mistake #3: Expecting C to run

After E13, compiled programs fail; that is expected. Use the hazard-free test to check the pipeline registers, and look at a failing C program in the waveform to see the two hazards before you fix them.

Your Turn in the Lab

The exercise opens with a visible test, testbench.v, next to your design: the program pipe_nohaz.S, also checked for its cycle count, run on your core and compared write by write with the reference core, naming the first write that differs. Simulate it as you work and look at the waveform where it fails. When you submit, it runs together with the hidden tests, and your edits to it do not change the grading.

Turn your E12 core into a 3-stage pipeline for E13: add the F/X and X/W registers with valid bits, move each block to its stage, and make W use only X/W. Keep your E12 core too; the last article of this chapter compares the two.

Open exercise E13 in ChipVerify Lab

Summary

  • Pipelining splits an instruction's work into stages that work on different instructions at once, so the clock period is set by the slowest stage instead of the whole path.
  • The 3-stage core is F (fetch), X (decode, register read, ALU, branch decision) and W (data memory, write-back), with your blocks unchanged.
  • F/X carries the instruction, its PC and a valid bit; X/W carries everything W needs about its own instruction and a valid bit.
  • Valid bits start at 0 after reset, fill the pipeline, gate W's writes, and give retire; a hazard-free program of n instructions takes n + 2 cycles.
  • Back-to-back dependences and taken branches do not work yet: they are the data and control hazards of the next two articles.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.