Add more opcodes.

master
Fabien Freling 2015-03-23 22:38:11 +01:00
parent b3ca33a814
commit b611dfdd49
2 changed files with 38 additions and 5 deletions

View File

@ -93,12 +93,15 @@ let cmp_A cpu n =
cpu.flag.c <- diff > 0xFF || diff < 0;
update_flag_reg cpu
(** http://imrannazar.com/GameBoy-Z80-Opcode-Map *)
(**
http://imrannazar.com/GameBoy-Z80-Opcode-Map
https://github.com/sinamas/gambatte/blob/master/libgambatte/src/cpu.cpp
*)
let run cpu (mem: Memory.map) =
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 *)
let nn = Memory.get mem (cpu.reg.pc + 1) |> int_of_char in
(* Hexa.print_slice cartridge.full_rom cpu.reg.pc (cpu.reg.pc + 7); *)
match opcode with
| '\x00' -> printf " NOP\n";
@ -108,6 +111,9 @@ let run cpu (mem: Memory.map) =
inc_BC cpu;
inc_cycles cpu 8
| '\x18' -> printf " JP\t 0x%02X\n" n;
inc_pc cpu n; inc_cycles cpu 12
| '\x20' -> printf " JR\t NZ, 0x%02X\n" n;
if cpu.flag.z = false then
begin
@ -117,6 +123,23 @@ let run cpu (mem: Memory.map) =
inc_pc cpu 1; inc_cycles cpu 8
end
| '\x21' -> let addr = read_2B mem cpu.reg.pc in
printf " LD\t HL, 0x%04X\n" addr;
cpu.reg.h <- char_of_int nn;
cpu.reg.l <- char_of_int n;
inc_cycles cpu 12
| '\x28' -> printf " JR\t Z, 0x%02X\n" n;
if cpu.flag.z = true 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 1; inc_cycles cpu 8
@ -126,6 +149,14 @@ let run cpu (mem: Memory.map) =
cpu.reg.a <- char_of_int @@ int_A lxor int_A;
inc_cycles cpu 4
| '\xC0' -> printf " RET \tNZ\n";
if cpu.flag.z = false then
begin
cpu.reg.pc <- cpu.reg.sp;
inc_cycles cpu 20
end else
inc_cycles cpu 8
| '\xC3' -> let addr = read_2B mem cpu.reg.pc in
printf " JP \t0x%04X\n" addr;
cpu.reg.pc <- addr; inc_cycles cpu 16
@ -146,5 +177,5 @@ let run cpu (mem: Memory.map) =
cmp_A cpu n;
inc_pc cpu 1; inc_cycles cpu 8
| _ as x -> eprintf "opcode %02X\n" (int_of_char x);
failwith "Unimplemented opcode."
| x -> eprintf "opcode 0x%02X\n" (int_of_char x);
failwith "Unimplemented opcode."

View File

@ -1,4 +1,5 @@
open Bytes
open Printf
(** http://bgb.bircd.org/pandocs.htm#memorymap
http://imrannazar.com/GameBoy-Emulation-in-JavaScript:-Memory *)
@ -31,7 +32,8 @@ let get_mem_bank mem addr =
| x when x < 0xFF80 -> mem.io, x - 0xFF00
| x when x < 0xFFFF -> mem.hram, x - 0xFF80
| 0xFFFF -> mem.interrupt, 0
| _ -> failwith "Invalid memory range."
| x -> eprintf "Memory access 0x%08X\n" x;
failwith "Invalid memory range."
let get mem addr =