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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use std::cell::Cell;
use crate::nes::{Nes, NesIo};
use crate::rom::Rom;

#[derive(Clone)]
pub enum Mapper {
    Nrom(NromMapper),
    Uxrom(UxromMapper),
}

impl Mapper {
    pub fn from_rom(rom: Rom) -> Self {
        match rom.header.mapper {
            0 => Mapper::Nrom(NromMapper::from_rom(rom)),
            2 => Mapper::Uxrom(UxromMapper::from_rom(rom)),
            mapper => {
                unimplemented!("Mapper number {}", mapper);
            }
        }
    }

    pub fn read_u8(&self, addr: u16) -> u8 {
        match self {
            Mapper::Nrom(mapper) => mapper.read_u8(addr),
            Mapper::Uxrom(mapper) => mapper.read_u8(addr),
        }
    }

    pub fn write_u8(&self, addr: u16, value: u8) {
        match self {
            Mapper::Nrom(mapper) => mapper.write_u8(addr, value),
            Mapper::Uxrom(mapper) => mapper.write_u8(addr, value),
        }
    }

    pub fn read_ppu_u8(&self, nes: &Nes<impl NesIo>, addr: u16) -> u8 {
        match self {
            Mapper::Nrom(mapper) => mapper.read_ppu_u8(nes, addr),
            Mapper::Uxrom(mapper) => mapper.read_ppu_u8(nes, addr),
        }
    }

    pub fn write_ppu_u8(&self, nes: &Nes<impl NesIo>, addr: u16, value: u8) {
        match self {
            Mapper::Nrom(mapper) => mapper.write_ppu_u8(nes, addr, value),
            Mapper::Uxrom(mapper) => mapper.write_ppu_u8(nes, addr, value),
        }
    }
}



#[derive(Clone)]
pub struct NromMapper {
    rom: Rom,
    work_ram: Cell<[u8; 0x2000]>,
    chr_ram: Vec<Cell<u8>>,
}

impl NromMapper {
    pub fn from_rom(rom: Rom) -> Self {
        let work_ram = Cell::new([0; 0x2000]);
        let chr_ram = vec![Cell::new(0); rom.header.chr_ram_size_bytes];

        NromMapper { rom, work_ram, chr_ram }
    }

    pub fn read_u8(&self, addr: u16) -> u8 {
        let work_ram = self.work_ram();
        let prg_rom = &self.rom.prg_rom;

        match addr {
            0x0000..=0x5FFF => {
                panic!("Tried to read from mapper at address ${:04X}", addr);
            }
            0x6000..=0x7FFF => {
                let offset = ((addr - 0x6000) as usize) % work_ram.len();
                work_ram[offset].get()
            }
            0x8000..=0xFFFF => {
                let offset = ((addr - 0x8000) as usize) % prg_rom.len();
                prg_rom[offset]
            }
        }
    }

    pub fn write_u8(&self, addr: u16, value: u8) {
        let work_ram = self.work_ram();

        match addr {
            0x0000..=0x5FFF => {
                panic!("Tried to write to mapper at address ${:04X}", addr);
            }
            0x6000..=0x7FFF => {
                let offset = ((addr - 0x6000) as usize) % work_ram.len();
                work_ram[offset].set(value);
            }
            0x8000..=0xFFFF => { }
        }
    }

    pub fn read_ppu_u8(&self, nes: &Nes<impl NesIo>, addr: u16) -> u8 {
        let chr_rom = &self.rom.chr_rom;
        let chr_ram = &self.chr_ram;
        let ppu_ram = nes.ppu.ppu_ram();
        match addr {
            0x0000..=0x1FFF => {
                if chr_rom.is_empty() {
                    let offset = (addr as usize) % chr_ram.len();
                    chr_ram[offset].get()
                }
                else {
                    let offset = (addr as usize) % chr_rom.len();
                    chr_rom[offset]
                }
            }
            0x2000..=0x2FFF => {
                let offset = ((addr - 0x2000) as usize) % ppu_ram.len();
                ppu_ram[offset].get()
            }
            0x3000..=0x3EFF => {
                let offset = (addr - 0x3000) % 0x0FFF;
                self.read_ppu_u8(nes, offset + 0x2000)
            }
            0x3F00..=0xFFFF => {
                unreachable!();
            }
        }
    }

    pub fn write_ppu_u8(&self, nes: &Nes<impl NesIo>, addr: u16, value: u8) {
        let ppu_ram = nes.ppu.ppu_ram();
        let chr_rom = &self.rom.chr_rom;
        let chr_ram = &self.chr_ram;
        match addr {
            0x0000..=0x1FFF => {
                if chr_rom.is_empty() {
                    let offset = (addr as usize) % chr_ram.len();
                    chr_ram[offset].set(value);
                }
                else {
                    // Do nothing-- tried to write to read-only CHR ROM
                }
            }
            0x2000..=0x2FFF => {
                let offset = ((addr - 0x2000) as usize) % ppu_ram.len();
                ppu_ram[offset].set(value)
            }
            0x3000..=0x3EFF => {
                let offset = (addr - 0x3000) % 0x0FFF;
                self.write_ppu_u8(nes, offset + 0x2000, value)
            }
            0x3F00..=0xFFFF => {
                unreachable!();
            }
        }
    }

    fn work_ram(&self) -> &[Cell<u8>] {
        let work_ram: &Cell<[u8]> = &self.work_ram;
        work_ram.as_slice_of_cells()
    }
}

#[derive(Clone)]
pub struct UxromMapper {
    rom: Rom,
    bank: Cell<usize>,
    work_ram: Cell<[u8; 0x2000]>,
    chr_ram: Vec<Cell<u8>>,
}

impl UxromMapper {
    pub fn from_rom(rom: Rom) -> Self {
        let work_ram = Cell::new([0; 0x2000]);
        let chr_ram = vec![Cell::new(0); rom.header.chr_ram_size_bytes];
        let bank = Cell::new(5);

        UxromMapper { rom, bank, work_ram, chr_ram }
    }

    pub fn banks<'a>(&'a self) -> impl ExactSizeIterator<Item=&'a [u8]> + 'a {
        self.rom.prg_rom.chunks(16_384)
    }

    pub fn read_u8(&self, addr: u16) -> u8 {
        let work_ram = self.work_ram();
        let mut banks = self.banks();

        match addr {
            0x0000..=0x5FFF => {
                panic!("Tried to read from mapper at address ${:04X}", addr);
            }
            0x6000..=0x7FFF => {
                let offset = ((addr - 0x6000) as usize) % work_ram.len();
                work_ram[offset].get()
            }
            0x8000..=0xBFFF => {
                let n = self.bank.get();
                let bank = banks.nth(n).unwrap();
                let offset = ((addr - 0x8000) as usize) % bank.len();
                bank[offset]
            }
            0xC000..=0xFFFF => {
                let bank = banks.last().unwrap();
                let offset = ((addr - 0xC000) as usize) % bank.len();
                bank[offset]
            }
        }
    }

    pub fn write_u8(&self, addr: u16, value: u8) {
        let work_ram = self.work_ram();
        let banks = self.banks();

        match addr {
            0x0000..=0x5FFF => {
                panic!("Tried to write to mapper at address ${:04X}", addr);
            }
            0x6000..=0x7FFF => {
                let offset = ((addr - 0x6000) as usize) % work_ram.len();
                work_ram[offset].set(value);
            }
            0x8000..=0xFFFF => {
                let new_bank = (value & 0b_0000_1111) as usize;
                if new_bank > banks.len() {
                    unimplemented!("UxROM tried to select bank by writing byte 0x{:02X}, but ROM only has {} bank(s)", value, banks.len());
                }

                self.bank.set(new_bank);
            }
        }
    }

    pub fn read_ppu_u8(&self, nes: &Nes<impl NesIo>, addr: u16) -> u8 {
        let chr_rom = &self.rom.chr_rom;
        let chr_ram = &self.chr_ram;
        let ppu_ram = nes.ppu.ppu_ram();
        match addr {
            0x0000..=0x1FFF => {
                if chr_rom.is_empty() {
                    let offset = (addr as usize) % chr_ram.len();
                    chr_ram[offset].get()
                }
                else {
                    let offset = (addr as usize) % chr_rom.len();
                    chr_rom[offset]
                }
            }
            0x2000..=0x2FFF => {
                let offset = ((addr - 0x2000) as usize) % ppu_ram.len();
                ppu_ram[offset].get()
            }
            0x3000..=0x3EFF => {
                let offset = (addr - 0x3000) % 0x0FFF;
                self.read_ppu_u8(nes, offset + 0x2000)
            }
            0x3F00..=0xFFFF => {
                unreachable!();
            }
        }
    }

    pub fn write_ppu_u8(&self, nes: &Nes<impl NesIo>, addr: u16, value: u8) {
        let ppu_ram = nes.ppu.ppu_ram();
        let chr_rom = &self.rom.chr_rom;
        let chr_ram = &self.chr_ram;
        match addr {
            0x0000..=0x1FFF => {
                if chr_rom.is_empty() {
                    let offset = (addr as usize) % chr_ram.len();
                    chr_ram[offset].set(value);
                }
                else {
                    // Do nothing-- tried to write to read-only CHR ROM
                }
            }
            0x2000..=0x2FFF => {
                let offset = ((addr - 0x2000) as usize) % ppu_ram.len();
                ppu_ram[offset].set(value)
            }
            0x3000..=0x3EFF => {
                let offset = (addr - 0x3000) % 0x0FFF;
                self.write_ppu_u8(nes, offset + 0x2000, value)
            }
            0x3F00..=0xFFFF => {
                unreachable!();
            }
        }
    }

    fn work_ram(&self) -> &[Cell<u8>] {
        let work_ram: &Cell<[u8]> = &self.work_ram;
        work_ram.as_slice_of_cells()
    }
}