-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip8.c
545 lines (501 loc) · 11.5 KB
/
chip8.c
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#include "chip8.h"
#include <time.h>
#include "port.h"
uint8_t chip8_fontset[FONTSET_SIZE] = {
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
};
long get_file_size(FILE *rom_file) {
if (rom_file == NULL) {
printf("open rom error\n");
return 1;
}
fseek(rom_file, 0L, SEEK_END);
long file_size = ftell(rom_file);
rewind(rom_file);
return file_size;
}
void print_hex(uint8_t *buffer, size_t size) {
for (int i = 0; i < size; i++) {
printf("%02X ", buffer[i]);
if ((i + 1) % 16 == 0) {
printf("\n");
}
}
printf("\n");
}
CHIP8 *chip8_init() {
srand((unsigned)time(NULL));
CHIP8 *chip8 = malloc(sizeof(CHIP8));
memset(chip8, 0, sizeof(CHIP8));
chip8->pc = MEM_START;
// init font
for (int i = 0; i < FONTSET_SIZE; i++) {
chip8->mem[FONTSET_MEM_START + i] = chip8_fontset[i];
}
chip8->state = SYS_RUNNING;
return chip8;
}
uint8_t chip8_load_rom(CHIP8 *chip8, const char *rom_name) {
FILE *rom_file = fopen(rom_name, "r");
long file_size = get_file_size(rom_file);
if (file_size >= MEM_SIZE - MEM_START) {
printf("memory overflow");
return 0;
}
uint8_t *rom = chip8->mem + MEM_START; // 512
size_t result = fread(rom, 1, file_size, rom_file);
if (result != file_size) {
printf("read rom error\n");
return 0;
}
// print_hex(rom, file_size);
fclose(rom_file);
return 1;
}
void chip8_cycle(CHIP8 *chip8) {
// fetch and execute
uint16_t opcode =
(0xFF00 & (chip8->mem[chip8->pc] << 8)) | chip8->mem[chip8->pc + 1];
chip8->opcode = opcode;
chip8->pc += 2;
byte type = (0xF000 & opcode) >> 12;
switch (type) {
case 0x0:
if (opcode == 0x00E0) {
OPCODE(00E0);
} else if (opcode == 0x00EE) {
// 00EE return;
OPCODE(00EE);
}
break;
case 0x1:
// 1NNN goto NNN;
OPCODE(1NNN);
break;
case 0x2:
// 2NNN *(0xNNN)();
OPCODE(2NNN);
break;
case 0x3:
// 3XNN if (Vx == NN)
OPCODE(3XNN);
break;
case 0x4:
// 4XNN if (Vx != NN)
OPCODE(4XNN);
break;
case 0x5:
// 5XY0 if (Vx == Vy)
OPCODE(5XY0);
break;
case 0x6:
// 6XNN Vx = NN
OPCODE(6XNN);
break;
case 0x7:
// 7XNN Vx += NN, not affect VF
OPCODE(7XNN);
break;
case 0x8:
switch (N(_OPCODE)) {
case 0x0:
OPCODE(8XY0);
break;
case 0x1:
OPCODE(8XY1);
break;
case 0x2:
OPCODE(8XY2);
break;
case 0x3:
OPCODE(8XY3);
break;
case 0x4:
OPCODE(8XY4);
break;
case 0x5:
OPCODE(8XY5);
break;
case 0x6:
OPCODE(8XY6);
break;
case 0x7:
OPCODE(8XY7);
break;
case 0xE:
OPCODE(8XYE);
break;
}
break;
case 0x9:
// 9XY0 if (Vx != Vy)
OPCODE(9XY0);
break;
case 0xA:
// ANNN I = NNN
OPCODE(ANNN);
break;
case 0xB:
OPCODE(BNNN);
break;
case 0xC:
OPCODE(CXNN);
break;
case 0xD:
// DXYN draw(Vx, Vy, N)
// Draws a sprite at coordinate (VX, VY) that has a width of 8 pixels
// and a height of N pixels.
OPCODE(DXYN);
break;
case 0xE:
if (NN(_OPCODE) == 0x9E) {
OPCODE(EX9E);
} else if (NN(_OPCODE) == 0xA1) {
OPCODE(EXA1);
}
break;
case 0xF:
switch (NN(_OPCODE)) {
case 0x07:
OPCODE(FX07);
break;
case 0x0A:
// FX0A Vx = get_key()
OPCODE(FX0A);
break;
case 0x15:
OPCODE(FX15);
break;
case 0x18:
OPCODE(FX18);
break;
case 0x1E:
OPCODE(FX1E);
break;
case 0x29:
OPCODE(FX29);
break;
case 0x33:
OPCODE(FX33);
break;
case 0x55:
OPCODE(FX55);
break;
case 0x65:
OPCODE(FX65);
break;
}
break;
default:
break;
}
}
void chip8_timer(CHIP8 *chip8) {
// update timers
if (chip8->delay_timer > 0) {
chip8->delay_timer--;
}
if (chip8->sound_timer > 0) {
chip8->sound_timer--;
}
}
/**
* Clear the screen
*/
void opcode_00E0(CHIP8 *chip8) {
memset(chip8->display, 0, sizeof(chip8->display));
}
/**
* Return from a subroutine
*/
void opcode_00EE(CHIP8 *chip8) {
// attention please: decrease sp first
chip8->pc = chip8->stack[--(chip8->sp)];
}
/**
* Jump to address NNN
*/
void opcode_1NNN(CHIP8 *chip8) {
chip8->pc = NNN(_OPCODE);
}
/**
* Call subroutine at NNN
*/
void opcode_2NNN(CHIP8 *chip8) {
chip8->stack[chip8->sp++] = chip8->pc;
chip8->pc = NNN(_OPCODE);
}
/**
* Skip next instruction if VX equals NN
*/
void opcode_3XNN(CHIP8 *chip8) {
if (VX(_OPCODE) == NN(_OPCODE)) {
chip8->pc += 2;
}
}
/**
* Skip next instruction if VX doesn't equal NN
*/
void opcode_4XNN(CHIP8 *chip8) {
if (VX(_OPCODE) != NN(_OPCODE)) {
chip8->pc += 2;
}
}
/**
* Skip next instruction if VX equals VY
*/
void opcode_5XY0(CHIP8 *chip8) {
if (VX(_OPCODE) == VY(_OPCODE)) {
chip8->pc += 2;
}
}
/**
* Set VX to NN
*/
void opcode_6XNN(CHIP8 *chip8) {
VX(_OPCODE) = NN(_OPCODE);
// printf("set V%d: %02X\n", X(_OPCODE), NN(_OPCODE));
}
/**
* Add NN to VX
*/
void opcode_7XNN(CHIP8 *chip8) {
VX(_OPCODE) += NN(_OPCODE);
// printf("add V%d: %02X\n", X(_OPCODE), NN(_OPCODE));
}
/**
* Set VX to the value of VY
*/
void opcode_8XY0(CHIP8 *chip8) { VX(_OPCODE) = VY(_OPCODE); }
/**
* Set VX to VX OR VY
*/
void opcode_8XY1(CHIP8 *chip8) { VX(_OPCODE) |= VY(_OPCODE); }
/**
* Set VX to VX AND VY
*/
void opcode_8XY2(CHIP8 *chip8) { VX(_OPCODE) &= VY(_OPCODE); }
/**
* Set VX to VX XOR VY
*/
void opcode_8XY3(CHIP8 *chip8) { VX(_OPCODE) ^= VY(_OPCODE); }
/**
* Add VY to VX; VF is set to 1 if there's a carry, else 0
*/
void opcode_8XY4(CHIP8 *chip8) {
uint16_t add = (uint16_t)VX(_OPCODE) + (uint16_t)VY(_OPCODE);
VX(_OPCODE) = add & 0xFF;
// set to 1 if there's a carry
_VF = (add & 0x0100) >> 8;
}
/**
* Subtract VY from VX; VF is set to 0 if there's a borrow, else 1
*/
void opcode_8XY5(CHIP8 *chip8) {
if (VX(_OPCODE) > VY(_OPCODE)) {
_VF = 1;
} else {
_VF = 0;
}
VX(_OPCODE) -= VY(_OPCODE);
}
/**
* Store the least significant bit of VX in VF and then shift VX to the right by
* 1
*/
void opcode_8XY6(CHIP8 *chip8) {
_VF = VX(_OPCODE) & 0x01;
VX(_OPCODE) >>= 1;
}
/**
* Set VX to VY minus VX; VF is set to 0 if there's a borrow, else 1
*/
void opcode_8XY7(CHIP8 *chip8) {
if (VX(_OPCODE) < VY(_OPCODE)) {
_VF = 1;
} else {
_VF = 0;
}
VX(_OPCODE) = VY(_OPCODE) - VX(_OPCODE);
}
/**
* Store the most significant bit of VX in VF and then shift VX to the left by 1
*/
void opcode_8XYE(CHIP8 *chip8) {
_VF = (VX(_OPCODE) * 0x80) >> 7;
VX(_OPCODE) <<= 1;
}
/**
* Skip next instruction if VX doesn't equal VY
*/
void opcode_9XY0(CHIP8 *chip8) {
if (VX(_OPCODE) != VY(_OPCODE)) {
chip8->pc += 2;
}
}
/**
* Set I to the address NNN
*/
void opcode_ANNN(CHIP8 *chip8) {
chip8->index_reg = NNN(_OPCODE);
// printf("set I: %04X\n", NNN(_OPCODE));
}
/**
* Jump to the address NNN plus V0
*/
void opcode_BNNN(CHIP8 *chip8) { chip8->pc = NNN(_OPCODE) + chip8->reg[0]; }
/**
* Set VX to the result of a bitwise AND operation on a random number and NN
*/
void opcode_CXNN(CHIP8 *chip8) {
VX(_OPCODE) = NN(_OPCODE) & (uint8_t)(rand() % 256);
}
/**
* Draw a sprite at coordinates VX, VY with a width of 8 pixels and a height of
* N pixels
*/
void opcode_DXYN(CHIP8 *chip8) {
chip8->reg[0xF] = 0;
int x = X(_OPCODE);
int y = Y(_OPCODE);
int n = N(_OPCODE);
// printf("draw(%d,%d,%d) %04X\n", chip8->reg[x] & (DISPLAY_WIDTH - 1),
// chip8->reg[y] & (DISPLAY_HEIGHT - 1), n, chip8->index_reg);
// The starting position of the sprite will wrap
byte start_x = chip8->reg[x] & (DISPLAY_WIDTH - 1);
byte start_y = chip8->reg[y] & (DISPLAY_HEIGHT - 1);
uint16_t index = chip8->index_reg;
for (byte height = 0; height < n; height++) {
for (byte width = 0; width < 8; width++) {
// draw every byte
byte pixel = (chip8->mem[index] & (0x80 >> width));
// clip sprite out of edge
byte cur_x = start_x + width;
byte cur_y = start_y + height;
if (cur_x >= DISPLAY_WIDTH || cur_y >= DISPLAY_HEIGHT) {
continue;
}
// VF is set to 1 if any screen pixels are flipped from set(white) to
// unset(black) when the sprite is drawn
// XOR
if (pixel) {
if (chip8->display[cur_y][cur_x] == DISPLAY_WHITE) {
chip8->reg[0xF] = 1;
chip8->display[cur_y][cur_x] = DISPLAY_BLACK;
} else {
chip8->display[cur_y][cur_x] = DISPLAY_WHITE;
}
chip8->display_refresh_flag = 1;
}
}
index++;
}
}
/**
* Skip next instruction if the key stored in VX is pressed
*/
void opcode_EX9E(CHIP8 *chip8) {
if (VX(_OPCODE) >= 16) {
return;
}
if (chip8->keys[VX(_OPCODE)]) {
chip8->pc += 2;
}
}
/**
* Skip next instruction if the key stored in VX isn't pressed
*/
void opcode_EXA1(CHIP8 *chip8) {
if (VX(_OPCODE) >= 16) {
return;
}
if (!(chip8->keys[VX(_OPCODE)])) {
chip8->pc += 2;
}
}
/**
* Set VX to the value of the delay timer
*/
void opcode_FX07(CHIP8 *chip8) { VX(_OPCODE) = chip8->delay_timer; }
/**
* Block instruction
*
* Wait for a key press and store the key in VX
* Timers continues counting
*/
void opcode_FX0A(CHIP8 *chip8) {
byte key_pressed = 0;
for (byte i = 0; i < 0xF; i++) {
if (chip8->keys[i]) {
key_pressed = 1;
VX(_OPCODE) = i;
}
}
if (!key_pressed) {
// loop forever to wait
chip8->pc -= 2;
}
}
/**
* Set the delay timer to the value in VX
*/
void opcode_FX15(CHIP8 *chip8) { chip8->delay_timer = VX(_OPCODE); }
/**
* Set the sound timer to the value in VX
*/
void opcode_FX18(CHIP8 *chip8) { chip8->sound_timer = VX(_OPCODE); }
/**
* Add the value stored in VX to I;
*/
void opcode_FX1E(CHIP8 *chip8) { _I += VX(_OPCODE); }
/**
* Set I to the location of the sprite for the character in VX
* Characters 0-F (in hexadecimal) are represented by a 8x5 font.
*/
void opcode_FX29(CHIP8 *chip8) {
_I = chip8->mem[FONTSET_MEM_START + 5 * VX(_OPCODE)];
}
/**
* Store the binary-coded decimal representation of VX at address I, I+1, and
* I+2
*/
void opcode_FX33(CHIP8 *chip8) {
int x = VX(_OPCODE);
byte one = x % 10u;
byte ten = x / 10u % 10u;
byte hund = x / 100u % 10u;
chip8->mem[_I] = one;
chip8->mem[_I + 1] = ten;
chip8->mem[_I + 2] = hund;
}
/**
* Store V0 to VX (inclusive) in memory starting at address I
*/
void opcode_FX55(CHIP8 *chip8) {
for (int i = 0; i <= X(_OPCODE); i++) {
chip8->mem[_I + i] = chip8->reg[i];
}
}
/**
* Fill V0 to VX (inclusive) with values from memory starting at address I
*/
void opcode_FX65(CHIP8 *chip8) {
for (int i = 0; i <= X(_OPCODE); i++) {
chip8->reg[i] = chip8->mem[_I + i];
}
}