diff --git a/cli/emulator.c b/cli/emulator.c index 929ee2b..9f680b3 100644 --- a/cli/emulator.c +++ b/cli/emulator.c @@ -32,8 +32,8 @@ int main(int argc, char *argv[]) cpu.pc = 0; - for (executed = 0; executed < 100000; executed++) { - cpu_step(&cpu); + for (executed = 0; executed < 1000000; executed++) { + dmg_step(&dmg); } rom_free(&rom); diff --git a/src/cpu.c b/src/cpu.c index 7c8b0c2..842d626 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -125,6 +125,7 @@ static void inc_with_carry(struct cpu *regs, u8 *reg) (*reg)++; if(*reg == 0) set_flag(regs, FLAG_ZERO); + else clear_flag(regs, FLAG_ZERO); } static void dec_with_carry(struct cpu *regs, u8 *reg) @@ -135,6 +136,7 @@ static void dec_with_carry(struct cpu *regs, u8 *reg) (*reg)--; if(*reg == 0) set_flag(regs, FLAG_ZERO); + else clear_flag(regs, FLAG_ZERO); } static u8 rotate_left(struct cpu *regs, u8 reg) @@ -179,6 +181,7 @@ static void xor(struct cpu *regs, u8 value) regs->a ^= value; if(regs->a == 0) set_flag(regs, FLAG_ZERO); + else clear_flag(regs, FLAG_ZERO); clear_flag(regs, FLAG_SIGN); clear_flag(regs, FLAG_HALF_CARRY); clear_flag(regs, FLAG_CARRY); @@ -189,6 +192,7 @@ static void or(struct cpu *regs, u8 value) regs->a |= value; if(regs->a == 0) set_flag(regs, FLAG_ZERO); + else clear_flag(regs, FLAG_ZERO); clear_flag(regs, FLAG_SIGN); clear_flag(regs, FLAG_HALF_CARRY); clear_flag(regs, FLAG_CARRY); @@ -199,6 +203,8 @@ static void and(struct cpu *cpu, u8 value) cpu->a &= value; if(cpu->a == 0) { set_flag(cpu, FLAG_ZERO); + } else { + clear_flag(cpu, FLAG_ZERO); } clear_flag(cpu, FLAG_SIGN); set_flag(cpu, FLAG_HALF_CARRY); @@ -650,6 +656,7 @@ void cpu_step(struct cpu *cpu) case 0xbd: subtract(cpu, cpu->l, 0, 1); break; case 0xbe: subtract(cpu, read8(cpu, read_hl(cpu)), 0, 1); break; case 0xbf: subtract(cpu, cpu->a, 0, 1); break; + case 0xfe: subtract(cpu, read8(cpu, cpu->pc), 0, 1); cpu->pc++; break; // RST case 0xc7: push(cpu, cpu->pc); cpu->pc = 0x00; break; @@ -689,11 +696,7 @@ void cpu_step(struct cpu *cpu) case 0xf0: // LDH A,(a8) cpu->a = read16(cpu, 0xff00 + read8(cpu, cpu->pc)); cpu->pc++; - case 0xfe: // CP d8 - if (cpu->a - read8(cpu, cpu->pc) > 0x80) { - set_flag(cpu, FLAG_SIGN); - } - cpu->pc++; + printf("scanline was %d\n", cpu->a); break; case 0xf2: // LD A,(C) cpu->a = read8(cpu, 0xff00 + cpu->c); diff --git a/src/dmg.c b/src/dmg.c index 02e45ed..df4e439 100644 --- a/src/dmg.c +++ b/src/dmg.c @@ -19,8 +19,7 @@ u8 dmg_read(void *_dmg, u16 address) struct dmg *dmg = (struct dmg *) _dmg; if (address < 0x100) { return dmg_boot_rom[address]; - } - if (address < 0x4000) { + } else if (address < 0x4000) { return dmg->rom->data[address]; } else if (address < 0x8000) { // TODO switchable rom bank diff --git a/src/lcd.c b/src/lcd.c index 0acaf2d..d482d1f 100644 --- a/src/lcd.c +++ b/src/lcd.c @@ -25,10 +25,6 @@ u8 lcd_read(struct lcd *lcd, u16 addr) void lcd_write(struct lcd *lcd, u16 addr, u8 value) { - if (addr == REG_LY) { - // writing to this register always resets it - value = 0; - } lcd->regs[addr - REG_LCD_BASE] = value; }