-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.cpp
440 lines (416 loc) · 14.7 KB
/
chip8.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
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
#include "chip8.hpp"
#include <cstdlib>
#include <algorithm>
#include <fstream>
#include "OpcodeException.hpp"
uint8_t chip8_fontset[80] =
{
0xF0, 0x90, 0x90, 0x90, 0xF0, //0
0x20, 0x60, 0x20, 0x20, 0x70, //1
0xF0, 0x10, 0xF0, 0x80, 0xF0, //2
0xF0, 0x10, 0xF0, 0x10, 0xF0, //3
0x90, 0x90, 0xF0, 0x10, 0x10, //4
0xF0, 0x80, 0xF0, 0x10, 0xF0, //5
0xF0, 0x80, 0xF0, 0x90, 0xF0, //6
0xF0, 0x10, 0x20, 0x40, 0x40, //7
0xF0, 0x90, 0xF0, 0x90, 0xF0, //8
0xF0, 0x90, 0xF0, 0x10, 0xF0, //9
0xF0, 0x90, 0xF0, 0x90, 0x90, //A
0xE0, 0x90, 0xE0, 0x90, 0xE0, //B
0xF0, 0x80, 0x80, 0x80, 0xF0, //C
0xE0, 0x90, 0x90, 0x90, 0xE0, //D
0xF0, 0x80, 0xF0, 0x80, 0xF0, //E
0xF0, 0x80, 0xF0, 0x80, 0x80 //F
};
Chip8::Chip8() {
this->pc = 0x200;
this->opcode = 0;
this->I = 0;
this->sp = -1;
this->delayTimer = 0;
this->soundTimer = 0;
this->playSound = NULL;
this->sound = NULL;
this->isRunning = true;
this->waitForKey = false;
this->drawFlag = false;
std::fill_n(memory, 4096, 0); // Clear memory
std::fill_n(V, 16, 0); // Clear registers
std::fill_n(stack, 16, 0); // Clear stack
std::fill_n(pixels, RES, 0); // Clear display
std::fill_n(keys, 16, false); // Clear keys array
// Load fontset
//*memory = *(uint64_t*)chip8_fontset;
//*(memory + 64) = *(uint16_t*)(chip8_fontset + 64);
for (int i = 0; i < 80; i++) {
memory[i] = chip8_fontset[i];
}
}
void Chip8::emulateCycle(void) {
// If the execution is not halted
if (isRunning) {
// Fetch opcode
opcode = memory[pc] << 8 | memory[pc + 1];
if (!decodeOpCode(opcode)) {
throw OpCodeException(opcode, (uint8_t)memory + pc);
}
else {
if (delayTimer > 0) {
delayTimer--;
}
if (soundTimer > 0) {
if (soundTimer == 1) {
if (playSound) {
// If a sound implementation was provided
playSound(sound);
}
}
soundTimer--;
}
}
}
}
void Chip8::keyPress(const uint8_t key) {
if (waitForKey) {
waitForKey = false;
isRunning = true;
// Stores pressed key is VX cause of opcodes 0xEX9E and 0xEXA1
V[opcode >> 8 & 0x0F] = key;
}
keys[key] = true;
}
void Chip8::keyRelease(const uint8_t key) {
keys[key] = false;
}
uint8_t (&Chip8::getPixels(void))[RES]
{
// Return a reference of the array
return pixels;
}
bool Chip8::getDrawFlag(void) {
return drawFlag;
}
void Chip8::setDrawFlag(bool flag) {
drawFlag = flag;
}
void Chip8::loadROM(const char* fName) {
std::ifstream rom;
rom.open(fName, std::ios::binary);
if (rom.fail()) {
throw std::runtime_error("Error: file");
}
rom.seekg(0, rom.end);
uint8_t length = rom.tellg();
rom.seekg(0, rom.beg);
rom.read((char*)(memory + 0x200), length);
rom.close();
}
void Chip8::setSound(void (*func)(void *sound), void *sound) {
// Make sound a void pointer so the emulator implementation
// is not library dependend
this->playSound = func;
this->sound = sound;
}
bool Chip8::decodeOpCode(const uint16_t opcode) {
switch (opcode & 0xF000) {
case 0x0: {
switch (opcode & 0x0FF) {
case 0x0E0: { // Clear screan
std::fill_n(pixels, RES, 0);
pc += 2;
drawFlag = true;
break;
}
case 0x0EE: { // Return from subroutine
pc = stack[sp--];
pc += 2; // When returning, point to the next instuction
break;
}
default: {
return false;
}
}
break;
}
case 0x1000: { // Jump to address NNN
pc = opcode & 0x0FFF;
break;
}
case 0x2000: { // Call subroutine
stack[++sp] = pc;
pc = opcode & 0x0FFF;
break;
}
case 0x3000: {
// Skips the next instruction if VX equals NN
if (V[opcode >> 8 & 0x0F] == (opcode & 0x0FF)) {
pc += 2;
}
pc += 2;
break;
}
case 0x4000: {
// Skips the next instruction if VX doesn't equal NN
if (V[opcode >> 8 & 0x0F] != (opcode & 0x0FF)) {
pc += 4; // TODO FIX
}
pc += 2;
break;
}
case 0x5000: {
// Skips the next instruction if VX equals VY
if (V[opcode >> 8 & 0x0F] == V[opcode >> 4 & 0x0F]) {
pc += 2;
}
pc += 2;
break;
}
case 0x6000: { //Check
// Sets VX to NN
V[opcode >> 8 & 0x0F] = opcode & 0x0FF;
pc += 2;
break;
}
case 0x7000: {
// Adds NN to VX
V[opcode >> 8 & 0x0F] += opcode & 0x0FF;
pc += 2;
break;
}
case 0x8000: {
switch (opcode & 0x0F) {
case 0x00: {
// Sets VX to the value of VY
V[opcode >> 8 & 0xF] = V[opcode >> 4 & 0xF];
pc += 2;
break;
}
case 0x01: {
// Sets VX to VX or VY
V[opcode >> 8 & 0xF] |= V[opcode >> 4 & 0xF];
pc += 2;
break;
}
case 0x02: {
// Sets VX to VX and VY
V[opcode >> 8 & 0xF] &= V[opcode >> 4 & 0xF];
pc += 2;
break;
}
case 0x03: {
// Sets VX to VX xor VY
V[opcode >> 8 & 0xF] ^= V[opcode >> 4 & 0xF];
pc += 2;
break;
}
case 0x04: { // TODO Check
// Adds VY to VX
// VF is set to 1 when there's a carry, and to 0 when there isn't
//V[0x0F] = (V[opcode >> 8 & 0xF] + V[opcode >> 4 & 0xF] > 0xFFF) ? 1 : 0;
V[0x0F] = (V[(opcode & 0x00f0) >> 4] > (0xff - V[(opcode & 0x0f00) >> 8])) ? 1 : 0;
V[opcode >> 8 & 0xF] += V[opcode >> 4 & 0xF];
pc += 2;
break;
}
case 0x05: { //TODO Check
// VY is subtracted from VX
// VF is set to 0 when there's a borrow, and 1 when there isn't
//V[0x0F] = (V[opcode >> 8 & 0xF] > V[opcode >> 4 & 0xF]) ? 1 : 0;
V[0xf] = (V[(opcode & 0x0f00) >> 8] > (V[(opcode & 0x00f0) >> 4])) ? 1 : 0;
V[opcode >> 8 & 0xF] -= V[opcode >> 4 & 0xF];
pc += 2;
break;
}
case 0x06: {
// Stores the least significant bit of VX in VF and then shifts VX to the right by 1.
V[0xF] = V[opcode >> 8 & 0x0F] & 0x01;
V[opcode >> 8 & 0x0F] >>= 1;
pc += 2;
break;
}
case 0x07: { // TODO Check
// Sets VX to VY minus VX
// VF is set to 0 when there's a borrow, and 1 when there isn't
//V[0x0F] = (V[opcode >> 4 & 0x0F] > V[opcode >> 8 & 0x0F] ? 0 : 1);
V[0xf] = (V[(opcode & 0x00f0) >> 4] > (V[(opcode & 0x0f00) >> 8])) ? 1 : 0;
V[opcode >> 8 & 0x0F] = V[opcode >> 4 & 0xF] - V[opcode >> 8 & 0x0F];
pc += 2;
break;
}
case 0x0E: {
// Stores the most significant bit of VX in VF
// and then shifts VX to the left by 1
V[0x0F] = V[opcode >> 8 & 0x0F] >> 7;
V[opcode >> 8 & 0x0F] <<= 1;
pc += 2;
break;
}
default: {
return false;
}
}
break;
}
case 0x9000: {
// Skips the next instruction if VX doesn't equal VY
if (V[opcode >> 8 & 0x0F] != V[opcode >> 4 & 0x0F]) {
pc += 2;
}
pc += 2;
break;
}
case 0xA000: {
// Sets I to the address NNN
I = opcode & 0x0FFF;
pc += 2;
break;
}
case 0xB000: {
// Jumps to the address NNN plus V0
pc = (opcode & 0x0FFF) + V[0x0];
break;
}
case 0xC000: {
// Sets VX to the result of a bitwise and operation on a random number
// (Typically: 0 to 255) and NN
V[opcode >> 8 & 0x0F] = (std::rand() % 0xFF) & (opcode & 0x0FF);
pc += 2;
}
case 0xD000: {
// DXYN: Draws a sprite at coordinate (VX, VY) that has a width of 8 pixels and a
// height of N pixels. Each row of 8 pixels is read as bit-coded starting from memory
// location I; I value doesnt change after the execution of this instruction. As described
// above, VF is set to 1 if any screen pixels are flipped from set to unset when the sprite is
// drawn, and to 0 if that doesnt happen
uint8_t vx = V[opcode >> 8 & 0x0F];
uint8_t vy = V[opcode >> 4 & 0x0F];
uint16_t height = opcode & 0x0F;
uint16_t pixel;
V[0x0F] = 0;
for (uint16_t yline = 0; yline < height; yline++) {
pixel = memory[I + yline];
for (uint16_t xline = 0; xline < 8; xline++) {
if ((pixel & (0x80 >> xline)) != 0) {
if (pixels[(vx + xline + ((vy + yline) * 64))] == 1) {
V[0x0F] = 1;
}
pixels[vx + xline + ((vy + yline) * 64)] ^= 1;
}
}
}
pc += 2;
drawFlag = true;
break;
}
case 0xE000: {
switch (opcode & 0x0FF) {
case 0x09E: { //TODO Check
// Skips the next instruction if the keys stored in VX is pressed
if (keys[V[opcode >> 8 & 0x0F]]) {
pc += 2;
}
pc += 2;
break;
}
case 0xA1: { // TODO check
// Skips the next instruction if the keys stored in VX isn't pressed
if (!keys[V[opcode >> 8 & 0x0F]]) {
pc += 2;
}
pc += 2;
break;
}
default: {
return false;
}
}
break;
}
case 0xF000: {
switch (opcode & 0xFF) {
case 0x07: {
// Sets VX to the value of the delay timer.
V[opcode >> 8 & 0x0F] = delayTimer;
pc += 2;
break;
}
case 0x0A: { // TODO check
// A keys press is awaited, and then stored in VX
waitForKey = true;
isRunning = false;
pc += 2;
break;
}
case 0x015: {
// Sets the delay timer to VX
delayTimer = V[opcode >> 8 & 0x0F];
pc += 2;
break;
}
case 0x018: {
// Sets the sound timer to VX
soundTimer = V[opcode >> 8 & 0x0F];
pc += 2;
break;
}
case 0x01E: { // TODO CHECK
// Adds VX to I
// VF is set to 1 when there is a range overflow
// and to 0 when there isn't
//V[0xF] = (I + (opcode >> 8 & 0x0F) > 0xFFF ? 1 : 0);
if (I > (0xfff - V[(opcode & 0x0f00) >> 8])){
V[0xf] = 1;
}
else {
V[0xf] = 0;
}
I += V[opcode >> 8 & 0x0F];
pc += 2;
break;
}
case 0x029: {
// Sets I to the location of the sprite for the character in VX.
// Characters 0-F (in hexadecimal) are represented by a 4x5 font
I = V[opcode >> 8 & 0x0F] * 5; // Each row has 5 elements
pc += 2;
break;
}
case 0x033: {
// Stores the binary-coded decimal representation of VX, with the most significant
// of three digits at the address in I, the middle digit at I plus 1, and the least
// significant digit at I plus 2. (In other words, take the decimal representation of
// VX, place the hundreds digit in memory at location in I, the tens digit at location
// I+1, and the ones digit at location I+2.)
memory[I] = V[(opcode & 0x0F00) >> 8] / 100;
memory[I + 1] = (V[(opcode & 0x0F00) >> 8] / 10) % 10;
memory[I + 2] = (V[(opcode & 0x0F00) >> 8] % 100) % 10;
pc += 2;
break;
}
case 0x055: {
// Stores V0 to VX (including VX) in memory starting at address I.
// The offset from I is increased by 1 for each value written, but I itself is left unmodified
for (uint16_t i = 0; i <= (opcode >> 8 & 0x0F); i++) {
memory[I + i] = V[i];
}
pc += 2;
break;
}
case 0x65: {
for (uint16_t i = 0; i <= (opcode >> 8 & 0x0F); i++) {
// Fills V0 to VX (including VX) with values from memory starting at address I.
// The offset from I is increased by 1 for each value written, but I itself is left unmodified.
V[i] = memory[I + i];
}
pc += 2;
break;
}
}
break;
}
default: {
return false;
}
}
return true;
}