-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.h
527 lines (464 loc) · 13.9 KB
/
chip8.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include <cstdint>
#include <random>
#include <memory>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <map>
#include <string>
#include <chrono>
// Copyright 2019 Daniel Weber
#ifndef CHIP8_H_
#define CHIP8_H_
namespace chip8 {
class OpCode {
public:
OpCode() = delete;
OpCode(const OpCode&) = delete;
static std::string as_string(std::uint16_t opcode) {
if( (opcode & 0xF000) == 0x0000 ) {
return "NOOP";
}
if( (opcode & 0xFFFF) == 0x00E0 ) {
return "CLR SCR";
}
if( (opcode & 0xFFFF) == 0x00EE ) {
return "RET";
}
if( (opcode & 0xF000) == 0x1000 ) {
return "0x1NNN: JMP NNN";
}
if( (opcode & 0xF000) == 0x2000 ) {
return "2NNN: CALL NNN";
}
if( (opcode & 0xF000) == 0x3000 ) {
return "3XNN: SKIP IF VX == NN";
}
if( (opcode & 0xF000) == 0x4000 ) {
return "4XNN: SKIP IF VX != NN";
}
if( (opcode & 0xF00F) == 0x5000 ) {
return "5XY0: SKIP IF VX == VY";
}
if( (opcode & 0xF000) == 0x6000 ) {
return "6XNN: VX = NN";
}
if( (opcode & 0xF000) == 0x7000 ) {
return "6XNN: VX += NN";
}
if( (opcode & 0xF00F) == 0x8000 ) {
return "8XY0: VX = VY";
}
if( (opcode & 0xF00F) == 0x8001 ) {
return "8XY1: VX = VX | VY";
}
if( (opcode & 0xF00F) == 0x8002 ) {
return "8XY2: VX = VX & VY";
}
if( (opcode & 0xF00F) == 0x8003 ) {
return "8XY3: VX = VX ^ VY";
}
if( (opcode & 0xF00F) == 0x8004 ) {
return "8XY4: VX += VY";
}
if( (opcode & 0xF00F) == 0x8005 ) {
return "8XY5: VX -= VY";
}
if( (opcode & 0xF00F) == 0x8006 ) {
return "8XY6: STORE LSB VX to VF, VX >> 1";
}
if( (opcode & 0xF00F) == 0x8007 ) {
return "8XY7: VX = VY - VX";
}
if( (opcode & 0xF00F) == 0x8007 ) {
return "8XY7: VX = VY - VX";
}
if( (opcode & 0xF00F) == 0x800E ) {
return "8XYE: VX <<= 1: MSB to VF, VX << 1";
}
if( (opcode & 0xF00F) == 0x9000 ) {
return "9XY0: Skip next if VX != VY";
}
if( (opcode & 0xF000) == 0xA000 ) {
return "ANNN: Set I to NNN";
}
if( (opcode & 0xF000) == 0xB000 ) {
return "BNNN: JMP to NNN+V0";
}
if( (opcode & 0xF000) == 0xC000 ) {
return "CXNN: VX = RAND() & NN";
}
if( (opcode & 0xF000) == 0xD000 ) {
return "DXYN: Draw Sprite (Vx,Vy) (w8,hN)";
}
if( (opcode & 0xF0FF) == 0xE09E ) {
return "EX9E: Skip if key Vx pressed";
}
if( (opcode & 0xF0FF) == 0xE0A1 ) {
return "EXA1: Skip if key Vx not pressed";
}
if( (opcode & 0xF0FF) == 0xF007 ) {
return "FX07: Set Vx to delay_timer";
}
if( (opcode & 0xF0FF) == 0xF00A ) {
return "FX0A: Vx = getkey() ";
}
if( (opcode & 0xF0FF) == 0xF015 ) {
return "FX15: delaytimer = Vx";
}
if( (opcode & 0xF0FF) == 0xF018 ) {
return "FX18: soundtimer = Vx";
}
if( (opcode & 0xF0FF) == 0xF01E ) {
return "FX1E: I += Vx";
}
if( (opcode & 0xF0FF) == 0xF029 ) {
return "FX29: I = spriteaddr[Vx]";
}
if( (opcode & 0xF0FF) == 0xF033 ) {
return "FX33: BCD of Vx to VI,VI+1,VI+2";
}
if( (opcode & 0xF0FF) == 0xF055 ) {
return "FX55: regdump(V0,VX) to MEM[I]";
}
if( (opcode & 0xF0FF) == 0xF065 ) {
return "FX65: regload(V0,VX) from MEM[I]";
}
//TODO: implement all opcodes
return "UNKONWN OPCODE";
}
private:
std::map<std::uint16_t,std::string> opcodes{
{ 0x0000, "Ignored" },
{ 0x00E0, "Clear Screen" },
{ 0x00EE, "Return from Routine" },
{ 0x1000, "Jump to Adr NNN"},
{ 0x2000, "2NNN: Call routine at NNN" },
{ 0x3000, "3XNN: Skip next if Vx eq NN" },
{ 0x4000, "4XNN: Skip next if Vx neq NN" },
{ 0x5000, "5XY0: Skip next if Vx eq Vy" },
{ 0x6000, "6XNN: Set Vx to NN" },
{ 0x7000, "7XNN: Add NN to Vx" },
{ 0x8000, "8XY1: Add NN to Vx" },
};
};
class KeyInterface {
public:
KeyInterface() = default;
KeyInterface(KeyInterface const&) = delete;
KeyInterface& operator=(KeyInterface const&) = delete;
virtual std::uint8_t getKey(int to) noexcept = 0;
};
constexpr unsigned char 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
};
struct emulator {
void set_keyinterface(std::unique_ptr<KeyInterface> arg) {
keyinterface = std::move(arg);
};
constexpr void initialize() noexcept {
for (auto& x : memory) x = 0;
for (auto& x : V) x = 0;
for (auto& x : stack) x = 0;
for (int i = 0; i < 32; i++) {
for (int k = 0; k < 64; k++) {
gfx[i][k] = 0x00;
}
}
pc = 0x200;
sp = 0;
I = 0;
delay_timer = 0;
sound_timer = 0;
opcode = 0;
for (int i = 0; i < 80; i++) {
memory[i] = fontset[i];
}
};
void emulateCycle(bool test = false) noexcept {
opcode = memory[pc];
opcode = opcode << 8;
opcode = opcode | memory[pc+1];
//std::cout << opcode << std::endl;
// DXYN: Draw starting at mem location I, at (Vx, Vy) on
// screen. Sprites are XORed, if collision with pixel, set
// VF=1
if ((opcode & 0xF000) == 0xD000) {
V[0xF] = 0;
for (int i = 0; i < (opcode & 0x000F); i++) {
for (int j = 0; j < 8; j++) {
if( ((memory[I+i] >> (7-j)) & 1) == 1 ) {
int t = gfx[(V[(opcode & 0x00F0) >> 4]+i)%32]
[(V[(opcode & 0x0F00) >> 8]+j)%64];
gfx[(V[(opcode & 0x00F0) >> 4]+i)%32]
[(V[(opcode & 0x0F00) >> 8]+j)%64]
= (((memory[I+i] >> (7-j)) & 1) != t) ? 1 : 0;
if ( t != gfx[(V[(opcode & 0x00F0) >> 4]+i)%32]
[(V[(opcode & 0x0F00) >> 8]+j)%64]
&& t==1 ) {
V[0xF] = 1;
}
}
}
}
}
// EX9E: Skips next instruction if key stored in VX is pressed
if ((opcode & 0xF0FF) == 0xE09E) {
if( V[(opcode & 0x0F00) >> 8] == keyinterface.get()->getKey(100) ) {
pc += 2;
}
}
// EXA1: Skips next instruction if key stored in VX is pressed
if ((opcode & 0xF0FF) == 0xE0A1) {
if( V[(opcode & 0x0F00) >> 8] != keyinterface.get()->getKey(100) ) {
pc += 2;
}
}
// FX55: Store V0 to VX in memory, starting at I.
if ((opcode & 0xF0FF) == 0xF055) {
for (int i = 0; i <= ( (opcode & 0x0F00) >> 8); i++) {
memory[I+i] = V[i];
}
}
// FX65: Fill V0 to VX with values starting at I.
if ((opcode & 0xF0FF) == 0xF065) {
for (int i = 0; i <= ((opcode & 0x0F00) >> 8); i++) {
V[i] = memory[I+i];
}
}
// 8XYE: Store MSB of VX in VF and shift VX to left by 1
if ((opcode & 0xF00F) == 0x800E) {
V[0xF] = (V[(opcode & 0x0F00) >> 8] & 128) >> 7;
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x0F00) >> 8] << 1;
}
// 8XY6: Store LSB of VX in VF and shift VX to right by 1
if ((opcode & 0xF00F) == 0x8006) {
V[0xF] = V[(opcode & 0x0F00) >> 8] & 1;
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x0F00) >> 8] >> 1;
}
// ANNN: MEM: I = NNN, Set I to the Address of NNN.
if ((opcode & 0xF000) == 0xA000) {
I = opcode & 0x0FFF;
}
// BNNN: PC = V0+NNN
if ((opcode & 0xF000) == 0xB000) {
pc = V[0] + (opcode & 0x0FFF);
}
// CXNN: Vx = rand() & NN
if ((opcode & 0xF000) == 0xC000) {
std::uint8_t t = 0;
if (test == true) {
t = 0x55;
} else {
unsigned int r = time(NULL);
t = rand_r(&r) % 255;
}
V[(opcode & 0x0F00) >> 8] = (t & (opcode & 0x00FF));
}
// FX29: I = spriteaddr[Vx]
if ((opcode & 0xF0FF) == 0xF029) {
I = 5 * V[(opcode & 0x0F00) >> 8];
}
// FX07: Vx = getdelay()
if ((opcode & 0xF0FF) == 0xF007) {
V[((opcode & 0x0F00) >> 8)] = delay_timer;
}
// 1NNN: goto NNN
if ((opcode & 0xF000) == 0x1000) {
pc = opcode & 0x0FFF;
pc -= 2;
}
// 2NNN: call subroutine at NNN: interpreter
// increments stack pointer then puts
// current pc on the top of the stack.
// the pc is then set to nnn.
if ((opcode & 0xF000) == 0x2000) {
sp++;
stack[sp] = pc;
pc = (opcode & 0x0FFF);
pc -= 2;
}
// 00EE: The interpreter sets the program
// counter to the address at the top
// of the stack, then substracts 1
// from the stack pointer
if ((opcode & 0xFFFF) == 0x00EE) {
pc = stack[sp];
sp--;
}
// 3XNN: Skip next instruction if V[X] == NN
if ((opcode & 0xF000) == 0x3000) {
if ((opcode & 0x00FF) == V[(opcode & 0x0F00) >> 8])
pc += 2;
}
// 4XNN: Skip next instruction if V[X] does not equal NN
if ((opcode & 0xF000) == 0x4000) {
if ((opcode & 0x00FF) != V[(opcode & 0x0F00) >> 8])
pc += 2;
}
// 5XY0: Skip next instruction if Vx equals Vy
if ((opcode & 0xF00F) == 0x5000) {
if (V[(opcode & 0x0F00) >> 8] == V[(opcode & 0x00F0) >> 4]) {
pc += 2;
}
}
// 6XNN: Set Vx to NN
if ((opcode & 0xF000) == 0x6000) {
V[(opcode & 0x0F00) >> 8] = (opcode & 0x00FF);
}
// 7XNN: Add NN to Vx
if ((opcode & 0xF000) == 0x7000) {
V[(opcode & 0x0F00) >> 8] += (opcode & 0x00FF);
}
// 8XY0: Vx = Vy
if ((opcode & 0xF00F) == 0x8000) {
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4 ];
}
// 8XY1: Vx = Vx & Vy
if ((opcode & 0xF00F) == 0x8001) {
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x0F00) >> 8 ] |
V[(opcode & 0x00F0) >> 4 ];
}
// 8XY2: Vx = Vx & Vy
if ((opcode & 0xF00F) == 0x8002) {
V[(opcode & 0x0F00) >> 8] = (V[(opcode & 0x0F00) >> 8 ] &
V[(opcode & 0x00F0) >> 4 ]);
}
// 8XY3: Vx = Vx ^ Vy
if ((opcode & 0xF00F) == 0x8003) {
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x0F00) >> 8 ] ^
V[(opcode & 0x00F0) >> 4 ];
}
// 8XY4: Vx += Vy
if ((opcode & 0xF00F) == 0x8004) {
// set carry
if (V[(opcode & 0x00F0) >> 4] > (0xFF-V[(opcode & 0x0F00) >> 8])) {
V[16] = 1;
} else {
V[16] = 0;
}
V[(opcode & 0x0F00) >> 8] += V[(opcode & 0x00F0) >> 4 ];
}
// FX0A: A key press is awaited, then stored in VX (Blocking)
if ((opcode & 0xF0FF) == 0xF00A) {
if( keyinterface.get() ) {
V[(opcode & 0x0F00) >> 8] = keyinterface.get()->getKey(-1);
}
}
// 8XY5: Vx -= Vy
if ((opcode & 0xF00F) == 0x8005) {
// set borrow (inverse logic to carry!!)
if (V[(opcode & 0x0F00) >> 8 ] > (V[(opcode & 0x00F0) >> 4])) {
V[0xf] = 1;
} else {
V[0xf] = 0;
}
V[(opcode & 0x0F00) >> 8] -= V[(opcode & 0x00F0) >> 4 ];
}
// 8XY7: Vx = Vy-Vx
if ((opcode & 0xF00F) == 0x8007) {
// set borrow (inverse logic to carry!!)
if (V[(opcode & 0x0FF0) >> 8 ] > (V[(opcode & 0x00F0) >> 4])) {
V[16] = 0;
} else {
V[16] = 1;
}
V[(opcode & 0x0F00) >> 8] = V[(opcode & 0x00F0) >> 4 ]
- V[(opcode & 0x0F00) >> 8];
}
// 9XY0: skip next if Vx != Vy
if ((opcode & 0xF00F) == 0x9000) {
if (V[(opcode & 0x0F00) >> 8] != V[(opcode & 0x00F0) >> 4])
pc += 2;
}
// FX15 set delay timer to Vx
if ((opcode & 0xF0FF) == 0xF015) {
delay_timer = V[(opcode & 0x0F00) >> 8 ];
}
// FX18 set sound timer to Vx
if ((opcode & 0xF0FF) == 0xF018) {
sound_timer = V[(opcode & 0x0F00) >> 8 ];
}
// FX1E: adds Vx to I
if ((opcode & 0xF0FF) == 0xF01E) {
I += V[(opcode & 0x0F00) >> 8];
}
// FX33: Store BCD representation of Vx in memory loc I,I+1,I+2
if ((opcode & 0xF0FF) == 0xF033) {
memory[I] = V[(opcode & 0x0F00) >> 8] / 100;
memory[I+1] = (V[(opcode & 0x0F00) >> 8] - memory[I]*100)/10;
memory[I+2] = V[(opcode & 0x0F00) >> 8] - memory[I]*100 - memory[I+1]*10;
}
// 00E0: Clear screen
if ((opcode & 0xFFFF) == 0x00E0) {
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 64; j++) {
gfx[i][j] = 0x00;
}
}
}
pc += 2;
update_timer();
};
void update_timer() {
std::chrono::system_clock::time_point now =
std::chrono::system_clock::now();
if(delay_timer != 0) {
std::chrono::milliseconds delay_delta =
std::chrono::duration_cast<std::chrono::milliseconds>(now-last_delay);
if( (delay_delta > std::chrono::duration<float, std::milli>(1/60)) ) {
last_delay = now;
delay_timer--;
}
}
if(sound_timer != 0) {
std::chrono::milliseconds sound_delta =
std::chrono::duration_cast<std::chrono::milliseconds>(now-last_sound);
if( (sound_delta > std::chrono::duration<float, std::milli>(1/60)) ) {
last_sound = now;
sound_timer--;
}
}
};
// opcode
std::uint16_t opcode;
// Chip8 has 4k of memory
std::uint8_t memory[4069];
// Chip8 has 15 8bit genereal purpose CPU registers. The 16th register
// holds the carry flag.
std::uint8_t V[16];
// Chip8 has a program counter
std::uint16_t pc;
// Chip8 has a stack pointer
std::uint8_t sp;
std::uint16_t stack[16];
// Chip8 has a index register
std::uint16_t I;
// Timer registers
std::uint8_t delay_timer;
std::uint8_t sound_timer;
// Chip8 has a grafic screen of black and white pixel
std::uint8_t gfx[32][64];
std::unique_ptr<KeyInterface> keyinterface;
// Save timepoint of last decrease of delay timer
std::chrono::time_point<std::chrono::system_clock> last_delay;
// Save timepoint of last decrease of sound timer
std::chrono::time_point<std::chrono::system_clock> last_sound;
};
} // namespace chip8
#endif // CHIP8_H_