-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchip8.c
934 lines (794 loc) · 19.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
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
#include "chip8.h"
#define N (currOp & 0x000F) // N: The fourth nibble. A 4-bit number.
#define NN (currOp & 0x00FF) // NN: The second byte (third and fourth nibbles). An 8-bit immediate number.
#define NNN (currOp & 0x0FFF) // NNN: The second, third and fourth nibbles. A 12-bit immediate memory address.
U16 pc = 0x0000; // Progmemory Counter
U16 regI = 0x0000; // Register Index
U16 sp = 0x0000; // Stack Pointer
U16 currOp = 0x0000; // Opcode
U8 delayTimer = 0x00; // Delay Timer
U8 soundTimer = 0x00; // Sound Timer
U8 regV[16]; // Register VXXX
U16 stackLevel[16]; // Level Stack
U8 memory[0x1000]; // Memory Ram 4 Kilobyte
U32 pixel[0x800]; // Pixels
U8 gpu[0x800]; // 64x32 Monochrome Display Memory
U16 keys[16]; // Input Keys
void *PtrPIXEL = pixel; // For Help
bool_t keyPressed = FALSE;
bool_t DEBUGMODE = FALSE; // Debug
bool_t CHECKMEMORY = FALSE;
U8 fontSet[0x50] = // FontSet
{
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
};
/*
U32 Keymap[] = {
SDLK_m,
SDLK_RIGHT,
SDLK_DOWN,
SDLK_3,
SDLK_LEFT,
SDLK_a,
SDLK_RIGHT,
SDLK_w,
SDLK_UP,
SDLK_d,
SDLK_n,
SDLK_c,
SDLK_z,
SDLK_x,
SDLK_f,
SDLK_v,
};
*/
void Reset()
{
pc = 0x200; // Set Progmemory Counter;
currOp = 0x0000;
regI = 0x00; // Reset Register I;
sp = 0x00; // Reset Register SP;
delayTimer = 0x00; // Reset Timer;
soundTimer = 0x00; // Reset Sound;
memset(regV, 0, sizeof(regV)); // Reset Register V
memset(keys, 0, sizeof(keys)); // Reset Input Keys
memset(memory, 0, sizeof(memory)); // Reset memory
memset(stackLevel, 0, sizeof(stackLevel)); // Reset Level Stack Pointer
memcpy(memory, fontSet, 0x50); // Load Fontset
}
bool_t LoadFile(const char *file) // Load Rom
{
FILE *fp = fopen(file, "rb"); // Open Rom in binary;
if (fp == NULL) // if fp is NULL error
{
perror("Rom not open check your path game ... ");
exit(EXIT_FAILURE);
return FALSE;
}
fseek(fp, 0, SEEK_END); // Set Position File BYTE 0
long size = ftell(fp); // Size Rom;
fseek(fp, 0, SEEK_SET); // Set Position File BYTE 0
// rewind(fp);
U8 *buf = malloc(sizeof(U8) * size); // Allocated Memory for Buffer;
if (buf == NULL) // if buf is NULL error
{
perror("Memory error ...");
return FALSE;
}
fread(buf, sizeof(U8), size, fp); // Read Rom
memcpy(memory + 0x200, buf, size); // Load in memory
fclose(fp); // Close Rom;
free(buf); // Clear Buffer;
return TRUE;
}
void Op00e0() // Clear the screen
{
memset(gpu, 0, sizeof(gpu));
}
void Op00ee() // Return from a subroutine
{
--sp;
pc = stackLevel[sp];
}
void Op0nnn() // Execute machine language subroutine at address NNN
{
// NOp
}
void Op1nnn() // Jump to address NNN
{
pc = NNN;
}
void Op2nnn() // Execute subroutine starting at address NNN
{
stackLevel[sp] = pc;
++sp;
pc = NNN;
}
void Op3xnn() // Skip the following instruction if the value of register VX equals NN
{
if (regV[(currOp & 0xF00) >> 8] == NN)
pc += 2;
}
void Op4xnn() // Skip the following instruction if the value of register VX is not equal to NN
{
if (regV[(currOp & 0xF00) >> 8] != NN)
pc += 2;
}
void Op5xy0() // Skip the following instruction if the value of register VX is equal to the value of register VY
{
if (regV[(currOp & 0xF00) >> 8] == regV[(currOp & 0x0F0) >> 4])
pc += 2;
}
void Op6xnn() // Store number NN in register VX
{
regV[(currOp & 0xF00) >> 8] = NN;
}
void Op7xnn() // Add the value NN to register VX
{
regV[(currOp & 0xF00) >> 8] += NN;
}
void Op8xy0() // Store the value of register VY in register VX
{
regV[(currOp & 0xF00) >> 8] = regV[(currOp & 0x0F0) >> 4];
}
void Op8xy1() // Set VX to VX OR VY
{
regV[(currOp & 0xF00) >> 8] |= regV[(currOp & 0x0F0) >> 4];
}
void Op8xy2() // Set VX to VX AND VY
{
regV[(currOp & 0xF00) >> 8] &= regV[(currOp & 0x0F0) >> 4];
}
void Op8xy3() // Set VX to VX XOR VY
{
regV[(currOp & 0xF00) >> 8] ^= regV[(currOp & 0x0F0) >> 4];
}
void Op8xy4() // Add the value of register VY to register VX Set VF to 01 if a carry occurs Set VF to 00 if a carry does not occur
{
regV[(currOp & 0xF00) >> 8] += regV[(currOp & 0x0F0) >> 4];
U16 C = regV[(currOp & 0x0F0) >> 4] + regV[(currOp & 0xF00) >> 8];
if (C > 255)
{
regV[0x0F] = 1;
}
else
{
regV[0x0F] = 0;
}
}
void Op8xy5() // Subtract the value of register VY from register VX Set VF to 00 if a borrow occurs Set VF to 01 if a borrow does not occur
{
if (regV[(currOp & 0xF00) >> 8] > regV[(currOp & 0x0F0) >> 4])
{
regV[0x0F] = 1;
}
else
{
regV[0x0F] = 0;
}
regV[(currOp & 0xF00) >> 8] -= regV[(currOp & 0x0F0) >> 4];
}
void Op8xy6() // Store the value of register VY shifted right one bit in register VX¹ Set register VF to the least significant bit prior to the shift VY is unchanged
{
regV[(currOp & 0xF00) >> 8] = regV[(currOp & 0x0F0) >> 4];
regV[0x0F] = regV[(currOp & 0xF00) >> 8] & 0x01;
regV[(currOp & 0xF00) >> 8] >>= 1;
}
void Op8xy7() // Set register VX to the value of VY minus VX Set VF to 00 if a borrow occurs Set VF to 01 if a borrow does not occur
{
if (regV[(currOp & 0xF00) >> 8] > regV[(currOp & 0x0F0) >> 4])
{
regV[0x0F] = 1;
}
else
{
regV[0x0F] = 0;
}
regV[(currOp & 0xF00) >> 8] = regV[(currOp & 0x0F0) >> 4] - regV[(currOp & 0xF00) >> 8] ;
}
void Op8xye() // Store the value of register VY shifted left one bit in register VX¹ Set register VF to the most significant bit prior to the shift VY is unchanged
{
regV[(currOp & 0xF00) >> 8] = regV[(currOp & 0x0F0) >> 4];
regV[0x0F] = regV[(currOp & 0xF00) >> 8] & 0x80;
regV[(currOp & 0xF00) >> 8] <<= 1;
}
void Op9xy0() // Skip the following instruction if the value of register VX is not equal to the value of register VY
{
if (regV[(currOp & 0xF00) >> 8] != regV[(currOp & 0x0F0) >> 4])
pc += 2;
}
void Opannn() // Store memory address NNN in register I
{
regI = NNN;
}
void Opbnnn() // Jump to address NNN + V0
{
pc = NNN + regV[0];
}
void Opcxnn() // Set VX to a random number with a mask of NN
{
int random = rand() % 0x100;
regV[(currOp & 0xF00) >> 8] = random & NN;
}
void Opdxyn() // Draw a sprite at position VX, VY with N bytes of sprite data starting at the address stored in I Set VF to 01 if any set pixels are changed to unset, and 00 otherwise
{
U8 vX = regV[(currOp & 0xF00) >> 8] % 64;
U8 vY = regV[(currOp & 0x0F0) >> 4] % 32;
U8 spriteData = N;
regV[0x0F] = 0x00;
for (int rows = 0; rows < spriteData; rows++)
{
U8 Pixel = memory[regI + rows];
for (int col = 0; col < 8; col++)
{
U16 index = (col + vX + (rows + vY) * 64);
if (Pixel & (0x80 >> col))
{
if (gpu[index] == 1)
{
regV[0x0F] = 1;
}
gpu[index] ^= 1;
}
}
}
}
void Opex9e() // Skip the following instruction if the key corresponding to the hex value currently stored in register VX is pressed
{
U8 regVX = regV[(currOp & 0xF00) >> 8];
if (keys[regVX] != 0)
{
pc += 2;
}
}
void Opexa1() // Skip the following instruction if the key corresponding to the hex value currently stored in register VX is not pressed
{
U8 regVX = regV[(currOp & 0xF00) >> 8];
if (keys[regVX] == 0)
{
pc += 2;
}
}
void Opfx07() // Store the current value of the delay timer in register VX
{
regV[(currOp & 0xF00) >> 8] = delayTimer;
}
void Opfx0a() // Wait for a keypress and store the result in register VX
{
for (int i = 0; i < 16; i++)
{
regV[(currOp & 0xF00) >> 8] = i;
keyPressed = TRUE;
}
}
void Opfx15() // Set the delay timer to the value of register VX
{
delayTimer = regV[(currOp & 0xF00) >> 8];
}
void Opfx18() // Set the sound timer to the value of register VX
{
soundTimer = regV[(currOp & 0xF00) >> 8];
}
void Opfx1e() // Add the value stored in register VX to register I
{
regI += regV[(currOp & 0xF00) >> 8];
}
void Opfx29() // Set I to the memory address of the sprite data corresponding to the hexadecimal digit stored in register VX
{
regI = regV[(currOp & 0xF00) >> 8] * 0x05;
}
void Opfx33() // Store the binary-coded decimal equivalent of the value stored in register VX at addresses I, I + 1, and I + 2
{
U8 regVX = regV[(currOp & 0xF00) >> 8]; // VX binary % 10 for number binary
memory[regI] = (regVX % 1000 / 100);
memory[regI + 1] = (regVX % 100 / 10);
memory[regI + 2] = (regVX % 10);
// printf("%d %d %d ",memory[regI],memory[regI+1],memory[regI+2]);
}
void Opfx55() // Store the values of registers V0 to VX inclusive in memory starting at address I I is set to I + X + 1 after Operation²
{
U8 regVX = (currOp & 0xF00) >> 8;
for (int i = 0; i <= regVX; i++)
{
memory[regI + i] = regV[i];
}
}
void Opfx65() // Fill registers V0 to VX inclusive with the values stored in memory starting at address I I is set to I + X + 1 after Operation²
{
U8 regVX = (currOp & 0xF00) >> 8;
for (int j = 0; j <= regVX; j++)
{
regV[j] = memory[regI + j];
}
}
U16 FetchOpcode() // Fetch Instruction
{
U8 lsb = memory[pc]; // lsb
U8 msb = memory[pc + 1]; // msb
return lsb << 8 | msb;
}
void ExecuteCpu() // Execute Instruction
{
currOp = FetchOpcode();
pc += 2;
switch (currOp & 0xF000)
{
// 00E_
case 0x0000:
switch (currOp & 0x000F)
{
case 0x0000:
Op00e0();
if(DEBUGMODE == TRUE)
printf("cls -> 0x%04X\n",(currOp&0x000F)<<1);
break;
case 0x000E:
Op00ee();
if(DEBUGMODE == TRUE)
printf("ret -> 0x%04X\n",(currOp&0x000F)<<1);
break;
default:
printf("\nUnknown Op code 1: %.4X\n", currOp);
exit(EXIT_FAILURE);
}
break;
case 0x1000:
Op1nnn();
if(DEBUGMODE == TRUE)
printf("jp -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x2000:
Op2nnn();
if(DEBUGMODE == TRUE)
printf("call -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x3000:
Op3xnn();
if(DEBUGMODE == TRUE)
printf("se -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x4000:
Op4xnn();
if(DEBUGMODE == TRUE)
printf("sne -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x5000:
Op5xy0();
// printf("ld -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x6000:
Op6xnn();
if(DEBUGMODE == TRUE)
printf("ld -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x7000:
Op7xnn();
if(DEBUGMODE == TRUE)
printf("add -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
// 8XY_
case 0x8000:
switch (currOp & 0x000F)
{
case 0x0000:
Op8xy0();
break;
case 0x0001:
Op8xy1();
if(DEBUGMODE == TRUE)
printf("or -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0002:
Op8xy2();
if(DEBUGMODE == TRUE)
printf("and -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0003:
Op8xy3();
if(DEBUGMODE == TRUE)
printf("xor -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0004:
Op8xy4();
if(DEBUGMODE == TRUE)
printf("add -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0005:
Op8xy5();
if(DEBUGMODE == TRUE)
printf("sub -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0006:
Op8xy6();
if(DEBUGMODE == TRUE)
printf("shr -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0007:
Op8xy7();
if(DEBUGMODE == TRUE)
printf("subn -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x000E:
Op8xye();
if(DEBUGMODE == TRUE)
printf("shl -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
default:
printf("\nUnknown Op code 2: %.4X\n", currOp);
exit(EXIT_FAILURE);
}
break;
case 0x9000:
Op9xy0();
if(DEBUGMODE == TRUE)
printf("sne -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0xA000:
Opannn();
if(DEBUGMODE == TRUE)
printf("ld -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0xB000:
Opbnnn();
if(DEBUGMODE == TRUE)
printf("jp -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0xC000:
Opcxnn();
if(DEBUGMODE == TRUE)
printf("rnd -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0xD000:
Opdxyn();
if(DEBUGMODE == TRUE)
printf("drw -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
// EX__
case 0xE000:
switch (currOp & 0x00FF)
{
case 0x00A1:
Opexa1();
if(DEBUGMODE == TRUE)
printf("skp -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x009E:
Opex9e();
if(DEBUGMODE == TRUE)
printf("sknp -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
default:
printf("\nUnknown Op code 3: %.4X\n", currOp);
exit(3);
}
break;
// FX__
case 0xF000:
switch (currOp & 0x00FF)
{
case 0x0007:
Opfx07();
if(DEBUGMODE == TRUE)
printf("ld dt -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x000A:
Opfx0a();
if(DEBUGMODE == TRUE)
printf("ld k -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0015:
Opfx15();
if(DEBUGMODE == TRUE)
printf("ld dt -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0018:
Opfx18();
if(DEBUGMODE == TRUE)
printf("ld st -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x001E:
Opfx1e();
if(DEBUGMODE == TRUE)
printf("add i -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0029:
Opfx29();
if(DEBUGMODE == TRUE)
printf("ld f -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0033:
Opfx33();
if(DEBUGMODE == TRUE)
printf("ld b -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0055:
Opfx55();
if(DEBUGMODE == TRUE)
printf("ld mem i -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
case 0x0065:
Opfx65();
if(DEBUGMODE == TRUE)
printf("ld x -> 0x%04X\n",(currOp&0x0FFF)<<1);
break;
default:
printf("Unknown Opcode [0xF000]: 0x%X\n", currOp);
}
break;
default:
printf("\nUnimplemented Op code 4: %.4X\n", currOp);
exit(3);
}
if (delayTimer > 0)
{
delayTimer--;
}
if (soundTimer > 0)
{
if (soundTimer == 1)
{
// playSound = true;
}
soundTimer--;
}
}
void ExecuteVideo()
{
// write value in to pixels
for (int i = 0; i < 0x800; ++i)
{
pixel[i] = (0x00FFFFFF * gpu[i]) | 0xFF000000;
}
}
void ExecuteKeys(SDL_Event event)
{
SDL_PollEvent(&event);
if (event.type == SDL_QUIT)
{
exit(EXIT_SUCCESS);
}
switch (event.key.keysym.sym)
{
case SDLK_m:
if (event.key.type == SDL_KEYUP)
{
keys[0] = 0;
}
else
{
keys[0] = 1;
}
break;
case SDLK_RIGHT:
if (event.key.type == SDL_KEYUP)
{
keys[1] = 0;
}
else
{
keys[1] = 1;
}
break;
case SDLK_DOWN:
if (event.key.type == SDL_KEYUP)
{
keys[2] = 0;
}
else
{
keys[2] = 1;
}
break;
case SDLK_3:
if (event.key.type == SDL_KEYUP)
{
keys[3] = 0;
}
else
{
keys[3] = 1;
}
break;
case SDLK_LEFT:
if (event.key.type == SDL_KEYUP)
{
keys[4] = 0;
}
else
{
keys[4] = 1;
}
break;
case SDLK_a:
if (event.key.type == SDL_KEYUP)
{
keys[5] = 0;
}
else
{
keys[5] = 1;
}
break;
case SDLK_w:
if (event.key.type == SDL_KEYUP)
{
keys[6] = 0;
}
else
{
keys[6] = 1;
}
break;
case SDLK_UP:
if (event.key.type == SDL_KEYUP)
{
keys[7] = 0;
}
else
{
keys[7] = 1;
}
break;
case SDLK_d:
if (event.key.type == SDL_KEYUP)
{
keys[8] = 0;
}
else
{
keys[8] = 1;
}
break;
case SDLK_n:
if (event.key.type == SDL_KEYUP)
{
keys[9] = 0;
}
else
{
keys[9] = 1;
}
break;
case SDLK_c:
if (event.key.type == SDL_KEYUP)
{
keys[10] = 0;
}
else
{
keys[10] = 1;
}
break;
case SDLK_z:
if (event.key.type == SDL_KEYUP)
{
keys[11] = 0;
}
else
{
keys[11] = 1;
}
break;
case SDLK_x:
if (event.key.type == SDL_KEYUP)
{
keys[12] = 0;
}
else
{
keys[12] = 1;
}
break;
case SDLK_f:
if (event.key.type == SDL_KEYUP)
{
keys[13] = 0;
}
else
{
keys[13] = 1;
}
break;
case SDLK_v:
if (event.key.type == SDL_KEYUP)
{
keys[14] = 0;
}
else
{
keys[14] = 1;
}
break;
case SDLK_RETURN:
if (event.key.type == SDL_KEYUP)
{
keys[15] = 0;
}
else
{
keys[15] = 1;
}
break;
case SDLK_F9:
if (event.key.type == SDL_KEYDOWN)
{
if(CHECKMEMORY == FALSE)
{
CHECKMEMORY = TRUE;
}else
{
printf("CHECK MEMORY DISABLED... \n");
CHECKMEMORY = FALSE;
}
}
break;
case SDLK_F10:
if (event.key.type == SDL_KEYDOWN){
if(DEBUGMODE == FALSE)
{
DEBUGMODE = TRUE;
printf("DEBUG MODE PRESS 9 FOR CONTINUE INSTR ... \n");
}else
{
DEBUGMODE = FALSE;
printf("EXIT DEBUG MODE ... \n");
}
}
break;
default:
break;
}
/*
for (int i = 0; i < 16; ++i)
{
if (event.key.keysym.sym == Keymap[i])
{
keys[i] = 1;
}
}
if (event.key.type == SDL_KEYUP)
{
for (int i = 0; i < 16; ++i)
{
if (event.key.keysym.sym == Keymap[i])
{
keys[i] = 0;
}
}
}
*/
}
void DebugMemory() // Debug Memory
{
for(int i = 0; i <= 0x1000; i++)
{
printf("%02x ",memory[i]);
if(i%16 == 15)
{
puts(" ");
}
}
}
void DebugInstr(SDL_Event event)
{
SDL_PollEvent(&event);
switch (event.key.keysym.sym){
case SDLK_9: if (event.key.type == SDL_KEYDOWN){ ExecuteCpu(); } break;
default: break;
}
}