-
Notifications
You must be signed in to change notification settings - Fork 1
/
ALU.h
112 lines (102 loc) · 2.18 KB
/
ALU.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once
#include <bitset>
#include <iostream>
#include <fstream>
#include <string>
#include"InterStateBuffers.h"
using namespace std;
class ALU {
private:
int RA,RB;
unsigned RAU,RBU;
public:
bool state;
int result;
void compute(InterStateBuffers &object)
{
string ins = object.ALU_OP;
RA = object.RA.readInt(); // reading both values before hand
RB = object.RB.readInt();
RAU = object.RA.readInt(); // reading both values before hand
RBU = object.RB.readInt();
if(ins == "add"){
result = RA + RB;
object.RZ.writeInt(result);
}
else if(ins == "sub"){
result = RA - RB;
object.RZ.writeInt(result);
}
else if(ins == "mul"){
result = RA * RB;
object.RZ.writeInt(result);
}
else if(ins == "div"){
result = RA / RB;
object.RZ.writeInt(result);
}
else if(ins == "rem"){
result = RA % RB;
object.RZ.writeInt(result);
}
else if(ins == "and"){
result = RA & RB;
object.RZ.writeInt(result);
}
else if(ins == "or"){
result = RA | RB;
object.RZ.writeInt(result);
}
else if(ins == "xor"){
result = RA ^ RB;
object.RZ.writeInt(result);
}
else if(ins == "sll"){
result = RA << RB ;
object.RZ.writeInt(result);
}
else if(ins == "srl"){
result = RA >> RB;
object.RZ.writeInt(result);
}
else if(ins == "blt"){
state = (RA < RB) ? 1 : 0;
}
else if(ins == "ble"){
state = (RA <= RB) ? 1 : 0;
}
else if(ins == "bgt"){
state = (RA > RB) ? 1 : 0;
}
else if(ins == "bge"){
state = (RA >= RB) ? 1 : 0;
}
else if(ins == "bltu"){
state = (RAU < RBU) ? 1 : 0;
}
else if(ins == "beq"){
state = RA == RB ? 1 : 0;
}
else if(ins == "bne"){
state = RA != RB ? 1 : 0;
}
else if(ins == "bgtu"){
state = (RAU > RBU) ? 1 : 0;
}
else if (ins == "sra"){ //arithmetic shift
int shift = RB;
bitset <32> finalresult = RA;
int size = finalresult.size();
int MSB = finalresult[size - 1];
finalresult >> shift;
for(int index = size - shift; index < size ; index++ )
{
finalresult[index] = MSB;
}
object.RZ.writeInt(finalresult.to_ulong());
}
else{
cout<<"Sorry instruction "<<ins<< " not found ! "<<endl;
}
}
};