Chapter 4: Q13E (page 364)
This exercise is intended to help you understand the relationship between forwarding, hazard detection, and ISA design. Problems in this exercise refer to the following sequence of instructions, and assume that it is executed on a 5-stage pipelined datapath:
add r5,r2,r1
lw r3,4(r5)
lw r2,0(r2)
or r3,r5,r3
sw r3,0(r5)
4.13.1 [5] If there is no forwarding or hazard detection, insert nops to ensure correct execution.
4.13.2 [10] Repeat 4.13.1 but now use nops only when a hazard cannot be avoided by changing or rearranging these instructions. You can assume register R7 can be used to hold temporary values in your modified code.
4.13.3 [10] If the processor has forwarding, but we forgot to implement the hazard detection unit, what happens when this code executes? 4.13.4 [20] If there is forwarding, for the first five cycles during the execution of this code, specify which signals are asserted in each cycle by hazard detection and forwarding units in Figure 4.60.
4.13.5 [10] If there is no forwarding, what new inputs and output signals do we need for the hazard detection unit in Figure 4.60? Using this instruction sequence as an example, explain why each signal is needed. 4.13.6 [20] For the new hazard detection unit from 4.13.5, specify which output signals it asserts in each of the first five cycles during the execution of this code.
Short Answer
4.13.1
The required sequence of the instructions:
add r5, r2, r1
nop
nop
ld r3, 4(r5)
ld r2, 0(x2)
nop
or r3, r5, r3
nop
nop
sd r3, 0(r5)
4.13.2
In the sequence of instructions, “nop” was used only when the necessary purpose. So, this can’t possible to change or rearrange these instructions.
4.13.3
For the given condition, the code will be executed without any obstacles.
4.13.4
Which signals are asserted in every cycle is specified by the hazard detection and the units of forwarding in the mentioned figure:
instruction | Cycles | ||||||||
add | IF | ID | EX | ME | WB | ||||
ld | IF | ID | EX | ME | WB | ||||
ld | IF | ID | EX | ME | WB | ||||
or | IF | ID | EX | ME | WB | ||||
sd | IF | ID | EX | ME | WB |
4.13.5
Further, no signal is needed.
4.13.6
After assertion of each output signal is specified in every cycle:
instruction | Cycles | Values | ||||||||
add | IF | ID | EX | ME | WB | PCWrite -1 | ||||
ld | IF | ID | EX | ME | WB | PCWrite-1 | ||||
ld | IF | ID | EX | ME | WB | PCWrite-1 | ||||
or | IF | ID | EX | ME | WB | PCWrite-0 | ||||
sd | IF | ID | EX | ME | WB | PCWrite-0 |