From 326bb06e82974403541fce0272d6ad21399a7fbc Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Wed, 4 Dec 2019 23:02:51 +0200 Subject: [PATCH] Move rom file loading logic to Cartridge Former-commit-id: 29045310d380657819529333b5231a98be5fa8c1 --- src/bin/main.rs | 27 +-------------------------- src/core/cartridge.rs | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 5fe3ea5..3aba732 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,14 +1,9 @@ use std::ffi::OsStr; -use std::fs::File; -use std::io::prelude::*; -use std::path::Path; use std::time; #[macro_use] extern crate clap; -extern crate zip; - use clap::{App, ArgMatches}; extern crate rustboyadvance_ng; @@ -20,25 +15,6 @@ use rustboyadvance_ng::core::{GBAError, GBAResult, GameBoyAdvance}; use rustboyadvance_ng::debugger::Debugger; use rustboyadvance_ng::util::read_bin_file; -fn load_rom(path: &str) -> GBAResult> { - if path.ends_with(".zip") { - let zipfile = File::open(path)?; - let mut archive = zip::ZipArchive::new(zipfile)?; - for i in 0..archive.len() { - let mut file = archive.by_index(i)?; - if file.name().ends_with(".gba") { - let mut buf = Vec::new(); - file.read_to_end(&mut buf)?; - return Ok(buf); - } - } - panic!("no .gba file contained in the zip file"); - } else { - let buf = read_bin_file(path)?; - Ok(buf) - } -} - fn run_emulator(matches: &ArgMatches) -> GBAResult<()> { let skip_bios = matches.occurrences_of("skip_bios") != 0; let no_framerate_limit = matches.occurrences_of("no_framerate_limit") != 0; @@ -54,8 +30,7 @@ fn run_emulator(matches: &ArgMatches) -> GBAResult<()> { let bios_bin = read_bin_file(matches.value_of("bios").unwrap_or_default())?; - let rom_bin = load_rom(matches.value_of("game_rom").unwrap())?; - let cart = Cartridge::new(rom_bin); + let cart = Cartridge::from_path(matches.value_of("game_rom").unwrap())?; println!("loaded rom: {:#?}", cart.header); let mut core = Core::new(); diff --git a/src/core/cartridge.rs b/src/core/cartridge.rs index f6a6f1c..4004d36 100644 --- a/src/core/cartridge.rs +++ b/src/core/cartridge.rs @@ -1,8 +1,14 @@ +use std::fs::File; +use std::io::prelude::*; +use std::path::Path; use std::str::from_utf8; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; +use zip::ZipArchive; +use super::super::util::read_bin_file; use super::arm7tdmi::{bus::Bus, Addr}; +use super::GBAResult; /// From GBATEK /// @@ -69,19 +75,40 @@ pub struct Cartridge { bytes: Box<[u8]>, } +fn load_rom(path: &str) -> GBAResult> { + if path.ends_with(".zip") { + let zipfile = File::open(path)?; + let mut archive = ZipArchive::new(zipfile)?; + for i in 0..archive.len() { + let mut file = archive.by_index(i)?; + if file.name().ends_with(".gba") { + let mut buf = Vec::new(); + file.read_to_end(&mut buf)?; + return Ok(buf); + } + } + panic!("no .gba file contained in the zip file"); + } else { + let buf = read_bin_file(path)?; + Ok(buf) + } +} + impl Cartridge { const MIN_SIZE: usize = 4 * 1024 * 1024; - pub fn new(mut rom_bin: Vec) -> Cartridge { + pub fn from_path(rom_path: &str) -> GBAResult { + let mut rom_bin = load_rom(rom_path)?; + if rom_bin.len() < Cartridge::MIN_SIZE { rom_bin.resize_with(Cartridge::MIN_SIZE, Default::default); } let header = CartridgeHeader::parse(&rom_bin); - Cartridge { + Ok(Cartridge { header: header, bytes: rom_bin.into_boxed_slice(), - } + }) } }