The lab of the lesson let its bench decide where each operand comes from. A real pipeline has a block for that. Write it: it compares register numbers and gives two selects.
Hardware context: The lab pipeline with forwarding multiplexers in front of the ALU and on the store data, and a bench that plays the forwarding unit; the forwarding can be switched off. Reading forwarding in a pipeline trace and reviewing the forwarding paths of a core
The instruction in EX reads up to two registers. A newer value for each of them may still be in the pipeline: in MEM, or in WB. The forwarding unit finds out, and tells the two multiplexers in front of the ALU.
Write that unit. It has no clock. It sees the two register numbers that EX reads, and what the instructions in MEM and in WB write.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
trial | 10 | input of each check |
Full specification
| Code | Meaning | When |
|---|---|---|
| 0 | take the value that was read in ID | no instruction in MEM or WB writes this register |
| 1 | take the result of the instruction in MEM | that instruction writes this register |
| 2 | take the value that the instruction in WB writes | that instruction writes this register, and the one in MEM does not |
| Case | What must happen |
|---|---|
| The instruction does not write a register | Its reg_write is 0. Its rd may hold any number, and it must be ignored. |
| Both instructions write the same register | The instruction in MEM is the younger one. Its value is the newer value. |
| The written register is x0 | x0 always reads 0. A value on its way to x0 is thrown away and must never be forwarded. |
How to read a result: a public case shows the two selects. A private case tries many inputs of one kind, and all_match is 1 when both selects were right every time.
Examples
Example 1
A Write in MEM- Input
rs1 = 5, rs2 = 6. MEM writes x5. WB writes nothing
- Output
forward_a = 1, forward_b = 0
The instruction in MEM writes the register that EX reads as rs1. So forward_a is 1. Nobody writes x6: forward_b is 0.
Example 2
Two Writes to One Register- Input
rs1 = 5, rs2 = 7. MEM writes x5, and WB writes x5 too
- Output
forward_a = 1, forward_b = 0
Both stages hold a value for x5. The instruction in MEM is the younger of the two, so its value is the newer one: forward_a is 1.
Example 3
A Store in MEM- Input
rs1 = 5, rs2 = 9. MEM holds a store with 5 in its rd field. WB writes x9
- Output
forward_a = 0, forward_b = 2
The store in MEM writes no register: reg_write_m is 0, so its rd field means nothing. WB writes x9, which EX reads as rs2: forward_b is 2.
Constraints and Contract
- The outputs depend only on the inputs of the same moment. The bench waits 1 ns after it sets them.
- The unit does not know whether the instruction in EX really reads rs1 or rs2. It gives a select for both numbers in every case.
- The codes are 0, 1 and 2, as in the table. The code 3 is never right.
Hints
Hint 1
Write the condition for one stage and one operand first: when may the value in MEM be used for rs1?
Hint 2
That condition has three parts. Each row of the second table removes one wrong match.
Hint 3
Then decide the order of the two stages. The second operand is the same logic with another register number.
What the grader checks: Every pair of a read register and a written register, for a write in MEM and for a write in WB. The same pairs for writes in both, to different registers and to one register, and for no write. Both operands on one register. The register x0 as source and as destination. 6,000 mixed cases with register numbers that often match.
11 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 problems on this path