From 763c81a1e28f88a5dec94426daf38e80ff35728b Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Fri, 8 May 2015 14:48:44 +0200 Subject: [PATCH] Add CPU instructions. --- src/cpu.ml | 33 +++++++++++++++++++++++++++------ src/oboy.ml | 3 --- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/cpu.ml b/src/cpu.ml index 776e696..6948b18 100644 --- a/src/cpu.ml +++ b/src/cpu.ml @@ -97,7 +97,7 @@ let read_pc_byte cpu mem = let b = Memory.get mem cpu.reg.pc in inc_pc cpu 1; int_of_char b - + let read_pc_2bytes cpu mem = let word = read_2B mem cpu.reg.pc in inc_pc cpu 2; @@ -118,13 +118,26 @@ let run cpu (mem: Memory.map) = | '\x03' -> printf " INC \tBC\n"; inc_BC cpu; inc_cycles cpu 8 + + | '\x05' -> printf " DEC \tB\n"; + let dec = int_of_char(cpu.reg.b) - 1 in + cpu.flag.z <- dec = 0; + cpu.flag.n <- true; + cpu.flag.h <- dec < 0; + cpu.reg.b <- char_of_int @@ if dec >= 0 then dec else 0; + inc_cycles cpu 4 + + | '\x06' -> let n = read_pc_byte cpu mem in + printf " LD \tB, 0x%02X\n" n; + cpu.reg.b <- char_of_int n; + inc_cycles cpu 8 | '\x18' -> let n = read_pc_byte cpu mem in - printf " JP\t 0x%02X\n" n; + printf " JP \t0x%02X\n" n; inc_pc cpu (n - 1); inc_cycles cpu 12 | '\x20' -> let n = read_pc_byte cpu mem in - printf " JR\t NZ, 0x%02X\n" n; + printf " JR \tNZ, 0x%02X\n" n; if cpu.flag.z = false then begin inc_pc cpu (n - 1); inc_cycles cpu 12 @@ -134,15 +147,23 @@ let run cpu (mem: Memory.map) = end | '\x21' -> let nn = read_pc_2bytes cpu mem in - printf " LD\t HL, 0x%04X\n" nn; + printf " LD \tHL, 0x%04X\n" nn; let high, low = split_2B nn in cpu.reg.h <- high; cpu.reg.l <- low; inc_cycles cpu 12 + | '\x22' -> printf " LDI \t(HL), A\n"; + let hl = merge_bytes cpu.reg.l cpu.reg.h in + Memory.set mem hl cpu.reg.a; + let high, low = split_2B (hl + 1) in + cpu.reg.h <- high; + cpu.reg.l <- low; + inc_cycles cpu 8 + | '\x28' -> let n = read_pc_byte cpu mem in - printf " JR\t Z, 0x%02X\n" n; + printf " JR \tZ, 0x%02X\n" n; if cpu.flag.z = true then begin inc_pc cpu (n - 1); inc_cycles cpu 12 @@ -189,7 +210,7 @@ let run cpu (mem: Memory.map) = inc_cycles cpu 4 | '\xFE' -> let n = read_pc_byte cpu mem in - printf " CP\t0x%02X\n" n; + printf " CP \t0x%02X\n" n; cmp_A cpu n; inc_cycles cpu 8 diff --git a/src/oboy.ml b/src/oboy.ml index 9fe8855..33f74be 100644 --- a/src/oboy.ml +++ b/src/oboy.ml @@ -1,7 +1,6 @@ open Printf let rec run (cpu: Cpu.t) (mem: Memory.map) = - let command = read_line () in Cpu.run cpu mem; run cpu mem @@ -17,8 +16,6 @@ let power_up cartridge = let cpu = Cpu.init_cpu in let mem = Memory.init cartridge in - Graphics.open_graph ""; - Graphics.resize_window 256 256; run cpu mem