This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
LaiNES/src/mapper.cpp

73 lines
1.7 KiB
C++
Raw Normal View History

#include "ppu.hpp"
2014-03-19 15:55:18 +00:00
#include "mapper.hpp"
2014-06-01 15:06:30 +00:00
Mapper::Mapper(u8* rom) : rom(rom)
2014-03-19 15:55:18 +00:00
{
2014-03-19 16:21:57 +00:00
// Read infos from header:
prgSize = rom[4] * 0x4000;
chrSize = rom[5] * 0x2000;
prgRamSize = rom[8] ? rom[8] * 0x2000 : 0x2000;
set_mirroring((rom[6] & 1) ? PPU::VERTICAL : PPU::HORIZONTAL);
2014-03-19 15:55:18 +00:00
this->prg = rom + 16;
this->prgRam = new u8[prgRamSize];
2014-03-19 16:21:57 +00:00
// CHR ROM:
2014-03-19 15:55:18 +00:00
if (chrSize)
this->chr = rom + 16 + prgSize;
2014-03-19 16:21:57 +00:00
// CHR RAM:
2014-03-19 15:55:18 +00:00
else
{
2014-06-01 15:06:30 +00:00
chrRam = true;
2014-03-19 15:55:18 +00:00
chrSize = 0x2000;
this->chr = new u8[chrSize];
}
}
2014-06-01 15:06:30 +00:00
Mapper::~Mapper()
{
delete rom;
delete prgRam;
if (chrRam)
delete chr;
}
2014-03-19 15:55:18 +00:00
/* Access to memory */
u8 Mapper::read(u16 addr)
{
if (addr >= 0x8000)
return prg[prgMap[(addr - 0x8000) / 0x2000] + ((addr - 0x8000) % 0x2000)];
else
return prgRam[addr - 0x6000];
}
u8 Mapper::chr_read(u16 addr)
{
return chr[chrMap[addr / 0x400] + (addr % 0x400)];
}
/* PRG mapping functions */
2014-03-20 02:21:09 +00:00
template <int pageKBs> void Mapper::map_prg(int slot, int bank)
2014-03-19 15:55:18 +00:00
{
2014-03-20 02:21:09 +00:00
if (bank < 0)
bank = (prgSize / (0x400*pageKBs)) + bank;
2014-03-19 15:55:18 +00:00
2014-03-20 02:21:09 +00:00
for (int i = 0; i < (pageKBs/8); i++)
prgMap[(pageKBs/8) * slot + i] = (pageKBs*0x400*bank + 0x2000*i) % prgSize;
2014-03-19 15:55:18 +00:00
}
2014-03-20 02:21:09 +00:00
template void Mapper::map_prg<32>(int, int);
template void Mapper::map_prg<16>(int, int);
template void Mapper::map_prg<8> (int, int);
2014-03-19 15:55:18 +00:00
/* CHR mapping functions */
2014-03-20 02:21:09 +00:00
template <int pageKBs> void Mapper::map_chr(int slot, int bank)
2014-03-19 15:55:18 +00:00
{
2014-03-20 02:21:09 +00:00
for (int i = 0; i < pageKBs; i++)
chrMap[pageKBs*slot + i] = (pageKBs*0x400*bank + 0x400*i) % chrSize;
2014-03-19 15:55:18 +00:00
}
2014-03-20 02:21:09 +00:00
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);