Pipeline Registers and Control That Travels
S2.7 drew four pipeline registers as four arrows. This page opens them. It lists what each one carries, and it follows a control signal from the decoder to the stage that uses it.
You Will Be Able To
- List what each of the four pipeline registers carries, and which stage uses it.
- Explain why a control signal must travel with its instruction.
- Say where the longest path of the pipelined core lies, and what that does to the speed-up.
- Lesson
- S2.24
- Time
- 30 min
- Runs on
- Icarus service
- Check
- 6 questions
In the single-cycle core, the decoder and the register file look at the same instruction. In a pipeline, the decoder is three instructions further on when a result reaches the write port. Something must remember what the decoder said about the instruction that is now in WB.
Before You Start
You need the five stages and the pipeline diagram from S2.7.
You need the control signals of S2.5 and the levels of logic of S2.6. You need to read a struct in SystemVerilog.
| Question | |
|---|---|
| 1 | What does each of the four pipeline registers carry? |
| 2 | Why must a control signal travel with its instruction? |
| 3 | Which control signals does each stage use? |
| 4 | What happens when the write port takes its signals from ID? |
| 5 | Where is the longest path of the pipelined core? |
The Idea: Whatever an Instruction Needs Later, It Carries
Rule 4 of S2.7 said it in one line: a stage works only on what the pipeline register before it holds. This page takes that line seriously.
Think of an add in WB. Its result must go to its rd, and only if its reg_write is 1. The decoder made that reg_write three cycles ago. Now the decoder is busy with another instruction.
So the pipeline keeps the signal. It is stored in ID/EX, beside the instruction. At the next edge it moves to EX/MEM, and then to MEM/WB. The signal travels with its instruction.
The same holds for data. The register values, the immediate, the ALU result and the number rd are all needed in a later stage. Each waits in a pipeline register until its stage comes.
A stage that has used a signal does not pass it on. So the registers get narrower from left to right. That is the picture of this page.
New Words
| Word | Meaning |
|---|---|
| Travels with its instruction | Said of a signal that is stored in each pipeline register beside its instruction, until it is used. |
| Valid bit | One bit in each pipeline register. It is 1 when the register holds an instruction. |
| Bubble | An empty place in the pipeline: a pipeline register whose valid bit is 0. It moves on like an instruction and changes nothing. |
lab_pipeline | The pipeline of the lab: the five stages of ap_rv32_p5, cut down to arithmetic, lw and sw. |
| Speed-up | How many times faster one design is than another, for the same work. |
One Picture: The Registers Get Narrower
- Memory
- Control / FSM
- Processor
- Cycle 8: IF/ID
The register holds the address 18 and the word 00552023. Nothing has looked at the word yet.
- Cycle 9: ID/EX
ID decoded the word in cycle 8. Now ID/EX holds mem_write = 1 and reg_write = 0. It also holds the two register values: 80000000 and 7.
- Cycle 10: EX/MEM
The ALU added the offset 0 to 80000000. EX/MEM holds that address, the value 7 to store, and mem_write = 1. The ALU signals are gone.
- Cycle 11: MEM/WB
The store happened in MEM. MEM/WB still holds reg_write = 0, so WB writes no register.
Read the diagram as text
- IF/ID: the address and the instruction word
- ID/EX: register values, the immediate, rd, every control signal
- EX/MEM: the ALU result, the store data, rd, the signals for MEM and WB
- MEM/WB: the result, the loaded word, rd, the signals for WB
- IF/ID → ID/EX: ID: decode, read registers
- ID/EX → EX/MEM: EX: the ALU
- EX/MEM → MEM/WB: MEM: the data memory
The decoder saw this store once, in cycle 8. Its mem_write was used two cycles later, from EX/MEM.
| Carried | IF/ID | ID/EX | EX/MEM | MEM/WB | Used by |
|---|---|---|---|---|---|
valid, pc | yes | yes | yes | yes | every stage: is there an instruction, and which one |
instr | yes | ID: the decoder and the register numbers | |||
rs1_data, rs2_data, imm | yes | EX: the operands of the ALU | |||
alu_b_select, alu_operation | yes | EX: control signals of the ALU | |||
result | yes | yes | MEM: the address. WB: the value for rd | ||
store_data | yes | MEM: the word to store | |||
mem_write | yes | yes | MEM: this is a store | ||
mem_read | yes | yes | yes | MEM: this is a load. WB: take the loaded word | |
load_data | yes | WB: the loaded word | |||
rd, reg_write | yes | yes | yes | WB: the write port of the register file |
Read from the four structs of the lab. The lab also carries two selects, forward_a and forward_b, in ID/EX. S2.8 explains them.
The Smallest Example, Worked by Hand
Take one instruction: lw x9, 0(x10). The decoder gives it five control signals. Ask of each one: which stage uses it?
| Signal | Value | Used in | In ID/EX | In EX/MEM | In MEM/WB |
|---|---|---|---|---|---|
alu_b_select | 1: the immediate | EX | yes | ||
alu_operation | 0: add | EX | yes | ||
mem_write | 0 | MEM | yes | yes | |
mem_read | 1 | MEM and WB | yes | yes | yes |
reg_write | 1 | WB | yes | yes | yes |
A signal is carried up to the last stage that uses it, and no further.
Count them. ID/EX carries all five. EX/MEM carries three, because EX has used two. MEM/WB carries two.
The number 9, the rd of the load, goes all the way. WB needs it for the write, so all three registers after ID carry it.
The signal mem_read is used twice. MEM uses it to ask the memory. WB uses it to choose the loaded word, not the ALU result.
The Rules
Rule 1: The decoder works once on each instruction, in ID.
It reads the word in IF/ID. Its outputs go into ID/EX at the next clock edge.
Rule 2: A stage takes its control signals from its own pipeline register.
Never from the decoder: the decoder is looking at a younger instruction.
Rule 3: A signal is carried up to the last stage that uses it.
After that it is dropped. So each register is narrower than the one before.
Rule 4: The register number rd travels to WB beside reg_write.
The write port needs both, and both must belong to the instruction in WB.
Rule 5: A bubble moves through the registers like an instruction.
Its valid bit is 0. A stage stores or writes only when the valid bit is 1.
Rule 6: The clock waits for the slowest stage.
In this pipeline that is EX, the stage of the ALU.
Rules 1 to 4 are visible in the lab. Its four registers are four structs, and each struct lists what it carries. The comments say which stage uses a signal.
Sources for These Rules
Pipeline registers and the travelling control signals 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. The contents of the four registers are read from the lab of this page. The registers of the real core are listed in docs/soc/ap_rv32-blueprint.md, section 8.1. The numbers of levels come from S2.6 and from section 14.2 of that document.
Now Run It
The lab at the end of this page runs nine instructions. It follows two of them: the store at address 18 and the load at address 1c. Each line shows the pipeline register that holds one of them.
7 | 18 in IF | the PC is 18, the word is 00552023
8 | 1c in IF | the PC is 1c, the word is 00052483
8 | 18 in ID | IF/ID carries pc=18 instr=00552023
9 | 1c in ID | IF/ID carries pc=1c instr=00052483
9 | 18 in EX | ID/EX carries rs1_data=80000000 rs2_data=7 imm=0 rd=0 alu_b_select=1 alu_operation=0 mem_read=0 mem_write=1 reg_write=0
10 | 1c in EX | ID/EX carries rs1_data=80000000 rs2_data=0 imm=0 rd=9 alu_b_select=1 alu_operation=0 mem_read=1 mem_write=0 reg_write=1
10 | 18 in MEM | EX/MEM carries result=80000000 store_data=7 rd=0 mem_read=0 mem_write=1 reg_write=0
11 | 1c in MEM | EX/MEM carries result=80000000 store_data=0 rd=9 mem_read=1 mem_write=0 reg_write=1
11 | 18 in WB | MEM/WB carries result=80000000 load_data=0 rd=0 mem_read=0 reg_write=0
12 | 1c in WB | MEM/WB carries result=80000000 load_data=7 rd=9 mem_read=1 reg_write=1
x9=7 mem[80000000]=7 cycles=12Rule 1 at work: in cycle 8, IF/ID holds only the address and the word of the store. In cycle 9, ID/EX holds the decoded signals.
Rule 3 at work: follow the store, 18. ID/EX carries nine values for it. EX/MEM carries six, and the two ALU signals are gone. MEM/WB carries five.
Rule 2 at work: look at cycle 10. EX/MEM says mem_write = 1 for the store. In the same cycle, ID/EX says mem_read = 1 for the load. Two stages, two instructions, two sets of signals.
Rule 4 at work: the load carries rd = 9 and reg_write = 1 up to MEM/WB. There, in cycle 12, the loaded word 7 is written to x9.
The store carries reg_write = 0 just as far. That 0 is as important as the 1 of the load: it keeps WB from writing.
Where the Longest Path Moves
S2.6 measured the blocks of the core in levels of logic. In the single-cycle core, one path runs through all of them. In the pipeline, a path ends at the next pipeline register.
| Stage | Its slowest block | Levels |
|---|---|---|
| IF | the instruction memory | 20 (chosen in S2.6) |
| ID | the decoder; the register file beside it has 10 | 11 |
| EX | the ALU | 65 |
| MEM | the data memory | 20 (chosen in S2.6) |
| WB | the load unit | 7 |
Levels of logic of the real blocks, as Yosys counted them for S2.6. The two memories are outside the core: their 20 levels are a choice of that page.
The stages are not equal. EX holds the ALU, and the ALU alone has more levels than the other four stages together. So the clock of the pipeline waits for EX.
Yosys counted the whole cores too. The longest path of ap_rv32_sc has 122 levels. The longest path of ap_rv32_p5 has 80. The pipeline did not cut the path by five. It cut it by about a third.
Now count a program of 100 instructions with no hazard. The single-cycle core needs 100 cycles of 122 levels: 12,200. The pipeline needs 104 cycles of 80 levels: 8,320. The speed-up is about 1.5.
For a program of 7 instructions, the pipeline loses: 854 levels against 880. Its four extra cycles cost more than its shorter clock saves.
The path of 80 levels was not traced for this page. The design notes of the core expect it in EX. It would run through the multiplexers in front of the ALU, the adder, and the data address.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Decode once, carry the signals | Carry the word and decode again in each stage | One decoder. Each stage has its signals at the start of its cycle. The registers hold more bits. |
| Drop a signal after its last use | Carry everything to WB | Fewer flip-flops. Each register must be planned, signal by signal. |
| A valid bit in every register | Fill empty stages with an instruction that does nothing | An empty stage is easy to see and to make. Every write needs one more AND gate. |
| rd travels as a number | Read rd from the word in WB | Five bits in three registers. The real core carries the whole word as well, for its trace. |
| The ALU alone in EX | Cut the ALU into two stages | Five simple stages. The clock waits for a stage about three times longer than any other. |
The Trap: Control Signals Taken from ID
Here is a mistake that looks harmless. The decoder is in ID, and it makes reg_write and rd. The register file is also in ID. So why not wire the two together directly?
The lab below does that. Its result still comes from WB. But the write enable and the register number come from the instruction that is in ID in the same cycle.
The program puts 7, 5, 3 and 1 into x5, x6, x7 and x8. Four instructions that do nothing follow.
cy | IF ID EX MEM WB | what happens
1 | 00 -- -- -- -- |
2 | 04 00 -- -- -- | x5=0
3 | 08 04 00 -- -- | x6=0
4 | 0c 08 04 00 -- | x7=0
5 | 10 0c 08 04 00 | x8=7
6 | 14 10 0c 08 04 |
7 | 18 14 10 0c 08 |
8 | 1c 18 14 10 0c |
9 | 20 1c 18 14 10 |
10 | -- 20 1c 18 14 |
11 | -- -- 20 1c 18 |
12 | -- -- -- 20 1c |
x5=0 (wrong: the instruction set says 7) x6=0 (wrong: the instruction set says 5) x7=0 (wrong: the instruction set says 3) x8=7 (wrong: the instruction set says 1) cycles=12Read row 2. The first instruction is in ID, so x5 is written at once. But its result is still three stages away. The write port takes what WB holds: nothing, a 0.
Read row 5. Now the value 7 of the first instruction is in WB. The instruction in ID is the fourth, with rd = 8. So the 7 goes into x8.
From row 6 on, ID holds instructions that write x0. The values 5, 3 and 1 reach WB and are thrown away.
Every value was computed correctly. Each one was written with the control signals of another instruction. With the signals carried in MEM/WB, the same program gives this last line.
x5=7 (right) x6=5 (right) x7=3 (right) x8=1 (right) cycles=12At Scale
This section looks ahead. The registers of the lab carry what an add, a load and a store need. The real core carries more.
Traps travel too
An instruction that must trap carries that fact, and its cause, down the pipeline. The trap is taken in MEM. S2.13 uses this.
The trace travels too
The real core carries the word, the operand values and the next PC to WB. Its trace record is put together there.
Registers that must hold or empty
On this page every register takes a new value at every edge. S2.9 makes two of them hold. S2.10 empties two of them.
A balanced pipeline
A design team moves logic between stages until they are nearly equal. Then the speed-up comes closer to the number of stages. That is common practice.
Try It
Run the lab. For the store at 18, find the cycle in which mem_write is used, and the register that holds it then.
Follow the add at address 10 as well: add its address to the function follows. Which signals does it carry to MEM/WB?
Look at rs1_data of that add in ID/EX. It is not the value the ALU used. S2.8 explains why the result is still right.
7 | 18 in IF | the PC is 18, the word is 00552023 8 | 1c in IF | the PC is 1c, the word is 00052483 8 | 18 in ID | IF/ID carries pc=18 instr=00552023 9 | 1c in ID | IF/ID carries pc=1c instr=00052483 9 | 18 in EX | ID/EX carries rs1_data=80000000 rs2_data=7 imm=0 rd=0 alu_b_select=1 alu_operation=0 mem_read=0 mem_write=1 reg_write=0 10 | 1c in EX | ID/EX carries rs1_data=80000000 rs2_data=0 imm=0 rd=9 alu_b_select=1 alu_operation=0 mem_read=1 mem_write=0 reg_write=1 10 | 18 in MEM | EX/MEM carries result=80000000 store_data=7 rd=0 mem_read=0 mem_write=1 reg_write=0 11 | 1c in MEM | EX/MEM carries result=80000000 store_data=0 rd=9 mem_read=1 mem_write=0 reg_write=1 11 | 18 in WB | MEM/WB carries result=80000000 load_data=0 rd=0 mem_read=0 reg_write=0 12 | 1c in WB | MEM/WB carries result=80000000 load_data=7 rd=9 mem_read=1 reg_write=1 x9=7 mem[80000000]=7 cycles=12Read 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 Wire a control signal from the decoder straight to the stage that uses it.
The decoder holds a younger instruction by then. The signal belongs to that one.
Fix Store the signal in each pipeline register up to the stage that uses it.
Wrong Carry reg_write to WB and take rd from the instruction word in ID.
Then the right result is written to the register of another instruction.
Fix Let rd travel beside reg_write, through the same registers.
Wrong Expect five stages to make the clock five times faster.
The clock waits for the slowest stage, and the stages are not equal.
Fix Find the stage with the most levels. Here it is EX, with the ALU.
Remember
- A stage works on its own pipeline register. Whatever it needs must have travelled there with the instruction.
- The decoder works once, in ID. Its signals are stored in ID/EX and passed on until their last use.
- The number rd and the signal reg_write travel together to WB.
- The clock waits for the slowest stage. Here that is EX. The longest path of the real core has 80 levels, against 122 for the single cycle.