lab-04-Rushilwiz/instructions.c
2024-03-22 17:20:01 -04:00

69 lines
2.3 KiB
C

// PID: 730677144
// I pledge the COMP 211 honor code.
#include <stdint.h>
#include <stdlib.h>
#include "bit_utils.h"
#include "instructions.h"
// ------------------------------------
// Determines whether instruct is an
// R-type or I-type instruction
//
// Arguments: an unsigned 32-bit integer
//
// Return: instruction_type: R_TYPE or I_TYPE (see structures)
//
instruction_type get_type_of_instruction(uint32_t instruct) {
if (bit_select(instruct, OPCODE_START_BIT, OPCODE_END_BIT) == 0) {
return R_TYPE;
} else {
return I_TYPE;
}
} // end get_type_of_instruction() function
// ------------------------------------
// Creates an R-type instruction
// based on the integer given (see structures)
//
// Arguments: an unsigned 32-bit integer
//
// Return: a pointer to an r_instruction (see structures).
// This consists of the following structure members
// you will have to set: rs, rt, rd, shamt, func
//
r_instruction* create_r_instruction(uint32_t instruct) {
r_instruction* r = malloc(sizeof(r_instruction));
r->rs = bit_select(instruct, RS_START_BIT, RS_END_BIT);
r->rt = bit_select(instruct, RT_START_BIT, RT_END_BIT);
r->rd = bit_select(instruct, RD_START_BIT, RD_END_BIT);
r->shamt = bit_select(instruct, SHAMT_START_BIT, SHAMT_END_BIT);
r->func = bit_select(instruct, FUNC_START_BIT, FUNC_END_BIT);
return r;
} // end create_r_instruction() function
// ------------------------------------
// Creates an I-type instruction
// based on the integer given (see structures)
//
// Arguments: an unsigned 32-bit integer
//
// Return: a pointer to an i_instruction (see structures).
// This consists of the following structure members
// you will have to set: opcode, rs, rt, immediate
//
i_instruction* create_i_instruction(uint32_t instruct) {
i_instruction* i = malloc(sizeof(i_instruction));
i->opcode = bit_select(instruct, OPCODE_START_BIT, OPCODE_END_BIT);
i->rs = bit_select(instruct, RS_START_BIT, RS_END_BIT);
i->rt = bit_select(instruct, RT_START_BIT, RT_END_BIT);
i->immediate = bit_select(instruct, IMMEDIATE_START_BIT, IMMEDIATE_END_BIT);
return i;
} // end create_i_instruction() function