-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpu.cpp
More file actions
2922 lines (2513 loc) · 69.4 KB
/
Copy pathcpu.cpp
File metadata and controls
2922 lines (2513 loc) · 69.4 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
// Implementation of OPCODES and cpu step function
#include <stdexcept>
#include <assert.h>
#include "cpu.h"
#include "mem.h"
// cpu registers, halted state, and timers
CpuState globalState;
typedef void OpcodeHandler(u8 opcode);
// the GB has several invalid opcodes
void invHandler(u8 opcode) {
printf("got invalid opcode 0x%x\n", opcode);
assert(false);
}
////////////////////////////////
// CB Opcode Helper Functions //
////////////////////////////////
u8 rlcReg(u8 value, bool isA) {
u8 result = value;
clearAllFlags();
if((result & 0x80) != 0) {
setCarryFlag();
result <<= 1;
result |= 0x1;
} else {
result <<= 1;
}
if(!isA) {
if((u8)result == 0) {
setZeroFlag();
}
}
return result;
}
u8 rlReg(u8 value, bool isA) {
u8 carry = getCarryFlag() ? (u8)1 : (u8)0;
u8 result = value;
clearAllFlags();
if((result & 0x80) != 0) {
setCarryFlag();
}
result <<= 1;
result |= carry;
if(!isA) {
if((u8)result == 0) {
setZeroFlag();
}
}
return result;
}
u8 rrc(u8 value, bool isA) {
u8 result = value;
clearAllFlags();
if((result & 1) != 0) {
setCarryFlag();
result >>= 1;
result |= 0x80;
} else {
result >>= 1;
}
if(!isA) {
if((u8)result == 0) {
setZeroFlag();
}
}
return result;
}
u8 rr(u8 value, bool isA) {
u8 carry = getCarryFlag() ? (u8)0x80 : (u8)0x00;
u8 result = value;
clearAllFlags();
if((result & 1) != 0) {
setCarryFlag();
}
result >>= 1;
result |= carry;
if(!isA) {
if((u8)result == 0) {
setZeroFlag();
}
}
return result;
}
u8 srl(u8 value) {
u8 result = value;
clearAllFlags();
if(result & 1) {
setCarryFlag();
}
result >>= 1;
if(result == 0) {
setZeroFlag();
}
return result;
}
u8 sla(u8 value) {
clearAllFlags();
if(value & 0x80) {
setCarryFlag();
}
u8 result = value << 1;
if(result == 0) {
setZeroFlag();
}
return result;
}
u8 sra(u8 value) {
u8 result = value;
clearAllFlags();
if(result & 1) {
setCarryFlag();
}
if((result & 0x80)) {
result >>= 1;
result |= 0x80;
} else {
result >>= 1;
}
if(result == 0) {
setZeroFlag();
}
return result;
}
u8 swapRegister(u8 value) {
u8 low = value & 0xf;
u8 hi = (value >> 4) & 0xf;
u8 result = (low << 4) + hi;
clearAllFlags();
if((u8)result == 0) {
setZeroFlag();
}
return result;
}
////////////////////////////////
// CB Opcodes //
////////////////////////////////
void SLA_A(u8 opcode) { // 0x27
globalState.pc++;
globalState.cycleCount += 8;
globalState.a = sla(globalState.a);
}
void SLA_B(u8 opcode) { // 0x20
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.hi = sla(globalState.bc.hi);
}
void SLA_C(u8 opcode) { // 0x21
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.lo = sla(globalState.bc.lo);
}
void SLA_D(u8 opcode) { // 0x22
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.hi = sla(globalState.de.hi);
}
void SLA_E(u8 opcode) { // 0x23
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.lo = sla(globalState.de.lo);
}
void SLA_H(u8 opcode) { // 0x24
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.hi = sla(globalState.hl.hi);
}
void SLA_L(u8 opcode) { // 0x25
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.lo = sla(globalState.hl.lo);
}
void SLA_DHL(u8 opcode) { // 0x26
globalState.pc++;
globalState.cycleCount += 16;
writeByte(sla(readByte(globalState.hl.v)), globalState.hl.v);
}
void SRA_A(u8 opcode) { // 0x2f
globalState.pc++;
globalState.cycleCount += 8;
globalState.a = sra(globalState.a);
}
void SRA_B(u8 opcode) { // 0x28
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.hi = sra(globalState.bc.hi);
}
void SRA_C(u8 opcode) { // 0x29
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.lo = sra(globalState.bc.lo);
}
void SRA_D(u8 opcode) { // 0x2a
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.hi = sra(globalState.de.hi);
}
void SRA_E(u8 opcode) { // 0x2b
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.lo = sra(globalState.de.lo);
}
void SRA_H(u8 opcode) { // 0x2c
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.hi = sra(globalState.hl.hi);
}
void SRA_L(u8 opcode) { // 0x2d
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.lo = sra(globalState.hl.lo);
}
void SRA_DHL(u8 opcode) { // 0x2e
globalState.pc++;
globalState.cycleCount += 16;
writeByte(sra(readByte(globalState.hl.v)), globalState.hl.v);
}
void SRL_A(u8 opcode) { // 0x3f
globalState.pc++;
globalState.cycleCount += 8;
globalState.a = srl(globalState.a);
}
void SRL_B(u8 opcode) { // 0x38
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.hi = srl(globalState.bc.hi);
}
void SRL_C(u8 opcode) { // 0x39
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.lo = srl(globalState.bc.lo);
}
void SRL_D(u8 opcode) { // 0x3a
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.hi = srl(globalState.de.hi);
}
void SRL_E(u8 opcode) { // 0x3b
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.lo = srl(globalState.de.lo);
}
void SRL_H(u8 opcode) { // 0x3c
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.hi = srl(globalState.hl.hi);
}
void SRL_L(u8 opcode) { // 0x3d
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.lo = srl(globalState.hl.lo);
}
void SRL_DHL(u8 opcode) { // 0x3e
globalState.pc++;
globalState.cycleCount += 16;
writeByte(srl(readByte(globalState.hl.v)), globalState.hl.v);
}
void RR_A(u8 opcode) { // 0x1f
globalState.pc++;
globalState.cycleCount += 8;
globalState.a = rr(globalState.a, false);
}
void RR_B(u8 opcode) { // 0x18
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.hi = rr(globalState.bc.hi, false);
}
void RR_C(u8 opcode) { // 0x19
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.lo = rr(globalState.bc.lo, false);
}
void RR_D(u8 opcode) { // 0x1a
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.hi = rr(globalState.de.hi, false);
}
void RR_E(u8 opcode) { // 0x1b
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.lo = rr(globalState.de.lo, false);
}
void RR_H(u8 opcode) { // 0x1c
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.hi = rr(globalState.hl.hi, false);
}
void RR_L(u8 opcode) { // 0x1d
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.lo = rr(globalState.hl.lo, false);
}
void RR_DHL(u8 opcode) { // 0x1e
globalState.pc++;
globalState.cycleCount += 16;
writeByte(rr(readByte(globalState.hl.v), false), globalState.hl.v);
}
void RL_A(u8 opcode) { // 0x17
globalState.pc++;
globalState.cycleCount += 8;
globalState.a = rlReg(globalState.a, false);
}
void RL_B(u8 opcode) { // 0x10
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.hi = rlReg(globalState.bc.hi, false);
}
void RL_C(u8 opcode) { // 0x11
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.lo = rlReg(globalState.bc.lo, false);
}
void RL_D(u8 opcode) { // 0x12
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.hi = rlReg(globalState.de.hi, false);
}
void RL_E(u8 opcode) { // 0x13
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.lo = rlReg(globalState.de.lo, false);
}
void RL_H(u8 opcode) { // 0x14
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.hi = rlReg(globalState.hl.hi, false);
}
void RL_L(u8 opcode) { // 0x15
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.lo = rlReg(globalState.hl.lo, false);
}
void RL_DHL(u8 opcode) { // 0x16
globalState.pc++;
globalState.cycleCount += 16;
writeByte(rlReg(readByte(globalState.hl.v), false), globalState.hl.v);
}
void SWAP_A(u8 opcode) { // 37
globalState.pc++;
globalState.cycleCount += 8;
globalState.a = swapRegister(globalState.a);
}
void SWAP_B(u8 opcode) { // 30
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.hi = swapRegister(globalState.bc.hi);
}
void SWAP_C(u8 opcode) { // 31
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.lo = swapRegister(globalState.bc.lo);
}
void SWAP_D(u8 opcode) { // 32
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.hi = swapRegister(globalState.de.hi);
}
void SWAP_E(u8 opcode) { // 33
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.lo = swapRegister(globalState.de.lo);
}
void SWAP_H(u8 opcode) { // 34
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.hi = swapRegister(globalState.hl.hi);
}
void SWAP_L(u8 opcode) { // 35
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.lo = swapRegister(globalState.hl.lo);
}
void SWAP_DHL(u8 opcode) { // 36
globalState.pc++;
globalState.cycleCount += 16;
writeByte(swapRegister(readByte(globalState.hl.v)), globalState.hl.v);
}
void RLC_A(u8 opcode) { // 07
globalState.pc++;
globalState.cycleCount += 8;
globalState.a = rlcReg(globalState.a, false);
}
void RLC_B(u8 opcode) { // 00
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.hi = rlcReg(globalState.bc.hi, false);
}
void RLC_C(u8 opcode) { // 01
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.lo = rlcReg(globalState.bc.lo, false);
}
void RLC_D(u8 opcode) { // 02
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.hi = rlcReg(globalState.de.hi, false);
}
void RLC_E(u8 opcode) { // 03
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.lo = rlcReg(globalState.de.lo, false);
}
void RLC_H(u8 opcode) { // 04
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.hi = rlcReg(globalState.hl.hi, false);
}
void RLC_L(u8 opcode) { // 05
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.lo = rlcReg(globalState.hl.lo, false);
}
void RLC_DHL(u8 opcode) { // 06
globalState.pc++;
globalState.cycleCount += 16;
u8 result = rlcReg(readByte(globalState.hl.v), false);
writeByte(result, globalState.hl.v);
}
void RRC_A(u8 opcode) { // 0f
globalState.pc++;
globalState.cycleCount += 8;
globalState.a = rrc(globalState.a, false);
}
void RRC_B(u8 opcode) { // 08
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.hi = rrc(globalState.bc.hi, false);
}
void RRC_C(u8 opcode) { // 09
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.lo = rrc(globalState.bc.lo, false);
}
void RRC_D(u8 opcode) { // 0a
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.hi = rrc(globalState.de.hi, false);
}
void RRC_E(u8 opcode) { // 0b
globalState.pc++;
globalState.cycleCount += 8;
globalState.de.lo = rrc(globalState.de.lo, false);
}
void RRC_H(u8 opcode) { // 0c
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.hi = rrc(globalState.hl.hi, false);
}
void RRC_L(u8 opcode) { // 0d
globalState.pc++;
globalState.cycleCount += 8;
globalState.hl.lo = rrc(globalState.hl.lo, false);
}
void RRC_DHL(u8 opcode) { // 0e
globalState.pc++;
globalState.cycleCount += 16;
u8 result = rrc(readByte(globalState.hl.v), false);
writeByte(result, globalState.hl.v);
}
void bit_B_set(u8 opcode) {
u8 bitID = (opcode - (u8)0xC0) >> 3;
assert(bitID < 8);
globalState.bc.hi |= (1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_C_set(u8 opcode) {
u8 bitID = (opcode - (u8)0xC1) >> 3;
assert(bitID < 8);
globalState.bc.lo |= (1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_D_set(u8 opcode) {
u8 bitID = (opcode - (u8)0xC2) >> 3;
assert(bitID < 8);
globalState.de.hi |= (1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_E_set(u8 opcode) {
u8 bitID = (opcode - (u8)0xC3) >> 3;
assert(bitID < 8);
globalState.de.lo |= (1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_H_set(u8 opcode) {
u8 bitID = (opcode - (u8)0xC4) >> 3;
assert(bitID < 8);
globalState.hl.hi |= (1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_L_set(u8 opcode) {
u8 bitID = (opcode - (u8)0xC5) >> 3;
assert(bitID < 8);
globalState.hl.lo |= (1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_DHL_set(u8 opcode) {
u8 bitID = (opcode - (u8)0xC6) >> 3;
assert(bitID < 8);
u8 value = readByte(globalState.hl.v);
value |= (1 << bitID);
writeByte(value, globalState.hl.v);
globalState.pc += 1;
globalState.cycleCount += 16;
}
void bit_A_set(u8 opcode) {
u8 bitID = (opcode - (u8)0xC7) >> 3;
assert(bitID < 8);
globalState.a |= (1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_B_res(u8 opcode) {
u8 bitID = (opcode - (u8)0x80) >> 3;
assert(bitID < 8);
globalState.bc.hi &= ~(1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_C_res(u8 opcode) {
u8 bitID = (opcode - (u8)0x81) >> 3;
assert(bitID < 8);
globalState.bc.lo &= ~(1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_D_res(u8 opcode) {
u8 bitID = (opcode - (u8)0x82) >> 3;
assert(bitID < 8);
globalState.de.hi &= ~(1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_E_res(u8 opcode) {
u8 bitID = (opcode - (u8)0x83) >> 3;
assert(bitID < 8);
globalState.de.lo &= ~(1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_H_res(u8 opcode) {
u8 bitID = (opcode - (u8)0x84) >> 3;
assert(bitID < 8);
globalState.hl.hi &= ~(1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_L_res(u8 opcode) {
u8 bitID = (opcode - (u8)0x85) >> 3;
assert(bitID < 8);
globalState.hl.lo &= ~(1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_DHL_res(u8 opcode) {
u8 bitID = (opcode - (u8)0x86) >> 3;
assert(bitID < 8);
u8 value = readByte(globalState.hl.v);
value &= ~(1 << bitID);
writeByte(value, globalState.hl.v);
globalState.pc += 1;
globalState.cycleCount += 16;
}
void bit_A_res(u8 opcode) {
u8 bitID = (opcode - (u8)0x87) >> 3;
assert(bitID < 8);
globalState.a &= ~(1 << bitID);
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_A_test(u8 opcode) {
u8 bitID = (opcode - (u8)0x47) >> 3;
assert(bitID < 8);
//printf("check bit %d of A\n", bitID);
u8 val = globalState.a;
if(((val >> bitID) & 1) == 0) {
setZeroFlag();
} else {
clearZeroFlag();
}
setHalfCarryFlag();
clearSubtractFlag();
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_B_test(u8 opcode) {
u8 bitID = (opcode - (u8)0x40) >> 3;
//printf("B opcode 0x%x bitId %d\n", opcode, bitID);
assert(bitID < 8);
//printf("check bit %d of B\n", bitID);
u8 val = globalState.bc.hi;
if(((val >> bitID) & 1) == 0) {
setZeroFlag();
} else {
clearZeroFlag();
}
setHalfCarryFlag();
clearSubtractFlag();
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_C_test(u8 opcode) {
u8 bitID = (opcode - (u8)0x41) >> 3;
assert(bitID < 8);
//printf("check bit %d of C\n", bitID);
u8 val = globalState.bc.lo;
if(((val >> bitID) & 1) == 0) {
setZeroFlag();
} else {
clearZeroFlag();
}
setHalfCarryFlag();
clearSubtractFlag();
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_D_test(u8 opcode) {
u8 bitID = (opcode - (u8)0x42) >> 3;
assert(bitID < 8);
//printf("check bit %d of D\n", bitID);
u8 val = globalState.de.hi;
if(((val >> bitID) & 1) == 0) {
setZeroFlag();
} else {
clearZeroFlag();
}
setHalfCarryFlag();
clearSubtractFlag();
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_E_test(u8 opcode) {
u8 bitID = (opcode - (u8)0x43) >> 3;
assert(bitID < 8);
//printf("check bit %d of E\n", bitID);
u8 val = globalState.de.lo;
if(((val >> bitID) & 1) == 0) {
setZeroFlag();
} else {
clearZeroFlag();
}
setHalfCarryFlag();
clearSubtractFlag();
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_H_test(u8 opcode) {
u8 bitID = (opcode - (u8)0x44) >> 3;
assert(bitID < 8);
//printf("check bit %d of H\n", bitID);
u8 val = globalState.hl.hi;
if(((val >> bitID) & 1) == 0) {
setZeroFlag();
} else {
clearZeroFlag();
}
setHalfCarryFlag();
clearSubtractFlag();
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_L_test(u8 opcode) {
u8 bitID = (opcode - (u8)0x45) >> 3;
assert(bitID < 8);
//printf("check bit %d of L\n", bitID);
u8 val = globalState.hl.lo;
if(((val >> bitID) & 1) == 0) {
setZeroFlag();
} else {
clearZeroFlag();
}
setHalfCarryFlag();
clearSubtractFlag();
globalState.pc += 1;
globalState.cycleCount += 8;
}
void bit_DHL_test(u8 opcode) {
u8 bitID = (opcode - (u8)0x45) >> 3;
assert(bitID < 8);
//printf("check bit %d of L\n", bitID);
u8 val = readByte(globalState.hl.v);
if(((val >> bitID) & 1) == 0) {
setZeroFlag();
} else {
clearZeroFlag();
}
setHalfCarryFlag();
clearSubtractFlag();
globalState.pc += 1;
globalState.cycleCount += 16;
}
// CB-prefixed opcode handler table
static OpcodeHandler* opcode_cbs[256] =
{RLC_B, RLC_C, RLC_D, RLC_E, RLC_H, RLC_L, RLC_DHL, RLC_A, // 0x0 - 0x7
RRC_B, RRC_C, RRC_D, RRC_E, RRC_H, RRC_L, RRC_DHL, RRC_A, // 0x8 - 0xf
RL_B, RL_C, RL_D, RL_E, RL_H, RL_L, RL_DHL, RL_A, // 0x10 - 0x17
RR_B, RR_C, RR_D, RR_E, RR_H, RR_L, RR_DHL, RR_A, // 0x18 - 0x1f
SLA_B, SLA_C, SLA_D, SLA_E, SLA_H, SLA_L, SLA_DHL, SLA_A, // 0x20 - 0x27
SRA_B, SRA_C, SRA_D, SRA_E, SRA_H, SRA_L, SRA_DHL, SRA_A, // 0x28 - 0x2f
SWAP_B, SWAP_C, SWAP_D, SWAP_E, SWAP_H, SWAP_L, SWAP_DHL, SWAP_A, // 0x30 - 0x37
SRL_B, SRL_C, SRL_D, SRL_E, SRL_H, SRL_L, SRL_DHL, SRL_A, // 0x38 - 0x3f
bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x40 - 0x47
bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x48 - 0x4f
bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x50 - 0x57
bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x58 - 0x5f
bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x60 - 0x67
bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x68 - 0x6f
bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x70 - 0x77
bit_B_test, bit_C_test, bit_D_test, bit_E_test, bit_H_test, bit_L_test, bit_DHL_test, bit_A_test, // 0x78 - 0x7f
bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0x80 - 0x8
bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0x88 - 0x8f
bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0x90 - 0x97
bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0x98 - 0x9f
bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0xa0 - 0xa7
bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0xa8 - 0xaf
bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0xb0 - 0xb7
bit_B_res, bit_C_res, bit_D_res, bit_E_res, bit_H_res, bit_L_res, bit_DHL_res, bit_A_res, // 0xb8 - 0xbf
bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xc0 - 0xc7
bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xc8 - 0xcf
bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xd0 - 0xd7
bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xd8 - 0xdf
bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xe0 - 0xe7
bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xe8 - 0xef
bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set , // 0xf0 - 0xf7
bit_B_set, bit_C_set, bit_D_set, bit_E_set, bit_H_set, bit_L_set, bit_DHL_set, bit_A_set }; // 0xf8 - 0xff
////////////////////////////////
// Opcodes //
////////////////////////////////
void LD_B_n(u8 opocde) { // 06
u8 imm = readByte(++globalState.pc);
globalState.pc++;
globalState.bc.hi = imm;
globalState.cycleCount += 8;
}
void LD_C_n(u8 opocde) { // 0E
u8 imm = readByte(++globalState.pc);
globalState.pc++;
globalState.bc.lo = imm;
globalState.cycleCount += 8;
}
void LD_D_n(u8 opocde) { // 16
u8 imm = readByte(++globalState.pc);
globalState.pc++;
globalState.de.hi = imm;
globalState.cycleCount += 8;
}
void LD_E_n(u8 opocde) { // 1e
u8 imm = readByte(++globalState.pc);
globalState.pc++;
globalState.de.lo = imm;
globalState.cycleCount += 8;
}
void LD_H_n(u8 opocde) { // 26
u8 imm = readByte(++globalState.pc);
globalState.pc++;
globalState.hl.hi = imm;
globalState.cycleCount += 8;
}
void LD_L_n(u8 opocde) { // 2e
u8 imm = readByte(++globalState.pc);
globalState.pc++;
globalState.hl.lo = imm;
globalState.cycleCount += 8;
}
// REGISTER-REGISTER LOADS (REGISTER A)
void LD_A_A(u8 opcode) { // 0x7f
globalState.pc++;
globalState.cycleCount += 4;
globalState.a = globalState.a;
}
void LD_A_B(u8 opcode) { // 0x78
globalState.pc++;
globalState.cycleCount += 4;
globalState.a = globalState.bc.hi;
}
void LD_A_C(u8 opcode) { // 0x79
globalState.pc++;
globalState.cycleCount += 4;
globalState.a = globalState.bc.lo;
}
void LD_A_D(u8 opcode) { // 0x7a
globalState.pc++;
globalState.cycleCount += 4;
globalState.a = globalState.de.hi;
}
void LD_A_E(u8 opcode) { // 0x7b
globalState.pc++;
globalState.cycleCount += 4;
globalState.a = globalState.de.lo;
}
void LD_A_H(u8 opcode) { // 0x7c
globalState.pc++;
globalState.cycleCount += 4;
globalState.a = globalState.hl.hi;
}
void LD_A_L(u8 opcode) { // 0x7d
globalState.pc++;
globalState.cycleCount += 4;
globalState.a = globalState.hl.lo;
}
void LD_A_DHL(u8 opcode) { // 0x7e
globalState.pc++;
globalState.cycleCount += 8;
globalState.a = readByte(globalState.hl.v);
}
// REGISTER-REGISTER LOADS (REGISTER B)
void LD_B_B(u8 opcode) { // 0x40
globalState.pc++;
globalState.cycleCount += 4;
globalState.bc.hi = globalState.bc.hi;
}
void LD_B_C(u8 opcode) { // 0x41
globalState.pc++;
globalState.cycleCount += 4;
globalState.bc.hi = globalState.bc.lo;
}
void LD_B_D(u8 opcode) { // 0x42
globalState.pc++;
globalState.cycleCount += 4;
globalState.bc.hi = globalState.de.hi;
}
void LD_B_E(u8 opcode) { // 0x43
globalState.pc++;
globalState.cycleCount += 4;
globalState.bc.hi = globalState.de.lo;
}
void LD_B_H(u8 opcode) { // 0x44
globalState.pc++;
globalState.cycleCount += 4;
globalState.bc.hi = globalState.hl.hi;
}
void LD_B_L(u8 opcode) { // 0x45
globalState.pc++;
globalState.cycleCount += 4;
globalState.bc.hi = globalState.hl.lo;
}
void LD_B_DHL(u8 opcode) { // 0x46
globalState.pc++;
globalState.cycleCount += 8;
globalState.bc.hi = readByte(globalState.hl.v);
}
// REGISTER-REGISTER LOADS (REGISTER C)
void LD_C_B(u8 opcode) { // 0x48
globalState.pc++;
globalState.cycleCount += 4;
globalState.bc.lo = globalState.bc.hi;
}
void LD_C_C(u8 opcode) { // 0x49
globalState.pc++;
globalState.cycleCount += 4;
globalState.bc.lo = globalState.bc.lo;
}
void LD_C_D(u8 opcode) { // 0x4a
globalState.pc++;
globalState.cycleCount += 4;
globalState.bc.lo = globalState.de.hi;
}
void LD_C_E(u8 opcode) { // 0x4b