aboutsummaryrefslogtreecommitdiff
path: root/src/mapper.rs
blob: 210b59f1c9fad73a0a0a6235fc7746e19ab3ee34 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#![allow(dead_code)]
use memory::VMem;
use cartridge::{Cartridge, BankType};
use core::cell::UnsafeCell;

pub struct Mapper2<'a> {
    cart: &'a Cartridge,
    prg_bank1: UnsafeCell<&'a [u8]>,
    prg_bank2: UnsafeCell<&'a [u8]>,
    chr_bank: UnsafeCell<&'a mut [u8]>,
    sram: UnsafeCell<&'a mut [u8]>,
    prg_nbank: usize
}

impl<'a> VMem for Mapper2<'a> {
    fn read(&self, addr: u16) -> u8 {
        let addr = addr as usize;
        unsafe {
            if addr < 0x2000 {         /* 0x2000 size bank */
                (*self.chr_bank.get())[addr]
            } else if addr >= 0xc000 { /* 0x4000 size bank */
                (*self.prg_bank2.get())[addr - 0xc000]
            } else if addr >= 0x8000 { /* 0x4000 size bank */
                (*self.prg_bank1.get())[addr - 0x8000]
            } else if addr >= 0x6000 {
                (*self.sram.get())[addr - 0x6000]
            } else {
                panic!("unmapped address: 0x{:04x}", addr)
            }
        }
    }

    fn write(&self, addr: u16, data: u8) {
        let addr = addr as usize;
        unsafe {
            if addr < 0x2000 {
                (*self.chr_bank.get())[addr] = data;
            } else if addr >= 0x8000 {
                (*self.prg_bank1.get()) =
                    &*self.cart.get_bank(((data as usize) % self.prg_nbank) << 14,
                    0x4000,
                    BankType::PrgRom);
            } else if addr >= 0x6000 {
                (*self.sram.get())[addr - 0x6000] = data;
            } else {
                panic!("invalid write to address: 0x{:04x}", addr);
            }
        }
    }
}

impl<'a> Mapper2<'a> {
    pub fn new(cart: &'a Cartridge) -> Self {
        unsafe {
            let nbank = cart.get_size(BankType::PrgRom) >> 14;
            Mapper2{cart,
                prg_bank1: UnsafeCell::new(&*cart.get_bank(0, 0x4000, BankType::PrgRom)),
                prg_bank2: UnsafeCell::new(&*cart.get_bank((nbank - 1) << 14, 0x4000, BankType::PrgRom)),
                chr_bank: UnsafeCell::new(&mut *cart.get_bank(0, 0x2000, BankType::ChrRom)),
                sram: UnsafeCell::new(&mut *cart.get_bank(0, 0x2000, BankType::Sram)),
                prg_nbank: nbank}
        }
    }
}