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
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
This is a comprehensive question about MIPS pipeline data hazards and forwarding logic. Let me break this down systematically.
MIPS 5-Stage Pipeline Overview:
IF (Instruction Fetch)
ID (Instruction Decode)
EX (Execute)
MEM (Memory)
WB (Write Back)
The Scenario:
code
ADD R1, R2, R3 // Writes to R1 in WB stage (at end of cycle 5)
SUB R4, R1, R5 // Reads R1 in ID stage (at start of cycle 2)
Wait, let me reconsider the timing. If ADD writes to register in cycle 5 (WB stage), and SUB reads in cycle 2 (ID stage), there's no conflict because SUB already has the value (the old value). The hazard occurs when SUB enters EX stage needing the value that ADD will write.
Let me reconsider:
Cycle 1: ADD in IF
Cycle 2: ADD in ID, SUB in IF
Cycle 3: ADD in EX, SUB in ID (here SUB reads the register file, gets old value - READ AFTER WRITE hazard conceptually, but timing matters)
Actually, the classic data hazard is:
Cycle 1: ADD in IF
Cycle 2: ADD in ID, SUB in IF
Cycle 3: ADD in EX, SUB in ID
Cycle 4: ADD in MEM, SUB in EX (SUB needs R1 value in EX stage, but ADD hasn't written it yet - it will write in cycle 5)
This is a Load-Use hazard variant. The ADD writes in cycle 5 (WB stage), but SUB needs it in cycle 4 (EX stage).
The forwarding solution bypasses the result from ADD's EX stage (cycle 3) or later stages directly to SUB's ALU input.
When ADD computes R1 in its EX stage (cycle 3), that result can be forwarded directly to SUB's ALU during cycle 4 when SUB is executing. Without forwarding, SUB would stall waiting for ADD to complete the write-back phase, but forwarding eliminates this delay by routing the computed value immediately to where it's needed.
The forwarding unit detects data dependencies by comparing the destination register of instructions in later pipeline stages against the source registers of the current instruction. When a match occurs and the later instruction writes to a register, the forwarding unit selects that result as an ALU input instead of waiting for the register file. The most critical case is EX-to-EX forwarding, where an instruction in the execute stage can receive a result from another instruction also in the execute stage, preventing any stall cycles.
I need to map out the exact timing to get the forwarding paths right. In a standard five-stage pipeline, results flow from EX/MEM and MEM/WB registers back to the ID/EX stage where operands are selected. The key forwarding scenarios are when an instruction two cycles ahead produces a result that the current instruction needs, or when the immediately preceding instruction's result is available. MEM stage (MEM/WB register) to EX stage when the source operand in ID/EX matches the destination in MEM/WB.
In cycle 4, ADD is in MEM with the computed result for R1, while SUB is in EX and needs that value. The forwarding path from EX/MEM directly supplies this result to SUB's execution, bypassing the need to wait for ADD to complete writeback. This is the critical forwarding dependency that prevents a stall.
The forwarding unit detects when an instruction in the EX stage requires a register that an earlier instruction is producing. It compares the source registers (ID/EX.Rs) against destination registers in both the MEM stage (EX/MEM.Rd) and WB stage (MEM/WB.Rd), then routes the appropriate result directly to the ALU input when conditions are met—specifically when the destination register is valid and not R0.
For the ADD-to-SUB case, forwarding from the MEM stage suffices since the ADD result becomes available immediately after its EX stage completes, allowing the SUB instruction to use it without stalling.
The forwarding unit detects when the destination register of an earlier instruction matches a source register of the current instruction, then routes the computed value directly to the ALU rather than waiting for it to propagate through the register file. In this scenario, the ADD's result in the EX/MEM register gets selected and fed into the SUB's ALU operand multiplexer, eliminating the data dependency delay. Outputs for forwarding control signals that drive 3-way multiplexers, each 2 bits wide to select between register file, MEM stage result, or WB stage result. The logic defaults to no forwarding, then checks if the destination register from the previous instruction matches either source register of the current instruction—if so, it forwards from the EX/MEM pipeline register. Similarly, it checks the MEM/WB register for forwarding opportunities when the WB stage instruction writes to a register that the current instruction needs. The multiplexer selects the appropriate ALU input based on the forwarding control signal. It takes the register file data as the default path, but substitutes the result from the MEM stage if forwarding is needed there, or from the WB stage if that's where the data originates. The case statement routes each forwarding option to the correct data source, ensuring the ALU always operates on the most current value available.
Regarding the EX-to-EX forwarding path mentioned in the original question—this would require pulling directly from the ALU's combinational output rather than waiting for the pipeline register. While this creates a shorter critical path than typical stage-to-stage forwarding, it's only feasible in single-cycle pipelines; in a 5-stage design, the next instruction sees data through the EX/MEM register, not the raw ALU combinational logic. During cycle 4, instruction 2 reaches the EX stage where it can access the result stored in the EX/MEM register from the previous cycle. The phrasing "from EX stage directly to the next instruction's EX stage" refers to forwarding through this pipeline register rather than combinational logic, since the result isn't available to the following instruction until it's captured at the end of the current cycle. Attempting direct ALU-to-ALU forwarding wouldn't work because the dependent instruction hasn't reached EX yet when the result is produced.
MIPS Pipeline Data Hazard Analysis & Forwarding Unit Design
1. Hazard Analysis: ADD→SUB Data Dependency
Instruction Sequence
code
ADD R1, R2, R3 // Write to R1
SUB R4, R1, R5 // Read R1 (RAW hazard)
Cycle-by-Cycle Pipeline Timing
code
Cycle │ IF │ ID │ EX │ MEM │ WB │
─────┼────────┼────────┼────────┼────────┼────────┤
1 │ ADD │ │ │ │ │
2 │ SUB │ ADD │ │ │ │
3 │ │ SUB │ ADD │ │ │
4 │ │ │ SUB │ ADD │ │ ← HAZARD!
5 │ │ │ │ SUB │ ADD │ (writes after use)
Critical Issue at Cycle 4
Stage
Instruction
Status
EX
SUB
Needs R1 value ✗
MEM
ADD
Result in EX/MEM register ✓
WB
-
R1 not yet written to register file
Without forwarding: SUB reads stale/uninitialized R1 from register file
2. Forwarding Unit Design
Forwarding Logic
verilog
module ForwardingUnit (
// Current instruction in EX stage (ID/EX pipeline register)
input [4:0] ID_EX_Rs1, // Source register 1
input [4:0] ID_EX_Rs2, // Source register 2
// Previous instruction in MEM/WB stages
input [4:0] EX_MEM_Rd, // Dest register (MEM stage)
input EX_MEM_RegWrite,
input [4:0] MEM_WB_Rd, // Dest register (WB stage)
input MEM_WB_RegWrite,
// 2-bit control: 00=RegFile, 01=EX/MEM, 10=MEM/WB
output reg [1:0] ForwardA, // For Rs1
output reg [1:0] ForwardB // For Rs2
);
// Constants
localparam NO_FWD = 2'b00, MEM_FWD = 2'b01, WB_FWD = 2'b10;
always @(*) begin
// Initialize to no forwarding
ForwardA = NO_FWD;
ForwardB = NO_FWD;
// ========== PRIMARY FORWARDING: EX/MEM → ID/EX ==========
// For Rs1
if (EX_MEM_RegWrite && (EX_MEM_Rd != 5'b0) &&
(EX_MEM_Rd == ID_EX_Rs1)) begin
ForwardA = MEM_FWD; // Forward from previous instruction's EX result
end
// For Rs2
if (EX_MEM_RegWrite && (EX_MEM_Rd != 5'b0) &&
(EX_MEM_Rd == ID_EX_Rs2)) begin
ForwardB = MEM_FWD;
end
// ========== SECONDARY FORWARDING: MEM/WB → ID/EX ==========
// (Lower priority - only if not already forwarded from MEM stage)
if (MEM_WB_RegWrite && (MEM_WB_Rd != 5'b0) &&
(MEM_WB_Rd == ID_EX_Rs1) &&
!(EX_MEM_RegWrite && EX_MEM_Rd == ID_EX_Rs1)) begin
ForwardA = WB_FWD; // Forward from 2-instruction-ago's result
end
if (MEM_WB_RegWrite && (MEM_WB_Rd != 5'b0) &&
(MEM_WB_Rd == ID_EX_Rs2) &&
!(EX_MEM_RegWrite && EX_MEM_Rd == ID_EX_Rs2)) begin
ForwardB = WB_FWD;
end
end
endmodule
3. ALU Input Multiplexer
verilog
module ALU_Mux (
input [31:0] regfile_val,
input [31:0] ex_mem_result, // From EX/MEM register
input [31:0] mem_wb_result, // From MEM/WB register
input [1:0] forward_ctrl,
output reg [31:0] alu_input
);
always @(*) begin
case (forward_ctrl)
2'b00: alu_input = regfile_val; // Normal path
2'b01: alu_input = ex_mem_result; // Forward from MEM stage
2'b10: alu_input = mem_wb_result; // Forward from WB stage
default: alu_input = regfile_val;
endcase
end
endmodule