open Bytes (** http://bgb.bircd.org/pandocs.htm#memorymap http://imrannazar.com/GameBoy-Emulation-in-JavaScript:-Memory *) 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) = { 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