-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip.c
594 lines (524 loc) · 14.1 KB
/
chip.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <SDL.h>
//SDL screen variables
//CPU register array
char V[16];
//Index register
unsigned short idex;
//program counter
unsigned short pc;
//stack pointer
unsigned short sp;
//emulated stack
unsigned short stack[16];
//emulated memory
uint8_t mem[4096];
//array of pixels for display output
uint8_t display[64 * 32];
//delay timer
char delayTimer;
//sound timer
char soundTimer;
//store key state
bool keys[16];
//required font
const int font[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
};
void initialize()
{
pc = 0x200; // start of program
idex = 0;
sp = 0;
//initialize arrays used to 0
for (int i = 0; i < 64 * 32; i++)
{
display[i] = 0;
}
for (int i = 0; i < 4096; i++)
{
mem[i] = 0;
}
for (int i = 0; i < 16; i++)
{
stack[i] = 0;
V[i] = 0;
}
//load font
for (int i = 0; i < 80; i++)
{
mem[0x50 + i] = font[i];
}
}
static const SDL_Scancode input_map[16] = {//list of keyboard inputs to shorten input detection
SDL_SCANCODE_1,
SDL_SCANCODE_2,
SDL_SCANCODE_3,
SDL_SCANCODE_4,
SDL_SCANCODE_Q,
SDL_SCANCODE_W,
SDL_SCANCODE_E,
SDL_SCANCODE_R,
SDL_SCANCODE_A,
SDL_SCANCODE_S,
SDL_SCANCODE_D,
SDL_SCANCODE_F,
SDL_SCANCODE_Z,
SDL_SCANCODE_X,
SDL_SCANCODE_C,
SDL_SCANCODE_V,
};
void handleKey(SDL_KeyboardEvent *event)
{
if (event->repeat == 0)
{
for (int i = 0; i < 16; i++)
{
if (event->keysym.scancode == input_map[i])
{
if (event->type == SDL_KEYDOWN)
{
keys[i] = 1;
}
if (event->type == SDL_KEYUP)
{
keys[i] = 0;
}
}
}
if (event->keysym.scancode == SDL_SCANCODE_ESCAPE)
{
exit(1);
}
}
}
void detect_input()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
exit(0);
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
handleKey(&event.key);
break;
default:
break;
}
}
}
void run_emu_cycle()
{
//get opcode (2 bytes)
unsigned short opcode = mem[pc] << 8 | mem[pc + 1];
//get variables from opcode
unsigned short x = (opcode & 0x0F00)>> 8;
unsigned short y = (opcode & 0x00F0) >> 4;
unsigned short address;
uint8_t NN = opcode & 0x00FF;
//decode opcode by ANDing for digits to compare to list on wikipedia
switch (opcode & 0xF000) {
case 0x0000:
switch (opcode & 0x00FF) {
//00E0
case 0x00E0:
printf("00E0");
for (int i = 0; i < 64 * 32; i++)
{
display[i] = 0;
}
pc += 2;
break;
//00EE
case 0x00EE:
printf("00EE");
pc = stack[sp];
sp--;
pc += 2;
break;
default:
printf("Unkown %X\n", opcode);
break;
}
break;
// 1NNN
case 0x1000:
printf("1NNN");
address = opcode & 0x0FFF;
pc = address;
break;
// 2NNN
case 0x2000:
printf("2NNN");
address = opcode & 0x0FFF;
sp++;
stack[sp] = pc;
pc = address;
break;
// 3XNN
case 0x3000:
printf("3XNN");
if (V[x] == NN)
{
pc += 2;
}
pc += 2;
break;
// 4XNN
case 0x4000:
printf("4XNN");
if (V[x] != NN)
{
pc += 2;
}
pc += 2;
break;
// 5XY0
case 0x5000:
printf("5XY0");
if (V[x] == V[y])
{
pc += 2;
}
pc += 2;
break;
// 6XNN
case 0x6000:
printf("6XNN");
V[x] = NN;
pc += 2;
break;
// 7XNN
case 0x7000:
printf("7XNN");
V[x] = V[x] + NN;
pc += 2;
break;
// 8XYn
case 0x8000:
switch (opcode & 0x000F) {
// 8XY0
case 0x0000:
printf("8XY0");
V[x] = V[y];
pc += 2;
break;
// 8XY1
case 0x0001:
printf("8XY1");
V[x] = (V[x] | V[y]);
pc += 2;
break;
// 8XY2
case 0x0002:
printf("8XY2");
V[x] = (V[x] & V[y]);
pc += 2;
break;
// 8XY3
case 0x0003:
printf("8XY3");
V[x] = (V[x] ^ V[y]);
pc += 2;
break;
// 8XY4
case 0x0004:
printf("8XY4");
if ((V[x] + V[y]) > 0xFF)
{
V[0xF] = 1;
}
else
{
V[0xF] = 0;
}
V[x] = V[x] + V[y];
pc += 2;
break;
// 8XY5
case 0x0005:
printf("8XY5");
V[0xF] = 0;
if (V[x] > V[y])
{
V[0xF] = 1;
}
V[x] -= V[y];
pc += 2;
break;
// 8XY6
case 0x0006:
printf("8XY6");
V[0xF] = V[x] & 1;
V[x] = (V[x] >> 1);
pc += 2;
break;
// 8XY7
case 0x0007:
printf("8XY7");
if (V[y] > V[x])
{
V[0xF] = 1;
}
else
{
V[0xF] = 0;
}
V[x] = V[y] - V[x];
pc += 2;
break;
// 8XYE
case 0x000E:
printf("8XYE");
V[0xF] = x >> 7;
V[x] <<= 1;
pc += 2;
break;
default:
printf("Unknown %X\n", opcode);
break;
}
break;
// 9XY0
case 0x9000:
printf("9XY0");
if (V[x] != V[y])
{
pc += 2;
}
pc += 2;
break;
// ANNN
case 0xA000:
printf("ANNN");
address = opcode & 0x0FFF;
idex = address;
pc += 2;
break;
// BNNN
case 0xB000:
printf("BNNN");
address = opcode & 0x0FFF;
pc = address + V[0];
break;
// CXNN
case 0xC000:
printf("CXNN");
V[x] = rand() & NN;
pc += 2;
break;
//DXYN pain
case 0xD000:
printf("DXYN");
V[0xF] = 0;
unsigned short height = opcode & 0x000F;
unsigned short pixel;
for (int row = 0; row < height; row++)
{
pixel = mem[idex + row];
for (int column = 0; column < 8; column++)
{
if ((pixel & (0x80 >> column)) != 0)
{
if (display[V[x] + column + ((V[y] + row) * 64)] == 1)
{
V[0xF] = 1;// if pixel turns off set collision flag
}
display[V[x] + column + ((V[y] + row) * 64)] ^= 1;
}
}
}
pc += 2;
break;
case 0xE000:
switch (opcode & 0x00FF) {
// EX9E
case 0x009E:
printf("EX9E");
if(keys[V[x]] == 1)
{
pc += 2;
}
pc += 2;
break;
// EXA1
case 0x00A1:
printf("EXA1");
if(keys[V[x]] == 0)
{
pc += 2;
}
pc += 2;
break;
default:
printf("Unkown %X", opcode);
}
break;
case 0xF000:
switch (opcode & 0x00FF) {
// FX07
case 0x0007:
printf("FX07");
V[x] = delayTimer;
pc += 2;
break;
// FX0A
case 0x000A:
printf("FX0A");
for (int i = 0; i < 16; i++)
{
if (keys[i] == 1)
{
V[x] = i;
pc += 2;
break;
}
}
break;
// FX15
case 0x0015:
printf("FX15");
delayTimer = V[x];
pc += 2;
break;
// FX18
case 0x0018:
printf("FX18");
soundTimer = V[x];
pc += 2;
break;
// FX1E
case 0x001E:
printf("FX1E");
idex = idex + V[x];
pc += 2;
break;
// FX29
case 0x0029:
printf("FX29");
idex = V[x] * 5;
pc += 2;
break;
//FX33
case 0x0033:
printf("FX33");
mem[idex] = V[(x) >> 8] / 100;
mem[idex+1] = (V[(x) >> 8] / 10) % 10;
mem[idex+2] = V[(x) >> 8] % 10;
pc += 2;
break;
// FX55
case 0x0055:
printf("FX55");
for (int i = 0; i <= x; i++)
{
mem[idex + i] = V[i];
}
pc += 2;
break;
//FX65
case 0x0065:
printf("FX65");
for (int i = 0; i <= x; i++)
{
V[i] = mem[idex + i];
}
pc += 2;
break;
default:
printf("Unknown %X\n", opcode);
break;
}
break;
default:
printf("Unknown %X\n", opcode);
break;
}
}
int main(int argc, char *argv[])
{
long timertracker = 0;
printf("App is Running\n");
//setup SDL window
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Surface *surface;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
return 3;
}
if (SDL_CreateWindowAndRenderer(1200, 600, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window and renderer: %s", SDL_GetError());
return 3;
}
SDL_RenderSetLogicalSize(renderer, 64, 32);
//set variables to zero and prepare for emulation
initialize();
//load game into memory
//open rom
FILE* fp = fopen(argv[1], "rb");
if (fp == NULL)
{
printf("fopen failure\n");
return 1;
}
//load rom into chip8 memory
fread(&mem[0x200], 1, 4096 - 0x200, fp);
//SDL_Surface * image = SDL_LoadBMP("sample.bmp");
SDL_Surface *screen = SDL_GetWindowSurface(window);
SDL_Texture * texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 64, 32);
uint8_t *pixels = malloc(64 * 32 * 4 * sizeof(uint8_t));
//cycle
while (true)
{
//detect keyboard presses and update keys array
detect_input();
//run next opcode
run_emu_cycle();
timertracker++;
// update texture
for (int i = 0; i < 64 * 32; i++)
{
pixels[i * 4] = 255;
pixels[i * 4 + 1] = display[i] ? 255 : 0;
pixels[i * 4 + 2] = display[i] ? 255 : 0;
pixels[i * 4 + 3] = display[i] ? 255 : 0;
}
SDL_UpdateTexture(texture, NULL, pixels, 64 * 4);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
//decrement timers
if (delayTimer > 0 && timertracker >= 10)
{
delayTimer--;
timertracker = 0;
}
if (soundTimer > 0)
{
soundTimer--;
}
}
}