26 #define ZFLAG(n) ( (n) ? 0 : FZ )
27 #define HFLAG(n) ( (n) ? 0 : FH )
28 #define CFLAG(n) ( (n) ? 0 : FC )
31 #define PUSH(w) ( (SP -= 2), (writew(xSP, (w))) )
32 #define POP(w) ( ((w) = readw(xSP)), (SP += 2) )
35 #define FETCH_OLD ( mbc.rmap[PC>>12] \
36 ? mbc.rmap[PC>>12][PC++] \
39 #define FETCH (readb(PC++))
42 #define INC(r) { ((r)++); \
43 F = (F & (FL|FC)) | incflag_table[(r)]; }
45 #define DEC(r) { ((r)--); \
46 F = (F & (FL|FC)) | decflag_table[(r)]; }
48 #define INCW(r) ( (r)++ )
50 #define DECW(r) ( (r)-- )
53 W(acc) = (un16)A + (un16)(n); \
54 F = (ZFLAG(LB(acc))) \
55 | (FH & ((A ^ (n) ^ LB(acc)) << 1)) \
60 W(acc) = (un16)A + (un16)(n) + (un16)((F&FC)>>4); \
61 F = (ZFLAG(LB(acc))) \
62 | (FH & ((A ^ (n) ^ LB(acc)) << 1)) \
67 DW(acc) = (un32)HL + (un32)(n); \
69 | (FH & ((H ^ ((n)>>8) ^ HB(acc)) << 1)) \
70 | (acc.b[HI][LO] << 4); \
74 DW(acc) = (un32)SP + (un32)(n8)(n); \
75 F = (FH & (((SP>>8) ^ ((n)>>8) ^ HB(acc)) << 1)) \
76 | (acc.b[HI][LO] << 4); \
80 DW(acc) = (un32)SP + (un32)(n8)(n); \
81 F = (FH & (((SP>>8) ^ ((n)>>8) ^ HB(acc)) << 1)) \
82 | (acc.b[HI][LO] << 4); \
86 W(acc) = (un16)A - (un16)(n); \
89 | (FH & ((A ^ (n) ^ LB(acc)) << 1)) \
90 | ((un8)(-(n8)HB(acc)) << 4); }
92 #define SUB(n) { CP((n)); A = LB(acc); }
95 W(acc) = (un16)A - (un16)(n) - (un16)((F&FC)>>4); \
97 | (ZFLAG((n8)LB(acc))) \
98 | (FH & ((A ^ (n) ^ LB(acc)) << 1)) \
99 | ((un8)(-(n8)HB(acc)) << 4); \
102 #define AND(n) { A &= (n); \
105 #define XOR(n) { A ^= (n); \
108 #define OR(n) { A |= (n); \
111 #define RLCA(r) { (r) = ((r)>>7) | ((r)<<1); \
112 F = (((r)&0x01)<<4); }
114 #define RRCA(r) { (r) = ((r)<<7) | ((r)>>1); \
115 F = (((r)&0x80)>>3); }
118 LB(acc) = (((r)&0x80)>>3); \
119 (r) = ((r)<<1) | ((F&FC)>>4); \
123 LB(acc) = (((r)&0x01)<<4); \
124 (r) = ((r)>>1) | ((F&FC)<<3); \
127 #define RLC(r) { RLCA(r); F |= ZFLAG(r); }
128 #define RRC(r) { RRCA(r); F |= ZFLAG(r); }
129 #define RL(r) { RLA(r); F |= ZFLAG(r); }
130 #define RR(r) { RRA(r); F |= ZFLAG(r); }
133 LB(acc) = (((r)&0x80)>>3); \
135 F = ZFLAG((r)) | LB(acc); }
138 LB(acc) = (((r)&0x01)<<4); \
139 (r) = (un8)(((n8)(r))>>1); \
140 F = ZFLAG((r)) | LB(acc); }
143 LB(acc) = (((r)&0x01)<<4); \
145 F = ZFLAG((r)) | LB(acc); }
151 #define SCF { F = (F & (FZ)) | FC; }
153 #define CCF { F = (F & (FZ|FC)) ^ FC; }
156 A += (LB(acc) = daa_table[((((int)F)&0x70)<<4) | A]); \
157 F = (F & (FN)) | ZFLAG(A) | daa_carry_table[LB(acc)>>2]; }
160 (r) = swap_table[(r)]; \
163 #define BIT(n,r) { F = (F & FC) | ZFLAG(((r) & (1 << (n)))) | FH; }
164 #define RES(n,r) { (r) &= ~(1 << (n)); }
165 #define SET(n,r) { (r) |= (1 << (n)); }
167 #define CB_REG_CASES(r, n) \
168 case 0x00|(n): RLC(r); break; \
169 case 0x08|(n): RRC(r); break; \
170 case 0x10|(n): RL(r); break; \
171 case 0x18|(n): RR(r); break; \
172 case 0x20|(n): SLA(r); break; \
173 case 0x28|(n): SRA(r); break; \
174 case 0x30|(n): SWAP(r); break; \
175 case 0x38|(n): SRL(r); break; \
176 case 0x40|(n): BIT(0, r); break; \
177 case 0x48|(n): BIT(1, r); break; \
178 case 0x50|(n): BIT(2, r); break; \
179 case 0x58|(n): BIT(3, r); break; \
180 case 0x60|(n): BIT(4, r); break; \
181 case 0x68|(n): BIT(5, r); break; \
182 case 0x70|(n): BIT(6, r); break; \
183 case 0x78|(n): BIT(7, r); break; \
184 case 0x80|(n): RES(0, r); break; \
185 case 0x88|(n): RES(1, r); break; \
186 case 0x90|(n): RES(2, r); break; \
187 case 0x98|(n): RES(3, r); break; \
188 case 0xA0|(n): RES(4, r); break; \
189 case 0xA8|(n): RES(5, r); break; \
190 case 0xB0|(n): RES(6, r); break; \
191 case 0xB8|(n): RES(7, r); break; \
192 case 0xC0|(n): SET(0, r); break; \
193 case 0xC8|(n): SET(1, r); break; \
194 case 0xD0|(n): SET(2, r); break; \
195 case 0xD8|(n): SET(3, r); break; \
196 case 0xE0|(n): SET(4, r); break; \
197 case 0xE8|(n): SET(5, r); break; \
198 case 0xF0|(n): SET(6, r); break; \
199 case 0xF8|(n): SET(7, r); break;
202 #define ALU_CASES(base, imm, op, label) \
203 case (imm): b = FETCH; goto label; \
204 case (base): b = B; goto label; \
205 case (base)+1: b = C; goto label; \
206 case (base)+2: b = D; goto label; \
207 case (base)+3: b = E; goto label; \
208 case (base)+4: b = H; goto label; \
209 case (base)+5: b = L; goto label; \
210 case (base)+6: b = readb(HL); goto label; \
211 case (base)+7: b = A; \
221 #define JR ( PC += 1+(n8)readb(PC) )
222 #define JP ( PC = readw(PC) )
224 #define CALL ( PUSH(PC+2), JP )
226 #define NOJR ( clen--, PC++ )
227 #define NOJP ( clen--, PC+=2 )
228 #define NOCALL ( clen-=3, PC+=2 )
229 #define NORET ( clen-=3 )
231 #define RST(n) { PUSH(PC); PC = (n); }
233 #define RET ( POP(PC) )
235 #define EI ( IMA = 1 )
236 #define DI ( cpu.halt = IMA = IME = 0 )
240 #define PRE_INT ( DI, PUSH(PC) )
241 #define THROW_INT(n) ( (IF &= ~(1<<(n))), (PC = 0x40+((n)<<3)) )
257 PC
= bootrom
.bank
? 0 : 0x0100;
264 if (hw
.cgb
) A
= 0x11;
265 if (hw
.gba
) B
= 0x01;
269 void div_advance(int cnt
)
274 R_DIV
+= (cpu
.div
>> 8);
279 void timer_advance(int cnt
)
283 if (!(R_TAC
& 0x04)) return;
285 unit
= ((-R_TAC
) & 3) << 1;
286 cpu
.tim
+= (cnt
<<unit
);
290 tima
= R_TIMA
+ (cpu
.tim
>> 9);
294 hw_interrupt(IF_TIMER
, IF_TIMER
);
295 hw_interrupt(0, IF_TIMER
);
298 tima
= tima
- 256 + R_TMA
;
303 void lcdc_advance(int cnt
)
306 if (cpu
.lcdc
<= 0) lcdc_trans();
309 void sound_advance(int cnt
)
314 void cpu_timers(int cnt
)
316 div_advance(cnt
<< cpu
.speed
);
317 timer_advance(cnt
<< cpu
.speed
);
322 int cpu_idle(int max
)
326 if (!(cpu
.halt
&& IME
)) return 0;
333 /* Make sure we don't miss lcdc status events! */
334 if ((R_IE
& (IF_VBLANK
| IF_STAT
)) && (max
> cpu
.lcdc
))
337 /* If timer interrupt cannot happen, this is very simple! */
338 if (!((R_IE
& IF_TIMER
) && (R_TAC
& 0x04)))
344 /* Figure out when the next timer interrupt will happen */
345 unit
= ((-R_TAC
) & 3) << 1;
346 cnt
= (511 - cpu
.tim
+ (1<<unit
)) >> unit
;
347 cnt
+= (255 - R_TIMA
) << (9 - unit
);
356 #ifndef ASM_CPU_EMULATE
358 extern int debug_trace
;
360 int cpu_emulate(int cycles
)
365 static union reg acc
;
371 if ((clen
= cpu_idle(i
)))
374 if (i
> 0) goto next
;
378 if (IME
&& (IF
& IE
))
381 switch ((byte
)(IF
& IE
))
383 case 0x01: case 0x03: case 0x05: case 0x07:
384 case 0x09: case 0x0B: case 0x0D: case 0x0F:
385 case 0x11: case 0x13: case 0x15: case 0x17:
386 case 0x19: case 0x1B: case 0x1D: case 0x1F:
388 case 0x02: case 0x06: case 0x0A: case 0x0E:
389 case 0x12: case 0x16: case 0x1A: case 0x1E:
391 case 0x04: case 0x0C: case 0x14: case 0x1C:
393 case 0x08: case 0x18:
401 if (debug_trace
) debug_disassemble(PC
, 1);
403 clen
= cycles_table
[op
];
408 case 0x40: /* LD B,B */
409 case 0x49: /* LD C,C */
410 case 0x52: /* LD D,D */
411 case 0x5B: /* LD E,E */
412 case 0x64: /* LD H,H */
413 case 0x6D: /* LD L,L */
414 case 0x7F: /* LD A,A */
417 case 0x41: /* LD B,C */
419 case 0x42: /* LD B,D */
421 case 0x43: /* LD B,E */
423 case 0x44: /* LD B,H */
425 case 0x45: /* LD B,L */
427 case 0x46: /* LD B,(HL) */
428 B
= readb(xHL
); break;
429 case 0x47: /* LD B,A */
432 case 0x48: /* LD C,B */
434 case 0x4A: /* LD C,D */
436 case 0x4B: /* LD C,E */
438 case 0x4C: /* LD C,H */
440 case 0x4D: /* LD C,L */
442 case 0x4E: /* LD C,(HL) */
443 C
= readb(xHL
); break;
444 case 0x4F: /* LD C,A */
447 case 0x50: /* LD D,B */
449 case 0x51: /* LD D,C */
451 case 0x53: /* LD D,E */
453 case 0x54: /* LD D,H */
455 case 0x55: /* LD D,L */
457 case 0x56: /* LD D,(HL) */
458 D
= readb(xHL
); break;
459 case 0x57: /* LD D,A */
462 case 0x58: /* LD E,B */
464 case 0x59: /* LD E,C */
466 case 0x5A: /* LD E,D */
468 case 0x5C: /* LD E,H */
470 case 0x5D: /* LD E,L */
472 case 0x5E: /* LD E,(HL) */
473 E
= readb(xHL
); break;
474 case 0x5F: /* LD E,A */
477 case 0x60: /* LD H,B */
479 case 0x61: /* LD H,C */
481 case 0x62: /* LD H,D */
483 case 0x63: /* LD H,E */
485 case 0x65: /* LD H,L */
487 case 0x66: /* LD H,(HL) */
488 H
= readb(xHL
); break;
489 case 0x67: /* LD H,A */
492 case 0x68: /* LD L,B */
494 case 0x69: /* LD L,C */
496 case 0x6A: /* LD L,D */
498 case 0x6B: /* LD L,E */
500 case 0x6C: /* LD L,H */
502 case 0x6E: /* LD L,(HL) */
503 L
= readb(xHL
); break;
504 case 0x6F: /* LD L,A */
507 case 0x70: /* LD (HL),B */
509 case 0x71: /* LD (HL),C */
511 case 0x72: /* LD (HL),D */
513 case 0x73: /* LD (HL),E */
515 case 0x74: /* LD (HL),H */
517 case 0x75: /* LD (HL),L */
519 case 0x77: /* LD (HL),A */
525 case 0x78: /* LD A,B */
527 case 0x79: /* LD A,C */
529 case 0x7A: /* LD A,D */
531 case 0x7B: /* LD A,E */
533 case 0x7C: /* LD A,H */
535 case 0x7D: /* LD A,L */
537 case 0x7E: /* LD A,(HL) */
538 A
= readb(xHL
); break;
540 case 0x01: /* LD BC,imm */
541 BC
= readw(xPC
); PC
+= 2; break;
542 case 0x11: /* LD DE,imm */
543 DE
= readw(xPC
); PC
+= 2; break;
544 case 0x21: /* LD HL,imm */
545 HL
= readw(xPC
); PC
+= 2; break;
546 case 0x31: /* LD SP,imm */
547 SP
= readw(xPC
); PC
+= 2; break;
549 case 0x02: /* LD (BC),A */
550 writeb(xBC
, A
); break;
551 case 0x0A: /* LD A,(BC) */
552 A
= readb(xBC
); break;
553 case 0x12: /* LD (DE),A */
554 writeb(xDE
, A
); break;
555 case 0x1A: /* LD A,(DE) */
556 A
= readb(xDE
); break;
558 case 0x22: /* LDI (HL),A */
559 writeb(xHL
, A
); HL
++; break;
560 case 0x2A: /* LDI A,(HL) */
561 A
= readb(xHL
); HL
++; break;
562 case 0x32: /* LDD (HL),A */
563 writeb(xHL
, A
); HL
--; break;
564 case 0x3A: /* LDD A,(HL) */
565 A
= readb(xHL
); HL
--; break;
567 case 0x06: /* LD B,imm */
569 case 0x0E: /* LD C,imm */
571 case 0x16: /* LD D,imm */
573 case 0x1E: /* LD E,imm */
575 case 0x26: /* LD H,imm */
577 case 0x2E: /* LD L,imm */
579 case 0x36: /* LD (HL),imm */
580 b
= FETCH
; writeb(xHL
, b
); break;
581 case 0x3E: /* LD A,imm */
584 case 0x08: /* LD (imm),SP */
585 writew(readw(xPC
), SP
); PC
+= 2; break;
586 case 0xEA: /* LD (imm),A */
587 writeb(readw(xPC
), A
); PC
+= 2; break;
589 case 0xE0: /* LDH (imm),A */
590 writehi(FETCH
, A
); break;
591 case 0xE2: /* LDH (C),A */
592 writehi(C
, A
); break;
593 case 0xF0: /* LDH A,(imm) */
594 A
= readhi(FETCH
); break;
595 case 0xF2: /* LDH A,(C) (undocumented) */
596 A
= readhi(C
); break;
599 case 0xF8: /* LD HL,SP+imm */
601 /* https://gammpei.github.io/blog/posts/2018-03-04/how-to-write-a-game-boy-emulator-part-8-blarggs-cpu-test-roms-1-3-4-5-7-8-9-10-11.html */
602 signed char v
= (signed char) FETCH
;
603 int temp
= (int)(SP
) + (int)v
;
605 byte half_carry
= ((SP
& 0xff) ^ v
^ temp
) & 0x10;
607 F
&= ~(FZ
| FN
| FH
| FC
);
609 if (half_carry
) F
|= FH
;
610 if ((SP
& 0xff) + (byte
)v
> 0xff) F
|= FC
;
615 case 0xF9: /* LD SP,HL */
617 case 0xFA: /* LD A,(imm) */
618 A
= readb(readw(xPC
)); PC
+= 2; break;
620 ALU_CASES(0x80, 0xC6, ADD
, __ADD
)
621 ALU_CASES(0x88, 0xCE, ADC
, __ADC
)
622 ALU_CASES(0x90, 0xD6, SUB
, __SUB
)
623 ALU_CASES(0x98, 0xDE, SBC
, __SBC
)
624 ALU_CASES(0xA0, 0xE6, AND
, __AND
)
625 ALU_CASES(0xA8, 0xEE, XOR
, __XOR
)
626 ALU_CASES(0xB0, 0xF6, OR
, __OR
)
627 ALU_CASES(0xB8, 0xFE, CP
, __CP
)
629 case 0x09: /* ADD HL,BC */
631 case 0x19: /* ADD HL,DE */
633 case 0x39: /* ADD HL,SP */
635 case 0x29: /* ADD HL,HL */
641 case 0x04: /* INC B */
643 case 0x0C: /* INC C */
645 case 0x14: /* INC D */
647 case 0x1C: /* INC E */
649 case 0x24: /* INC H */
651 case 0x2C: /* INC L */
653 case 0x34: /* INC (HL) */
658 case 0x3C: /* INC A */
661 case 0x03: /* INC BC */
663 case 0x13: /* INC DE */
665 case 0x23: /* INC HL */
667 case 0x33: /* INC SP */
670 case 0x05: /* DEC B */
672 case 0x0D: /* DEC C */
674 case 0x15: /* DEC D */
676 case 0x1D: /* DEC E */
678 case 0x25: /* DEC H */
680 case 0x2D: /* DEC L */
682 case 0x35: /* DEC (HL) */
687 case 0x3D: /* DEC A */
690 case 0x0B: /* DEC BC */
692 case 0x1B: /* DEC DE */
694 case 0x2B: /* DEC HL */
696 case 0x3B: /* DEC SP */
699 case 0x07: /* RLCA */
701 case 0x0F: /* RRCA */
713 if ((F
& FH
) || ((a
& 0x0f) > 9)) a
+= 0x06;
715 if ((F
& FC
) || (a
> 0x9f)) a
+= 0x60;
719 if (F
& FH
) a
= (a
- 6) & 0xff;
720 if (F
& FC
) a
-= 0x60;
725 if (a
& 0x100) F
|= FC
;
740 case 0x20: /* JR NZ */
741 if (!(F
&FZ
)) goto __JR
; NOJR
; break;
742 case 0x28: /* JR Z */
743 if (F
&FZ
) goto __JR
; NOJR
; break;
744 case 0x30: /* JR NC */
745 if (!(F
&FC
)) goto __JR
; NOJR
; break;
746 case 0x38: /* JR C */
747 if (F
&FC
) goto __JR
; NOJR
; break;
752 case 0xC2: /* JP NZ */
753 if (!(F
&FZ
)) goto __JP
; NOJP
; break;
754 case 0xCA: /* JP Z */
755 if (F
&FZ
) goto __JP
; NOJP
; break;
756 case 0xD2: /* JP NC */
757 if (!(F
&FC
)) goto __JP
; NOJP
; break;
758 case 0xDA: /* JP C */
759 if (F
&FC
) goto __JP
; NOJP
; break;
760 case 0xE9: /* JP HL */
766 case 0xC0: /* RET NZ */
767 if (!(F
&FZ
)) goto __RET
; NORET
; break;
768 case 0xC8: /* RET Z */
769 if (F
&FZ
) goto __RET
; NORET
; break;
770 case 0xD0: /* RET NC */
771 if (!(F
&FC
)) goto __RET
; NORET
; break;
772 case 0xD8: /* RET C */
773 if (F
&FC
) goto __RET
; NORET
; break;
774 case 0xD9: /* RETI */
775 IME
= IMA
= 1; goto __RET
;
777 case 0xCD: /* CALL */
780 case 0xC4: /* CALL NZ */
781 if (!(F
&FZ
)) goto __CALL
; NOCALL
; break;
782 case 0xCC: /* CALL Z */
783 if (F
&FZ
) goto __CALL
; NOCALL
; break;
784 case 0xD4: /* CALL NC */
785 if (!(F
&FC
)) goto __CALL
; NOCALL
; break;
786 case 0xDC: /* CALL C */
787 if (F
&FC
) goto __CALL
; NOCALL
; break;
789 case 0xC7: /* RST 0 */
790 b
= 0x00; goto __RST
;
791 case 0xCF: /* RST 8 */
792 b
= 0x08; goto __RST
;
793 case 0xD7: /* RST 10 */
794 b
= 0x10; goto __RST
;
795 case 0xDF: /* RST 18 */
796 b
= 0x18; goto __RST
;
797 case 0xE7: /* RST 20 */
798 b
= 0x20; goto __RST
;
799 case 0xEF: /* RST 28 */
800 b
= 0x28; goto __RST
;
801 case 0xF7: /* RST 30 */
802 b
= 0x30; goto __RST
;
803 case 0xFF: /* RST 38 */
808 case 0xC1: /* POP BC */
810 case 0xC5: /* PUSH BC */
812 case 0xD1: /* POP DE */
814 case 0xD5: /* PUSH DE */
816 case 0xE1: /* POP HL */
818 case 0xE5: /* PUSH HL */
820 case 0xF1: /* POP AF */
821 POP(AF
); AF
&= 0xfff0; break;
822 case 0xF5: /* PUSH AF */
825 case 0xE8: /* ADD SP,imm */
827 /* https://gammpei.github.io/blog/posts/2018-03-04/how-to-write-a-game-boy-emulator-part-8-blarggs-cpu-test-roms-1-3-4-5-7-8-9-10-11.html */
828 signed char v
= (signed char) FETCH
;
829 int temp
= (int)(SP
) + (int)v
;
831 byte half_carry
= ((SP
& 0xff) ^ v
^ temp
) & 0x10;
833 F
&= ~(FZ
| FN
| FH
| FC
);
835 if (half_carry
) F
|= FH
;
836 if ((SP
& 0xff) + (byte
)v
> 0xff) F
|= FC
;
853 case 0x10: /* STOP */
857 cpu
.speed
= cpu
.speed
^ 1;
858 R_KEY1
= (R_KEY1
& 0x7E) | (cpu
.speed
<< 7);
861 /* NOTE - we do not implement dmg STOP whatsoever */
864 case 0x76: /* HALT */
868 case 0xCB: /* CB prefix */
870 clen
= cb_cycles_table
[cbop
];
886 if ((cbop
& 0xC0) != 0x40) /* exclude BIT */
894 "invalid opcode 0x%02X at address 0x%04X, rombank = %d\n",
895 op
, (PC
-1) & 0xffff, mbc
.rombank
);
907 if (i
> 0) goto next
;
911 #endif /* ASM_CPU_EMULATE */
916 int cpu_step(int max
)
919 if ((cnt
= cpu_idle(max
))) return cnt
;
920 return cpu_emulate(1);
923 #endif /* ASM_CPU_STEP */