-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathKoreanFFFFHiComMemoryRule.cpp
113 lines (95 loc) · 3.13 KB
/
KoreanFFFFHiComMemoryRule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Gearsystem - Sega Master System / Game Gear Emulator
* Copyright (C) 2013 Ignacio Sanchez
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
*
*/
#include "KoreanFFFFHiComMemoryRule.h"
#include "Memory.h"
#include "Cartridge.h"
KoreanFFFFHiComMemoryRule::KoreanFFFFHiComMemoryRule(Memory* pMemory, Cartridge* pCartridge, Input* pInput) : MemoryRule(pMemory, pCartridge, pInput)
{
Reset();
}
KoreanFFFFHiComMemoryRule::~KoreanFFFFHiComMemoryRule()
{
}
u8 KoreanFFFFHiComMemoryRule::PerformRead(u16 address)
{
if (address < 0xC000)
{
int slot = (address >> 14) & 0x03;
return m_pCartridge->GetROM()[m_iMapperSlotAddress[slot] + (address & 0x3FFF)];
}
else
{
// RAM + RAM mirror
return m_pMemory->Retrieve(address);
}
}
void KoreanFFFFHiComMemoryRule::PerformWrite(u16 address, u8 value)
{
if (address >= 0xC000)
{
if (address == 0xFFFF)
{
int bank = (value << 1) & (m_pCartridge->GetROMBankCount() - 1);
m_iMapperSlot[0] = bank;
m_iMapperSlot[1] = bank + 1;
m_iMapperSlotAddress[0] = 0x4000 * m_iMapperSlot[0];
m_iMapperSlotAddress[1] = 0x4000 * m_iMapperSlot[1];
}
if (address < 0xE000)
{
// RAM
m_pMemory->Load(address, value);
m_pMemory->Load(address + 0x2000, value);
}
else
{
// RAM (mirror)
m_pMemory->Load(address, value);
m_pMemory->Load(address - 0x2000, value);
}
}
else
{
Debug("--> ** Attempting to write on ROM address $%X %X", address, value);
}
}
void KoreanFFFFHiComMemoryRule::Reset()
{
m_iMapperSlot[0] = 0;
m_iMapperSlot[1] = 1;
m_iMapperSlot[2] = 0;
for (int i = 0; i < 3; i++)
m_iMapperSlotAddress[i] = 0x4000 * m_iMapperSlot[i];
}
u8* KoreanFFFFHiComMemoryRule::GetPage(int index)
{
return m_pCartridge->GetROM() + m_iMapperSlotAddress[index];
}
int KoreanFFFFHiComMemoryRule::GetBank(int index)
{
return m_iMapperSlot[index];
}
void KoreanFFFFHiComMemoryRule::SaveState(std::ostream& stream)
{
stream.write(reinterpret_cast<const char*> (m_iMapperSlotAddress), sizeof(m_iMapperSlotAddress));
stream.write(reinterpret_cast<const char*> (m_iMapperSlot), sizeof(m_iMapperSlot));
}
void KoreanFFFFHiComMemoryRule::LoadState(std::istream& stream)
{
using namespace std;
stream.read(reinterpret_cast<char*> (m_iMapperSlotAddress), sizeof(m_iMapperSlotAddress));
stream.read(reinterpret_cast<char*> (m_iMapperSlot), sizeof(m_iMapperSlot));
}