From 4857ed73e80619d6046af223a61242e99e81235a Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 14 Nov 2017 20:38:45 -0500 Subject: ppu optimization --- src/mapper.rs | 71 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 34 deletions(-) (limited to 'src/mapper.rs') diff --git a/src/mapper.rs b/src/mapper.rs index c54acb4..210b59f 100644 --- a/src/mapper.rs +++ b/src/mapper.rs @@ -1,61 +1,64 @@ #![allow(dead_code)] use memory::VMem; use cartridge::{Cartridge, BankType}; -use core::cell::RefCell; +use core::cell::UnsafeCell; pub struct Mapper2<'a> { cart: &'a Cartridge, - prg_bank1: RefCell<&'a [u8]>, - prg_bank2: RefCell<&'a [u8]>, - chr_bank: RefCell<&'a mut [u8]>, - sram: RefCell<&'a mut [u8]>, + prg_bank1: UnsafeCell<&'a [u8]>, + prg_bank2: UnsafeCell<&'a [u8]>, + chr_bank: UnsafeCell<&'a mut [u8]>, + sram: UnsafeCell<&'a mut [u8]>, prg_nbank: usize } impl<'a> VMem for Mapper2<'a> { fn read(&self, addr: u16) -> u8 { let addr = addr as usize; - if addr < 0x2000 { /* 0x2000 size bank */ - self.chr_bank.borrow()[addr] - } else if addr >= 0xc000 { /* 0x4000 size bank */ - self.prg_bank2.borrow()[addr - 0xc000] - } else if addr >= 0x8000 { /* 0x4000 size bank */ - self.prg_bank1.borrow()[addr - 0x8000] - } else if addr >= 0x6000 { - self.sram.borrow()[addr - 0x6000] - } else { - panic!("unmapped address: 0x{:04x}", addr) + unsafe { + if addr < 0x2000 { /* 0x2000 size bank */ + (*self.chr_bank.get())[addr] + } else if addr >= 0xc000 { /* 0x4000 size bank */ + (*self.prg_bank2.get())[addr - 0xc000] + } else if addr >= 0x8000 { /* 0x4000 size bank */ + (*self.prg_bank1.get())[addr - 0x8000] + } else if addr >= 0x6000 { + (*self.sram.get())[addr - 0x6000] + } else { + panic!("unmapped address: 0x{:04x}", addr) + } } } fn write(&self, addr: u16, data: u8) { let addr = addr as usize; - if addr < 0x2000 { - self.chr_bank.borrow_mut()[addr] = data; - } else if addr >= 0x8000 { - (*self.prg_bank1.borrow_mut()) = unsafe { - &*self.cart.get_bank(((data as usize) % self.prg_nbank) << 14, - 0x4000, - BankType::PrgRom)}; - } else if addr >= 0x6000 { - self.sram.borrow_mut()[addr - 0x6000] = data; - } else { - panic!("invalid write to address: 0x{:04x}", addr); + unsafe { + if addr < 0x2000 { + (*self.chr_bank.get())[addr] = data; + } else if addr >= 0x8000 { + (*self.prg_bank1.get()) = + &*self.cart.get_bank(((data as usize) % self.prg_nbank) << 14, + 0x4000, + BankType::PrgRom); + } else if addr >= 0x6000 { + (*self.sram.get())[addr - 0x6000] = data; + } else { + panic!("invalid write to address: 0x{:04x}", addr); + } } } } impl<'a> Mapper2<'a> { - pub fn new(cart: *const Cartridge) -> Self { + pub fn new(cart: &'a Cartridge) -> Self { unsafe { - let cart = &*cart; let nbank = cart.get_size(BankType::PrgRom) >> 14; - Mapper2{cart: &cart, - prg_bank1: RefCell::new(&*cart.get_bank(0, 0x4000, BankType::PrgRom)), - prg_bank2: RefCell::new(&*cart.get_bank((nbank - 1) << 14, 0x4000, BankType::PrgRom)), - chr_bank: RefCell::new(&mut *cart.get_bank(0, 0x2000, BankType::ChrRom)), - sram: RefCell::new(&mut *cart.get_bank(0, 0x2000, BankType::Sram)), - prg_nbank: nbank} + Mapper2{cart, + prg_bank1: UnsafeCell::new(&*cart.get_bank(0, 0x4000, BankType::PrgRom)), + prg_bank2: UnsafeCell::new(&*cart.get_bank((nbank - 1) << 14, 0x4000, BankType::PrgRom)), + chr_bank: UnsafeCell::new(&mut *cart.get_bank(0, 0x2000, BankType::ChrRom)), + sram: UnsafeCell::new(&mut *cart.get_bank(0, 0x2000, BankType::Sram)), + prg_nbank: nbank} } } } -- cgit v1.2.3-70-g09d2