-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcpu.c
4463 lines (3280 loc) · 88.9 KB
/
cpu.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
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 <stdio.h>
#include <assert.h>
#include "gb.h"
void gb_cpu_reset(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->irq_enable = false;
cpu->irq_enable_next = false;
cpu->halted = false;
cpu->sp = 0xfffe;
cpu->a = 0;
cpu->b = 0;
cpu->c = 0;
cpu->d = 0;
cpu->e = 0;
cpu->h = 0;
cpu->l = 0;
cpu->f_z = false;
cpu->f_n = false;
cpu->f_h = false;
cpu->f_c = false;
/* XXX For the time being we don't emulate the BOOTROM so we start the
* execution just past it */
cpu->pc = 0x100;
if (gb->gbc) {
/* In GBC mode the boot ROM sets A to 0x11 before starting the game.
* The game can use this to detect whether it's running on DMG or GBC.
*/
cpu->a = 0x11;
}
}
static inline void gb_cpu_clock_tick(struct gb *gb, int32_t cycles) {
gb->timestamp += cycles >> gb->double_speed;
if (gb->timestamp >= gb->sync.first_event) {
/* We have a device sync pending */
gb_sync_check_events(gb);
}
}
static uint8_t gb_cpu_readb(struct gb *gb, uint16_t addr) {
uint8_t b = gb_memory_readb(gb, addr);
gb_cpu_clock_tick(gb, 4);
return b;
}
static void gb_cpu_writeb(struct gb *gb, uint16_t addr, uint8_t val) {
gb_memory_writeb(gb, addr, val);
gb_cpu_clock_tick(gb, 4);
}
static uint16_t gb_cpu_bc(struct gb *gb) {
uint16_t b = gb->cpu.b;
uint16_t c = gb->cpu.c;
return (b << 8) | c;
}
static void gb_cpu_set_bc(struct gb *gb, uint16_t v) {
gb->cpu.b = v >> 8;
gb->cpu.c = v & 0xff;
}
static uint16_t gb_cpu_de(struct gb *gb) {
uint16_t d = gb->cpu.d;
uint16_t e = gb->cpu.e;
return (d << 8) | e;
}
static void gb_cpu_set_de(struct gb *gb, uint16_t v) {
gb->cpu.d = v >> 8;
gb->cpu.e = v & 0xff;
}
static uint16_t gb_cpu_hl(struct gb *gb) {
uint16_t h = gb->cpu.h;
uint16_t l = gb->cpu.l;
return (h << 8) | l;
}
static void gb_cpu_set_hl(struct gb *gb, uint16_t v) {
gb->cpu.h = v >> 8;
gb->cpu.l = v & 0xff;
}
void gb_cpu_dump(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
fprintf(stderr, "Flags: %c %c %c %c IME: %d\n",
cpu->f_z ? 'Z' : '-',
cpu->f_n ? 'N' : '-',
cpu->f_h ? 'H' : '-',
cpu->f_c ? 'C' : '-',
cpu->irq_enable);
fprintf(stderr, "PC: 0x%04x [%02x %02x %02x]\n",
cpu->pc,
gb_memory_readb(gb, cpu->pc),
gb_memory_readb(gb, cpu->pc + 1),
gb_memory_readb(gb, cpu->pc + 2));
fprintf(stderr, "SP: 0x%04x\n", cpu->sp);
fprintf(stderr, "A : 0x%02x\n", cpu->a);
fprintf(stderr, "B : 0x%02x C : 0x%02x BC : 0x%04x\n",
cpu->b, cpu->c, gb_cpu_bc(gb));
fprintf(stderr, "D : 0x%02x E : 0x%02x DE : 0x%04x\n",
cpu->d, cpu->e, gb_cpu_de(gb));
fprintf(stderr, "H : 0x%02x L : 0x%02x HL : 0x%04x\n",
cpu->h, cpu->l, gb_cpu_hl(gb));
fprintf(stderr, "\n");
}
static void gb_cpu_load_pc(struct gb *gb, uint16_t new_pc) {
gb->cpu.pc = new_pc;
gb_cpu_clock_tick(gb, 4);
}
static void gb_cpu_pushb(struct gb *gb, uint8_t b) {
struct gb_cpu *cpu = &gb->cpu;
cpu->sp = (cpu->sp - 1) & 0xffff;
gb_cpu_writeb(gb, cpu->sp, b);
}
static uint8_t gb_cpu_popb(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t b = gb_cpu_readb(gb, cpu->sp);
cpu->sp = (cpu->sp + 1) & 0xffff;
return b;
}
static void gb_cpu_pushw(struct gb *gb, uint16_t w) {
gb_cpu_pushb(gb, w >> 8);
gb_cpu_pushb(gb, w & 0xff);
}
static uint16_t gb_cpu_popw(struct gb *gb) {
uint16_t b0 = gb_cpu_popb(gb);
uint16_t b1 = gb_cpu_popb(gb);
return b0 | (b1 << 8);
}
static uint8_t gb_cpu_next_i8(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t i8 = gb_cpu_readb(gb, cpu->pc);
cpu->pc = (cpu->pc + 1) & 0xffff;
return i8;
}
static uint16_t gb_cpu_next_i16(struct gb *gb) {
uint16_t b0 = gb_cpu_next_i8(gb);
uint16_t b1 = gb_cpu_next_i8(gb);
return b0 | (b1 << 8);
}
/****************
* Instructions *
****************/
typedef void (*gb_instruction_f)(struct gb *);
/*****************
* Miscellaneous *
*****************/
static void gb_i_nop(struct gb *gb) {
// NOP
}
static void gb_i_undefined(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint16_t instruction_pc = (cpu->pc - 1) & 0xffff;
uint8_t instruction = gb_memory_readb(gb, instruction_pc);
// Undefined opcode. Apparently freezes the CPU on real hardware
fprintf(stderr,
"Undefined instruction instruction 0x%02x at 0x%04x\n",
instruction, instruction_pc);
die();
}
static void gb_i_di(struct gb *gb) {
gb->cpu.irq_enable = false;
gb->cpu.irq_enable_next = false;
}
static void gb_i_ei(struct gb *gb) {
/* Interrupts are re-enabled after the *next* instruction */
gb->cpu.irq_enable_next = true;
}
static void gb_i_stop(struct gb *gb) {
if (gb->speed_switch_pending) {
/* If a speed change has been requested it is executed on STOP and the
* execution resumes normally after that. */
/* XXX: this should take about 16ms when switching to double-speed
* mode and about 32ms when switching back to normal mode */
/* Clock speed is going to change, synchronize the relevant devices
* with the current clock speed */
gb_timer_sync(gb);
gb_dma_sync(gb);
gb->double_speed = !gb->double_speed;
/* Re-sync with new prediction */
gb_timer_sync(gb);
gb_dma_sync(gb);
return;
}
// XXX TODO: stop CPU and screen until button press
fprintf(stderr, "Implement STOP!\n");
die();
}
/* Halt and wait for interrupt */
static void gb_i_halt(struct gb *gb) {
gb->cpu.halted = true;
}
/* Set Carry Flag */
static void gb_i_scf(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->f_n = false;
cpu->f_h = false;
cpu->f_c = true;
}
/* Complement Carry Flag */
static void gb_i_ccf(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->f_n = false;
cpu->f_h = false;
cpu->f_c = !cpu->f_c;
}
/**************
* Arithmetic *
**************/
static uint8_t gb_cpu_inc(struct gb *gb, uint8_t v) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t r = (v + 1) & 0xff;
cpu->f_z = (r == 0);
cpu->f_n = false;
/* We'll have a half-carry if the low nibble is 0xf */
cpu->f_h = ((v & 0xf) == 0xf);
/* Carry is not modified by this instruction */
return r;
}
static uint8_t gb_cpu_dec(struct gb *gb, uint8_t v) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t r = (v - 1) & 0xff;
cpu->f_z = (r == 0);
cpu->f_n = true;
/* We'll have a half-carry if the low nibble is 0 */
cpu->f_h = ((v & 0xf) == 0);
/* Carry is not modified by this instruction */
return r;
}
static void gb_i_inc_a(struct gb *gb) {
gb->cpu.a = gb_cpu_inc(gb, gb->cpu.a);
}
static void gb_i_inc_b(struct gb *gb) {
gb->cpu.b = gb_cpu_inc(gb, gb->cpu.b);
}
static void gb_i_inc_c(struct gb *gb) {
gb->cpu.c = gb_cpu_inc(gb, gb->cpu.c);
}
static void gb_i_inc_d(struct gb *gb) {
gb->cpu.d = gb_cpu_inc(gb, gb->cpu.d);
}
static void gb_i_inc_e(struct gb *gb) {
gb->cpu.e = gb_cpu_inc(gb, gb->cpu.e);
}
static void gb_i_inc_h(struct gb *gb) {
gb->cpu.h = gb_cpu_inc(gb, gb->cpu.h);
}
static void gb_i_inc_l(struct gb *gb) {
gb->cpu.l = gb_cpu_inc(gb, gb->cpu.l);
}
static void gb_i_inc_mhl(struct gb *gb) {
uint16_t hl = gb_cpu_hl(gb);
uint8_t v = gb_cpu_readb(gb, hl);
v = gb_cpu_inc(gb, v);
gb_cpu_writeb(gb, hl, v);
}
static void gb_i_dec_a(struct gb *gb) {
gb->cpu.a = gb_cpu_dec(gb, gb->cpu.a);
}
static void gb_i_dec_b(struct gb *gb) {
gb->cpu.b = gb_cpu_dec(gb, gb->cpu.b);
}
static void gb_i_dec_c(struct gb *gb) {
gb->cpu.c = gb_cpu_dec(gb, gb->cpu.c);
}
static void gb_i_dec_d(struct gb *gb) {
gb->cpu.d = gb_cpu_dec(gb, gb->cpu.d);
}
static void gb_i_dec_e(struct gb *gb) {
gb->cpu.e = gb_cpu_dec(gb, gb->cpu.e);
}
static void gb_i_dec_h(struct gb *gb) {
gb->cpu.h = gb_cpu_dec(gb, gb->cpu.h);
}
static void gb_i_dec_l(struct gb *gb) {
gb->cpu.l = gb_cpu_dec(gb, gb->cpu.l);
}
static void gb_i_dec_mhl(struct gb *gb) {
uint16_t hl = gb_cpu_hl(gb);
uint8_t v = gb_cpu_readb(gb, hl);
v = gb_cpu_dec(gb, v);
gb_cpu_writeb(gb, hl, v);
}
/* Add two 16 bit values, update the CPU flags and return the result */
static uint16_t gb_cpu_addw_set_flags(struct gb *gb, uint16_t a, uint16_t b) {
struct gb_cpu *cpu = &gb->cpu;
/* Widen to 32bits to get the carry */
uint32_t wa = a;
uint32_t wb = b;
uint32_t r = a + b;
cpu->f_n = false;
cpu->f_c = r & 0x10000;
cpu->f_h = (wa ^ wb ^ r) & 0x1000;
/* cpu->f_z is not altered */
gb_cpu_clock_tick(gb, 4);
return r;
}
static uint8_t gb_cpu_sub_set_flags(struct gb *gb, uint8_t a, uint8_t b) {
struct gb_cpu *cpu = &gb->cpu;
/* Check for carry using 16bit arithmetic */
uint16_t al = a;
uint16_t bl = b;
uint16_t r = al - bl;
cpu->f_z = !(r & 0xff);
cpu->f_n = true;
cpu->f_h = (a ^ b ^ r) & 0x10;
cpu->f_c = r & 0x100;
return r;
}
static void gb_i_sub_a_a(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sub_set_flags(gb, cpu->a, cpu->a);
}
static void gb_i_sub_a_b(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sub_set_flags(gb, cpu->a, cpu->b);
}
static void gb_i_sub_a_c(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sub_set_flags(gb, cpu->a, cpu->c);
}
static void gb_i_sub_a_d(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sub_set_flags(gb, cpu->a, cpu->d);
}
static void gb_i_sub_a_e(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sub_set_flags(gb, cpu->a, cpu->e);
}
static void gb_i_sub_a_h(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sub_set_flags(gb, cpu->a, cpu->h);
}
static void gb_i_sub_a_l(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sub_set_flags(gb, cpu->a, cpu->l);
}
static void gb_i_sub_a_mhl(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint16_t hl = gb_cpu_hl(gb);
uint8_t v;
v = gb_cpu_readb(gb, hl);
cpu->a = gb_cpu_sub_set_flags(gb, cpu->a, v);
}
static void gb_i_sub_a_i8(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t i8 = gb_cpu_next_i8(gb);
cpu->a = gb_cpu_sub_set_flags(gb, cpu->a, i8);
}
/* Subtract with carry */
static uint8_t gb_cpu_sbc_set_flags(struct gb *gb, uint8_t a, uint8_t b) {
struct gb_cpu *cpu = &gb->cpu;
/* Check for carry using 16bit arithmetic */
uint16_t al = a;
uint16_t bl = b;
uint16_t c = cpu->f_c;
uint16_t r = al - bl - c;
cpu->f_z = !(r & 0xff);
cpu->f_n = true;
cpu->f_h = (a ^ b ^ r) & 0x10;
cpu->f_c = r & 0x100;
return r;
}
static void gb_i_sbc_a_a(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sbc_set_flags(gb, cpu->a, cpu->a);
}
static void gb_i_sbc_a_b(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sbc_set_flags(gb, cpu->a, cpu->b);
}
static void gb_i_sbc_a_c(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sbc_set_flags(gb, cpu->a, cpu->c);
}
static void gb_i_sbc_a_d(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sbc_set_flags(gb, cpu->a, cpu->d);
}
static void gb_i_sbc_a_e(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sbc_set_flags(gb, cpu->a, cpu->e);
}
static void gb_i_sbc_a_h(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sbc_set_flags(gb, cpu->a, cpu->h);
}
static void gb_i_sbc_a_l(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_sbc_set_flags(gb, cpu->a, cpu->l);
}
static void gb_i_sbc_a_mhl(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint16_t hl = gb_cpu_hl(gb);
uint8_t v;
v = gb_cpu_readb(gb, hl);
cpu->a = gb_cpu_sbc_set_flags(gb, cpu->a, v);
}
static void gb_i_sbc_a_i8(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t i8 = gb_cpu_next_i8(gb);
cpu->a = gb_cpu_sbc_set_flags(gb, cpu->a, i8);
}
static uint8_t gb_cpu_add_set_flags(struct gb *gb, uint8_t a, uint8_t b) {
struct gb_cpu *cpu = &gb->cpu;
/* Check for carry using 16bit arithmetic */
uint16_t al = a;
uint16_t bl = b;
uint16_t r = al + bl;
cpu->f_z = !(r & 0xff);
cpu->f_n = false;
cpu->f_h = (a ^ b ^ r) & 0x10;
cpu->f_c = r & 0x100;
return r;
}
static void gb_i_add_a_a(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_add_set_flags(gb, cpu->a, cpu->a);
}
static void gb_i_add_a_b(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_add_set_flags(gb, cpu->a, cpu->b);
}
static void gb_i_add_a_c(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_add_set_flags(gb, cpu->a, cpu->c);
}
static void gb_i_add_a_d(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_add_set_flags(gb, cpu->a, cpu->d);
}
static void gb_i_add_a_e(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_add_set_flags(gb, cpu->a, cpu->e);
}
static void gb_i_add_a_h(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_add_set_flags(gb, cpu->a, cpu->h);
}
static void gb_i_add_a_l(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_add_set_flags(gb, cpu->a, cpu->l);
}
static void gb_i_add_a_mhl(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint16_t hl = gb_cpu_hl(gb);
uint8_t v;
v = gb_cpu_readb(gb, hl);
cpu->a = gb_cpu_add_set_flags(gb, cpu->a, v);
}
static void gb_i_add_a_i8(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t i8 = gb_cpu_next_i8(gb);
cpu->a = gb_cpu_add_set_flags(gb, cpu->a, i8);
}
/* Add with carry and set flags */
static uint8_t gb_cpu_adc_set_flags(struct gb *gb, uint8_t a, uint8_t b) {
struct gb_cpu *cpu = &gb->cpu;
/* Check for carry using 16bit arithmetic */
uint16_t al = a;
uint16_t bl = b;
uint16_t c = cpu->f_c;
uint16_t r = al + bl + c;
cpu->f_z = !(r & 0xff);
cpu->f_n = false;
cpu->f_h = (a ^ b ^ r) & 0x10;
cpu->f_c = r & 0x100;
return r;
}
static void gb_i_adc_a_a(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_adc_set_flags(gb, cpu->a, cpu->a);
}
static void gb_i_adc_a_b(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_adc_set_flags(gb, cpu->a, cpu->b);
}
static void gb_i_adc_a_c(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_adc_set_flags(gb, cpu->a, cpu->c);
}
static void gb_i_adc_a_d(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_adc_set_flags(gb, cpu->a, cpu->d);
}
static void gb_i_adc_a_e(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_adc_set_flags(gb, cpu->a, cpu->e);
}
static void gb_i_adc_a_h(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_adc_set_flags(gb, cpu->a, cpu->h);
}
static void gb_i_adc_a_l(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_adc_set_flags(gb, cpu->a, cpu->l);
}
static void gb_i_adc_a_mhl(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint16_t hl = gb_cpu_hl(gb);
uint8_t v;
v = gb_cpu_readb(gb, hl);
cpu->a = gb_cpu_adc_set_flags(gb, cpu->a, v);
}
static void gb_i_adc_a_i8(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t i8 = gb_cpu_next_i8(gb);
cpu->a = gb_cpu_adc_set_flags(gb, cpu->a, i8);
}
static uint16_t gb_add_sp_si8(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
/* Offset is signed */
int8_t i8 = gb_cpu_next_i8(gb);
/* Use 32bit arithmetic to avoid signed integer overflow UB */
int32_t r = cpu->sp;
r += i8;
cpu->f_z = false;
cpu->f_n = false;
/* Carry and Half-carry are for the low byte */
cpu->f_h = (cpu->sp ^ i8 ^ r) & 0x10;
cpu->f_c = (cpu->sp ^ i8 ^ r) & 0x100;
return (uint16_t)r;
}
static void gb_i_add_sp_si8(struct gb *gb) {
gb->cpu.sp = gb_add_sp_si8(gb);
gb_cpu_clock_tick(gb, 8);
}
static void gb_i_add_hl_bc(struct gb *gb) {
uint16_t hl = gb_cpu_hl(gb);
uint16_t bc = gb_cpu_bc(gb);
hl = gb_cpu_addw_set_flags(gb, hl, bc);
gb_cpu_set_hl(gb, hl);
}
static void gb_i_add_hl_de(struct gb *gb) {
uint16_t hl = gb_cpu_hl(gb);
uint16_t de = gb_cpu_de(gb);
hl = gb_cpu_addw_set_flags(gb, hl, de);
gb_cpu_set_hl(gb, hl);
}
static void gb_i_add_hl_hl(struct gb *gb) {
uint16_t hl = gb_cpu_hl(gb);
hl = gb_cpu_addw_set_flags(gb, hl, hl);
gb_cpu_set_hl(gb, hl);
}
static void gb_i_add_hl_sp(struct gb *gb) {
uint16_t hl = gb_cpu_hl(gb);
hl = gb_cpu_addw_set_flags(gb, hl, gb->cpu.sp);
gb_cpu_set_hl(gb, hl);
}
static void gb_i_inc_sp(struct gb *gb) {
uint16_t sp = gb->cpu.sp;
sp = (sp + 1) & 0xffff;
gb->cpu.sp = sp;
gb_cpu_clock_tick(gb, 4);
}
static void gb_i_inc_bc(struct gb *gb) {
uint16_t bc = gb_cpu_bc(gb);
bc = (bc + 1) & 0xffff;
gb_cpu_set_bc(gb, bc);
gb_cpu_clock_tick(gb, 4);
}
static void gb_i_inc_de(struct gb *gb) {
uint16_t de = gb_cpu_de(gb);
de = (de + 1) & 0xffff;
gb_cpu_set_de(gb, de);
gb_cpu_clock_tick(gb, 4);
}
static void gb_i_inc_hl(struct gb *gb) {
uint16_t hl = gb_cpu_hl(gb);
hl = (hl + 1) & 0xffff;
gb_cpu_set_hl(gb, hl);
gb_cpu_clock_tick(gb, 4);
}
static void gb_i_dec_sp(struct gb *gb) {
uint16_t sp = gb->cpu.sp;
sp = (sp - 1) & 0xffff;
gb->cpu.sp = sp;
gb_cpu_clock_tick(gb, 4);
}
static void gb_i_dec_bc(struct gb *gb) {
uint16_t bc = gb_cpu_bc(gb);
bc = (bc - 1) & 0xffff;
gb_cpu_set_bc(gb, bc);
gb_cpu_clock_tick(gb, 4);
}
static void gb_i_dec_de(struct gb *gb) {
uint16_t de = gb_cpu_de(gb);
de = (de - 1) & 0xffff;
gb_cpu_set_de(gb, de);
gb_cpu_clock_tick(gb, 4);
}
static void gb_i_dec_hl(struct gb *gb) {
uint16_t hl = gb_cpu_hl(gb);
hl = (hl - 1) & 0xffff;
gb_cpu_set_hl(gb, hl);
gb_cpu_clock_tick(gb, 4);
}
static void gb_i_cp_a_a(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
gb_cpu_sub_set_flags(gb, cpu->a, cpu->a);
}
static void gb_i_cp_a_b(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
gb_cpu_sub_set_flags(gb, cpu->a, cpu->b);
}
static void gb_i_cp_a_c(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
gb_cpu_sub_set_flags(gb, cpu->a, cpu->c);
}
static void gb_i_cp_a_d(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
gb_cpu_sub_set_flags(gb, cpu->a, cpu->d);
}
static void gb_i_cp_a_e(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
gb_cpu_sub_set_flags(gb, cpu->a, cpu->e);
}
static void gb_i_cp_a_h(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
gb_cpu_sub_set_flags(gb, cpu->a, cpu->h);
}
static void gb_i_cp_a_l(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
gb_cpu_sub_set_flags(gb, cpu->a, cpu->l);
}
static void gb_i_cp_a_mhl(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint16_t hl = gb_cpu_hl(gb);
uint8_t v;
v = gb_cpu_readb(gb, hl);
gb_cpu_sub_set_flags(gb, cpu->a, v);
}
static void gb_i_cp_a_i8(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t i8 = gb_cpu_next_i8(gb);
gb_cpu_sub_set_flags(gb, cpu->a, i8);
}
static uint8_t gb_cpu_and_set_flags(struct gb *gb, uint8_t a, uint8_t b) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t r = a & b;
cpu->f_z = (r == 0);
cpu->f_n = false;
cpu->f_h = true;
cpu->f_c = false;
return r;
}
static void gb_i_and_a_a(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_and_set_flags(gb, cpu->a, cpu->a);
}
static void gb_i_and_a_b(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_and_set_flags(gb, cpu->a, cpu->b);
}
static void gb_i_and_a_c(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_and_set_flags(gb, cpu->a, cpu->c);
}
static void gb_i_and_a_d(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_and_set_flags(gb, cpu->a, cpu->d);
}
static void gb_i_and_a_e(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_and_set_flags(gb, cpu->a, cpu->e);
}
static void gb_i_and_a_h(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_and_set_flags(gb, cpu->a, cpu->h);
}
static void gb_i_and_a_l(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_and_set_flags(gb, cpu->a, cpu->l);
}
static void gb_i_and_a_mhl(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint16_t hl = gb_cpu_hl(gb);
uint8_t v;
v = gb_cpu_readb(gb, hl);
cpu->a = gb_cpu_and_set_flags(gb, cpu->a, v);
}
static void gb_i_and_a_i8(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t i8 = gb_cpu_next_i8(gb);
cpu->a = gb_cpu_and_set_flags(gb, cpu->a, i8);
}
static uint8_t gb_cpu_xor_set_flags(struct gb *gb, uint8_t a, uint8_t b) {
struct gb_cpu *cpu = &gb->cpu;
uint8_t r = a ^ b;
cpu->f_z = (r == 0);
cpu->f_n = false;
cpu->f_h = false;
cpu->f_c = false;
return r;
}
static void gb_i_xor_a_a(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_xor_set_flags(gb, cpu->a, cpu->a);
}
static void gb_i_xor_a_b(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_xor_set_flags(gb, cpu->a, cpu->b);
}
static void gb_i_xor_a_c(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_xor_set_flags(gb, cpu->a, cpu->c);
}
static void gb_i_xor_a_d(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_xor_set_flags(gb, cpu->a, cpu->d);
}
static void gb_i_xor_a_e(struct gb *gb) {
struct gb_cpu *cpu = &gb->cpu;
cpu->a = gb_cpu_xor_set_flags(gb, cpu->a, cpu->e);
}