diff --git a/cli/save.sav b/cli/save.sav new file mode 100644 index 0000000..aa3652c Binary files /dev/null and b/cli/save.sav differ diff --git a/src/cpu.c b/src/cpu.c index 2b75c19..88b4379 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -356,6 +356,32 @@ static void add16(struct cpu *cpu, u16 src) write_hl(cpu, trunc); } +static void add_sp(struct cpu *cpu, u8 value) +{ + int total = cpu->sp + (signed char) value; + clear_flag(cpu, FLAG_ZERO); + clear_flag(cpu, FLAG_SIGN); + if (total > 0xffff) { + set_flag(cpu, FLAG_CARRY); + } else { + clear_flag(cpu, FLAG_CARRY); + } + cpu->sp = (u16) total; +} + +static void ld_hl_sp(struct cpu *cpu, u8 value) +{ + int total = cpu->sp + (signed char) value; + clear_flag(cpu, FLAG_ZERO); + clear_flag(cpu, FLAG_SIGN); + if (total > 0xffff) { + set_flag(cpu, FLAG_CARRY); + } else { + clear_flag(cpu, FLAG_CARRY); + } + write_hl(cpu, total); +} + static u8 read_reg(struct cpu *cpu, int index) { switch (index) { @@ -1026,6 +1052,10 @@ void cpu_step(struct cpu *cpu) case 0xe5: // PUSH HL push(cpu, read_hl(cpu)); break; + case 0xe8: + add_sp(cpu, read8(cpu, cpu->pc)); + cpu->pc++; + break; case 0xe9: // JP HL cpu->pc = read_hl(cpu); break; @@ -1051,7 +1081,7 @@ void cpu_step(struct cpu *cpu) push(cpu, read_af(cpu)); break; case 0xf8: // LD HL, SP+i8 - write_hl(cpu, cpu->sp + (signed) read8(cpu, cpu->pc)); + ld_hl_sp(cpu, read8(cpu, cpu->pc)); cpu->pc++; break; case 0xf9: // LD SP, HL diff --git a/src/dmg.c b/src/dmg.c index b4f6b3d..e9d95b2 100644 --- a/src/dmg.c +++ b/src/dmg.c @@ -263,6 +263,7 @@ static void render_objs(struct dmg *dmg) static void timer_step(struct dmg *dmg) { dmg->timer_div++; + return; if (!(dmg_read(dmg, REG_TIMER_CONTROL) & TIMER_CONTROL_ENABLED)) { return; @@ -290,7 +291,7 @@ void dmg_step(void *_dmg) // order of dependencies? i think cpu needs to step first then update // all other hw cpu_step(dmg->cpu); - //timer_step(dmg); + timer_step(dmg); // each line takes 456 cycles int cycle_diff = dmg->cpu->cycle_count - dmg->last_lcd_update;