Deciding Earlier, Jumps and Returns
A branch decided in EX loses two instructions. Decided in ID, it would lose one. This page builds that in the lab, and finds out why the real core does not do it.
You Will Be Able To
- Explain what a decision in ID saves, and the two things it costs.
- Count the cycles of a loop for a decision in ID and for a decision in EX.
- Say what a jal and a jalr cost in ap_rv32, and what each one needs to know its target.
- Lesson
- S2.26
- Time
- 30 min
- Runs on
- Icarus service
- Check
- 6 questions
The penalty of a branch is the number of stages between the fetch and the decision. So the obvious repair is to decide sooner. The catch is what a stage knows at that point.
Before You Start
You need the flush, the redirect and the penalty of 2 cycles from S2.10.
You need forwarding from S2.8, the stall from S2.9, and the critical path from S2.6.
You need jal, jalr and the return address from S2.3.
| Question | |
|---|---|
| 1 | How many instructions does a taken branch lose when it is decided in ID? |
| 2 | Why must a branch in ID sometimes wait? |
| 3 | When is the decision in ID slower than the decision in EX? |
| 4 | What does the earlier decision do to the clock period? |
| 5 | What do a jal and a jalr cost in ap_rv32? |
The Idea: One Stage Earlier, One Instruction Less
A branch needs two things: a compare of two registers, and a target. Put the compare and the target adder into ID. Then a taken branch is known one cycle sooner.
At that moment only one younger instruction exists, the one in IF. The flush removes one instruction, and the penalty is 1 cycle.
But ID is early. The forwarding multiplexers are in EX. In ID, a value comes from the register file, and a result that is still in EX or MEM is not there.
There are two ways out. The first is to forward into ID as well. Then the ALU and the compare work one after the other in one cycle, and the clock gets longer.
The second way is to wait. The branch stays in ID until its registers are written. The lab on this page does that. Each cycle of waiting eats what the early decision saved.
So the early decision wins only when the branch does not depend on the instructions right before it.
New Words
| Word | Meaning |
|---|---|
| Decision in ID | A design in which the compare and the target adder of a branch are in ID, not in EX. |
| Decision in EX | The design of ap_rv32: the branch is decided behind the forwarding multiplexers. |
| Indirect jump | A jump whose target comes from a register, like a jalr. The jump back from a function, jalr x0, 0(x1), is one. |
One Picture: Where the Branch Is Decided
- Memory
- Control / FSM
- Processor
- Decision in EX
The branch is in EX. Its two values come from the forwarding multiplexers, so they are always the newest. Two younger instructions are in ID and IF.
- Decision in ID
The branch is in ID. Only one younger instruction exists, in IF. A taken branch flushes that one.
- What ID has to work with
In ID the two values come from the register file. A result that is still in EX or MEM is not there yet.
Read the diagram as text
- IF: the PC and the instruction memory
- ID: decode, read two registers
- EX: the ALU, behind the forwarding multiplexers
- Decision in EX: ap_rv32: the compare and the target adder are here
- Decision in ID: the lab variant: the compare and the target adder are here
- IF → ID: IF/ID
- ID → EX: ID/EX
- EX → Decision in EX: two forwarded values
- ID → Decision in ID: two values of the register file
- Decision in EX → IF: redirect: 2 instructions are behind
- Decision in ID → IF: redirect: 1 instruction is behind
One stage earlier means one instruction less to throw away. It also means older values to decide with.
| Decision in EX | Decision in ID | |
|---|---|---|
| Instructions behind the branch at the decision | 2: in ID and IF | 1: in IF |
| Cycles lost when taken | 2 | 1 |
| Where its two values come from | the forwarding multiplexers | the register file |
| A value still in EX or MEM | is forwarded | is not there: wait, or forward into ID |
The Smallest Example, Worked by Hand
Take addi x5, x5, -1 and then bne x5, x0, 0x08. The branch reads x5. Ask in which cycle the branch can be decided, in each design.
| Cycle | The addi | Decision in EX: the branch | Decision in ID: the branch |
|---|---|---|---|
| 1 | in EX: the new x5 is worked out | in ID | in ID: x5 is not written yet. It waits |
| 2 | in MEM | in EX: x5 is forwarded from MEM. Decided | in ID: still not written. It waits |
| 3 | in WB: x5 is written | in MEM | in ID: the register file gives the new x5. Decided |
With the decision in ID, the branch is decided one cycle later than with the decision in EX.
Decided in EX, a taken branch here loses 2 cycles. Decided in ID, it waits 2 cycles and then loses 1: that is 3.
Now put two other instructions between the addi and the branch. When the branch is in ID, the addi is in WB. Nothing waits, and the early decision loses 1 cycle against 2.
The Rules
Rule 1: Decided in ID, a taken branch flushes one instruction.
Only the instruction in IF is younger than the branch.
Rule 2: A branch in ID reads the register file.
The lab forwards nothing into ID. A read that meets a write in the same cycle gets the new value, as in S2.25.
Rule 3: The branch waits while a writer of its registers is in EX or MEM.
That is 2 cycles for a writer right before it, 1 cycle with one instruction between, and 0 after that.
Rule 4: A jal needs no register.
Its target is its own address plus an immediate. In ID it never waits.
Rule 5: A jalr needs a register for its target.
It is an indirect jump. In ID it waits like a branch.
Rule 6: In ap_rv32 every branch, jal and jalr is decided in EX.
Each costs 2 cycles when it leaves the line, and nothing ever waits for a decision.
Sources for These Rules
Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.8, part "Reducing the Delay of Branches". It moves the decision to the decode stage for a penalty of one instruction. It names the two costs: forwarding into that stage, and a stall when the value is not ready. Harris and Harris (ARM edition), section 7.5.3, says that an earlier decision may lengthen the cycle. The lab on this page forwards nothing into ID, which is a choice of the lab. The core ap_rv32 decides in EX: docs/soc/ap_rv32-blueprint.md, section 8.4. The levels of logic are the block depths of S2.6, added by hand. No synthesis was run for the lab variant.
Now Run It
The lab at the end of this page is the lab of S2.10 with the decision in ID. Its loop counts x5 down first, then runs two other instructions, then branches.
First the real core, which decides in EX.
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<WB
6 | 14 10 0c 08 04 | x6=0 a<WB b<MEM
7 | 18 14 10 0c 08 | x5=2 a<MEM
8 | 1c 18 14 10 0c | x6=2 go 08
9 | 08 -- -- 14 10 | x8=2
10 | 0c 08 -- -- 14 |
11 | 10 0c 08 -- -- |
12 | 14 10 0c 08 -- | b<MEM
13 | 18 14 10 0c 08 | x5=1 a<MEM
14 | 1c 18 14 10 0c | x6=3 go 08
15 | 08 -- -- 14 10 | x8=3
16 | 0c 08 -- -- 14 |
17 | 10 0c 08 -- -- |
18 | 14 10 0c 08 -- | b<MEM
19 | 18 14 10 0c 08 | x5=0 a<MEM
20 | 1c 18 14 10 0c | x6=3
21 | -- 1c 18 14 10 | x8=3
22 | -- -- 1c 18 14 |
23 | -- -- -- 1c 18 | x7=4
x6=3 x7=4 redirects=2 cycles=23The branch, at 14, is in EX in rows 8, 14 and 20. Fifteen instructions are executed and two branches are taken: 15 plus 4 plus 4 is 23.
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<WB
6 | 14 10 0c 08 04 | x6=0 a<WB b<MEM
7 | 18 14 10 0c 08 | x5=2 a<MEM go 08
8 | 08 -- 14 10 0c | x6=2
9 | 0c 08 -- 14 10 | x8=2
10 | 10 0c 08 -- 14 |
11 | 14 10 0c 08 -- | b<MEM
12 | 18 14 10 0c 08 | x5=1 a<MEM go 08
13 | 08 -- 14 10 0c | x6=3
14 | 0c 08 -- 14 10 | x8=3
15 | 10 0c 08 -- 14 |
16 | 14 10 0c 08 -- | b<MEM
17 | 18 14 10 0c 08 | x5=0 a<MEM
18 | 1c 18 14 10 0c | x6=3
19 | -- 1c 18 14 10 | x8=3
20 | -- -- 1c 18 14 |
21 | -- -- -- 1c 18 | x7=4
x6=3 x7=4 redirects=2 cycles=21Rule 1 at work: read row 7. The branch, 14, is in ID, and the note says go 08. In row 8, IF holds 08 and only ID holds two dashes.
Rule 2 at work: row 7 also writes x5 = 2. The instruction that counts x5 down is in WB, and the branch in ID reads the new value in that same cycle.
Rule 3 at work: no row says stall. The run takes 21 cycles: 15 plus 4, plus 1 for each of the two taken branches.
| Instruction | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|
| 00: addi x5, x0, 3 | WB | |||||||
| 04: addi x6, x0, 0 | MEM | WB | ||||||
| 08: addi x5, x5, -1 | EX | MEM | WB | |||||
| 0c: add x6, x6, x5 | ID | EX | MEM | WB | ||||
| 10: addi x8, x6, 0 | IF | ID | EX | MEM | WB | |||
| 14: bne x5, x0, 0x08 | IF | ID | EX | MEM | WB | |||
| 18: addi x7, x6, 1 | IF | gone | ||||||
| 08: addi x5, x5, -1 | IF | ID | EX | MEM | WB | |||
| 0c: add x6, x6, x5 | IF | ID | EX | MEM | ||||
| 10: addi x8, x6, 0 | IF | ID | EX | |||||
| 14: bne x5, x0, 0x08 | IF | ID | ||||||
| 18: addi x7, x6, 1 | IF |
Drawn from the recorded rows. Only the instruction at 18 is thrown away. The branch itself moves on to EX and does nothing more.
Now a call and its return, first on the real core.
cy | IF ID EX MEM WB | what happens
1 | 00 -- -- -- -- |
2 | 04 00 -- -- -- |
3 | 08 04 00 -- -- |
4 | 0c 08 04 00 -- | go 14
5 | 14 -- -- 04 00 | x10=5
6 | 18 14 -- -- 04 | x1=8
7 | -- 18 14 -- -- |
8 | -- -- 18 14 -- | go 08
9 | 08 -- -- 18 14 | x10=a
10 | 0c 08 -- -- 18 |
11 | 10 0c 08 -- -- |
12 | 14 10 0c 08 -- |
13 | 18 14 10 0c 08 | x11=b
x10=10 x11=11 x1=8 redirects=2 cycles=13Rule 6 at work: the jal, at 04, is in EX in row 4: go 14. The jalr, at 18, is in EX in row 8: go 08. Five instructions are executed: 5 plus 4 plus 4 is 13.
Row 6 writes x1 = 8. That is the return address. The jalr uses it in row 8.
cy | IF ID EX MEM WB | what happens
1 | 00 -- -- -- -- |
2 | 04 00 -- -- -- |
3 | 08 04 00 -- -- | go 14
4 | 14 -- 04 00 -- |
5 | 18 14 -- 04 00 | x10=5
6 | -- 18 14 -- 04 | x1=8 go 08
7 | 08 -- 18 14 -- |
8 | 0c 08 -- 18 14 | x10=a
9 | 10 0c 08 -- 18 |
10 | 14 10 0c 08 -- |
11 | 18 14 10 0c 08 | x11=b
x10=10 x11=11 x1=8 redirects=2 cycles=11Rules 4 and 5 at work: the jal goes in row 3, while it is in ID. The jalr goes in row 6, in the cycle in which x1 is written. The run takes 11 cycles.
Why It Is Built This Way
The clock period is set by the slowest stage. S2.6 measured the blocks of the core in levels of gates. Add them up for the stage that decides.
| Design | The path | Levels | Against the 65 of the ALU |
|---|---|---|---|
| Decision in EX | the compare beside the ALU: 19; the target adder beside it: 57 | 65 | no change |
| Decision in ID, nothing forwarded | the immediate at level 11, then the target adder: 57 | 68 | 3 levels longer |
| Decision in ID, forwarded from EX | the ALU, then the compare: 65 plus 19 | 84 | 19 levels longer |
Sums of the block depths of S2.6, made by hand. The register read and the compare, 10 plus 19, are the shorter path in ID. No synthesis was run for the two designs in ID.
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| ap_rv32 decides in EX | Decide in ID | The clock stays as it is, and no branch ever waits. Each taken branch loses 2 cycles, not 1. |
| One place for every decision | jal in ID, branches in EX | One redirect and one flush to build and to verify. A jal loses 2 cycles where 1 would do. |
| The lab waits in ID | Forward into ID | No longer path. A branch right behind its writer loses 3 cycles. |
| The compare uses forwarded values | Compare what the register file holds | The branch always sees the newest value. The compare sits behind the forwarding multiplexers. |
The Trap: An Earlier Decision Can Be Slower
The loop of S2.10 has its branch right behind addi x5, x5, -1. That is how loops are commonly written. Run it with the decision in ID.
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 stall
7 | 14 10 -- 0c 08 | x6=3 stall
8 | 14 10 -- -- 0c | x5=2 go 08
9 | 08 -- 10 -- -- |
10 | 0c 08 -- 10 -- |
11 | 10 0c 08 -- 10 |
12 | 14 10 0c 08 -- | stall
13 | 14 10 -- 0c 08 | x6=5 stall
14 | 14 10 -- -- 0c | x5=1 go 08
15 | 08 -- 10 -- -- |
16 | 0c 08 -- 10 -- |
17 | 10 0c 08 -- 10 |
18 | 14 10 0c 08 -- | stall
19 | 14 10 -- 0c 08 | x6=6 stall
20 | 14 10 -- -- 0c | x5=0
21 | 18 14 10 -- -- |
22 | -- 18 14 10 -- |
23 | -- -- 18 14 10 |
24 | -- -- -- 18 14 | x7=7
x6=6 x7=7 redirects=2 cycles=24Read rows 6 and 7. The branch, 10, is in ID and the note says stall, twice. The instruction that writes x5 is in EX, then in MEM.
In row 8 that instruction is in WB. The branch reads x5 = 2 and goes. It lost 2 cycles waiting and 1 for the flush.
The run takes 24 cycles. With the decision in EX, the same loop took 20 in S2.10. The design that looked faster is slower here.
Even the last round pays. In rows 18 and 19 the branch waits again, and then it is not taken.
| Instruction | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|
| 00: addi x5, x0, 3 | MEM | WB | ||||||
| 04: addi x6, x0, 0 | EX | MEM | WB | |||||
| 08: add x6, x6, x5 | ID | EX | MEM | WB | ||||
| 0c: addi x5, x5, -1 | IF | ID | EX | MEM | WB | |||
| 10: bne x5, x0, 0x08 | IF | ID | ID | ID | EX | MEM | WB | |
| 14: addi x7, x6, 1 | IF | IF | IF | gone | ||||
| 08: add x6, x6, x5 | IF | ID | EX | |||||
| 0c: addi x5, x5, -1 | IF | ID | ||||||
| 10: bne x5, x0, 0x08 | IF |
Drawn from the recorded rows. The branch at 10 is in ID for three cycles.
At Scale
This section looks ahead. A wait and a flush are both lost cycles. What differs is when they are paid.
Measured on the real core
The checks of ap_rv32_p5 measure a taken branch, a jal and a jalr on its pipeline view. Each costs 2 cycles.
A return has many targets
A function is called from many places, so its return goes to a different address each time. S2.28 gives the fetch a small stack to guess it.
Guessing instead of deciding
A fetch that guesses the direction and the target in IF loses nothing when it is right. S2.11 and the pages after it build such guesses.
Compilers know the pipeline
A compiler can move the update of a loop counter away from the branch. That is common practice where a core decides early.
Try It
Run the lab. Find the two rows with a redirect, and the one bubble behind each.
Count the rows in which the branch at 14 is in ID. Why is it there only once in each round?
In the bench, find the line that makes a branch wait. Which two places of a writer does it test?
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<WB 6 | 14 10 0c 08 04 | x6=0 a<WB b<MEM 7 | 18 14 10 0c 08 | x5=2 a<MEM go 08 8 | 08 -- 14 10 0c | x6=2 9 | 0c 08 -- 14 10 | x8=2 10 | 10 0c 08 -- 14 | 11 | 14 10 0c 08 -- | b<MEM 12 | 18 14 10 0c 08 | x5=1 a<MEM go 08 13 | 08 -- 14 10 0c | x6=3 14 | 0c 08 -- 14 10 | x8=3 15 | 10 0c 08 -- 14 | 16 | 14 10 0c 08 -- | b<MEM 17 | 18 14 10 0c 08 | x5=0 a<MEM 18 | 1c 18 14 10 0c | x6=3 19 | -- 1c 18 14 10 | x8=3 20 | -- -- 1c 18 14 | 21 | -- -- -- 1c 18 | x7=4 x6=3 x7=4 redirects=2 cycles=21Read 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 1 lost cycle for every taken branch that is decided in ID.
A branch that reads a register written just before it must wait first.
Fix Add the cycles of waiting: 2 for a writer right before the branch, 1 with one instruction between.
Wrong Treat a jalr like a jal.
A jal has its target in its own word. A jalr takes it from a register, which may still be on its way.
Fix A jal can go as soon as it is decoded. A jalr depends on a register, like a branch.
Wrong Judge a design by its penalty alone.
Waits and a longer clock cost time as well.
Fix Count all the cycles of a real program, and multiply by the clock period.
Remember
- Decided in ID, a taken branch flushes 1 instruction, not 2.
- A branch in ID has only the register file. It waits for a writer in EX or MEM, or the design forwards into ID and lengthens the clock.
- The loop of S2.10 takes 24 cycles with the decision in ID and 20 with the decision in EX. The loop of this page takes 21 against 23.
- ap_rv32 decides branches, jal and jalr in EX. Each costs 2 cycles, measured on the real core.