From a2d65de73befbdcf5d95f7b861d1847b3529c33c Mon Sep 17 00:00:00 2001 From: Matt Laux Date: Sat, 20 Apr 2019 19:01:21 -0500 Subject: [PATCH] some progress for today --- src/cpu.c | 41 ++++++++++++++++++++++++++++++++++++++--- src/dmg.c | 3 --- src/main.c | 2 +- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/cpu.c b/src/cpu.c index 4bcd951..4d40f06 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -157,12 +157,20 @@ static void rotate_right(struct cpu *regs, u8 *reg) *reg |= (regs->f & FLAG_CARRY) << 3; } +static void add8(struct cpu *cpu, u8 value) +{ + cpu->a += value; + clear_flag(cpu, FLAG_SIGN); + set_flag(cpu, cpu->a == 0 ? FLAG_ZERO : 0); + // TODO H, C +} + static void subtract(struct cpu *cpu, u8 value) { cpu->a -= value; - if (cpu->a > 0x80) { - set_flag(cpu, FLAG_SIGN); - } + set_flag(cpu, FLAG_SIGN); + set_flag(cpu, cpu->a == 0 ? FLAG_ZERO : 0); + // TODO H, C } static void xor(struct cpu *regs, u8 value) @@ -185,6 +193,17 @@ static void or(struct cpu *regs, u8 value) clear_flag(regs, FLAG_CARRY); } +static void and(struct cpu *cpu, u8 value) +{ + cpu->a &= value; + if(cpu->a == 0) { + set_flag(cpu, FLAG_ZERO); + } + clear_flag(cpu, FLAG_SIGN); + set_flag(cpu, FLAG_HALF_CARRY); + clear_flag(cpu, FLAG_CARRY); +} + static void push(struct cpu *cpu, u16 value) { write16(cpu, cpu->sp - 2, value & 0xff); @@ -268,8 +287,10 @@ static void extended_insn(struct cpu *cpu, u8 insn) set_flag(cpu, FLAG_HALF_CARRY); break; case 2: // RES + write_reg(cpu, reg, read_reg(cpu, reg) & ~(1 << bit)); break; case 3: // SET + write_reg(cpu, reg, read_reg(cpu, reg) | (1 << bit)); break; } } @@ -435,6 +456,9 @@ void cpu_step(struct cpu *cpu) case 0x29: // ADD HL,HL add16(cpu, read_hl(cpu)); break; + case 0x2f: // CPL + cpu->a = ~cpu->a; + break; case 0x31: // LD SP,d16 cpu->sp = read16(cpu, cpu->pc); cpu->pc += 2; @@ -464,6 +488,17 @@ void cpu_step(struct cpu *cpu) cpu->pc = read16(cpu, cpu->pc); break; + // AND + case 0xa0: and(cpu, cpu->b); break; + case 0xa1: and(cpu, cpu->c); break; + case 0xa2: and(cpu, cpu->d); break; + case 0xa3: and(cpu, cpu->e); break; + case 0xa4: and(cpu, cpu->h); break; + case 0xa5: and(cpu, cpu->l); break; + case 0xa6: and(cpu, read8(cpu, read_hl(cpu))); break; + case 0xa7: and(cpu, cpu->a); break; + case 0xe6: and(cpu, read8(cpu, cpu->pc)); cpu->pc++; break; + // OR case 0xb0: or(cpu, cpu->b); break; case 0xb1: or(cpu, cpu->c); break; diff --git a/src/dmg.c b/src/dmg.c index c619bfd..ca4f5ad 100644 --- a/src/dmg.c +++ b/src/dmg.c @@ -15,9 +15,6 @@ void dmg_new(struct dmg *dmg, struct cpu *cpu, struct rom *rom) u8 dmg_read(void *_dmg, u16 address) { struct dmg *dmg = (struct dmg *) _dmg; - if (address < 0x100) { - return dmg_boot_rom[address]; - } if (address < 0x4000) { return dmg->rom->data[address]; } else if (address < 0x8000) { diff --git a/src/main.c b/src/main.c index b84a8dc..997bb6a 100644 --- a/src/main.c +++ b/src/main.c @@ -26,7 +26,7 @@ int main(int argc, char *argv[]) dmg_new(&dmg, &cpu, &rom); cpu_bind_mem_model(&cpu, &dmg, dmg_read, dmg_write); - cpu.pc = 0; + cpu.pc = 0x100; while (1) { cpu_step(&cpu);