Skip to content

Commit

Permalink
Added Mapper number 2 aka UxROM
Browse files Browse the repository at this point in the history
  • Loading branch information
amhndu committed Sep 28, 2016
1 parent cdf9a4b commit fbb3761
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
27 changes: 27 additions & 0 deletions include/MapperUxROM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef MAPPERUXROM_H
#define MAPPERUXROM_H
#include "Mapper.h"

namespace sn
{
class MapperUxROM : public Mapper
{
public:
MapperUxROM(Cartridge& cart);
void writePRG (Address addr, Byte value);
Byte readPRG (Address addr);
const Byte* getPagePtr(Address addr);

Byte readCHR (Address addr);
void writeCHR (Address addr, Byte value);
private:
bool m_usesCharacterRAM;

const Byte* m_lastBankPtr;
Address m_selectPRG;

std::vector<Byte> m_characterRAM;

};
}
#endif // MAPPERUXROM_H
4 changes: 2 additions & 2 deletions src/Cartridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ namespace sn
else
LOG(Info) << "ROM is NTSC compatible.\n";

//PRG-ROM
//PRG-ROM 16KB banks
m_PRG_ROM.resize(0x4000 * banks);
if (!romFile.read(reinterpret_cast<char*>(&m_PRG_ROM[0]), 0x4000 * banks))
{
LOG(Error) << "Reading PRG-ROM from image file failed." << std::endl;
return false;
}

//CHR-ROM
//CHR-ROM 8KB banks
if (vbanks)
{
m_CHR_ROM.resize(0x2000 * vbanks);
Expand Down
4 changes: 4 additions & 0 deletions src/Mapper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Mapper.h"
#include "MapperNROM.h"
#include "MapperUxROM.h"

namespace sn
{
Expand All @@ -11,6 +12,9 @@ namespace sn
case NROM:
ret.reset(new MapperNROM(cart));
break;
case UxROM:
ret.reset(new MapperUxROM(cart));
break;
default:
ret.reset(nullptr);
}
Expand Down
58 changes: 58 additions & 0 deletions src/MapperUxROM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "MapperUxROM.h"
#include "Log.h"

namespace sn
{
MapperUxROM::MapperUxROM(Cartridge &cart) :
Mapper(cart, Mapper::UxROM),
m_selectPRG(0)
{
if (cart.getVROM().size() == 0)
{
m_usesCharacterRAM = true;
m_characterRAM.resize(0x2000);
LOG(Info) << "Uses character RAM" << std::endl;
}
else
m_usesCharacterRAM = false;

m_lastBankPtr = &cart.getROM()[cart.getROM().size() - 0x4000]; //last - 16KB
}

Byte MapperUxROM::readPRG(Address addr)
{
if (addr < 0xc000)
return m_cartridge.getROM()[((addr - 0x8000) & 0x3fff) | (m_selectPRG << 14)];
else
return *(m_lastBankPtr + (addr & 0x3fff));
}

void MapperUxROM::writePRG(Address addr, Byte value)
{
m_selectPRG = value;
}

const Byte* MapperUxROM::getPagePtr(Address addr)
{
if (addr < 0xc000)
return &m_cartridge.getROM()[((addr - 0x8000) & 0x3fff) | (m_selectPRG << 14)];
else
return m_lastBankPtr + (addr & 0x3fff);
}

Byte MapperUxROM::readCHR(Address addr)
{
if (m_usesCharacterRAM)
return m_characterRAM[addr];
else
return m_cartridge.getVROM()[addr];
}

void MapperUxROM::writeCHR(Address addr, Byte value)
{
if (m_usesCharacterRAM)
m_characterRAM[addr] = value;
else
LOG(Info) << "Read-only CHR memory write attempt at " << std::hex << addr << std::endl;
}
}

0 comments on commit fbb3761

Please sign in to comment.