experimenting with more mappers, nametable adjustment, four screen mirroring

This commit is contained in:
Theron 2020-01-15 19:27:53 -06:00
parent c12022e2ed
commit 10370a0f16
8 changed files with 265 additions and 32 deletions

View File

@ -1,6 +1,6 @@
# nestur
This is an NES emulator and a work in progress. There are still some minor bugs and the audio is kind of scratchy. I've mostly tested on Donkey Kong, Super Mario Bros., and Zelda so far. There are plenty of full-featured emulators out there; this is primarily an educational project but I do want it to run well. SDL2 is the only dependency, it's NTSC timing, and contains no `unsafe` code.
Nestur is an NES emulator and a work in progress. There are still some minor bugs and the audio is kind of scratchy. I've mostly tested on Donkey Kong, Super Mario Bros., and Zelda so far. There are plenty of full-featured emulators out there; this is primarily an educational project but I do want it to run well. SDL2 is the only dependency, it's NTSC timing, and contains no `unsafe` code.
<img src="pics/smb.png" width=350> <img src="pics/zelda_dungeon.png" width=350>
@ -20,13 +20,25 @@ ___________________
```
The code aims to follow the explanations from the [NES dev wiki](https://wiki.nesdev.com/w/index.php/NES_reference_guide) where possible, especially in the PPU, and the comments quote from it often. Thanks to everyone who contributes to that wiki/forum, and to Michael Fogleman's [NES](https://github.com/fogleman/nes) and Scott Ferguson's [Fergulator](https://github.com/scottferg/Fergulator) for getting me unstuck at several points.
## Compilation and Use
1. Install Rust: https://www.rust-lang.org/tools/install
2. Configure SDL2 for your platform:
- Windows: `SDL2.dll` is already in the repo so you don't have to do anything.
- macOS: Install [Homebrew](https://brew.sh/) and run `brew install sdl2`
- Linux: `sudo apt-get install libsdl2-dev` (or whatever your package manager is)
3. `cd nestur/ && cargo build --release`
4. The `nestur` executable or `nestur.exe` will be in `nestur/target/release`.
5. Run with `$ ./nestur path/to/rom_filename.nes` or `> nestur.exe path\to\rom_filename.nes`.
6. If the game uses battery-backed RAM (if it can save data when turned off), a save file like `rom_filename.sav` will be created in the same folder as the ROM when the program is exited. When Nestur is run again, it will look for a file matching the ROM name, with a `.sav` extension instead of `.nes`.
## To do:
- More mappers (only mappers 0 (NROM) and 1 (MMC1) implemented so far)
- DMC audio channel, high- and low-pass filters
- Save/load functionality and battery-backed RAM solution
- Save state/load functionality
- Player 2 controller?

48
src/cartridge/cnrom.rs Normal file
View File

@ -0,0 +1,48 @@
use super::{Cartridge, Mapper, Mirror};
pub struct Cnrom {
cart: Cartridge,
chr_bank_select: usize,
}
impl Cnrom {
pub fn new(cart: Cartridge) -> Self {
Cnrom{
cart: cart,
chr_bank_select: 0,
}
}
}
impl Mapper for Cnrom {
fn read(&mut self, address: usize) -> u8 {
let cl = self.cart.chr_rom.len();
let pl = self.cart.prg_rom.len();
let addr = address % 0x4000;
match address {
0x0000..=0x1FFF => self.cart.chr_rom[self.chr_bank_select][address],
0x8000..=0xBFFF => self.cart.prg_rom[0][addr],
0xC000..=0xFFFF => self.cart.prg_rom[pl-1][addr],
_ => panic!("bad address read from CNROM mapper: 0x{:X}", address),
}
}
fn write(&mut self, address: usize, value: u8) {
match address {
0x8000..=0xFFFF => self.chr_bank_select = (value & 0b11) as usize,
_ => println!("bad address written to CNROM mapper: 0x{:X}", address),
}
}
fn get_mirroring(&mut self) -> Mirror {
self.cart.mirroring
// if self.cart.four_screen_vram {
// Mirror::FourScreen
// } else {
// self.cart.mirroring
// }
}
fn load_battery_backed_ram(&mut self) {}
fn save_battery_backed_ram(&self) {}
}

96
src/cartridge/mmc3.rs Normal file
View File

@ -0,0 +1,96 @@
use super::{Cartridge, Mapper, Mirror};
pub struct Mmc3 {
cart: Cartridge,
reg0: u8,
reg1: u8,
reg2: u8,
reg3: u8,
reg4: u8,
reg5: u8,
reg6: u8,
reg7: u8,
irq_counter: u8,
irq_latch: u8,
}
impl Mmc3 {
pub fn new(cart: Cartridge) -> Self {
Mmc3{
cart: cart,
reg0: 0,
reg1: 0,
reg2: 0,
reg3: 0,
reg4: 0,
reg5: 0,
reg6: 0,
reg7: 0,
}
}
}
impl Mmc3 {
}
impl Mapper for Mmc3 {
fn read(&mut self, address: usize) -> u8 {
match address {
0x0000..=0x07FF => ,
0x0800..=0x0FFF => ,
0x1000..=0x13FF => ,
0x1400..=0x17FF => ,
0x1800..=0x1BFF => ,
0x1C00..=0x1FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
}
}
fn write(&mut self, address: usize, value: u8) {
match address % 2 == 0 {
true => { // even
match address {
0x8000..=0x9FFF => self.bank_select(address),
0xA000..=0xBFFF => self.mirroring = if value & 1 == 0 {Mirror::Vertical} else {Mirror::Horizontal},
0xC000..=0xDFFF => ,
0x1400..=0x17FF => ,
0x1800..=0x1BFF => ,
0x1C00..=0x1FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
}
},
false => { // odd
match address {
0x8000..=0x9FFF => self.bank_data(value),
0xA000..=0xBFFF => self.prg_ram_protect(value),
0xC000..=0xDFFF => ,
0x1400..=0x17FF => ,
0x1800..=0x1BFF => ,
0x1C00..=0x1FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
0x6000..=0x7FFF => ,
}
},
}
fn get_mirroring(&mut self) -> Mirror {}
fn load_battery_backed_ram(&mut self) {}
fn save_battery_backed_ram(&self) {}
}

View File

@ -1,8 +1,14 @@
mod nrom;
mod mmc1;
mod uxrom;
mod cnrom;
// mod mmc3;
use nrom::Nrom;
use mmc1::Mmc1;
use uxrom::Uxrom;
use cnrom::Cnrom;
// use mmc3::Mmc3;
use std::cell::RefCell;
use std::rc::Rc;
@ -22,6 +28,7 @@ pub enum Mirror {
HighBank,
Horizontal,
Vertical,
FourScreen,
}
pub fn get_mapper() -> Rc<RefCell<dyn Mapper>> {
@ -30,6 +37,9 @@ pub fn get_mapper() -> Rc<RefCell<dyn Mapper>> {
match num {
0 => Rc::new(RefCell::new(Nrom::new(cart))),
1 => Rc::new(RefCell::new(Mmc1::new(cart))),
2 => Rc::new(RefCell::new(Uxrom::new(cart))),
3 => Rc::new(RefCell::new(Cnrom::new(cart))),
// 4 => Rc::new(RefCell::new(Mmc3::new(cart))),
_ => panic!("unimplemented mapper: {}", num),
}
}
@ -41,7 +51,7 @@ pub struct Cartridge {
pub mirroring: Mirror, // 0 horizontal, 1 vertical
battery_backed_ram: bool, // 1: Cartridge contains battery-backed PRG RAM ($6000-7FFF) or other persistent memory
trainer_present: bool, // 1: 512-byte trainer at $7000-$71FF (stored before PRG data)
_four_screen_vram: u8, // 1: Ignore mirroring control or above mirroring bit; instead provide four-screen VRAM
four_screen_vram: bool, // 1: Ignore mirroring control or above mirroring bit; instead provide four-screen VRAM
// TODO: other iNES header flags
pub prg_rom: Vec<Vec<u8>>, // 16 KiB chunks for CPU
@ -68,7 +78,7 @@ impl Cartridge {
mirroring: if data[6] & (1 << 0) == 0 {Mirror::Horizontal} else {Mirror::Vertical},
battery_backed_ram: data[6] & (1 << 1) != 0,
trainer_present: data[6] & (1 << 2) != 0,
_four_screen_vram: (data[6] & (1 << 3) != 0) as u8,
four_screen_vram: data[6] & (1 << 3) != 0,
prg_rom: Vec::new(),
chr_rom: Vec::new(),
all_data: data,

View File

@ -31,7 +31,7 @@ impl Mapper for Nrom {
0xC000..=0xFFFF => {
self.cart.prg_rom[pl-1][addr]
},
_ => panic!("bad address sent to NROM mapper: 0x{:X}", address),
_ => {println!("bad address read from NROM mapper: 0x{:X}", address); 0},
}
}
@ -45,13 +45,9 @@ impl Mapper for Nrom {
self.cart.chr_rom[cl-1][address] = value;
}
},
0x8000..=0xBFFF => {
self.cart.prg_rom[0][addr] = value;
},
0xC000..=0xFFFF => {
self.cart.prg_rom[pl-1][addr] = value;
},
_ => panic!("bad address sent to NROM mapper: 0x{:X}", address),
0x8000..=0xBFFF => self.cart.prg_rom[0][addr] = value,
0xC000..=0xFFFF => self.cart.prg_rom[pl-1][addr] = value,
_ => println!("bad address written to NROM mapper: 0x{:X}", address),
}
}

53
src/cartridge/uxrom.rs Normal file
View File

@ -0,0 +1,53 @@
use super::{Cartridge, Mapper, Mirror};
pub struct Uxrom {
cart: Cartridge,
chr_ram: Vec<u8>,
bank_select: usize,
}
impl Uxrom {
pub fn new(cart: Cartridge) -> Self {
Uxrom{
cart: cart,
chr_ram: vec![0; 0x2000],
bank_select: 0,
}
}
}
impl Mapper for Uxrom {
fn read(&mut self, address: usize) -> u8 {
match address {
0x0000..=0x1FFF => {
if self.cart.chr_rom_size > 0 {
self.cart.chr_rom[0][address]
} else {
self.chr_ram[address]
}
},
0x8000..=0xBFFF => self.cart.prg_rom[self.bank_select][address % 0x4000],
0xC000..=0xFFFF => self.cart.prg_rom[self.cart.prg_rom.len()-1][address % 0x4000],
_ => {println!("bad address read from UxROM mapper: 0x{:X}", address); 0},
}
}
fn write(&mut self, address: usize, value: u8) {
match address {
0x0000..=0x1FFF => {
if self.cart.chr_rom_size == 0 {
self.chr_ram[address] = value;
}
},
0x8000..=0xFFFF => self.bank_select = value as usize,
_ => println!("bad address written to UxROM mapper: 0x{:X}", address),
}
}
fn get_mirroring(&mut self) -> Mirror {
self.cart.mirroring
}
fn load_battery_backed_ram(&mut self) {}
fn save_battery_backed_ram(&self) {}
}

View File

@ -55,19 +55,28 @@ impl super::Ppu {
let base = address % 0x1000;
let offset = base % 0x0400;
match self.mapper.borrow_mut().get_mirroring() {
Mirror::LowBank => self.nametable_low_bank[offset],
Mirror::HighBank => self.nametable_high_bank[offset],
Mirror::LowBank => self.nametable_A[offset],
Mirror::HighBank => self.nametable_B[offset],
Mirror::Horizontal => {
match base {
0x0000..=0x07FF => self.nametable_low_bank[offset],
0x0800..=0x0FFF => self.nametable_high_bank[offset],
0x0000..=0x07FF => self.nametable_A[offset],
0x0800..=0x0FFF => self.nametable_B[offset],
_ => panic!("panicked reading nametable base: {}", base),
}
},
Mirror::Vertical => {
match base {
0x0000..=0x03FF | 0x0800..=0x0BFF => self.nametable_low_bank[offset],
0x0400..=0x07FF | 0x0C00..=0x0FFF => self.nametable_high_bank[offset],
0x0000..=0x03FF | 0x0800..=0x0BFF => self.nametable_A[offset],
0x0400..=0x07FF | 0x0C00..=0x0FFF => self.nametable_B[offset],
_ => panic!("panicked reading nametable base: {}", base),
}
},
Mirror::FourScreen => {
match base {
0x0000..=0x03FF => self.nametable_A[offset],
0x0400..=0x07FF => self.nametable_B[offset],
0x0800..=0x0BFF => self.nametable_C[offset],
0x0C00..=0x0FFF => self.nametable_D[offset],
_ => panic!("panicked reading nametable base: {}", base),
}
},
@ -78,23 +87,28 @@ impl super::Ppu {
let base = address % 0x1000;
let offset = base % 0x0400;
match self.mapper.borrow_mut().get_mirroring() {
Mirror::LowBank => {
self.nametable_low_bank[offset] = value;
},
Mirror::HighBank => {
self.nametable_high_bank[offset] = value;
},
Mirror::LowBank => self.nametable_A[offset] = value,
Mirror::HighBank => self.nametable_B[offset] = value,
Mirror::Horizontal => {
match base {
0x0000..=0x07FF => self.nametable_low_bank[offset] = value,
0x0800..=0x0FFF => self.nametable_high_bank[offset] = value,
0x0000..=0x07FF => self.nametable_A[offset] = value,
0x0800..=0x0FFF => self.nametable_B[offset] = value,
_ => panic!("panicked writing nametable base: {}", base),
}
},
Mirror::Vertical => {
match base {
0x0000..=0x03FF | 0x0800..=0x0BFF => self.nametable_low_bank[offset] = value,
0x0400..=0x07FF | 0x0C00..=0x0FFF => self.nametable_high_bank[offset] = value,
0x0000..=0x03FF | 0x0800..=0x0BFF => self.nametable_A[offset] = value,
0x0400..=0x07FF | 0x0C00..=0x0FFF => self.nametable_B[offset] = value,
_ => panic!("panicked writing nametable base: {}", base),
}
},
Mirror::FourScreen => {
match base {
0x0000..=0x03FF => self.nametable_A[offset] = value,
0x0400..=0x07FF => self.nametable_B[offset] = value,
0x0800..=0x0BFF => self.nametable_C[offset] = value,
0x0C00..=0x0FFF => self.nametable_D[offset] = value,
_ => panic!("panicked writing nametable base: {}", base),
}
},

View File

@ -26,8 +26,10 @@ pub struct Ppu {
// Pictures on http://wiki.nesdev.com/w/index.php/Mirroring refer to them as A and B.
// http://wiki.nesdev.com/w/index.php/MMC1 calls them higher and lower.
// They can be mirrored at certain memory ranges.
nametable_low_bank: Vec<u8>,
nametable_high_bank: Vec<u8>,
nametable_A: Vec<u8>,
nametable_B: Vec<u8>,
nametable_C: Vec<u8>,
nametable_D: Vec<u8>,
// The palette shared by both background and sprites.
// Consists of 32 bytes, each of which represents an index into the global PALETTE_TABLE.
@ -99,8 +101,10 @@ impl Ppu {
x: 0,
w: 0,
mapper: mapper,
nametable_low_bank: vec![0u8; 0x0400],
nametable_high_bank: vec![0u8; 0x0400],
nametable_A: vec![0u8; 0x0400],
nametable_B: vec![0u8; 0x0400],
nametable_C: vec![0u8; 0x0400],
nametable_D: vec![0u8; 0x0400],
palette_ram: vec![0u8; 0x0020],
background_pattern_sr_low: 0,
background_pattern_sr_high: 0,