function FETCH();
function DECODE(inst);
function ALU(A, B, ALUcontrol);
function MEM(alu_res, data);
function WRITEBACK(data);
 
function DATAPATH() {
	inst, RR1, RR2, RD1, RD2, WR, IMM, A, B, alu_res, mem_res, data;
	inst = FETCH();
	A,B = decode(inst);
	alu_res = ALU(A, B, ALUcontrol);
	data = MEM(alu_res, RD2, MemWrite, MemRead);
	WRITEBACK(data, WR, RegWrite);
}
 
function FETCH() {
	inst = IM(PC); // read instruction at address from PC
	PC = Add(PC, 4); // update PC
	return inst;
}
 
function DECODE(inst) {
	RR1 = inst[21:25]; // $rs
	RR2 = inst[16:20]; // $rd/rt
	RD1, RD2 = RegRead(RR1, RR2); // read from registers
 
	WR = Mux(
		inst[16:20] // $rt
		inst[11:15] // $rd
		RegDst); // control signal: 0 for $rt, 1 for $rd
 
	IMM = SignExtend(inst[0:15]);
	return [RD1, 
		Mux(RD2, 
		IMM, 
		ALUSrc)]; // control signal: 0 for $rt, 1 for IMM
}
 
function ALU(A, B, ALUcontrol) {
	case 0000: return [A & B, A & B  == 0]; // AND
	case 0001: return [A | B, A | B  == 0]; // OR
	case 0010: return [A + B, A + B  == 0]; // ADD
	case 0110: return [A - B, A - B  == 0]; // SUB
	case 0111: return [A < B, A < B  == 0]; // SLT
	case 1100: return [~ (A | B), ~ (A | B)  == 0]; // NOR
}
 
function MEM(alu_res, data) {
	mem_res = DataMem(
		alu_res,
		data,
		MemWrite, // 1 if write, 0 if no write
		MemRead // 1 if read, 0 if not
	);
}
 
function WRITEBACK(data) {
	RegWrite(data,
		WR,
		RegWrite); // 1 if write, 0 if no write
}

Control Signals

SignalStagePurposeValue
RegDstDecodeSelect destination register number0: $rt
1: $rd
RegWriteDecode/WritebackEnable write of register0: no write
1: write
ALUSrcALUSelect 2nd operand for ALU0: $rt
1: IMM
ALUcontrolALUSelect operation to be performed0000: AND
0001: OR
0010: ADD
0110: SUB
0111: SLT
1100: NOR
MemReadMemoryEnable reading of data memory0: no read
1: write
RegWriteDecode/WritebackEnable write of register0: no write
1: write
MemToRegWritebackSelect result to be written back0: ALU result
1: Memory data
PCSrcMemory/WritebackSelect next $PC value0: $PC + 4
1: $(PC + 4) + IMM

Stages

StageActionInputOutput
FETCHGets the instruction from the instruction address.
Adds 4 to the PC.
Instruction
DECODEReads the opcode to determine instruction type and field lengths.
Reads data from all necessary registers.
InstructionOperand 1 and 2 for ALU
ALUComputes resultsOperands 1 and 2Result, as well as isZero
MEMORYReads calculated address from ALU stage and data to be stored if any
Reads/writes from memory
Calculated address and result from ALUData read from memory or result from ALU
WRITEBACKRead result from memory stage to WriteData and WR
Writes result
Result from MEMORY

Examples

Branch instruction beq

![](../../../schoolwork/cs2100/MIPS/media/Pasted image 20240913181127.png)

f

I-Format lw

![](../../../schoolwork/cs2100/MIPS/media/Pasted image 20240913181402.png)

I-Format sw

![](../../../schoolwork/cs2100/MIPS/media/Pasted image 20240913181443.png)

R-format

![](../../../schoolwork/cs2100/MIPS/media/Pasted image 20240913181509.png)