Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Core/Core.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<ClInclude Include="NES\Mappers\Homebrew\Cheapocabra.h" />
<ClInclude Include="NES\Mappers\Homebrew\FaridSlrom.h" />
<ClInclude Include="NES\Mappers\Homebrew\FaridUnrom.h" />
<ClInclude Include="NES\Mappers\Homebrew\FourMRom.h" />
<ClInclude Include="NES\Mappers\Homebrew\FlashSST39SF040.h" />
<ClInclude Include="NES\Mappers\Homebrew\MagicFloor218.h" />
<ClInclude Include="NES\Mappers\Homebrew\SealieComputing.h" />
Expand Down
3 changes: 3 additions & 0 deletions Core/Core.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2211,6 +2211,9 @@
<ClInclude Include="NES\Mappers\Homebrew\UnRom512.h">
<Filter>NES\Mappers\Homebrew</Filter>
</ClInclude>
<ClInclude Include="NES\Mappers\Homebrew\FourMRom.h">
<Filter>NES\Mappers\Homebrew</Filter>
</ClInclude>
<ClInclude Include="NES\Mappers\Txc\Bmc11160.h">
<Filter>NES\Mappers\Txc</Filter>
</ClInclude>
Expand Down
3 changes: 3 additions & 0 deletions Core/NES/MapperFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "NES/Mappers/Homebrew/Cheapocabra.h"
#include "NES/Mappers/Homebrew/FaridSlrom.h"
#include "NES/Mappers/Homebrew/FaridUnrom.h"
#include "NES/Mappers/Homebrew/FourMRom.h"
#include "NES/Mappers/Homebrew/Rainbow.h"
#include "NES/Mappers/Homebrew/SealieComputing.h"
#include "NES/Mappers/Homebrew/UnRom512.h"
Expand Down Expand Up @@ -623,6 +624,8 @@ BaseMapper* MapperFactory::GetMapperFromID(RomData& romData)

case 552: return new TaitoX1017();

case 595: return new FourMRom();

case 682: return new Rainbow();

case UnifBoards::Ac08: return new Ac08(); //mapper 42?
Expand Down
126 changes: 126 additions & 0 deletions Core/NES/Mappers/Homebrew/FourMRom.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#pragma once
#include "pch.h"
#include "NES/BaseMapper.h"
#include "NES/Mappers/Homebrew/FlashSST39SF040.h"
#include "Utilities/Serializer.h"

// NES 2.0 mapper 595 - 4MROM-512
class FourMRom : public BaseMapper
{
private:
unique_ptr<FlashSST39SF040> _flash;
vector<uint8_t> _orgPrgRom;

// 5-bit bank shift register.
uint8_t _prgReg = 0;

protected:
uint16_t GetPrgPageSize() override { return 0x4000; }
uint16_t GetChrPageSize() override { return 0x2000; }
uint32_t GetChrRamSize() override { return 0x4000; }
uint32_t GetWorkRamSize() override { return 0x2000; }
uint32_t GetSaveRamSize() override { return 0; }

// $8000-$BFFF: flash chip command writes (not a register as such)
// $C000-$FFFF: the 5-bit bank shift register
uint16_t RegisterStartAddress() override { return 0x8000; }
uint16_t RegisterEndAddress() override { return 0xFFFF; }

bool AllowRegisterRead() override { return true; }
bool HasBusConflicts() override { return false; }

void InitMapper() override
{
_flash.reset(new FlashSST39SF040(_prgRom, _prgSize));

// Only $8000-$BFFF might read anything (namely, flash software ID)
RemoveRegisterRange(0xC000, 0xFFFF, MemoryOperation::Read);

if(HasBattery()) {
_orgPrgRom = vector<uint8_t>(_prgRom, _prgRom + _prgSize);
ApplySaveData();
}
SelectChrPage(0, 0);
// Fixed bank at $C000-$FFFF
SelectPrgPage(1, -1);

_prgReg = 0;
SetPrgBank();

if(_chrRamSize >= 0x4000) {
SetPpuMemoryMapping(0x2000, 0x3FFF, ChrMemoryType::ChrRam, 0x2000, MemoryAccessType::ReadWrite);
}
}

void SetPrgBank()
{
SelectPrgPage(0, _prgReg & 0x1F);
}

void Reset(bool softReset) override
{
BaseMapper::Reset(softReset);

// Put the flash command state machine back to idle, so a reset in
// the middle of a command sequence (e.g. after $AA, $55) doesn't
// leave it half-way through.
_flash->ResetState();

_prgReg = 0;
SetPrgBank();
}

// Same as UnRom512
void Serialize(Serializer& s) override
{
BaseMapper::Serialize(s);
SV(_flash);
SV(_prgReg);
SerializeRomDiff(s, _orgPrgRom);
}

// Same as UnRom512
void ApplySaveData()
{
if(_console->GetNesConfig().DisableFlashSaves) {
return;
}
LoadRomPatch(_orgPrgRom);
}

// Same as UnRom512
void SaveBattery() override
{
if(_console->GetNesConfig().DisableFlashSaves) {
return;
}

if(HasBattery()) {
SaveRom(_orgPrgRom);
}
}

// Same as UnRom512
uint8_t ReadRegister(uint16_t addr) override
{
int16_t value = _flash->Read(addr);
if(value >= 0) {
return static_cast<uint8_t>(value);
}

return BaseMapper::InternalReadRam(addr);
}

void WriteRegister(uint16_t addr, uint8_t value) override
{
if(addr >= 0xC000) {
// Shift the input bit into the high-bit of the register
_prgReg = ((value << 4) & 0x10) | (_prgReg >> 1);
// Note that every intermediate bit shifted in updates the bank
SetPrgBank();
} else {
// Same as UnRom512
_flash->Write((addr & 0x3FFF) | (_prgReg << 14), value);
}
}
};
Loading