-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstruction.c
More file actions
1493 lines (1263 loc) · 50.5 KB
/
instruction.c
File metadata and controls
1493 lines (1263 loc) · 50.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 "./instruction.h"
#include "types.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Loads 8-bit data to any 8-bit register
void ld_regd8_d8(EmulationState *emu, u8 *reg) {
u8 target = emu->rom[*emu->pc];
*reg = target;
*emu->pc += 1;
}
// Loads 16-bit data to any 16-bit register
void ld_regd16_d16(EmulationState *emu, u16 *reg) {
u16 target = *(u16 *)&emu->rom[*emu->pc];
*reg = target;
*emu->pc += 2;
}
// Loads 8-bit data from any the memory address pointed by any 16-bit register
void ld_regd8_rega16(EmulationState *emu, u8 *target, u16 *from) {
*target = emu->mem[*from];
}
// Loads 8-bit register data to any the memory address pointed by any 16-bit
// register
void ld_a16_d8(EmulationState *emu, u16 target, u8 from) {
emu->mem[target] = from;
// https://gbdev.io/pandocs/OAM_DMA_Transfer.html
// TODO: Update timing
if (target == DMA) {
u16 addr = from << 8;
memcpy(emu->oam, emu->mem + addr, 0xA0);
}
}
// Loads 8-bit data to any the memory address pointed by any 16-bit register
void ld_rega16_d8(EmulationState *emu, u16 *target) {
u8 from = emu->rom[*emu->pc];
emu->mem[*target] = from;
*emu->pc += 1;
}
// AND with 8-bit register
void and_regd8(EmulationState *emu, u8 reg) {
*emu->a &= reg;
set_flags(emu, *emu->a == 0, 0, 1, 0);
}
// AND with 8-bit data
void and_d8(EmulationState *emu) {
u8 target = emu->rom[*emu->pc];
*emu->pc += 1;
and_regd8(emu, target);
}
// OR with 8-bit register
void or_regd8(EmulationState *emu, u8 reg) {
*emu->a |= reg;
set_flags(emu, *emu->a == 0, 0, 0, 0);
}
// OR with 8-bit data
void or_d8(EmulationState *emu) {
u8 target = emu->rom[*emu->pc];
*emu->pc += 1;
or_regd8(emu, target);
}
// XOR with 8-bit register
void xor_regd8(EmulationState *emu, u8 reg) {
*emu->a ^= reg;
set_flags(emu, *emu->a == 0, 0, 0, 0);
}
// XOR with 8-bit data
void xor_d8(EmulationState *emu) {
u8 target = emu->rom[*emu->pc];
*emu->pc += 1;
xor_regd8(emu, target);
}
// Increment 1 from 8-bit register
void inc_regd8(EmulationState *emu, u8 *reg) {
*reg += 1;
set_flags(emu, *reg == 0, 0, (*reg & 0x0F) == 0x00, -1);
}
// Increment 1 from 8-bit register
void inc_rega16(EmulationState *emu, u16 from) {
inc_regd8(emu, &emu->mem[from]);
}
// Decrement 1 from 8-bit register
void dec_regd8(EmulationState *emu, u8 *reg) {
*reg -= 1;
set_flags(emu, *reg == 0, 1, (*reg & 0x0F) == 0x0F, -1);
}
// Decrement 1 from 16-bit address data
void dec_rega16(EmulationState *emu, u16 from) {
dec_regd8(emu, &emu->mem[from]);
}
// Add 8-bit data to A register
void add_regd8(EmulationState *emu, u8 from) {
bool h = (*emu->a & 0x0F) + (from & 0x0F) > 0x0F;
bool c = *emu->a + from > 0xFF;
*emu->a += from;
set_flags(emu, *emu->a == 0, 0, h, c);
}
void add_d8(EmulationState *emu) {
u8 target = emu->rom[*emu->pc];
*emu->pc += 1;
add_regd8(emu, target);
}
// Add 16-bit data to HL register
void add_regd16(EmulationState *emu, u16 *from) {
bool h = (*emu->hl & 0x00FF) + (*from & 0x00FF) > 0x00FF;
bool c = *emu->hl + *from > 0xFFFF;
*emu->hl += *from;
set_flags(emu, -1, 0, h, c);
}
void adc_regd8(EmulationState *emu, u8 from) {
u8 carry = ((*emu->f & C_MASK) >> C_BIT) & (1 << 0);
bool h = (*emu->a & 0x0F) + (from & 0x0F) + carry > 0x0F;
bool c = *emu->a + from + carry > 0xFF;
*emu->a += from + carry;
set_flags(emu, *emu->a == 0, 0, h, c);
}
void sub_regd8(EmulationState *emu, u8 from) {
bool h = (*emu->a & 0x0F) - (from & 0x0F) < 0;
bool c = *emu->a - from < 0;
*emu->a -= from;
set_flags(emu, *emu->a == 0, 1, h, c);
}
void sub_d8(EmulationState *emu) {
u8 target = emu->rom[*emu->pc];
*emu->pc += 1;
sub_regd8(emu, target);
}
void rotate_left_regd8(EmulationState *emu, u8 *target) {
u8 c = *target >> 7;
*target = (*target << 1) | c;
set_flags(emu, 0, 0, 0, c);
}
void rotate_right_regd8(EmulationState *emu, u8 *target) {
u8 c = *target << 7;
*target = (*target >> 1) | c;
set_flags(emu, 0, 0, 0, c);
}
void pop(EmulationState *emu, u16 *reg) {
*reg = *(u16 *)&emu->mem[*emu->sp];
*emu->sp += 2;
}
void push(EmulationState *emu, u16 *reg) {
*emu->sp -= 2;
*(u16 *)&emu->mem[*emu->sp] = *reg;
}
void cpl(EmulationState *emu) {
*emu->a ^= 0xFF;
set_flags(emu, -1, 1, 1, -1);
}
void ccf(EmulationState *emu) {
u8 set = ((*emu->f & C_MASK) >> C_BIT) ^ 1;
set_flags(emu, -1, 0, 0, set);
}
void scf(EmulationState *emu) {
set_flags(emu, -1, 0, 0, 1);
}
void nop(EmulationState *emu) {
(void)emu;
}
// https://gbdev.io/pandocs/Interrupts.html
// TODO: Delay instruction by one
void ei(EmulationState *emu) { emu->ime = true; }
void di(EmulationState *emu) { emu->ime = false; }
void cp(EmulationState *emu, u8 value) {
if (*emu->a == value) {
set_flags(emu, 1, 1, 0, 0);
} else if (*emu->a > value) {
set_flags(emu, 0, 1, 0, 0);
} else {
set_flags(emu, 0, 1, 1, 1);
}
}
void jp_a16(EmulationState *emu) {
u16 target = *(u16 *)&emu->rom[*emu->pc];
*emu->pc = target;
}
void jp_hl(EmulationState *emu) {
*emu->pc = *emu->hl;
}
void jp_z_a16(EmulationState *emu) {
if (*emu->f & Z_MASK) { // Z is set
jp_a16(emu);
} else {
*emu->pc += 2;
}
}
void jp_nz_a16(EmulationState *emu) {
if (!(*emu->f & Z_MASK)) { // Z is not set
jp_a16(emu);
} else {
*emu->pc += 2;
}
}
void jp_c_a16(EmulationState *emu) {
if (*emu->f & C_MASK) { // C is set
jp_a16(emu);
} else {
*emu->pc += 2;
}
}
void jp_nc_a16(EmulationState *emu) {
if (!(*emu->f & C_MASK)) { // C is not set
jp_a16(emu);
} else {
*emu->pc += 2;
}
}
void jr_d8(EmulationState *emu) {
i8 target = *(i8 *)&emu->rom[*emu->pc];
*emu->pc += 1;
*emu->pc += target;
}
void jr_z_d8(EmulationState *emu) {
if (*emu->f & Z_MASK) { // Z is set
jr_d8(emu);
} else {
*emu->pc += 1;
}
}
void jr_nz_d8(EmulationState *emu) {
if (!(*emu->f & Z_MASK)) { // Z is not set
jr_d8(emu);
} else {
*emu->pc += 1;
}
}
void jr_c_d8(EmulationState *emu) {
if (*emu->f & C_MASK) { // C is set
jr_d8(emu);
} else {
*emu->pc += 1;
}
}
void jr_nc_d8(EmulationState *emu) {
if (!(*emu->f & C_MASK)) { // C not is set
jr_d8(emu);
} else {
*emu->pc += 1;
}
}
void call(EmulationState *emu, u16 target) {
push(emu, emu->pc);
*emu->pc = target;
}
void call_a16(EmulationState *emu) {
u16 target = *(u16 *)&emu->rom[*emu->pc];
*emu->pc += 2;
call(emu, target);
}
void call_z_a16(EmulationState *emu) {
if (*emu->f & Z_MASK) { // Z is set
call_a16(emu);
} else {
*emu->pc += 2;
}
}
void call_nz_a16(EmulationState *emu) {
if (!(*emu->f & Z_MASK)) { // C not is set
call_a16(emu);
} else {
*emu->pc += 2;
}
}
void call_c_a16(EmulationState *emu) {
if (*emu->f & C_MASK) { // Z is set
call_a16(emu);
} else {
*emu->pc += 2;
}
}
void call_nc_a16(EmulationState *emu) {
if (!(*emu->f & C_MASK)) { // C not is set
call_a16(emu);
} else {
*emu->pc += 2;
}
}
void rst_00(EmulationState *emu) { call(emu, 0x0000); }
void rst_08(EmulationState *emu) { call(emu, 0x0008); }
void rst_10(EmulationState *emu) { call(emu, 0x0010); }
void rst_18(EmulationState *emu) { call(emu, 0x0018); }
void rst_20(EmulationState *emu) { call(emu, 0x0020); }
void rst_28(EmulationState *emu) { call(emu, 0x0028); }
void rst_30(EmulationState *emu) { call(emu, 0x0030); }
void rst_38(EmulationState *emu) { call(emu, 0x0038); }
void pop_bc(EmulationState *emu) { pop(emu, emu->bc); }
void pop_de(EmulationState *emu) { pop(emu, emu->de); }
void pop_hl(EmulationState *emu) { pop(emu, emu->hl); }
void pop_af(EmulationState *emu) { pop(emu, emu->af); }
void push_bc(EmulationState *emu) { push(emu, emu->bc); }
void push_de(EmulationState *emu) { push(emu, emu->de); }
void push_hl(EmulationState *emu) { push(emu, emu->hl); }
void push_af(EmulationState *emu) { push(emu, emu->af); }
void ret(EmulationState *emu) { pop(emu, emu->pc); }
void reti(EmulationState *emu) {
ei(emu);
pop(emu, emu->pc);
}
void ret_z(EmulationState *emu) {
if (*emu->f & Z_MASK) { // Z is set
ret(emu);
}
}
void ret_nz(EmulationState *emu) {
if (!(*emu->f & Z_MASK)) { // C not is set
ret(emu);
}
}
void ret_c(EmulationState *emu) {
if (*emu->f & C_MASK) { // Z is set
ret(emu);
}
}
void ret_nc(EmulationState *emu) {
if (!(*emu->f & C_MASK)) { // C not is set
ret(emu);
}
}
void ldh_a8_a(EmulationState *emu) {
u8 target = emu->rom[*emu->pc];
ld_a16_d8(emu, target + 0xFF00, *emu->a);
*emu->pc += 1;
}
void ldh_a_a8(EmulationState *emu) {
u8 target = emu->rom[*emu->pc];
*emu->a = emu->io[target];
*emu->pc += 1;
}
void ld_ca_a(EmulationState *emu) { emu->io[*emu->c] = *emu->a; }
void ld_a_ca(EmulationState *emu) { *emu->a = emu->io[*emu->c]; }
void inc_bc(EmulationState *emu) { *emu->bc += 1; }
void inc_de(EmulationState *emu) { *emu->de += 1; }
void inc_hl(EmulationState *emu) { *emu->hl += 1; }
void inc_sp(EmulationState *emu) { *emu->sp += 1; }
void inc_b(EmulationState *emu) { inc_regd8(emu, emu->b); }
void inc_d(EmulationState *emu) { inc_regd8(emu, emu->d); }
void inc_h(EmulationState *emu) { inc_regd8(emu, emu->h); }
void inc_ahl(EmulationState *emu) { inc_rega16(emu, *emu->hl); }
void inc_c(EmulationState *emu) { inc_regd8(emu, emu->c); }
void inc_e(EmulationState *emu) { inc_regd8(emu, emu->e); }
void inc_l(EmulationState *emu) { inc_regd8(emu, emu->l); }
void inc_a(EmulationState *emu) { inc_regd8(emu, emu->a); }
void dec_bc(EmulationState *emu) { *emu->bc -= 1; }
void dec_de(EmulationState *emu) { *emu->de -= 1; }
void dec_hl(EmulationState *emu) { *emu->hl -= 1; }
void dec_sp(EmulationState *emu) { *emu->sp -= 1; }
void dec_b(EmulationState *emu) { dec_regd8(emu, emu->b); }
void dec_d(EmulationState *emu) { dec_regd8(emu, emu->d); }
void dec_h(EmulationState *emu) { dec_regd8(emu, emu->h); }
void dec_ahl(EmulationState *emu) { dec_rega16(emu, *emu->hl); }
void dec_c(EmulationState *emu) { dec_regd8(emu, emu->c); }
void dec_e(EmulationState *emu) { dec_regd8(emu, emu->e); }
void dec_l(EmulationState *emu) { dec_regd8(emu, emu->l); }
void dec_a(EmulationState *emu) { dec_regd8(emu, emu->a); }
void ld_b_b(EmulationState *emu) { *emu->b = *emu->b; }
void ld_d_b(EmulationState *emu) { *emu->d = *emu->b; }
void ld_h_b(EmulationState *emu) { *emu->h = *emu->b; }
void ld_b_c(EmulationState *emu) { *emu->b = *emu->c; }
void ld_d_c(EmulationState *emu) { *emu->d = *emu->c; }
void ld_h_c(EmulationState *emu) { *emu->h = *emu->c; }
void ld_b_d(EmulationState *emu) { *emu->b = *emu->d; }
void ld_d_d(EmulationState *emu) { *emu->d = *emu->d; }
void ld_h_d(EmulationState *emu) { *emu->h = *emu->d; }
void ld_b_e(EmulationState *emu) { *emu->b = *emu->e; }
void ld_d_e(EmulationState *emu) { *emu->d = *emu->e; }
void ld_h_e(EmulationState *emu) { *emu->h = *emu->e; }
void ld_b_h(EmulationState *emu) { *emu->b = *emu->h; }
void ld_d_h(EmulationState *emu) { *emu->d = *emu->h; }
void ld_h_h(EmulationState *emu) { *emu->h = *emu->h; }
void ld_b_l(EmulationState *emu) { *emu->b = *emu->l; }
void ld_d_l(EmulationState *emu) { *emu->d = *emu->l; }
void ld_h_l(EmulationState *emu) { *emu->h = *emu->l; }
void ld_b_a(EmulationState *emu) { *emu->b = *emu->a; }
void ld_d_a(EmulationState *emu) { *emu->d = *emu->a; }
void ld_h_a(EmulationState *emu) { *emu->h = *emu->a; }
void ld_c_b(EmulationState *emu) { *emu->c = *emu->b; }
void ld_e_b(EmulationState *emu) { *emu->e = *emu->b; }
void ld_l_b(EmulationState *emu) { *emu->l = *emu->b; }
void ld_a_b(EmulationState *emu) { *emu->a = *emu->b; }
void ld_c_c(EmulationState *emu) { *emu->c = *emu->c; }
void ld_e_c(EmulationState *emu) { *emu->e = *emu->c; }
void ld_l_c(EmulationState *emu) { *emu->l = *emu->c; }
void ld_a_c(EmulationState *emu) { *emu->a = *emu->c; }
void ld_c_d(EmulationState *emu) { *emu->c = *emu->d; }
void ld_e_d(EmulationState *emu) { *emu->e = *emu->d; }
void ld_l_d(EmulationState *emu) { *emu->l = *emu->d; }
void ld_a_d(EmulationState *emu) { *emu->a = *emu->d; }
void ld_c_e(EmulationState *emu) { *emu->c = *emu->e; }
void ld_e_e(EmulationState *emu) { *emu->e = *emu->e; }
void ld_l_e(EmulationState *emu) { *emu->l = *emu->e; }
void ld_a_e(EmulationState *emu) { *emu->a = *emu->e; }
void ld_c_h(EmulationState *emu) { *emu->c = *emu->h; }
void ld_e_h(EmulationState *emu) { *emu->e = *emu->h; }
void ld_l_h(EmulationState *emu) { *emu->l = *emu->h; }
void ld_a_h(EmulationState *emu) { *emu->a = *emu->h; }
void ld_c_l(EmulationState *emu) { *emu->c = *emu->l; }
void ld_e_l(EmulationState *emu) { *emu->e = *emu->l; }
void ld_l_l(EmulationState *emu) { *emu->l = *emu->l; }
void ld_a_l(EmulationState *emu) { *emu->a = *emu->l; }
void ld_c_a(EmulationState *emu) { *emu->c = *emu->a; }
void ld_e_a(EmulationState *emu) { *emu->e = *emu->a; }
void ld_l_a(EmulationState *emu) { *emu->l = *emu->a; }
void ld_a_a(EmulationState *emu) { *emu->a = *emu->a; }
void ld_a16_a(EmulationState *emu) {
u16 target_addr = *(u16 *)&emu->rom[*emu->pc];
ld_a16_d8(emu, target_addr, *emu->a);
*emu->pc += 2;
}
void ld_a_a16(EmulationState *emu) {
u16 target_addr = *(u16 *)&emu->rom[*emu->pc];
*emu->a = emu->mem[target_addr];
*emu->pc += 2;
}
void ld_b_d8(EmulationState *emu) { ld_regd8_d8(emu, emu->b); }
void ld_d_d8(EmulationState *emu) { ld_regd8_d8(emu, emu->d); }
void ld_h_d8(EmulationState *emu) { ld_regd8_d8(emu, emu->h); }
void ld_hl_d8(EmulationState *emu) { ld_rega16_d8(emu, emu->hl); }
void ld_c_d8(EmulationState *emu) { ld_regd8_d8(emu, emu->c); }
void ld_e_d8(EmulationState *emu) { ld_regd8_d8(emu, emu->e); }
void ld_l_d8(EmulationState *emu) { ld_regd8_d8(emu, emu->l); }
void ld_a_d8(EmulationState *emu) { ld_regd8_d8(emu, emu->a); }
void ld_bc_d16(EmulationState *emu) { ld_regd16_d16(emu, emu->bc); }
void ld_de_d16(EmulationState *emu) { ld_regd16_d16(emu, emu->de); }
void ld_hl_d16(EmulationState *emu) { ld_regd16_d16(emu, emu->hl); }
void ld_sp_d16(EmulationState *emu) { ld_regd16_d16(emu, emu->sp); }
void ld_a_bc(EmulationState *emu) { ld_regd8_rega16(emu, emu->a, emu->bc); }
void ld_a_de(EmulationState *emu) { ld_regd8_rega16(emu, emu->a, emu->de); }
void ld_a_hli(EmulationState *emu) {
ld_regd8_rega16(emu, emu->a, emu->hl);
inc_hl(emu);
}
void ld_a_hld(EmulationState *emu) {
ld_regd8_rega16(emu, emu->a, emu->hl);
dec_hl(emu);
}
void ld_b_hl(EmulationState *emu) { ld_regd8_rega16(emu, emu->b, emu->hl); }
void ld_d_hl(EmulationState *emu) { ld_regd8_rega16(emu, emu->d, emu->hl); }
void ld_h_hl(EmulationState *emu) { ld_regd8_rega16(emu, emu->h, emu->hl); }
void ld_c_hl(EmulationState *emu) { ld_regd8_rega16(emu, emu->c, emu->hl); }
void ld_e_hl(EmulationState *emu) { ld_regd8_rega16(emu, emu->e, emu->hl); }
void ld_l_hl(EmulationState *emu) { ld_regd8_rega16(emu, emu->l, emu->hl); }
void ld_a_hl(EmulationState *emu) { ld_regd8_rega16(emu, emu->a, emu->hl); }
void ld_bc_a(EmulationState *emu) { ld_a16_d8(emu, *emu->bc, *emu->a); }
void ld_de_a(EmulationState *emu) { ld_a16_d8(emu, *emu->de, *emu->a); }
void ld_hli_a(EmulationState *emu) {
ld_a16_d8(emu, *emu->hl, *emu->a);
inc_hl(emu);
}
void ld_hld_a(EmulationState *emu) {
ld_a16_d8(emu, *emu->hl, *emu->a);
dec_hl(emu);
}
void ld_hl_b(EmulationState *emu) { ld_a16_d8(emu, *emu->hl, *emu->b); }
void ld_hl_c(EmulationState *emu) { ld_a16_d8(emu, *emu->hl, *emu->c); }
void ld_hl_d(EmulationState *emu) { ld_a16_d8(emu, *emu->hl, *emu->d); }
void ld_hl_e(EmulationState *emu) { ld_a16_d8(emu, *emu->hl, *emu->e); }
void ld_hl_h(EmulationState *emu) { ld_a16_d8(emu, *emu->hl, *emu->h); }
void ld_hl_l(EmulationState *emu) { ld_a16_d8(emu, *emu->hl, *emu->l); }
void ld_hl_a(EmulationState *emu) { ld_a16_d8(emu, *emu->hl, *emu->a); }
void add_b(EmulationState *emu) { add_regd8(emu, *emu->b); }
void add_c(EmulationState *emu) { add_regd8(emu, *emu->c); }
void add_d(EmulationState *emu) { add_regd8(emu, *emu->d); }
void add_e(EmulationState *emu) { add_regd8(emu, *emu->e); }
void add_h(EmulationState *emu) { add_regd8(emu, *emu->h); }
void add_l(EmulationState *emu) { add_regd8(emu, *emu->l); }
void add_ahl(EmulationState *emu) { add_regd8(emu, emu->mem[*emu->hl]); }
void add_a(EmulationState *emu) { add_regd8(emu, *emu->a); }
void add_bc(EmulationState *emu) { add_regd16(emu, emu->bc); }
void add_de(EmulationState *emu) { add_regd16(emu, emu->de); }
void add_hl(EmulationState *emu) { add_regd16(emu, emu->hl); }
void add_sp(EmulationState *emu) { add_regd16(emu, emu->sp); }
void adc_b(EmulationState *emu) { adc_regd8(emu, *emu->b); }
void adc_c(EmulationState *emu) { adc_regd8(emu, *emu->c); }
void adc_d(EmulationState *emu) { adc_regd8(emu, *emu->d); }
void adc_e(EmulationState *emu) { adc_regd8(emu, *emu->e); }
void adc_h(EmulationState *emu) { adc_regd8(emu, *emu->h); }
void adc_l(EmulationState *emu) { adc_regd8(emu, *emu->l); }
void adc_ahl(EmulationState *emu) { adc_regd8(emu, emu->mem[*emu->hl]); }
void adc_a(EmulationState *emu) { adc_regd8(emu, *emu->a); }
void sub_b(EmulationState *emu) { sub_regd8(emu, *emu->b); }
void sub_c(EmulationState *emu) { sub_regd8(emu, *emu->c); }
void sub_d(EmulationState *emu) { sub_regd8(emu, *emu->d); }
void sub_e(EmulationState *emu) { sub_regd8(emu, *emu->e); }
void sub_h(EmulationState *emu) { sub_regd8(emu, *emu->h); }
void sub_l(EmulationState *emu) { sub_regd8(emu, *emu->l); }
void sub_ahl(EmulationState *emu) { sub_regd8(emu, emu->mem[*emu->hl]); }
void sub_a(EmulationState *emu) { sub_regd8(emu, *emu->a); }
void and_b(EmulationState *emu) { and_regd8(emu, *emu->b); }
void and_c(EmulationState *emu) { and_regd8(emu, *emu->c); }
void and_d(EmulationState *emu) { and_regd8(emu, *emu->d); }
void and_e(EmulationState *emu) { and_regd8(emu, *emu->e); }
void and_h(EmulationState *emu) { and_regd8(emu, *emu->h); }
void and_l(EmulationState *emu) { and_regd8(emu, *emu->l); }
void and_ahl(EmulationState *emu) { and_regd8(emu, emu->mem[*emu->hl]); }
void and_a(EmulationState *emu) { and_regd8(emu, *emu->a); }
void or_b(EmulationState *emu) { or_regd8(emu, *emu->b); }
void or_c(EmulationState *emu) { or_regd8(emu, *emu->c); }
void or_d(EmulationState *emu) { or_regd8(emu, *emu->d); }
void or_e(EmulationState *emu) { or_regd8(emu, *emu->e); }
void or_h(EmulationState *emu) { or_regd8(emu, *emu->h); }
void or_l(EmulationState *emu) { or_regd8(emu, *emu->l); }
void or_ahl(EmulationState *emu) { or_regd8(emu, emu->mem[*emu->hl]); }
void or_a(EmulationState *emu) { or_regd8(emu, *emu->a); }
void xor_b(EmulationState *emu) { xor_regd8(emu, *emu->b); }
void xor_c(EmulationState *emu) { xor_regd8(emu, *emu->c); }
void xor_d(EmulationState *emu) { xor_regd8(emu, *emu->d); }
void xor_e(EmulationState *emu) { xor_regd8(emu, *emu->e); }
void xor_h(EmulationState *emu) { xor_regd8(emu, *emu->h); }
void xor_l(EmulationState *emu) { xor_regd8(emu, *emu->l); }
void xor_ahl(EmulationState *emu) { xor_regd8(emu, emu->mem[*emu->hl]); }
void xor_a(EmulationState *emu) { xor_regd8(emu, *emu->a); }
void cp_b(EmulationState *emu) { cp(emu, *emu->b); }
void cp_c(EmulationState *emu) { cp(emu, *emu->c); }
void cp_d(EmulationState *emu) { cp(emu, *emu->d); }
void cp_e(EmulationState *emu) { cp(emu, *emu->e); }
void cp_h(EmulationState *emu) { cp(emu, *emu->h); }
void cp_l(EmulationState *emu) { cp(emu, *emu->l); }
void cp_ahl(EmulationState *emu) { cp(emu, emu->mem[*emu->hl]); }
void cp_a(EmulationState *emu) { cp(emu, *emu->a); }
void cp_d8(EmulationState *emu) {
u8 value = emu->rom[*emu->pc];
cp(emu, value);
*emu->pc += 1;
}
void rlca(EmulationState *emu) { rotate_left_regd8(emu, emu->a); }
void rrca(EmulationState *emu) { rotate_right_regd8(emu, emu->a); }
void prefix(EmulationState *emu) {
u8 inst = emu->rom[*emu->pc];
*emu->pc += 1;
Instruction instruction = GB_INSTRUCTIONS_PREFIXED[inst];
printf("Prefixed Inst: %02X\n", inst);
instruction.execute(emu);
}
Instruction GB_INSTRUCTIONS[256] = {
[0x00] = {.execute = &nop},
[0xF3] = {.execute = &di},
[0xFB] = {.execute = &ei},
[0xCB] = {.execute = &prefix},
[0x01] = {.execute = &ld_bc_d16},
[0x11] = {.execute = &ld_de_d16},
[0x21] = {.execute = &ld_hl_d16},
[0x31] = {.execute = &ld_sp_d16},
[0x40] = {.execute = &ld_b_b},
[0x50] = {.execute = &ld_d_b},
[0x60] = {.execute = &ld_h_b},
[0x41] = {.execute = &ld_b_c},
[0x51] = {.execute = &ld_d_c},
[0x61] = {.execute = &ld_h_c},
[0x42] = {.execute = &ld_b_d},
[0x52] = {.execute = &ld_d_d},
[0x62] = {.execute = &ld_h_d},
[0x43] = {.execute = &ld_b_e},
[0x53] = {.execute = &ld_d_e},
[0x63] = {.execute = &ld_h_e},
[0x44] = {.execute = &ld_b_h},
[0x54] = {.execute = &ld_d_h},
[0x64] = {.execute = &ld_h_h},
[0x45] = {.execute = &ld_b_l},
[0x55] = {.execute = &ld_d_l},
[0x65] = {.execute = &ld_h_l},
[0x46] = {.execute = &ld_b_hl},
[0x56] = {.execute = &ld_d_hl},
[0x66] = {.execute = &ld_h_hl},
[0x47] = {.execute = &ld_b_a},
[0x57] = {.execute = &ld_d_a},
[0x67] = {.execute = &ld_h_a},
[0x48] = {.execute = &ld_c_b},
[0x58] = {.execute = &ld_e_b},
[0x68] = {.execute = &ld_l_b},
[0x78] = {.execute = &ld_a_b},
[0x49] = {.execute = &ld_c_c},
[0x59] = {.execute = &ld_e_c},
[0x69] = {.execute = &ld_l_c},
[0x79] = {.execute = &ld_a_c},
[0x4A] = {.execute = &ld_c_d},
[0x5A] = {.execute = &ld_e_d},
[0x6A] = {.execute = &ld_l_d},
[0x7A] = {.execute = &ld_a_d},
[0x4B] = {.execute = &ld_c_e},
[0x5B] = {.execute = &ld_e_e},
[0x6B] = {.execute = &ld_l_e},
[0x7B] = {.execute = &ld_a_e},
[0x4C] = {.execute = &ld_c_h},
[0x5C] = {.execute = &ld_e_h},
[0x6C] = {.execute = &ld_l_h},
[0x7C] = {.execute = &ld_a_h},
[0x4D] = {.execute = &ld_c_l},
[0x5D] = {.execute = &ld_e_l},
[0x6D] = {.execute = &ld_l_l},
[0x7D] = {.execute = &ld_a_l},
[0x4E] = {.execute = &ld_c_hl},
[0x5E] = {.execute = &ld_e_hl},
[0x6E] = {.execute = &ld_l_hl},
[0x7E] = {.execute = &ld_a_hl},
[0x4F] = {.execute = &ld_c_a},
[0x5F] = {.execute = &ld_e_a},
[0x6F] = {.execute = &ld_l_a},
[0x7F] = {.execute = &ld_a_a},
[0x02] = {.execute = &ld_bc_a},
[0x12] = {.execute = &ld_de_a},
[0x22] = {.execute = &ld_hli_a},
[0x32] = {.execute = &ld_hld_a},
[0xE2] = {.execute = &ld_ca_a},
[0xF2] = {.execute = &ld_a_ca},
[0x70] = {.execute = &ld_hl_b},
[0x71] = {.execute = &ld_hl_c},
[0x72] = {.execute = &ld_hl_d},
[0x73] = {.execute = &ld_hl_e},
[0x74] = {.execute = &ld_hl_h},
[0x75] = {.execute = &ld_hl_l},
[0x77] = {.execute = &ld_hl_a},
[0x03] = {.execute = &inc_bc},
[0x13] = {.execute = &inc_de},
[0x23] = {.execute = &inc_hl},
[0x33] = {.execute = &inc_sp},
[0x04] = {.execute = &inc_b},
[0x14] = {.execute = &inc_d},
[0x24] = {.execute = &inc_h},
[0x34] = {.execute = &inc_ahl},
[0x0C] = {.execute = &inc_c},
[0x1C] = {.execute = &inc_e},
[0x2C] = {.execute = &inc_l},
[0x3C] = {.execute = &inc_a},
[0x05] = {.execute = &dec_b},
[0x15] = {.execute = &dec_d},
[0x25] = {.execute = &dec_h},
[0x35] = {.execute = &dec_ahl},
[0x0D] = {.execute = &dec_c},
[0x1D] = {.execute = &dec_e},
[0x2D] = {.execute = &dec_l},
[0x3D] = {.execute = &dec_a},
[0x0B] = {.execute = &dec_bc},
[0x1B] = {.execute = &dec_de},
[0x2B] = {.execute = &dec_hl},
[0x3B] = {.execute = &dec_sp},
[0x0A] = {.execute = &ld_a_bc},
[0x1A] = {.execute = &ld_a_de},
[0x2A] = {.execute = &ld_a_hli},
[0x3A] = {.execute = &ld_a_hld},
[0x06] = {.execute = &ld_b_d8},
[0x16] = {.execute = &ld_d_d8},
[0x26] = {.execute = &ld_h_d8},
[0x36] = {.execute = &ld_hl_d8},
[0x0E] = {.execute = &ld_c_d8},
[0x1E] = {.execute = &ld_e_d8},
[0x2E] = {.execute = &ld_l_d8},
[0x3E] = {.execute = &ld_a_d8},
[0xEA] = {.execute = &ld_a16_a},
[0xFA] = {.execute = &ld_a_a16},
[0xE0] = {.execute = &ldh_a8_a},
[0xF0] = {.execute = &ldh_a_a8},
[0x80] = {.execute = &add_b},
[0x81] = {.execute = &add_c},
[0x82] = {.execute = &add_d},
[0x83] = {.execute = &add_e},
[0x84] = {.execute = &add_h},
[0x85] = {.execute = &add_l},
[0x86] = {.execute = &add_ahl},
[0x87] = {.execute = &add_a},
[0x09] = {.execute = &add_bc},
[0x19] = {.execute = &add_de},
[0x29] = {.execute = &add_hl},
[0x39] = {.execute = &add_sp},
[0x88] = {.execute = &adc_b},
[0x89] = {.execute = &adc_c},
[0x8A] = {.execute = &adc_d},
[0x8B] = {.execute = &adc_e},
[0x8C] = {.execute = &adc_h},
[0x8D] = {.execute = &adc_l},
[0x8E] = {.execute = &adc_ahl},
[0x8F] = {.execute = &adc_a},
[0x90] = {.execute = &sub_b},
[0x91] = {.execute = &sub_c},
[0x92] = {.execute = &sub_d},
[0x93] = {.execute = &sub_e},
[0x94] = {.execute = &sub_h},
[0x95] = {.execute = &sub_l},
[0x96] = {.execute = &sub_ahl},
[0x97] = {.execute = &sub_a},
[0xA0] = {.execute = &and_b},
[0xA1] = {.execute = &and_c},
[0xA2] = {.execute = &and_d},
[0xA3] = {.execute = &and_e},
[0xA4] = {.execute = &and_h},
[0xA5] = {.execute = &and_l},
[0xA6] = {.execute = &and_ahl},
[0xA7] = {.execute = &and_a},
[0xA8] = {.execute = &xor_b},
[0xA9] = {.execute = &xor_c},
[0xAA] = {.execute = &xor_d},
[0xAB] = {.execute = &xor_e},
[0xAC] = {.execute = &xor_h},
[0xAD] = {.execute = &xor_l},
[0xAE] = {.execute = &xor_ahl},
[0xAF] = {.execute = &xor_a},
[0xEE] = {.execute = &xor_d8},
[0xB0] = {.execute = &or_b},
[0xB1] = {.execute = &or_c},
[0xB2] = {.execute = &or_d},
[0xB3] = {.execute = &or_e},
[0xB4] = {.execute = &or_h},
[0xB5] = {.execute = &or_l},
[0xB7] = {.execute = &or_a},
[0xC6] = {.execute = &add_d8},
[0xD6] = {.execute = &sub_d8},
[0xE6] = {.execute = &and_d8},
[0xF6] = {.execute = &or_d8},
[0x07] = {.execute = &rlca},
[0x0F] = {.execute = &rrca},
[0x2F] = {.execute = &cpl},
[0x37] = {.execute = &scf},
[0x3F] = {.execute = &ccf},
[0xB8] = {.execute = &cp_b},
[0xB9] = {.execute = &cp_c},
[0xBA] = {.execute = &cp_d},
[0xBB] = {.execute = &cp_e},
[0xBC] = {.execute = &cp_h},
[0xBD] = {.execute = &cp_l},
[0xBE] = {.execute = &cp_ahl},
[0xBF] = {.execute = &cp_a},
[0xFE] = {.execute = &cp_d8},
[0xC3] = {.execute = &jp_a16},
[0xE9] = {.execute = &jp_hl},
[0xC2] = {.execute = &jp_nz_a16},
[0xD2] = {.execute = &jp_nc_a16},
[0xCA] = {.execute = &jp_z_a16},
[0xDA] = {.execute = &jp_c_a16},
[0xCD] = {.execute = &call_a16},
[0xC4] = {.execute = &call_nz_a16},
[0xD4] = {.execute = &call_nc_a16},
[0xCC] = {.execute = &call_z_a16},
[0xDC] = {.execute = &call_c_a16},
[0xC7] = {.execute = &rst_00},
[0xD7] = {.execute = &rst_10},
[0xE7] = {.execute = &rst_20},
[0xF7] = {.execute = &rst_30},
[0xCF] = {.execute = &rst_08},
[0xDF] = {.execute = &rst_18},
[0xEF] = {.execute = &rst_28},
[0xFF] = {.execute = &rst_38},
[0xC1] = {.execute = &pop_bc},
[0xD1] = {.execute = &pop_de},
[0xE1] = {.execute = &pop_hl},
[0xF1] = {.execute = &pop_af},
[0xC5] = {.execute = &push_bc},
[0xD5] = {.execute = &push_de},
[0xE5] = {.execute = &push_hl},
[0xF5] = {.execute = &push_af},
[0xC9] = {.execute = &ret},
[0xD9] = {.execute = &reti},
[0xC0] = {.execute = &ret_nz},
[0xD0] = {.execute = &ret_nc},
[0xC8] = {.execute = &ret_z},
[0xD8] = {.execute = &ret_c},
[0x20] = {.execute = &jr_nz_d8},
[0x30] = {.execute = &jr_nc_d8},
[0x18] = {.execute = &jr_d8},
[0x28] = {.execute = &jr_z_d8},
[0x38] = {.execute = &jr_c_d8},
};
void sla_regd8(EmulationState *emu, u8 *reg) {
u8 c = *reg & (1 << 7);
*reg <<= 1;
set_flags(emu, *reg == 0, 0, 0, c);
}
void sla_rega16(EmulationState *emu, u16 addr) {
sla_regd8(emu, &emu->mem[addr]);
}
void sra_regd8(EmulationState *emu, u8 *reg) {
u8 c = *reg & (1 << 0);
u8 left = *reg & (1 << 7);
*reg >>= 1;
*reg |= left;
set_flags(emu, *reg == 0, 0, 0, c);
}
void sra_rega16(EmulationState *emu, u16 addr) {
sra_regd8(emu, &emu->mem[addr]);
}
void swap_regd8(EmulationState *emu, u8 *reg) {
*reg = (*reg << 4) | (*reg >> 4);
set_flags(emu, *reg == 0, 0, 0, 0);
}
void swap_rega16(EmulationState *emu, u16 addr) {
swap_regd8(emu, &emu->mem[addr]);
}
void srl_regd8(EmulationState *emu, u8 *reg) {
u8 c = *reg & (1 << 0);
*reg >>= 1;
set_flags(emu, *reg == 0, 0, 0, c);
}
void srl_rega16(EmulationState *emu, u16 addr) {
srl_regd8(emu, &emu->mem[addr]);
}
void bit_regd8(EmulationState *emu, u8 *reg, u8 bit) {
set_flags(emu, (*reg & (1 << bit)) == 0, 0, 1, -1);
}
void bit_rega16(EmulationState *emu, u16 addr, u8 bit) {
bit_regd8(emu, &emu->mem[addr], bit);
}
void res_regd8(EmulationState *emu, u8 *reg, u8 bit) {
(void)emu;
*reg &= ~(1 << bit);
}
void res_rega16(EmulationState *emu, u16 addr, u8 bit) {
res_regd8(emu, &emu->mem[addr], bit);
}
void set_regd8(EmulationState *emu, u8 *reg, u8 bit) {
(void)emu;
*reg |= (1 << bit);
}
void set_rega16(EmulationState *emu, u16 addr, u8 bit) {
set_regd8(emu, &emu->mem[addr], bit);
}
void sla_b(EmulationState *emu) { sla_regd8(emu, emu->b); }
void sla_c(EmulationState *emu) { sla_regd8(emu, emu->c); }
void sla_d(EmulationState *emu) { sla_regd8(emu, emu->d); }
void sla_e(EmulationState *emu) { sla_regd8(emu, emu->e); }
void sla_h(EmulationState *emu) { sla_regd8(emu, emu->h); }
void sla_l(EmulationState *emu) { sla_regd8(emu, emu->h); }
void sla_hl(EmulationState *emu) { sla_rega16(emu, *emu->hl); }
void sla_a(EmulationState *emu) { sla_regd8(emu, emu->a); }
void sra_b(EmulationState *emu) { sra_regd8(emu, emu->b); }
void sra_c(EmulationState *emu) { sra_regd8(emu, emu->c); }
void sra_d(EmulationState *emu) { sra_regd8(emu, emu->d); }