Double Stage Forwarding with Priority
In a 5-stage pipeline both EX/MEM and MEM/WB may hold the register an instruction needs. The younger one, EX/MEM, has the newer value and must win.
The forwarding unit compares the source register addresses (rs1_addr, rs2_addr) of the current instruction against the destination register addresses (ex_mem_rd, mem_wb_rd) of the instructions currently in the EX/MEM and MEM/WB stages. If a match is found and the corresponding stage is actively writing to the register file (regwrite is 1), the unit forwards the data from that stage instead of using the stale default register data (rs1_data, rs2_data). If both stages write to the same source register, the EX/MEM stage holds the most recent data and must take strict priority. The resolved operands are then latched into output registers for the next pipeline stage.
The module is driven by a positive-edge triggered clock clk and an asynchronous active-low reset rst_n. On reset, both output registers out_op1 and out_op2 must be driven to 0.
Cycle by cycle execution trace:
- Cycle 1:
rst_n=0. Outputsout_op1andout_op2are forced to 0. - Cycle 2:
rst_n=1. Inputs arers1_addr=1,rs1_data=10. No forwarding matches occur. On the clock edge,out_op1updates to 10. - Cycle 3:
rs1_addr=3,ex_mem_rd=3,ex_mem_regwrite=1,ex_mem_data=30. On the clock edge,out_op1updates to 30. - Cycle 4:
rs1_addr=5. Bothex_mem_rd=5 (ex_mem_data=50) andmem_wb_rd=5 (mem_wb_data=60) match with activeregwrite. EX/MEM takes priority. On the clock edge,out_op1updates to 50. - Cycle 5:
rs1_addr=0,ex_mem_rd=0,ex_mem_regwrite=1 (ex_mem_data=99). Register 0 cannot be forwarded to. On the clock edge,out_op1updates to 0 (usingrs1_data).
| Signal | Direction | Width | Description |
|---|---|---|---|
clk |
input | 1 | Positive-edge triggered clock |
rst_n |
input | 1 | Asynchronous active-low reset; outputs go to 0 when asserted |
rs1_addr |
input | 5 | Source register 1 address |
rs2_addr |
input | 5 | Source register 2 address |
rs1_data |
input | 32 | Default data for source register 1 from the register file |
rs2_data |
input | 32 | Default data for source register 2 from the register file |
ex_mem_rd |
input | 5 | Destination register address in EX/MEM stage |
ex_mem_data |
input | 32 | Data to be written by EX/MEM stage |
ex_mem_regwrite |
input | 1 | Write enable from EX/MEM stage |
mem_wb_rd |
input | 5 | Destination register address in MEM/WB stage |
mem_wb_data |
input | 32 | Data to be written by MEM/WB stage |
mem_wb_regwrite |
input | 1 | Write enable from MEM/WB stage |
out_op1 |
output | 32 | Registered operand 1; resets to 0 |
out_op2 |
output | 32 | Registered operand 2; resets to 0 |
Constraints
- The module must trigger on the positive edge of
clkand reset asynchronously on the active-lowrst_n. - The outputs
out_op1andout_op2must be registered and default to 0 on reset. - Register 0 (
5'b00000) is hardwired to zero in RISC architectures. If a source address is 0, the forwarding logic must ignore any matches and output the correspondingrs_data. - Priority rule: If both EX/MEM and MEM/WB stages have a valid forwarding match for the same register, the EX/MEM stage takes strict priority.
- A forwarding match only occurs if the stage's
regwritesignal is asserted.
Topics
Solve this problem
Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.
This problem is part of Codiode Pro. The statement above is free to read.
The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.
Part of Pipelining
Problem 15 of 20 in the Pipelining track.
Related problems
Browse all problems · Verilog practice problems · Learning tracks