The Five-Stage Pipeline
The single-cycle core finishes one instruction before it starts the next. A pipeline starts the next one a cycle later, while the first is still at work. This page draws that, and reads the drawing.
You Will Be Able To
- Name the five stages of the pipeline and say what each does.
- Read a pipeline diagram: which instruction is in which stage in which cycle.
- Count the cycles of a program on a five-stage pipeline with no hazards.
- Lesson
- S2.7
- Time
- 30 min
- Runs on
- Icarus service
- Check
- 6 questions
S2.6 found the price of one cycle for each instruction: the clock waits for the slowest one. Yet for most of that cycle, most of the datapath is idle. The register file is done early, and the data memory waits until late. A pipeline puts the idle parts to work on the next instructions.
Before You Start
You need the single-cycle datapath from S2.4 and its memory part from S2.21.
You need the critical path and the cycle time from S2.6. You need to read a little SystemVerilog.
| Question | |
|---|---|
| 1 | What are the five stages, and what does each one do? |
| 2 | What stands between two stages? |
| 3 | How do you read a pipeline diagram? |
| 4 | How many cycles do n instructions take? |
| 5 | What goes wrong when an instruction reads a register that is still on its way? |
The Idea: Start the Next Instruction Before This One Ends
An instruction does its work in a fixed order. It is fetched. Its registers are read. The ALU works. The data memory is used. The result is written. The single-cycle core does all five steps in one long cycle.
A pipeline cuts that work into steps and puts a register between them. Each step is a stage of the pipeline, or a stage for short. A stage takes one short clock cycle.
Now an instruction needs five cycles, not one. That sounds worse. But when the first instruction moves to the second stage, the first stage is free. The next instruction enters it at once.
So five instructions are at work at the same time, each in its own stage. One instruction still finishes in every cycle. And the cycle is short: the clock waits for one stage only.
The five-stage build of ap_rv32 is a module named ap_rv32_p5. Its source is not on this page, because a later problem asks you to write part of it. The lab here is that pipeline cut down, and it runs the same way.
New Words
| Word | Meaning |
|---|---|
| Pipeline | A datapath cut into steps, with a register between them, so that several instructions are at work at once. |
| Stage of a pipeline | One step of a pipeline. It takes one clock cycle. |
| IF | Instruction fetch. The stage that reads the instruction word at the PC. |
| ID | Instruction decode. The stage that makes the control signals and reads the two registers. |
| EX | Execute. The stage of the ALU. |
| MEM | Memory access. The stage in which a load or a store uses the data memory. |
| WB | Write back. The stage that writes the result to the register rd. |
| Pipeline register | The register between two stages. It is named after both. |
| IF/ID | The pipeline register between IF and ID. ID works on what it holds. |
| ID/EX | The pipeline register between ID and EX. EX works on what it holds. |
| EX/MEM | The pipeline register between EX and MEM. MEM works on what it holds. |
| MEM/WB | The pipeline register between MEM and WB. WB works on what it holds. |
| Pipeline diagram | A table with one row for each instruction and one column for each cycle. A cell names the stage. |
| Pipeline grid | What the lab prints: one row for each cycle, one column for each stage. A cell holds an address. |
ap_rv32_p5 | The five-stage build of ap_rv32, the processor of this track. |
| Hazard | A case in which a pipeline would give another result than the instruction set says. |
One Picture: An Instruction Moves One Stage in Each Cycle
- Memory
- Control / FSM
- Processor
- Cycle 5: IF
The PC holds 10 in hex. The instruction memory returns the word of add x9, x5, x5. At the clock edge it goes into IF/ID.
- Cycle 6: ID
ID works on what IF/ID holds. It decodes the word and reads x5, which holds 7. The values go into ID/EX.
- Cycle 7: EX
EX works on what ID/EX holds. The ALU adds 7 and 7. The result, e in hex, goes into EX/MEM.
- Cycle 8: MEM
An add has nothing to do in MEM. It passes through, and its result moves into MEM/WB.
- Cycle 9: WB
WB hands the result to the write port of the register file. At the clock edge, x9 takes e.
Read the diagram as text
- IF: instruction fetch: read the word at the PC
- ID: instruction decode: make the control signals, read two registers
- EX: execute: the ALU
- MEM: memory access: a load or a store uses the data memory
- WB: write back: the result goes to the register rd
- IF → ID: IF/ID
- ID → EX: ID/EX
- EX → MEM: EX/MEM
- MEM → WB: MEM/WB
- WB → ID: write to the register file
Each arrow between two stages is a pipeline register. In every one of these five cycles, the other four stages worked on other instructions.
| Stage | Its work | The blocks of S2.4 and S2.21 in it |
|---|---|---|
| IF | read the word at the PC, and add 4 to the PC | the instruction memory, the adder of the PC |
| ID | decode the word, make the immediate, read rs1 and rs2 | the decoder, the register file |
| EX | choose the second operand and run the ALU | the operand select, the ALU |
| MEM | load or store, for the instructions that do so | the data memory |
| WB | choose the value for rd and write it | the write port of the register file |
The register file is in two stages. Its two read ports are in ID. Its write port belongs to WB.
The Smallest Example, Worked by Hand
Take three instructions: addi x5, x0, 7, then addi x6, x0, 5, then addi x7, x0, 3. They sit at the addresses 00, 04 and 08. None of them reads a register that another one writes.
Draw a pipeline diagram. Give each instruction a row and each cycle a column. The first instruction enters IF in cycle 1 and moves one stage in each cycle.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| 00: addi x5, x0, 7 | IF | ID | EX | MEM | WB | ||
| 04: addi x6, x0, 5 | IF | ID | EX | MEM | WB | ||
| 08: addi x7, x0, 3 | IF | ID | EX | MEM | WB |
Read a row to follow one instruction. Read a column to see what the whole pipeline does in one cycle.
Each row is the row above it, moved one cycle to the right. That is the whole method.
Now read column 3. The first instruction is in EX, the second in ID, the third in IF. Three instructions are at work in one cycle.
Count the cycles. The first instruction is done after cycle 5. The second is done one cycle later, and the third one more. Three instructions take 7 cycles: 3 plus 4.
The Rules
Rule 1: Every instruction passes through all five stages, in order.
An add has no work in MEM. It still spends one cycle there.
Rule 2: An instruction moves one stage at each clock edge.
On this page nothing ever waits. S2.9 shows the one case where an instruction must.
Rule 3: A stage holds one instruction at most.
A new instruction enters IF in every cycle.
Rule 4: A stage works only on what the pipeline register before it holds.
So whatever a later stage needs must travel in the pipeline registers.
Rule 5: Registers are read in ID and written in WB.
WB is three stages after ID.
Rule 6: With no hazard, n instructions take n plus 4 cycles.
The first needs 5 cycles. Each one after it ends one cycle later.
Rule 6 is where the speed comes from. On the single-cycle core, n instructions take n long cycles. Here they take n plus 4 short ones.
Sources for These Rules
The five stages are described here as textbooks commonly describe them. The plan of this track points to Patterson and Hennessy, section 4.6, and to Harris and Harris, section 7.5. Those places are not checked. Every grid on this page is recorded from the lab and from the real core ap_rv32_p5 in this repository. The stages of that core are in docs/soc/ap_rv32-blueprint.md, section 8.1.
Now Run It
The lab at the end of this page runs seven instructions and an ebreak. It prints a pipeline grid: one row for each cycle. The five columns are the five stages.
A cell holds the address of the instruction in that stage, in hex. Two dashes mean that the stage is empty. The last column shows a register write, with its value in hex.
cy | IF ID EX MEM WB | what happens
1 | 00 -- -- -- -- |
2 | 04 00 -- -- -- |
3 | 08 04 00 -- -- |
4 | 0c 08 04 00 -- |
5 | 10 0c 08 04 00 | x5=7
6 | 14 10 0c 08 04 | x6=5
7 | 18 14 10 0c 08 | x7=3
8 | 1c 18 14 10 0c | x8=1
9 | -- 1c 18 14 10 | x9=e
10 | -- -- 1c 18 14 | x11=2
11 | -- -- -- 1c 18 | x12=4
x9=14 (right) x11=2 (right) x12=4 (right) cycles=11Read row 5 from right to left. The instruction at 00 is in WB and writes x5. Behind it, 04 is in MEM, 08 in EX and 0c in ID. The instruction at 10 is being fetched.
Rule 2 at work: follow the address 10. It is in IF in row 5, and in WB in row 9. Each row moves it one column to the right.
Rule 5 at work: the instruction at 10 is add x9, x5, x5. It reads x5 in row 6. Row 5 wrote x5, so the read is one cycle late enough.
Rule 6 at work: the seven instructions end in row 11. That is 7 plus 4. The ebreak, at address 1c, stops the run when it reaches MEM.
The same run as a pipeline diagram looks like this. It is drawn from the recorded rows.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 00: addi x5, x0, 7 | IF | ID | EX | MEM | WB | ||||||
| 04: addi x6, x0, 5 | IF | ID | EX | MEM | WB | ||||||
| 08: addi x7, x0, 3 | IF | ID | EX | MEM | WB | ||||||
| 0c: addi x8, x0, 1 | IF | ID | EX | MEM | WB | ||||||
| 10: add x9, x5, x5 | IF | ID | EX | MEM | WB | ||||||
| 14: sub x11, x5, x6 | IF | ID | EX | MEM | WB | ||||||
| 18: xor x12, x7, x5 | IF | ID | EX | MEM | WB | ||||||
| 1c: ebreak | IF | ID | EX | MEM |
The grid has a row for each cycle. This diagram has a column for each cycle. Both show the same run.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Five stages | Two or three stages | Each stage is short, so the clock is fast. There are four registers to pay for, and more hazards. |
| Every instruction visits all five stages | An add skips MEM | Instructions never overtake each other. An add ends one cycle later than it could. |
| Registers are read in ID | Read them in EX, beside the ALU | The read has a stage of its own. A value written by an instruction close ahead is not there yet. |
| The write is in the last stage | Write an ALU result from EX at once | All writes happen in program order, in one place. Results wait two more stages. |
| A register after every stage | Registers only where the path is long | The stages are easy to tell apart. Every instruction takes five cycles from start to end. |
One thing differs in the real core. Its memories take the address at a clock edge, as a real SRAM does. So ap_rv32_p5 sends the address of the next instruction one cycle early. The stages and the grid stay the same.
The Trap: The Register That Is Still on Its Way
The program of the lab was chosen with care. No instruction reads a register that an instruction close ahead of it writes. Real programs are not like that.
Here is a short one. It puts 7 into x5 and 5 into x6. Then add x7, x5, x6 uses both at once. The instruction set says that x7 is 12.
cy | IF ID EX MEM WB | what happens
1 | 00 -- -- -- -- |
2 | 04 00 -- -- -- |
3 | 08 04 00 -- -- |
4 | 0c 08 04 00 -- |
5 | 10 0c 08 04 00 | x5=7
6 | -- 10 0c 08 04 | x6=5
7 | -- -- 10 0c 08 | x7=0
8 | -- -- -- 10 0c | x8=0
x7=0 (wrong: the instruction set says 12) x8=0 (wrong: the instruction set says 7) cycles=8The grid looks healthy. Every instruction moves one stage in each cycle, and the run takes 4 plus 4 cycles. Only the values are wrong.
Follow the add, at address 08. It is in ID in row 4, and there it reads x5 and x6. But x5 is written in row 5, and x6 in row 6. The add read two registers that still held 0.
This is a hazard. Rule 5 says why it exists: the write is three stages after the read. A reader needs three other instructions between it and the writer, or it reads an old value.
The real core has no such limit. It hands a result straight to the instruction that needs it. S2.8 builds that, and the same program then gives 12.
At Scale
This section looks ahead. The pipeline of this page moves instructions. The real core must also keep them right.
What the registers carry
Each pipeline register holds what the later stages need. S2.24 lists the contents of all four, and follows the control signals.
Handing a result on
A result that is still on its way can be handed to the instruction that needs it. S2.8 and S2.25 build that.
Waiting a cycle
A loaded value arrives too late even for that. One instruction must wait a cycle. S2.9 counts the cost.
Branches
A branch is decided in EX. By then, two more instructions have been fetched. S2.10 throws them away.
Deeper pipelines
Fast processors cut the work into more stages than five. That is common practice, and no source in the library was checked for it.
Try It
Run the lab. Find the row in which all five stages hold an instruction of the program.
Add one more addi before the ebreak, with its row of the program. How many rows does the grid have now?
Change the program so that the fifth instruction reads x8. What does x9 become, and why?
cy | IF ID EX MEM WB | what happens 1 | 00 -- -- -- -- | 2 | 04 00 -- -- -- | 3 | 08 04 00 -- -- | 4 | 0c 08 04 00 -- | 5 | 10 0c 08 04 00 | x5=7 6 | 14 10 0c 08 04 | x6=5 7 | 18 14 10 0c 08 | x7=3 8 | 1c 18 14 10 0c | x8=1 9 | -- 1c 18 14 10 | x9=e 10 | -- -- 1c 18 14 | x11=2 11 | -- -- -- 1c 18 | x12=4 x9=14 (right) x11=2 (right) x12=4 (right) cycles=11Read included file: ap_rv32_pkg.sv
// ap_rv32_pkg: the named constants of the ap_rv32 core.
// Opcodes, function fields, control-signal values, trap causes and CSR addresses.
// Used by both builds (ap_rv32_sc and ap_rv32). Taught in S2.2 and S2.5.
// Blueprint: docs/soc/ap_rv32-blueprint.md sections 4 (instructions), 5 (CSRs) and 7 (traps).
package ap_rv32_pkg;
// ---- Opcodes: instruction bits 6:0
localparam logic [6:0] OPCODE_LUI = 7'b0110111;
localparam logic [6:0] OPCODE_AUIPC = 7'b0010111;
localparam logic [6:0] OPCODE_JAL = 7'b1101111;
localparam logic [6:0] OPCODE_JALR = 7'b1100111;
localparam logic [6:0] OPCODE_BRANCH = 7'b1100011;
localparam logic [6:0] OPCODE_LOAD = 7'b0000011;
localparam logic [6:0] OPCODE_STORE = 7'b0100011;
localparam logic [6:0] OPCODE_OP_IMM = 7'b0010011;
localparam logic [6:0] OPCODE_OP = 7'b0110011;
localparam logic [6:0] OPCODE_MISC_MEM = 7'b0001111;
localparam logic [6:0] OPCODE_SYSTEM = 7'b1110011;
// ---- funct3 (instruction bits 14:12) of the branches
localparam logic [2:0] FUNCT3_BEQ = 3'b000;
localparam logic [2:0] FUNCT3_BNE = 3'b001;
localparam logic [2:0] FUNCT3_BLT = 3'b100;
localparam logic [2:0] FUNCT3_BGE = 3'b101;
localparam logic [2:0] FUNCT3_BLTU = 3'b110;
localparam logic [2:0] FUNCT3_BGEU = 3'b111;
// ---- funct3 of the arithmetic instructions (OP and OP_IMM) that the decoder must tell apart.
// All eight values are the low three bits of alu_operation, below.
localparam logic [2:0] FUNCT3_ADD_SUB = 3'b000;
localparam logic [2:0] FUNCT3_SLL = 3'b001;
localparam logic [2:0] FUNCT3_SRL_SRA = 3'b101;
// ---- funct3 of JALR and FENCE. Loads and stores: bits 1:0 are mem_size, bit 2 is unsigned.
// SYSTEM: 000 is ecall, ebreak, mret, wfi; else bits 1:0 are csr_operation, bit 2 is immediate.
localparam logic [2:0] FUNCT3_JALR = 3'b000;
localparam logic [2:0] FUNCT3_FENCE = 3'b000;
// ---- funct7 (instruction bits 31:25)
localparam logic [6:0] FUNCT7_BASE = 7'b0000000;
localparam logic [6:0] FUNCT7_ALT = 7'b0100000; // sub, sra, srai
// ---- Instructions matched on all 32 bits
localparam logic [31:0] INSTR_ECALL = 32'h0000_0073;
localparam logic [31:0] INSTR_EBREAK = 32'h0010_0073;
localparam logic [31:0] INSTR_MRET = 32'h3020_0073;
localparam logic [31:0] INSTR_WFI = 32'h1050_0073;
// ---- imm_select: which immediate format the instruction carries
localparam logic [2:0] IMM_I = 3'd0;
localparam logic [2:0] IMM_S = 3'd1;
localparam logic [2:0] IMM_B = 3'd2;
localparam logic [2:0] IMM_U = 3'd3;
localparam logic [2:0] IMM_J = 3'd4;
// ---- alu_operation: bit 3 is instruction bit 30, bits 2:0 are funct3
localparam logic [3:0] ALU_ADD = 4'b0000;
localparam logic [3:0] ALU_SUB = 4'b1000;
localparam logic [3:0] ALU_SLL = 4'b0001;
localparam logic [3:0] ALU_SLT = 4'b0010;
localparam logic [3:0] ALU_SLTU = 4'b0011;
localparam logic [3:0] ALU_XOR = 4'b0100;
localparam logic [3:0] ALU_SRL = 4'b0101;
localparam logic [3:0] ALU_SRA = 4'b1101;
localparam logic [3:0] ALU_OR = 4'b0110;
localparam logic [3:0] ALU_AND = 4'b0111;
// ---- alu_a_select and alu_b_select: the two ALU operands
localparam logic [1:0] ALU_A_RS1 = 2'd0;
localparam logic [1:0] ALU_A_PC = 2'd1;
localparam logic [1:0] ALU_A_ZERO = 2'd2;
localparam logic ALU_B_RS2 = 1'b0;
localparam logic ALU_B_IMM = 1'b1;
// ---- result_select: the value written to rd
localparam logic [1:0] RESULT_ALU = 2'd0;
localparam logic [1:0] RESULT_MEM = 2'd1;
localparam logic [1:0] RESULT_PC_PLUS_4 = 2'd2;
localparam logic [1:0] RESULT_CSR = 2'd3;
// ---- mem_size: bytes moved by a load or store (as AHB hsize)
localparam logic [1:0] SIZE_BYTE = 2'd0;
localparam logic [1:0] SIZE_HALF = 2'd1;
localparam logic [1:0] SIZE_WORD = 2'd2;
// ---- csr_operation: what a CSR instruction does with its operand
localparam logic [1:0] CSR_OPERATION_NONE = 2'd0; // not a CSR instruction
localparam logic [1:0] CSR_OPERATION_WRITE = 2'd1;
localparam logic [1:0] CSR_OPERATION_SET = 2'd2;
localparam logic [1:0] CSR_OPERATION_CLEAR = 2'd3;
// ---- Trap causes: the value written to mcause (bit 31 marks an interrupt)
localparam logic [31:0] CAUSE_INSTR_ADDR_MISALIGNED = 32'd0;
localparam logic [31:0] CAUSE_INSTR_ACCESS_FAULT = 32'd1;
localparam logic [31:0] CAUSE_ILLEGAL_INSTRUCTION = 32'd2;
localparam logic [31:0] CAUSE_BREAKPOINT = 32'd3;
localparam logic [31:0] CAUSE_LOAD_ADDR_MISALIGNED = 32'd4;
localparam logic [31:0] CAUSE_LOAD_ACCESS_FAULT = 32'd5;
localparam logic [31:0] CAUSE_STORE_ADDR_MISALIGNED = 32'd6;
localparam logic [31:0] CAUSE_STORE_ACCESS_FAULT = 32'd7;
localparam logic [31:0] CAUSE_ECALL_FROM_M = 32'd11;
localparam logic [31:0] CAUSE_IRQ_SOFTWARE = 32'h8000_0003;
localparam logic [31:0] CAUSE_IRQ_TIMER = 32'h8000_0007;
localparam logic [31:0] CAUSE_IRQ_EXTERNAL = 32'h8000_000b;
// ---- Bit positions in mstatus, and in mie and mip
localparam int MSTATUS_MIE_BIT = 3;
localparam int MSTATUS_MPIE_BIT = 7;
localparam int IRQ_SOFTWARE_BIT = 3;
localparam int IRQ_TIMER_BIT = 7;
localparam int IRQ_EXTERNAL_BIT = 11;
// ---- CSR addresses (blueprint 5.1)
localparam logic [11:0] CSR_MSTATUS = 12'h300;
localparam logic [11:0] CSR_MISA = 12'h301;
localparam logic [11:0] CSR_MIE = 12'h304;
localparam logic [11:0] CSR_MTVEC = 12'h305;
localparam logic [11:0] CSR_MSTATUSH = 12'h310;
localparam logic [11:0] CSR_MSCRATCH = 12'h340;
localparam logic [11:0] CSR_MEPC = 12'h341;
localparam logic [11:0] CSR_MCAUSE = 12'h342;
localparam logic [11:0] CSR_MTVAL = 12'h343;
localparam logic [11:0] CSR_MIP = 12'h344;
localparam logic [11:0] CSR_MCYCLE = 12'hb00;
localparam logic [11:0] CSR_MINSTRET = 12'hb02;
localparam logic [11:0] CSR_MCYCLEH = 12'hb80;
localparam logic [11:0] CSR_MINSTRETH = 12'hb82;
localparam logic [11:0] CSR_MVENDORID = 12'hf11;
localparam logic [11:0] CSR_MARCHID = 12'hf12;
localparam logic [11:0] CSR_MIMPID = 12'hf13;
localparam logic [11:0] CSR_MHARTID = 12'hf14;
localparam logic [11:0] CSR_MCONFIGPTR = 12'hf15;
// Performance monitors 3 to 31 exist and read 0: first and last address of each range
localparam logic [11:0] CSR_MHPMEVENT3 = 12'h323;
localparam logic [11:0] CSR_MHPMEVENT31 = 12'h33f;
localparam logic [11:0] CSR_MHPMCOUNTER3 = 12'hb03;
localparam logic [11:0] CSR_MHPMCOUNTER31 = 12'hb1f;
localparam logic [11:0] CSR_MHPMCOUNTER3H = 12'hb83;
localparam logic [11:0] CSR_MHPMCOUNTER31H = 12'hb9f;
// Address bits 11:10 of a read-only CSR
localparam logic [1:0] CSR_READ_ONLY = 2'b11;
// ---- CSR values and write masks (a 1 marks a bit that software can change)
localparam logic [31:0] MSTATUS_RESET = 32'h0000_1800; // MPP = 11: machine mode only
localparam logic [31:0] MSTATUS_WRITE_MASK = 32'h0000_0088; // MIE and MPIE
localparam logic [31:0] MISA_VALUE = 32'h4000_0100; // 32 bits, extension I
localparam logic [31:0] MIMPID_VALUE = 32'h0001_0000; // ap_rv32 version 1.0
localparam logic [31:0] MIE_WRITE_MASK = 32'h0000_0888; // MSIE, MTIE and MEIE
localparam logic [31:0] MTVEC_WRITE_MASK = 32'hffff_fffd; // BASE and MODE bit 0
localparam logic [31:0] MEPC_WRITE_MASK = 32'hffff_fffc; // instructions are 4-byte aligned
localparam logic [31:0] ALL_BITS = 32'hffff_ffff;
// ---- Data bits that a byte and a half cover, from bit 0
localparam logic [31:0] BYTE_BITS = 32'h0000_00ff;
localparam logic [31:0] HALF_BITS = 32'h0000_ffff;
// ---- Other
localparam logic [1:0] PRIV_MACHINE = 2'b11; // the only privilege level in v1.0
localparam int REGISTER_COUNT = 32;
endpackage
Read included file: ap_rv32_p5_pkg.sv
// ap_rv32_p5_pkg: the named constants that only the five-stage build needs.
// The forwarding selects of the forward unit, and the AHB-Lite codes of the two bus ports.
// Used by the five-stage build (ap_rv32). Taught in S2.8 and S2.16.
// Blueprint: docs/soc/ap_rv32-blueprint.md sections 8.2 (forwarding) and 3.2 (bus ports).
package ap_rv32_p5_pkg;
// ---- forward_a and forward_b: where the value of a register operand comes from
localparam logic [1:0] FORWARD_NONE = 2'd0; // the value read from the register file in ID
localparam logic [1:0] FORWARD_MEM = 2'd1; // the result of the instruction now in MEM
localparam logic [1:0] FORWARD_WB = 2'd2; // the value that the instruction now in WB writes
// ---- htrans: the two transfer types the core uses (every transfer is a single transfer)
localparam logic [1:0] HTRANS_IDLE = 2'b00;
localparam logic [1:0] HTRANS_NONSEQ = 2'b10;
// ---- hburst and hprot: constant on each port
localparam logic [2:0] HBURST_SINGLE = 3'b000;
localparam logic [3:0] HPROT_INSTRUCTION = 4'b0010; // opcode fetch, privileged
localparam logic [3:0] HPROT_DATA = 4'b0011; // data access, privileged
endpackage
Read included file: ap_rv32_regfile.sv
// ap_rv32_regfile: the register file.
// 31 registers, x1 to x31, and x0 which always reads 0. Two read ports, one write port.
// A read is combinational; a write happens at the clock edge. All registers reset to 0.
// Used by both builds. Taught in S2.4. Blueprint: sections 2.3 and 6.
module ap_rv32_regfile (
input logic clk, // clock
input logic rst_n, // reset, active low
input logic [4:0] rs1_addr, // first register to read
output logic [31:0] rs1_data, // its value
input logic [4:0] rs2_addr, // second register to read
output logic [31:0] rs2_data, // its value
input logic write_enable, // write rd at this clock edge
input logic [4:0] rd_addr, // register to write; a write to x0 is discarded
input logic [31:0] rd_data // value to write
);
import ap_rv32_pkg::*;
logic [31:0] registers_q [1:REGISTER_COUNT-1]; // x1 to x31; x0 has no storage
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
for (int i = 1; i < REGISTER_COUNT; i++) registers_q[i] <= '0;
end else if (write_enable && rd_addr != '0) begin
registers_q[rd_addr] <= rd_data;
end
end
assign rs1_data = (rs1_addr == '0) ? '0 : registers_q[rs1_addr];
assign rs2_data = (rs2_addr == '0) ? '0 : registers_q[rs2_addr];
endmodule
Read included file: ap_rv32_alu.sv
// ap_rv32_alu: the arithmetic and logic unit.
// Add, subtract, shifts, compares and bitwise logic on two 32-bit operands.
// Used by both builds. Taught in S2.4. Blueprint: section 2.3.
module ap_rv32_alu (
input logic [3:0] alu_operation, // operation: one of the ALU_* constants
input logic [31:0] operand_a, // first operand
input logic [31:0] operand_b, // second operand; shifts use only its bits 4:0
output logic [31:0] result // result of the operation
);
import ap_rv32_pkg::*;
logic [4:0] shift_amount; // number of bit positions to shift by
assign shift_amount = operand_b[4:0];
always_comb begin
result = '0;
unique case (alu_operation)
ALU_ADD: result = operand_a + operand_b;
ALU_SUB: result = operand_a - operand_b;
ALU_SLL: result = operand_a << shift_amount;
ALU_SLT: result = {31'b0, $signed(operand_a) < $signed(operand_b)};
ALU_SLTU: result = {31'b0, operand_a < operand_b};
ALU_XOR: result = operand_a ^ operand_b;
ALU_SRL: result = operand_a >> shift_amount;
ALU_SRA: result = $signed(operand_a) >>> shift_amount;
ALU_OR: result = operand_a | operand_b;
ALU_AND: result = operand_a & operand_b;
default: result = '0;
endcase
end
endmodule
Check Yourself
Common Mistakes
Wrong Say that a pipelined core needs five cycles for each instruction, so it is five times slower.
Five instructions are at work at once. One of them ends in every cycle.
Fix Count n plus 4 cycles for n instructions.
Wrong Read a row of the pipeline grid as the life of one instruction.
A row of the grid is one cycle. It shows five different instructions.
Fix Follow one address from column to column, one row down each time.
Wrong Expect a register to hold its new value as soon as the ALU has made it.
The value is written in WB, two stages after EX.
Fix Find the row in which the instruction is in WB. The register changes at the end of that cycle.
Remember
- The five stages are IF, ID, EX, MEM and WB. A pipeline register stands between every two of them.
- An instruction moves one stage at each clock edge. Five instructions are at work at once.
- With no hazard, n instructions take n plus 4 cycles.
- Registers are read in ID and written in WB. A reader too close behind a writer gets an old value: a hazard.