Use memory map instead of cartridge.

This commit is contained in:
Fabien Freling 2015-03-22 16:57:03 +01:00
parent 16d303008c
commit e5bc9bc4db
3 changed files with 36 additions and 16 deletions

View file

@ -61,9 +61,9 @@ let split_2B x =
let high = x / 256 |> char_of_int in let high = x / 256 |> char_of_int in
(high, low) (high, low)
let read_2B b addr = let read_2B m addr =
let low = Bytes.get b addr in let low = Memory.get m addr in
let high = Bytes.get b (addr + 1) in let high = Memory.get m (addr + 1) in
merge_bytes low high merge_bytes low high
let inc_BC cpu = let inc_BC cpu =
@ -73,12 +73,11 @@ let inc_BC cpu =
cpu.reg.b <- high cpu.reg.b <- high
(** http://imrannazar.com/GameBoy-Z80-Opcode-Map *) (** http://imrannazar.com/GameBoy-Z80-Opcode-Map *)
let run cpu (cartridge: Cartridge.t) = let run cpu (mem: Memory.map) =
let data = cartridge.full_rom in let n = Memory.get mem (cpu.reg.pc + 1) in
let n = Bytes.get data (cpu.reg.pc + 1) in let nn = Memory.get mem (cpu.reg.pc + 2) in
let nn = Bytes.get data (cpu.reg.pc + 2) 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 Bytes.get data cpu.reg.pc with match Memory.get mem cpu.reg.pc with
| '\x00' -> printf " NOP\n"; | '\x00' -> printf " NOP\n";
inc_pc cpu 1; inc_cycles cpu 4 inc_pc cpu 1; inc_cycles cpu 4
@ -95,16 +94,16 @@ let run cpu (cartridge: Cartridge.t) =
cpu.reg.a <- char_of_int @@ int_A lxor int_A; cpu.reg.a <- char_of_int @@ int_A lxor int_A;
inc_pc cpu 1; inc_cycles cpu 4 inc_pc cpu 1; inc_cycles cpu 4
| '\xC3' -> let addr = read_2B data (cpu.reg.pc + 1) in | '\xC3' -> let addr = read_2B mem (cpu.reg.pc + 1) 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
| '\xE0' -> printf " LDH \t(0xFF%02X), A\n" (int_of_char n); | '\xE0' -> printf " LDH \t(0xFF%02X), A\n" (int_of_char n);
(* fixme *) Memory.set mem (0xFF00 + (int_of_char n)) cpu.reg.a;
inc_pc cpu 2; inc_cycles cpu 12 inc_pc cpu 2; inc_cycles cpu 12
| '\xF0' -> printf " LDH \tA, (0xFF%02X)\n" (int_of_char n); | '\xF0' -> printf " LDH \tA, (0xFF%02X)\n" (int_of_char n);
(* fixme *) cpu.reg.a = Memory.get mem (0xFF00 + (int_of_char n));
inc_pc cpu 2; inc_cycles cpu 12 inc_pc cpu 2; inc_cycles cpu 12
| '\xF3' -> printf " DI\n"; | '\xF3' -> printf " DI\n";

View file

@ -1,3 +1,5 @@
open Bytes
(** 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 *)
@ -5,12 +7,31 @@ type map = {
rom_bank_00 : bytes; (* cartridge, 16KB *) rom_bank_00 : bytes; (* cartridge, 16KB *)
rom_bank_01 : bytes; (* additional bank, 16KB *) rom_bank_01 : bytes; (* additional bank, 16KB *)
vram : bytes; (* Video RAM, 8KB *) vram : bytes; (* Video RAM, 8KB *)
io : bytes; (* I/O ports *)
} }
let init (cartridge: Cartridge.t) = let init (cartridge: Cartridge.t) =
let open Bytes in
{ {
rom_bank_00 = sub cartridge.full_rom 0 0x4000; rom_bank_00 = sub cartridge.full_rom 0 0x4000;
rom_bank_01 = create 0x4000; rom_bank_01 = create 0x4000;
vram = create 0x2000; vram = create 0x2000;
io = create 0x0080;
} }
let get_mem_bank mem addr =
match addr with
| x when x < 0x4000 -> mem.rom_bank_00, x
| x when x < 0x8000 -> mem.rom_bank_01, (x - 0x4000)
| x when x < 0xA000 -> mem.vram, (x - 0x8000)
| x when x < 0xFF00 -> failwith "Unimplemented memory range."
| x when x < 0xFF80 -> mem.io, x - 0xFF00
| _ -> failwith "Invalid memory range."
let get mem addr =
let m, x = get_mem_bank mem addr in
get m x
let set mem addr c =
let m, x = get_mem_bank mem addr in
set m x c

View file

@ -1,8 +1,8 @@
open Printf open Printf
let rec run (cpu: Cpu.t) (cartridge: Cartridge.t) = let rec run (cpu: Cpu.t) (mem: Memory.map) =
Cpu.run cpu cartridge; Cpu.run cpu mem;
run cpu cartridge run cpu mem
(** Power up sequence (** Power up sequence
http://bgb.bircd.org/pandocs.htm#powerupsequence *) http://bgb.bircd.org/pandocs.htm#powerupsequence *)
@ -16,7 +16,7 @@ let power_up cartridge =
let cpu = Cpu.init_cpu in let cpu = Cpu.init_cpu in
let mem = Memory.init cartridge in let mem = Memory.init cartridge in
run cpu cartridge run cpu mem
let () = let () =