diff --git a/Core/Core.vcxproj b/Core/Core.vcxproj index 8e23268d8f..75b8764a49 100644 --- a/Core/Core.vcxproj +++ b/Core/Core.vcxproj @@ -158,6 +158,7 @@ + diff --git a/Core/Core.vcxproj.filters b/Core/Core.vcxproj.filters index 45f8b485e2..5463cab6d1 100644 --- a/Core/Core.vcxproj.filters +++ b/Core/Core.vcxproj.filters @@ -2211,6 +2211,9 @@ NES\Mappers\Homebrew + + NES\Mappers\Homebrew + NES\Mappers\Txc diff --git a/Core/NES/MapperFactory.cpp b/Core/NES/MapperFactory.cpp index b5f6c5f61f..b3ed53ce98 100644 --- a/Core/NES/MapperFactory.cpp +++ b/Core/NES/MapperFactory.cpp @@ -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" @@ -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? diff --git a/Core/NES/Mappers/Homebrew/FourMRom.h b/Core/NES/Mappers/Homebrew/FourMRom.h new file mode 100644 index 0000000000..0c35ee29b6 --- /dev/null +++ b/Core/NES/Mappers/Homebrew/FourMRom.h @@ -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 _flash; + vector _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(_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(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); + } + } +};