CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/HW/MemoryStick.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <algorithm>
19
#include <condition_variable>
20
#include <mutex>
21
#include <thread>
22
23
#include "Common/Serialize/Serializer.h"
24
#include "Common/Serialize/SerializeFuncs.h"
25
#include "Common/Thread/ThreadUtil.h"
26
#include "Core/Config.h"
27
#include "Core/CoreTiming.h"
28
#include "Core/Compatibility.h"
29
#include "Core/FileSystems/MetaFileSystem.h"
30
#include "Core/HW/MemoryStick.h"
31
#include "Core/System.h"
32
33
// MS and FatMS states.
34
static MemStickState memStickState;
35
static MemStickFatState memStickFatState;
36
static bool memStickNeedsAssign = false;
37
static u64 memStickInsertedAt = 0;
38
static uint64_t memstickInitialFree = 0;
39
static uint64_t memstickCurrentUse = 0;
40
static bool memstickCurrentUseValid = false;
41
42
enum FreeCalcStatus {
43
NONE,
44
RUNNING,
45
DONE,
46
CLEANED_UP,
47
};
48
49
static std::thread freeCalcThread;
50
static std::condition_variable freeCalcCond;
51
static std::mutex freeCalcMutex;
52
static FreeCalcStatus freeCalcStatus = FreeCalcStatus::NONE;
53
54
static const u64 normalMemstickSize = 9ULL * 1024 * 1024 * 1024;
55
static const u64 smallMemstickSize = 1ULL * 1024 * 1024 * 1024;
56
57
void MemoryStick_DoState(PointerWrap &p) {
58
auto s = p.Section("MemoryStick", 1, 5);
59
if (!s)
60
return;
61
62
Do(p, memStickState);
63
Do(p, memStickFatState);
64
if (s >= 4) {
65
// Do nothing.
66
} else if (s >= 2) {
67
// Really no point in storing the memstick size.
68
u64 memStickSize = normalMemstickSize;
69
Do(p, memStickSize);
70
}
71
if (s >= 5) {
72
Do(p, memstickInitialFree);
73
}
74
75
if (s >= 3) {
76
Do(p, memStickNeedsAssign);
77
Do(p, memStickInsertedAt);
78
}
79
}
80
81
MemStickState MemoryStick_State() {
82
return memStickState;
83
}
84
85
MemStickFatState MemoryStick_FatState() {
86
if (memStickNeedsAssign && CoreTiming::GetTicks() > memStickInsertedAt + msToCycles(500)) {
87
// It's been long enough for us to be done mounting the memory stick.
88
memStickFatState = PSP_FAT_MEMORYSTICK_STATE_ASSIGNED;
89
memStickNeedsAssign = false;
90
}
91
return memStickFatState;
92
}
93
94
u64 MemoryStick_SectorSize() {
95
return 32 * 1024; // 32KB
96
}
97
98
static void MemoryStick_CalcInitialFree() {
99
std::unique_lock<std::mutex> guard(freeCalcMutex);
100
freeCalcStatus = FreeCalcStatus::RUNNING;
101
freeCalcThread = std::thread([] {
102
SetCurrentThreadName("CalcInitialFree");
103
104
AndroidJNIThreadContext jniContext;
105
106
memstickInitialFree = pspFileSystem.FreeSpace("ms0:/") + pspFileSystem.ComputeRecursiveDirectorySize("ms0:/PSP/SAVEDATA/");
107
108
std::unique_lock<std::mutex> guard(freeCalcMutex);
109
freeCalcStatus = FreeCalcStatus::DONE;
110
freeCalcCond.notify_all();
111
});
112
}
113
114
static void MemoryStick_WaitInitialFree() {
115
std::unique_lock<std::mutex> guard(freeCalcMutex);
116
while (freeCalcStatus == FreeCalcStatus::RUNNING) {
117
freeCalcCond.wait(guard);
118
}
119
if (freeCalcStatus == FreeCalcStatus::DONE)
120
freeCalcThread.join();
121
freeCalcStatus = FreeCalcStatus::CLEANED_UP;
122
}
123
124
u64 MemoryStick_FreeSpace() {
125
MemoryStick_WaitInitialFree();
126
127
const CompatFlags &flags = PSP_CoreParameter().compat.flags();
128
u64 realFreeSpace = pspFileSystem.FreeSpace("ms0:/");
129
130
// Cap the memory stick size to avoid math errors when old games get sizes that were
131
// not planned for back then (even though 2GB cards were available.)
132
// We have a compat setting to make it even smaller for Harry Potter : Goblet of Fire, see #13266.
133
const u64 memStickSize = flags.ReportSmallMemstick ? smallMemstickSize : (u64)g_Config.iMemStickSizeGB * 1024 * 1024 * 1024;
134
135
// Assume the memory stick is only used to store savedata.
136
if (!memstickCurrentUseValid) {
137
memstickCurrentUse = pspFileSystem.ComputeRecursiveDirectorySize("ms0:/PSP/SAVEDATA/");
138
memstickCurrentUseValid = true;
139
}
140
141
u64 simulatedFreeSpace = 0;
142
if (memstickCurrentUse < memStickSize) {
143
simulatedFreeSpace = memStickSize - memstickCurrentUse;
144
} else if (flags.ReportSmallMemstick) {
145
// There's more stuff in the memstick than the size we report.
146
// This doesn't work, so we'll just have to lie. Not sure what the best way is.
147
simulatedFreeSpace = smallMemstickSize / 2; // just pick a value.
148
}
149
if (flags.MemstickFixedFree) {
150
// Assassin's Creed: Bloodlines fails to save if free space changes incorrectly during game.
151
realFreeSpace = 0;
152
if (memstickCurrentUse <= memstickInitialFree) {
153
realFreeSpace = memstickInitialFree - memstickCurrentUse;
154
}
155
}
156
157
return std::min(simulatedFreeSpace, realFreeSpace);
158
}
159
160
void MemoryStick_NotifyWrite() {
161
memstickCurrentUseValid = false;
162
}
163
164
void MemoryStick_SetFatState(MemStickFatState state) {
165
memStickFatState = state;
166
memStickNeedsAssign = false;
167
}
168
169
void MemoryStick_SetState(MemStickState state) {
170
if (memStickState == state) {
171
return;
172
}
173
174
memStickState = state;
175
176
// If removed, we unmount. Otherwise, mounting is delayed.
177
if (state == PSP_MEMORYSTICK_STATE_NOT_INSERTED) {
178
MemoryStick_SetFatState(PSP_FAT_MEMORYSTICK_STATE_UNASSIGNED);
179
} else {
180
memStickInsertedAt = CoreTiming::GetTicks();
181
memStickNeedsAssign = true;
182
}
183
}
184
185
void MemoryStick_Init() {
186
if (g_Config.bMemStickInserted) {
187
memStickState = PSP_MEMORYSTICK_STATE_INSERTED;
188
memStickFatState = PSP_FAT_MEMORYSTICK_STATE_ASSIGNED;
189
} else {
190
memStickState = PSP_MEMORYSTICK_STATE_NOT_INSERTED;
191
memStickFatState = PSP_FAT_MEMORYSTICK_STATE_UNASSIGNED;
192
}
193
194
memStickNeedsAssign = false;
195
MemoryStick_CalcInitialFree();
196
}
197
198
void MemoryStick_Shutdown() {
199
MemoryStick_WaitInitialFree();
200
}
201
202