From c0262cba634348097555a54f753cd7329d39da95 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Tue, 28 Aug 2018 22:39:36 -0700 Subject: [PATCH] cpu: get blargh cpu test 3 passing --- gbdb_boot_test | 2 +- src/gbemu/cpu.c | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/gbdb_boot_test b/gbdb_boot_test index f52f7e1..2c21517 100644 --- a/gbdb_boot_test +++ b/gbdb_boot_test @@ -25,7 +25,7 @@ assert $hl == 0x9ffe step assert $pc == 10 -assert $f == 0x2 +assert $f == 0x20 step runto 0xc diff --git a/src/gbemu/cpu.c b/src/gbemu/cpu.c index 18fe0eb..f2f2f55 100644 --- a/src/gbemu/cpu.c +++ b/src/gbemu/cpu.c @@ -22,6 +22,10 @@ #define CALC_H_ADD(a, b) ((((a) & 0xf) + ((b) & 0xf)) > 0xf) #define CALC_H_SUB(a, b) (((a) & 0xf) < ((b) & 0xf)) +#define CARRY_MASK(idx) ((1 << idx) - 1) +#define ADD_CARRY(a, b, idx) \ + ((((a) & CARRY_MASK(idx)) + ((b) & CARRY_MASK(idx))) > CARRY_MASK(idx)) + #define CALC_C_ADD_16(a, b) (0xffff - (a) < (b)) #define CALC_C_ADD_8(a, b) (0xff - (a) < (b)) #define CALC_C_SUB(a, b) ((a) < (b)) @@ -400,8 +404,10 @@ int lr35902_cycle(struct lr35902_state *cpu) WRITE_BIT(cpu->a, 0, cpu->cf); break; case 0x08: /* LD (a16), SP */ - cpu->sp = gb_mem_read(cpu->memory, cpu->pc++); - cpu->sp |= gb_mem_read(cpu->memory, cpu->pc++) << 8; + val_16 = gb_mem_read(cpu->memory, cpu->pc++); + val_16 |= gb_mem_read(cpu->memory, cpu->pc++) << 8; + gb_mem_write(cpu->memory, val_16, cpu->sp & 0xff); + gb_mem_write(cpu->memory, val_16 + 1, (cpu->sp >> 8) & 0xff); break; case 0x09: /* ADD HL, BC */ ADD_16(cpu, cpu->hl, cpu->bc); @@ -1538,8 +1544,17 @@ int lr35902_cycle(struct lr35902_state *cpu) case 0xe7: /* RST 0x20 */ RST(cpu, 0x20); break; - case 0xe8: - ASSERT(0); + case 0xe8: /* ADD SP, r8 */ + val_16 = gb_mem_read(cpu->memory, cpu->pc++); + if (val_16 & 0x80) { + val_16 |= 0xff00; + } + cpu->cf = ADD_CARRY(cpu->sp, val_16, 8); + cpu->hf = ADD_CARRY(cpu->sp, val_16, 4); + cpu->zf = 0; + cpu->nf = 0; + cpu->sp += val_16; + break; case 0xe9: /* JP (hl) */ cpu->pc = cpu->hl; break; @@ -1597,9 +1612,19 @@ int lr35902_cycle(struct lr35902_state *cpu) case 0xf7: /* RST 0x30 */ RST(cpu, 0x30); break; - case 0xf8: - case 0xf9: - ASSERT(0); + case 0xf8: /* LD HL, SP+r8 */ + val_16 = gb_mem_read(cpu->memory, cpu->pc++); + if (val_16 & 0x80) { + val_16 |= 0xff00; + } + cpu->cf = ADD_CARRY(cpu->sp, val_16, 8); + cpu->hf = ADD_CARRY(cpu->sp, val_16, 4); + cpu->zf = 0; + cpu->nf = 0; + cpu->hl = cpu->sp + val_16; + break; + case 0xf9: /* LD SP, HL */ + cpu->sp = cpu->hl; break; case 0xfa: val_16 = gb_mem_read(cpu->memory, cpu->pc++); @@ -1607,7 +1632,7 @@ int lr35902_cycle(struct lr35902_state *cpu) cpu->a = gb_mem_read(cpu->memory, val_16); break; case 0xfb: /* EI */ - gb_log0("Interrupts ON\n"); + gb_log("Interrupts ON\n"); break; case 0xfc: /* UNDEF */ ASSERT(0);