Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/Cherry/Core/include/MegaCartMapper.h
2 views
1
/*
2
* Gearcoleco - ColecoVision Emulator
3
* Copyright (C) 2021 Ignacio Sanchez
4
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* any later version.
9
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
14
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see http://www.gnu.org/licenses/
17
*
18
*/
19
20
#ifndef MEGACARTMAPPER_H
21
#define MEGACARTMAPPER_H
22
23
#include "Mapper.h"
24
#include "Cartridge.h"
25
26
class MegaCartMapper : public Mapper
27
{
28
public:
29
MegaCartMapper(Cartridge* pCartridge);
30
virtual ~MegaCartMapper();
31
32
virtual void Reset();
33
virtual u8 Read(u16 address);
34
virtual void Write(u16 address, u8 value);
35
virtual void SaveState(std::ostream& stream);
36
virtual void LoadState(std::istream& stream);
37
virtual u8 GetRomBank() { return m_RomBank; }
38
virtual u32 GetRomBankAddress() { return m_RomBankAddress; }
39
40
private:
41
u8 m_RomBank;
42
u32 m_RomBankAddress;
43
};
44
45
inline MegaCartMapper::MegaCartMapper(Cartridge* pCartridge) : Mapper(pCartridge)
46
{
47
Reset();
48
}
49
50
inline MegaCartMapper::~MegaCartMapper()
51
{
52
}
53
54
inline void MegaCartMapper::Reset()
55
{
56
m_RomBank = 0;
57
m_RomBankAddress = 0;
58
}
59
60
inline u8 MegaCartMapper::Read(u16 address)
61
{
62
u8* pRom = m_pCartridge->GetROM();
63
int romSize = m_pCartridge->GetROMSize();
64
65
if (address < 0xC000)
66
{
67
return pRom[(address & 0x3FFF) + (romSize - 0x4000)];
68
}
69
else
70
{
71
if (address >= 0xFFC0)
72
{
73
m_RomBank = address & (m_pCartridge->GetROMBankCount() - 1);
74
m_RomBankAddress = m_RomBank << 14;
75
}
76
return pRom[(address & 0x3FFF) + m_RomBankAddress];
77
}
78
}
79
80
inline void MegaCartMapper::Write(u16 address, u8 value)
81
{
82
if (m_pCartridge->HasSRAM() && (address >= 0xE000) && (address < 0xE800))
83
{
84
u8* pRom = m_pCartridge->GetROM();
85
pRom[(address + 0x800) & 0x7FFF] = value;
86
}
87
else if (address >= 0xFFC0)
88
{
89
m_RomBank = address & (m_pCartridge->GetROMBankCount() - 1);
90
m_RomBankAddress = m_RomBank << 14;
91
}
92
else
93
{
94
Debug("--> ** Attempting to write on ROM: %X %X", address, value);
95
}
96
}
97
98
inline void MegaCartMapper::SaveState(std::ostream& stream)
99
{
100
stream.write(reinterpret_cast<const char*> (&m_RomBank), sizeof(m_RomBank));
101
stream.write(reinterpret_cast<const char*> (&m_RomBankAddress), sizeof(m_RomBankAddress));
102
}
103
104
inline void MegaCartMapper::LoadState(std::istream& stream)
105
{
106
stream.read(reinterpret_cast<char*> (&m_RomBank), sizeof(m_RomBank));
107
stream.read(reinterpret_cast<char*> (&m_RomBankAddress), sizeof(m_RomBankAddress));
108
}
109
110
#endif /* MEGACARTMAPPER_H */
111
112