Skip to content

Commit

Permalink
Use Box instead of Rc for cartridge
Browse files Browse the repository at this point in the history
  • Loading branch information
joajfreitas committed Jul 26, 2024
1 parent 852667d commit d2d9a47
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
3 changes: 1 addition & 2 deletions fpt/src/memory/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ use crate::memory::{create_empty_mbc, create_mbc, Cartridge};
pub type Address = usize;
pub type MemoryRange = Range<Address>;

#[derive(Clone)]
pub struct Memory {
mem: Vec<u8>,
pub bootrom_loaded: bool,
pub cartridge: Rc<RefCell<dyn Cartridge>>,
pub cartridge: Box<RefCell<dyn Cartridge>>,
bootrom: &'static [u8; 256],
code_listing: Vec<Option<String>>,
pub buttons: Buttons,
Expand Down
9 changes: 4 additions & 5 deletions fpt/src/memory/mbc_builder.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use std::cell::RefCell;
use std::rc::Rc;

use super::cartridge::{get_cartridge_type, EmptyCartridge};
use super::mbc_none::NoMbcCartridge;
use super::Cartridge;

pub fn create_mbc(cartridge_data: &[u8]) -> Option<Rc<RefCell<dyn Cartridge>>> {
pub fn create_mbc(cartridge_data: &[u8]) -> Option<Box<RefCell<dyn Cartridge>>> {
// https://gbdev.io/pandocs/The_Cartridge_Header.html#0147--cartridge-type
match get_cartridge_type(cartridge_data) {
0x00 => Some(Rc::new(RefCell::new(NoMbcCartridge::new(cartridge_data)))), // rom only
0x00 => Some(Box::new(RefCell::new(NoMbcCartridge::new(cartridge_data)))), // rom only
_ => None,
}
}

pub fn create_empty_mbc() -> Rc<RefCell<dyn Cartridge>> {
Rc::new(RefCell::new(EmptyCartridge::new()))
pub fn create_empty_mbc() -> Box<RefCell<dyn Cartridge>> {
Box::new(RefCell::new(EmptyCartridge::new()))
}
4 changes: 2 additions & 2 deletions fpt/src/memory/mbc_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{map, Address};
///
/// <https://gbdev.io/pandocs/nombc.html>
pub struct NoMbcCartridge {
memory: Vec<u8>,
memory: [u8; 0x8000],
}

impl NoMbcCartridge {
Expand All @@ -15,7 +15,7 @@ impl NoMbcCartridge {
"Expected cartridge size of 0x8000"
);
NoMbcCartridge {
memory: cartridge.to_vec(),
memory: cartridge.try_into().unwrap(),
}
}
}
Expand Down

0 comments on commit d2d9a47

Please sign in to comment.