Mesen-X/Core/BaseMapper.h

199 lines
4 KiB
C
Raw Normal View History

2014-06-14 11:27:55 -04:00
#pragma once
#include "stdafx.h"
#include "IMemoryHandler.h"
#include "ROMLoader.h"
2014-06-14 11:27:55 -04:00
class BaseMapper : public IMemoryHandler
{
private:
MirroringType _mirroringType;
2014-06-14 11:27:55 -04:00
protected:
uint8_t* _prgRAM;
uint8_t* _chrRAM;
uint32_t _prgSize;
uint32_t _chrSize;
2014-06-14 11:27:55 -04:00
vector<uint8_t*> _prgPages;
vector<uint8_t*> _chrPages;
2014-06-14 18:20:56 -04:00
virtual void InitMapper() = 0;
2014-06-14 11:27:55 -04:00
public:
const static int PRGSize = 0x8000;
const static int CHRSize = 0x2000;
protected:
virtual uint32_t GetPRGPageSize() = 0;
virtual uint32_t GetCHRPageSize() = 0;
void SelectPRGPage(uint32_t slot, uint32_t page)
{
//std::cout << std::dec << "PRG Slot " << (short)slot << ": " << (short)page << std::endl;
_prgPages[slot] = &_prgRAM[(page & (GetPRGPageCount() - 1)) * GetPRGPageSize()];
}
void SelectCHRPage(uint32_t slot, uint32_t page)
{
//std::cout << std::dec << "CHR Slot " << (short)slot << ": " << (short)page << std::endl;
_chrPages[slot] = &_chrRAM[(page & (GetCHRPageCount() - 1)) * GetCHRPageSize()];
}
uint32_t GetPRGSlotCount()
{
return BaseMapper::PRGSize / GetPRGPageSize();
}
uint32_t GetCHRSlotCount()
{
return BaseMapper::CHRSize / GetCHRPageSize();
}
uint32_t GetPRGPageCount()
{
return _prgSize / GetPRGPageSize();
}
uint32_t GetCHRPageCount()
{
return _chrSize / GetCHRPageSize();
}
uint32_t log2(uint32_t value)
{
uint32_t counter = 0;
while(value >>= 1) {
counter++;
}
return counter;
}
uint32_t AddrToPRGSlot(uint16_t addr)
{
static uint32_t prgShift = -1;
if(prgShift == -1) {
prgShift = this->log2(GetPRGSlotCount());
}
return (addr >> (15 - prgShift)) & (GetPRGSlotCount() - 1);
}
uint32_t AddrToCHRSlot(uint16_t addr)
{
static uint32_t chrShift = -1;
if(chrShift == -1) {
chrShift = this->log2(GetCHRSlotCount());
}
return (addr >> (13 - chrShift)) & (GetCHRSlotCount() - 1);
}
public:
void Initialize(ROMLoader &romLoader)
2014-06-14 11:27:55 -04:00
{
_mirroringType = romLoader.GetMirroringType();
_prgRAM = romLoader.GetPRGRam();
_chrRAM = romLoader.GetCHRRam();
_prgSize = romLoader.GetPRGSize();
_chrSize = romLoader.GetCHRSize();
if(_chrSize == 0) {
_chrRAM = new uint8_t[BaseMapper::CHRSize];
_chrSize = BaseMapper::CHRSize;
}
2014-06-14 18:20:56 -04:00
for(int i = GetPRGSlotCount(); i > 0; i--) {
_prgPages.push_back(nullptr);
}
for(int i = GetCHRSlotCount(); i > 0; i--) {
_chrPages.push_back(nullptr);
}
2014-06-14 18:20:56 -04:00
InitMapper();
2014-06-14 11:27:55 -04:00
}
~BaseMapper()
{
delete[] _prgRAM;
delete[] _chrRAM;
}
2014-06-15 21:45:36 -04:00
vector<std::array<uint16_t, 2>> GetRAMAddresses()
{
return { { { 0x8000, 0xFFFF } } };
}
vector<std::array<uint16_t, 2>> GetVRAMAddresses()
2014-06-14 11:27:55 -04:00
{
2014-06-15 21:45:36 -04:00
return { { { 0x0000, 0x1FFF } } };
2014-06-14 11:27:55 -04:00
}
virtual MirroringType GetMirroringType()
2014-06-14 11:27:55 -04:00
{
return _mirroringType;
2014-06-14 11:27:55 -04:00
}
uint8_t ReadRAM(uint16_t addr)
2014-06-14 11:27:55 -04:00
{
return _prgPages[AddrToPRGSlot(addr)][addr & (GetPRGPageSize() - 1)];
2014-06-14 11:27:55 -04:00
}
2014-06-15 21:45:36 -04:00
void WriteRAM(uint16_t addr, uint8_t value)
{
_prgPages[AddrToPRGSlot(addr)][addr & (GetPRGPageSize() - 1)] = value;
}
virtual uint8_t ReadVRAM(uint16_t addr)
2014-06-15 21:45:36 -04:00
{
return _chrPages[AddrToCHRSlot(addr)][addr & (GetCHRPageSize() - 1)];
2014-06-15 21:45:36 -04:00
}
virtual void WriteVRAM(uint16_t addr, uint8_t value)
2014-06-15 21:45:36 -04:00
{
_chrPages[AddrToCHRSlot(addr)][addr & (GetCHRPageSize() - 1)] = value;
2014-06-15 21:45:36 -04:00
}
2014-06-14 11:27:55 -04:00
};
class DefaultMapper : public BaseMapper
2014-06-21 19:26:17 -04:00
{
protected:
virtual uint32_t GetPRGPageSize() { return 0x4000; }
virtual uint32_t GetCHRPageSize() { return 0x2000; }
virtual void InitMapper()
{
if(_prgSize == 0x4000) {
SelectPRGPage(0, 0);
SelectPRGPage(1, 0);
} else {
SelectPRGPage(0, 0);
SelectPRGPage(1, 1);
}
SelectCHRPage(0, 0);
}
};
class Mapper2 : public BaseMapper
{
protected:
virtual uint32_t GetPRGPageSize() { return 0x4000; }
virtual uint32_t GetCHRPageSize() { return 0x2000; }
2014-06-21 19:26:17 -04:00
void InitMapper()
{
//First and last PRG page
SelectPRGPage(0, 0);
SelectPRGPage(1, 0xFF);
2014-06-21 19:26:17 -04:00
SelectCHRPage(0, 0);
2014-06-21 19:26:17 -04:00
}
public:
void WriteRAM(uint16_t addr, uint8_t value)
{
SelectPRGPage(0, value);
2014-06-14 11:27:55 -04:00
}
};