-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmulator.h
106 lines (72 loc) · 3.88 KB
/
Emulator.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
#include <iostream>
#include <iomanip>
#include <ostream>
#include <fstream>
#include <sstream>
#include <string>
#include <utility>
#include <map>
using std::pair;
using std::map;
using std::cout;
using std::endl;
using std::flush;
using std::ends;
using std::hex;
using std::dec;
using std::showbase;
using std::noshowbase;
using std::setw;
using std::setfill;
using std::left;
using std::right;
using std::ostream;
using std::ostringstream;
using std::ifstream;
using std::string;
class Emulator // class for EMULATOR
{
public:
enum endianess {lil_endian, big_endian};
private:
static const unsigned pc_lower = 0x0; // PC start address
static const unsigned pc_upper = 0x3ff; // PC end address - TOTAL 1K words text segment
static const unsigned data_lower = 0x400;
static const unsigned end_address = 0xffff;
static const unsigned total_memory = end_address+1; // TOTAL 16K words memory space
static const unsigned long long int max_instruction_count = 0xffffffff; // approx. 4 billion instructions can be executed
// before infinite loop exception triggers
static const char format_code[8]; // to check format of object file provided
static const map<unsigned, pair<unsigned, string> > decoder; // opcode to mnemonic converter
static const map<unsigned, string> faults;
static const endianess machine_type; // to store endianess of local machine
string obj_filename; // stores object file's name
int A, B; // internal registers
int PC, SP; // Program Counter and Stack Pointer
int *mempory_space; // 16k words memory space
int data_upper;
unsigned long long int instr_cnt; // to store number of instructions executed so far
unsigned text_size; // text segment size of provided object file
unsigned data_size; // data segment size of provided object file
bool finished; // whether execution of program finished
bool fault; // whether some fault has occurred
string fault_cause; // stores the reason of fault and the instruction at which fault occurred
public:
Emulator(const string& s): obj_filename(s), A(0), B(0), PC(0), SP(0), mempory_space(new int[total_memory]), data_upper(data_lower-1), instr_cnt(0), finished(false), fault(false), fault_cause(string()) {} // PC and SP will be put to corect values by the loader
~Emulator() { delete [] mempory_space; }
bool loader(); // loads machine code into memory_space
void memory_dump(ostream&) const; // memory-dumper function
string current_state() const; // returns current architectural state of the emulated machine
unsigned long long int instructions_executed() const { return instr_cnt; } // returns number of instructions executed so far
int get_program_status() const; // returns program status -> ACTIVE, FINISHED, FAULT
string get_fault_cause() const { return fault_cause; }
string execute(); // returns current architectural state and then executes one instruction
void disassemble(ostream&) const;
private:
static endianess get_endianess(); // finds endianess of the local machine
unsigned get_int(ifstream&) const; // extracts operand's value from machine code
string reverse_decode(unsigned) const; // gets assembly-level statement from machine code
bool check_PC(int) ; // checks for PC value and raises fault
bool check_SP(int) ; // checks for SP value and raises fault
bool check_memory_access(int) ; // checks for memory address value and raises fault
};