Implemented NROM mapper

This commit is contained in:
Steins7 2022-05-21 17:45:38 +02:00
parent f916f77641
commit 50ba57db74
3 changed files with 76 additions and 30 deletions

View File

@ -6,7 +6,6 @@ use cpu::Cpu;
mod bus; mod bus;
mod peripherals; mod peripherals;
use peripherals::mapper::Nrom;
mod utils; mod utils;
fn main() -> Result<(), &'static str> { fn main() -> Result<(), &'static str> {
@ -21,6 +20,7 @@ fn main() -> Result<(), &'static str> {
rc::Rc, rc::Rc,
cell::RefCell, cell::RefCell,
}; };
use peripherals::mapper::Nrom128;
// setup // setup
setup_logger().map_err(|_| "Failed to setup logger")?; setup_logger().map_err(|_| "Failed to setup logger")?;
@ -36,7 +36,7 @@ fn main() -> Result<(), &'static str> {
let mut renderer = FontRenderer::new(20.0, [0u8, 0, 0]); let mut renderer = FontRenderer::new(20.0, [0u8, 0, 0]);
// first image // first image
let mapper = Rc::new(RefCell::new(Nrom::new())); let mapper = Rc::new(RefCell::new(Nrom128::new(false)));
let mut cpu = Cpu::new(mapper); let mut cpu = Cpu::new(mapper);
let mut debug_str = String::new(); let mut debug_str = String::new();
write!(debug_str, "{:#?}", cpu).unwrap(); write!(debug_str, "{:#?}", cpu).unwrap();

View File

@ -1,5 +1,8 @@
use super::Peripheral; use super::Peripheral;
mod nrom;
pub use nrom::Nrom128;
//--Mapper struct----------------------------------------------------------------------------------- //--Mapper struct-----------------------------------------------------------------------------------
///A trait used as API for the different implementations of mappers in cartridges ///A trait used as API for the different implementations of mappers in cartridges
/// ///
@ -11,32 +14,4 @@ pub trait Mapper: Peripheral {
fn ppu_read_addr(&self, addr: u16) -> u8; fn ppu_read_addr(&self, addr: u16) -> u8;
} }
pub struct Nrom {
}
impl Peripheral for Nrom {
fn read_addr(&self, _addr: u16) -> u8 {
unimplemented!();
}
fn write_addr(&mut self, _addr: u16, _data: u8) {
unimplemented!();
}
}
impl Mapper for Nrom {
fn ppu_read_addr(&self, _addr: u16) -> u8 {
unimplemented!();
}
}
impl Nrom {
pub fn new() -> Self {
unimplemented!();
}
}

View File

@ -0,0 +1,71 @@
use super::{
Mapper,
super::{
Peripheral,
Ram,
},
};
pub struct Nrom128 {
prg_rom: Ram<0x4000>, //16KiB
prg_ram: Ram<0x2000>, //8KiB
chr_rom: Ram<0x2000>, //8KiB
v_ram: Ram<0x0800>, //2KiB
vertical: bool,
}
impl Peripheral for Nrom128 {
fn read_addr(&self, addr: u16) -> u8 {
match addr {
0x6000..=0x7FFF => self.prg_ram.read_addr(addr - 0x6000),
0x8000..=0xBFFF => self.prg_rom.read_addr((addr - 0x6000) % 0x4000),
//mirrored every 16KiB
_ => panic!("{}: Invalid address", addr),
}
}
fn write_addr(&mut self, addr: u16, data: u8) {
match addr {
0x6000..=0x7FFF => self.prg_ram.write_addr(addr - 0x6000, data),
_ => (), //writes are ignored for rom or invald addresses
}
}
}
impl Mapper for Nrom128 {
fn ppu_read_addr(&self, addr: u16) -> u8 {
match addr {
0x0000..=0x1FFF => self.chr_rom.read_addr(addr),
0x2000..=0x3EFF => {
match self.vertical {
false => match (addr - 0x2000) % 0x0FFF {
0x0000..=0x07FF => self.v_ram.read_addr(addr % 0x0400),
0x0800..=0x0FFF => self.v_ram.read_addr((addr % 0x0400) + 0x0800),
_ => panic!("Unexpected behaviour"),
},
true => self.v_ram.read_addr(addr % 0x0800),
}},
_ => panic!("{}: Invalid ppu address", addr),
}
}
}
impl Nrom128 {
pub fn new(vertical: bool) -> Self {
Self {
prg_rom: Ram::<0x4000>::new(),
prg_ram: Ram::<0x2000>::new(),
chr_rom: Ram::<0x2000>::new(),
v_ram: Ram::<0x0800>::new(),
vertical,
}
}
}