#![allow(dead_code)]
use ppu::PPU;
use mos6502::CPU;
use cartridge::{MirrorType, Cartridge};
use core::cell::{UnsafeCell, Cell};
use core::ptr::null_mut;
pub trait VMem {
fn read(&self, addr: u16) -> u8;
fn write(&self, addr: u16, data: u8);
}
pub struct CPUMemory<'a> {
sram: UnsafeCell<[u8; 2048]>,
ppu: Cell<*mut PPU<'a>>,
cpu: Cell<*mut CPU<'a>>,
mapper: &'a VMem
}
impl<'a> CPUMemory<'a> {
pub fn new(ppu: *mut PPU<'a>, mapper: &'a VMem) -> Self {
CPUMemory{sram: UnsafeCell::new([0; 2048]),
cpu: Cell::new(null_mut()),
ppu: Cell::new(ppu),
mapper}
}
pub fn init(&self, cpu: *mut CPU<'a>) {
self.cpu.set(cpu);
}
}
impl<'a> VMem for CPUMemory<'a> {
fn read(&self, addr: u16) -> u8 {
if addr < 0x2000 {
unsafe{(*self.sram.get())[(addr & 0x07ff) as usize]}
} else if addr < 0x4000 {
let ppu = unsafe {&mut *self.ppu.get()};
match addr & 0x7 {
0x2 => ppu.read_status(),
0x4 => ppu.read_oamdata(),
0x7 => ppu.read_data(),
_ => 0
}
} else if addr < 0x6000 {
0
} else {
self.mapper.read(addr)
}
}
fn write(&self, addr: u16, data: u8) {
let ppu = unsafe {&mut *self.ppu.get()};
let cpu = unsafe {&mut *self.cpu.get()};
if addr < 0x2000 {
unsafe{(*self.sram.get())[(addr & 0x07ff) as usize] = data;}
} else if addr < 0x4000 {
match addr & 0x7 {
0x0 => ppu.write_ctl(data),
0x1 =>