fix: implement Default and Clone for Box<dyn MBC>

This commit is contained in:
Rekai Nyangadzayi Musuka 2021-03-15 19:08:19 -05:00
parent 4013bbeb9b
commit 5d95eadd2e
1 changed files with 17 additions and 6 deletions

View File

@ -29,7 +29,6 @@ impl Cartridge {
let mbc = match mbc_kind { let mbc = match mbc_kind {
MBCKind::None => todo!("Handle no MBC Situation"), MBCKind::None => todo!("Handle no MBC Situation"),
MBCKind::MBC1 => MBC1 { MBCKind::MBC1 => MBC1 {
kind: mbc_kind,
ram_size, ram_size,
ram: vec![0; ram_byte_count as usize].into_boxed_slice(), ram: vec![0; ram_byte_count as usize].into_boxed_slice(),
bank_count, bank_count,
@ -87,7 +86,6 @@ impl Cartridge {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
struct MBC1 { struct MBC1 {
pub kind: MBCKind,
current_rom_bank: u8, // 5-bit Number current_rom_bank: u8, // 5-bit Number
current_ram_bank: u8, // 2-bit number current_ram_bank: u8, // 2-bit number
mode: bool, mode: bool,
@ -215,11 +213,24 @@ impl MBC for MBC1 {
} }
} }
trait MBC { trait MBC: CloneMBC {
fn handle_read(&self, addr: u16) -> MBCResult; fn handle_read(&self, addr: u16) -> MBCResult;
fn handle_write(&mut self, addr: u16, byte: u8); fn handle_write(&mut self, addr: u16, byte: u8);
} }
trait CloneMBC {
fn clone_mbc<'a>(&self) -> Box<dyn MBC>;
}
impl<T> CloneMBC for T
where
T: MBC + Clone + 'static,
{
fn clone_mbc<'a>(&self) -> Box<dyn MBC> {
Box::new(self.clone())
}
}
enum MBCResult { enum MBCResult {
Address(u16), Address(u16),
RamValue(u8), RamValue(u8),
@ -345,18 +356,18 @@ impl From<u8> for BankCount {
impl std::fmt::Debug for Box<dyn MBC> { impl std::fmt::Debug for Box<dyn MBC> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!() todo!("Implement Debug for Box<dyn MBC> Trait Object");
} }
} }
impl std::clone::Clone for Box<dyn MBC> { impl std::clone::Clone for Box<dyn MBC> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
todo!() self.clone_mbc()
} }
} }
impl std::default::Default for Box<dyn MBC> { impl std::default::Default for Box<dyn MBC> {
fn default() -> Self { fn default() -> Self {
todo!() Box::new(MBC1::default())
} }
} }