-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtual_machine.h
150 lines (127 loc) · 2.74 KB
/
virtual_machine.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
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
#ifndef VIRTUAL_MACHINE_H
#define VIRTUAL_MACHINE_H
#include <cassert>
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
typedef unsigned char uint8;
typedef unsigned short int uint16;
typedef signed long int int32;
typedef unsigned long int uint32;
class VirtualMachine
{
public:
VirtualMachine();
void LoadFile( std::string &fileName );
void Run( bool debugEnabled, uint32 stepFrom = 0 );
void ShutDown();
void Disassemble( const std::string &fileName );
void DebugHandleOpCode( std::string &opName, uint16 opCode, uint16 *args, int32 ArgCount );
private:
uint16 GetLiteral( uint16 input );
void WriteRegister( uint16 reg, uint16 value );
void Jump( uint16 pos );
void HandleOPCode( uint16 opCode, uint16 *args );
void PrintStack();
uint16 *Memory;
uint32 MemorySize;
uint32 MemoryOffset;
uint16 Registers[8];
std::stack<uint16> Stack;
bool Executing;
bool DebugEnabled;
bool DebugActive;
const uint16 register_0 = 32768;
const uint16 register_1 = 32769;
const uint16 register_2 = 32770;
const uint16 register_3 = 32771;
const uint16 register_4 = 32772;
const uint16 register_5 = 32773;
const uint16 register_6 = 32774;
const uint16 register_7 = 32775;
uint16 ArgCount[22] = {
0, // halt: 0
2, // set: 1 a b
1, // push: 2 a
1, // pop: 3 a
3, // eq: 4 a b c
3, // gt: 5 a b c
1, // jmp: 6 a
2, // jt: 7 a b
2, // jf: 8 a b
3, // add: 9 a b c
3, // mult: 10 a b c
3, // mod: 11 a b c
3, // and: 12 a b c
3, // or: 13 a b c
2, // not: 14 a b
2, // rmem: 15 a b
2, // wmem: 16 a b
1, // call: 17 a
0, // ret: 18
1, // out: 19 a
1, // in: 20 a
0, // noop: 21
};
enum class OPCODE
{
HALT = 0,
SET,
PUSH,
POP,
EQ,
GT,
JMP,
JT,
JF,
ADD,
MULT,
MOD,
AND,
OR,
NOT,
RMEM,
WMEM,
CALL,
RET,
OUT,
IN,
NOOP
};
std::string OpNames[22] = {
"HALT",
"SET",
"PUSH",
"POP",
"EQ",
"GT",
"JMP",
"JT",
"JF",
"ADD",
"MULT",
"MOD",
"AND",
"OR",
"NOT",
"RMEM",
"WMEM",
"CALL",
"RET",
"OUT",
"IN",
"NOOP"
};
std::string RegisterNames[8] = {
"R0",
"R1",
"R2",
"R3",
"R4",
"R5",
"R6",
"R7",
};
};
#endif /* VIRTUAL_MACHINE_H */