-
-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathEmuInstance.h
More file actions
executable file
·386 lines (308 loc) · 10.7 KB
/
EmuInstance.h
File metadata and controls
executable file
·386 lines (308 loc) · 10.7 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
Copyright 2016-2026 melonDS team
This file is part of melonDS.
melonDS 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 (at your option)
any later version.
melonDS 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 melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef EMUINSTANCE_H
#define EMUINSTANCE_H
#include <SDL2/SDL.h>
#include "Platform.h"
#include "main.h"
#include "NDS.h"
#include "EmuThread.h"
#include "Window.h"
#include "Config.h"
#include "SaveManager.h"
const int kMaxWindows = 4;
enum
{
HK_Lid = 0,
HK_Mic,
HK_Pause,
HK_Reset,
HK_FastForward,
HK_FrameLimitToggle,
HK_FullscreenToggle,
HK_SwapScreens,
HK_SwapScreenEmphasis,
HK_SolarSensorDecrease,
HK_SolarSensorIncrease,
HK_FrameStep,
HK_PowerButton,
HK_VolumeUp,
HK_VolumeDown,
HK_AudioMuteToggle,
HK_SlowMo,
HK_FastForwardToggle,
HK_SlowMoToggle,
HK_GuitarGripGreen,
HK_GuitarGripRed,
HK_GuitarGripYellow,
HK_GuitarGripBlue,
HK_SpeedStep,
HK_MAX
};
enum
{
micInputType_Silence,
micInputType_External,
micInputType_Noise,
micInputType_Wav,
micInputType_MAX,
};
enum
{
renderer3D_Software = 0,
#ifdef OGLRENDERER_ENABLED
renderer3D_OpenGL,
renderer3D_OpenGLCompute,
#endif
renderer3D_Max,
};
bool isRightModKey(QKeyEvent* event);
int getEventKeyVal(QKeyEvent* event);
class EmuInstance
{
public:
EmuInstance(int inst);
~EmuInstance();
int getInstanceID() { return instanceID; }
int getConsoleType() { return consoleType; }
EmuThread* getEmuThread() { return emuThread; }
melonDS::NDS* getNDS() { return nds; }
MainWindow* getMainWindow() { return mainWindow; }
int getNumWindows() { return numWindows; }
MainWindow* getWindow(int id) { return windowList[id]; }
void doOnAllWindows(std::function<void(MainWindow*)> func, int exclude = -1);
void saveEnabledWindows();
Config::Table& getGlobalConfig() { return globalCfg; }
Config::Table& getLocalConfig() { return localCfg; }
void broadcastCommand(int cmd, QVariant param = QVariant());
void handleCommand(int cmd, QVariant& param);
std::string instanceFileSuffix();
void createWindow(int id = -1);
void deleteWindow(int id, bool close);
void deleteAllWindows();
void osdAddMessage(unsigned int color, const char* fmt, ...);
bool emuIsActive();
void emuStop(melonDS::Platform::StopReason reason);
bool usesOpenGL();
void initOpenGL(int win);
void deinitOpenGL(int win);
void setVSyncGL(bool vsync);
void makeCurrentGL();
void releaseGL();
void drawScreen();
// return: empty string = setup OK, non-empty = error message
QString verifySetup();
bool updateConsole() noexcept;
void enableCheats(bool enable);
melonDS::ARCodeFile* getCheatFile();
void romIcon(const melonDS::u8 (&data)[512],
const melonDS::u16 (&palette)[16],
melonDS::u32 (&iconRef)[32*32]);
void animatedROMIcon(const melonDS::u8 (&data)[8][512],
const melonDS::u16 (&palette)[8][16],
const melonDS::u16 (&sequence)[64],
melonDS::u32 (&animatedIconRef)[64][32*32],
std::vector<int> &animatedSequenceRef);
static const char* buttonNames[12];
static const char* hotkeyNames[HK_MAX];
void inputInit();
void inputDeInit();
void inputLoadConfig();
void inputRumbleStart(melonDS::u32 len_ms);
void inputRumbleStop();
bool inputHotkeyDown(int id) { return hotkeyDown(id); }
float inputMotionQuery(melonDS::Platform::MotionQueryType type);
void setJoystick(int id);
int getJoystickID() { return joystickID; }
SDL_Joystick* getJoystick() { return joystick; }
std::shared_ptr<SDL_mutex> getJoyMutex() { return joyMutex; }
void touchScreen(int x, int y);
void releaseScreen();
// mic start/stop control from core
void micStart();
void micStop();
int micReadInput(melonDS::s16* data, int maxlength);
QMutex renderLock;
private:
static int lastSep(const std::string& path);
std::string getAssetPath(bool gba, const std::string& configpath, const std::string& ext, const std::string& file);
QString verifyDSBIOS();
QString verifyDSiBIOS();
QString verifyDSFirmware();
QString verifyDSiFirmware();
QString verifyDSiNAND(bool isoptional);
std::string getEffectiveFirmwareSavePath();
void initFirmwareSaveManager() noexcept;
std::string getSavestateName(int slot);
bool savestateExists(int slot);
bool loadState(const std::string& filename);
bool saveState(const std::string& filename);
void undoStateLoad();
void unloadCheats();
void loadCheats();
std::unique_ptr<melonDS::ARM9BIOSImage> loadARM9BIOS() noexcept;
std::unique_ptr<melonDS::ARM7BIOSImage> loadARM7BIOS() noexcept;
std::unique_ptr<melonDS::DSiBIOSImage> loadDSiARM9BIOS() noexcept;
std::unique_ptr<melonDS::DSiBIOSImage> loadDSiARM7BIOS() noexcept;
melonDS::Firmware generateFirmware(int type) noexcept;
std::optional<melonDS::Firmware> loadFirmware(int type) noexcept;
std::optional<melonDS::DSi_NAND::NANDImage> loadNAND(const std::array<melonDS::u8, melonDS::DSiBIOSSize>& arm7ibios) noexcept;
std::optional<melonDS::FATStorageArgs> getSDCardArgs(const std::string& key) noexcept;
std::optional<melonDS::FATStorage> loadSDCard(const std::string& key) noexcept;
void setBatteryLevels();
void reset();
bool bootToMenu(QString& errorstr);
melonDS::u32 decompressROM(const melonDS::u8* inContent, const melonDS::u32 inSize, std::unique_ptr<melonDS::u8[]>& outContent);
void clearBackupState();
std::pair<std::unique_ptr<melonDS::Firmware>, std::string> generateDefaultFirmware();
bool parseMacAddress(void* data);
void customizeFirmware(melonDS::Firmware& firmware, bool overridesettings) noexcept;
bool loadROMData(const QStringList& filepath, std::unique_ptr<melonDS::u8[]>& filedata, melonDS::u32& filelen, std::string& basepath, std::string& romname) noexcept;
QString getSavErrorString(std::string& filepath, bool gba);
bool loadROM(QStringList filepath, bool reset, QString& errorstr);
void ejectCart();
bool cartInserted();
QString cartLabel();
bool loadGBAROM(QStringList filepath, QString& errorstr);
void loadGBAAddon(int type, QString& errorstr);
void ejectGBACart();
bool gbaCartInserted();
QString gbaAddonName(int addon);
QString gbaCartLabel();
void audioInit();
void audioDeInit();
void audioEnable();
void audioDisable();
void updateAudioMuteByWindowFocus();
void toggleAudioMute();
void updateFastForwardMute(bool fastForward);
void audioSync();
void audioUpdateSettings();
void micOpen();
void micClose();
void micLoadWav(const std::string& name);
void setupMicInputData();
int audioGetNumSamplesOut(int outlen);
static void audioCallback(void* data, Uint8* stream, int len);
int micGetNumSamplesIn(int inlen);
void micResample(melonDS::s16* inbuf, int inlen);
static void micCallback(void* data, Uint8* stream, int len);
void onKeyPress(QKeyEvent* event);
void onKeyRelease(QKeyEvent* event);
void keyReleaseAll();
void openJoystick();
void closeJoystick();
bool joystickButtonDown(int val);
void inputProcess();
bool hotkeyDown(int id) { return hotkeyMask & (1<<id); }
bool hotkeyPressed(int id) { return hotkeyPress & (1<<id); }
bool hotkeyReleased(int id) { return hotkeyRelease & (1<<id); }
void loadRTCData();
void saveRTCData();
void setDateTime();
bool deleting;
int instanceID;
EmuThread* emuThread;
MainWindow* mainWindow;
MainWindow* windowList[kMaxWindows];
int numWindows;
Config::Table globalCfg;
Config::Table localCfg;
int consoleType;
melonDS::NDS* nds;
int cartType;
std::string baseROMDir;
std::string baseROMName;
std::string baseAssetName;
bool changeCart;
std::unique_ptr<melonDS::NDSCart::CartCommon> nextCart;
int gbaCartType;
std::string baseGBAROMDir;
std::string baseGBAROMName;
std::string baseGBAAssetName;
bool changeGBACart;
std::unique_ptr<melonDS::GBACart::CartCommon> nextGBACart;
// HACK
public:
std::unique_ptr<SaveManager> ndsSave;
std::unique_ptr<SaveManager> gbaSave;
std::unique_ptr<SaveManager> firmwareSave;
bool doLimitFPS;
double curFPS;
double targetFPS;
double fastForwardFPS;
double slowmoFPS;
bool fastForwardToggled;
bool slowmoToggled;
bool speedStepToggled;
bool doAudioSync;
private:
std::unique_ptr<melonDS::Savestate> backupState;
bool savestateLoaded;
std::unique_ptr<melonDS::ARCodeFile> cheatFile;
bool cheatsOn;
SDL_AudioDeviceID audioDevice;
int audioFreq;
int audioBufSize;
float audioSampleFrac;
bool audioMutedToggle;
bool audioMutedByFastForward;
bool audioMutedByWindowFocus;
SDL_cond* audioSyncCond;
SDL_mutex* audioSyncLock;
int mpAudioMode;
bool micStarted;
SDL_AudioDeviceID micDevice;
int micFreq;
int micBufSize;
float micSampleFrac;
melonDS::s16 micExtBuffer[4096];
melonDS::u32 micExtBufferWritePos;
melonDS::u32 micExtBufferCount;
melonDS::u32 micWavLength;
melonDS::s16* micWavBuffer;
melonDS::s16* micBuffer;
melonDS::u32 micBufferLength;
melonDS::u32 micBufferReadPos;
SDL_mutex* micLock;
//int audioInterp;
int audioVolume;
bool audioDSiVolumeSync;
int micInputType;
std::string micDeviceName;
std::string micWavPath;
int keyMapping[12];
int joyMapping[12];
int hkKeyMapping[HK_MAX];
int hkJoyMapping[HK_MAX];
int joystickID;
SDL_Joystick* joystick;
SDL_GameController* controller;
bool hasAccelerometer = false;
bool hasGyroscope = false;
bool hasRumble = false;
bool isRumbling = false;
static std::shared_ptr<SDL_mutex> joyMutexGlobal;
std::shared_ptr<SDL_mutex> joyMutex;
melonDS::u32 keyInputMask, joyInputMask;
melonDS::u32 keyHotkeyMask, joyHotkeyMask;
melonDS::u32 hotkeyMask, lastHotkeyMask;
melonDS::u32 hotkeyPress, hotkeyRelease;
melonDS::u32 inputMask;
bool isTouching;
melonDS::u16 touchX, touchY;
friend class EmuThread;
friend class MainWindow;
};
#endif //EMUINSTANCE_H