-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemu.cpp
194 lines (147 loc) · 5.68 KB
/
emu.cpp
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
193
194
#include "Emulator.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ends;
using std::flush;
using std::unitbuf;
using std::nounitbuf;
using std::hex;
using std::dec;
void print_options()
{
cout<<"Options :-"<<endl;
cout<<"\tt - Execute one instruction with trace"<<endl;
cout<<"\ts - Execute all instrutions with trace"<<endl;
cout<<"\tn number - Execute specified number of instructions"<<endl;
cout<<"\ta - Execute all instrucions without trace"<<endl;
cout<<"\tq - Show current architectural state"<<endl;
cout<<"\tc - Display number of instructions executed so far"<<endl;
cout<<"\tu - Disassemble"<<endl;
cout<<"\td - Show memory dump"<<endl;
cout<<"\tx - Quit the emulator"<<endl;
return;
}
int main(int argc, char** argv)
{
cout<<unitbuf;
cin.tie(&cout);
if(argc==1)
{
cout<<"No input file specified"<<endl;
return EXIT_FAILURE;
}
Emulator e1(argv[1]);
int loading = e1.loader();
if(loading)
cout<<"Object file loaded successfully"<<endl<<endl;
else
{
cout<<"Error in loading object file"<<endl;
cout<<"Process terminated"<<endl;
return EXIT_FAILURE;
}
print_options();
cout<<endl;
char c; // interact with the user
do
{
cin>>c;
switch(c)
{
case 'a': // execute all instructions in one go
if(e1.get_program_status()==-1) // program in fault state
{
cout<<e1.get_fault_cause()<<endl<<endl;
break;
}
if(e1.get_program_status()==0)
{
cout<<"Program finished execution"<<endl<<endl;
break;
}
while(e1.get_program_status()==1)
e1.execute();
if(e1.get_program_status()==-1) // program in fault state after completing execution
{
cout<<e1.get_fault_cause()<<endl<<endl;
break;
}
cout<<"Total instructions executed = "<<e1.instructions_executed()<<endl;
cout<<endl;
break;
case 't': // execute a single instruction with trace
if(e1.get_program_status()==-1)
{
cout<<e1.get_fault_cause()<<endl;
cout<<endl;
break;
}
if(e1.get_program_status()==0)
{ cout<<"Program finished execution"<<endl;
cout<<endl;
break;
}
cout<<e1.execute()<<endl;
cout<<endl;
break;
case 's': // execute all instrutions with trace
if(e1.get_program_status()==-1) // program in fault state
{
cout<<e1.get_fault_cause()<<endl<<endl;
break;
}
if(e1.get_program_status()==0)
{
cout<<"Program finished execution"<<endl<<endl;
break;
}
while(e1.get_program_status()==1)
cout<<e1.execute()<<endl;
cout<<endl;
cout<<"Total instructions executed = "<<e1.instructions_executed()<<endl;
cout<<endl;
break;
case 'n': // print specified number of instructions
{
int a = 0;
cin>>a;
int i = 0;
while(i<a && e1.get_program_status()==1)
{
i++;
cout<<e1.execute()<<endl;
}
cout<<endl;
}
break;
case 'q': // show current architectural state
cout<<e1.current_state()<<endl;
cout<<endl;
break;
case 'u': // disassemble
e1.disassemble(cout);
cout<<endl;
break;
case 'd': // outputs memory dump
e1.memory_dump(cout);
break;
case 'c': // number of instructions executed thus far
cout<<"Number of instructions executed = "<<e1.instructions_executed()<<endl;
cout<<endl;
break;
case 'x': // QUIT
cout<<"Emulation terminated"<<endl;
cout<<endl;
break;
default: cout<<"Invalid option"<<endl;
print_options();
cout<<endl;
continue;
break;
}
} while (c!='x');
cout<<nounitbuf;
return EXIT_SUCCESS;
}