forked from Nilanshrajput/AC2MC-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IType.h
144 lines (114 loc) · 4.09 KB
/
IType.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
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <bitset>
#include <algorithm>
using namespace std;
class IType {
private:
vector <string> instructions; // instruction : 0->31 | opcode (7) | rd (5) | funct3 | rs1(5) | rs2 (5) | funct7 |
vector <string> opcode;
vector <string> funct3;
/* Function extracts the all the integers in an instruction basically */
/* for ex : lw x2,42(x3) : will extract 2,24,3 i.e parameters needed for MC generation.*/
vector <int> extractint(string str) { // recieves a string and extracts all the integers and returns them in a list (vector)
vector <int> result;
int sum,currentint, sum2;
for(int strIndex = 0 ; strIndex < str.size() ; strIndex++) {
sum = 0;
bool intfound = 0, intfound2 = 0;
while(strIndex < str.size() && isdigit(str[strIndex])) {
currentint = str[strIndex] - '0';
sum = sum*10 + currentint;
strIndex++;
intfound = 1;
}
if(str[strIndex] == '-'){
sum2 = 0;
strIndex++;
while(strIndex < str.size() && isdigit(str[strIndex])){
currentint = str[strIndex] - '0';
sum2 = sum2*10 + currentint;
strIndex++;
intfound2 = 1;
}
sum2 = sum2*(-1);
}
if(intfound)
result.push_back(sum);
if(intfound2)
result.push_back(sum2);
}
return result; //returning vector of extracted parameters
}
public:
// initialise the vectors with their respective values from the input file.
void initialise (string filename) {
ifstream ifile(filename.c_str());
string line;
while(getline(ifile,line)) {
stringstream ss(line);
string token;
ss >> token;
instructions.push_back(token);
ss >> token;
opcode.push_back(token);
ss >> token;
funct3.push_back(token);
}
}
// checks if given command is present in the list of S Type instructions.
bool isPresent(string command) {
stringstream ss(command);
string ins;
ss >> ins;
vector <string> :: iterator it = find(instructions.begin(),instructions.end(),ins);
if(it == instructions.end())
return false;
else
return true;
}
bitset <32> decode (string instruction) {
/*Edit this*/
bitset <32> MachineCode;
stringstream ss(instruction); //helpful for tokenizing space separated strings.
string insname;
vector <int> parameters = extractint(instruction); // extracted all register names, offsets etc.
string action;
ss >> action;
int index = find(instructions.begin(),instructions.end(),action) - instructions.begin();
string opcodestr,funct3str;
opcodestr = opcode[index];
funct3str = funct3[index];
bitset <12> immediate; // loading offset
bitset <5> rd, rs1;
if(action[0] == 'l' || action[0] == 'j')
{
immediate = parameters[1];
rd = parameters[0];
rs1 = parameters[2];
}
else{
immediate= (parameters[2]); // loading offset
rd = (parameters[0]);
rs1 = (parameters[1]);
}
for(int i=0;i<7;i++) {
MachineCode[i] = (opcodestr[opcodestr.size()-1-i] == '0') ? 0 : 1; //copying opcode string to the opcode field
}
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()-i-1] == '0') ? 0 : 1;
}
for(int i=0;i<5;i++)
MachineCode[i+15] = rs1[i];
for(int i=0;i<12;i++)
MachineCode[i+20] = immediate[i];
return MachineCode;
}
};