-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
378 lines (314 loc) · 11.5 KB
/
Copy pathmain.cpp
File metadata and controls
378 lines (314 loc) · 11.5 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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <SFML/Graphics.hpp>
#include <unistd.h>
#include "cpu.h"
#include "gpu.h"
using namespace std;
int main (int argc, char *argv[])
{
// Notes : registers are named V0 -> V15
// NNN is a 12 bits address
// val is a 8 bits value
// x,y are 8 bits registers ids
// Declare cpu arguments
string file = "";
int load_quirk = 0;
int shift_quirk = 0;
// Parse the arguments
// Must be provided :
// - path to the game
// Can be provided :
// - s : set shift_quirk to true
// - l : set load_quirk to true
if (argc < 2 ){
cout << "Wrong number of argument: at least 1 is required but " << argc-1 << " was given" << endl;
return 1;
}
else if (argc > 4){
cout << "Wrong number of argument: max 3 can be provided but " << argc-1 << " were given" << endl;
return 1;
}
else{
for(int i = 1; i < argc; i++){
// Convert args to string
string arg = string(argv[i]);
if (arg == "-l"){
load_quirk = 1;
}
else if (arg == "-s"){
shift_quirk = 1;
}
else if (arg == "-ls"){
shift_quirk = 1;
load_quirk = 1;
}
else if (arg == "-sl"){
shift_quirk = 1;
load_quirk = 1;
}
else if (arg == "--help"){
cout << "Welcome to CHIP 8 emulator help page \n\n"
"You must provide the path to the game file\n\n"
"Flags :\n"
"\t -s : set shift_quirk to true (must be enabled for some games)\n"
"\t -l : set load_quirk to true (must be enabled for some games)\n" << endl;
return 0;
}
else if (arg == "-h"){
cout << "Welcome to CHIP 8 emulator help page \n\n"
"You must provide the path to the game file\n\n"
"Flags :\n"
"\t -s : set shift_quirk to true (must be enabled for some games)\n"
"\t -l : set load_quirk to true (must be enabled for some games)\n" << endl;
return 0;
}
else if ((char)arg[0] == '-'){
cout << "Unknown argument " << arg << ". Please see help page (-h or --help)" << endl;
return 1;
}
else{
file = arg;
}
}
}
// Creating Cpu and Gpu objects
Cpu cpu(file, load_quirk, shift_quirk);
Gpu gpu(file);
unsigned char x, y, val;
unsigned short address, opcode, index;
unsigned char height;
vector<unsigned char> sprite;
// Event Loop (note : events are caught by the Gpu)
while(gpu.isOpen()){
sf::Event event;
// Fetching the opcode
opcode = cpu.getMemory(cpu.getPc()) << 8 | cpu.getMemory(cpu.getPc() + 1);
// Parsing the opcode
switch(opcode & 0xF000){
case 0x0000:
switch(opcode & 0x00FF){
case 0x00E0:
// Wipe the screen
gpu.clear();
cpu.setPc(cpu.getPc() + 2);
break;
// Return to the previous function on the stack
case 0x00EE:
cpu.ret();
break;
default:
// Call the program at address NNN
address = opcode & 0x0FFF;
cpu.call(address);
break;
}
break;
case 0x1000:
// Jump to address NNN
address = opcode & 0x0FFF;
cpu.jump(address);
break;
case 0x2000:
// Call the function at address NNN
address = opcode & 0x0FFF;
cpu.call(address);
break;
case 0x3000:
// Pass if V[x] == val
x = (opcode & 0x0F00) >> 8;
val = opcode & 0x00FF;
cpu.ifEq(x, val);
break;
case 0x4000:
// Pass if V[x] != val
x = (opcode & 0x0F00) >> 8;
val = opcode & 0x00FF;
cpu.ifNotEq(x, val);
break;
case 0x5000:
// Pass if V[x] == V[y]
x = (opcode & 0x0F00) >> 8;
y = (opcode & 0x00F0) >> 4;
cpu.ifEqReg(x, y);
break;
case 0x6000:
// Set V[x] to val
x = (opcode & 0x0F00) >> 8;
val = opcode & 0x00FF;
cpu.set(x, val);
break;
case 0x7000:
// Increment V[x] by val
x = (opcode & 0x0F00) >> 8;
val = opcode & 0x00FF;
cpu.add(x, val);
break;
case 0x8000:
x = (opcode & 0x0F00) >> 8;
y = (opcode & 0x00F0) >> 4;
switch(opcode & 0x000F){
case 0x0000:
// assign V[y] to V[x]
cpu.assign(x, y);
break;
case 0x0001:
// Perform bitwise Or on V[x] and V[y] and store it to V[x]
cpu.BitOr(x, y);
break;
case 0x0002:
// Perform bitwise And on V[x] and V[y] and store it to V[x]
cpu.BitAnd(x, y);
break;
case 0x0003:
// Perform bitwise Xor on V[x] and V[y] and store it to V[x]
cpu.BitXor(x, y);
break;
case 0x0004:
// Add V[x] to V[y] and store it to V[x]
cpu.addReg(x, y);
break;
case 0x0005:
// Substract V[y] from V[x] and store it to V[x]
cpu.subReg(x, y);
break;
case 0x0006:
// Divide V[y] by 2 and store the result to V[x]
cpu.shiftRight(x, y);
break;
case 0x0007:
// Substract V[x] from V[y] and store it to V[x]
cpu.subCopy(x, y);
break;
case 0x000E:
// Multiply V[y] by 2 and store it to V[x]
cpu.shiftLeft(x, y);
break;
default:
break;
}
break;
case 0x9000:
// Pass if V[x] != V[y]
x = (opcode & 0x0F00) >> 8;
y = (opcode & 0x00F0) >> 4;
cpu.ifNotEqReg(x, y);
break;
case 0xA000:
// Set index to NNN
address = opcode & 0x0FFF;
cpu.setI(address);
break;
case 0xB000:
// Jump to address NNN + V0
address = opcode & 0x0FFF;
cpu.jumpV0(address);
break;
case 0xC000:
// Store a random number in V[x]
x = (opcode & 0x0F00) >> 8;
val = opcode & 0x00FF;
cpu.andRand(x, val);
break;
case 0xD000:
// Draw height * 8 sprite in Xor mode with values at the address stored in the index
x = (opcode & 0x0F00) >> 8;
y = (opcode & 0x00F0) >> 4;
height = opcode & 0x000F;
index = cpu.getIndex();
sprite.clear();
for(int i = 0; i < height; i++){
sprite.push_back(cpu.getMemory(index + i));
}
val = gpu.draw(cpu.getReg(x), cpu.getReg(y), height, sprite);
cpu.set(15, val);
break;
case 0xE000:
// Keyboard interrupt checks
x = (opcode & 0x0F00) >> 8;
switch(opcode & 0x00FF){
case 0x009E:
cpu.ifKey(x);
break;
case 0x00A1:
cpu.ifNotKey(x);
break;
default:
break;
}
break;
case 0xF000:
x = (opcode & 0x0F00) >> 8;
switch(opcode & 0x00FF){
case 0x0007:
// Get the state of the delay timer
cpu.getDelay(x);
break;
case 0x000A:
// Wait for an event to occur
val = gpu.waitEvent(&event);
cpu.setKey(val, 1);
cpu.set(x, val);
break;
case 0x0015:
// Set delay timer to V[x]
cpu.setDelay(x);
break;
case 0x0018:
// Set sound timer to V[x]
cpu.setSound(x);
break;
case 0x001E:
// Add V[x] to the index
cpu.addI(x);
break;
case 0x0029:
// Get the font which number is stored in V[x]
cpu.getFont(x);
break;
case 0x0033:
// Store BCD representation of V[x] at the address stored in the index
cpu.BCD(x);
break;
case 0x0055:
// Dump the x+1 registries in memory starting at the address sotred in the index
cpu.regDump(x);
break;
case 0x0065:
// Load the x+1 registries in memory starting at the address sotred in the index
cpu.regLoad(x);
break;
default:
break;
}
break;
default:
// Stop if an unknown opcode occur
cout << "Unknown opcode" << opcode << endl;
break;
}
unsigned short res;
// Check if the is a keyboard interrupt
res = gpu.pollEvent(&event);
if(res != 0x00FF){
cpu.setKey(res & 0x00FF, (res & 0xFF00) >> 8);
}
// Decrement delay timer
if(cpu.getDelayTimer() > 0){
cpu.updateDelay();
}
// Decrement sound timer
if(cpu.getSoundTimer() > 0)
{
if(cpu.getSoundTimer() == 1){
printf("BEEP!\n");
}
cpu.updateSound();
}
// Try to emulate 50 Hz frequency of CPU
usleep(2000);
}
return 0;
}