Analyze a 5-stage MIPS pipeline data hazard where ADD writes to a register that SUB…
- User
Analyze a 5-stage MIPS pipeline data hazard where ADD writes to a register that SUB immediately reads. Design forwarding unit logic that bypasses the ALU result from EX stage directly to the next instruction's EX stage.
- Assistant
Designing forwarding logic for EX stage hazard
The forwarding unit compares the EX/MEM destination register with the current EX stage source registers and sets control signals to forward the ALU result from EX/MEM directly to the next instruction’s ALU inputs, resolving the RAW hazard without stalls by bypassing data one cycle early. In the textbook five–stage MIPS the architectural state is only updated in the WB stage. If you feed an
ADDand then immediately aSUBthat uses theADD’s destination, the naive pipeline looks like this:C1 C2 C3 C4 C5 … ADD IF ID EX MEM WB SUB IF ID EX MEM WBSUBreads its operands out of the register file in its ID stage in cycle 3, but theADDdoesn’t write the new value back until its WB stage in cycle 5, so without any interventionSUBwill see the old value – a classic RAW hazard.The key observation is that the
ADD’s result is available a lot earlier than WB: it appears on the ALU result bus at the end of cycle 3 and is latched intoEX/MEMin cycle 4. If we make that value an alternate source for the next instruction’s ALU inputs, we can avoid stalling.A typical forwarding unit therefore has:
- Inputs: the source register numbers in
ID/EX(ID_EX.Rs,ID_EX.Rt), the destination register numbers and RegWrite bits inEX/MEMandMEM/WB. - Outputs: control signals to two multiplexers in front of the ALU, usually called
ForwardAandForwardB.
One way of coding the combinational logic is:
// default: take operands from register file as latched in ID/EX ForwardA = 2'b00; ForwardB = 2'b00; // bypass from EX/MEM (result of previous ALU op) if (EX_MEM.RegWrite && EX_MEM.WriteReg != 5'd0 && EX_MEM.WriteReg == ID_EX.Rs) ForwardA = 2'b10; if (EX_MEM.RegWrite && EX_MEM.WriteReg != 5'd0 && EX_MEM.WriteReg == ID_EX.Rt) ForwardB = 2'b10; // otherwise, bypass from MEM/WB (older instruction) if (MEM_WB.RegWrite && MEM_WB.WriteReg != 5'd0 && MEM_WB.WriteReg == ID_EX.Rs && ForwardA == 2'b00) ForwardA = 2'b01; if (MEM_WB.RegWrite && MEM_WB.WriteReg != 5'd0 && MEM_WB.WriteReg == ID_EX.Rt && ForwardB == 2'b00) ForwardB = 2'b01;The
ForwardA/Bsignals drive 3‑way muxes at the ALU inputs:00selects the register operand fromID/EX,10selects the ALU result fromEX/MEM(the immediately preceding instruction’s EX result), and01selects the value being written back inMEM/WB. In theADD/SUBsequence above, whenSUBreaches EX in cycle 4 the forwarding unit assertsForwardA/B = 2'b10for the matching source register, and the ALU sees theADDresult directly fromEX/MEMwithout waiting for WB. - Inputs: the source register numbers in
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.