-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUJType.h
121 lines (107 loc) · 2.88 KB
/
UJType.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
#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<bitset>
#include<algorithm>
#include<ctype.h>
using namespace std;
class UJType{
// instruction imm[20][10:1][11][19:12] | rd[11:7] | opcode[6:0]
private:
vector <string> instructions;
vector <string> opcode;
public:
// initialise the vectors with their respective values from the input file
void initialise (string filename);
//checks if given command is present in the list of UJ Type instructions
bool isPresent(string command);
//Converts string instruction to 32 bit machine code
bitset <32> decode (string instruction);
};
void UJType::initialise (string filename){
ifstream ifile(filename.c_str());
string instName, instOpcode;
while(ifile >> instName >> instOpcode){
instructions.push_back(instName);
opcode.push_back(instOpcode);
}
}
bool UJType::isPresent (string inst){
int i=0;
string instName;
while(!isalnum(inst[i])) i++; //isalnum checks whether inst[i] is digit,lower or upper case letter
while(isalpha(inst[i])){
instName.push_back(inst[i]);
i++;
}
vector <string> :: iterator itr = find(instructions.begin(), instructions.end(), instName) ;
if(itr == instructions.end()) return false;
return true;
}
bitset <32> UJType::decode (string instruction){
bitset <32> machineCode;
string instName, rdName, labelOffset;
int offset = 0;
bool isNegative = false;
int error =0;
//Splitting instruction into various parts, ignoring whitespaces and commas
int rdNum, i=0;
while(!isalnum(instruction[i])) i++;
while(isalnum(instruction[i])){
instName.push_back(instruction[i]);
i++;
}
while(!isalnum(instruction[i])) i++;
while(isalnum(instruction[i])){
rdName.push_back(instruction[i]);
i++;
}
while(!isdigit(instruction[i])){
if( instruction[i] == '-') isNegative = true;
if(isalpha(instruction[i])){
error=1;
for(int i=0;i<32;i++)
machineCode[i]=-1;
error = 0;
return machineCode;
}
i++;
}
while(isdigit(instruction[i])){
labelOffset.push_back(instruction[i]);
i++;
}
//index for opcode
int index = find(instructions.begin(), instructions.end(), instName) - instructions.begin();
// Finding register no.
if(rdName.size()==2)
rdNum = rdName[1] - '0';
else
rdNum = 10*(rdName[1]-'0') + (rdName[2]-'0');
bitset <5> rd(rdNum);
// Finding offset
for(i=0; labelOffset[i]!='\0'; i++)
offset = offset*10 + (labelOffset[i] - '0');
if(isNegative)
offset *= -1;
bitset <20> imm(offset);
if(rdNum<0 || rdNum >31)
{
for(int i=0;i<32;i++)
machineCode[i]=-1;
return machineCode;
}
// Generatng machine code
for(int i=0; i<7; i++)
machineCode[i] = opcode[index][6-i] - '0';
for(int i=0; i<5; i++)
machineCode[7+i] = rd[i];
for(int i=0; i<8; i++)
machineCode[12+i] = imm [i+11];
machineCode[20] = imm[10];
machineCode[31] = imm[19];
for(int i=0; i<11; i++)
machineCode[21+i] = imm[i];
return machineCode;
}