Control Hazards: Flush and the Cost of a Branch
A pipeline fetches a new instruction in every cycle. After a branch, it does not yet know which one. It fetches anyway, and sometimes it must throw the work away.
You Will Be Able To
- Say in which stage ap_rv32 decides a branch, and what has been fetched by then.
- Explain what a flush does, and read it in a pipeline grid.
- Count the cycles of a program from its instructions and its taken branches.
- Lesson
- S2.10
- Time
- 30 min
- Runs on
- Icarus service
- Check
- 6 questions
A data hazard is about a value that is late. This hazard is about the next address. The fetch needs it in every cycle, and a branch gives it two cycles after it was fetched.
Before You Start
You need the pipeline grid, the bubble and the valid bit from S2.7 and S2.24.
You need the branch compare and the target adder from S2.21, and the hazard unit from S2.9.
| Question | |
|---|---|
| 1 | In which stage is a branch decided in ap_rv32? |
| 2 | What has the pipeline fetched by then? |
| 3 | What does a flush do to those instructions? |
| 4 | What does a taken branch cost, and a branch that is not taken? |
| 5 | How many cycles does a loop of three rounds take? |
The Idea: Fetch on, and Throw Away When Wrong
A branch has two possible next instructions. One is the next in line, at the PC plus 4. The other is at the target.
Which one is right depends on a compare of two registers. In ap_rv32 the compare is in EX. So the answer exists two cycles after the branch was fetched.
The fetch cannot stand still for two cycles at every branch. So it goes on in line, as if the branch were not taken. This guess has a name: predict not taken.
When the branch is not taken, the guess was right. Nothing is lost.
When the branch is taken, two instructions are in the pipeline that must not run. They are on the wrong path. The pipeline turns both into bubbles. That is a flush.
In the same clock edge the PC takes the target. This change of the PC is a redirect. The right instruction is fetched in the next cycle.
A hazard about which instruction comes next is a control hazard. Its price here is two cycles for each taken branch.
New Words
| Word | Meaning |
|---|---|
| Control hazard | The pipeline has fetched instructions before it knows which instruction comes next. |
| Predict not taken | To fetch on at the PC plus 4 behind every branch, before the branch is decided. |
| Wrong path | The instructions fetched behind a branch that turn out not to follow it. |
| Flush | To turn an instruction in the pipeline into a bubble, because it is on the wrong path. |
| Redirect | The PC takes a target in place of the next address in line. |
| Branch penalty | The cycles lost when the guess about a branch was wrong. In ap_rv32: 2. |
redirect, kill | In the lab: the bench says that the PC takes the target, and that IF/ID and ID/EX take bubbles. |
One Picture: Two Instructions Are Thrown Away
- Memory
- Control / FSM
- Processor
- Cycle 6: the branch is decided
The branch is in EX. The compare says that x5 equals x6: taken. The target adder gives 18.
- Cycle 6: two instructions are already behind it
ID holds the instruction at 10. IF fetches the one at 14. Both were fetched as if the branch were not taken.
- Cycle 6, at the clock edge
The PC takes the target, 18, and not the next address in line. IF/ID and ID/EX take bubbles. The two instructions are gone.
- Cycle 7: the right path starts
IF fetches the instruction at 18. ID and EX hold bubbles. The branch itself moves on to MEM.
Read the diagram as text
- IF: the PC and the instruction memory
- ID: decode, read two registers
- EX: the branch compare and the target adder
- MEM: the data memory
- Hazard unit: sees that the branch in EX is taken
- IF → ID: IF/ID
- ID → EX: ID/EX
- EX → MEM: EX/MEM
- EX → Hazard unit: taken, with the target
- Hazard unit → IF: redirect: the PC takes 18
- Hazard unit → ID: flush: bubbles into IF/ID and ID/EX
The decision comes in EX. By then two younger instructions are in the pipeline, and a taken branch removes both.
| 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, 7 | IF | ID | EX | MEM | WB | ||||||
| 08: bne x5, x6, 0x18 | IF | ID | EX | MEM | WB | ||||||
| 0c: beq x5, x6, 0x18 | IF | ID | EX | MEM | WB | ||||||
| 10: addi x7, x0, 1 | IF | ID | gone | ||||||||
| 14: addi x8, x0, 1 | IF | gone | |||||||||
| 18: add x9, x5, x6 | IF | ID | EX | MEM | WB | ||||||
| 1c: ebreak | IF | ID | EX | MEM |
Drawn from the recorded rows. The instructions at 10 and 14 enter the pipeline and are gone after cycle 6. They never reach EX.
The Smallest Example, Worked by Hand
Take the branch beq x5, x6, 0x18 at address 0c. Both registers hold 7, so it is taken. Follow it, and the fetch behind it.
| Cycle | The branch at 0c | What IF fetches | Is that the right instruction? |
|---|---|---|---|
| 4 | in IF | 0c: the branch itself | yes |
| 5 | in ID | 10: the next in line | not known yet |
| 6 | in EX: taken, target 18 | 14: the next in line | no, and neither was 10 |
| 7 | in MEM | 18: the target | yes |
Two fetches, in cycles 5 and 6, were made before the decision. Both were wrong.
Without the branch, the instruction at 18 would follow two instructions later. With a taken branch, it is fetched in cycle 7 instead of cycle 5. Two cycles are lost.
Now suppose the registers differ. The branch is not taken, and the fetches of cycles 5 and 6 were right. Nothing is lost.
The Rules
Rule 1: Behind a branch, the fetch goes on at the PC plus 4.
That is the guess: not taken. No cycle waits for the decision.
Rule 2: The branch is decided in EX.
The compare needs both registers after forwarding. The target adder works beside it.
Rule 3: A branch that is not taken costs nothing.
The two instructions behind it were the right ones.
Rule 4: A taken branch flushes the instructions in IF and ID.
IF/ID and ID/EX take bubbles at the clock edge. No register and no memory word was changed by them.
Rule 5: At the same clock edge, the PC takes the target.
The redirect and the flush belong together. Each alone gives a wrong program.
Rule 6: A program takes n plus 4 cycles, plus 2 for each taken branch.
Here n counts the instructions that are executed, so an instruction in a loop counts once for each round.
Sources for These Rules
Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.8. It has the control hazard, the assumption that a branch is not taken, and the flush. Harris and Harris, Digital Design and Computer Architecture (ARM edition), section 7.5.3, decides in the execute stage. It flushes two instructions. Both books use other processors than ours. The stage of the decision in ap_rv32 and its cost are in docs/soc/ap_rv32-blueprint.md, sections 8.4 and 14.2. Every grid on this page is recorded from the lab and from the real core.
Now Run It
The lab at the end of this page runs two branches. The first, bne x5, x6, 0x18 at address 08, is not taken. The second, beq x5, x6, 0x18 at 0c, is taken.
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 a<WB b<MEM
6 | 14 10 0c 08 04 | x6=7 b<WB go 18
7 | 18 -- -- 0c 08 |
8 | 1c 18 -- -- 0c |
9 | -- 1c 18 -- -- |
10 | -- -- 1c 18 -- |
11 | -- -- -- 1c 18 | x9=e
x7=0 x8=0 x9=14 redirects=1 cycles=11Rule 3 at work: read row 5. The first branch, 08, is in EX. No note says go. In row 6 everything has moved on as usual.
Rule 2 at work: read row 6. The second branch, 0c, is in EX. The note b<WB says that its compare took x6 by forwarding.
Rules 4 and 5 at work: the note go 18 in row 6 is the redirect. In row 7, IF holds 18, and ID and EX hold two dashes each.
Follow the two bubbles. They are in EX and MEM in row 8, and in MEM and WB in row 9. The last line shows x7 and x8 at 0: the two instructions never ran.
Rule 6 at work: five instructions are executed, and one branch is taken. The run ends in row 11: 5 plus 4 plus 2.
Now a loop. It adds x5 to x6 and counts x5 down from 3. The branch bne x5, x0, 0x08 at address 10 goes back while x5 is not 0.
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=3 a<MEM b<WB
6 | 14 10 0c 08 04 | x6=0
7 | 18 14 10 0c 08 | x6=3 a<MEM go 08
8 | 08 -- -- 10 0c | x5=2
9 | 0c 08 -- -- 10 |
10 | 10 0c 08 -- -- |
11 | 14 10 0c 08 -- |
12 | 18 14 10 0c 08 | x6=5 a<MEM go 08
13 | 08 -- -- 10 0c | x5=1
14 | 0c 08 -- -- 10 |
15 | 10 0c 08 -- -- |
16 | 14 10 0c 08 -- |
17 | 18 14 10 0c 08 | x6=6 a<MEM
18 | -- 18 14 10 0c | x5=0
19 | -- -- 18 14 10 |
20 | -- -- -- 18 14 | x7=7
x6=6 x7=7 redirects=2 cycles=20The branch is in EX in rows 7, 12 and 17. In rows 7 and 12 it is taken: go 08. In row 17, x5 is 0 and the loop ends.
Count it. Twelve instructions are executed: two before the loop, three in each of three rounds, one after. Two branches are taken. 12 plus 4 plus 4 is 20.
| Instruction | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
|---|---|---|---|---|---|---|---|---|---|
| 00: addi x5, x0, 3 | WB | ||||||||
| 04: addi x6, x0, 0 | MEM | WB | |||||||
| 08: add x6, x6, x5 | EX | MEM | WB | ||||||
| 0c: addi x5, x5, -1 | ID | EX | MEM | WB | |||||
| 10: bne x5, x0, 0x08 | IF | ID | EX | MEM | WB | ||||
| 14: addi x7, x6, 1 | IF | ID | gone | ||||||
| 18: ebreak | IF | gone | |||||||
| 08: add x6, x6, x5 | IF | ID | EX | MEM | WB | ||||
| 0c: addi x5, x5, -1 | IF | ID | EX | MEM | WB | ||||
| 10: bne x5, x0, 0x08 | IF | ID | EX | MEM | |||||
| 14: addi x7, x6, 1 | IF | ID | gone | ||||||
| 18: ebreak | IF | gone | |||||||
| 08: add x6, x6, x5 | IF |
Drawn from the recorded rows. Behind each taken branch, the instructions at 14 and 18 are fetched and thrown away.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Fetch on behind a branch | Stall the fetch until the branch is decided | A branch that is not taken costs 0 cycles, not 2. A flush is needed. |
| Guess "not taken" | Guess "taken" | The next address is the PC plus 4, known at once. A target would need an adder before the guess can be used. |
| Decide in EX | Decide in MEM, after the ALU | Two instructions are lost, not three. The compare and the target adder must fit into EX. |
| Flush by clearing the valid bit | Replace the instruction word with a nop | One bit for each stage. Every write must test the valid bit, as for a bubble of a stall. |
| Flush only IF and ID | Flush the branch as well | The branch itself is on the right path. A jump must still write its return address. |
Why can the wrong-path instructions be removed without harm? Because nothing permanent happens before MEM. A store writes in MEM, and a register is written in WB. An instruction in IF or ID has changed nothing yet.
The Trap: A Redirect Without a Flush
What if only the PC is redirected? The target is fetched at the right time. It looks as if the branch worked.
But the two instructions behind the branch are still in the pipeline. Nothing stops them. They move on to EX, MEM and WB like any other instruction.
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 a<WB b<MEM
6 | 14 10 0c 08 04 | x6=7 b<WB go 18
7 | 18 14 10 0c 08 |
8 | 1c 18 14 10 0c |
9 | -- 1c 18 14 10 | x7=1
10 | -- -- 1c 18 14 | x8=1
11 | -- -- -- 1c 18 | x9=e
x7=1 (wrong: the instruction set says 0) x8=1 (wrong: the instruction set says 0) x9=14 (right) redirects=1 cycles=11Read row 7. IF holds 18, as it should. But ID holds 14 and EX holds 10. Rows 9 and 10 write x7 = 1 and x8 = 1.
The program said: when x5 equals x6, skip these two instructions. They ran all the same. The run has the same 11 rows, and nothing warned.
So the redirect and the flush are one action. A test that only checks where the PC goes will not find this fault.
At Scale
This section looks ahead. Two cycles are small. Branches are frequent, so the two cycles add up.
Measured on the real core
The checks of ap_rv32_p5 measure the cost on its pipeline view. A taken branch costs 2 cycles. A branch that is not taken costs 0.
A loop is the worst case for this guess
The branch at the end of a loop is taken in every round but the last. Predict not taken is wrong almost every time. S2.11 makes a better guess.
Deeper pipelines lose more
A core with more stages before the decision flushes more instructions. That is common practice in fast cores, and it is why they predict branches.
Deciding earlier
A decision in ID would lose one instruction, not two. S2.26 builds that in the lab and shows what it costs.
Try It
Run the lab. Find the row with the redirect, and the two bubbles in the row after it.
Set FLUSH to 0 and run again. Which two registers are wrong at the end, and which instructions wrote them?
Change the first row of the program to addi x5, x0, 8: its word is 0080_0293 and its immediate is 8. Which branch is taken now? How many cycles does the run take?
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 a<WB b<MEM 6 | 14 10 0c 08 04 | x6=7 b<WB go 18 7 | 18 -- -- 0c 08 | 8 | 1c 18 -- -- 0c | 9 | -- 1c 18 -- -- | 10 | -- -- 1c 18 -- | 11 | -- -- -- 1c 18 | x9=e x7=0 x8=0 x9=14 redirects=1 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
Read included file: ap_rv32_branch.sv
// ap_rv32_branch: the branch compare.
// Decides whether the condition of a branch instruction is true.
// Used by both builds. Taught in S2.4 and S2.10. Blueprint: section 2.3.
module ap_rv32_branch (
input logic [2:0] funct3, // funct3 of the branch instruction: which condition
input logic [31:0] rs1_data, // first value compared
input logic [31:0] rs2_data, // second value compared
output logic condition // the condition is true: the branch is taken
);
import ap_rv32_pkg::*;
always_comb begin
condition = 1'b0;
unique case (funct3)
FUNCT3_BEQ: condition = (rs1_data == rs2_data);
FUNCT3_BNE: condition = (rs1_data != rs2_data);
FUNCT3_BLT: condition = ($signed(rs1_data) < $signed(rs2_data));
FUNCT3_BGE: condition = ($signed(rs1_data) >= $signed(rs2_data));
FUNCT3_BLTU: condition = (rs1_data < rs2_data);
FUNCT3_BGEU: condition = (rs1_data >= rs2_data);
default: condition = 1'b0;
endcase
end
endmodule
Check Yourself
Common Mistakes
Wrong Count two lost cycles for every branch.
A branch that is not taken loses nothing. The instructions behind it were the right ones.
Fix Count the taken branches.
Wrong Redirect the PC and leave the two fetched instructions alone.
They are in the pipeline and will run. The program skips nothing.
Fix Flush IF/ID and ID/EX in the same clock edge.
Wrong Flush the branch itself with the two instructions behind it.
The branch is on the right path. A jump that is flushed would lose its return address.
Fix Flush only the instructions younger than the branch.
Remember
- ap_rv32 decides a branch in EX. Until then it fetches on at the PC plus 4: predict not taken.
- A taken branch redirects the PC and flushes the two instructions in IF and ID, in one clock edge.
- A taken branch costs 2 cycles. A branch that is not taken costs 0. Both are measured on the real core.
- Cycles are n plus 4, plus 2 for each taken branch, with n the instructions that are executed.