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:
		
						commit
						09f1ffb488
					
				
					 7 changed files with 80 additions and 0 deletions
				
			
		
							
								
								
									
										7
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								.gitignore
									
										
									
									
										vendored
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,7 @@
 | 
			
		|||
# vim
 | 
			
		||||
.*.swp
 | 
			
		||||
 | 
			
		||||
# OCaml
 | 
			
		||||
_build
 | 
			
		||||
*.byte
 | 
			
		||||
*.native
 | 
			
		||||
							
								
								
									
										24
									
								
								LICENSE
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								LICENSE
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,24 @@
 | 
			
		|||
Copyright (c) 2015, Fabien Freling
 | 
			
		||||
All rights reserved.
 | 
			
		||||
 | 
			
		||||
Redistribution and use in source and binary forms, with or without
 | 
			
		||||
modification, are permitted provided that the following conditions are met:
 | 
			
		||||
 | 
			
		||||
* Redistributions of source code must retain the above copyright notice, this
 | 
			
		||||
  list of conditions and the following disclaimer.
 | 
			
		||||
 | 
			
		||||
* Redistributions in binary form must reproduce the above copyright notice,
 | 
			
		||||
  this list of conditions and the following disclaimer in the documentation
 | 
			
		||||
  and/or other materials provided with the distribution.
 | 
			
		||||
 | 
			
		||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 | 
			
		||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 | 
			
		||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 | 
			
		||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 | 
			
		||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 | 
			
		||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 | 
			
		||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 | 
			
		||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 | 
			
		||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 | 
			
		||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										2
									
								
								Makefile
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
all:
 | 
			
		||||
	ocamlbuild -use-ocamlfind -I src oboy.byte
 | 
			
		||||
							
								
								
									
										1
									
								
								_tags
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								_tags
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
true: package(unix)
 | 
			
		||||
							
								
								
									
										28
									
								
								src/cartridge.ml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/cartridge.ml
									
										
									
									
									
										Normal 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
									
								
							
							
						
						
									
										14
									
								
								src/hexa.ml
									
										
									
									
									
										Normal 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
									
								
							
							
						
						
									
										4
									
								
								src/oboy.ml
									
										
									
									
									
										Normal 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)
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue