-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
GCMemcardRaw.cpp
211 lines (182 loc) · 5.54 KB
/
GCMemcardRaw.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "Core/HW/GCMemcard/GCMemcardRaw.h"
#include <chrono>
#include <cstring>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include "Common/ChunkFile.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/Logging/Log.h"
#include "Common/StringUtil.h"
#include "Common/Thread.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/HW/GCMemcard/GCMemcard.h"
#define SIZE_TO_Mb (1024 * 8 * 16)
#define MC_HDR_SIZE 0xA000
MemoryCard::MemoryCard(const std::string& filename, int card_index, u16 size_mbits)
: MemoryCardBase(card_index, size_mbits), m_filename(filename)
{
File::IOFile file(m_filename, "rb");
if (file)
{
// Measure size of the existing memcard file.
m_memory_card_size = (u32)file.GetSize();
m_nintendo_card_id = m_memory_card_size / SIZE_TO_Mb;
m_memcard_data = std::make_unique<u8[]>(m_memory_card_size);
memset(&m_memcard_data[0], 0xFF, m_memory_card_size);
INFO_LOG(EXPANSIONINTERFACE, "Reading memory card %s", m_filename.c_str());
file.ReadBytes(&m_memcard_data[0], m_memory_card_size);
}
else
{
// Create a new 128Mb memcard
m_nintendo_card_id = size_mbits;
m_memory_card_size = size_mbits * SIZE_TO_Mb;
m_memcard_data = std::make_unique<u8[]>(m_memory_card_size);
// Fills in MC_HDR_SIZE bytes
GCMemcard::Format(&m_memcard_data[0], m_filename.find(".JAP.raw") != std::string::npos,
size_mbits);
memset(&m_memcard_data[MC_HDR_SIZE], 0xFF, m_memory_card_size - MC_HDR_SIZE);
INFO_LOG(EXPANSIONINTERFACE, "No memory card found. A new one was created instead.");
}
// Class members (including inherited ones) have now been initialized, so
// it's safe to startup the flush thread (which reads them).
m_flush_buffer = std::make_unique<u8[]>(m_memory_card_size);
m_flush_thread = std::thread(&MemoryCard::FlushThread, this);
}
MemoryCard::~MemoryCard()
{
if (m_flush_thread.joinable())
{
m_flush_trigger.Set();
m_flush_thread.join();
}
}
void MemoryCard::FlushThread()
{
if (!SConfig::GetInstance().bEnableMemcardSdWriting)
{
return;
}
Common::SetCurrentThreadName(
StringFromFormat("Memcard %d flushing thread", m_card_index).c_str());
const auto flush_interval = std::chrono::seconds(15);
while (true)
{
// If triggered, we're exiting.
// If timed out, check if we need to flush.
bool do_exit = m_flush_trigger.WaitFor(flush_interval);
if (!do_exit)
{
bool is_dirty = m_dirty.TestAndClear();
if (!is_dirty)
{
continue;
}
}
// Opening the file is purposefully done each iteration to ensure the
// file doesn't disappear out from under us after the first check.
File::IOFile file(m_filename, "r+b");
if (!file)
{
std::string dir;
SplitPath(m_filename, &dir, nullptr, nullptr);
if (!File::IsDirectory(dir))
{
File::CreateFullPath(dir);
}
file.Open(m_filename, "wb");
}
// Note - file may have changed above, after ctor
if (!file)
{
PanicAlertT(
"Could not write memory card file %s.\n\n"
"Are you running Dolphin from a CD/DVD, or is the save file maybe write protected?\n\n"
"Are you receiving this after moving the emulator directory?\nIf so, then you may "
"need to re-specify your memory card location in the options.",
m_filename.c_str());
// Exit the flushing thread - further flushes will be ignored unless
// the thread is recreated.
return;
}
{
std::unique_lock<std::mutex> l(m_flush_mutex);
memcpy(&m_flush_buffer[0], &m_memcard_data[0], m_memory_card_size);
}
file.WriteBytes(&m_flush_buffer[0], m_memory_card_size);
if (!do_exit)
{
Core::DisplayMessage(StringFromFormat("Wrote memory card %c contents to %s",
m_card_index ? 'B' : 'A', m_filename.c_str())
.c_str(),
4000);
}
else
{
return;
}
}
}
void MemoryCard::MakeDirty()
{
m_dirty.Set();
}
s32 MemoryCard::Read(u32 src_address, s32 length, u8* dest_address)
{
if (!IsAddressInBounds(src_address))
{
PanicAlertT("MemoryCard: Read called with invalid source address (0x%x)", src_address);
return -1;
}
memcpy(dest_address, &m_memcard_data[src_address], length);
return length;
}
s32 MemoryCard::Write(u32 dest_address, s32 length, const u8* src_address)
{
if (!IsAddressInBounds(dest_address))
{
PanicAlertT("MemoryCard: Write called with invalid destination address (0x%x)", dest_address);
return -1;
}
{
std::unique_lock<std::mutex> l(m_flush_mutex);
memcpy(&m_memcard_data[dest_address], src_address, length);
}
MakeDirty();
return length;
}
void MemoryCard::ClearBlock(u32 address)
{
if (address & (BLOCK_SIZE - 1) || !IsAddressInBounds(address))
{
PanicAlertT("MemoryCard: ClearBlock called on invalid address (0x%x)", address);
return;
}
else
{
std::unique_lock<std::mutex> l(m_flush_mutex);
memset(&m_memcard_data[address], 0xFF, BLOCK_SIZE);
}
MakeDirty();
}
void MemoryCard::ClearAll()
{
{
std::unique_lock<std::mutex> l(m_flush_mutex);
memset(&m_memcard_data[0], 0xFF, m_memory_card_size);
}
MakeDirty();
}
void MemoryCard::DoState(PointerWrap& p)
{
p.Do(m_card_index);
p.Do(m_memory_card_size);
p.DoArray(&m_memcard_data[0], m_memory_card_size);
}