Add more opcodes.

This commit is contained in:
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; cpu.flag.c <- diff > 0xFF || diff < 0;
update_flag_reg cpu 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 run cpu (mem: Memory.map) =
let opcode = Memory.get mem cpu.reg.pc in let opcode = Memory.get mem cpu.reg.pc in
inc_pc cpu 1; inc_pc cpu 1;
let n = Memory.get mem cpu.reg.pc |> int_of_char in 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); *) (* Hexa.print_slice cartridge.full_rom cpu.reg.pc (cpu.reg.pc + 7); *)
match opcode with match opcode with
| '\x00' -> printf " NOP\n"; | '\x00' -> printf " NOP\n";
@ -108,6 +111,9 @@ let run cpu (mem: Memory.map) =
inc_BC cpu; inc_BC cpu;
inc_cycles cpu 8 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; | '\x20' -> printf " JR\t NZ, 0x%02X\n" n;
if cpu.flag.z = false then if cpu.flag.z = false then
begin begin
@ -117,6 +123,23 @@ let run cpu (mem: Memory.map) =
inc_pc cpu 1; inc_cycles cpu 8 inc_pc cpu 1; inc_cycles cpu 8
end 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; | '\x3E' -> printf " LD \tA, 0x%02X\n" n;
cpu.reg.a <- char_of_int(n); cpu.reg.a <- char_of_int(n);
inc_pc cpu 1; inc_cycles cpu 8 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; cpu.reg.a <- char_of_int @@ int_A lxor int_A;
inc_cycles cpu 4 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 | '\xC3' -> let addr = read_2B mem cpu.reg.pc in
printf " JP \t0x%04X\n" addr; printf " JP \t0x%04X\n" addr;
cpu.reg.pc <- addr; inc_cycles cpu 16 cpu.reg.pc <- addr; inc_cycles cpu 16
@ -146,5 +177,5 @@ let run cpu (mem: Memory.map) =
cmp_A cpu n; cmp_A cpu n;
inc_pc cpu 1; inc_cycles cpu 8 inc_pc cpu 1; inc_cycles cpu 8
| _ as x -> eprintf "opcode %02X\n" (int_of_char x); | x -> eprintf "opcode 0x%02X\n" (int_of_char x);
failwith "Unimplemented opcode." failwith "Unimplemented opcode."

View file

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