forked from linoscope/CAMLBOY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartridge_header.ml
51 lines (47 loc) · 1.21 KB
/
cartridge_header.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
type t = {
cartridge_type : Cartridge_type.t;
rom_bank_count : int;
ram_bank_count : int;
}
let create ~rom_bytes =
let cartridge_type =
let open Cartridge_type in
match Bigstringaf.unsafe_get rom_bytes 0x147 |> Char.code with
| 0x00 -> ROM_ONLY
| 0x01 -> MBC1
| 0x02 -> MBC1_RAM
| 0x03 -> MBC1_RAM_BATTERY
| 0x05 -> MBC2
| 0x06 -> MBC2_BATTERY
| 0x0F -> MBC3_TIMER_BATTERY
| 0x10 -> MBC3_TIMER_RAM_BATTERY
| 0x11 -> MBC3
| x -> raise @@ Invalid_argument (Printf.sprintf "Unknown rom type : 0x%x" x)
in
let rom_bank_count =
match Bigstringaf.unsafe_get rom_bytes 0x148 |> Char.code with
| 0x00 -> 2
| 0x01 -> 4
| 0x02 -> 8
| 0x03 -> 16
| 0x04 -> 32
| 0x05 -> 64
| 0x06 -> 128
| 0x07 -> 256
| 0x08 -> 512
| _ -> assert false
in
let ram_bank_count =
match Bigstringaf.unsafe_get rom_bytes 0x149 |> Char.code with
| 0x00 -> 0
| 0x01 -> 1
| 0x02 -> 1
| 0x03 -> 4
| 0x04 -> 16
| 0x05 -> 8
| _ -> assert false
in
{ cartridge_type; rom_bank_count; ram_bank_count }
let get_cartridge_type t = t.cartridge_type
let get_rom_bank_count t = t.rom_bank_count
let get_ram_bank_count t = t.ram_bank_count