Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions ALU.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
#include "ALU.h"

/* This class functions as the Arithmetic Logic Unit by performing arithmetic and
* logical operations based on a given hex input value and outputs the result of the
* operation performed.
*/

// Set-up ALU with default values
ALU::ALU(){
input1 = "0x00000000";
input2 = "0x00000000";
result = "0x00000000";
}


// testing
// void ALU::setInputs(int first, int second){
// input1 = first;
// input2 = second;
// }


// Set string values as inputs for ALU
void ALU::setInputs(string first, string second){
input1 = first;
input2 = second;
}


// First calculation method for ALU given a control signal
void ALU::calculate(string _control){
control = _control;
stringstream ss;

// Control signal = 2
if(control == "0x2"){
one = stol(input1.substr(2), nullptr, 16);
two = stol(input2.substr(2), nullptr, 16);
Expand All @@ -31,6 +45,8 @@ void ALU::calculate(string _control){
}else{
result = "0x" + ss.str();
}

// Control signal = 6
}else if(control == "0x6"){
one = stol(input1.substr(2), nullptr, 16);
two = stol(input2.substr(2), nullptr, 16);
Expand All @@ -43,6 +59,8 @@ void ALU::calculate(string _control){
}else{
result = "0x" + ss.str();
}

// Control signal = 9
}else if(control == "0x9"){
one = stol(input1.substr(2), nullptr, 16);
two = stol(input2.substr(2), nullptr, 16);
Expand All @@ -52,43 +70,56 @@ void ALU::calculate(string _control){
}else{
result = "0x0";
}

// testing
// ss << std::hex << calc;
// if (ss.str().size()>8){
// result = "0x" + ss.str().substr(ss.str().size()-8);
// }else{
// result = "0x" + ss.str();
// }

}else{
cout << control << endl;
cerr << "Wrong ALU Control signal" << endl;
exit(1);
}

// testing
// stringstream toHex;
// toHex << std::hex << result;
// string s = toHex.str();
// if (s.size()>8){
// s = s.substr(s.size()-8);
// }
// result = stoi(s, nullptr, 16);

if(calc == 0){
zeroValue = "0x1";

}else{
zeroValue = "0x0";
}

}


// Second calulation method for ALU without a given control signal
void ALU::calculate(){
stringstream ss;
one = stol(input1.substr(2), nullptr, 16);
two = stol(input2.substr(2), nullptr, 16);
calc = one + two;
ss << std::hex << calc;

if (ss.str().size()>8){
result = "0x" + ss.str().substr(ss.str().size()-8);

}else{
result = "0x" + ss.str();
}

// testing
// result = input1 + input2;
// stringstream toHex;
// toHex << std::hex << result;
Expand All @@ -97,13 +128,17 @@ void ALU::calculate(){
// s = s.substr(s.size()-8);
// }
// result = stoi(s, nullptr, 16);

if(calc == 0){
zeroValue = "0x1";

}else{
zeroValue = "0x0";
}
}


// Returns the input values to the ALU in hexadecimal
string ALU::inputs(){
stringstream toHex;
toHex << "First input: ";
Expand All @@ -115,6 +150,8 @@ string ALU::inputs(){
return toHex.str();
}


// Returns the output values to the ALU in hexadecimal
string ALU::outputs(){
stringstream toHex;
toHex << "Output: ";
Expand Down
14 changes: 14 additions & 0 deletions ALU.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,36 @@
#include <iostream>

using namespace std;

/* This class functions as the Arithmetic Logic Unit by performing arithmetic and
* logical operations based on a given hex input value and outputs the result of the
* operation performed.
*/

class ALU{
public:
// Set-up ALU with default values of hex 0
ALU();

// void setInputs(int first, int second);

// Sets string values as inputs for ALU
void setInputs(string first, string second);

// Two calculate methods, with or without control signals
void calculate(string _control);
void calculate();

// Returns the input values to the ALU in hexadecimal
string inputs();
// Returns the output values to the ALU in hexadecimal
string outputs();

// Getter methods
string getZeroValue(){return zeroValue;};
string getResult(){return result;};
string getControlSignal(){return control;};

private:
string input1;
string input2;
Expand Down
32 changes: 24 additions & 8 deletions ALUControl.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
#include "ALUControl.h"

/* This class computes the output signal of the ALU based on either a control signal
* or a function code and returns the values for ALUOp1, ALUOp2, and the function code
*/

// Set up ALU Control
void ALUControl::setControl(string op1, string op2){
control = op1.substr(2) + op2.substr(2);
}


// Compute output signal based on the control signal (I-type) or opcode (R-Type)
void ALUControl::compute(){
// LW and SW == ADD
if(control == "00"){
// LW and SW == add
output = "0x2";

// BEQ == SUB
}else if(control == "01"){
// Beq == sub
output = "0x6";

// R-type
}else{
// R-type
// ADD
if (funcCode == "100000"){
// add
output = "0x2";

// SUB
}else if(funcCode == "100010"){
// sub
output = "0x6";

// AND
}else if(funcCode == "100100"){
// and
output = "0x0";
// OR
}else if(funcCode == "100101"){
// or
output = "0x1";

// SLT
}else if(funcCode == "101010"){
// set on less than
output = "0x9";
}
}
}



string ALUControl::inputs(){
// testing
// cout << control << ' ' << funcCode << ' ' << output;

stringstream ss;
ss << "Inputs: ";
ss << "\n ALUOp1: 0x" << control.at(0);
Expand Down
13 changes: 13 additions & 0 deletions ALUControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@
#include <sstream>

using namespace std;

/* This class computes the output signal of the ALU based on either a control signal
* or a function code and returns the values for ALUOp1, ALUOp2, and the function code
*/

class ALUControl{
public:
ALUControl(){};

// Set up ALU Control
void setControl(string op1, string op2);

// Compute output signal based on the control signal (I-type) or function code (R-Type)
void compute();

// Sets function code based on given string of numbers
void setFuncCode(string f){funcCode = f;};

// Returns values for ALUOp1, ALUOp2, and the function code
string inputs();
// Returns output
string outputs();

private:
Expand Down
Loading