Path: blob/a-new-beginning/Cherry/Core/include/MegaCartMapper.h
2 views
/*1* Gearcoleco - ColecoVision Emulator2* Copyright (C) 2021 Ignacio Sanchez34* This program is free software: you can redistribute it and/or modify5* it under the terms of the GNU General Public License as published by6* the Free Software Foundation, either version 3 of the License, or7* any later version.89* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.1314* You should have received a copy of the GNU General Public License15* along with this program. If not, see http://www.gnu.org/licenses/16*17*/1819#ifndef MEGACARTMAPPER_H20#define MEGACARTMAPPER_H2122#include "Mapper.h"23#include "Cartridge.h"2425class MegaCartMapper : public Mapper26{27public:28MegaCartMapper(Cartridge* pCartridge);29virtual ~MegaCartMapper();3031virtual void Reset();32virtual u8 Read(u16 address);33virtual void Write(u16 address, u8 value);34virtual void SaveState(std::ostream& stream);35virtual void LoadState(std::istream& stream);36virtual u8 GetRomBank() { return m_RomBank; }37virtual u32 GetRomBankAddress() { return m_RomBankAddress; }3839private:40u8 m_RomBank;41u32 m_RomBankAddress;42};4344inline MegaCartMapper::MegaCartMapper(Cartridge* pCartridge) : Mapper(pCartridge)45{46Reset();47}4849inline MegaCartMapper::~MegaCartMapper()50{51}5253inline void MegaCartMapper::Reset()54{55m_RomBank = 0;56m_RomBankAddress = 0;57}5859inline u8 MegaCartMapper::Read(u16 address)60{61u8* pRom = m_pCartridge->GetROM();62int romSize = m_pCartridge->GetROMSize();6364if (address < 0xC000)65{66return pRom[(address & 0x3FFF) + (romSize - 0x4000)];67}68else69{70if (address >= 0xFFC0)71{72m_RomBank = address & (m_pCartridge->GetROMBankCount() - 1);73m_RomBankAddress = m_RomBank << 14;74}75return pRom[(address & 0x3FFF) + m_RomBankAddress];76}77}7879inline void MegaCartMapper::Write(u16 address, u8 value)80{81if (m_pCartridge->HasSRAM() && (address >= 0xE000) && (address < 0xE800))82{83u8* pRom = m_pCartridge->GetROM();84pRom[(address + 0x800) & 0x7FFF] = value;85}86else if (address >= 0xFFC0)87{88m_RomBank = address & (m_pCartridge->GetROMBankCount() - 1);89m_RomBankAddress = m_RomBank << 14;90}91else92{93Debug("--> ** Attempting to write on ROM: %X %X", address, value);94}95}9697inline void MegaCartMapper::SaveState(std::ostream& stream)98{99stream.write(reinterpret_cast<const char*> (&m_RomBank), sizeof(m_RomBank));100stream.write(reinterpret_cast<const char*> (&m_RomBankAddress), sizeof(m_RomBankAddress));101}102103inline void MegaCartMapper::LoadState(std::istream& stream)104{105stream.read(reinterpret_cast<char*> (&m_RomBank), sizeof(m_RomBank));106stream.read(reinterpret_cast<char*> (&m_RomBankAddress), sizeof(m_RomBankAddress));107}108109#endif /* MEGACARTMAPPER_H */110111112