1 #include "mapper_003.h"
2
3 #include <cassert>
4 #include <cstdio>
5 #include <cstring>
6
Mapper_003(const ROM_File & rom_file)7 Mapper_003::Mapper_003(const ROM_File& rom_file)
8 : Mapper(3, "CNROM", rom_file, 0x4000, 0x2000)
9 { this->mirror_mode = rom_file.meta.mirror_mode; }
10
peek(u16 addr) const11 u8 Mapper_003::peek(u16 addr) const {
12 // Wired to the PPU MMU
13 if (in_range(addr, 0x0000, 0x1FFF)) return this->chr_mem->peek(addr);
14
15 // Wired to the CPU MMU
16 if (in_range(addr, 0x4020, 0x5FFF)) return 0x00; // Nothing in "Expansion ROM"
17 if (in_range(addr, 0x6000, 0x7FFF)) return 0x00; // Nothing in SRAM
18 if (in_range(addr, 0x8000, 0xBFFF)) return this->prg_lo->peek(addr - 0x8000);
19 if (in_range(addr, 0xC000, 0xFFFF)) return this->prg_hi->peek(addr - 0xC000);
20
21 assert(false);
22 return 0;
23 }
24
write(u16 addr,u8 val)25 void Mapper_003::write(u16 addr, u8 val) {
26 // TODO: handle bus conflicts?
27
28 // Since there is potentially CHR RAM, try to write to it (if in range)
29 if (in_range(addr, 0x0000, 0x1FFF)) {
30 this->chr_mem->write(addr, val);
31 }
32
33 // Otherwise, handle writing to registers
34
35 if (in_range(addr, 0x8000, 0xFFFF)) {
36 this->reg.bank_select = val;
37 this->update_banks();
38 }
39 }
40
update_banks()41 void Mapper_003::update_banks() {
42 this->prg_lo = &this->get_prg_bank(0);
43 this->prg_hi = &this->get_prg_bank(1);
44
45 this->chr_mem = &this->get_chr_bank(this->reg.bank_select);
46 }
47
reset()48 void Mapper_003::reset() {
49 memset(&this->reg, 0, sizeof this->reg);
50 }
51