-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.cpp
355 lines (302 loc) · 11.9 KB
/
main.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
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
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
Copyright (C) 2018-23 divingkatae and maximum
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
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
(at your option) 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 <https://www.gnu.org/licenses/>.
*/
// The main runfile - main.cpp
// This is where the magic begins
#include <core/hostevents.h>
#include <core/timermanager.h>
#include <cpu/ppc/ppcdisasm.h>
#include <cpu/ppc/ppcemu.h>
#include <cpu/ppc/ppcmmu.h>
#include <debugger/debugger.h>
#include <machines/machinebase.h>
#include <machines/machinefactory.h>
#include <utils/profiler.h>
#include <main.h>
#include <cinttypes>
#include <csignal>
#include <cstring>
#include <iostream>
#include <CLI11.hpp>
#include <loguru.hpp>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#endif
using namespace std;
static void sigint_handler(int signum) {
power_on = false;
power_off_reason = po_signal_interrupt;
}
static void sigabrt_handler(int signum) {
LOG_F(INFO, "Shutting down...");
delete gMachineObj.release();
cleanup();
}
static string appDescription = string(
"\nDingusPPC - Alpha 1.01 (10/31/2024) "
"\nWritten by divingkatae, maximumspatium, "
"\njoevt, mihaip, kkaisershot, et. al. "
"\n(c) 2018-2024 The DingusPPC Dev Team. "
"\nThis is a build intended for testing. "
"\nUse at your own discretion. "
"\n"
);
/// Check for an existing directory (returns error message if check fails)
class WorkingDirectoryValidator : public CLI::detail::ExistingDirectoryValidator {
public:
WorkingDirectoryValidator() {
func_ = [](std::string& filename) {
std::string result = CLI::ExistingDirectory.operator()(filename);
if (result.empty()) {
#ifdef _WIN32
_chdir(filename.c_str());
#else
chdir(filename.c_str());
#endif
}
return result;
};
}
};
const WorkingDirectoryValidator WorkingDirectory;
void run_machine(std::string machine_str, char *rom_data, size_t rom_size, uint32_t execution_mode, uint32_t profiling_interval_ms);
int main(int argc, char** argv) {
uint32_t execution_mode = interpreter;
CLI::App app(appDescription);
app.allow_windows_style_options(); /* we want Windows-style options */
app.allow_extras();
bool realtime_enabled = false;
bool debugger_enabled = false;
string bootrom_path("bootrom.bin");
string working_directory_path(".");
auto execution_mode_group = app.add_option_group("execution mode")
->require_option(-1);
execution_mode_group->add_flag("-r,--realtime", realtime_enabled,
"Run the emulator in real-time");
execution_mode_group->add_flag("-d,--debugger", debugger_enabled,
"Enter the built-in debugger");
app.add_option("-w,--workingdir", working_directory_path, "Specifies working directory")
->check(WorkingDirectory);
app.add_option("-b,--bootrom", bootrom_path, "Specifies BootROM path")
->check(CLI::ExistingFile);
app.add_flag("--deterministic", is_deterministic,
"Make execution deterministic");
bool log_to_stderr = false;
loguru::Verbosity log_verbosity = loguru::Verbosity_INFO;
bool log_no_uptime = false;
app.add_flag("--log-to-stderr", log_to_stderr,
"Send internal logging to stderr (instead of dingusppc.log)");
app.add_flag("--log-verbosity", log_verbosity,
"Adjust logging verbosity (default is 0 a.k.a. INFO)")
->check(CLI::Number);
app.add_flag("--log-no-uptime", log_no_uptime,
"Disable the uptime preamble of logged messages");
uint32_t profiling_interval_ms = 0;
#ifdef CPU_PROFILING
app.add_option("--profiling-interval-ms", profiling_interval_ms,
"Specifies periodic interval (in ms) at which to output CPU profiling information");
#endif
string machine_str;
CLI::Option* machine_opt = app.add_option("-m,--machine",
machine_str, "Specify machine ID");
auto list_cmd = app.add_subcommand("list",
"Display available machine configurations and exit");
string sub_arg;
list_cmd->add_option("machines", sub_arg, "List supported machines");
list_cmd->add_option("properties", sub_arg, "List available properties");
CLI11_PARSE(app, argc, argv);
if (*list_cmd) {
if (sub_arg == "machines") {
MachineFactory::list_machines();
} else if (sub_arg == "properties") {
MachineFactory::list_properties();
} else {
cout << "Unknown list subcommand " << sub_arg << endl;
}
return 0;
}
if (debugger_enabled) {
execution_mode = debugger;
}
/* initialize logging */
loguru::g_preamble_date = false;
loguru::g_preamble_time = false;
loguru::g_preamble_thread = false;
loguru::g_preamble_uptime = !log_no_uptime;
if (execution_mode == interpreter && !log_to_stderr) {
loguru::g_stderr_verbosity = loguru::Verbosity_OFF;
loguru::init(argc, argv);
loguru::add_file("dingusppc.log", loguru::Append, log_verbosity);
} else {
loguru::g_stderr_verbosity = log_verbosity;
loguru::init(argc, argv);
}
auto rom_data = std::unique_ptr<char[]>(new char[4 * 1024 * 1024]);
memset(&rom_data[0], 0, 4 * 1024 * 1024);
size_t rom_size = MachineFactory::read_boot_rom(bootrom_path, &rom_data[0]);
if (!rom_size) {
return 1;
}
string machine_str_from_rom = MachineFactory::machine_name_from_rom(&rom_data[0], rom_size);
if (machine_str_from_rom.empty()) {
LOG_F(ERROR, "Could not autodetect machine from ROM.");
} else {
LOG_F(INFO, "Machine detected from ROM as: %s", machine_str_from_rom.c_str());
}
if (*machine_opt) {
LOG_F(INFO, "Machine option was passed in: %s", machine_str.c_str());
} else {
machine_str = machine_str_from_rom;
}
if (machine_str.empty()) {
LOG_F(ERROR, "Must specificy a machine or provide a supported ROM.");
return 1;
}
// Hook to allow properties to be read from the command-line, regardless
// of when they are registered.
MachineFactory::get_setting_value = [&](const std::string& name) -> std::optional<std::string> {
CLI::App sa;
sa.allow_extras();
std::string value;
sa.add_option("--" + name, value);
try {
sa.parse(app.remaining_for_passthrough());
} catch (const CLI::Error& e) {
ABORT_F("Cannot parse CLI: %s", e.get_name().c_str());
}
if (sa.count("--" + name) > 0) {
return value;
} else {
return std::nullopt;
}
};
if (MachineFactory::register_machine_settings(machine_str) < 0) {
return 1;
}
cout << "BootROM path: " << bootrom_path << endl;
cout << "Execution mode: " << execution_mode << endl;
if (is_deterministic) {
cout << "Using deterministic execution mode, input will be ignored." << endl;
}
if (!init()) {
LOG_F(ERROR, "Cannot initialize");
return 1;
}
// initialize global profiler object
gProfilerObj.reset(new Profiler());
// graceful handling of fatal errors
loguru::set_fatal_handler([](const loguru::Message& message) {
// Make sure the reason for the failure is visible (it may have been
// sent to the logfile only).
cerr << message.preamble << message.indentation << message.prefix << message.message << endl;
power_off_reason = po_enter_debugger;
DppcDebugger::get_instance()->enter_debugger();
// Ensure that NVRAM and other state is persisted before we terminate.
delete gMachineObj.release();
});
// redirect SIGINT to our own handler
signal(SIGINT, sigint_handler);
// redirect SIGABRT to our own handler
signal(SIGABRT, sigabrt_handler);
while (true) {
run_machine(machine_str, &rom_data[0], rom_size, execution_mode, profiling_interval_ms);
if (power_off_reason == po_restarting) {
LOG_F(INFO, "Restarting...");
power_on = true;
continue;
}
if (power_off_reason == po_shutting_down) {
if (execution_mode != debugger) {
LOG_F(INFO, "Shutdown.");
break;
}
LOG_F(INFO, "Shutdown...");
power_on = true;
continue;
}
break;
}
cleanup();
return 0;
}
void run_machine(std::string machine_str, char *rom_data, size_t rom_size, uint32_t execution_mode,
uint32_t
#ifdef CPU_PROFILING
profiling_interval_ms
#endif
) {
if (MachineFactory::create_machine_for_id(machine_str, rom_data, rom_size) < 0) {
return;
}
uint32_t deterministic_timer;
if (is_deterministic) {
EventManager::get_instance()->disable_input_handlers();
// Log the PC and instruction every second to make it easier to validate
// that execution is the same every time.
deterministic_timer = TimerManager::get_instance()->add_cyclic_timer(MSECS_TO_NSECS(1000), [] {
PPCDisasmContext ctx;
ctx.instr_code = ppc_read_instruction(mmu_translate_imem(ppc_state.pc));
ctx.instr_addr = ppc_state.pc;
ctx.simplified = false;
auto op_name = disassemble_single(&ctx);
LOG_F(INFO, "TS=%016llu PC=0x%08x executing %s", get_virt_time_ns(), ppc_state.pc, op_name.c_str());
});
}
// set up system wide event polling using
// default Macintosh polling rate of 11 ms
uint32_t event_timer = TimerManager::get_instance()->add_cyclic_timer(MSECS_TO_NSECS(11), [] {
EventManager::get_instance()->poll_events();
});
#ifdef CPU_PROFILING
uint32_t profiling_timer;
if (profiling_interval_ms > 0) {
profiling_timer = TimerManager::get_instance()->add_cyclic_timer(MSECS_TO_NSECS(profiling_interval_ms), [] {
gProfilerObj->print_profile("PPC_CPU");
});
}
#endif
switch (execution_mode) {
case interpreter:
power_off_reason = po_starting_up;
DppcDebugger::get_instance()->enter_debugger();
break;
case threaded_int:
power_off_reason = po_starting_up;
DppcDebugger::get_instance()->enter_debugger();
break;
case debugger:
power_off_reason = po_enter_debugger;
DppcDebugger::get_instance()->enter_debugger();
break;
default:
LOG_F(ERROR, "Invalid EXECUTION MODE");
return;
}
LOG_F(INFO, "Cleaning up...");
TimerManager::get_instance()->cancel_timer(event_timer);
#ifdef CPU_PROFILING
if (profiling_interval_ms > 0) {
TimerManager::get_instance()->cancel_timer(profiling_timer);
}
#endif
if (is_deterministic) {
TimerManager::get_instance()->cancel_timer(deterministic_timer);
}
EventManager::get_instance()->disconnect_handlers();
delete gMachineObj.release();
}