Skip to content

Commit

Permalink
Refactor...
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubG-git committed Dec 8, 2024
1 parent fcbbbd8 commit f76354e
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 27 deletions.
8 changes: 2 additions & 6 deletions emulator/src/cartridge/cartridge.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::cartridge::common::consts::NES_FILE_MAGIC_BYTES;
use crate::cartridge::common::enums::errors::NesRomReadError;
use crate::cartridge::common::enums::nes::Nes;
use crate::cartridge::file_loader::FileLoadable;
use crate::cartridge::common::traits::cartridge_data::CartridgeData;
use crate::cartridge::common::traits::file_loadable::FileLoadable;
use crate::cartridge::formats::i_nes::Ines;
use crate::cartridge::formats::nes_2::Nes2;
use crate::cartridge::registers::chr_rom::ChrRom;
Expand All @@ -10,11 +11,6 @@ use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::Path;

pub trait CartridgeData {
fn prg_rom(&self) -> &PrgRom;
fn chr_rom(&self) -> &ChrRom;
}

pub struct Cartridge {
data: Box<dyn CartridgeData>,
}
Expand Down
2 changes: 2 additions & 0 deletions emulator/src/cartridge/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod consts;
pub mod enums;
pub mod traits;
pub mod utils;
7 changes: 7 additions & 0 deletions emulator/src/cartridge/common/traits/cartridge_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::cartridge::registers::chr_rom::ChrRom;
use crate::cartridge::registers::prg_rom::PrgRom;

pub trait CartridgeData {
fn prg_rom(&self) -> &PrgRom;
fn chr_rom(&self) -> &ChrRom;
}
7 changes: 7 additions & 0 deletions emulator/src/cartridge/common/traits/file_loadable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::path::Path;

pub trait FileLoadable {
fn from_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Self>
where
Self: Sized;
}
2 changes: 2 additions & 0 deletions emulator/src/cartridge/common/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cartridge_data;
pub mod file_loadable;
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
use std::io::Read;
use std::path::Path;

pub trait FileLoadable {
fn from_file<P: AsRef<Path>>(path: P) -> anyhow::Result<Self>
where
Self: Sized;
}

pub fn read_banks<R: Read>(
file: &mut R,
Expand All @@ -23,8 +16,7 @@ pub fn read_banks<R: Read>(

#[cfg(test)]
mod tests {
use super::*;

use crate::cartridge::common::utils::file::read_banks;
#[test]
fn test_read_banks_2_4() {
let data = vec![1, 2, 3, 4, 5, 6, 7, 8];
Expand Down
1 change: 1 addition & 0 deletions emulator/src/cartridge/common/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod file;
8 changes: 4 additions & 4 deletions emulator/src/cartridge/formats/i_nes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cartridge::cartridge::CartridgeData;
use crate::cartridge::common::enums::mirroring::Mirroring;
use crate::cartridge::file_loader::read_banks;
use crate::cartridge::file_loader::FileLoadable;
use crate::cartridge::common::traits::cartridge_data::CartridgeData;
use crate::cartridge::common::traits::file_loadable::FileLoadable;
use crate::cartridge::common::utils::file::read_banks;
use crate::cartridge::registers::chr_rom::ChrRom;
use crate::cartridge::registers::prg_rom::PrgRom;
use std::fs::File;
Expand Down Expand Up @@ -197,7 +197,7 @@ impl CartridgeData for Ines {
#[cfg(test)]
mod tests {
use super::*;
use crate::cartridge::formats::i_nes::FileLoadable;
use crate::cartridge::common::traits::file_loadable::FileLoadable;
use std::io::Cursor;

#[test]
Expand Down
12 changes: 5 additions & 7 deletions emulator/src/cartridge/formats/nes_2.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::cartridge::cartridge::CartridgeData;
use crate::cartridge::common::consts::NES_FILE_MAGIC_BYTES;
use crate::cartridge::common::consts::PRG_UNIT_SIZE;
use crate::cartridge::common::enums::errors::NesRomReadError;
use crate::cartridge::common::enums::mirroring::Mirroring;
use crate::cartridge::file_loader::read_banks;
use crate::cartridge::file_loader::FileLoadable;
use crate::cartridge::common::traits::cartridge_data::CartridgeData;
use crate::cartridge::common::traits::file_loadable::FileLoadable;
use crate::cartridge::common::utils::file::read_banks;
use crate::cartridge::registers::chr_ram::ChrRam;
use crate::cartridge::registers::chr_rom::ChrRom;
use crate::cartridge::registers::prg_ram::PrgRam;
Expand Down Expand Up @@ -62,6 +62,7 @@ pub struct Nes2 {
trainer: Option<[u8; 512]>,
prg_ram: Option<PrgRam>,
chr_ram: Option<ChrRam>,
mirroring: Mirroring,
}

impl Debug for Nes2 {
Expand Down Expand Up @@ -156,8 +157,6 @@ impl FileLoadable for Nes2 {
Mirroring::Horizontal
};

let battery = header.flags_6 & 0b00000010 != 0;

let mut trainer = if is_trainer_present {
let mut trainer_data = [0; 512];
file.read_exact(&mut trainer_data)?;
Expand All @@ -166,8 +165,6 @@ impl FileLoadable for Nes2 {
None
};

let four_screen_vram = header.flags_6 & 0b00001000 != 0;

let prg_rom =
PrgRom::new_with_data(read_banks(&mut file, header.prg_rom_size, PRG_UNIT_SIZE)?);

Expand Down Expand Up @@ -200,6 +197,7 @@ impl FileLoadable for Nes2 {
trainer,
prg_ram,
chr_ram,
mirroring,
})
}
}
Expand Down
1 change: 0 additions & 1 deletion emulator/src/cartridge/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod cartridge;

pub mod common;
pub mod file_loader;
mod formats;
mod registers;

0 comments on commit f76354e

Please sign in to comment.