-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
192 lines (163 loc) · 4.2 KB
/
main.cpp
File metadata and controls
192 lines (163 loc) · 4.2 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <iostream>
#include <fstream>
#include "stdlib.h"
#include <iomanip>
#include <cstring>
#include <bits/stdc++.h>
#include "compressed.h"
#include "32_bit.h"
#include "ThirtyTwoBit.h"
using namespace std;
unsigned int pc = 0x0;
unsigned char memory[8*1024]; // only 8KB of memory located at address 0
void emitError(char *s)
{
cout << s;
exit(0);
}
string CompressedIns ( unsigned int ComInsWord)
{
unsigned int opcode = ComInsWord & 0x0003;
switch (opcode)
{
case 0:
{
return QuadrantZero(ComInsWord); //opcode 00
break;
}
case 1:
{
return QuadrantOne(ComInsWord, pc); //opcode 01
break;
}
case 2:
{
return QuadrantTwo(ComInsWord); //opcode 10
break;
}
default:
{
// return to main?? & re-read the instruction
// cout << "Error Reading machine code \n";
return "Error Reading machine code \n";
break;
}
}
}
string thirtyTwo_bit_inst(unsigned int instWord){
unsigned int opcode;
//unsigned int instPC = pc - 4;
opcode = instWord & 0x0000007F;
switch(opcode){
case 111:
{
return JFormat(instWord,pc);
break;
}
case 55:
{
return UFormat(instWord);
break;
}
case 23:
{
return UFormat(instWord);
break;
}
case 51:
{
return r_type(instWord);
break;
}
case 19:
{
return i_type(instWord);
break;
}
case 3:
{
return i_type_load(instWord);
break;
}
case 35:
{
return SFormat(instWord);
break;
}
case 99:
{
return BFormat(instWord,pc);
break;
}
case 103:
{
return jalr_type(instWord);
break;
}
case 115:
{
return ecall_func();
break;
}
default:
{
// return to main?? & re-read the instruction
// cout << "Error Reading machine code \n";
return "Error Reading machine code \n";
break;
}
}
}
void RUN (int argc, char *argv[])
{
unsigned int instWord=0;
ifstream inFile;
ofstream outFile;
string AssemblyInstruction;
unsigned int pc2 = 0x0;
memset(memory, 0, sizeof(8*1024));
if(argc<2) emitError("use: rvcdiss <machine_code_file_name>\n");
string outputFilename = argv[1];
string::size_type pos = outputFilename.find('.');
if (pos != std::string::npos)
{
outputFilename = outputFilename.substr(0, pos);
}
outputFilename = outputFilename + ".s\n";
outFile.open (outputFilename, ios::out);
inFile.open(argv[1], ios::in | ios::binary | ios::ate);
if(inFile.is_open())
{
int fsize = inFile.tellg();
inFile.seekg (0, inFile.beg);
if(!inFile.read((char *)memory, fsize)) emitError("Cannot read from input file\n");
while(true){
instWord = (unsigned char)memory[pc] | (((unsigned char)memory[pc+1])<<8);
pc2 = pc;
if ((instWord & 0x0003 ) != 0x0003)
{
// compression encoding
AssemblyInstruction = CompressedIns(instWord);
pc += 2;
}
else
{
instWord = instWord | (((unsigned char)memory[pc+2])<<16) |
(((unsigned char)memory[pc+3])<<24);
// normal encoding
AssemblyInstruction = thirtyTwo_bit_inst(instWord);
pc += 4;
}
// remove the following line once you have a complete simulator
outFile << "0x" << hex << pc2 << setw(6) << "\t" << AssemblyInstruction << "\n";
if( memory[pc] == NULL) break;
}
} else emitError("Cannot access input file\n");
inFile.close();
outFile.close();
}
int main(int argc, char *argv[])
{
RUN (argc, argv);
return 0;
}