Skip to content

Commit

Permalink
Read basics of INES/NES2.0 files to a cart file
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPWill committed Nov 26, 2015
1 parent 702d7e4 commit cdddd90
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 23 deletions.
97 changes: 75 additions & 22 deletions src/cart.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,87 @@
use std::iter::repeat;

use mem::Mem;

pub struct Cart;
pub struct Cart
{
prg_rom_pages: Vec<PrgRom>,
prg_ram_pages: Vec<PrgRam>,
chr_rom_pages: Vec<ChrMem>,
chr_ram_pages: Vec<ChrMem>,
sram_pages: Vec<Sram>,
}

struct PrgMem
impl Cart
{
pub fn new(num_prg_ram: usize, num_chr_ram: usize, num_sram: usize) -> Cart
{
let mut cart = Cart{
prg_rom_pages: Vec::new(),
chr_rom_pages: Vec::new(),
prg_ram_pages: Vec::new(),
chr_ram_pages: Vec::new(),
sram_pages: Vec::new(),
};
for _ in 0..num_prg_ram { cart.prg_ram_pages.push(PrgRam::new()); }
for _ in 0..num_chr_ram { cart.chr_ram_pages.push(ChrMem::new_ram()); }
for _ in 0..num_sram { cart.sram_pages.push(Sram::new()); }
return cart;
}

pub fn load_prg_rom(&mut self, prg_rom: &[u8; 0x4000])
{
self.prg_rom_pages.push(PrgRom::new(*prg_rom));
}
pub fn load_chr_rom(&mut self, chr_rom: &[u8; 0x2000])
{
self.chr_rom_pages.push(ChrMem::new_rom(*chr_rom));
}
}

struct PrgRom
{
ram: bool,
mem: [u8; 0x4000],
}

impl PrgMem
impl PrgRom
{
fn new(ram: bool) -> PrgMem
fn new(data: [u8; 0x4000]) -> PrgRom
{
PrgMem{ram: ram, mem: [0; 0x4000]}
PrgRom{mem: data}
}
}

impl Mem<u16, u8> for PrgMem
impl Mem<u16, u8> for PrgRom
{
fn read_word(&self, addr: u16) -> u8
{
self.mem[(addr%0x4000) as usize]
}
fn write_word(&mut self, _: u16, _: u8)
{
panic!("Unable to write to PrgRom");
}
}

struct PrgRam
{
mem: [u8; 0x2000],
}

impl PrgRam
{
fn new() -> PrgRam { PrgRam{mem: [0; 0x2000]} }
}

impl Mem<u16, u8> for PrgRam
{
fn read_word(&self, addr: u16) -> u8
{
self.mem[(addr%0x2000) as usize]
}
fn write_word(&mut self, addr: u16, word: u8)
{
if self.ram
{
self.mem[(addr%0x4000) as usize] = word
}
else
{
panic!("Unable to write to PrgRom");
}
self.mem[(addr%0x2000) as usize] = word
}
}

Expand All @@ -43,10 +93,8 @@ struct ChrMem

impl ChrMem
{
fn new(ram: bool) -> ChrMem
{
ChrMem{ram: ram, mem: [0; 0x2000]}
}
fn new_rom(data: [u8; 0x2000]) -> ChrMem { ChrMem{ram: false, mem: data} }
fn new_ram() -> ChrMem { ChrMem{ram: true, mem: [0; 0x2000]} }
}

impl Mem<u16, u8> for ChrMem
Expand All @@ -70,18 +118,23 @@ impl Mem<u16, u8> for ChrMem

struct Sram
{
ram: [u8; 0x2000],
mem: [u8; 0x2000],
}

impl Sram
{
fn new() -> Sram { Sram{mem: [0; 0x2000]} }
}

impl Mem<u16, u8> for Sram
{
fn read_word(&self, addr: u16) -> u8
{
self.ram[(addr%0x2000) as usize]
self.mem[(addr%0x2000) as usize]
}
fn write_word(&mut self, addr: u16, word: u8)
{
self.ram[(addr%0x2000) as usize] = word
self.mem[(addr%0x2000) as usize] = word
}
}

Expand Down
27 changes: 26 additions & 1 deletion src/parser/nes_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,32 @@ impl NesParser for Parser
let mut header_bytes: [u8; 15] = [0; 15];
let _ = file.read_exact(&mut header_bytes);
let header = Nes2Header::new(header_bytes);
return Cart;
let mut cart =
if header.nes_2_rules
{
Cart::new((header.prgram_bat_backed + header.prgram_not_bat_backed) as usize,
(header.chrram_bat_backed + header.chrram_not_bat_backed) as usize,
0)
}
else
{
Cart::new(header.prg_ram_pages as usize, 0, 0)
};
// Read 16384 byte PrgRom blocks
for _ in 0..header.prg_pages
{
let mut buf: [u8; 0x4000] = [0; 0x4000];
let _ = file.read_exact(&mut buf);
cart.load_prg_rom(&buf);
}
// Read 8192 byte ChrRom blocks
for _ in 0..header.chr_pages
{
let mut buf: [u8; 0x2000] = [0; 0x2000];
let _ = file.read_exact(&mut buf);
cart.load_chr_rom(&buf);
}
return cart;
}
}

Expand Down

0 comments on commit cdddd90

Please sign in to comment.