forked from pcwalton/sprocketnes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapper.rs
369 lines (323 loc) · 10.6 KB
/
mapper.rs
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//
// sprocketnes/mapper.rs
//
// Author: Patrick Walton
//
use rom::Rom;
use util;
#[deriving(Eq)]
pub enum MapperResult {
Continue,
Irq,
}
pub trait Mapper {
fn prg_loadb(&mut self, addr: u16) -> u8;
fn prg_storeb(&mut self, addr: u16, val: u8);
fn chr_loadb(&mut self, addr: u16) -> u8;
fn chr_storeb(&mut self, addr: u16, val: u8);
fn next_scanline(&mut self) -> MapperResult;
}
pub fn create_mapper(rom: ~Rom) -> ~Mapper:Send {
match rom.header.ines_mapper() {
0 => {
~Nrom {
rom: rom,
} as ~Mapper:Send
},
1 => ~SxRom::new(rom) as ~Mapper:Send,
4 => ~TxRom::new(rom) as ~Mapper:Send,
_ => fail!("unsupported mapper")
}
}
//
// Mapper 0 (NROM)
//
// See http://wiki.nesdev.com/w/index.php/NROM
//
// TODO: RAM.
pub struct Nrom {
pub rom: ~Rom,
}
impl Mapper for Nrom {
fn prg_loadb(&mut self, addr: u16) -> u8 {
if addr < 0x8000 {
0u8
} else if self.rom.prg.len() > 16384 {
*self.rom.prg.get(addr as uint & 0x7fff)
} else {
*self.rom.prg.get(addr as uint & 0x3fff)
}
}
fn prg_storeb(&mut self, _: u16, _: u8) {} // Can't store to PRG-ROM.
fn chr_loadb(&mut self, addr: u16) -> u8 { *self.rom.chr.get(addr as uint) }
fn chr_storeb(&mut self, _: u16, _: u8) {} // Can't store to CHR-ROM.
fn next_scanline(&mut self) -> MapperResult { Continue }
}
//
// Mapper 1 (SxROM/MMC1)
//
// See http://wiki.nesdev.com/w/index.php/Nintendo_MMC1
//
struct SxCtrl{ val: u8 }
impl Deref<u8> for SxCtrl {
fn deref<'a>(&'a self) -> &'a u8 {
&self.val
}
}
pub enum Mirroring {
OneScreenLower,
OneScreenUpper,
Vertical,
Horizontal,
}
enum SxPrgBankMode {
Switch32K, // Switch 32K at $8000, ignore low bit
FixFirstBank, // Fix first bank at $8000, switch 16K bank at $C000
FixLastBank, // Fix last bank at $C000, switch 16K bank at $8000
}
impl SxCtrl {
fn prg_rom_mode(self) -> SxPrgBankMode {
match (*self >> 2) & 3 {
0 | 1 => Switch32K,
2 => FixFirstBank,
3 => FixLastBank,
_ => fail!("can't happen")
}
}
}
struct SxRegs {
ctrl: SxCtrl, // $8000-$9FFF
chr_bank_0: u8, // $A000-$BFFF
chr_bank_1: u8, // $C000-$DFFF
prg_bank: u8, // $E000-$FFFF
}
pub struct SxRom {
rom: ~Rom,
regs: SxRegs,
// The internal accumulator.
accum: u8,
// The write count. At the 5th write, we update the register.
write_count: u8,
prg_ram: ~([u8, ..8192]),
chr_ram: ~([u8, ..8192]),
}
impl SxRom {
fn new(rom: ~Rom) -> SxRom {
SxRom {
rom: rom,
regs: SxRegs {
ctrl: SxCtrl{val: 3 << 2},
chr_bank_0: 0,
chr_bank_1: 0,
prg_bank: 0,
},
accum: 0,
write_count: 0,
prg_ram: ~([ 0, ..8192 ]),
chr_ram: ~([ 0, ..8192 ]),
}
}
}
impl Mapper for SxRom {
fn prg_loadb(&mut self, addr: u16) -> u8 {
if addr < 0x8000 {
0u8
} else if addr < 0xc000 {
let bank = match self.regs.ctrl.prg_rom_mode() {
Switch32K => self.regs.prg_bank & 0xfe,
FixFirstBank => 0,
FixLastBank => self.regs.prg_bank,
};
*self.rom.prg.get((bank as uint * 16384) | ((addr & 0x3fff) as uint))
} else {
let bank = match self.regs.ctrl.prg_rom_mode() {
Switch32K => (self.regs.prg_bank & 0xfe) | 1,
FixFirstBank => self.regs.prg_bank,
FixLastBank => (*self.rom).header.prg_rom_size - 1,
};
*self.rom.prg.get((bank as uint * 16384) | ((addr & 0x3fff) as uint))
}
}
fn prg_storeb(&mut self, addr: u16, val: u8) {
if addr < 0x8000 {
return;
}
// Check the reset flag.
if (val & 0x80) != 0 {
self.write_count = 0;
self.accum = 0;
self.regs.ctrl = SxCtrl{val: *self.regs.ctrl | (3 << 2)};
return;
}
// Write the lowest bit of the value into the right location of the accumulator.
self.accum = self.accum | ((val & 1) << self.write_count);
self.write_count += 1;
if self.write_count == 5 {
self.write_count = 0;
// Write to the right internal register.
if addr <= 0x9fff {
self.regs.ctrl = SxCtrl{val: self.accum};
} else if addr <= 0xbfff {
self.regs.chr_bank_0 = self.accum;
} else if addr <= 0xdfff {
self.regs.chr_bank_1 = self.accum;
} else {
self.regs.prg_bank = self.accum;
}
self.accum = 0;
}
}
// FIXME: Apparently this mapper can have CHR-ROM as well. Handle this case.
fn chr_loadb(&mut self, addr: u16) -> u8 { self.chr_ram[addr as uint] }
fn chr_storeb(&mut self, addr: u16, val: u8) { self.chr_ram[addr as uint] = val }
fn next_scanline(&mut self) -> MapperResult { Continue }
}
//
// Mapper 4 (TxROM/MMC3)
//
// See http://wiki.nesdev.com/w/index.php/MMC3
//
struct TxBankSelect{ val: u8 }
impl Deref<u8> for TxBankSelect {
fn deref<'a>(&'a self) -> &'a u8 {
&self.val
}
}
enum TxPrgBankMode {
Swappable8000,
SwappableC000,
}
impl TxBankSelect {
fn bank_update_select(self) -> u8 { *self & 0x7 }
fn prg_bank_mode(self) -> TxPrgBankMode {
if (*self & 0x40) == 0 { Swappable8000 } else { SwappableC000 }
}
fn chr_a12_inversion(self) -> bool { (*self & 0x80) != 0 }
}
struct TxRegs {
bank_select: TxBankSelect, // Bank select (0x8000-0x9ffe even)
}
struct TxRom {
rom: ~Rom,
regs: TxRegs,
prg_ram: ~([u8, ..8192]),
chr_banks_2k: [u8, ..2], // 2KB CHR-ROM banks
chr_banks_1k: [u8, ..4], // 1KB CHR-ROM banks
prg_banks: [u8, ..2], // 8KB PRG-ROM banks
scanline_counter: u8,
irq_reload: u8, // Copied into the scanline counter when it hits zero.
irq_enabled: bool,
}
impl TxRom {
fn new(rom: ~Rom) -> TxRom {
TxRom {
rom: rom,
regs: TxRegs { bank_select: TxBankSelect{val: 0} },
prg_ram: ~([ 0, ..8192 ]),
chr_banks_2k: [ 0, 0 ],
chr_banks_1k: [ 0, 0, 0, 0 ],
prg_banks: [ 0, 0 ],
scanline_counter: 0,
irq_reload: 0,
irq_enabled: false,
}
}
fn prg_bank_count(&self) -> u8 { self.rom.header.prg_rom_size * 2 }
}
impl Mapper for TxRom {
fn prg_loadb(&mut self, addr: u16) -> u8 {
if addr < 0x6000 {
0u8
} else if addr < 0x8000 {
self.prg_ram[addr as uint & 0x1fff]
} else if addr < 0xa000 {
// $8000-$9FFF might be switchable or fixed to the second to last bank.
let bank = match self.regs.bank_select.prg_bank_mode() {
Swappable8000 => self.prg_banks[0],
SwappableC000 => self.prg_bank_count() - 2,
};
*self.rom.prg.get((bank as uint * 8192) | (addr as uint & 0x1fff))
} else if addr < 0xc000 {
// $A000-$BFFF is switchable.
*self.rom.prg.get((self.prg_banks[1] as uint * 8192) | (addr as uint & 0x1fff))
} else if addr < 0xe000 {
// $C000-$DFFF might be switchable or fixed to the second to last bank.
let bank = match self.regs.bank_select.prg_bank_mode() {
Swappable8000 => self.prg_bank_count() - 2,
SwappableC000 => self.prg_banks[0],
};
*self.rom.prg.get((bank as uint * 8192) | (addr as uint & 0x1fff))
} else {
// $E000-$FFFF is fixed to the last bank.
let bank = self.prg_bank_count() - 1;
*self.rom.prg.get((bank as uint * 8192) | (addr as uint & 0x1fff))
}
}
fn prg_storeb(&mut self, addr: u16, val: u8) {
if addr < 0x6000 {
return;
}
if addr < 0x8000 {
self.prg_ram[addr as uint & 0x1fff] = val;
} else if addr < 0xa000 {
if (addr & 1) == 0 {
// Bank select.
self.regs.bank_select = TxBankSelect{val: val};
} else {
// Bank data.
let bank_update_select = self.regs.bank_select.bank_update_select() as uint;
match bank_update_select {
0..1 => self.chr_banks_2k[bank_update_select] = val,
2..5 => self.chr_banks_1k[bank_update_select - 2] = val,
6..7 => self.prg_banks[bank_update_select - 6] = val,
_ => fail!()
}
}
} else if addr < 0xc000 {
// TODO: Mirroring and PRG-RAM protect
} else if addr < 0xe000 {
if (addr & 1) == 0 {
// IRQ latch.
self.irq_reload = val;
} else {
// IRQ reload.
self.scanline_counter = self.irq_reload;
}
} else {
// IRQ enable.
self.irq_enabled = (addr & 1) == 1;
}
}
fn chr_loadb(&mut self, addr: u16) -> u8 {
let (bank, two_kb) = match (addr, self.regs.bank_select.chr_a12_inversion()) {
(0x0000..0x07ff, false) | (0x1000..0x17ff, true) => (self.chr_banks_2k[0], true),
(0x0800..0x0fff, false) | (0x1800..0x1fff, true) => (self.chr_banks_2k[1], true),
(0x1000..0x13ff, false) | (0x0000..0x03ff, true) => (self.chr_banks_1k[0], false),
(0x1400..0x17ff, false) | (0x0400..0x07ff, true) => (self.chr_banks_1k[1], false),
(0x1800..0x1bff, false) | (0x0800..0x0bff, true) => (self.chr_banks_1k[2], false),
(0x1c00..0x1fff, false) | (0x0c00..0x0fff, true) => (self.chr_banks_1k[3], false),
_ => return 0,
};
if two_kb {
*self.rom.chr.get((bank as uint * 1024) + (addr as uint & 0x7ff))
} else {
*self.rom.chr.get((bank as uint * 1024) | (addr as uint & 0x3ff))
}
}
fn chr_storeb(&mut self, _: u16, _: u8) {
// TODO: CHR-RAM
}
fn next_scanline(&mut self) -> MapperResult {
if self.scanline_counter != 0 {
self.scanline_counter -= 1;
if self.scanline_counter == 0 {
self.scanline_counter = self.irq_reload;
if self.irq_enabled {
util::debug_print("*** Generated IRQ! ***");
return Irq;
}
}
}
Continue
}
}