Forwarding: Two Matches, x0 and the Register File
S2.8 forwards a value from MEM or from WB. Three cases are left. Both stages may hold the register. The register may be x0. And the writer may be three instructions ahead.
You Will Be Able To
- Choose the right source when the instructions in MEM and in WB both write the register.
- Explain why a value on its way to x0 must never be forwarded.
- Say what a reader three instructions behind a writer gets, and from where.
- Lesson
- S2.25
- Time
- 25 min
- Runs on
- Icarus service
- Check
- 6 questions
A forwarding unit that only compares register numbers passes almost every program. It fails on a loop that updates one register again and again, and on an instruction that throws its result away. Both are common in real code.
Before You Start
You need forwarding from MEM and from WB, and the two selects, from S2.8.
You need the register file from S2.4: its two read ports, its write port, and x0.
| Question | |
|---|---|
| 1 | MEM and WB both write the register that EX reads. Which value is right? |
| 2 | Why must a write to x0 never be forwarded? |
| 3 | What does a reader get that is three instructions behind its writer? |
| 4 | Where does the bypass of the register file take its value from? |
| 5 | Which data hazard is still open after this page? |
The Idea: Find the Newest Value That Really Exists
Forwarding answers one question for each operand. Where is the newest value of this register, among the instructions ahead of me? S2.8 answered it for the easy case: one writer, one place.
Now let two instructions ahead write the same register. Then two pipeline registers hold a value for it, and the register file holds a third. Only the youngest of them is right.
Two words help here. Of two instructions, the younger one comes later in the program. And a stage matches when its instruction writes the register that the reader reads.
Next, let the register be x0. A value can be on its way to x0, but it will be thrown away. The register x0 is 0 by definition. So here no value in the pipeline is right.
Last, let the writer be three instructions ahead. It is in WB while the reader is in ID. The register file is being written in the very cycle in which it is read.
For that last case the register file gets a short path from its write port to its read ports. This page calls it the bypass of the register file, or the bypass for short.
New Words
| Word | Meaning |
|---|---|
| Bypass of the register file | A path from the write port of the register file to its read ports. A read gets the value that is being written. |
| Write-then-read | The behaviour that the bypass gives. A write and a read of one register in one cycle act as if the write came first. |
bypass_a_d | In the lab: 1 when the first read in ID must take the value that WB is writing. bypass_b_d is the same for the second read. |
One Picture: Three Values of One Register
- Memory
- Processor
- Control / FSM
- The oldest value
ID/EX holds what the register file gave in cycle 4. No write to x5 had happened yet: it holds 0.
- An older result
The first instruction made x5 = 1. It is in WB now, so MEM/WB holds 1. WB writes x5, and x5 is what EX reads: a match.
- A newer result
The second instruction made x5 = 2. It is in MEM now, so EX/MEM holds 2. MEM writes x5 too: a second match.
- The younger writer wins
In program order, the second instruction wrote x5 last. So its value is the right one. The unit sets forward_a to MEM.
- The result
The ALU adds 1 to 2 and gives 3. With the value from WB it would have given 2.
Read the diagram as text
- ID/EX: x5 as read in cycle 4 from the register file: 0
- Forwarding multiplexer: for rs1: three inputs, three different values
- ALU: adds 1 to the value it is given
- EX/MEM: the result of the second instruction: x5 = 2
- MEM/WB: the result of the first instruction: x5 = 1
- Forwarding unit: two matches: it must choose
- ID/EX → Forwarding multiplexer: 0
- EX/MEM → Forwarding multiplexer: 2
- MEM/WB → Forwarding multiplexer: 1
- Forwarding unit → Forwarding multiplexer: forward_a
- Forwarding multiplexer → ALU: the chosen value
Three copies of x5 exist at once, each of another age. Forwarding must take the youngest one that is older than the reader.
| The writer is | When the reader needs the value, the writer is | The value comes from |
|---|---|---|
| one instruction ahead | in MEM, the reader in EX | EX/MEM, by forwarding |
| two ahead | in WB, the reader in EX | MEM/WB, by forwarding |
| three ahead | in WB, the reader in ID | the write port, by the bypass of the register file |
| four or more ahead | gone from the pipeline | the register file itself |
When several rows fit, the first one that fits wins: it is the youngest writer.
The Smallest Example, Worked by Hand
Take three instructions that each add 1 to x5. The first is addi x5, x0, 1. Then addi x5, x5, 1 comes twice. The instruction set says that x5 ends at 3.
Look at the third instruction in EX. That is cycle 5. Ask where each copy of x5 is.
| Copy of x5 | Made by | Value | Age |
|---|---|---|---|
| in EX/MEM | the second instruction, now in MEM | 2 | youngest |
| in MEM/WB | the first instruction, now in WB | 1 | older |
| in ID/EX, read in cycle 4 | the reset | 0 | oldest |
Both pipeline registers match. The right operand is 2, and 2 plus 1 is 3.
Take the copy in MEM/WB by mistake, and the result is 1 plus 1. Then x5 ends at 2. The second addition is lost without a trace.
The Rules
Rule 1: When MEM and WB both match, take the value from MEM.
The instruction in MEM is the younger writer.
Rule 2: An instruction whose rd is x0 never matches.
Its result is thrown away. The reader of x0 must get 0.
Rule 3: An instruction whose reg_write is 0 never matches.
A store has bits in the rd place, and they mean something else.
Rule 4: A read in ID gets the value that WB is writing, when the register is the same.
That is the bypass of the register file. It follows rules 2 and 3 too.
Rule 5: The value from MEM or WB wins over the bypass.
A reader that used the bypass in ID may still find a younger writer when it is in EX.
With these rules and those of S2.8, a reader gets the right value at every distance from its writer. One case is left out on purpose, and S2.9 deals with it: the writer is a load.
Sources for These Rules
The three points of this page are described 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 rules of the real core are in docs/soc/ap_rv32-blueprint.md, section 8.2. It says that MEM wins as the younger write and that x0 is never forwarded. It also lists the register file bypass. 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 six instructions. The first three add 1 to x5. The fourth, add x0, x5, x5, throws its result away. The fifth reads x0. The sixth reads x5 from three instructions ahead.
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 | x5=1 a<MEM
6 | 14 10 0c 08 04 | x5=2 a<MEM b<MEM
7 | 18 14 10 0c 08 | x5=3
8 | -- 18 14 10 0c |
9 | -- -- 18 14 10 | x6=1
10 | -- -- -- 18 14 | x7=6
x5=3 x6=1 x7=6 cycles=10Rule 1 at work: read row 5. The instruction at 08 is in EX. The instruction at 04 is in MEM, and 00 is in WB and writes x5 = 1. Both match, and the note says a<MEM.
The proof is two rows down. Row 7 writes x5 = 3. With the value from WB it would be 2.
Rule 2 at work: read row 7. The instruction at 10 is addi x6, x0, 1, and it is in EX. In MEM is add x0, x5, x5, with the result 6. There is no note: nothing is forwarded. Row 9 writes x6 = 1.
Rule 4 at work: the instruction at 14 is add x7, x5, x5. It is in ID in row 7, the row that writes x5 = 3. It gets the 3 through the bypass, and row 10 writes x7 = 6.
The bypass leaves no note in the grid. The pipeline view of the real core shows forwarding in EX only.
| Instruction | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| 00: addi x5, x0, 1 | IF | ID | EX | MEM | WB | |||||
| 04: addi x5, x5, 1 | IF | ID | EX: a from MEM | MEM | WB | |||||
| 08: addi x5, x5, 1 | IF | ID | EX: a from MEM | MEM | WB | |||||
| 0c: add x0, x5, x5 | IF | ID | EX: a from MEM, b from MEM | MEM | WB | |||||
| 10: addi x6, x0, 1 | IF | ID | EX | MEM | WB | |||||
| 14: add x7, x5, x5 | IF | ID | EX | MEM | WB | |||||
| 18: ebreak | IF | ID | EX | MEM |
Drawn from the recorded rows. The instruction at 14 is in ID in cycle 7, while the instruction at 08 is in WB.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| MEM before WB | WB before MEM | The newest value. There is no cost: one of the two orders must be chosen anyway. |
| Test rd against 0 in the forwarding logic | Keep the result of such an instruction at 0 | One small comparison for each source. The ALU needs no special case. |
| A bypass at the register file | A third forwarding source in EX | The multiplexers in EX stay at three inputs. ID gets one multiplexer for each read port. |
| The bypass sits outside the register file | Build it into the register file | The single-cycle core can use the same file. There, a path from write port to read port would close a loop. |
| A real path for the bypass | Write in the first half of the cycle, read in the second | Every flip-flop uses the same clock edge. The half-cycle scheme is a textbook device, and common practice avoids it. |
In ap_rv32 the bypass is two lines in the ID stage of the pipeline. The register file of S2.4 is used unchanged by both builds.
The Trap: The Forward to x0
Rule 2 looks like a detail. Who writes to x0? In fact, x0 as rd is the usual way to say: do this, and throw the result away.
Now take a forwarding unit that only compares register numbers. It sees that MEM writes register 0 and that EX reads register 0. For that unit, this is a match like any other.
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 | x5=1 a<MEM
6 | 14 10 0c 08 04 | x5=2 a<MEM b<MEM
7 | 18 14 10 0c 08 | x5=3 a<MEM
8 | -- 18 14 10 0c |
9 | -- -- 18 14 10 | x6=7
10 | -- -- -- 18 14 | x7=6
x5=3 (right) x6=7 (wrong: the instruction set says 1) x7=6 (right) cycles=10Read row 7. The note a<MEM is new. The instruction at 10 reads x0, and it is handed the 6 that add x0, x5, x5 made. Row 9 writes x6 = 7, and the instruction set says 1.
The register x0 itself is still 0, and it always was. Only one instruction, at one distance, ever saw the 6. That is why such a fault can pass many tests.
The bypass can be switched off in the lab as well, with a switch named BYPASS. Then rule 4 is gone, and the reader three instructions behind gets an old value.
x5=3 (right) x6=1 (right) x7=4 (wrong: the instruction set says 6) cycles=10The add at 14 read x5 in row 7, one cycle too early. It got 2, the value that row 6 had written, and 2 plus 2 is 4.
At Scale
This section looks ahead. The lab shows each case once. A real core must be shown to handle each case for every instruction.
The load
When the writer in MEM is a load, EX/MEM holds an address, not the value. Forwarding it would be wrong. S2.9 makes the reader wait.
Planted faults
The checks of the real core plant 60 faults, one at a time. Some remove the test for x0 or turn the order of MEM and WB round. Each must be caught.
Freezes
When the data bus is slow, the real core holds all five stages. Then MEM and WB stay where they are, and every forwarding source stays valid. S2.33 shows the bus.
More sources
A longer pipeline has more stages between EX and the write. Each one is one more source, and the order among them stays: youngest first. That is common practice.
Try It
Run the lab. Find the row with two matches, and the row in which a read meets a write.
Set BYPASS to 0 and run again. Which register is wrong, and by how much?
Put addi x0, x0, 0 before the last add, with its row. Does the add still need the bypass?
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 | x5=1 a<MEM 6 | 14 10 0c 08 04 | x5=2 a<MEM b<MEM 7 | 18 14 10 0c 08 | x5=3 8 | -- 18 14 10 0c | 9 | -- -- 18 14 10 | x6=1 10 | -- -- -- 18 14 | x7=6 x5=3 x6=1 x7=6 cycles=10Read 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 Take the value from WB when MEM and WB both match.
The instruction in WB is the older writer. A newer value of the register exists.
Fix Ask MEM first. Ask WB only when MEM does not match.
Wrong Forward a result whose rd is x0.
That result is thrown away. A reader of x0 must get 0.
Fix Let an instruction with rd = x0 never match, in forwarding and in the bypass.
Wrong Leave the reader three instructions behind the writer to the register file as it is.
The write happens at the end of the cycle in which that reader reads.
Fix Give the read ports a bypass from the write port.
Remember
- When MEM and WB both write the register, the value from MEM is right: its writer is younger.
- An instruction with rd = x0, or with reg_write = 0, never matches.
- A reader three instructions behind its writer reads in the cycle of the write. The bypass of the register file hands it the new value.
- After forwarding and the bypass, one data hazard is left: the reader right behind a load.