-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapper.cpp
More file actions
71 lines (62 loc) · 1.6 KB
/
Mapper.cpp
File metadata and controls
71 lines (62 loc) · 1.6 KB
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
#pragma once
#include "Mapper.h"
Mapper::Mapper(uint8_t* rom) : rom(rom)
{
// Read infos from header
prgSize = rom[4] * 0x4000;
chrSize = rom[5] * 0x2000;
prgRamSize = rom[8] ? rom[8] * 0x2000 : 0x2000;
this->prg = &rom[0] + 16;
this->prgRam = new uint8_t[prgRamSize];
// CHR ROM:
if(chrSize)
this->chr = &rom[0] + 16 + prgSize;
// CHR RAM:
else
{
chrRam = true;
chrSize = 0x2000;
this->chr = new uint8_t[chrSize];
}
}
Mapper::~Mapper()
{
delete rom;
delete prgRam;
if(chrRam)
delete chr;
}
/* Access to memory */
uint8_t Mapper::read(uint16_t addr)
{
if(addr >= 0x8000)
return prg[prgMap[(addr - 0x8000) / 0x2000] + ((addr - 0x8000) % 0x2000)];
//return (prgSize);
else
return prgRam[addr - 0x6000];
}
uint8_t Mapper::chr_read(uint16_t addr)
{
return chr[chrMap[addr / 0x400] + (addr % 0x400)];
}
/* PRG mapping functions */
template <int pageKBs> void Mapper::map_prg(int slot, int bank)
{
if(bank < 0)
bank = (prgSize / (0x400 * pageKBs)) + bank;
for(int i = 0; i < (pageKBs / 8); i++)
prgMap[(pageKBs / 8) * slot + i] = (pageKBs * 0x400 * bank + 0x2000 * i) % prgSize;
}
template void Mapper::map_prg<32>(int, int);
template void Mapper::map_prg<16>(int, int);
template void Mapper::map_prg<8>(int, int);
/* CHR mapping functions */
template <int pageKBs> void Mapper::map_chr(int slot, int bank)
{
for(int i = 0; i < pageKBs; i++)
chrMap[pageKBs*slot + i] = (pageKBs * 0x400 * bank + 0x400 * i) % chrSize;
}
template void Mapper::map_chr<8>(int, int);
template void Mapper::map_chr<4>(int, int);
template void Mapper::map_chr<2>(int, int);
template void Mapper::map_chr<1>(int, int);