From b3ca33a8147ed5bd9112a9f0bf4961789cf896de Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 23 Mar 2015 17:34:22 +0100 Subject: [PATCH] Automatically increment PC register. --- src/cpu.ml | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/cpu.ml b/src/cpu.ml index 861e2e2..294a767 100644 --- a/src/cpu.ml +++ b/src/cpu.ml @@ -95,44 +95,56 @@ let cmp_A cpu n = (** http://imrannazar.com/GameBoy-Z80-Opcode-Map *) let run cpu (mem: Memory.map) = - let n = Memory.get mem (cpu.reg.pc + 1) |> int_of_char in + let opcode = Memory.get mem cpu.reg.pc in + inc_pc cpu 1; + let n = Memory.get mem cpu.reg.pc |> int_of_char in (* let nn = Memory.get mem (cpu.reg.pc + 2) in *) (* Hexa.print_slice cartridge.full_rom cpu.reg.pc (cpu.reg.pc + 7); *) - match Memory.get mem cpu.reg.pc with + match opcode with | '\x00' -> printf " NOP\n"; - inc_pc cpu 1; inc_cycles cpu 4 + inc_cycles cpu 4 | '\x03' -> printf " INC \tBC\n"; inc_BC cpu; - inc_pc cpu 1; inc_cycles cpu 8 + inc_cycles cpu 8 + + | '\x20' -> printf " JR\t NZ, 0x%02X\n" n; + if cpu.flag.z = false then + begin + inc_pc cpu n; inc_cycles cpu 12 + end else + begin + inc_pc cpu 1; inc_cycles cpu 8 + end | '\x3E' -> printf " LD \tA, 0x%02X\n" n; cpu.reg.a <- char_of_int(n); - inc_pc cpu 2; inc_cycles cpu 8 + inc_pc cpu 1; inc_cycles cpu 8 | '\xAF' -> printf " XOR \tA, A\n"; let int_A = int_of_char cpu.reg.a in cpu.reg.a <- char_of_int @@ int_A lxor int_A; - inc_pc cpu 1; inc_cycles cpu 4 + inc_cycles cpu 4 - | '\xC3' -> let addr = read_2B mem (cpu.reg.pc + 1) in + | '\xC3' -> let addr = read_2B mem cpu.reg.pc in printf " JP \t0x%04X\n" addr; cpu.reg.pc <- addr; inc_cycles cpu 16 | '\xE0' -> printf " LDH \t(0xFF%02X), A\n" n; Memory.set mem (0xFF00 + n) cpu.reg.a; - inc_pc cpu 2; inc_cycles cpu 12 + inc_pc cpu 1; inc_cycles cpu 12 | '\xF0' -> printf " LDH \tA, (0xFF%02X)\n" n; cpu.reg.a <- Memory.get mem (0xFF00 + n); - inc_pc cpu 2; inc_cycles cpu 12 + inc_pc cpu 1; inc_cycles cpu 12 | '\xF3' -> printf " DI\n"; (* fixme *) - inc_pc cpu 1; inc_cycles cpu 4 + inc_cycles cpu 4 | '\xFE' -> printf " CP\t0x%02X\n" n; cmp_A cpu n; - inc_pc cpu 2; inc_cycles cpu 8 + inc_pc cpu 1; inc_cycles cpu 8 - | _ as x -> eprintf "opcode %02X\n" (int_of_char x); failwith "Unimplemented opcode." + | _ as x -> eprintf "opcode %02X\n" (int_of_char x); + failwith "Unimplemented opcode."