aboutsummaryrefslogtreecommitdiff
path: root/src/mapper.rs
blob: 93c6fa97e42539c373e4d56573870e0d0b054b47 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#![allow(dead_code)]
extern crate core;
use memory::VMem;
use cartridge::{Cartridge, BankType, MirrorType};

pub trait Mapper : VMem {
    fn get_cart(&self) -> &Cartridge;
}

pub struct Mapper1<'a, C> where C: Cartridge {
    cart: C,
    prg_banks: [&'a [u8]; 2],
    chr_banks: [&'a mut [u8]; 2],
    sram: &'a mut [u8],
    ctl_reg: u8,
    load_reg: u8,
    prg_nbank: usize, /* num of 16k PRG ROM banks */
    chr_nbank: usize /* num of 8k PRG ROM banks */
}

impl<'a, C> VMem for Mapper1<'a, C> where C: Cartridge {
    fn read(&self, addr: u16) -> u8 {
        let addr = addr as usize;
        if addr < 0x2000 {         /* 0x2000 size bank */
            self.chr_banks[(addr >> 12) & 1][addr & 0xfff]
        } else if addr >= 0x8000 { /* 0x4000 size bank */
            self.prg_banks[(addr >> 14) & 1][addr & 0x3fff]
        } else if addr >= 0x6000 {
            self.sram[addr - 0x6000]
        } else {
            panic!("unmapped address: 0x{:04x}", addr)
        }
    }

    fn write(&mut self, addr: u16, data: u8) {
        let addr = addr as usize;
        if addr < 0x2000 {
            self.chr_banks[(addr >> 12) & 1][addr & 0xfff] = data
        } else if addr >= 0x8000 {
            self.write_loadreg(addr as u16, data)
        } else if addr >= 0x6000 {
            self.sram[addr - 0x6000] = data
        } else {
            panic!("invalid write to address: 0x{:04x}", addr);
        }
    }
}

impl<'a, C> Mapper1<'a, C> where C: Cartridge {
    pub fn new(cart: C) -> Self {
        unsafe {
            let prg_nbank = cart.get_size(BankType::PrgRom) >> 14;
            let chr_nbank = cart.get_size(BankType::ChrRom) >> 13;
            let null = core::mem::uninitialized();
            let mut m = Mapper1{cart,
                        prg_nbank,
                        chr_nbank,
                        load_reg: 0x10,
                        ctl_reg: 0x0c,
                        prg_banks: [null; 2],
                        chr_banks: [core::mem::uninitialized(),
                                    core::mem::uninitialized()],
                        sram: core::mem::uninitialized()};
            {
                let c = &mut m.cart;
                    m.prg_banks[0] = &*c.get_bank(0, 0x4000, BankType::PrgRom);
                    m.prg_banks[1] = &*c.get_bank((prg_nbank - 1) << 14, 0x4000, BankType::PrgRom);
                    m.chr_banks[0] = &mut *c.get_bank(0, 0x1000, BankType::ChrRom);
                    m.chr_banks[1] = &mut *c.get_bank(0x1000, 0x1000, BankType::ChrRom);
                    m.sram = &mut *c.get_bank(0, 0x2000, BankType::Sram);
            }
            m
        }
    }

    fn write_loadreg(&mut self, addr: u16, data: u8) {
        if data & 0x80 == 0x80 {
            self.ctl_reg |= 0x0c;
            self.load_reg = 0x10;
            return
        }
        let triggered = self.load_reg & 1 == 1;
        self.load_reg = (self.load_reg >> 1) | ((data & 1) << 4);
        if triggered { unsafe {
            let load_reg = self.load_reg;
            match (addr >> 13) & 3 {
                0x0 => {
                    self.ctl_reg = load_reg;
                    self.cart.set_mirror_type(match load_reg & 3 {
                        0x0 => MirrorType::Single0,
                        0x1 => MirrorType::Single1,
                        0x2 => MirrorType::Vertical,
                        _ => MirrorType::Horizontal
                    });
                },
                0x1 => {
                    match (self.ctl_reg >> 4) & 1 {
                        0x0 => {
                            let base = ((load_reg & 0xfe) as usize % self.chr_nbank) << 13;
                            self.chr_banks[0] =
                                &mut *self.cart.get_bank(base, 0x1000, BankType::ChrRom);
                            self.chr_banks[1] =
                                &mut *self.cart.get_bank(base + 0x1000, 0x1000, BankType::ChrRom)
                        },
                        _ =>
                            self.chr_banks[0] = &mut *self.cart.get_bank(
                                (load_reg as usize % (self.chr_nbank << 1)) << 12,
                                0x1000, BankType::ChrRom)
                    }
                },
                0x2 => {
                    if (self.ctl_reg >> 4) & 1 == 1 {
                        self.chr_banks[1] = &mut *self.cart.get_bank(
                                (load_reg as usize % (self.chr_nbank << 1)) << 12,
                                0x1000, BankType::ChrRom)
                    }
                },
                0x3 => {
                    let load_reg = load_reg & 0xf;
                    match (self.ctl_reg >> 2) & 3 {
                        0x0 | 0x1 => {
                            let base = ((load_reg & 0xfe) as usize % (self.prg_nbank >> 1)) << 15;
                            self.prg_banks[0] =
                                &*self.cart.get_bank(base, 0x4000, BankType::PrgRom);
                            self.prg_banks[1] =
                                &*self.cart.get_bank(base + 0x4000, 0x4000, BankType::PrgRom)
                        },
                        0x2 => {
                            self.prg_banks[0] = &*self.cart.get_bank(0, 0x4000, BankType::PrgRom);
                            self.prg_banks[1] = &*self.cart.get_bank(
                                (load_reg as usize % self.prg_nbank) << 14,
                                0x4000, BankType::PrgRom);
                        },
                        0x3 => {
                            self.prg_banks[0] = &*self.cart.get_bank(
                                (load_reg as usize % self.prg_nbank) << 14,
                                0x4000, BankType::PrgRom);
                            self.prg_banks[1] = &*self.cart.get_bank(
                                (self.prg_nbank - 1) << 14,
                                0x4000, BankType::PrgRom);
                        }
                        _ => ()
                    }
                },
                _ => ()
            }
            self.load_reg = 0x10;
        }}
    }
}

impl<'a, C> Mapper for Mapper1<'a, C> where C: Cartridge {
    fn get_cart(&self) -> &Cartridge {&self.cart}
}

pub struct Mapper2<'a, C> where C: Cartridge {
    cart: C,
    prg_banks: [&'a [u8]; 2],
    chr_bank: &'a mut [u8],
    sram: &'a mut [u8],
    prg_nbank: usize
}

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

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

impl<'a, C> Mapper2<'a, C> where C: Cartridge {
    pub fn new(cart: C) -> Self {
        unsafe {
            let nbank = cart.get_size(BankType::PrgRom) >> 14;
            let null = core::mem::uninitialized();
            let mut m = Mapper2{cart,
                        prg_nbank: nbank,
                        prg_banks: [null; 2],
                        chr_bank: core::mem::uninitialized(),
                        sram: core::mem::uninitialized()};
            {
                let c = &mut m.cart;
                    m.prg_banks[0] = &*c.get_bank(0, 0x4000, BankType::PrgRom);
                    m.prg_banks[1] = &*c.get_bank((nbank - 1) << 14, 0x4000, BankType::PrgRom);
                    m.chr_bank = &mut *c.get_bank(0, 0x2000, BankType::ChrRom);
                    m.sram = &mut *c.get_bank(0, 0x2000, BankType::Sram);
            }
            m
        }
    }
}

impl<'a, C> Mapper for Mapper2<'a, C> where C: Cartridge {
    fn get_cart(&self) -> &Cartridge {&self.cart}
}