Data Hazards and Forwarding
The pipeline of S2.7 gave 0 where the instruction set says 12. An instruction read a register that was still on its way. This page hands the value over on the way, at no cost of cycles.
You Will Be Able To
- Say when two instructions have a data hazard, from the distance between them.
- Say from which stage a forwarded operand comes, for a writer one and two instructions ahead.
- Read forwarding in a pipeline grid, for ALU operands and for the data of a store.
- Lesson
- S2.8
- Time
- 30 min
- Runs on
- Icarus service
- Check
- 6 questions
Look again at the hazard of S2.7. When the add needed x5, the 7 was not lost and not late. It sat in a pipeline register, two stages ahead, in plain sight. The only place that did not have it yet was the register file.
Before You Start
You need the five stages and the pipeline grid from S2.7. You need its last section too: the register that is still on its way.
You need the pipeline registers from S2.24: what EX/MEM and MEM/WB carry.
| Question | |
|---|---|
| 1 | What is a data hazard, and how close must two instructions be to have one? |
| 2 | Where is the value that the reader needs while the reader is in EX? |
| 3 | What does the forwarding unit compare? |
| 4 | Which values of an instruction can be forwarded? |
| 5 | What does forwarding cost? |
The Idea: The Value Exists, Take It from Where It Is
Call an instruction that writes a register the writer. Call a later instruction that reads that register the reader. In a pipeline the reader may read before the writer has written.
That is a data hazard. Its full name is a read-after-write hazard. In program order the read comes after the write. In the pipeline it happens first.
The cure starts from one fact. The ALU makes the result of an add in EX. From then on the result exists. It travels through EX/MEM and MEM/WB for two cycles before it is written.
The reader needs the value in EX, where its own ALU is. So the pipeline gets two extra paths. One runs from EX/MEM back to the ALU inputs. One runs from MEM/WB.
Handing a value over on such a path is called forwarding. A small block, the forwarding unit, decides in every cycle which path each ALU input takes.
New Words
| Word | Meaning |
|---|---|
| Data hazard | A hazard about a value: an instruction would use an old value of a register. |
| Read-after-write hazard | The data hazard of this pipeline. The reader reads before the writer, which is ahead of it, has written. |
| Forwarding | Handing a result from a pipeline register straight to the stage that needs it. |
| Forwarding unit | The block that decides, for each operand of the instruction in EX, where its value comes from. |
| Forwarding multiplexer | A multiplexer in front of an ALU input. It chooses among ID/EX, EX/MEM and MEM/WB. |
forward_a | The select of the forwarding multiplexer for rs1. forward_b is the one for rs2. |
FORWARD_NONE | The select value for the value read in ID. FORWARD_MEM and FORWARD_WB name the two other sources. |
One Picture: Two Paths Back to the ALU
- Memory
- Processor
- Control / FSM
- What ID read
The sub read x7 and x6 in cycle 5. Neither was written yet. ID/EX holds two old values, both 0.
- Where x7 is
The add that makes x7 is one instruction ahead. In cycle 6 it is in MEM. Its result, c in hex, is in EX/MEM.
- Where x6 is
The addi that makes x6 is two instructions ahead. In cycle 6 it is in WB, and writes 5 at the end of the cycle. The 5 is in MEM/WB.
- The forwarding unit decides
It sees that MEM writes x7 and WB writes x6. It sets forward_a to MEM and forward_b to WB.
- The ALU gets new values
The multiplexers pass c and 5. The ALU subtracts and gives 7. The two old values in ID/EX are never used.
Read the diagram as text
- ID/EX: the two values read in ID: both are old
- Forwarding multiplexers: one for each operand: three inputs each
- ALU: works on the two chosen values
- EX/MEM: the result of the instruction in MEM
- MEM/WB: the value that the instruction in WB writes
- Forwarding unit: compares register numbers, gives forward_a and forward_b
- ID/EX → Forwarding multiplexers: values read in ID
- Forwarding multiplexers → ALU: rs1_value_e, rs2_value_e
- ALU → EX/MEM: result
- EX/MEM → MEM/WB: one cycle later
- EX/MEM → Forwarding multiplexers: forward from MEM
- MEM/WB → Forwarding multiplexers: forward from WB
- Forwarding unit → Forwarding multiplexers: forward_a, forward_b
No value goes back in time. Both results existed before cycle 6 began. They only had not reached the register file.
| The reader comes | When the reader is in EX, the writer is | The value comes from |
|---|---|---|
| right after the writer | in MEM | EX/MEM: forward from MEM |
| two instructions after | in WB | MEM/WB: forward from WB |
| three instructions after | done. It was in WB when the reader was in ID | the register file, with a small addition that S2.25 shows |
| four or more after | done before the reader reached ID | the register file: no hazard |
On a five-stage pipeline, only the three instructions right behind a writer can have a data hazard with it.
The Smallest Example, Worked by Hand
Take the program of the trap in S2.7: addi x5, x0, 7, then addi x6, x0, 5, then add x7, x5, x6. The add is the third instruction, so it is in EX in cycle 5.
| Operand | Its writer | Where the writer is in cycle 5 | Select | Value |
|---|---|---|---|---|
| rs1 is x5 | the first instruction, two ahead | WB | forward_a = FORWARD_WB | 7 |
| rs2 is x6 | the second instruction, one ahead | MEM | forward_b = FORWARD_MEM | 5 |
The values that the add read in ID, in cycle 4, were both 0. They are not used.
The ALU adds 7 and 5 and gives 12. That is what the instruction set says. And the add was in EX in cycle 5, exactly as in S2.7: it did not wait.
The Rules
Rule 1: A data hazard needs a writer and a reader of one register, close together.
The reader is at most three instructions behind the writer.
Rule 2: The forwarding unit looks at the instruction in EX.
It compares the two register numbers that EX reads with the rd of the instructions in MEM and in WB.
Rule 3: If the instruction in MEM writes that register, the operand comes from EX/MEM.
That writer is one instruction ahead.
Rule 4: If the instruction in WB writes it, the operand comes from MEM/WB.
That writer is two instructions ahead.
Rule 5: Each operand has a select of its own.
One instruction can take rs1 from MEM and rs2 from WB in the same cycle.
Rule 6: Forwarding costs no cycle.
It costs two multiplexers in front of the ALU, and they make the path through EX longer.
The real core has a third select value for the plain case. These are the three names, from a file of its source.
// ---- 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 writesTwo fine points are left for S2.25. What if MEM and WB both write the register? And what about x0?
Sources for These Rules
Data hazards and forwarding 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 forwarding rules of the real core are in docs/soc/ap_rv32-blueprint.md, section 8.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 five instructions. The last three each use a result of the one before. A note in the last column tells where EX takes an operand from: a is rs1, b is rs2.
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=5 a<MEM b<WB
7 | -- 14 10 0c 08 | x7=c a<MEM
8 | -- -- 14 10 0c | x8=7
9 | -- -- -- 14 10 | x9=e
x7=12 x8=7 x9=14 cycles=9Rules 3 and 4 at work: read row 5. The add at 08 is in EX. It takes a from WB, where the instruction at 00 writes x5. It takes b from MEM, where 04 holds the new x6.
Rule 5 at work: row 6 has the two selects the other way round. The sub at 0c takes x7 from MEM and x6 from WB.
Rule 6 at work: five instructions end in row 9. That is 5 plus 4, the count of a pipeline with no hazard at all.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|
| 00: addi x5, x0, 7 | IF | ID | EX | MEM | WB | ||||
| 04: addi x6, x0, 5 | IF | ID | EX | MEM | WB | ||||
| 08: add x7, x5, x6 | IF | ID | EX: a from WB, b from MEM | MEM | WB | ||||
| 0c: sub x8, x7, x6 | IF | ID | EX: a from MEM, b from WB | MEM | WB | ||||
| 10: add x9, x8, x5 | IF | ID | EX: a from MEM | MEM | WB | ||||
| 14: ebreak | IF | ID | EX | MEM |
Drawn from the recorded rows. A forwarded operand is written into the EX cell of its reader.
Now the same program with the forwarding switched off. The lab has a switch named FORWARD for that, and here it is 0. This is the pipeline of S2.7 again, with one small difference that S2.25 explains.
cy | IF ID EX MEM WB | what happens
1 | 00 -- -- -- -- |
2 | 04 00 -- -- -- |
3 | 08 04 00 -- -- |
4 | 0c 08 04 00 -- |
5 | 10 0c 08 04 00 | x5=7
6 | 14 10 0c 08 04 | x6=5
7 | -- 14 10 0c 08 | x7=0
8 | -- -- 14 10 0c | x8=0
9 | -- -- -- 14 10 | x9=7
x7=0 (wrong: the instruction set says 12) x8=0 (wrong: the instruction set says 7) x9=7 (wrong: the instruction set says 14) cycles=9The grid has the same shape and the same nine rows. Only the values differ. So forwarding changed no timing at all: it changed which value the ALU sees.
In the lab, the bench plays the forwarding unit. It keeps a note for each register: in which stage the newest writer is. The real unit compares register numbers. Problem S2.8.1 asks you to build it.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Forward the value | Make the reader wait until the writer is done | No lost cycle. Waiting would cost up to two cycles for every close pair. |
| Forward to the start of EX | Forward to ID, before ID/EX | A value made in EX can be used in the very next cycle. The multiplexers add to the longest stage. |
| Two sources: MEM and WB | One source: MEM only | A writer two ahead is covered too. Each multiplexer needs one more input. |
| Decide by register numbers | Tag every value with its owner | Four small comparisons. The numbers rd must travel to MEM and WB, which they do anyway. |
| The unit sees only EX, MEM and WB | Decide in ID and carry the select | The choice is made when the values are there. The comparison sits in the longest stage. |
The Trap: An Operand That Never Reaches the ALU
It is easy to think of forwarding as a matter of the two ALU inputs. One value of one instruction goes round the ALU: the data of a store.
A store adds rs1 and its offset in the ALU. That is its address. Its rs2 is the value to store. It goes past the ALU, straight into EX/MEM.
Now put the writer of that rs2 right before the store. The program below does it: addi x5, x0, 7 and then sw x5, 0(x10).
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 | -- 18 14 10 0c |
9 | -- -- 18 14 10 | x6=7
10 | -- -- -- 18 14 |
x6=7 mem[80000000]=7 cycles=10Read row 6. The store, at 0c, is in EX. It takes a from WB: that is x10, its address. It takes b from MEM: that is x5, its data.
So the store data needs a forwarding multiplexer just like an ALU operand. A core that forwards only into the ALU stores an old value here. The address is right, and the memory holds a wrong word.
The last line shows that this lab got it right. The load reads back 7.
At Scale
This section looks ahead. The lab forwards for three kinds of instruction. The real core must forward for every instruction that reads a register.
Two matches, and x0
When MEM and WB both write the register, one of them must win. And a value on its way to x0 must never be used. S2.25 covers both.
A value that is not there yet
A load has its value only after MEM. An instruction right behind it cannot get it by forwarding. S2.9 makes it wait one cycle.
Branches
A branch compares two registers in EX. It takes them through the same multiplexers. S2.10 shows what a taken branch costs.
Wider cores
A core that starts several instructions in a cycle has many more pairs to compare. That is common practice, and no source in the library was checked for it.
Try It
Run the lab. For each note in the last column, name the instruction that made the value.
Set FORWARD to 0 and run again. Which rows change, and which column never changes?
Put addi x0, x0, 0 between the second and the third instruction, with its row. Where does the add take x5 from now?
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=5 a<MEM b<WB 7 | -- 14 10 0c 08 | x7=c a<MEM 8 | -- -- 14 10 0c | x8=7 9 | -- -- -- 14 10 | x9=e x7=12 x8=7 x9=14 cycles=9Read 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 Think that forwarding sends a value back in time.
The result was made a cycle or two earlier. It moves sideways, from a pipeline register to the ALU inputs.
Fix Check that the writer has passed EX before the reader needs the value.
Wrong Forward only into the two ALU inputs.
The data of a store goes round the ALU. It can be just as old.
Fix Give the store data the same forwarding as an ALU operand.
Wrong Forward whenever two register numbers are equal.
An instruction such as a store has bits in the rd place and writes no register.
Fix Forward only from an instruction whose reg_write is 1.
Remember
- A data hazard: a reader would use an old value, because a writer close ahead has not written yet.
- A writer one ahead is in MEM when the reader is in EX. A writer two ahead is in WB.
- The forwarding unit compares register numbers and sets one select for each operand.
- Forwarding costs no cycle. The store data needs it as much as the ALU operands do.