From b0c690c6938a46d3a2511a2cb3e2c90aa0c6f36b Mon Sep 17 00:00:00 2001 From: Matthew Laux Date: Mon, 20 Jun 2022 19:46:32 -0500 Subject: [PATCH] update lcd every 456 cycles --- cli/emulator.c | 3 ++- src/cpu.c | 15 ++++++++++----- src/dmg.c | 4 ++-- src/lcd.c | 1 + 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/cli/emulator.c b/cli/emulator.c index 402245d..92e2e3b 100644 --- a/cli/emulator.c +++ b/cli/emulator.c @@ -32,7 +32,8 @@ int main(int argc, char *argv[]) cpu.pc = 0; - for (executed = 0; executed < 100000; executed++) { + // for (executed = 0; executed < 100000; executed++) { + while (1) { dmg_step(&dmg); } diff --git a/src/cpu.c b/src/cpu.c index 5970285..7b5c6ae 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -300,6 +300,9 @@ static void extended_insn(struct cpu *cpu, u8 insn) shift_right // TODO SRL }; + printf(" %s\n", instructions[insn + 0x100].format); + cpu->cycle_count += instructions[insn + 0x100].cycles; + switch (op) { case 0: write_reg(cpu, reg, funcs[bit](cpu, read_reg(cpu, reg))); @@ -330,6 +333,7 @@ void cpu_step(struct cpu *cpu) u8 opc = cpu->mem_read(cpu->mem_model, cpu->pc); printf("0x%04x %s\n", cpu->pc, instructions[opc].format); cpu->pc++; + cpu->cycle_count += instructions[opc].cycles; switch (opc) { case 0: // NOP break; @@ -500,6 +504,7 @@ void cpu_step(struct cpu *cpu) temp = read8(cpu, cpu->pc); if ((opc == 0x20) ^ flag_isset(cpu, FLAG_ZERO)) { cpu->pc += *((signed char *) &temp) + 1; + cpu->cycle_count += instructions[opc].cycles_branch - instructions[opc].cycles; } else { cpu->pc++; } @@ -650,19 +655,19 @@ void cpu_step(struct cpu *cpu) add(cpu, read8(cpu, cpu->pc), 1); cpu->pc++; break; - case 0xe0: // LDH (a8),A - write16(cpu, 0xff00 + read8(cpu, cpu->pc), cpu->a); + case 0xe0: // LD (a8),A + write8(cpu, 0xff00 + read8(cpu, cpu->pc), cpu->a); cpu->pc++; break; case 0xe2: // LD (C),A write8(cpu, 0xff00 + cpu->c, cpu->a); break; case 0xea: // LD (a16),A - write16(cpu, read16(cpu, cpu->pc), cpu->a); + write8(cpu, read16(cpu, cpu->pc), cpu->a); cpu->pc += 2; break; - case 0xf0: // LDH A,(a8) - cpu->a = read16(cpu, 0xff00 + read8(cpu, cpu->pc)); + case 0xf0: // LD A,(a8) + cpu->a = read8(cpu, 0xff00 + read8(cpu, cpu->pc)); cpu->pc++; break; case 0xf2: // LD A,(C) diff --git a/src/dmg.c b/src/dmg.c index 8dca22d..3794907 100644 --- a/src/dmg.c +++ b/src/dmg.c @@ -75,8 +75,8 @@ void dmg_step(void *_dmg) // all other hw cpu_step(dmg->cpu); - if (dmg->cpu->pc % 456 == 0) { - // each line takes 456 cycles + // each line takes 456 cycles + if (dmg->cpu->cycle_count % 456 == 0) { lcd_step(dmg->lcd); } } \ No newline at end of file diff --git a/src/lcd.c b/src/lcd.c index d482d1f..21b682e 100644 --- a/src/lcd.c +++ b/src/lcd.c @@ -49,4 +49,5 @@ void lcd_step(struct lcd *lcd) // step to next scanline 0-153 u8 next_scanline = (lcd_read(lcd, REG_LY) + 1) % 154; lcd_write(lcd, REG_LY, next_scanline); + // printf("update lcd %d\n", next_scanline); } \ No newline at end of file