-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain1.cpp
More file actions
132 lines (105 loc) · 3.04 KB
/
main1.cpp
File metadata and controls
132 lines (105 loc) · 3.04 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
#include <iostream>
#include <fstream>
#include "stdlib.h"
#include <iomanip>
#include <cstring>
#include <bits/stdc++.h>
#include "compressed.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;
if (ComInsWord == 0)
{
return "Illegal Instruction!\n";
}
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 inCstruction
// cout << "Error Reading machine code \n";
return "Error Reading machine code \n";
break;
}
}
return "errorr\n";
}
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)
{
if (instWord == 0)
{
AssemblyInstruction = "Illegal Instructions!";
}
// compression encoding
else 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)\n";
pc += 4;
}
outFile << "0x" << hex << pc2 << setw(6) << "\t" << AssemblyInstruction;
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;
}