-
Notifications
You must be signed in to change notification settings - Fork 0
/
VM.cpp
222 lines (202 loc) · 6.24 KB
/
VM.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
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
#include "VM.h"
#include "Timer.h"
#include "FileHelper.h"
VM::VM(const char* filename, const char* settings)
{
this->filename = filename;
this->settings = settings;
for (int i = 0; i < 256; i++)
strSlot[i] = "";
loadProgram();
}
VM::~VM()
{
}
void VM::loadProgram()
{
std::string filepath = GetWorkingDirectory() + "/" + filename;
program = readFile(filepath.c_str());
program.push_back((byte)Opcode::HALT); // Need an extra byte. Not a part of the program. E.g: Not really executed
}
void VM::cpu()
{
printf("Started..\n");
Timer timer;
timer.StartTimer();
while (run)
{
int opcode = program[ip++]; // Fetch instruction
switch (opcode)
{
case Opcode::JMP:
ip = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip--;
break;
case Opcode::CALL:
{
stackFrame sf;
int len = (int)program[ip++];
for (int i = 0; i < len; i++) // <----- Number of parameters
{
// Fetch parameter
iSlotSel = (int)program[ip++]; // Select int slot
sf.parameterInts.push_back(iSlot[iSlotSel]); // add parameter from iSlot[i]
}
sf.functionIP = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip++]);
sf.functionIP--;
sf.returnStackFramePointer = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip = sf.functionIP;
stackFrames.push_back(sf);
}
break;
case Opcode::RET:
// NOTE: Return values should already be stored at iSlot[i]
ip = stackFrames[(int)stackFrames.size()-1].returnStackFramePointer;
stackFrames.pop_back();
break;
case Opcode::INC:
iSlotSel = (int)program[ip++]; // Select int slot
iSlot[iSlotSel]++;
break;
case Opcode::DEC:
iSlotSel = (int)program[ip++]; // Select int slot
iSlot[iSlotSel]--;
break;
case Opcode::IADD:
iSlotSel = (int)program[ip++];
iSlot[iSlotSel] = iSlot[(int)program[ip++]] + iSlot[(int)program[ip++]];
break;
case Opcode::ISUB:
iSlotSel = (int)program[ip++];
iSlot[iSlotSel] = iSlot[(int)program[ip++]] - iSlot[(int)program[ip++]];
break;
case Opcode::IMUL:
iSlotSel = (int)program[ip++];
iSlot[iSlotSel] = iSlot[(int)program[ip++]] * iSlot[(int)program[ip++]];
break;
case Opcode::IDIV:
iSlotSel = (int)program[ip++];
iSlot[iSlotSel] = iSlot[(int)program[ip++]] / iSlot[(int)program[ip++]];
break;
case Opcode::IVAR:
iSlotSel = (int)program[ip++]; // Select int slot
iSlot[iSlotSel] = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip++]);
break;
case Opcode::ICPY:
iSlotSel = (int)program[ip++]; // Select int slot (copy to)
iSlot[iSlotSel] = iSlot[(int)program[ip++]]; // (copy from)
break;
case Opcode::IJMPT:
iSlotSel = (int)program[ip++];
if (iSlot[iSlotSel] != 0) // true if not false
{
ip = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip--;
}
else
ip += 4;
break;
case Opcode::IJMPF:
iSlotSel = (int)program[ip++];
if (iSlot[iSlotSel] == 0) // true if false
{
ip = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip--;
}
else
ip += 4;
break;
case Opcode::IJMPGT:
if (iSlot[(int)program[ip++]] >= iSlot[(int)program[ip++]]) // (A >= B) is it true or false ?
{
ip = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip--;
}
else
ip += 5;
break;
case Opcode::IJMPG:
if (iSlot[(int)program[ip++]] > iSlot[(int)program[ip++]]) // (A > B) is it true or false ?
{
ip = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip--;
}
else
ip += 5;
break;
case Opcode::IJMPLT:
if (iSlot[(int)program[ip++]] <= iSlot[(int)program[ip++]]) // (A <= B) is it true or false ?
{
ip = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip--;
}
else
ip += 5;
break;
case Opcode::IJMPL:
if (iSlot[(int)program[ip++]] < iSlot[(int)program[ip++]]) // (A < B) is it true or false ?
{
ip = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip--;
}
else
ip += 5;
break;
case Opcode::IJEQU:
if (iSlot[(int)program[ip++]] == iSlot[(int)program[ip++]]) // (A == B) is it true or false ?
{
ip = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip--;
}
else
ip += 5;
break;
case Opcode::IJEQUN:
if (iSlot[(int)program[ip++]] != iSlot[(int)program[ip++]]) // (A != B) is it true or false ?
{
ip = static_cast<int>((program[ip++] << 24) | (program[ip++] << 16) | (program[ip++] << 8) | program[ip]);
ip--;
}
else
ip += 5;
break;
case Opcode::STRVAR:
strSlotSel = (int)program[ip++]; // Select string slot
strSlotSize[strSlotSel] = (int)program[ip++]; // String length
{
for (int i = 0; i < strSlotSize[strSlotSel]; i++)
strSlot[strSlotSel] += program[ip++];
}
break;
case Opcode::IPRINT:
iSlotSel = (int)program[ip++];
printf("%d", iSlot[iSlotSel]);
break;
case Opcode::STRPRINT:
strSlotSel = (int)program[ip++]; // Select string slot
printf("%s", strSlot[strSlotSel].c_str());
break;
case Opcode::PRINTLN:
printf("\n");
break;
case Opcode::HALT:
run = false; // quit program
ip = 0;
break;
}
}
printf("Elapsed time: %f ms\n", timer.StopTimer());
}
/*
// INT32 to BYTE4
unsigned char bytes[4];
unsigned long n = 175;
bytes[0] = (n >> 24) & 0xFF;
bytes[1] = (n >> 16) & 0xFF;
bytes[2] = (n >> 8) & 0xFF;
bytes[3] = n & 0xFF;
*/
/*
// BYTE4 to INT32
uint32_t var2 = (v[3] << 24) | (v[2] << 16) | (v[1] << 8) | v[0];
*/