2020-02-28 16:04:28 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2023-04-01 15:20:53 +00:00
|
|
|
Copyright (C) 2018-23 divingkatae and maximum
|
2020-02-28 16:04:28 +00:00
|
|
|
(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/>.
|
|
|
|
*/
|
2019-07-02 02:15:33 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
// The main runfile - main.cpp
|
|
|
|
// This is where the magic begins
|
2019-07-02 02:15:33 +00:00
|
|
|
|
2023-04-01 15:20:53 +00:00
|
|
|
#include <core/hostevents.h>
|
|
|
|
#include <core/timermanager.h>
|
|
|
|
#include <cpu/ppc/ppcemu.h>
|
|
|
|
#include <debugger/debugger.h>
|
2023-11-03 07:21:33 +00:00
|
|
|
#include <machines/machinebase.h>
|
2023-04-01 15:20:53 +00:00
|
|
|
#include <machines/machinefactory.h>
|
|
|
|
#include <utils/profiler.h>
|
Make Emscripten build not depend on SDL2 or cubeb
While Emscripten has an SDL compabtility layer, it assumes that the
code is executing in the main browser process (and thus has access to
them DOM). The Infinite Mac project runs emulators in a worker thread
(for better performance) and has a custom API for the display, sound,
input, etc. Similarly, it does not need the cross-platform sound support
from cubeb, there there is a sound API as well.
This commit makes SDL (*_sdl.cpp) and cubeb-based (*_cubeb.cpp) code be
skipped when targeting Emscripten, and instead *_js.cpp files are used
instead (this is the cross-platform convention used by Chromium[^1], and
could be extended for other targets).
For hostevents.cpp and soundserver.cpp the entire file was replaced,
whereas for videoctrl.cpp there was enough shared logic that it was
kept, and the platform-specific bits were moved behind a Display class
that can have per-platform implementations. For cases where we need
additional private fields in the platform-specific classes, we use
a PIMPL pattern.
The *_js.cpp files with implementations are not included in this
commit, since they are closely tied to the Infinite Mac project, and
will live in its fork of DingusPPC.
[^1]: https://www.chromium.org/developers/design-documents/conventions-and-patterns-for-multi-platform-development/
2023-09-04 00:59:35 +00:00
|
|
|
#include <main.h>
|
2023-04-01 15:20:53 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
#include <cinttypes>
|
2021-08-19 09:29:44 +00:00
|
|
|
#include <csignal>
|
2020-05-12 18:55:45 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
2021-09-15 22:46:38 +00:00
|
|
|
#include <CLI11.hpp>
|
|
|
|
#include <loguru.hpp>
|
2019-07-02 02:15:33 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2024-05-08 14:07:32 +00:00
|
|
|
static void sigint_handler(int signum) {
|
2023-11-22 00:57:28 +00:00
|
|
|
power_on = false;
|
|
|
|
power_off_reason = po_signal_interrupt;
|
2021-08-19 09:29:44 +00:00
|
|
|
}
|
|
|
|
|
2024-05-08 14:07:32 +00:00
|
|
|
static void sigabrt_handler(int signum) {
|
2021-09-25 08:26:28 +00:00
|
|
|
LOG_F(INFO, "Shutting down...");
|
|
|
|
|
|
|
|
delete gMachineObj.release();
|
Make Emscripten build not depend on SDL2 or cubeb
While Emscripten has an SDL compabtility layer, it assumes that the
code is executing in the main browser process (and thus has access to
them DOM). The Infinite Mac project runs emulators in a worker thread
(for better performance) and has a custom API for the display, sound,
input, etc. Similarly, it does not need the cross-platform sound support
from cubeb, there there is a sound API as well.
This commit makes SDL (*_sdl.cpp) and cubeb-based (*_cubeb.cpp) code be
skipped when targeting Emscripten, and instead *_js.cpp files are used
instead (this is the cross-platform convention used by Chromium[^1], and
could be extended for other targets).
For hostevents.cpp and soundserver.cpp the entire file was replaced,
whereas for videoctrl.cpp there was enough shared logic that it was
kept, and the platform-specific bits were moved behind a Display class
that can have per-platform implementations. For cases where we need
additional private fields in the platform-specific classes, we use
a PIMPL pattern.
The *_js.cpp files with implementations are not included in this
commit, since they are closely tied to the Infinite Mac project, and
will live in its fork of DingusPPC.
[^1]: https://www.chromium.org/developers/design-documents/conventions-and-patterns-for-multi-platform-development/
2023-09-04 00:59:35 +00:00
|
|
|
cleanup();
|
2021-09-25 08:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 09:01:13 +00:00
|
|
|
static string appDescription = string(
|
2024-04-15 14:01:51 +00:00
|
|
|
"\nDingusPPC - Alpha 1 (5/10/2024) "
|
|
|
|
"\nWritten by divingkatae, maximumspatium, "
|
|
|
|
"\njoevt, mihaip, et. al. "
|
|
|
|
"\n(c) 2018-2024 The DingusPPC Dev Team. "
|
|
|
|
"\nThis is a build intended for testing. "
|
2020-10-06 09:01:13 +00:00
|
|
|
"\nUse at your own discretion. "
|
|
|
|
"\n"
|
|
|
|
);
|
2020-08-26 03:07:02 +00:00
|
|
|
|
2024-05-20 04:59:18 +00:00
|
|
|
void run_machine(std::string machine_str, std::string bootrom_path, uint32_t execution_mode, uint32_t profiling_interval_ms);
|
2024-03-08 07:30:55 +00:00
|
|
|
|
2020-05-12 18:55:45 +00:00
|
|
|
int main(int argc, char** argv) {
|
2020-08-22 18:05:08 +00:00
|
|
|
|
2023-06-20 05:36:27 +00:00
|
|
|
uint32_t execution_mode = interpreter;
|
2020-08-22 18:05:08 +00:00
|
|
|
|
2020-10-06 09:01:13 +00:00
|
|
|
CLI::App app(appDescription);
|
2020-09-20 21:25:29 +00:00
|
|
|
app.allow_windows_style_options(); /* we want Windows-style options */
|
|
|
|
app.allow_extras();
|
|
|
|
|
2024-08-26 00:05:46 +00:00
|
|
|
bool realtime_enabled = false;
|
|
|
|
bool debugger_enabled = false;
|
2020-09-20 21:25:29 +00:00
|
|
|
string bootrom_path("bootrom.bin");
|
|
|
|
|
|
|
|
app.add_flag("-r,--realtime", realtime_enabled,
|
|
|
|
"Run the emulator in real-time");
|
|
|
|
app.add_flag("-d,--debugger", debugger_enabled,
|
|
|
|
"Enter the built-in debugger");
|
|
|
|
app.add_option("-b,--bootrom", bootrom_path, "Specifies BootROM path")
|
|
|
|
->check(CLI::ExistingFile);
|
|
|
|
|
2024-08-26 00:05:46 +00:00
|
|
|
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");
|
|
|
|
|
2024-05-20 04:59:18 +00:00
|
|
|
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
|
|
|
|
|
2024-08-26 00:05:46 +00:00
|
|
|
string machine_str;
|
2020-09-20 21:25:29 +00:00
|
|
|
CLI::Option* machine_opt = app.add_option("-m,--machine",
|
|
|
|
machine_str, "Specify machine ID");
|
|
|
|
|
2020-10-06 09:01:13 +00:00
|
|
|
auto list_cmd = app.add_subcommand("list",
|
2020-09-20 21:25:29 +00:00
|
|
|
"Display available machine configurations and exit");
|
|
|
|
|
2020-10-06 09:01:13 +00:00
|
|
|
string sub_arg;
|
|
|
|
|
|
|
|
list_cmd->add_option("machines", sub_arg, "List supported machines");
|
|
|
|
list_cmd->add_option("properties", sub_arg, "List available properties");
|
|
|
|
|
2020-09-20 21:25:29 +00:00
|
|
|
CLI11_PARSE(app, argc, argv);
|
|
|
|
|
2020-10-06 09:01:13 +00:00
|
|
|
if (*list_cmd) {
|
2020-10-13 02:24:54 +00:00
|
|
|
if (sub_arg == "machines") {
|
2022-07-18 09:48:23 +00:00
|
|
|
MachineFactory::list_machines();
|
2020-10-13 02:24:54 +00:00
|
|
|
} else if (sub_arg == "properties") {
|
2022-07-18 09:48:23 +00:00
|
|
|
MachineFactory::list_properties();
|
2020-10-13 02:24:54 +00:00
|
|
|
} else {
|
|
|
|
cout << "Unknown list subcommand " << sub_arg << endl;
|
|
|
|
}
|
|
|
|
return 0;
|
2020-10-06 09:01:13 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 21:25:29 +00:00
|
|
|
if (debugger_enabled) {
|
|
|
|
if (realtime_enabled)
|
|
|
|
cout << "Both realtime and debugger enabled! Using debugger" << endl;
|
2024-08-26 00:05:46 +00:00
|
|
|
execution_mode = debugger;
|
2020-09-20 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize logging */
|
|
|
|
loguru::g_preamble_date = false;
|
|
|
|
loguru::g_preamble_time = false;
|
|
|
|
loguru::g_preamble_thread = false;
|
2024-08-26 00:05:46 +00:00
|
|
|
loguru::g_preamble_uptime = !log_no_uptime;
|
2020-09-20 21:25:29 +00:00
|
|
|
|
2024-08-26 00:05:46 +00:00
|
|
|
if (execution_mode == interpreter && !log_to_stderr) {
|
2020-09-20 21:25:29 +00:00
|
|
|
loguru::g_stderr_verbosity = loguru::Verbosity_OFF;
|
|
|
|
loguru::init(argc, argv);
|
2024-08-26 00:05:46 +00:00
|
|
|
loguru::add_file("dingusppc.log", loguru::Append, log_verbosity);
|
2020-09-20 21:25:29 +00:00
|
|
|
} else {
|
2024-08-26 00:05:46 +00:00
|
|
|
loguru::g_stderr_verbosity = log_verbosity;
|
2020-09-20 21:25:29 +00:00
|
|
|
loguru::init(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*machine_opt) {
|
|
|
|
LOG_F(INFO, "Machine option was passed in: %s", machine_str.c_str());
|
|
|
|
} else {
|
2022-07-18 09:48:23 +00:00
|
|
|
machine_str = MachineFactory::machine_name_from_rom(bootrom_path);
|
2020-09-20 21:25:29 +00:00
|
|
|
if (machine_str.empty()) {
|
|
|
|
LOG_F(ERROR, "Could not autodetect machine");
|
2023-08-31 06:10:46 +00:00
|
|
|
return 1;
|
2020-10-06 09:01:13 +00:00
|
|
|
}
|
2020-10-04 16:58:21 +00:00
|
|
|
else {
|
|
|
|
LOG_F(INFO, "Machine was autodetected as: %s", machine_str.c_str());
|
2020-09-20 21:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 09:01:13 +00:00
|
|
|
/* handle overriding of machine settings from command line */
|
2020-09-20 21:25:29 +00:00
|
|
|
map<string, string> settings;
|
2022-07-18 09:48:23 +00:00
|
|
|
if (MachineFactory::get_machine_settings(machine_str, settings) < 0) {
|
2023-08-31 06:10:46 +00:00
|
|
|
return 1;
|
2020-10-06 09:01:13 +00:00
|
|
|
}
|
2020-09-20 21:25:29 +00:00
|
|
|
|
|
|
|
CLI::App sa;
|
|
|
|
sa.allow_extras();
|
|
|
|
|
|
|
|
for (auto& s : settings) {
|
|
|
|
sa.add_option("--" + s.first, s.second);
|
|
|
|
}
|
|
|
|
sa.parse(app.remaining_for_passthrough()); /* TODO: handle exceptions! */
|
|
|
|
|
2022-07-18 09:48:23 +00:00
|
|
|
MachineFactory::set_machine_settings(settings);
|
2020-09-20 21:25:29 +00:00
|
|
|
|
|
|
|
cout << "BootROM path: " << bootrom_path << endl;
|
|
|
|
cout << "Execution mode: " << execution_mode << endl;
|
|
|
|
|
Make Emscripten build not depend on SDL2 or cubeb
While Emscripten has an SDL compabtility layer, it assumes that the
code is executing in the main browser process (and thus has access to
them DOM). The Infinite Mac project runs emulators in a worker thread
(for better performance) and has a custom API for the display, sound,
input, etc. Similarly, it does not need the cross-platform sound support
from cubeb, there there is a sound API as well.
This commit makes SDL (*_sdl.cpp) and cubeb-based (*_cubeb.cpp) code be
skipped when targeting Emscripten, and instead *_js.cpp files are used
instead (this is the cross-platform convention used by Chromium[^1], and
could be extended for other targets).
For hostevents.cpp and soundserver.cpp the entire file was replaced,
whereas for videoctrl.cpp there was enough shared logic that it was
kept, and the platform-specific bits were moved behind a Display class
that can have per-platform implementations. For cases where we need
additional private fields in the platform-specific classes, we use
a PIMPL pattern.
The *_js.cpp files with implementations are not included in this
commit, since they are closely tied to the Infinite Mac project, and
will live in its fork of DingusPPC.
[^1]: https://www.chromium.org/developers/design-documents/conventions-and-patterns-for-multi-platform-development/
2023-09-04 00:59:35 +00:00
|
|
|
if (!init()) {
|
|
|
|
LOG_F(ERROR, "Cannot initialize");
|
2023-08-31 06:10:46 +00:00
|
|
|
return 1;
|
2021-09-11 19:02:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 23:27:07 +00:00
|
|
|
// initialize global profiler object
|
|
|
|
gProfilerObj.reset(new Profiler());
|
|
|
|
|
2021-09-25 08:26:28 +00:00
|
|
|
// graceful handling of fatal errors
|
|
|
|
loguru::set_fatal_handler([](const loguru::Message& message) {
|
2023-10-19 14:50:58 +00:00
|
|
|
// 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;
|
2023-11-22 00:57:28 +00:00
|
|
|
power_off_reason = po_enter_debugger;
|
2021-09-25 08:26:28 +00:00
|
|
|
enter_debugger();
|
|
|
|
|
2024-03-09 19:56:43 +00:00
|
|
|
// Ensure that NVRAM and other state is persisted before we terminate.
|
|
|
|
delete gMachineObj.release();
|
2021-09-25 08:26:28 +00:00
|
|
|
});
|
|
|
|
|
2021-08-19 09:29:44 +00:00
|
|
|
// redirect SIGINT to our own handler
|
|
|
|
signal(SIGINT, sigint_handler);
|
|
|
|
|
2021-09-25 08:26:28 +00:00
|
|
|
// redirect SIGABRT to our own handler
|
|
|
|
signal(SIGABRT, sigabrt_handler);
|
|
|
|
|
2024-03-08 07:30:55 +00:00
|
|
|
while (true) {
|
2024-05-20 04:59:18 +00:00
|
|
|
run_machine(machine_str, bootrom_path, execution_mode, profiling_interval_ms);
|
2024-03-08 07:30:55 +00:00
|
|
|
if (power_off_reason == po_restarting) {
|
|
|
|
LOG_F(INFO, "Restarting...");
|
|
|
|
power_on = true;
|
|
|
|
continue;
|
|
|
|
}
|
2024-05-06 06:40:32 +00:00
|
|
|
if (power_off_reason == po_shutting_down) {
|
|
|
|
if (execution_mode != debugger) {
|
|
|
|
LOG_F(INFO, "Shutdown.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LOG_F(INFO, "Shutdown...");
|
|
|
|
power_on = true;
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-08 07:30:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-20 04:59:18 +00:00
|
|
|
void run_machine(std::string machine_str, std::string bootrom_path, uint32_t execution_mode, uint32_t profiling_interval_ms) {
|
2024-03-08 07:30:55 +00:00
|
|
|
if (MachineFactory::create_machine_for_id(machine_str, bootrom_path) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-01 15:20:53 +00:00
|
|
|
// set up system wide event polling using
|
|
|
|
// default Macintosh polling rate of 11 ms
|
2024-03-08 07:30:55 +00:00
|
|
|
uint32_t event_timer = TimerManager::get_instance()->add_cyclic_timer(MSECS_TO_NSECS(11), [] {
|
2023-04-01 15:20:53 +00:00
|
|
|
EventManager::get_instance()->poll_events();
|
|
|
|
});
|
|
|
|
|
2024-05-20 04:59:18 +00:00
|
|
|
#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
|
|
|
|
|
2020-10-04 16:58:21 +00:00
|
|
|
switch (execution_mode) {
|
2023-06-20 05:36:27 +00:00
|
|
|
case interpreter:
|
2023-11-22 00:57:28 +00:00
|
|
|
power_off_reason = po_starting_up;
|
|
|
|
enter_debugger();
|
2022-03-03 02:22:13 +00:00
|
|
|
break;
|
2023-06-20 05:36:27 +00:00
|
|
|
case debugger:
|
2023-11-22 00:57:28 +00:00
|
|
|
power_off_reason = po_enter_debugger;
|
2022-03-03 02:22:13 +00:00
|
|
|
enter_debugger();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_F(ERROR, "Invalid EXECUTION MODE");
|
2024-03-08 07:30:55 +00:00
|
|
|
return;
|
2020-09-20 21:25:29 +00:00
|
|
|
}
|
2020-10-06 09:01:13 +00:00
|
|
|
|
2020-03-13 21:49:58 +00:00
|
|
|
LOG_F(INFO, "Cleaning up...");
|
2024-03-08 07:30:55 +00:00
|
|
|
TimerManager::get_instance()->cancel_timer(event_timer);
|
2024-05-20 04:59:18 +00:00
|
|
|
#ifdef CPU_PROFILING
|
|
|
|
if (profiling_interval_ms > 0) {
|
|
|
|
TimerManager::get_instance()->cancel_timer(profiling_timer);
|
|
|
|
}
|
|
|
|
#endif
|
2024-03-08 07:30:55 +00:00
|
|
|
EventManager::get_instance()->disconnect_handlers();
|
2020-03-13 21:49:58 +00:00
|
|
|
delete gMachineObj.release();
|
2019-07-02 02:15:33 +00:00
|
|
|
}
|