Let's look at an LW instruction: 0000 0000 0100 0001 0010 0000 1000 0011 (Immediate=4, RS1=2 (SP), RD=1 (RA)):
- IR: PC is provided to Instruction Memory as a memory address and the instruction is presented at the ReadInstr output.
- IF/ID: ReadInstr output is latched into the IF/ID latch. At this point the immediate value is extended/expanded by the "immediate generator" and the address (RS1=2) is provided to one of the ports of the dual-read-port register memory. (A dummy value for RS2 of 4 is also provided to the second port, but this read result doesn't matter.) Various slices of the instruction bits are also presented to the input of the ID/EX latch. This is where a lot of decode takes place with parallel units picking off various parts of the latched instruction.
- ID/EX: The value read from the SP register (2) is latched and presented to the ALU, along with the immediate value of #4 and the "add operation." The ALU presents the sum of the SP register and #4 to its output. The destination register address is also latched here.
- EX/MEM: The ALU output (SP+4) is latched as the address presented to the Data Memory and the READ control line is activated to perform a read from Data Memory at the presented address. The value read from this address is now presented and ready to be latched at the next step. The destination register address is copied from the ID/EX and latched into EX/MEM, as well.
- MEM/WB: The destination register address is latched here from the EX/MEM stage and the value read from the Data Memory is latched, as well, and presented back to the register file as a Write Register data value, along with the latched register address that has been copied along through the stages. At the next clock, the presented data value will be written into the addressed register.
If the MEM/WB stage were earlier than the EX/MEM stage, then there would be a serious problem for the LW instruction. The write-back stage would occur too early and the data memory accessed too late.