bootrom as option

This commit is contained in:
Alex Janka 2023-02-22 10:26:20 +11:00
parent 932e70a443
commit 3df569a7a1
3 changed files with 20 additions and 20 deletions

View file

@ -45,11 +45,7 @@ struct Args {
/// BootROM path /// BootROM path
#[arg(short, long)] #[arg(short, long)]
bootrom: String, bootrom: Option<String>,
/// Just run BootROM
#[arg(long)]
run_bootrom: bool,
/// Verbose print /// Verbose print
#[arg(short, long)] #[arg(short, long)]
@ -102,12 +98,18 @@ fn main() {
return; return;
} }
}; };
let bootrom: Vec<u8> = match fs::read(args.bootrom) {
Ok(data) => data, let bootrom_enabled = args.bootrom.is_some();
Err(e) => { let bootrom: Option<Vec<u8>> = if let Some(path) = args.bootrom {
println!("Error reading bootROM: {e}"); match fs::read(path) {
return; Ok(data) => Some(data),
Err(e) => {
println!("Error reading bootROM: {e}");
return;
}
} }
} else {
None
}; };
let mut window = Window::new( let mut window = Window::new(
@ -125,10 +127,10 @@ fn main() {
window.topmost(true); window.topmost(true);
let mut cpu = Cpu::new( let mut cpu = Cpu::new(
Memory::init(bootrom, args.run_bootrom, rom), Memory::init(bootrom, rom),
window, window,
args.tile_window, args.tile_window,
args.run_bootrom, bootrom_enabled,
Gilrs::new().unwrap(), Gilrs::new().unwrap(),
); );

View file

@ -11,8 +11,7 @@ pub(crate) type Address = u16;
#[allow(dead_code)] #[allow(dead_code)]
pub struct Memory { pub struct Memory {
bootrom: Vec<u8>, bootrom: Option<Vec<u8>>,
bootrom_enabled: bool,
rom: Rom, rom: Rom,
vram: [u8; 8192], vram: [u8; 8192],
ram: [u8; 8192], ram: [u8; 8192],
@ -30,10 +29,9 @@ pub struct Memory {
} }
impl Memory { impl Memory {
pub fn init(bootrom: Vec<u8>, bootrom_enabled: bool, rom: Rom) -> Self { pub fn init(bootrom: Option<Vec<u8>>, rom: Rom) -> Self {
Self { Self {
bootrom, bootrom,
bootrom_enabled,
rom, rom,
vram: [0x0; 8192], vram: [0x0; 8192],
ram: [0x0; 8192], ram: [0x0; 8192],
@ -56,8 +54,8 @@ impl Memory {
0x0..0x8000 => { 0x0..0x8000 => {
// rom access // rom access
// todo - switchable rom banks // todo - switchable rom banks
if self.bootrom_enabled && ((address as usize) < self.bootrom.len()) { if let Some(bootrom) = &self.bootrom && (address as usize) < bootrom.len() {
self.bootrom[address as usize] bootrom[address as usize]
} else { } else {
self.rom.get(address) self.rom.get(address)
} }
@ -91,7 +89,7 @@ impl Memory {
0xFE00..0xFEA0 => self.oam[(address - 0xFE00) as usize] = data, 0xFE00..0xFEA0 => self.oam[(address - 0xFE00) as usize] = data,
0xFEA0..0xFF00 => {} 0xFEA0..0xFF00 => {}
0xFF00..0xFF4C => self.set_io(address, data), 0xFF00..0xFF4C => self.set_io(address, data),
0xFF50 => self.bootrom_enabled = false, 0xFF50 => self.bootrom = None,
0xFF4C..0xFF50 | 0xFF51..0xFF80 => {} 0xFF4C..0xFF50 | 0xFF51..0xFF80 => {}
0xFF80..0xFFFF => self.cpu_ram[(address - 0xFF80) as usize] = data, 0xFF80..0xFFFF => self.cpu_ram[(address - 0xFF80) as usize] = data,
0xFFFF => { 0xFFFF => {

View file

@ -50,7 +50,7 @@ impl Cpu {
run_bootrom: bool, run_bootrom: bool,
gamepad_handler: Gilrs, gamepad_handler: Gilrs,
) -> Self { ) -> Self {
if run_bootrom { if !run_bootrom {
memory.cpu_ram_init(); memory.cpu_ram_init();
} }
Self { Self {