Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nes2 #12

Merged
merged 3 commits into from
Dec 15, 2024
Merged

Nes2 #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions emulator/src/cartridge/cartridge.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::cartridge::common::consts::NES_FILE_MAGIC_BYTES;
use crate::cartridge::common::enums::Nes;
use crate::cartridge::file_loader::{FileLoadable, NesRomReadError};
use crate::cartridge::common::enums::errors::NesRomReadError;
use crate::cartridge::common::enums::nes::Nes;
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 @@ -9,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 Expand Up @@ -72,6 +69,8 @@ impl CartridgeData for Cartridge {
#[cfg(test)]
mod tests {
use super::*;
use crate::cartridge::common::consts::{CHR_UNIT_SIZE, PRG_UNIT_SIZE};

#[test]
fn test_from_file() {
// Super Mario Bros
Expand All @@ -90,13 +89,7 @@ mod tests {

let chr_rom = cartridge.chr_rom();

assert_eq!(
prg_rom.size(),
2 * crate::cartridge::file_loader::PRG_UNIT_SIZE as usize
);
assert_eq!(
chr_rom.size(),
1 * crate::cartridge::file_loader::CHR_UNIT_SIZE as usize
);
assert_eq!(prg_rom.size(), 2 * PRG_UNIT_SIZE as usize);
assert_eq!(chr_rom.size(), 1 * CHR_UNIT_SIZE as usize);
}
}
11 changes: 11 additions & 0 deletions emulator/src/cartridge/common/enums/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[derive(thiserror::Error, Debug)]
pub enum NesRomReadError {
#[error("file format not supported")]
FileFormatNotSupported,

#[error("missing magic bytes")]
MissingMagicBytes,

#[error("missing prg rom")]
MissingPrgRom,
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,5 @@
use std::fmt::Debug;
pub enum Nes {
Ines,
Nes2,
}

impl Debug for Nes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Nes::Ines => write!(f, "Ines"),
Nes::Nes2 => write!(f, "Nes2"),
}
}
}

impl PartialEq for Nes {
fn eq(&self, other: &Self) -> bool {
matches!(
(self, other),
(Nes::Ines, Nes::Ines) | (Nes::Nes2, Nes::Nes2)
)
}
}
pub enum Mirroring {
Horizontal,
Vertical,
Expand Down
4 changes: 4 additions & 0 deletions emulator/src/cartridge/common/enums/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod mirroring;

pub mod errors;
pub mod nes;
24 changes: 24 additions & 0 deletions emulator/src/cartridge/common/enums/nes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::fmt::Debug;

pub enum Nes {
Ines,
Nes2,
}

impl Debug for Nes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Nes::Ines => write!(f, "Ines"),
Nes::Nes2 => write!(f, "Nes2"),
}
}
}

impl PartialEq for Nes {
fn eq(&self, other: &Self) -> bool {
matches!(
(self, other),
(Nes::Ines, Nes::Ines) | (Nes::Nes2, Nes::Nes2)
)
}
}
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,27 +1,4 @@
use std::io::Read;
use std::path::Path;

pub const NES_FILE_MAGIC_BYTES: [u8; 4] = ['N' as u8, 'E' as u8, 'S' as u8, 0x1A];
pub const PRG_UNIT_SIZE: u16 = 16;
pub const CHR_UNIT_SIZE: u16 = 8;

#[derive(thiserror::Error, Debug)]
pub enum NesRomReadError {
#[error("file format not supported")]
FileFormatNotSupported,

#[error("missing magic bytes")]
MissingMagicBytes,

#[error("missing prg rom")]
MissingPrgRom,
}

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 @@ -39,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;
19 changes: 11 additions & 8 deletions emulator/src/cartridge/formats/i_nes.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::cartridge::cartridge::CartridgeData;
use crate::cartridge::common::enums::Mirroring;
use crate::cartridge::file_loader::read_banks;
use crate::cartridge::file_loader::{
FileLoadable, NesRomReadError, CHR_UNIT_SIZE, NES_FILE_MAGIC_BYTES, PRG_UNIT_SIZE,
};
use crate::cartridge::common::enums::mirroring::Mirroring;
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;
use std::io::{BufReader, Read};
use std::path::Path;

use crate::cartridge::common::consts::{CHR_UNIT_SIZE, NES_FILE_MAGIC_BYTES, PRG_UNIT_SIZE};
use crate::cartridge::common::enums::errors::NesRomReadError;
use std::fmt::Debug;

// Bytes Description
Expand Down Expand Up @@ -188,13 +188,16 @@ impl CartridgeData for Ines {
}

fn chr_rom(&self) -> &ChrRom {
self.chr_rom.as_ref().unwrap()
match self.chr_rom.as_ref() {
Some(x) => x,
None => panic!("CHR ROM is not present"),
}
}
}
#[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
Loading
Loading