Use memory map instead of cartridge.
This commit is contained in:
parent
16d303008c
commit
e5bc9bc4db
3 changed files with 36 additions and 16 deletions
|
@ -1,3 +1,5 @@
|
|||
open Bytes
|
||||
|
||||
(** http://bgb.bircd.org/pandocs.htm#memorymap
|
||||
http://imrannazar.com/GameBoy-Emulation-in-JavaScript:-Memory *)
|
||||
|
||||
|
@ -5,12 +7,31 @@ type map = {
|
|||
rom_bank_00 : bytes; (* cartridge, 16KB *)
|
||||
rom_bank_01 : bytes; (* additional bank, 16KB *)
|
||||
vram : bytes; (* Video RAM, 8KB *)
|
||||
io : bytes; (* I/O ports *)
|
||||
}
|
||||
|
||||
let init (cartridge: Cartridge.t) =
|
||||
let open Bytes in
|
||||
{
|
||||
rom_bank_00 = sub cartridge.full_rom 0 0x4000;
|
||||
rom_bank_01 = create 0x4000;
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue