Load-Use Stalls and the Hazard Unit
Forwarding hands over a value that exists. The value of a load does not exist until the data memory has answered. For the instruction right behind a load, that is one cycle too late.
You Will Be Able To
- Explain why the value of a load cannot be forwarded to the instruction right behind it.
- Name the three things a stall does, and read a bubble in a pipeline grid.
- Count the cycles of a program from its number of instructions and its load-use pairs.
- Lesson
- S2.9
- Time
- 30 min
- Runs on
- Icarus service
- Check
- 6 questions
Every data hazard so far was solved for free, and this one is not. A loaded word comes out of the data memory at the end of MEM. The next instruction wants it at the start of that same cycle. No wire can carry a value back to an earlier moment.
Before You Start
You need forwarding from MEM and from WB from S2.8, and the rules of S2.25.
You need the bubble and the valid bit from S2.24, and the path of a load from S2.21.
| Question | |
|---|---|
| 1 | Why can a load not forward its value to the next instruction in time? |
| 2 | What three things does a stall do? |
| 3 | Where does the reader get the value after the stall? |
| 4 | What does one load-use pair cost? |
| 5 | When does an instruction behind a load not have to wait? |
The Idea: When the Value Is Late, the Reader Waits
An add has its result at the end of EX. A load has only an address then. Its value comes out of the data memory at the end of MEM, one stage later.
Now put a reader right behind the load. When the load is in MEM, the reader is in EX. It needs the value at the start of that cycle. The memory gives it at the end.
This is the load-use hazard. Forwarding cannot solve it, because the value is not in any pipeline register yet.
So the reader must wait one cycle. The pipeline does three things for that. It keeps the PC. It keeps IF/ID, so the reader stays in ID. And it sends a bubble into EX in place of the reader.
A register that keeps its value at a clock edge is said to hold. So the PC and IF/ID hold for one cycle.
Together these are called a stall. One cycle later the load is in WB, its value is in MEM/WB, and forwarding from WB does the rest.
The block that sees the hazard and orders the stall is the hazard unit. It looks at ID and at EX, one stage earlier than the forwarding unit.
New Words
| Word | Meaning |
|---|---|
| Load-use hazard | A data hazard in which the writer is a load and the reader is the instruction right behind it. |
| Load-use pair | A load and, right behind it, an instruction that reads the loaded register. |
| Stall | One cycle in which an instruction stays in its stage, and a bubble moves on in its place. |
| Hazard unit | The block that sees a hazard and tells the pipeline registers to hold or to take a bubble. |
wait_d | In the lab: 1 in the cycle in which the instruction in ID must wait. The bench gives it. |
One Picture: The Reader Waits, a Bubble Moves On
- Memory
- Control / FSM
- Processor
- Cycle 7: the hazard is seen
The load is in EX and works out its address. The add is in ID and reads x6. The hazard unit sees a load in EX whose rd is the rs1 of ID.
- Cycle 7, at the clock edge
The PC and IF/ID keep their values. ID/EX takes a bubble instead of the add. The load moves on to MEM as usual.
- Cycle 8: the wait
The add is in ID for a second cycle. EX holds the bubble and does nothing. In MEM, the data memory returns the word 7.
- Cycle 9: the value is there
The add is in EX. The load is in WB, and MEM/WB holds the 7. The forwarding unit hands it over: forward from WB.
Read the diagram as text
- IF: the PC and the instruction memory
- ID: the reader: add x7, x6, x5
- EX: the ALU, behind the forwarding multiplexers
- MEM: the data memory: a loaded word appears here
- WB: the loaded word, one cycle later
- Hazard unit: sees a load in EX and its reader in ID
- IF → ID: IF/ID
- ID → EX: ID/EX
- EX → MEM: EX/MEM
- MEM → WB: MEM/WB
- Hazard unit → IF: hold the PC and IF/ID
- Hazard unit → EX: a bubble into ID/EX
- WB → EX: forward from WB
The stall does not fetch the value. It moves the reader one cycle later, to where forwarding can reach the value.
| Part | In a normal cycle | In a stall |
|---|---|---|
| The PC | takes the PC plus 4 | holds |
| IF/ID | takes the fetched instruction | holds: the reader stays in ID |
| ID/EX | takes the instruction from ID | takes a bubble: its valid bit is 0 |
| EX/MEM and MEM/WB | take the instruction of the stage before | the same: the load moves on |
Everything in front of the reader waits. Everything behind it moves.
The Smallest Example, Worked by Hand
Take two instructions: lw x6, 0(x10) and then add x7, x6, x5. In the lab, the load is in EX in cycle 7. Ask when the value of x6 exists, and when the add needs it.
| Cycle | The load | The value of x6 | The add, with no stall | The add, with a stall |
|---|---|---|---|---|
| 7 | in EX: the address | does not exist | in ID | in ID |
| 8 | in MEM: the memory answers | exists at the end of the cycle | in EX: needs it at the start | in ID again |
| 9 | in WB | in MEM/WB, all cycle long | in MEM | in EX: takes it from WB |
Without the stall, the add would need the value almost a whole cycle before it exists.
With the stall, the add is in EX in cycle 9 instead of cycle 8. Everything behind it is one cycle late as well. That one cycle is the whole price.
The Rules
Rule 1: A load has its value at the end of MEM.
From the next cycle on, the value is in MEM/WB and can be forwarded.
Rule 2: The hazard unit compares the rd of a load in EX with the registers that the instruction in ID reads.
A register that the instruction does not read does not count. Neither does x0.
Rule 3: On a match, the PC and IF/ID hold, and ID/EX takes a bubble.
That is the stall. It lasts one cycle.
Rule 4: The load moves on during the stall.
So in the next cycle the hazard is gone: EX holds a bubble, not a load.
Rule 5: After the stall, the reader takes the value by forwarding from WB.
The stall and the forwarding work together. Neither is enough alone.
Rule 6: A program takes n plus 4 cycles, plus one for each load-use pair.
One instruction between the load and its reader is enough: then nothing waits.
Sources for These Rules
The load-use hazard and the stall are described here as textbooks commonly describe them. The plan of this track points to Patterson and Hennessy, section 4.7, and to Harris and Harris, section 7.5. Those places are not checked. The stall of the real core is in docs/soc/ap_rv32-blueprint.md, section 8.3, and its measured cost in section 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 seven instructions. The fifth is lw x6, 0(x10), at address 10. The sixth, at 14, is add x7, x6, x5.
cy | IF ID EX MEM WB | what happens
1 | 00 -- -- -- -- |
2 | 04 00 -- -- -- |
3 | 08 04 00 -- -- |
4 | 0c 08 04 00 -- | a<MEM
5 | 10 0c 08 04 00 | x10=1
6 | 14 10 0c 08 04 | x10=80000000 a<WB b<MEM
7 | 18 14 10 0c 08 | x5=7 stall
8 | 18 14 -- 10 0c |
9 | 1c 18 14 -- 10 | x6=7 a<WB
10 | -- 1c 18 14 -- | a<MEM
11 | -- -- 1c 18 14 | x7=e
12 | -- -- -- 1c 18 | x8=7
x7=14 x8=7 cycles=12Rule 2 at work: read row 7. The load, 10, is in EX. The add, 14, is in ID. The note says stall.
Rule 3 at work: read row 8. IF and ID hold the same two addresses as in row 7. EX holds two dashes: that is the bubble.
Rule 4 at work: in row 8 the load is in MEM. It was not held. In row 9 it is in WB and writes x6 = 7.
Rule 5 at work: in row 9 the add is in EX, with the note a<WB. Follow the bubble too. It is in MEM in row 9 and in WB in row 10.
Rule 6 at work: seven instructions, one load-use pair. The run ends in row 12: 7 plus 4 plus 1.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 00: addi x10, x0, 1 | IF | ID | EX | MEM | WB | |||||||
| 04: slli x10, x10, 31 | IF | ID | EX: a from MEM | MEM | WB | |||||||
| 08: addi x5, x0, 7 | IF | ID | EX | MEM | WB | |||||||
| 0c: sw x5, 0(x10) | IF | ID | EX: a from WB, b from MEM | MEM | WB | |||||||
| 10: lw x6, 0(x10) | IF | ID | EX | MEM | WB | |||||||
| 14: add x7, x6, x5 | IF | ID | ID | EX: a from WB | MEM | WB | ||||||
| bubble | EX | MEM | WB | |||||||||
| 18: sub x8, x7, x6 | IF | IF | ID | EX: a from MEM | MEM | WB | ||||||
| 1c: ebreak | IF | ID | EX | MEM |
Drawn from the recorded rows. The add is in ID twice. The bubble has a row of its own, and it never enters IF or ID.
Now put one instruction between the load and the add: addi x9, x5, 1. It does not read x6.
cy | IF ID EX MEM WB | what happens
1 | 00 -- -- -- -- |
2 | 04 00 -- -- -- |
3 | 08 04 00 -- -- |
4 | 0c 08 04 00 -- | a<MEM
5 | 10 0c 08 04 00 | x10=1
6 | 14 10 0c 08 04 | x10=80000000 a<WB b<MEM
7 | 18 14 10 0c 08 | x5=7
8 | 1c 18 14 10 0c |
9 | 20 1c 18 14 10 | x6=7 a<WB
10 | -- 20 1c 18 14 | x9=8 a<MEM
11 | -- -- 20 1c 18 | x7=e
12 | -- -- -- 20 1c | x8=7
x7=14 x8=7 cycles=12No row says stall. When the add is in EX, in row 10, the load is already in WB. The program has one instruction more and takes the same 12 cycles.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Stall one cycle, then forward from WB | Wait until the load has written the register | One lost cycle, not two. The stall depends on the forwarding path. |
| Decide in ID, while the load is in EX | Decide in EX, when the value is found missing | The reader never enters EX with a wrong value. ID needs to know which registers an instruction reads. |
| A bubble: only the valid bit is cleared | Clear every field of ID/EX | One bit. Every write and every memory access must test the valid bit. |
| No stall when the register is not read | Compare the fields of every instruction | No cycle is lost to bits that only look like a register number. The decoder must give two more signals. |
| A load is not sped up | Read the memory in EX, with the address from a short adder | The clock stays as it is. One cycle is lost for each load-use pair. |
One case more waits in ap_rv32: a store that stores the value loaded right before it. Its data could be handed over in MEM, one stage later. The core has no such path, so the store waits a cycle.
The Trap: Forwarding the Address of a Load
What if the stall is left out, and forwarding is trusted to do the job? The forwarding unit knows nothing of loads. It sees an instruction in MEM that writes x6, and an instruction in EX that reads x6.
So it forwards from MEM. It hands over what EX/MEM holds for the load. That is the ALU result of the load: its address.
cy | IF ID EX MEM WB | what happens
1 | 00 -- -- -- -- |
2 | 04 00 -- -- -- |
3 | 08 04 00 -- -- |
4 | 0c 08 04 00 -- | a<MEM
5 | 10 0c 08 04 00 | x10=1
6 | 14 10 0c 08 04 | x10=80000000 a<WB b<MEM
7 | 18 14 10 0c 08 | x5=7
8 | 1c 18 14 10 0c | a<MEM
9 | -- 1c 18 14 10 | x6=7 a<MEM b<WB
10 | -- -- 1c 18 14 | x7=80000007
11 | -- -- -- 1c 18 | x8=80000000
x7=2147483655 (wrong: the instruction set says 14) x8=2147483648 (wrong: the instruction set says 7) cycles=11Read row 8. The add is in EX one cycle early, with the note a<MEM. It adds the address 80000000 and 7. Row 10 writes x7 = 80000007.
Nothing warned. The run is one cycle shorter, 11 rows, and every instruction did something. Only the comparison with the instruction set, in the last line, shows the fault.
So a load in MEM is the one case in which a match must not be forwarded. In the real core the stall keeps that case from ever happening.
At Scale
This section looks ahead. One lost cycle is small. How often it is lost decides what it costs.
Measured on the real core
The checks of ap_rv32_p5 measure the cost on its pipeline view. A load-use pair costs 1 cycle. A load with one instruction between costs 0.
The compiler helps
A compiler that knows the pipeline moves another instruction between a load and its reader when it can. That is common practice.
A slow memory
When the data bus does not answer in one cycle, the real core holds all five stages until it does. S2.33 shows that freeze, and S3.2 the cost of a cache miss.
The other job of the hazard unit
A taken branch has fetched two instructions that must not run. The hazard unit turns them into bubbles. S2.10 counts that cost.
Try It
Run the lab. Find the bubble in each of the three rows in which it appears.
Set STALL to 0 and run again. Which value does the add take, and from which pipeline register?
Change the add to add x7, x5, x5, with its row. Is there still a stall? 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 -- | a<MEM 5 | 10 0c 08 04 00 | x10=1 6 | 14 10 0c 08 04 | x10=80000000 a<WB b<MEM 7 | 18 14 10 0c 08 | x5=7 stall 8 | 18 14 -- 10 0c | 9 | 1c 18 14 -- 10 | x6=7 a<WB 10 | -- 1c 18 14 -- | a<MEM 11 | -- -- 1c 18 14 | x7=e 12 | -- -- -- 1c 18 | x8=7 x7=14 x8=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 Forward from MEM whenever the instruction in MEM writes the register.
For a load, EX/MEM holds the address. The loaded word is not there yet.
Fix Stall the reader for one cycle, then forward from WB.
Wrong Hold the reader in ID and let ID/EX take it as well.
Then the reader runs twice: once now with a wrong value, and once more a cycle later.
Fix In the cycle of the stall, give ID/EX a bubble.
Wrong Count one lost cycle for every load.
Only a reader right behind the load waits. Most loads are not followed by one.
Fix Count the load-use pairs.
Remember
- A load has its value at the end of MEM. The instruction right behind it would need it one cycle earlier.
- A stall holds the PC and IF/ID and puts a bubble into ID/EX. The load moves on.
- After the stall, the reader takes the value by forwarding from WB.
- Cycles are n plus 4, plus one for each load-use pair. Measured on the real core: 1 cycle for a pair, 0 with one instruction between.