neser 0.1.0

NESER - NES Emulator in Rust - is a NES emulator written in Rust. It aims to be a high-quality, hardware-accurate emulator that is also easy to use and extend. It supports a wide range of NES games and features, including various mappers, audio processing, and input handling. NESER is designed to be modular and extensible, allowing developers to easily add new features or support for additional hardware. It can be run using one of two frontends: a native desktop application using SDL2, or a web application using WebAssembly. The desktop application provides a high-performance, feature-rich experience with support for various input devices and display options, while the web application allows users to play NES games directly in their browsers without needing to install any software in a BYOR manner (Bring Your Own Roms).
Documentation
use crate::bus::bus::BusDevice;
use crate::cartridge::Cartridge;
use crate::debugging::log_info;
use crate::ppu;
use std::cell::RefCell;
use std::ops::RangeInclusive;
use std::rc::Rc;

pub(crate) struct MapperDevice {
    cartridge: Rc<RefCell<Option<Rc<RefCell<Cartridge>>>>>,
    ppu: Rc<RefCell<ppu::Ppu>>,
}

impl MapperDevice {
    pub(crate) fn new(
        cartridge: Rc<RefCell<Option<Rc<RefCell<Cartridge>>>>>,
        ppu: Rc<RefCell<ppu::Ppu>>,
    ) -> Self {
        Self { cartridge, ppu }
    }
}

impl BusDevice for MapperDevice {
    fn read(&mut self, addr: u16, open_bus: u8, _is_dummy_read: bool) -> Option<u8> {
        let Some(cartridge) = self.cartridge.borrow().as_ref().cloned() else {
            return match addr {
                0x4020..=0x5FFF => Some(open_bus),
                0x6000..=0x7FFF => {
                    log_info(format!(
                        "Warning: Read from PRG-RAM {:04X} without cartridge, returning 0",
                        addr
                    ));
                    Some(0)
                }
                0x8000..=0xFFFF => panic!("No cartridge mapped, cannot read from {:04X}", addr),
                _ => None,
            };
        };

        Some(
            cartridge
                .borrow()
                .mapper()
                .read_prg_open_bus(addr, open_bus),
        )
    }

    fn write(&mut self, addr: u16, value: u8, _is_dummy_write: bool) -> bool {
        let Some(cartridge) = self.cartridge.borrow().as_ref().cloned() else {
            match addr {
                0x4020..=0x5FFF => log_info(format!(
                    "Warning: Write to mapper expansion area {:04X} without cartridge, ignored",
                    addr
                )),
                0x6000..=0x7FFF => log_info(format!(
                    "Warning: Write to PRG-RAM {:04X} without cartridge, ignored",
                    addr
                )),
                0x8000..=0xFFFF => log_info(format!(
                    "Warning: Write to PRG ROM area {:04X} without cartridge, ignored",
                    addr
                )),
                _ => {}
            }
            return true;
        };

        let old_mirroring = cartridge.borrow().mapper().get_mirroring();
        cartridge.borrow_mut().mapper_mut().write_prg(addr, value);
        let new_mirroring = cartridge.borrow().mapper().get_mirroring();
        if new_mirroring != old_mirroring {
            self.ppu.borrow_mut().set_mirroring(new_mirroring);
        }

        true
    }

    fn address_range(&self) -> RangeInclusive<u16> {
        0x4020..=0xFFFF
    }
}