-
Notifications
You must be signed in to change notification settings - Fork 1
/
RType.h
158 lines (141 loc) · 3.17 KB
/
RType.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <fstream>
#include <sstream>
#include <bitset>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class RType
{
private:
vector <string> instructions;
vector <string> opcode;
int error = 0;
vector <string> funct3;
vector <string> funct7;
// instruction : 0->31
// |opcode (7) | rd (5) | funct3 | rs1(5) | rs2 (5) | funct7 |
//Extraction of integers from instruction for Machine code generation
vector<int> intextraction(string a)
{
vector<int> final;//Containing all the numeric values of parameters
int sum,currentint;
for(int index=0 ; index < a.size() ; index++)
{
sum = 0;
int flag =0;
//handling Error
if(index < a.size() && isdigit(a[index]))
{
if(a[index-1]!='x')
error = -1;
}
while(index < a.size() && isdigit(a[index]))
{
currentint =a[index] - '0';
sum = sum* 10 + currentint;
index++;
flag=1;
}
if(flag==1)
{
final.push_back(sum);
}
}
return final;
}
public:
void initialise(string parameter)
{
ifstream myfile(parameter.c_str());
string line;
while(getline(myfile,line))
{
stringstream ss(line);
string stemp;
ss >> stemp;
instructions.push_back(stemp);
ss >> stemp;
opcode.push_back(stemp);
ss >> stemp;
funct3.push_back(stemp);
ss >> stemp;
funct7.push_back(stemp);
}
}
//Checking the list of instructions and checking whether its R type or not
bool isPresent(string line)
{
stringstream ss(line);
string ins;
ss>>ins;
int flag=0;
for(auto i=instructions.begin() ;i<instructions.end() ; i++ )
{
if(*i==ins)
{
flag=1;
break;
}
}
if(flag)
return true;
else
return false;
}
bitset<32> decode(string instruction)
{
bitset<32> Machinecode;
stringstream ss(instruction);
string action;
ss >> action;
vector<int> parameter=intextraction(instruction);
if(error==-1)
{
for(int i=0;i<32;i++)
Machinecode[i]=-1;
error=0;
return Machinecode;
}
int index=find(instructions.begin(),instructions.end(),action)-instructions.begin();
string opcodestr,funct3str,funct7str;
opcodestr=opcode[index];
funct3str=funct3[index];
funct7str=funct7[index];
if(parameter.size()!=3 || parameter[0]<0 || parameter[0]>31 || parameter[1]<0 || parameter[1]>31 || parameter[2]<0 || parameter[2]>31 )
{
for(int i=0;i<32;i++)
Machinecode[i]=-1;
return Machinecode;
}
bitset<5> rd(parameter[0]);
bitset<5> rs1(parameter[1]);
bitset<5> rs2(parameter[2]);
for(int i=0;i<7;i++)
{
Machinecode[i] = (opcodestr[opcodestr.size()-1-i] == '0')?0:1;
}
for(int i=0;i<5;i++)
{
Machinecode[i+7] = rd[i];
}
for(int i=0;i<3;i++)
{
Machinecode[i+12] = (funct3str[funct3str.size()-1-i] == '0')?0:1;
}
for(int i=0;i<5;i++)
{
Machinecode[i+15] = rs1[i];
}
for(int i=0;i<5;i++)
{
Machinecode[i+20] = rs2[i];
}
for(int i=0;i<7;i++)
{
Machinecode[i+25] = (funct7str[funct7str.size()-1-i] == '0')?0:1;
}
return Machinecode;
}
};