-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmulator.cpp
More file actions
554 lines (396 loc) · 12.8 KB
/
Copy pathEmulator.cpp
File metadata and controls
554 lines (396 loc) · 12.8 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#include "Emulator.h"
const char Emulator::format_code[8] = "\\\'\'\"\t\f\b";
const map<unsigned, pair<unsigned, string> > Emulator::decoder = { {0, {1, "ldc"}},
{1, {1, "adc"}},
{2, {1, "ldl"}},
{3, {1, "stl"}},
{4, {1, "ldnl"}},
{5, {1, "stnl"}},
{6, {0, "add"}},
{7, {0, "sub"}},
{8, {0, "shl"}},
{9, {0, "shr"}},
{10, {1, "adj"}},
{11, {0, "a2sp"}},
{12, {0, "sp2a"}},
{13, {1, "call"}},
{14, {0, "return"}},
{15, {1, "brz"}},
{16, {1, "brlz"}},
{17, {1, "br"}},
{18, {0, "HALT"}}
};
const map<unsigned, string> Emulator::faults = {{0, "PC goes out of text segment"},
{1, "SP goes out of segment"},
{2, "Program tried to access text segment memory area"},
{3, "Program tried to access out of range memory address"},
{4, "Too large text segment"},
{5, "Too large data segment"},
{6, "Program took too long to finish"},
{7, "Invalid opcode encountered"}
};
Emulator::endianess Emulator::get_endianess()
{
unsigned a = 1;
char* ptr = (char *) &a;
if(*ptr==1)
return lil_endian;
else
return big_endian;
}
const Emulator::endianess Emulator::machine_type = Emulator::get_endianess();
bool Emulator::loader() // no size checking has been done now -> just simple loading
{
ifstream fi(obj_filename, ifstream::binary | ifstream::in);
if(!fi) // ERROR in opening file
{
return false;
}
char waste;
char aux[8] = "\0";
for(unsigned i=0; i<8; ++i)
{
fi.read(aux+i, 1);
}
for(unsigned i=0; i<8; ++i)
{
if(aux[i]!=format_code[i])
{
cout<<i<<" ";
cout<<"Unknown object file format"<<endl;
return false;
}
}
fi.read(&waste, 1);
if(waste!='\n')
{
cout<<"Unknown onject file format"<<endl;
return false;
}
text_size = get_int(fi);
fi.read(&waste, 1); // dump newline character
for(unsigned i=0; i<text_size; ++i) // load text segment
{
mempory_space[i] = get_int(fi);
}
fi.read(&waste, 1); // dump newline character
fi.read(&waste, 1); // dump newline character
data_size = get_int(fi);
fi.read(&waste, 1); // dump newline character
for(unsigned i=0; i<data_size; ++i)
{
mempory_space[i+data_lower] = get_int(fi);
}
PC = 0x00000000; // set PC to 0x0
A = 0x00000000;
B = 0x00000000;
data_upper = data_lower + data_size - 1; // denotes upper boundary of data loaded
SP = 0xff00; // set SP to ?? NOT CONFIRMED
fi.close(); // close the object file
return true; // loading successful
}
unsigned Emulator::get_int(ifstream& fi) const
{
unsigned res = 0;
char *ptr = (char *) &res;
char byte = 0;
if(machine_type==big_endian) // just read byte and put it
{
fi.read(&byte, 1);
*ptr = byte;
ptr++;
fi.read(&byte, 1);
*ptr = byte;
ptr++;
fi.read(&byte, 1);
*ptr = byte;
ptr++;
fi.read(&byte, 1);
*ptr = byte;
}
else // read and put in reverse order
{
ptr += 3;
fi.read(&byte, 1);
*ptr = byte;
ptr--;
fi.read(&byte, 1);
*ptr = byte;
ptr--;
fi.read(&byte, 1);
*ptr = byte;
ptr--;
fi.read(&byte, 1);
*ptr = byte;
}
return res;
}
string Emulator::execute()
{
if(fault==true)
{
return fault_cause;
}
if(finished==true)
return string();
if(text_size==0)
{
finished = true;
return "No text segment to execute";
}
string res = current_state();
int mach_code = mempory_space[PC]; // get current machine code
int opcode = (mach_code<<24)>>24;
int operand = (mach_code & static_cast<int>(0xffffff00)) / 256; // cast required for operations to work correctly work correctly
++PC; // PC incremented to point to next instruction
check_PC(PC);
if(PC>pc_upper)
{
fault = true;
string tmp = faults.find(0)->second;
fault_cause = tmp + " as upper limit for text segment crossed; no HALT encountered";
return fault_cause;
}
switch(opcode)
{
case 0: // ldc value
B = A;
A = operand;
break;
case 1: // adc value
A = A + operand;
break;
case 2: // ldl value // proper checks to be included
check_memory_access(SP+operand);
if(fault==true)
return fault_cause;
B = A;
A = mempory_space[SP+operand];
break;
case 3: // stl value
check_memory_access(SP+operand);
if(fault==true)
return fault_cause;
mempory_space[SP+operand] = A;
A = B;
break;
case 4: // ldnl offset
check_memory_access(A+operand);
if(fault==true)
return fault_cause;
A = mempory_space[A+operand];
break;
case 5: // stnl offset
check_memory_access(A+operand);
if(fault==true)
return fault_cause;
mempory_space[A+operand] = B;
break;
case 6: // add
A = B + A;
break;
case 7: // sub
A = B - A;
break;
case 8: // shl
A = B<<A;
break;
case 9: // shr
A = B>>A;
break;
case 10: // adj value
check_SP(SP+operand);
if(fault==true)
return fault_cause;
SP = SP + operand;
break;
case 11: //a2sp
check_SP(A);
if(fault==true)
return fault_cause;
SP = A;
A = B;
break;
case 12: // sp2a
B = A;
A = SP;
break;
case 13: // call offset
check_PC(PC+operand);
if(fault==true)
return fault_cause;
B = A;
A = PC;
PC = PC+operand;
break;
case 14: // return
check_PC(A);
if(fault==true)
return fault_cause;
PC = A;
A = B;
break;
case 15: // brz offset
if(A==0)
{ check_PC(PC+operand);
if(fault==true)
return fault_cause;
PC = PC+operand;
}
break;
case 16: // brlz offset
if(A<0)
{
check_PC(PC+operand);
if(fault==true)
return fault_cause;
PC = PC+operand;
}
break;
case 17: // br offset
check_PC(PC+operand);
if(fault==true)
return fault_cause;
PC = PC+operand;
break;
case 18: // HALT
finished = true;
break;
default:
fault = true;
fault_cause = faults.find(7)->second;
return fault_cause;
break;
}
++instr_cnt;
if(instr_cnt==max_instruction_count) // looped back to 0 -> limit crossed
{
fault = true;
// indiacte program took too long to finish
ostringstream oss;
oss<<faults.find(6)->second;
oss<<'\n';
oss<<instr_cnt<<" instructions executed so far\n";
fault_cause = oss.str();
return fault_cause;
}
return res;
}
bool Emulator::check_PC(int a)
{
if(a<0 || a>pc_upper)
{
fault = true;
string tmp = faults.find(0)->second;
PC--;
string tmp2 = current_state();
PC++;
fault_cause = tmp + " when executing " + tmp2;
return false;
}
return true;
}
bool Emulator::check_SP(int a)
{
if(a>end_address || a<=data_upper)
{
fault = true;
string tmp = faults.find(1)->second;
PC--;
string tmp2 = current_state();
PC++;
fault_cause = tmp + " when executing " + tmp2;
return false;
}
return true;
}
bool Emulator::check_memory_access(int a)
{
string tmp;
if(a<0 || a>end_address)
{
fault = true;
tmp = faults.find(3)->second;
}
if(a<=pc_upper)
{
fault = true;
tmp = faults.find(2)->second;
}
if(fault==false)
return true;
PC--;
string tmp2 = current_state();
PC++;
fault_cause = tmp + " when executing " + tmp2;
return false;
}
int Emulator::get_program_status() const
{
if(finished==true)
return 0; // finished execution
if(fault==true)
return -1;
return 1; // active
}
string Emulator::reverse_decode(unsigned a) const
{
unsigned opcode = (a<<24)>>24;
ostringstream oss;
map<unsigned, pair<unsigned, string> >::const_iterator mt = decoder.find(opcode);
string mnem = mt->second.second; // storing the mnemonic
oss<<mnem;
if(mt->second.first==1) // operand also present
{
int tmp = a & 0xffffff00;
tmp /= 256;
oss<<" "<<tmp;
}
return oss.str();
}
void Emulator::disassemble(ostream& os) const
{
for(int i=0; i<text_size; ++i)
{
string tmp = reverse_decode(mempory_space[i]);
os<<"PC = ";
os<<setw(10)<<setfill('0')<<i<<setfill(' ')<<" ";
os<<"Memory Content = ";
os<<"0x"<<setw(8)<<setfill('0')<<hex<<mempory_space[i]<<dec<<setfill(' ')<<" ";
cout<<" "<<tmp<<endl;
}
}
void Emulator::memory_dump(ostream& os) const
{
int per_line = 4;
os<<hex<<showbase;
os<<"Text Segment :-\n\tsize = "<<text_size<<'\n';
unsigned i = pc_lower;
while(i<text_size)
{
os<<i<<": "<<mempory_space[i]<<'\n';
i++;
}
os<<"\n\n";
os<<"Data Segment :-\n\tsize = "<<data_size<<'\n';
i = 0;
while(i<data_size)
{
os<<i+data_lower<<": "<<mempory_space[i+data_lower];
os<<" (In decimal: "<<dec<<mempory_space[i+data_lower]<<hex<<")\n";
i++;
}
os<<'\n'<<noshowbase;
os<<dec;
}
string Emulator::current_state() const
{
ostringstream oss;
oss<<setw(11)<<"PC = "<<PC<<" ";
oss<<setw(11)<<"SP = "<<SP<<" ";
oss<<setw(11)<<"A = "<<A<<" ";
oss<<setw(11)<<"B = "<<B<<" ";
oss<<"Memory Content = "<<"0x"<<setw(8)<<setfill('0')<<hex<<mempory_space[PC]<<dec<<setfill(' ')<<" ";
oss<<" "<<reverse_decode(mempory_space[PC]);
return oss.str();
}