Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
4119 lines (3888 loc) · 85.5 KB
/
Copy pathmain.cpp
File metadata and controls
4119 lines (3888 loc) · 85.5 KB
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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <malloc.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <windows.h>
#include <fstream>
#include <iostream>
#include <vector>
#include "cb.h"
#include "cpu.h"
#include "include\SDL.h"
using namespace std;
// Screen Dimensions.
const int SCREEN_WIDTH = 160;
const int SCREEN_HEIGHT = 144;
// Clockspeed.
#define CLOCKSPEED 4194304
#define CYCLES_PER_FRAME 69905
int timer_count = 0;
uint16_t curr_clock_speed = 1024;
int divider_count = 0;
// Operands (either one depending on size).
uint8_t Operand8;
uint16_t Operand16;
// Cycle Counter
long int cycle_count;
// Stores amount of cycles in last instruction.
int last_cycles;
bool IME = 0; // Interrupt Master Enable Flag.
// Graphics Variables
int scanline_count;
uint8_t Tile_Map[384][8][8];
struct RGB {
uint8_t red;
uint8_t green;
uint8_t blue;
} frame_buffer[SCREEN_HEIGHT][SCREEN_WIDTH], color_palette[4];
// Joypad Variable
uint8_t joypad_state = 0xFF;
// Memory Variables
/*
$FFFF Interrupt Enable Flag
$FF80-$FFFE Zero Page - 127 bytes
$FF00-$FF7F Hardware I/O Registers
$FEA0-$FEFF Unusable Memory
$FE00-$FE9F OAM - Object Attribute Memory
$E000-$FDFF Echo RAM - Reserved, Do Not Use
$D000-$DFFF Internal RAM - Bank 1-7 (switchable - CGB only)
$C000-$CFFF Internal RAM - Bank 0 (fixed)
$A000-$BFFF Cartridge RAM (If Available)
$9C00-$9FFF BG Map Data 2
$9800-$9BFF BG Map Data 1
$8000-$97FF TILE RAM (Graphics)
$4000-$7FFF Cartridge ROM - Switchable Banks 1-xx
$0150-$3FFF Cartridge ROM - Bank 0 (fixed)
$0100-$014F Cartridge Header Area
$0000-$00FF Restart and Interrupt Vectors
*/
uint8_t memory[65536] = { 0 };
uint8_t* rom;
uint8_t* boot_rom;
bool enable_boot = true;
uint8_t bank_offset = 0;
bool mbc1 = false;
bool mbc2 = false;
// Registers
struct registers {
struct {
union {
struct {
uint8_t f; // Flag Register. [z,n,h,c,0,0,0,0]
uint8_t a;
};
uint16_t af;
};
};
struct {
union {
struct {
uint8_t c;
uint8_t b;
};
uint16_t bc;
};
};
struct {
union {
struct {
uint8_t e;
uint8_t d;
};
uint16_t de;
};
};
struct {
union {
struct {
uint8_t l;
uint8_t h;
};
uint16_t hl;
};
};
uint16_t sp; // Stack pointer.
uint16_t pc; // Program counter.
} registers;
// Instruction Lookup Struct
struct instruction {
char name[15];
int length;
void (*fcnPtr)();
};
// SDL State
SDL_Event event;
SDL_Renderer* renderer;
SDL_Window* window;
SDL_Texture* texture;
SDL_Surface* icon;
// Rom Loading
void read_rom(char* filename);
void load_bootrom(char* filename);
void detect_banking_mode();
// User I/O
uint8_t key_state(); // Sets up FF00 depending on key presses.
void key_press(int key); // Does a key press
void key_release(int key); // Does a key release
void handle_input(); // Detects key presses.
// Memory Operations
uint8_t read_byte(uint16_t location); // Read memory at location.
void write_byte(uint8_t data, uint16_t location); // Write memory at location.
void dma_transfer(uint8_t data); // Does a direct memory transfer.
// CPU Operations
void cpu_cycle(); // Reads current opcode then executes instruction. Also prints output.
void interupts(); // Checks if there is any interputs to do and then does them.
void do_interupt(uint8_t interupt); // Carries out the specified interupt and resets ime.
void set_interupt(uint8_t interupt); // Allows for interupts to be set.
void print_registers(); // Prints registers info.
void update_timers();
// Graphics functions.
void initialize_sdl(); // Starts SDL Window and render surface.
void setup_color_pallete(); // Sets up the colours. (Todo: load from rom)
void load_tiles(); // Loads Tiles into Array Tiles[][x][y].
void render_tile_map_line(); // Arranges tiles according to tilemap and displays
// onto
// screen.
void render_all_tiles(); // Test function to render all the tiles onto screen.
void render_sprites(); // Renders the sprites.
void display_buffer(); // Loads buffer into texture and renders it.
void render_graphics(); // Combines above.
void shutdown(); // Shuts down SDL and exits.
void set_lcd_status(); // Sets the lcd status register [0xFF41] according to
// the
// current scanline.
void increment_scan_line();
// Arithmetic Instructions (on register a).
void add_byte(uint8_t value2); // Adds value2 to register a and sets relevent flags.
uint16_t add_2_byte(uint16_t a, uint16_t b); // Adds a to b and sets relevent flags.
void sub_byte(uint8_t value); // Subtracts value from register a and sets relevant flags.
void adc(uint8_t a);
void cp(uint8_t value); // Compare value with register a setting flags. (Basically subtraction without storing value)
uint8_t inc(uint8_t value); // Increment value and set flags.
uint8_t dec(uint8_t value); // Decrement value and set flags.
// Rotations.
uint8_t RotByteLeft(uint8_t number); // Rotate left and set carry flag.
uint8_t RotByteRight(uint8_t number); // Rotate right and set carry flag.
uint8_t Rotate_Left_Carry(uint8_t number); // Rotate left into carry.
uint8_t Rotate_Right_Carry(uint8_t number); // Rotate right into carry.
// Shifts.
uint8_t Shift_Left(uint8_t number); // Shift left into carry.
uint8_t Shift_Right(uint8_t number); // Shift right into carry.
uint8_t Shift_Right_A(uint8_t number); // Arithmetic Shift.
// Swap.
uint8_t Swap(uint8_t number); // Swaps highest 4 bits with lowest 4 bits.
// Logic
void And(uint8_t a); // register a AND value.
void Or(uint8_t a); // register a OR value.
void Xor(uint8_t a); // register a XOR value.
// Stack Instructions.
void Push(uint16_t a); // Places value on top of stack and decrements stack pointer.
uint16_t Pop(); // Stack value off stack, stores it and increments stack pointer.
// Bit tests.
uint8_t Bit_Test(uint8_t bit, uint8_t number);
uint8_t test_bit(uint8_t bit, uint8_t number); // Doesn't set flags.
// Bit sets.
uint8_t Res(uint8_t bit, uint8_t number); // Resets specified bit.
uint8_t Set(uint8_t bit, uint8_t number); // Sets specified bit.
// Flag functions.
void Set_Z_Flag(); // Set zero flag.
void Set_N_Flag(); // Set negative flag.
void Set_H_Flag(); // Set half-carry flag.
void Set_C_Flag(); // Set full-carry flag.
void Clear_Z_Flag(); // Clear zero flag.
void CLear_N_Flag(); // Clear negative flag.
void Clear_H_Flag(); // Clear half-carry flag.
void Clear_C_Flag(); // Clear carry flag.
// Array of structures that uses instruction opcode as index and stores name
// length and function pointer.
const struct instruction instructions[] = {
{"NOP", 1, NOP}, // 0x0
{"LD BC d16", 3, LD_BC_d16}, // 0x1
{"LD BCp A", 1, LD_BCp_A}, // 0x2
{"INC BC", 1, INC_BC}, // 0x3
{"INC B", 1, INC_B}, // 0x4
{"DEC B", 1, DEC_B}, // 0x5
{"LD B d8", 2, LD_B_d8}, // 0x6
{"RLCA", 1, RLCA}, // 0x7
{"LD a16p SP", 3, LD_a16p_SP}, // 0x8
{"ADD HL BC", 1, ADD_HL_BC}, // 0x9
{"LD A BCp", 1, LD_A_BCp}, // 0xa
{"DEC BC", 1, DEC_BC}, // 0xb
{"INC C", 1, INC_C}, // 0xc
{"DEC C", 1, DEC_C}, // 0xd
{"LD C d8", 2, LD_C_d8}, // 0xe
{"RRCA", 1, RRCA}, // 0xf
{"STOP", 1, STOP_0}, // 0x10
{"LD DE d16", 3, LD_DE_d16}, // 0x11
{"LD DEp A", 1, LD_DEp_A}, // 0x12
{"INC DE", 1, INC_DE}, // 0x13
{"INC D", 1, INC_D}, // 0x14
{"DEC D", 1, DEC_D}, // 0x15
{"LD D d8", 2, LD_D_d8}, // 0x16
{"RLA", 1, RLA}, // 0x17
{"JR r8", 2, JR_r8}, // 0x18
{"ADD HL DE", 1, ADD_HL_DE}, // 0x19
{"LD A DEp", 1, LD_A_DEp}, // 0x1a
{"DEC DE", 1, DEC_DE}, // 0x1b
{"INC E", 1, INC_E}, // 0x1c
{"DEC E", 1, DEC_E}, // 0x1d
{"LD E d8", 2, LD_E_d8}, // 0x1e
{"RRA", 1, RRA}, // 0x1f
{"JR NZ r8", 2, JR_NZ_r8}, // 0x20
{"LD HL d16", 3, LD_HL_d16}, // 0x21
{"LD HLIp A", 1, LD_HLIp_A}, // 0x22
{"INC HL", 1, INC_HL}, // 0x23
{"INC H", 1, INC_H}, // 0x24
{"DEC H", 1, DEC_H}, // 0x25
{"LD H d8", 2, LD_H_d8}, // 0x26
{"DAA", 1, DAA}, // 0x27
{"JR Z r8", 2, JR_Z_r8}, // 0x28
{"ADD HL HL", 1, ADD_HL_HL}, // 0x29
{"LD A HLIp", 1, LD_A_HLIp}, // 0x2a
{"DEC HL", 1, DEC_HL}, // 0x2b
{"INC L", 1, INC_L}, // 0x2c
{"DEC L", 1, DEC_L}, // 0x2d
{"LD L d8", 2, LD_L_d8}, // 0x2e
{"CPL", 1, CPL}, // 0x2f
{"JR NC r8", 2, JR_NC_r8}, // 0x30
{"LD SP d16", 3, LD_SP_d16}, // 0x31
{"LD HLdp A", 1, LD_HLdp_A}, // 0x32
{"INC SP", 1, INC_SP}, // 0x33
{"INC HLp", 1, INC_HLp}, // 0x34
{"DEC HLp", 1, DEC_HLp}, // 0x35
{"LD HLp d8", 2, LD_HLp_d8}, // 0x36
{"SCF", 1, SCF}, // 0x37
{"JR C r8", 2, JR_C_r8}, // 0x38
{"ADD HL SP", 1, ADD_HL_SP}, // 0x39
{"LD A HLdp", 1, LD_A_HLdp}, // 0x3a
{"DEC SP", 1, DEC_SP}, // 0x3b
{"INC A", 1, INC_A}, // 0x3c
{"DEC A", 1, DEC_A}, // 0x3d
{"LD A d8", 2, LD_A_d8}, // 0x3e
{"CCF", 1, CCF}, // 0x3f
{"LD B B", 1, LD_B_B}, // 0x40
{"LD B C", 1, LD_B_C}, // 0x41
{"LD B D", 1, LD_B_D}, // 0x42
{"LD B E", 1, LD_B_E}, // 0x43
{"LD B H", 1, LD_B_H}, // 0x44
{"LD B L", 1, LD_B_L}, // 0x45
{"LD B HLp", 1, LD_B_HLp}, // 0x46
{"LD B A", 1, LD_B_A}, // 0x47
{"LD C B", 1, LD_C_B}, // 0x48
{"LD C C", 1, LD_C_C}, // 0x49
{"LD C D", 1, LD_C_D}, // 0x4a
{"LD C E", 1, LD_C_E}, // 0x4b
{"LD C H", 1, LD_C_H}, // 0x4c
{"LD C L", 1, LD_C_L}, // 0x4d
{"LD C HLp", 1, LD_C_HLp}, // 0x4e
{"LD C A", 1, LD_C_A}, // 0x4f
{"LD D B", 1, LD_D_B}, // 0x50
{"LD D C", 1, LD_D_C}, // 0x51
{"LD D D", 1, LD_D_D}, // 0x52
{"LD D E", 1, LD_D_E}, // 0x53
{"LD D H", 1, LD_D_H}, // 0x54
{"LD D L", 1, LD_D_L}, // 0x55
{"LD D HLp", 1, LD_D_HLp}, // 0x56
{"LD D A", 1, LD_D_A}, // 0x57
{"LD E B", 1, LD_E_B}, // 0x58
{"LD E C", 1, LD_E_C}, // 0x59
{"LD E D", 1, LD_E_D}, // 0x5a
{"LD E E", 1, LD_E_E}, // 0x5b
{"LD E H", 1, LD_E_H}, // 0x5c
{"LD E L", 1, LD_E_L}, // 0x5d
{"LD E HLp", 1, LD_E_HLp}, // 0x5e
{"LD E A", 1, LD_E_A}, // 0x5f
{"LD H B", 1, LD_H_B}, // 0x60
{"LD H C", 1, LD_H_C}, // 0x61
{"LD H D", 1, LD_H_D}, // 0x62
{"LD H E", 1, LD_H_E}, // 0x63
{"LD H H", 1, LD_H_H}, // 0x64
{"LD H L", 1, LD_H_L}, // 0x65
{"LD H HLp", 1, LD_H_HLp}, // 0x66
{"LD H A", 1, LD_H_A}, // 0x67
{"LD L B", 1, LD_L_B}, // 0x68
{"LD L C", 1, LD_L_C}, // 0x69
{"LD L D", 1, LD_L_D}, // 0x6a
{"LD L E", 1, LD_L_E}, // 0x6b
{"LD L H", 1, LD_L_H}, // 0x6c
{"LD L L", 1, LD_L_L}, // 0x6d
{"LD L HLp", 1, LD_L_HLp}, // 0x6e
{"LD L A", 1, LD_L_A}, // 0x6f
{"LD HLp B", 1, LD_HLp_B}, // 0x70
{"LD HLp C", 1, LD_HLp_C}, // 0x71
{"LD HLp D", 1, LD_HLp_D}, // 0x72
{"LD HLp E", 1, LD_HLp_E}, // 0x73
{"LD HLp H", 1, LD_HLp_H}, // 0x74
{"LD HLp L", 1, LD_HLp_L}, // 0x75
{"HALT", 1, HALT}, // 0x76
{"LD HLp A", 1, LD_HLp_A}, // 0x77
{"LD A B", 1, LD_A_B}, // 0x78
{"LD A C", 1, LD_A_C}, // 0x79
{"LD A D", 1, LD_A_D}, // 0x7a
{"LD A E", 1, LD_A_E}, // 0x7b
{"LD A H", 1, LD_A_H}, // 0x7c
{"LD A L", 1, LD_A_L}, // 0x7d
{"LD A HLp", 1, LD_A_HLp}, // 0x7e
{"LD A A", 1, LD_A_A}, // 0x7f
{"ADD A B", 1, ADD_A_B}, // 0x80
{"ADD A C", 1, ADD_A_C}, // 0x81
{"ADD A D", 1, ADD_A_D}, // 0x82
{"ADD A E", 1, ADD_A_E}, // 0x83
{"ADD A H", 1, ADD_A_H}, // 0x84
{"ADD A L", 1, ADD_A_L}, // 0x85
{"ADD A HLp", 1, ADD_A_HLp}, // 0x86
{"ADD A A", 1, ADD_A_A}, // 0x87
{"ADC A B", 1, ADC_A_B}, // 0x88
{"ADC A C", 1, ADC_A_C}, // 0x89
{"ADC A D", 1, ADC_A_D}, // 0x8a
{"ADC A E", 1, ADC_A_E}, // 0x8b
{"ADC A H", 1, ADC_A_H}, // 0x8c
{"ADC A L", 1, ADC_A_L}, // 0x8d
{"ADC A HLp", 1, ADC_A_HLp}, // 0x8e
{"ADC A A", 1, ADC_A_A}, // 0x8f
{"SUB B", 1, SUB_B}, // 0x90
{"SUB C", 1, SUB_C}, // 0x91
{"SUB D", 1, SUB_D}, // 0x92
{"SUB E", 1, SUB_E}, // 0x93
{"SUB H", 1, SUB_H}, // 0x94
{"SUB L", 1, SUB_L}, // 0x95
{"SUB HLp", 1, SUB_HLp}, // 0x96
{"SUB A", 1, SUB_A}, // 0x97
{"SBC A B", 1, SBC_A_B}, // 0x98
{"SBC A C", 1, SBC_A_C}, // 0x99
{"SBC A D", 1, SBC_A_D}, // 0x9a
{"SBC A E", 1, SBC_A_E}, // 0x9b
{"SBC A H", 1, SBC_A_H}, // 0x9c
{"SBC A L", 1, SBC_A_L}, // 0x9d
{"SBC A HLp", 1, SBC_A_HLp}, // 0x9e
{"SBC A A", 1, SBC_A_A}, // 0x9f
{"AND B", 1, AND_B}, // 0xa0
{"AND C", 1, AND_C}, // 0xa1
{"AND D", 1, AND_D}, // 0xa2
{"AND E", 1, AND_E}, // 0xa3
{"AND H", 1, AND_H}, // 0xa4
{"AND L", 1, AND_L}, // 0xa5
{"AND HLp", 1, AND_HLp}, // 0xa6
{"AND A", 1, AND_A}, // 0xa7
{"XOR B", 1, XOR_B}, // 0xa8
{"XOR C", 1, XOR_C}, // 0xa9
{"XOR D", 1, XOR_D}, // 0xaa
{"XOR E", 1, XOR_E}, // 0xab
{"XOR H", 1, XOR_H}, // 0xac
{"XOR L", 1, XOR_L}, // 0xad
{"XOR HLp", 1, XOR_HLp}, // 0xae
{"XOR A", 1, XOR_A}, // 0xaf
{"OR B", 1, OR_B}, // 0xb0
{"OR C", 1, OR_C}, // 0xb1
{"OR D", 1, OR_D}, // 0xb2
{"OR E", 1, OR_E}, // 0xb3
{"OR H", 1, OR_H}, // 0xb4
{"OR L", 1, OR_L}, // 0xb5
{"OR HLp", 1, OR_HLp}, // 0xb6
{"OR A", 1, OR_A}, // 0xb7
{"CP B", 1, CP_B}, // 0xb8
{"CP C", 1, CP_C}, // 0xb9
{"CP D", 1, CP_D}, // 0xba
{"CP E", 1, CP_E}, // 0xbb
{"CP H", 1, CP_H}, // 0xbc
{"CP L", 1, CP_L}, // 0xbd
{"CP HLp", 1, CP_HLp}, // 0xbe
{"CP A", 1, CP_A}, // 0xbf
{"RET", 1, RET_NZ}, // 0xc0
{"POP", 1, POP_BC}, // 0xc1
{"JP NZ a16", 3, JP_NZ_a16}, // 0xc2
{"JP", 3, JP_a16}, // 0xc3
{"CALL NZ a16", 3, CALL_NZ_a16}, // 0xc4
{"PUSH BC", 1, PUSH_BC}, // 0xc5
{"ADD A d8", 2, ADD_A_d8}, // 0xc6
{"RST", 1, RST_00H}, // 0xc7
{"RET Z", 1, RET_Z}, // 0xc8
{"RET", 1, RET}, // 0xc9
{"JP Z a16", 3, JP_Z_a16}, // 0xca
{"PREFIX", 2, PREFIX_CB}, // 0xcb
{"CALL Z a16", 3, CALL_Z_a16}, // 0xcc
{"CALL a16", 3, CALL_a16}, // 0xcd
{"ADC A d8", 2, ADC_A_d8}, // 0xce
{"RST", 1, RST_08H}, // 0xcf
{"RET", 1, RET_NC}, // 0xd0
{"POP", 1, POP_DE}, // 0xd1
{"JP NC a16", 3, JP_NC_a16}, // 0xd2
{"UNKNOWN", 0, NULL}, // 0xd3
{"CALL NC a16", 3, CALL_NC_a16}, // 0xd4
{"PUSH DE", 1, PUSH_DE}, // 0xd5
{"SUB d8", 2, SUB_d8}, // 0xd6
{"RST", 1, RST_10H}, // 0xd7
{"RET C", 1, RET_C}, // 0xd8
{"RETI", 1, RETI}, // 0xd9
{"JP C a16", 3, JP_C_a16}, // 0xda
{"UNKNOWN", 0, NULL}, // 0xdb
{"CALL C a16", 3, CALL_C_a16}, // 0xdc
{"UNKNOWN", 0, NULL}, // 0xdd
{"SBC A d8", 2, SBC_A_d8}, // 0xde
{"RST", 1, RST_18H}, // 0xdf
{"LDH a8p A", 2, LDH_a8p_A}, // 0xe0
{"POP HL", 1, POP_HL}, // 0xe1
{"LD cp A", 1, LD_Cp_A}, // 0xe2
{"UNKNOWN", 0, NULL}, // 0xe3
{"UNKNOWN", 0, NULL}, // 0xe4
{"PUSH HL", 1, PUSH_HL}, // 0xe5
{"AND D8", 2, AND_d8}, // 0xe6
{"RST", 1, RST_20H}, // 0xe7
{"ADD SP r8", 2, ADD_SP_r8}, // 0xe8
{"JP HLp", 1, JP_HLp}, // 0xe9
{"LD a16p A", 3, LD_a16p_A}, // 0xea
{"UNKNOWN", 0, NULL}, // 0xeb
{"UNKNOWN", 0, NULL}, // 0xec
{"UNKNOWN", 0, NULL}, // 0xed
{"XOR D8", 2, XOR_d8}, // 0xee
{"RST", 1, RST_28H}, // 0xef
{"LDH A a8p", 2, LDH_A_a8p}, // 0xf0
{"POP AF", 1, POP_AF}, // 0xf1
{"LD A cp", 1, LD_A_Cp}, // 0xf2
{"DI", 1, DI}, // 0xf3
{"UNKNOWN", 0, NULL}, // 0xf4
{"PUSH AF", 1, PUSH_AF}, // 0xf5
{"OR d8", 2, OR_d8}, // 0xf6
{"RST", 1, RST_30H}, // 0xf7
{"LD HL SP+r8", 2, LD_HL_SPr8}, // 0xf8
{"LD SP HL", 1, LD_SP_HL}, // 0xf9
{"LD A a16p", 3, LD_A_a16p}, // 0xfa
{"EI", 1, EI}, // 0xfb
{"UNKNOWN", 0, NULL}, // 0xfc
{"UNKNOWN", 0, NULL}, // 0xfd
{"CP d8", 2, CP_d8}, // 0xfe
{"RST", 1, RST_38H}, // 0xff
};
// Array of structures for cb instruction.
const struct instruction CB_instructions[] = {
{"RLC B", 2, RLC_B}, // 0x0
{"RLC C", 2, RLC_C}, // 0x1
{"RLC D", 2, RLC_D}, // 0x2
{"RLC E", 2, RLC_E}, // 0x3
{"RLC H", 2, RLC_H}, // 0x4
{"RLC L", 2, RLC_L}, // 0x5
{"RLC HLp", 2, RLC_HLp}, // 0x6
{"RLC A", 2, RLC_A}, // 0x7
{"RRC B", 2, RRC_B}, // 0x8
{"RRC C", 2, RRC_C}, // 0x9
{"RRC D", 2, RRC_D}, // 0xa
{"RRC E", 2, RRC_E}, // 0xb
{"RRC H", 2, RRC_H}, // 0xc
{"RRC L", 2, RRC_L}, // 0xd
{"RRC HLp", 2, RRC_HLp}, // 0xe
{"RRC A", 2, RRC_A}, // 0xf
{"RL B", 2, RL_B}, // 0x10
{"RL C", 2, RL_C}, // 0x11
{"RL D", 2, RL_D}, // 0x12
{"RL E", 2, RL_E}, // 0x13
{"RL H", 2, RL_H}, // 0x14
{"RL L", 2, RL_L}, // 0x15
{"RL HLp", 2, RL_HLp}, // 0x16
{"RL A", 2, RL_A}, // 0x17
{"RR B", 2, RR_B}, // 0x18
{"RR C", 2, RR_C}, // 0x19
{"RR D", 2, RR_D}, // 0x1a
{"RR E", 2, RR_E}, // 0x1b
{"RR H", 2, RR_H}, // 0x1c
{"RR L", 2, RR_L}, // 0x1d
{"RR HLp", 2, RR_HLp}, // 0x1e
{"RR A", 2, RR_A}, // 0x1f
{"SLA B", 2, SLA_B}, // 0x20
{"SLA C", 2, SLA_C}, // 0x21
{"SLA D", 2, SLA_D}, // 0x22
{"SLA E", 2, SLA_E}, // 0x23
{"SLA H", 2, SLA_H}, // 0x24
{"SLA L", 2, SLA_L}, // 0x25
{"SLA HLp", 2, SLA_HLp}, // 0x26
{"SLA A", 2, SLA_A}, // 0x27
{"SRA B", 2, SRA_B}, // 0x28
{"SRA C", 2, SRA_C}, // 0x29
{"SRA D", 2, SRA_D}, // 0x2a
{"SRA E", 2, SRA_E}, // 0x2b
{"SRA H", 2, SRA_H}, // 0x2c
{"SRA L", 2, SRA_L}, // 0x2d
{"SRA HLp", 2, SRA_HLp}, // 0x2e
{"SRA A", 2, SRA_A}, // 0x2f
{"SWAP B", 2, SWAP_B}, // 0x30
{"SWAP C", 2, SWAP_C}, // 0x31
{"SWAP D", 2, SWAP_D}, // 0x32
{"SWAP E", 2, SWAP_E}, // 0x33
{"SWAP H", 2, SWAP_H}, // 0x34
{"SWAP L", 2, SWAP_L}, // 0x35
{"SWAP HLp", 2, SWAP_HLp}, // 0x36
{"SWAP A", 2, SWAP_A}, // 0x37
{"SRL B", 2, SRL_B}, // 0x38
{"SRL C", 2, SRL_C}, // 0x39
{"SRL D", 2, SRL_D}, // 0x3a
{"SRL E", 2, SRL_E}, // 0x3b
{"SRL H", 2, SRL_H}, // 0x3c
{"SRL L", 2, SRL_L}, // 0x3d
{"SRL HLp", 2, SRL_HLp}, // 0x3e
{"SRL A", 2, SRL_A}, // 0x3f
{"BIT 0 B", 2, BIT_0_B}, // 0x40
{"BIT 0 C", 2, BIT_0_C}, // 0x41
{"BIT 0 D", 2, BIT_0_D}, // 0x42
{"BIT 0 E", 2, BIT_0_E}, // 0x43
{"BIT 0 H", 2, BIT_0_H}, // 0x44
{"BIT 0 L", 2, BIT_0_L}, // 0x45
{"BIT 0 HLp", 2, BIT_0_HLp}, // 0x46
{"BIT 0 A", 2, BIT_0_A}, // 0x47
{"BIT 1 B", 2, BIT_1_B}, // 0x48
{"BIT 1 C", 2, BIT_1_C}, // 0x49
{"BIT 1 D", 2, BIT_1_D}, // 0x4a
{"BIT 1 E", 2, BIT_1_E}, // 0x4b
{"BIT 1 H", 2, BIT_1_H}, // 0x4c
{"BIT 1 L", 2, BIT_1_L}, // 0x4d
{"BIT 1 HLp", 2, BIT_1_HLp}, // 0x4e
{"BIT 1 A", 2, BIT_1_A}, // 0x4f
{"BIT 2 B", 2, BIT_2_B}, // 0x50
{"BIT 2 C", 2, BIT_2_C}, // 0x51
{"BIT 2 D", 2, BIT_2_D}, // 0x52
{"BIT 2 E", 2, BIT_2_E}, // 0x53
{"BIT 2 H", 2, BIT_2_H}, // 0x54
{"BIT 2 L", 2, BIT_2_L}, // 0x55
{"BIT 2 HLp", 2, BIT_2_HLp}, // 0x56
{"BIT 2 A", 2, BIT_2_A}, // 0x57
{"BIT 3 B", 2, BIT_3_B}, // 0x58
{"BIT 3 C", 2, BIT_3_C}, // 0x59
{"BIT 3 D", 2, BIT_3_D}, // 0x5a
{"BIT 3 E", 2, BIT_3_E}, // 0x5b
{"BIT 3 H", 2, BIT_3_H}, // 0x5c
{"BIT 3 L", 2, BIT_3_L}, // 0x5d
{"BIT 3 HLp", 2, BIT_3_HLp}, // 0x5e
{"BIT 3 A", 2, BIT_3_A}, // 0x5f
{"BIT 4 B", 2, BIT_4_B}, // 0x60
{"BIT 4 C", 2, BIT_4_C}, // 0x61
{"BIT 4 D", 2, BIT_4_D}, // 0x62
{"BIT 4 E", 2, BIT_4_E}, // 0x63
{"BIT 4 H", 2, BIT_4_H}, // 0x64
{"BIT 4 L", 2, BIT_4_L}, // 0x65
{"BIT 4 HLp", 2, BIT_4_HLp}, // 0x66
{"BIT 4 A", 2, BIT_4_A}, // 0x67
{"BIT 5 B", 2, BIT_5_B}, // 0x68
{"BIT 5 C", 2, BIT_5_C}, // 0x69
{"BIT 5 D", 2, BIT_5_D}, // 0x6a
{"BIT 5 E", 2, BIT_5_E}, // 0x6b
{"BIT 5 H", 2, BIT_5_H}, // 0x6c
{"BIT 5 L", 2, BIT_5_L}, // 0x6d
{"BIT 5 HLp", 2, BIT_5_HLp}, // 0x6e
{"BIT 5 A", 2, BIT_5_A}, // 0x6f
{"BIT 6 B", 2, BIT_6_B}, // 0x70
{"BIT 6 C", 2, BIT_6_C}, // 0x71
{"BIT 6 D", 2, BIT_6_D}, // 0x72
{"BIT 6 E", 2, BIT_6_E}, // 0x73
{"BIT 6 H", 2, BIT_6_H}, // 0x74
{"BIT 6 L", 2, BIT_6_L}, // 0x75
{"BIT 6 HLp", 2, BIT_6_HLp}, // 0x76
{"BIT 6 A", 2, BIT_6_A}, // 0x77
{"BIT 7 B", 2, BIT_7_B}, // 0x78
{"BIT 7 C", 2, BIT_7_C}, // 0x79
{"BIT 7 D", 2, BIT_7_D}, // 0x7a
{"BIT 7 E", 2, BIT_7_E}, // 0x7b
{"BIT 7 H", 2, BIT_7_H}, // 0x7c
{"BIT 7 L", 2, BIT_7_L}, // 0x7d
{"BIT 7 HLp", 2, BIT_7_HLp}, // 0x7e
{"BIT 7 A", 2, BIT_7_A}, // 0x7f
{"RES 0 B", 2, RES_0_B}, // 0x80
{"RES 0 C", 2, RES_0_C}, // 0x81
{"RES 0 D", 2, RES_0_D}, // 0x82
{"RES 0 E", 2, RES_0_E}, // 0x83
{"RES 0 H", 2, RES_0_H}, // 0x84
{"RES 0 L", 2, RES_0_L}, // 0x85
{"RES 0 HLp", 2, RES_0_HLp}, // 0x86
{"RES 0 A", 2, RES_0_A}, // 0x87
{"RES 1 B", 2, RES_1_B}, // 0x88
{"RES 1 C", 2, RES_1_C}, // 0x89
{"RES 1 D", 2, RES_1_D}, // 0x8a
{"RES 1 E", 2, RES_1_E}, // 0x8b
{"RES 1 H", 2, RES_1_H}, // 0x8c
{"RES 1 L", 2, RES_1_L}, // 0x8d
{"RES 1 HLp", 2, RES_1_HLp}, // 0x8e
{"RES 1 A", 2, RES_1_A}, // 0x8f
{"RES 2 B", 2, RES_2_B}, // 0x90
{"RES 2 C", 2, RES_2_C}, // 0x91
{"RES 2 D", 2, RES_2_D}, // 0x92
{"RES 2 E", 2, RES_2_E}, // 0x93
{"RES 2 H", 2, RES_2_H}, // 0x94
{"RES 2 L", 2, RES_2_L}, // 0x95
{"RES 2 HLp", 2, RES_2_HLp}, // 0x96
{"RES 2 A", 2, RES_2_A}, // 0x97
{"RES 3 B", 2, RES_3_B}, // 0x98
{"RES 3 C", 2, RES_3_C}, // 0x99
{"RES 3 D", 2, RES_3_D}, // 0x9a
{"RES 3 E", 2, RES_3_E}, // 0x9b
{"RES 3 H", 2, RES_3_H}, // 0x9c
{"RES 3 L", 2, RES_3_L}, // 0x9d
{"RES 3 HLp", 2, RES_3_HLp}, // 0x9e
{"RES 3 A", 2, RES_3_A}, // 0x9f
{"RES 4 B", 2, RES_4_B}, // 0xa0
{"RES 4 C", 2, RES_4_C}, // 0xa1
{"RES 4 D", 2, RES_4_D}, // 0xa2
{"RES 4 E", 2, RES_4_E}, // 0xa3
{"RES 4 H", 2, RES_4_H}, // 0xa4
{"RES 4 L", 2, RES_4_L}, // 0xa5
{"RES 4 HLp", 2, RES_4_HLp}, // 0xa6
{"RES 4 A", 2, RES_4_A}, // 0xa7
{"RES 5 B", 2, RES_5_B}, // 0xa8
{"RES 5 C", 2, RES_5_C}, // 0xa9
{"RES 5 D", 2, RES_5_D}, // 0xaa
{"RES 5 E", 2, RES_5_E}, // 0xab
{"RES 5 H", 2, RES_5_H}, // 0xac
{"RES 5 L", 2, RES_5_L}, // 0xad
{"RES 5 HLp", 2, RES_5_HLp}, // 0xae
{"RES 5 A", 2, RES_5_A}, // 0xaf
{"RES 6 B", 2, RES_6_B}, // 0xb0
{"RES 6 C", 2, RES_6_C}, // 0xb1
{"RES 6 D", 2, RES_6_D}, // 0xb2
{"RES 6 E", 2, RES_6_E}, // 0xb3
{"RES 6 H", 2, RES_6_H}, // 0xb4
{"RES 6 L", 2, RES_6_L}, // 0xb5
{"RES 6 HLp", 2, RES_6_HLp}, // 0xb6
{"RES 6 A", 2, RES_6_A}, // 0xb7
{"RES 7 B", 2, RES_7_B}, // 0xb8
{"RES 7 C", 2, RES_7_C}, // 0xb9
{"RES 7 D", 2, RES_7_D}, // 0xba
{"RES 7 E", 2, RES_7_E}, // 0xbb
{"RES 7 H", 2, RES_7_H}, // 0xbc
{"RES 7 L", 2, RES_7_L}, // 0xbd
{"RES 7 HLp", 2, RES_7_HLp}, // 0xbe
{"RES 7 A", 2, RES_7_A}, // 0xbf
{"SET 0 B", 2, SET_0_B}, // 0xc0
{"SET 0 C", 2, SET_0_C}, // 0xc1
{"SET 0 D", 2, SET_0_D}, // 0xc2
{"SET 0 E", 2, SET_0_E}, // 0xc3
{"SET 0 H", 2, SET_0_H}, // 0xc4
{"SET 0 L", 2, SET_0_L}, // 0xc5
{"SET 0 HLp", 2, SET_0_HLp}, // 0xc6
{"SET 0 A", 2, SET_0_A}, // 0xc7
{"SET 1 B", 2, SET_1_B}, // 0xc8
{"SET 1 C", 2, SET_1_C}, // 0xc9
{"SET 1 D", 2, SET_1_D}, // 0xca
{"SET 1 E", 2, SET_1_E}, // 0xcb
{"SET 1 H", 2, SET_1_H}, // 0xcc
{"SET 1 L", 2, SET_1_L}, // 0xcd
{"SET 1 HLp", 2, SET_1_HLp}, // 0xce
{"SET 1 A", 2, SET_1_A}, // 0xcf
{"SET 2 B", 2, SET_2_B}, // 0xd0
{"SET 2 C", 2, SET_2_C}, // 0xd1
{"SET 2 D", 2, SET_2_D}, // 0xd2
{"SET 2 E", 2, SET_2_E}, // 0xd3
{"SET 2 H", 2, SET_2_H}, // 0xd4
{"SET 2 L", 2, SET_2_L}, // 0xd5
{"SET 2 HLp", 2, SET_2_HLp}, // 0xd6
{"SET 2 A", 2, SET_2_A}, // 0xd7
{"SET 3 B", 2, SET_3_B}, // 0xd8
{"SET 3 C", 2, SET_3_C}, // 0xd9
{"SET 3 D", 2, SET_3_D}, // 0xda
{"SET 3 E", 2, SET_3_E}, // 0xdb
{"SET 3 H", 2, SET_3_H}, // 0xdc
{"SET 3 L", 2, SET_3_L}, // 0xdd
{"SET 3 HLp", 2, SET_3_HLp}, // 0xde
{"SET 3 A", 2, SET_3_A}, // 0xdf
{"SET 4 B", 2, SET_4_B}, // 0xe0
{"SET 4 C", 2, SET_4_C}, // 0xe1
{"SET 4 D", 2, SET_4_D}, // 0xe2
{"SET 4 E", 2, SET_4_E}, // 0xe3
{"SET 4 H", 2, SET_4_H}, // 0xe4
{"SET 4 L", 2, SET_4_L}, // 0xe5
{"SET 4 HLp", 2, SET_4_HLp}, // 0xe6
{"SET 4 A", 2, SET_4_A}, // 0xe7
{"SET 5 B", 2, SET_5_B}, // 0xe8
{"SET 5 C", 2, SET_5_C}, // 0xe9
{"SET 5 D", 2, SET_5_D}, // 0xea
{"SET 5 E", 2, SET_5_E}, // 0xeb
{"SET 5 H", 2, SET_5_H}, // 0xec
{"SET 5 L", 2, SET_5_L}, // 0xed
{"SET 5 HLp", 2, SET_5_HLp}, // 0xee
{"SET 5 A", 2, SET_5_A}, // 0xef
{"SET 6 B", 2, SET_6_B}, // 0xf0
{"SET 6 C", 2, SET_6_C}, // 0xf1
{"SET 6 D", 2, SET_6_D}, // 0xf2
{"SET 6 E", 2, SET_6_E}, // 0xf3
{"SET 6 H", 2, SET_6_H}, // 0xf4
{"SET 6 L", 2, SET_6_L}, // 0xf5
{"SET 6 HLp", 2, SET_6_HLp}, // 0xf6
{"SET 6 A", 2, SET_6_A}, // 0xf7
{"SET 7 B", 2, SET_7_B}, // 0xf8
{"SET 7 C", 2, SET_7_C}, // 0xf9
{"SET 7 D", 2, SET_7_D}, // 0xfa
{"SET 7 E", 2, SET_7_E}, // 0xfb
{"SET 7 H", 2, SET_7_H}, // 0xfc
{"SET 7 L", 2, SET_7_L}, // 0xfd
{"SET 7 HLp", 2, SET_7_HLp}, // 0xfe
{"SET 7 A", 2, SET_7_A}, // 0xff
};
// Array storing the cycle length of each instruction / 2;
const uint8_t Cycles[256] = {
4, 6, 4, 4, 2, 2, 4, 4, 10, 4, 4, 4, 2, 2, 4, 4, // 0x0_
2, 6, 4, 4, 2, 2, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, // 0x1_
0, 6, 4, 4, 2, 2, 4, 2, 0, 4, 4, 4, 2, 2, 4, 2, // 0x2_
4, 6, 4, 4, 6, 6, 6, 2, 0, 4, 4, 4, 2, 2, 4, 2, // 0x3_
2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0x4_
2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0x5_
2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0x6_
4, 4, 4, 4, 4, 4, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, // 0x7_
2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0x8_
2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0x9_
2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0xa_
2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 4, 2, // 0xb_
0, 6, 0, 6, 0, 8, 4, 8, 0, 2, 0, 0, 0, 6, 4, 8, // 0xc_
0, 6, 0, 0, 0, 8, 4, 8, 0, 8, 0, 0, 0, 0, 4, 8, // 0xd_
6, 6, 4, 0, 0, 8, 4, 8, 8, 2, 8, 0, 0, 0, 4, 8, // 0xe_
6, 6, 4, 2, 0, 8, 4, 8, 6, 4, 8, 2, 0, 0, 4, 8 // 0xf_
};
int main(int argc, char** argv) {
read_rom("../../../roms/Wario Land.gb");
load_bootrom("../../../roms/DMG_BOOT.bin");
detect_banking_mode();
setup_color_pallete();
initialize_sdl();
// Main loop.
registers.pc = 0;
while (1) {
cycle_count = 0;
while (cycle_count < CYCLES_PER_FRAME) {
if (registers.pc == 0x100) {
enable_boot = false;
}
cpu_cycle();
update_timers();
increment_scan_line();
interupts();
}
// Read inputs from SDL
while (SDL_PollEvent(&event)) {
if (SDL_PollEvent(&event) && event.type == SDL_QUIT) {
print_registers();
shutdown();
break;
}
handle_input();
}
}
shutdown();
}
uint8_t read_byte(uint16_t location) {
// If in Bootrom
if (enable_boot) {
if (location < 0x100) {
return boot_rom[location];
}
}
// Base Rom Read
if (location < 0x4000) {
return rom[location];
}
// Rom Bank Read
if (location < 0x8000) {
return rom[location + bank_offset * 0x4000];
}
// Key interupt.
if (location == 0xFF00) {
return key_state();
}
return memory[location];
}
void write_byte(uint8_t data, uint16_t location) {
// Rom Bank Set
if (location >= 0x2000 && location <= 0x3FFF) {
bank_offset = data - 1;
}
// Writing to read only memory
else if (location < 0x8000) {
return;
}
// Execute DMA
else if (location == 0xFF46) {
dma_transfer(data);
}
// Reset scanline count
else if (location == 0xFF44) {
memory[0xFF44] = 0;
}
// Reset the divider register
else if (location == 0xFF04) {
memory[0xFF04] = 0;
divider_count = 0;
}
else {
memory[location] = data;
}
}
// Renders the graphics once per frame.
void render_graphics() {
SDL_Delay(15);
setup_color_pallete();
load_tiles();
render_sprites();
display_buffer();
}
void increment_scan_line() {
set_lcd_status();
if (test_bit(7, read_byte(0xFF40))) {
scanline_count -= last_cycles;
}
else {
return;
}
if (scanline_count <= 0) {
// Render tilemap line if scanline is completed.
render_tile_map_line();
memory[0xFF44]++;
scanline_count = 456;
// Check if all lines are finished and if so do a VBLANK.
if (read_byte(0xFF44) == 144)
{
render_graphics();
set_interupt(0);
}
// Reset scanline once it reaches the end.
else if (read_byte(0xFF44) > 153)
{
memory[0xFF44] = 0;
}
}
}
void set_lcd_status() {
uint8_t currentline = read_byte(0xFF44);
uint8_t current_mode = read_byte(0xFF41) & 0x3;
uint8_t new_mode = 0;
bool interupt_request = false;
uint8_t status = read_byte(0xFF41);
if (!test_bit(7, read_byte(0xFF40))) {
// set the mode to 0 during lcd disabled and reset scanline
scanline_count = 456;
memory[0xFF44] = 0;
status = Res(1, status);
status = Res(0, status);
write_byte(status, 0xFF41);
return;
}
// Check if in VBLANK (mode 1)
if (currentline >= 144) {
new_mode = 1; // Set status to 01.
status = Set(0, status);
status = Res(1, status);
interupt_request = test_bit(4, status);
}
else {
// Check if Searching OAM (mode 2)
if (scanline_count >= 376) {
new_mode = 2;
status = Set(1, status);
status = Res(0, status);
interupt_request = test_bit(5, status);
}
// Check if Transferring Data to LCD Controller (mode 3)
else if (scanline_count >= 204) {
new_mode = 3;
status = Set(1, status);
status = Set(0, status);
}
// Check if in HBLANK (mode 0)
else {
new_mode = 0;
status = Res(1, status);
status = Res(0, status);
interupt_request = test_bit(3, status);
}
}
// Interupt on changing mode
if (interupt_request && (new_mode != current_mode)) {
set_interupt(1);
}
// Handle Coincidence Interupt
if (read_byte(0xFF44) == read_byte(0xFF45)) {
status = Set(2, status);
if (test_bit(6, status)) {
set_interupt(1);
}
}
else {
status = Res(2, status);
}
write_byte(status, 0xFF41);
}
void handle_input() {
if (event.type == SDL_KEYDOWN) {
int key = -1;
switch (event.key.keysym.sym) {
case SDLK_TAB:
key = 4;
break;
case SDLK_LCTRL:
key = 5;
break;
case SDLK_RETURN:
key = 7;
break;
case SDLK_BACKSLASH:
key = 6;
break;
case SDLK_RIGHT:
key = 0;
break;
case SDLK_LEFT:
key = 1;
break;
case SDLK_UP:
key = 2;
break;
case SDLK_DOWN:
key = 3;
break;
default:
key = -1;
break;
}