Search code examples
cpumipscpu-architecture

Register file forwarding in MIPS


I am reading section 4.7 in in Computer Organization and Design: The Hardware/Software Interface on data forwarding. In the example given in the book, there is register file forwarding.(This is exactly how it appears in the book example)

Why do you need register file forwarding? Data in the register is written in the first half of the clock and read in the second half of the clock, so it should be safe without forwarding.

Just to clarify the example given in the link above: Left side shading of Reg means it being written into, right - being read from.

Thanks !


Solution

  • Figure 4.53 of Computer Organization and Design, (c)2009 Elsevier

    You need register file forwarding to prevent stalls.

    If you look closely the second instruction (and $12, $2, $5) needs to use register $2, but the value computed in the previous instruction (sub $2, $1, $3) will get written back in the register file in clock cycle 5. Therefore if it wasn't because of register file forwarding, the second instruction would have to stall until register $2 were written back. The forwarding is shown with a skyblue line between the result of the ALU operation of instruction 1 and the first argument to the ALU operation of instruction 2.

    Likewise, the third instruction (or $13, $6, $2) also needs to use $2 before it is actually written back to the register file by the first instruction. Therefore again, to prevent stalls, register forwarding is used to use the result of the first instruction in as an argument to the third instruction.

    On the other hand, the fourth instruction (add $14, $2, $2) does not need to use pipeline register forwarding, as the result of the first instruction is written back in the first half of cycle 5 and therefore its contents are already updated when they are needed in the fourth instruction.