/** ALU - Stage 3 */ public static void stage3_execute() throws IOException { String binary_rep = pad_binary(Integer.toBinaryString(cpu.IR), 32); String op_code = (String) binary_rep.subSequence(binary_rep.length() - 6, binary_rep.length()); int immediate_value = 0; if (op_code.equals(definitions.STW)) // If the operation is a STW (store) { String imm_val = binary_rep.substring(10, 26); immediate_value = calc_imm_val(imm_val); // Get the immediate value cpu.RZ = ALU(cpu.c_ALU_op, cpu.RA, immediate_value); // RZ = ALU result from RA and immediate value cpu.RM = cpu.RB; // RM = RB } else // If the operation isn't a store { if (cpu.c_mux_B_select == 0) // If mux_B = 0 then use RB { cpu.RZ = ALU(cpu.c_ALU_op, cpu.RA, cpu.RB); // Set RZ = ALU result from RA and RB } else if (cpu.c_mux_B_select == 1) // If mux_B = 1 then use immediate value { String imm_val = binary_rep.substring(10, 26); immediate_value = calc_imm_val(imm_val); // Get the immediate value cpu.RZ = ALU( cpu.c_ALU_op, cpu.RA, immediate_value); // RZ = ALU result from RA and immediate value } if (cpu.c_condition_signal == 1) // If the condition signal is set then condition passed { String imm_val = ""; if (op_code.equals(definitions.BR)) // If the condition is BR (branch) { imm_val = binary_rep.substring(0, 26); // Get the immediate value (B-Type) } else { imm_val = binary_rep.substring(10, 26); // Get the immediate value (I-Type) } short imm_value = (short) Integer.parseInt(imm_val, 2); immediate_value = imm_value; cpu.R[15] = cpu.R[15] + immediate_value; // Update the program counter (PC) } } }
/** Reset all of the control signals (after each instruction execution in Stage 5) */ public static void reset_control_signals() { cpu.c_reg_address_A = 0; cpu.c_reg_address_B = 0; cpu.c_reg_address_C = 0; cpu.c_mux_B_select = 0; cpu.c_mux_C_select = 0; cpu.c_mux_Y_select = 0; cpu.c_RF_write = 0; cpu.c_condition_signal = 0; cpu.c_ALU_op = 0; cpu.RA = 0; cpu.RB = 0; cpu.RM = 0; cpu.RY = 0; cpu.RZ = 0; }