added basic cartridge functions
This commit is contained in:
parent
a03d23097b
commit
d7bd8a6c24
BIN
roms/nestest.nes
Normal file
BIN
roms/nestest.nes
Normal file
Binary file not shown.
11
src/bus.rs
11
src/bus.rs
|
@ -2,10 +2,12 @@ use std::cell::RefCell;
|
|||
use std::rc::{Rc, Weak};
|
||||
|
||||
use crate::cpu::CPU;
|
||||
use crate::cartridge::Cartridge;
|
||||
|
||||
pub struct Bus
|
||||
{
|
||||
cpu: Weak<RefCell<CPU>>,
|
||||
cartridge: Cartridge,
|
||||
|
||||
ram: Vec<u8>
|
||||
}
|
||||
|
@ -17,6 +19,7 @@ impl Bus
|
|||
Bus
|
||||
{
|
||||
cpu: Weak::new(),
|
||||
cartridge: Cartridge::new("roms/nestest.nes"),
|
||||
ram: vec![0; 0x800]
|
||||
}
|
||||
}
|
||||
|
@ -38,6 +41,12 @@ impl Bus
|
|||
|
||||
pub fn read_cpu(&self, addr: u16) -> u8
|
||||
{
|
||||
self.ram[addr as usize]
|
||||
match addr
|
||||
{
|
||||
0..=0x1FFF => self.ram[(addr & 0x7FF) as usize],
|
||||
0x8000..=0xFFFF => self.cartridge.read_prg(addr & 0x7FFF),
|
||||
|
||||
_ => panic!("Tried to access invalid memory address {}", addr)
|
||||
}
|
||||
}
|
||||
}
|
54
src/cartridge.rs
Normal file
54
src/cartridge.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
use std::fs;
|
||||
|
||||
struct Header
|
||||
{
|
||||
prg_blocks: u8,
|
||||
chr_blocks: u8
|
||||
}
|
||||
|
||||
pub struct Cartridge
|
||||
{
|
||||
header: Header,
|
||||
|
||||
prg: Vec<u8>,
|
||||
chr: Vec<u8>
|
||||
}
|
||||
|
||||
impl Cartridge
|
||||
{
|
||||
pub fn new(filepath: &str) -> Cartridge
|
||||
{
|
||||
let contents = fs::read(filepath).expect("Failed to load ROM");
|
||||
let iter = contents.iter();
|
||||
|
||||
let mut curr_pos: usize = 0;
|
||||
|
||||
let header_data = &contents[curr_pos..curr_pos + 16];
|
||||
let header = Header {
|
||||
prg_blocks: header_data[4],
|
||||
chr_blocks: header_data[5]
|
||||
};
|
||||
|
||||
// TODO: For now assume there is no trainer
|
||||
curr_pos = 16;
|
||||
let prg_data = &contents[curr_pos..(curr_pos + 0x4000 * header.prg_blocks as usize)];
|
||||
|
||||
curr_pos += 0x4000 * header.prg_blocks as usize;
|
||||
let chr_data = &contents[curr_pos..(curr_pos + 0x2000 * header.chr_blocks as usize)];
|
||||
|
||||
Cartridge
|
||||
{
|
||||
header: header,
|
||||
|
||||
prg: prg_data.to_vec(),
|
||||
chr: chr_data.to_vec()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: For now all memio is hardcoded to work with nestest.nes for testing
|
||||
|
||||
pub fn read_prg(&self, addr: u16) -> u8
|
||||
{
|
||||
self.prg[(addr & 0x3FFF) as usize]
|
||||
}
|
||||
}
|
|
@ -41,6 +41,9 @@ impl CPU
|
|||
self.x = 0;
|
||||
self.y = 0;
|
||||
self.sp = 0xFD;
|
||||
|
||||
// TODO: This is just for the nestest.nes
|
||||
self.pc = 0xC000;
|
||||
}
|
||||
|
||||
pub fn execute(&mut self)
|
||||
|
@ -51,7 +54,7 @@ impl CPU
|
|||
|
||||
match (opcode)
|
||||
{
|
||||
_ => panic!("Unimplemented opcode {}", opcode)
|
||||
_ => panic!("Unimplemented opcode {:X}", opcode)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
mod nes;
|
||||
mod bus;
|
||||
mod cpu;
|
||||
mod cartridge;
|
||||
|
||||
use nes::NES;
|
||||
|
||||
|
|
Loading…
Reference in a new issue