Skip to content

Commit

Permalink
Added CNROM i.e. Mapper amhndu#3
Browse files Browse the repository at this point in the history
Games like Adventure Island and Paperboy work.
  • Loading branch information
amhndu committed Sep 30, 2016
1 parent fbb3761 commit eb205f4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/Mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace sn
NROM = 0,
MMC1 = 1,
UxROM = 2,
CNROM = 3,
};

Mapper(Cartridge& cart, Type t) : m_cartridge(cart), m_type(t) {};
Expand Down
23 changes: 23 additions & 0 deletions include/MapperCNROM.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef MAPPERCNROM_H
#define MAPPERCNROM_H
#include "Mapper.h"

namespace sn
{
class MapperCNROM : public Mapper
{
public:
MapperCNROM(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_oneBank;

Address m_selectCHR;
};
}
#endif // MAPPERCNROM_H
8 changes: 6 additions & 2 deletions src/Mapper.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "Mapper.h"
#include "MapperNROM.h"
#include "MapperUxROM.h"
#include "MapperCNROM.h"

namespace sn
{
std::unique_ptr<Mapper> Mapper::createMapper(Mapper::Type mapper_t, sn::Cartridge& cart)
{
std::unique_ptr<Mapper> ret;
std::unique_ptr<Mapper> ret(nullptr);
switch (mapper_t)
{
case NROM:
Expand All @@ -15,8 +16,11 @@ namespace sn
case UxROM:
ret.reset(new MapperUxROM(cart));
break;
case CNROM:
ret.reset(new MapperCNROM(cart));
break;
default:
ret.reset(nullptr);
break;
}
return ret;
}
Expand Down
50 changes: 50 additions & 0 deletions src/MapperCNROM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "MapperCNROM.h"
#include "Log.h"

namespace sn
{
MapperCNROM::MapperCNROM(Cartridge &cart) :
Mapper(cart, Mapper::CNROM),
m_selectCHR(0)
{
if (cart.getROM().size() == 0x4000) //1 bank
{
m_oneBank = true;
}
else //2 banks
{
m_oneBank = false;
}
}

Byte MapperCNROM::readPRG(Address addr)
{
if (!m_oneBank)
return m_cartridge.getROM()[addr - 0x8000];
else //mirrored
return m_cartridge.getROM()[(addr - 0x8000) & 0x3fff];
}

void MapperCNROM::writePRG(Address addr, Byte value)
{
m_selectCHR = value & 0x3;
}

const Byte* MapperCNROM::getPagePtr(Address addr)
{
if (!m_oneBank)
return &m_cartridge.getROM()[addr - 0x8000];
else //mirrored
return &m_cartridge.getROM()[(addr - 0x8000) & 0x3fff];
}

Byte MapperCNROM::readCHR(Address addr)
{
return m_cartridge.getVROM()[addr | (m_selectCHR << 13)];
}

void MapperCNROM::writeCHR(Address addr, Byte value)
{
LOG(Info) << "Read-only CHR memory write attempt at " << std::hex << addr << std::endl;
}
}

0 comments on commit eb205f4

Please sign in to comment.