Skip to content

Instantly share code, notes, and snippets.

@RossMeikleham
Created March 20, 2015 17:44
Show Gist options
  • Save RossMeikleham/d8d3f50fafaeae3af59b to your computer and use it in GitHub Desktop.
Save RossMeikleham/d8d3f50fafaeae3af59b to your computer and use it in GitHub Desktop.
Binary Read
* Reads a ROM file and returns a vector containing
* its bytes if successful. Otherwise an error string
* if the file is too large or IoError is raised */
fn read_rom(file_path: String) -> Result<Vec<u8>,String> {
match File::open(&Path::new(file_path)).read_to_end() {
Ok(rom_contents) => {
/* Programs start at address 0x200, in original implementation
* 0x000 - 0x1FF reserved for VM, just pad start of mem with
* 0s in this case */
let start_mem : Vec<u8> = iter::repeat(0u8).take(START_RAM).collect();
let mem = start_mem + rom_contents.as_slice();
let size = mem.len();
if size <= MAX_RAM {
Ok(mem)
} else { /* Memory read in from game ROM is too large */
Err(format!("game image is too large
({} bytes), must be a maximum of {} bytes"
,size , MAX_RAM - START_RAM).to_string())
}
},
/* Error reading file */
Err(e) => Err(e.detail.unwrap_or("".to_string()))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment