Nov 19, 2015 at 4:29pm UTC
/* ---------------Add, Sub, AND, OR, lw, sw, addi, beq, bne Fuctions------------------------
Functions perform operations on registers and store the resulting value in the destination register, rd and displays the value */
//Add function
void add_funct(int regFile[], int rdVal, int rsVal, int rtVal)
{
regFile[rdVal] = regFile[rsVal] + regFile[rtVal];
cout << "$ " << rdVal << " = " << regFile[rdVal] << endl;
}
//Subtract function
void sub_funct(int regFile[], int rdVal, int rsVal, int rtVal)
{
regFile[rdVal] = regFile[rsVal] - regFile[rtVal];
cout << "$ " << rdVal << " = " << regFile[rdVal] << endl;
}
//AND function
void and_funct(int regFile[], int rdVal, int rsVal, int rtVal)
{
string rsBin_Num;
string rtBin_Num;
string rdBin_Num;
rsBin_Num = dec_binary(regFile[rsVal]);
rtBin_Num = dec_binary(regFile[rtVal]);
//making the rs string 32 bits long
if (rsBin_Num.length() != 32)
{
rsBin_Num.insert(0, (32 - rsBin_Num.length()), '0');
}
//making the rt string 32 bits long
if (rtBin_Num.length() != 32)
{
rtBin_Num.insert(0, (32 - rtBin_Num.length()), '0');
}
for (int i = 0; i < 32; i++)
{
if (rsBin_Num[i] == '1' && rtBin_Num[i] == '1')
{
rdBin_Num.append("1");
regFile[rdVal] += pow(2.0, (31 - i));
}
else
rdBin_Num.append("0");
}
cout << "$" << rdVal << " = " << regFile[rdVal] << endl;
}
//OR function
void or_funct(int regFile[], int rdVal, int rsVal, int rtVal)
{
string rsBin_Num;
string rtBin_Num;
string rdBin_Num;
rsBin_Num = dec_binary(regFile[rsVal]);
rtBin_Num = dec_binary(regFile[rtVal]);
if (rsBin_Num.length() != 32)
{
rsBin_Num.insert(0, (32 - rsBin_Num.length()), '0');
}
if (rtBin_Num.length() != 32)
{
rtBin_Num.insert(0, (32 - rtBin_Num.length()), '0');
}
for (int i = 0; i < 32; i++)
{
if (rsBin_Num[i] == '1' || rtBin_Num[i] == '1')
{
rdBin_Num.append("1");
regFile[rdVal] += pow(2.0, (31 - i));
}
else
rdBin_Num.append("0");
}
cout << "$" << rdVal << " = " << regFile[rdVal] << endl;
}
//load word function
void lw_funct(int regFile[], int memory[], int rtVal, int rsVal, int addrs)
{
regFile[rtVal] = memory[regFile[rsVal] + addrs];
cout << "$ " << rtVal << " = " << regFile[rtVal] << endl;
}
//store word function
void sw_funct(int regFile[], int memory[], int rtVal, int rsVal, int addrs)
{
memory[regFile[rsVal] + addrs] = regFile[rtVal];
cout << "Mem[" << regFile[rsVal] + addrs << "] = " << memory[regFile[rsVal] + addrs] << endl;
}
//add immediate function
void addi_funct(int regFile[], int rtVal, int rsVal, int immid)
{
regFile[rtVal] = regFile[rsVal] + immid;
cout << "$ " << rtVal << " = " << regFile[rtVal] << endl;
}
//branch on equal function
void beq_funct(int regFile[], int memory[], int rtVal, int rsVal, int offs)
{
if (regFile[rsVal] == regFile[rtVal])
progCounter = +offs;
else
progCounter = progCounter;
cout << "PC = " << progCounter<< endl;
}
//branch not equal function
void bne_funct(int regFile[], int memory[], int rtVal, int rsVal, int offs)
{
if (regFile[rsVal] != regFile[rtVal])
progCounter = + offs;
else
progCounter = progCounter;
cout << "PC = " << progCounter << endl;
}