From c8b231d48a37b0ab0d0cc6a721826915ae778be3 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Wed, 20 May 2015 16:37:56 +0200 Subject: [PATCH] Add CPU instructions. --- src/cpu.ml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/cpu.ml b/src/cpu.ml index 6948b18..f67bf2a 100644 --- a/src/cpu.ml +++ b/src/cpu.ml @@ -112,6 +112,15 @@ let run cpu (mem: Memory.map) = let opcode = read_pc_byte cpu mem |> char_of_int in (* Hexa.print_slice cartridge.full_rom cpu.reg.pc (cpu.reg.pc + 7); *) match opcode with + + (* 8-bit load *) + (* LD r,(HL) *) + | '\x7E' -> printf " LD \tA, (HL)\n"; + let hl = merge_bytes cpu.reg.l cpu.reg.h in + cpu.reg.a <- Memory.get mem hl; + inc_cycles cpu 8; + + | '\x00' -> printf " NOP\n"; inc_cycles cpu 4 @@ -172,6 +181,15 @@ let run cpu (mem: Memory.map) = inc_cycles cpu 8 end + | '\x34' -> printf " INC \t(HL)\n"; + let hl = merge_bytes cpu.reg.l cpu.reg.h in + let v = Memory.get mem hl |> int_of_char in + cpu.flag.z <- v + 1 = 0; + cpu.flag.n <- false; + cpu.flag.h <- v = 0b00001111; + Memory.set mem hl (char_of_int (v + 1)); + inc_cycles cpu 12 + | '\x3E' -> let n = read_pc_byte cpu mem in printf " LD \tA, 0x%02X\n" n; @@ -200,6 +218,12 @@ let run cpu (mem: Memory.map) = Memory.set mem (0xFF00 + n) cpu.reg.a; inc_cycles cpu 12 + | '\xEA' -> let addr = read_pc_2bytes cpu mem in + printf " LD \t(0X%04X), A\n" addr; + Memory.set mem addr cpu.reg.a; + inc_cycles cpu 16 + + | '\xF0' -> let n = read_pc_byte cpu mem in printf " LDH \tA, (0xFF%02X)\n" n; cpu.reg.a <- Memory.get mem (0xFF00 + n); @@ -214,5 +238,10 @@ let run cpu (mem: Memory.map) = cmp_A cpu n; inc_cycles cpu 8 + | '\xFB' -> printf " EI\n"; + (* enable interrupts, IME=1 *) + inc_cycles cpu 4 + | x -> eprintf "opcode 0x%02X\n" (int_of_char x); + eprintf "#cycles: %d\n" cpu.cycles; failwith "Unimplemented opcode."