Read RAM size.

master
Fabien Freling 2015-02-23 16:51:53 +01:00
parent bbd75e25f1
commit f89d34ec14
2 changed files with 28 additions and 9 deletions

View File

@ -14,8 +14,8 @@ type t = {
title : string;
(* mem_type : memory_bank_controller; *)
rom_size : int;
(* ram_size : int;
header_checksum : bytes;
ram_size : int;
(* header_checksum : bytes;
global_checksum : bytes; *)
}
@ -30,8 +30,8 @@ let check_nintendo_logo cartridge =
(** ROM size is expressed in KB *)
let get_ROM_size b =
match Bytes.get b 0 |> int_of_char with
| 0x00 -> 32 (* no ROM banking *)
| 0x01 -> 64 (* 4 banks *)
| 0x00 -> 32 (* no ROM banking *)
| 0x01 -> 64 (* 4 banks *)
| 0x02 -> 128 (* 8 banks *)
| 0x03 -> 256 (* 16 banks *)
| 0x04 -> 512 (* 32 banks *)
@ -43,6 +43,15 @@ let get_ROM_size b =
| 0x54 -> 1500 (* 96 banks *)
| _ -> raise Read_error
(** RAM size is expressed in KB *)
let get_RAM_size b =
match Bytes.get b 0 |> int_of_char with
| 0x00 -> 0
| 0x01 -> 2
| 0x02 -> 8
| 0x03 -> 32 (* 4 x 8KB banks *)
| _ -> raise Read_error
let read_cartridge file =
try
@ -53,25 +62,33 @@ let read_cartridge file =
if (lseek fd nin_logo_offset SEEK_SET) != nin_logo_offset
then raise Read_error;
(* Nintendo logo *)
let nin_logo_size = 48 in
let nintendo_logo = Bytes.create nin_logo_size in
if (read fd nintendo_logo 0 nin_logo_size) != nin_logo_size
then raise Read_error;
(* Title *)
let title_size = 16 in
let title_b = Bytes.create title_size in
if (read fd title_b 0 title_size) != title_size
then raise Read_error;
let title = Bytes.to_string title_b in
let size_b = Bytes.create 1 in
(* ROM size *)
let byte = Bytes.create 1 in
let _ = lseek fd 4 SEEK_CUR in
if (read fd size_b 0 1) != 1
if (read fd byte 0 1) != 1
then raise Read_error;
let rom_size = get_ROM_size size_b in
let rom_size = get_ROM_size byte in
(* RAM size *)
if (read fd byte 0 1) != 1
then raise Read_error;
let ram_size = get_RAM_size byte in
Unix.close fd;
Some { nintendo_logo; title; rom_size }
Some { nintendo_logo; title; rom_size; ram_size }
with Read_error -> Unix.close fd; None

View File

@ -1,3 +1,4 @@
open Cartridge
open Printf
(** Power up sequence
@ -8,7 +9,8 @@ let power_up cartridge =
then print_endline "Invalid ROM."
else
print_endline "Valid ROM.";
printf "ROM size: %iKB\n" cartridge.rom_size
printf "ROM size: %iKB\n" cartridge.rom_size;
printf "RAM size: %iKB\n" cartridge.ram_size
let () =