-
Notifications
You must be signed in to change notification settings - Fork 14
/
cpu.c
878 lines (727 loc) · 17.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
#include "defs.h"
#include "regs.h"
#include "hw.h"
#include "cpu.h"
#include "mem.h"
#include "fastmem.h"
#include "cpuregs.h"
#include "cpucore.h"
#ifdef USE_ASM
#include "asm.h"
#endif
struct cpu cpu;
#define ZFLAG(n) ( (n) ? 0 : FZ )
#define PUSH(w) ( (SP -= 2), (writew(xSP, (w))) )
#define POP(w) ( ((w) = readw(xSP)), (SP += 2) )
#define FETCH_OLD ( mbc.rmap[PC>>12] \
? mbc.rmap[PC>>12][PC++] \
: mem_read(PC++) )
#define FETCH (readb(PC++))
#define INC(r) { ((r)++); \
F = (F & (FL|FC)) | incflag_table[(r)]; }
#define DEC(r) { ((r)--); \
F = (F & (FL|FC)) | decflag_table[(r)]; }
#define INCW(r) ( (r)++ )
#define DECW(r) ( (r)-- )
#define ADD(n) { \
W(acc) = (un16)A + (un16)(n); \
F = (ZFLAG(LB(acc))) \
| (FH & ((A ^ (n) ^ LB(acc)) << 1)) \
| (HB(acc) << 4); \
A = LB(acc); }
#define ADC(n) { \
W(acc) = (un16)A + (un16)(n) + (un16)((F&FC)>>4); \
F = (ZFLAG(LB(acc))) \
| (FH & ((A ^ (n) ^ LB(acc)) << 1)) \
| (HB(acc) << 4); \
A = LB(acc); }
#define ADDW(n) { \
DW(acc) = (un32)HL + (un32)(n); \
F = (F & (FZ)) \
| (FH & ((H ^ ((n)>>8) ^ HB(acc)) << 1)) \
| (acc.b[HI][LO] << 4); \
HL = W(acc); }
#define ADDSP(n) { \
DW(acc) = (un32)SP + (un32)(n8)(n); \
F = (FH & (((SP>>8) ^ ((n)>>8) ^ HB(acc)) << 1)) \
| (acc.b[HI][LO] << 4); \
SP = W(acc); }
#define LDHLSP(n) { \
DW(acc) = (un32)SP + (un32)(n8)(n); \
F = (FH & (((SP>>8) ^ ((n)>>8) ^ HB(acc)) << 1)) \
| (acc.b[HI][LO] << 4); \
HL = W(acc); }
#define CP(n) { \
W(acc) = (un16)A - (un16)(n); \
F = FN \
| (ZFLAG(LB(acc))) \
| (FH & ((A ^ (n) ^ LB(acc)) << 1)) \
| ((un8)(-(n8)HB(acc)) << 4); }
#define SUB(n) { CP((n)); A = LB(acc); }
#define SBC(n) { \
W(acc) = (un16)A - (un16)(n) - (un16)((F&FC)>>4); \
F = FN \
| (ZFLAG((n8)LB(acc))) \
| (FH & ((A ^ (n) ^ LB(acc)) << 1)) \
| ((un8)(-(n8)HB(acc)) << 4); \
A = LB(acc); }
#define AND(n) { A &= (n); \
F = ZFLAG(A) | FH; }
#define XOR(n) { A ^= (n); \
F = ZFLAG(A); }
#define OR(n) { A |= (n); \
F = ZFLAG(A); }
#define RLCA(r) { (r) = ((r)>>7) | ((r)<<1); \
F = (((r)&0x01)<<4); }
#define RRCA(r) { (r) = ((r)<<7) | ((r)>>1); \
F = (((r)&0x80)>>3); }
#define RLA(r) { \
LB(acc) = (((r)&0x80)>>3); \
(r) = ((r)<<1) | ((F&FC)>>4); \
F = LB(acc); }
#define RRA(r) { \
LB(acc) = (((r)&0x01)<<4); \
(r) = ((r)>>1) | ((F&FC)<<3); \
F = LB(acc); }
#define RLC(r) { RLCA(r); F |= ZFLAG(r); }
#define RRC(r) { RRCA(r); F |= ZFLAG(r); }
#define RL(r) { RLA(r); F |= ZFLAG(r); }
#define RR(r) { RRA(r); F |= ZFLAG(r); }
#define SLA(r) { \
LB(acc) = (((r)&0x80)>>3); \
(r) <<= 1; \
F = ZFLAG((r)) | LB(acc); }
#define SRA(r) { \
LB(acc) = (((r)&0x01)<<4); \
(r) = (un8)(((n8)(r))>>1); \
F = ZFLAG((r)) | LB(acc); }
#define SRL(r) { \
LB(acc) = (((r)&0x01)<<4); \
(r) >>= 1; \
F = ZFLAG((r)) | LB(acc); }
#define CPL(r) { \
(r) = ~(r); \
F |= (FH|FN); }
#define SCF { F = (F & (FZ)) | FC; }
#define CCF { F = (F & (FZ|FC)) ^ FC; }
#define DAA { \
A += (LB(acc) = daa_table[((((int)F)&0x70)<<4) | A]); \
F = (F & (FN)) | ZFLAG(A) | daa_carry_table[LB(acc)>>2]; }
#define SWAP(r) { \
(r) = swap_table[(r)]; \
F = ZFLAG((r)); }
#define BIT(n,r) { F = (F & FC) | ZFLAG(((r) & (1 << (n)))) | FH; }
#define RES(n,r) { (r) &= ~(1 << (n)); }
#define SET(n,r) { (r) |= (1 << (n)); }
#define CB_REG_CASES(r, n) \
case 0x00|(n): RLC(r); break; \
case 0x08|(n): RRC(r); break; \
case 0x10|(n): RL(r); break; \
case 0x18|(n): RR(r); break; \
case 0x20|(n): SLA(r); break; \
case 0x28|(n): SRA(r); break; \
case 0x30|(n): SWAP(r); break; \
case 0x38|(n): SRL(r); break; \
case 0x40|(n): BIT(0, r); break; \
case 0x48|(n): BIT(1, r); break; \
case 0x50|(n): BIT(2, r); break; \
case 0x58|(n): BIT(3, r); break; \
case 0x60|(n): BIT(4, r); break; \
case 0x68|(n): BIT(5, r); break; \
case 0x70|(n): BIT(6, r); break; \
case 0x78|(n): BIT(7, r); break; \
case 0x80|(n): RES(0, r); break; \
case 0x88|(n): RES(1, r); break; \
case 0x90|(n): RES(2, r); break; \
case 0x98|(n): RES(3, r); break; \
case 0xA0|(n): RES(4, r); break; \
case 0xA8|(n): RES(5, r); break; \
case 0xB0|(n): RES(6, r); break; \
case 0xB8|(n): RES(7, r); break; \
case 0xC0|(n): SET(0, r); break; \
case 0xC8|(n): SET(1, r); break; \
case 0xD0|(n): SET(2, r); break; \
case 0xD8|(n): SET(3, r); break; \
case 0xE0|(n): SET(4, r); break; \
case 0xE8|(n): SET(5, r); break; \
case 0xF0|(n): SET(6, r); break; \
case 0xF8|(n): SET(7, r); break;
#define ALU_CASES(base, imm, op, label) \
case (imm): b = FETCH; goto label; \
case (base): b = B; goto label; \
case (base)+1: b = C; goto label; \
case (base)+2: b = D; goto label; \
case (base)+3: b = E; goto label; \
case (base)+4: b = H; goto label; \
case (base)+5: b = L; goto label; \
case (base)+6: b = readb(HL); goto label; \
case (base)+7: b = A; \
label: op(b); break;
#define JR ( PC += 1+(n8)readb(PC) )
#define JP ( PC = readw(PC) )
#define CALL ( PUSH(PC+2), JP )
#define NOJR ( clen--, PC++ )
#define NOJP ( clen--, PC+=2 )
#define NOCALL ( clen-=3, PC+=2 )
#define NORET ( clen-=3 )
#define RST(n) { PUSH(PC); PC = (n); }
#define RET ( POP(PC) )
#define EI ( IMA = 1 )
#define DI ( cpu.halt = IMA = IME = 0 )
#define PRE_INT ( DI, PUSH(PC) )
#define THROW_INT(n) ( (IF &= ~(1<<(n))), (PC = 0x40+((n)<<3)) )
void cpu_reset()
{
cpu.speed = 0;
cpu.halt = 0;
cpu.div = 0;
cpu.tim = 0;
cpu.lcdc = 40;
IME = 0;
IMA = 0;
PC = 0x0100;
SP = 0xFFFE;
AF = 0x01B0;
BC = 0x0013;
DE = 0x00D8;
HL = 0x014D;
if (hw.cgb) A = 0x11;
if (hw.gba) B = 0x01;
}
void div_advance(int cnt)
{
cpu.div += (cnt<<1);
if (cpu.div >= 256)
{
R_DIV += (cpu.div >> 8);
cpu.div &= 0xff;
}
}
void timer_advance(int cnt)
{
int unit, tima;
if (!(R_TAC & 0x04)) return;
unit = ((-R_TAC) & 3) << 1;
cpu.tim += (cnt<<unit);
if (cpu.tim >= 512)
{
tima = R_TIMA + (cpu.tim >> 9);
cpu.tim &= 0x1ff;
if (tima >= 256)
{
hw_interrupt(IF_TIMER, IF_TIMER);
hw_interrupt(0, IF_TIMER);
}
while (tima >= 256)
tima = tima - 256 + R_TMA;
R_TIMA = tima;
}
}
void lcdc_advance(int cnt)
{
cpu.lcdc -= cnt;
if (cpu.lcdc <= 0) lcdc_trans();
}
void sound_advance(int cnt)
{
cpu.snd += cnt;
}
void cpu_timers(int cnt)
{
div_advance(cnt << cpu.speed);
timer_advance(cnt << cpu.speed);
lcdc_advance(cnt);
sound_advance(cnt);
}
int cpu_idle(int max)
{
int cnt, unit;
if (!(cpu.halt && IME)) return 0;
if (R_IF & R_IE)
{
cpu.halt = 0;
return 0;
}
/* Make sure we don't miss lcdc status events! */
if ((R_IE & (IF_VBLANK | IF_STAT)) && (max > cpu.lcdc))
max = cpu.lcdc;
/* If timer interrupt cannot happen, this is very simple! */
if (!((R_IE & IF_TIMER) && (R_TAC & 0x04)))
{
cpu_timers(max);
return max;
}
/* Figure out when the next timer interrupt will happen */
unit = ((-R_TAC) & 3) << 1;
cnt = (511 - cpu.tim + (1<<unit)) >> unit;
cnt += (255 - R_TIMA) << (9 - unit);
if (max < cnt)
cnt = max;
cpu_timers(cnt);
return cnt;
}
#ifndef ASM_CPU_EMULATE
extern int debug_trace;
int cpu_emulate(int cycles)
{
int i;
byte op, cbop;
int clen;
static union reg acc;
static byte b;
static word w;
i = cycles;
next:
if ((clen = cpu_idle(i)))
{
i -= clen;
if (i > 0) goto next;
return cycles-i;
}
if (IME && (IF & IE))
{
PRE_INT;
switch ((byte)(IF & IE))
{
case 0x01: case 0x03: case 0x05: case 0x07:
case 0x09: case 0x0B: case 0x0D: case 0x0F:
case 0x11: case 0x13: case 0x15: case 0x17:
case 0x19: case 0x1B: case 0x1D: case 0x1F:
THROW_INT(0); break;
case 0x02: case 0x06: case 0x0A: case 0x0E:
case 0x12: case 0x16: case 0x1A: case 0x1E:
THROW_INT(1); break;
case 0x04: case 0x0C: case 0x14: case 0x1C:
THROW_INT(2); break;
case 0x08: case 0x18:
THROW_INT(3); break;
case 0x10:
THROW_INT(4); break;
}
}
IME = IMA;
// if (debug_trace) debug_disassemble(PC, 1);
op = FETCH;
clen = cycles_table[op];
switch(op)
{
case 0x00: /* NOP */
case 0x40: /* LD B,B */
case 0x49: /* LD C,C */
case 0x52: /* LD D,D */
case 0x5B: /* LD E,E */
case 0x64: /* LD H,H */
case 0x6D: /* LD L,L */
case 0x7F: /* LD A,A */
break;
case 0x41: /* LD B,C */
B = C; break;
case 0x42: /* LD B,D */
B = D; break;
case 0x43: /* LD B,E */
B = E; break;
case 0x44: /* LD B,H */
B = H; break;
case 0x45: /* LD B,L */
B = L; break;
case 0x46: /* LD B,(HL) */
B = readb(xHL); break;
case 0x47: /* LD B,A */
B = A; break;
case 0x48: /* LD C,B */
C = B; break;
case 0x4A: /* LD C,D */
C = D; break;
case 0x4B: /* LD C,E */
C = E; break;
case 0x4C: /* LD C,H */
C = H; break;
case 0x4D: /* LD C,L */
C = L; break;
case 0x4E: /* LD C,(HL) */
C = readb(xHL); break;
case 0x4F: /* LD C,A */
C = A; break;
case 0x50: /* LD D,B */
D = B; break;
case 0x51: /* LD D,C */
D = C; break;
case 0x53: /* LD D,E */
D = E; break;
case 0x54: /* LD D,H */
D = H; break;
case 0x55: /* LD D,L */
D = L; break;
case 0x56: /* LD D,(HL) */
D = readb(xHL); break;
case 0x57: /* LD D,A */
D = A; break;
case 0x58: /* LD E,B */
E = B; break;
case 0x59: /* LD E,C */
E = C; break;
case 0x5A: /* LD E,D */
E = D; break;
case 0x5C: /* LD E,H */
E = H; break;
case 0x5D: /* LD E,L */
E = L; break;
case 0x5E: /* LD E,(HL) */
E = readb(xHL); break;
case 0x5F: /* LD E,A */
E = A; break;
case 0x60: /* LD H,B */
H = B; break;
case 0x61: /* LD H,C */
H = C; break;
case 0x62: /* LD H,D */
H = D; break;
case 0x63: /* LD H,E */
H = E; break;
case 0x65: /* LD H,L */
H = L; break;
case 0x66: /* LD H,(HL) */
H = readb(xHL); break;
case 0x67: /* LD H,A */
H = A; break;
case 0x68: /* LD L,B */
L = B; break;
case 0x69: /* LD L,C */
L = C; break;
case 0x6A: /* LD L,D */
L = D; break;
case 0x6B: /* LD L,E */
L = E; break;
case 0x6C: /* LD L,H */
L = H; break;
case 0x6E: /* LD L,(HL) */
L = readb(xHL); break;
case 0x6F: /* LD L,A */
L = A; break;
case 0x70: /* LD (HL),B */
b = B; goto __LD_HL;
case 0x71: /* LD (HL),C */
b = C; goto __LD_HL;
case 0x72: /* LD (HL),D */
b = D; goto __LD_HL;
case 0x73: /* LD (HL),E */
b = E; goto __LD_HL;
case 0x74: /* LD (HL),H */
b = H; goto __LD_HL;
case 0x75: /* LD (HL),L */
b = L; goto __LD_HL;
case 0x77: /* LD (HL),A */
b = A;
__LD_HL:
writeb(xHL,b);
break;
case 0x78: /* LD A,B */
A = B; break;
case 0x79: /* LD A,C */
A = C; break;
case 0x7A: /* LD A,D */
A = D; break;
case 0x7B: /* LD A,E */
A = E; break;
case 0x7C: /* LD A,H */
A = H; break;
case 0x7D: /* LD A,L */
A = L; break;
case 0x7E: /* LD A,(HL) */
A = readb(xHL); break;
case 0x01: /* LD BC,imm */
BC = readw(xPC); PC += 2; break;
case 0x11: /* LD DE,imm */
DE = readw(xPC); PC += 2; break;
case 0x21: /* LD HL,imm */
HL = readw(xPC); PC += 2; break;
case 0x31: /* LD SP,imm */
SP = readw(xPC); PC += 2; break;
case 0x02: /* LD (BC),A */
writeb(xBC, A); break;
case 0x0A: /* LD A,(BC) */
A = readb(xBC); break;
case 0x12: /* LD (DE),A */
writeb(xDE, A); break;
case 0x1A: /* LD A,(DE) */
A = readb(xDE); break;
case 0x22: /* LDI (HL),A */
writeb(xHL, A); HL++; break;
case 0x2A: /* LDI A,(HL) */
A = readb(xHL); HL++; break;
case 0x32: /* LDD (HL),A */
writeb(xHL, A); HL--; break;
case 0x3A: /* LDD A,(HL) */
A = readb(xHL); HL--; break;
case 0x06: /* LD B,imm */
B = FETCH; break;
case 0x0E: /* LD C,imm */
C = FETCH; break;
case 0x16: /* LD D,imm */
D = FETCH; break;
case 0x1E: /* LD E,imm */
E = FETCH; break;
case 0x26: /* LD H,imm */
H = FETCH; break;
case 0x2E: /* LD L,imm */
L = FETCH; break;
case 0x36: /* LD (HL),imm */
b = FETCH; writeb(xHL, b); break;
case 0x3E: /* LD A,imm */
A = FETCH; break;
case 0x08: /* LD (imm),SP */
writew(readw(xPC), SP); PC += 2; break;
case 0xEA: /* LD (imm),A */
writeb(readw(xPC), A); PC += 2; break;
case 0xE0: /* LDH (imm),A */
writehi(FETCH, A); break;
case 0xE2: /* LDH (C),A */
writehi(C, A); break;
case 0xF0: /* LDH A,(imm) */
A = readhi(FETCH); break;
case 0xF2: /* LDH A,(C) (undocumented) */
A = readhi(C); break;
case 0xF8: /* LD HL,SP+imm */
b = FETCH; LDHLSP(b); break;
case 0xF9: /* LD SP,HL */
SP = HL; break;
case 0xFA: /* LD A,(imm) */
A = readb(readw(xPC)); PC += 2; break;
ALU_CASES(0x80, 0xC6, ADD, __ADD)
ALU_CASES(0x88, 0xCE, ADC, __ADC)
ALU_CASES(0x90, 0xD6, SUB, __SUB)
ALU_CASES(0x98, 0xDE, SBC, __SBC)
ALU_CASES(0xA0, 0xE6, AND, __AND)
ALU_CASES(0xA8, 0xEE, XOR, __XOR)
ALU_CASES(0xB0, 0xF6, OR, __OR)
ALU_CASES(0xB8, 0xFE, CP, __CP)
case 0x09: /* ADD HL,BC */
w = BC; goto __ADDW;
case 0x19: /* ADD HL,DE */
w = DE; goto __ADDW;
case 0x39: /* ADD HL,SP */
w = SP; goto __ADDW;
case 0x29: /* ADD HL,HL */
w = HL;
__ADDW:
ADDW(w);
break;
case 0x04: /* INC B */
INC(B); break;
case 0x0C: /* INC C */
INC(C); break;
case 0x14: /* INC D */
INC(D); break;
case 0x1C: /* INC E */
INC(E); break;
case 0x24: /* INC H */
INC(H); break;
case 0x2C: /* INC L */
INC(L); break;
case 0x34: /* INC (HL) */
b = readb(xHL);
INC(b);
writeb(xHL, b);
break;
case 0x3C: /* INC A */
INC(A); break;
case 0x03: /* INC BC */
INCW(BC); break;
case 0x13: /* INC DE */
INCW(DE); break;
case 0x23: /* INC HL */
INCW(HL); break;
case 0x33: /* INC SP */
INCW(SP); break;
case 0x05: /* DEC B */
DEC(B); break;
case 0x0D: /* DEC C */
DEC(C); break;
case 0x15: /* DEC D */
DEC(D); break;
case 0x1D: /* DEC E */
DEC(E); break;
case 0x25: /* DEC H */
DEC(H); break;
case 0x2D: /* DEC L */
DEC(L); break;
case 0x35: /* DEC (HL) */
b = readb(xHL);
DEC(b);
writeb(xHL, b);
break;
case 0x3D: /* DEC A */
DEC(A); break;
case 0x0B: /* DEC BC */
DECW(BC); break;
case 0x1B: /* DEC DE */
DECW(DE); break;
case 0x2B: /* DEC HL */
DECW(HL); break;
case 0x3B: /* DEC SP */
DECW(SP); break;
case 0x07: /* RLCA */
RLCA(A); break;
case 0x0F: /* RRCA */
RRCA(A); break;
case 0x17: /* RLA */
RLA(A); break;
case 0x1F: /* RRA */
RRA(A); break;
case 0x27: /* DAA */
DAA; break;
case 0x2F: /* CPL */
CPL(A); break;
case 0x18: /* JR */
__JR:
JR; break;
case 0x20: /* JR NZ */
if (!(F&FZ)) goto __JR; NOJR; break;
case 0x28: /* JR Z */
if (F&FZ) goto __JR; NOJR; break;
case 0x30: /* JR NC */
if (!(F&FC)) goto __JR; NOJR; break;
case 0x38: /* JR C */
if (F&FC) goto __JR; NOJR; break;
case 0xC3: /* JP */
__JP:
JP; break;
case 0xC2: /* JP NZ */
if (!(F&FZ)) goto __JP; NOJP; break;
case 0xCA: /* JP Z */
if (F&FZ) goto __JP; NOJP; break;
case 0xD2: /* JP NC */
if (!(F&FC)) goto __JP; NOJP; break;
case 0xDA: /* JP C */
if (F&FC) goto __JP; NOJP; break;
case 0xE9: /* JP HL */
PC = HL; break;
case 0xC9: /* RET */
__RET:
RET; break;
case 0xC0: /* RET NZ */
if (!(F&FZ)) goto __RET; NORET; break;
case 0xC8: /* RET Z */
if (F&FZ) goto __RET; NORET; break;
case 0xD0: /* RET NC */
if (!(F&FC)) goto __RET; NORET; break;
case 0xD8: /* RET C */
if (F&FC) goto __RET; NORET; break;
case 0xD9: /* RETI */
IME = IMA = 1; goto __RET;
case 0xCD: /* CALL */
__CALL:
CALL; break;
case 0xC4: /* CALL NZ */
if (!(F&FZ)) goto __CALL; NOCALL; break;
case 0xCC: /* CALL Z */
if (F&FZ) goto __CALL; NOCALL; break;
case 0xD4: /* CALL NC */
if (!(F&FC)) goto __CALL; NOCALL; break;
case 0xDC: /* CALL C */
if (F&FC) goto __CALL; NOCALL; break;
case 0xC7: /* RST 0 */
b = 0x00; goto __RST;
case 0xCF: /* RST 8 */
b = 0x08; goto __RST;
case 0xD7: /* RST 10 */
b = 0x10; goto __RST;
case 0xDF: /* RST 18 */
b = 0x18; goto __RST;
case 0xE7: /* RST 20 */
b = 0x20; goto __RST;
case 0xEF: /* RST 28 */
b = 0x28; goto __RST;
case 0xF7: /* RST 30 */
b = 0x30; goto __RST;
case 0xFF: /* RST 38 */
b = 0x38;
__RST:
RST(b); break;
case 0xC1: /* POP BC */
POP(BC); break;
case 0xC5: /* PUSH BC */
PUSH(BC); break;
case 0xD1: /* POP DE */
POP(DE); break;
case 0xD5: /* PUSH DE */
PUSH(DE); break;
case 0xE1: /* POP HL */
POP(HL); break;
case 0xE5: /* PUSH HL */
PUSH(HL); break;
case 0xF1: /* POP AF */
POP(AF); break;
case 0xF5: /* PUSH AF */
PUSH(AF); break;
case 0xE8: /* ADD SP,imm */
b = FETCH; ADDSP(b); break;
case 0xF3: /* DI */
DI; break;
case 0xFB: /* EI */
EI; break;
case 0x37: /* SCF */
SCF; break;
case 0x3F: /* CCF */
CCF; break;
case 0x10: /* STOP */
PC++;
if (R_KEY1 & 1)
{
cpu.speed = cpu.speed ^ 1;
R_KEY1 = (R_KEY1 & 0x7E) | (cpu.speed << 7);
break;
}
/* NOTE - we do not implement dmg STOP whatsoever */
break;
case 0x76: /* HALT */
cpu.halt = 1;
break;
case 0xCB: /* CB prefix */
cbop = FETCH;
clen = cb_cycles_table[cbop];
switch (cbop)
{
CB_REG_CASES(B, 0);
CB_REG_CASES(C, 1);
CB_REG_CASES(D, 2);
CB_REG_CASES(E, 3);
CB_REG_CASES(H, 4);
CB_REG_CASES(L, 5);
CB_REG_CASES(A, 7);
default:
b = readb(xHL);
switch(cbop)
{
CB_REG_CASES(b, 6);
}
if ((cbop & 0xC0) != 0x40) /* exclude BIT */
writeb(xHL, b);
break;
}
break;
default:
die(
"invalid opcode 0x%02X at address 0x%04X, rombank = %d\n",
op, (PC-1) & 0xffff, mbc.rombank);
break;
}
clen <<= 1;
div_advance(clen);
timer_advance(clen);
clen >>= cpu.speed;
lcdc_advance(clen);
sound_advance(clen);
i -= clen;
if (i > 0) goto next;
return cycles-i;
}
#endif /* ASM_CPU_EMULATE */
#ifndef ASM_CPU_STEP
int cpu_step(int max)
{
int cnt;
if ((cnt = cpu_idle(max))) return cnt;
return cpu_emulate(1);
}
#endif /* ASM_CPU_STEP */