Move rom file loading logic to Cartridge
Former-commit-id: 29045310d380657819529333b5231a98be5fa8c1
This commit is contained in:
parent
763ee217d8
commit
326bb06e82
|
@ -1,14 +1,9 @@
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::fs::File;
|
|
||||||
use std::io::prelude::*;
|
|
||||||
use std::path::Path;
|
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
|
||||||
extern crate zip;
|
|
||||||
|
|
||||||
use clap::{App, ArgMatches};
|
use clap::{App, ArgMatches};
|
||||||
|
|
||||||
extern crate rustboyadvance_ng;
|
extern crate rustboyadvance_ng;
|
||||||
|
@ -20,25 +15,6 @@ use rustboyadvance_ng::core::{GBAError, GBAResult, GameBoyAdvance};
|
||||||
use rustboyadvance_ng::debugger::Debugger;
|
use rustboyadvance_ng::debugger::Debugger;
|
||||||
use rustboyadvance_ng::util::read_bin_file;
|
use rustboyadvance_ng::util::read_bin_file;
|
||||||
|
|
||||||
fn load_rom(path: &str) -> GBAResult<Vec<u8>> {
|
|
||||||
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<()> {
|
fn run_emulator(matches: &ArgMatches) -> GBAResult<()> {
|
||||||
let skip_bios = matches.occurrences_of("skip_bios") != 0;
|
let skip_bios = matches.occurrences_of("skip_bios") != 0;
|
||||||
let no_framerate_limit = matches.occurrences_of("no_framerate_limit") != 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 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::from_path(matches.value_of("game_rom").unwrap())?;
|
||||||
let cart = Cartridge::new(rom_bin);
|
|
||||||
println!("loaded rom: {:#?}", cart.header);
|
println!("loaded rom: {:#?}", cart.header);
|
||||||
|
|
||||||
let mut core = Core::new();
|
let mut core = Core::new();
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use std::path::Path;
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
|
|
||||||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
||||||
|
use zip::ZipArchive;
|
||||||
|
|
||||||
|
use super::super::util::read_bin_file;
|
||||||
use super::arm7tdmi::{bus::Bus, Addr};
|
use super::arm7tdmi::{bus::Bus, Addr};
|
||||||
|
use super::GBAResult;
|
||||||
|
|
||||||
/// From GBATEK
|
/// From GBATEK
|
||||||
///
|
///
|
||||||
|
@ -69,19 +75,40 @@ pub struct Cartridge {
|
||||||
bytes: Box<[u8]>,
|
bytes: Box<[u8]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn load_rom(path: &str) -> GBAResult<Vec<u8>> {
|
||||||
|
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 {
|
impl Cartridge {
|
||||||
const MIN_SIZE: usize = 4 * 1024 * 1024;
|
const MIN_SIZE: usize = 4 * 1024 * 1024;
|
||||||
|
|
||||||
pub fn new(mut rom_bin: Vec<u8>) -> Cartridge {
|
pub fn from_path(rom_path: &str) -> GBAResult<Cartridge> {
|
||||||
|
let mut rom_bin = load_rom(rom_path)?;
|
||||||
|
|
||||||
if rom_bin.len() < Cartridge::MIN_SIZE {
|
if rom_bin.len() < Cartridge::MIN_SIZE {
|
||||||
rom_bin.resize_with(Cartridge::MIN_SIZE, Default::default);
|
rom_bin.resize_with(Cartridge::MIN_SIZE, Default::default);
|
||||||
}
|
}
|
||||||
|
|
||||||
let header = CartridgeHeader::parse(&rom_bin);
|
let header = CartridgeHeader::parse(&rom_bin);
|
||||||
Cartridge {
|
Ok(Cartridge {
|
||||||
header: header,
|
header: header,
|
||||||
bytes: rom_bin.into_boxed_slice(),
|
bytes: rom_bin.into_boxed_slice(),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue