forked from xenia-project/xenia
/
emulator.h
284 lines (226 loc) · 10.2 KB
/
emulator.h
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
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2020 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_EMULATOR_H_
#define XENIA_EMULATOR_H_
#include <cstddef>
#include <cstdint>
#include <functional>
#include <optional>
#include <string>
#include <vector>
#include "xenia/base/delegate.h"
#include "xenia/base/exception_handler.h"
#include "xenia/kernel/kernel_state.h"
#include "xenia/memory.h"
#include "xenia/patcher/patcher.h"
#include "xenia/vfs/device.h"
#include "xenia/vfs/virtual_file_system.h"
#include "xenia/xbox.h"
namespace xe {
namespace apu {
class AudioSystem;
} // namespace apu
namespace cpu {
class ExportResolver;
class Processor;
class ThreadState;
} // namespace cpu
namespace gpu {
class GraphicsSystem;
} // namespace gpu
namespace hid {
class InputDriver;
class InputSystem;
} // namespace hid
namespace ui {
class ImGuiDrawer;
class Window;
} // namespace ui
} // namespace xe
namespace xe {
constexpr fourcc_t kEmulatorSaveSignature = make_fourcc("XSAV");
// The main type that runs the whole emulator.
// This is responsible for initializing and managing all the various subsystems.
class Emulator {
public:
// This is the class for the top-level callbacks. They may be called in an
// undefined order, so among them there must be no dependencies on each other,
// especially hierarchical ones. If hierarchical handling is needed, for
// instance, if a specific implementation of a subsystem needs to handle
// changes, but the entire implementation must be reloaded, the implementation
// in this example _must not_ register / unregister its own callback - rather,
// the proper ordering and hierarchy should be constructed in a single
// callback (in this example, for the whole subsystem).
//
// All callbacks must be created and destroyed in the UI thread only (or the
// thread that takes its place in the architecture of the specific app if
// there's no UI), as they are invoked in the UI thread.
class GameConfigLoadCallback {
public:
GameConfigLoadCallback(Emulator& emulator);
GameConfigLoadCallback(const GameConfigLoadCallback& callback) = delete;
GameConfigLoadCallback& operator=(const GameConfigLoadCallback& callback) =
delete;
virtual ~GameConfigLoadCallback();
// The callback is invoked in the UI thread (or the thread that takes its
// place in the architecture of the specific app if there's no UI).
virtual void PostGameConfigLoad() = 0;
protected:
Emulator& emulator() const { return emulator_; }
private:
Emulator& emulator_;
};
explicit Emulator(const std::filesystem::path& command_line,
const std::filesystem::path& storage_root,
const std::filesystem::path& content_root,
const std::filesystem::path& cache_root);
~Emulator();
// Full command line used when launching the process.
const std::filesystem::path& command_line() const { return command_line_; }
// Folder persistent internal emulator data is stored in.
const std::filesystem::path& storage_root() const { return storage_root_; }
// Folder guest content is stored in.
const std::filesystem::path& content_root() const { return content_root_; }
// Folder files safe to remove without significant side effects are stored in.
const std::filesystem::path& cache_root() const { return cache_root_; }
// Name of the title in the default language.
const std::string& title_name() const { return title_name_; }
// Version of the title as a string.
const std::string& title_version() const { return title_version_; }
// Currently running title ID
uint32_t title_id() const {
return !title_id_.has_value() ? 0 : title_id_.value();
}
// Are we currently running a title?
bool is_title_open() const { return title_id_.has_value(); }
// Window used for displaying graphical output. Can be null.
ui::Window* display_window() const { return display_window_; }
// ImGui drawer for various kinds of dialogs requested by the guest. Can be
// null.
ui::ImGuiDrawer* imgui_drawer() const { return imgui_drawer_; }
// Guest memory system modelling the RAM (both virtual and physical) of the
// system.
Memory* memory() const { return memory_.get(); }
// Virtualized processor that can execute PPC code.
cpu::Processor* processor() const { return processor_.get(); }
// Audio hardware emulation for decoding and playback.
apu::AudioSystem* audio_system() const { return audio_system_.get(); }
// GPU emulation for command list processing.
gpu::GraphicsSystem* graphics_system() const {
return graphics_system_.get();
}
// Human-interface Device (HID) adapters for controllers.
hid::InputSystem* input_system() const { return input_system_.get(); }
// Kernel function export table used to resolve exports when JITing code.
cpu::ExportResolver* export_resolver() const {
return export_resolver_.get();
}
// File systems mapped to disc images, folders, etc for games and save data.
vfs::VirtualFileSystem* file_system() const { return file_system_.get(); }
// The 'kernel', tracking all kernel objects and other state.
// This is effectively the guest operating system.
kernel::KernelState* kernel_state() const { return kernel_state_.get(); }
patcher::Patcher* patcher() const { return patcher_.get(); }
// Initializes the emulator and configures all components.
// The given window is used for display and the provided functions are used
// to create subsystems as required.
// Once this function returns a game can be launched using one of the Launch
// functions.
X_STATUS Setup(
ui::Window* display_window, ui::ImGuiDrawer* imgui_drawer,
bool require_cpu_backend,
std::function<std::unique_ptr<apu::AudioSystem>(cpu::Processor*)>
audio_system_factory,
std::function<std::unique_ptr<gpu::GraphicsSystem>()>
graphics_system_factory,
std::function<std::vector<std::unique_ptr<hid::InputDriver>>(ui::Window*)>
input_driver_factory);
// Terminates the currently running title.
X_STATUS TerminateTitle();
const std::unique_ptr<vfs::Device> CreateVfsDeviceBasedOnPath(
const std::filesystem::path& path, const std::string_view mount_path);
X_STATUS MountPath(const std::filesystem::path& path,
const std::string_view mount_path);
// Launches a game from the given file path.
// This will attempt to infer the type of the given file (such as an iso, etc)
// using heuristics.
X_STATUS LaunchPath(const std::filesystem::path& path);
// Launches a game from a .xex file by mounting the containing folder as if it
// was an extracted STFS container.
X_STATUS LaunchXexFile(const std::filesystem::path& path);
// Launches a game from a disc image file (.iso, etc).
X_STATUS LaunchDiscImage(const std::filesystem::path& path);
// Launches a game from an STFS container file.
X_STATUS LaunchStfsContainer(const std::filesystem::path& path);
// Extract content of package to content specific directory.
X_STATUS InstallContentPackage(const std::filesystem::path& path);
void Pause();
void Resume();
bool is_paused() const { return paused_; }
bool SaveToFile(const std::filesystem::path& path);
bool RestoreFromFile(const std::filesystem::path& path);
// The game can request another title to be loaded.
bool TitleRequested();
void LaunchNextTitle();
const std::filesystem::path GetNewDiscPath(std::string window_message = "");
void WaitUntilExit();
public:
xe::Delegate<uint32_t, const std::string_view> on_launch;
xe::Delegate<bool> on_shader_storage_initialization;
xe::Delegate<> on_patch_apply;
xe::Delegate<> on_terminate;
xe::Delegate<> on_exit;
private:
enum : uint64_t {
EmulatorFlagDisclaimerAcknowledged = 1ULL << 0
};
static uint64_t GetPersistentEmulatorFlags();
static void SetPersistentEmulatorFlags(uint64_t new_flags);
static std::string CanonicalizeFileExtension(
const std::filesystem::path& path);
static bool ExceptionCallbackThunk(Exception* ex, void* data);
bool ExceptionCallback(Exception* ex);
void AddGameConfigLoadCallback(GameConfigLoadCallback* callback);
void RemoveGameConfigLoadCallback(GameConfigLoadCallback* callback);
std::string FindLaunchModule();
X_STATUS CompleteLaunch(const std::filesystem::path& path,
const std::string_view module_path);
std::filesystem::path command_line_;
std::filesystem::path storage_root_;
std::filesystem::path content_root_;
std::filesystem::path cache_root_;
std::string title_name_;
std::string title_version_;
ui::Window* display_window_ = nullptr;
ui::ImGuiDrawer* imgui_drawer_ = nullptr;
std::unique_ptr<Memory> memory_;
std::unique_ptr<cpu::Processor> processor_;
std::unique_ptr<apu::AudioSystem> audio_system_;
std::unique_ptr<gpu::GraphicsSystem> graphics_system_;
std::unique_ptr<hid::InputSystem> input_system_;
std::unique_ptr<cpu::ExportResolver> export_resolver_;
std::unique_ptr<vfs::VirtualFileSystem> file_system_;
std::unique_ptr<patcher::Patcher> patcher_;
std::unique_ptr<kernel::KernelState> kernel_state_;
// Accessible only from the thread that invokes those callbacks (the UI thread
// if the UI is available).
std::vector<GameConfigLoadCallback*> game_config_load_callbacks_;
// Using an index, not an iterator, because after the erasure, the adjustment
// must be done for the vector element indices that would be in the iterator
// range that would be invalidated.
// SIZE_MAX if not currently in the game config load callback loop.
size_t game_config_load_callback_loop_next_index_ = SIZE_MAX;
kernel::object_ref<kernel::XThread> main_thread_;
std::optional<uint32_t> title_id_; // Currently running title ID
bool paused_;
bool restoring_;
threading::Fence restore_fence_; // Fired on restore finish.
};
} // namespace xe
#endif // XENIA_EMULATOR_H_