Read Nintendo checksum in cartridge header.

This first commit parses the ROM to extract the Nintendo Logo and
display it on stdout.
This commit is contained in:
Fabien Freling 2015-02-21 17:11:18 +01:00
commit 09f1ffb488
7 changed files with 80 additions and 0 deletions

28
src/cartridge.ml Normal file
View file

@ -0,0 +1,28 @@
open Unix
open Printf
(** http://bgb.bircd.org/pandocs.htm#thecartridgeheader *)
type memory_bank_controller =
| ROM_ONLY
| MBC1
type t = {
nintendo_logo : bytes;
title : string;
mem_type : memory_bank_controller;
rom_size : int;
ram_size : int;
header_checksum : bytes;
global_checksum : bytes;
}
let read_cartridge file =
print_endline file;
let fd = openfile file [Unix.O_RDONLY] 0o644 in
let n_logo = Bytes.create 48 in
let _ = lseek fd 0x0104 SEEK_SET in
let _ = read fd n_logo 0 48 in
print_endline "Nintendo logo:";
Hexa.print_bytes n_logo ~width:16;
Unix.close fd

14
src/hexa.ml Normal file
View file

@ -0,0 +1,14 @@
open Printf
let print_bytes b ?(width=8) =
let l = Bytes.length b in
let rec print_line b start last =
if start < last then
let max = min (start + width - 1) last in
for i = start to max do
printf "%02X " (Bytes.get b i |> int_of_char)
done;
print_newline ();
print_line b (start + width) last
in
print_line b 0 l

4
src/oboy.ml Normal file
View file

@ -0,0 +1,4 @@
let () =
if Array.length Sys.argv < 2
then print_endline "Please specify a ROM."
else Cartridge.read_cartridge Sys.argv.(1)