Compare commits

...

2 Commits

Author SHA1 Message Date
iris 004dcee429 oops 2026-04-13 23:58:36 +02:00
iris ad6007b254 attempt moving core to separate thread again 2026-04-13 18:00:45 +02:00
7 changed files with 318 additions and 276 deletions
+31 -28
View File
@@ -1,52 +1,55 @@
#include <Core.hpp>
#include <EmuThread.hpp>
#include <KaizenGui.hpp>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
EmuThread::EmuThread(double &fps, SettingsWindow &settings) noexcept : settings(settings), fps(fps) {}
void EmuThread::run() const noexcept {
void EmuThread::run() noexcept {
n64::Core& core = n64::Core::GetInstance();
if(!core.romLoaded) return;
rdramCopy = core.GetMem().GetRDRAM();
auto lastSample = std::chrono::high_resolution_clock::now();
auto avgFps = 16.667;
auto sampledFps = 0;
static bool oneSecondPassed = false;
while(!shouldExit) {
if(!core.romLoaded) {
std::this_thread::sleep_for(1ms);
continue;
}
fps = 1000.0 / avgFps;
if(core.pause) {
std::this_thread::sleep_for(1ms);
continue;
}
const auto startFrameTime = std::chrono::high_resolution_clock::now();
if (!core.pause) {
core.Run(settings.getVolumeL(), settings.getVolumeR());
{
std::lock_guard<std::mutex> lk(presentMutex);
memcpy(rdramCopy.data(), core.GetMem().GetRDRAMPtr(), RDRAM_SIZE);
}
readyForPresentation.notify_all();
}
}
const auto endFrameTime = std::chrono::high_resolution_clock::now();
using namespace std::chrono_literals;
const auto frameTimeMs = std::chrono::duration<double>(endFrameTime - startFrameTime) / 1ms;
avgFps += frameTimeMs;
sampledFps++;
if (const auto elapsedSinceLastSample = std::chrono::duration<double>(endFrameTime - lastSample) / 1s;
elapsedSinceLastSample >= 1.0) {
if (!oneSecondPassed) {
oneSecondPassed = true;
return;
}
avgFps /= sampledFps;
fps = 1000.0 / avgFps;
}
void EmuThread::create() noexcept {
shouldExit.store(false, std::memory_order_release);
std::thread worker(&EmuThread::run, this);
worker.detach();
}
void EmuThread::TogglePause() const noexcept {
n64::Core::GetInstance().TogglePause();
}
void EmuThread::Reset() const noexcept {
n64::Core::GetInstance().Reset();
void EmuThread::Reset() noexcept {
n64::Core& core = n64::Core::GetInstance();
core.Reset();
rdramCopy = core.GetMem().GetRDRAM();
}
void EmuThread::Stop() const noexcept {
void EmuThread::Stop() noexcept {
shouldExit.store(true, std::memory_order_release);
n64::Core& core = n64::Core::GetInstance();
core.Stop();
core.rom = {};
+9 -3
View File
@@ -2,6 +2,7 @@
#include <RenderWidget.hpp>
#include <SettingsWindow.hpp>
#include <memory>
#include <atomic>
namespace n64 {
struct Core;
@@ -12,12 +13,17 @@ class EmuThread final {
public:
explicit EmuThread(double &, SettingsWindow &) noexcept;
~EmuThread() = default;
void run() const noexcept;
void create() noexcept;
void run() noexcept;
void TogglePause() const noexcept;
void Reset() const noexcept;
void Stop() const noexcept;
void Reset() noexcept;
void Stop() noexcept;
bool interruptionRequested = false, parallelRDPInitialized = false;
SettingsWindow &settings;
double& fps;
std::atomic_bool shouldExit = false;
std::mutex presentMutex;
std::condition_variable readyForPresentation;
std::vector<u8> rdramCopy;
};
+64 -32
View File
@@ -4,8 +4,13 @@
#include <ImGuiImpl/ProgressIndicators.hpp>
#include <ImGuiImpl/StatusBar.hpp>
#include <resources/gamecontrollerdb.h>
#include <chrono>
KaizenGui::KaizenGui() noexcept : window("Kaizen " KAIZEN_VERSION_STR, 1280, 720), settingsWindow(window), vulkanWidget(window.getHandle()), emuThread(fpsCounter, settingsWindow) {
using namespace std::chrono_literals;
KaizenGui::KaizenGui() noexcept :
window("Kaizen " KAIZEN_VERSION_STR, 1280, 720), settingsWindow(window), emuThread(fpsCounter, settingsWindow),
vulkanWidget(window.getHandle(), emuThread.rdramCopy.data()) {
gui::Initialize(n64::Core::GetInstance().parallel.wsi, window.getHandle());
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
@@ -33,7 +38,8 @@ void KaizenGui::QueryDevices(const SDL_Event &event) {
if (gamepad)
SDL_CloseGamepad(gamepad);
break;
default: break;
default:
break;
}
}
@@ -45,7 +51,8 @@ void KaizenGui::HandleInput(const SDL_Event &event) {
if (!gamepad)
break;
{
pif.UpdateButton(0, n64::Controller::Key::Z, SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) == SDL_JOYSTICK_AXIS_MAX);
pif.UpdateButton(0, n64::Controller::Key::Z,
SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) == SDL_JOYSTICK_AXIS_MAX);
pif.UpdateButton(0, n64::Controller::Key::CUp, SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTY) <= -127);
pif.UpdateButton(0, n64::Controller::Key::CDown, SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTY) >= 127);
pif.UpdateButton(0, n64::Controller::Key::CLeft, SDL_GetGamepadAxis(gamepad, SDL_GAMEPAD_AXIS_RIGHTX) <= -127);
@@ -134,16 +141,21 @@ void KaizenGui::HandleInput(const SDL_Event &event) {
float x = 0, y = 0;
if (keys[SDL_SCANCODE_UP]) y = 86;
if (keys[SDL_SCANCODE_DOWN]) y = -86;
if (keys[SDL_SCANCODE_LEFT]) x = -86;
if (keys[SDL_SCANCODE_RIGHT]) x = 86;
if (keys[SDL_SCANCODE_UP])
y = 86;
if (keys[SDL_SCANCODE_DOWN])
y = -86;
if (keys[SDL_SCANCODE_LEFT])
x = -86;
if (keys[SDL_SCANCODE_RIGHT])
x = 86;
pif.UpdateAxis(0, n64::Controller::Axis::X, x);
pif.UpdateAxis(0, n64::Controller::Axis::Y, y);
}
break;
default: break;
default:
break;
}
}
@@ -156,9 +168,9 @@ std::pair<std::optional<s64>, std::optional<Util::Error::MemoryAccess>> RenderEr
auto memoryAccess = Util::Error::GetMemoryAccess();
if (memoryAccess.has_value()) {
const auto [is_write, size, address, written_val] = memoryAccess.value();
ImGui::Text("%s", std::format("{} {}-bit value @ {:08X}{}", is_write ? "Writing" : "Reading",
static_cast<u8>(size), address,
is_write ? std::format(" (value = 0x{:X})", written_val) : "")
ImGui::Text("%s",
std::format("{} {}-bit value @ {:08X}{}", is_write ? "Writing" : "Reading", static_cast<u8>(size),
address, is_write ? std::format(" (value = 0x{:X})", written_val) : "")
.c_str());
}
@@ -260,7 +272,8 @@ void KaizenGui::RenderUI() {
if (ImGui::BeginPopupModal(Util::Error::GetSeverity().as_c_str(), nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
emuThread.TogglePause();
switch (Util::Error::GetSeverity().as_enum) {
case Util::Error::Severity::WARN: {
case Util::Error::Severity::WARN:
{
ImGui::PushStyleColor(ImGuiCol_TitleBg, 0x8054eae5);
ImGui::PushStyleColor(ImGuiCol_Text, 0xff7be4e1);
ImGui::Text("Warning of type: %s", Util::Error::GetType().as_c_str());
@@ -270,8 +283,10 @@ void KaizenGui::RenderUI() {
RenderErrorMessageDetails();
if (n64::Core::GetInstance().romLoaded && !n64::Core::GetInstance().pause) {
const bool ignore = ImGui::Button("Try continuing"); ImGui::SameLine();
const bool stop = ImGui::Button("Stop emulation"); ImGui::SameLine();
const bool ignore = ImGui::Button("Try continuing");
ImGui::SameLine();
const bool stop = ImGui::Button("Stop emulation");
ImGui::SameLine();
const bool chooseAnother = ImGui::Button("Choose another ROM");
if (ignore || stop || chooseAnother) {
Util::Error::SetHandled();
@@ -294,8 +309,10 @@ void KaizenGui::RenderUI() {
if (ImGui::Button("OK"))
ImGui::CloseCurrentPopup();
} break;
case Util::Error::Severity::UNRECOVERABLE: {
}
break;
case Util::Error::Severity::UNRECOVERABLE:
{
emuThread.Stop();
ImGui::PushStyleColor(ImGuiCol_TitleBg, 0x800000ff);
ImGui::PushStyleColor(ImGuiCol_Text, 0xff3b3bbf);
@@ -307,8 +324,10 @@ void KaizenGui::RenderUI() {
RenderErrorMessageDetails();
if (ImGui::Button("OK"))
ImGui::CloseCurrentPopup();
} break;
case Util::Error::Severity::NON_FATAL: {
}
break;
case Util::Error::Severity::NON_FATAL:
{
ImGui::PushStyleColor(ImGuiCol_TitleBg, 0x800000ff);
ImGui::PushStyleColor(ImGuiCol_Text, 0xff3b3bbf);
ImGui::Text("An error has occurred!");
@@ -318,10 +337,13 @@ void KaizenGui::RenderUI() {
ImGui::Text(R"(Error message: "%s")", Util::Error::GetError().c_str());
auto [lastPC, memoryAccess] = RenderErrorMessageDetails();
const bool ignore = ImGui::Button("Try continuing"); ImGui::SameLine();
const bool stop = ImGui::Button("Stop emulation"); ImGui::SameLine();
const bool ignore = ImGui::Button("Try continuing");
ImGui::SameLine();
const bool stop = ImGui::Button("Stop emulation");
ImGui::SameLine();
const bool chooseAnother = ImGui::Button("Choose another ROM");
const bool openInDebugger = lastPC.has_value() ? ImGui::Button("Add breakpoint at this PC and open the debugger") : false;
const bool openInDebugger =
lastPC.has_value() ? ImGui::Button("Add breakpoint at this PC and open the debugger") : false;
if (ignore || stop || chooseAnother || openInDebugger) {
Util::Error::SetHandled();
ImGui::CloseCurrentPopup();
@@ -346,8 +368,10 @@ void KaizenGui::RenderUI() {
debugger.Open();
emuThread.Reset();
}
} break;
default: break;
}
break;
default:
break;
}
ImGui::EndPopup();
@@ -359,7 +383,8 @@ void KaizenGui::RenderUI() {
}
if (shouldDisplaySpinner) {
ImGui::SetNextWindowPos({static_cast<float>(width) * 0.5f, static_cast<float>(height) * 0.5f}, 0, ImVec2(0.5f, 0.5f));
ImGui::SetNextWindowPos({static_cast<float>(width) * 0.5f, static_cast<float>(height) * 0.5f}, 0,
ImVec2(0.5f, 0.5f));
ImGui::PushStyleColor(ImGuiCol_WindowBg, IM_COL32_BLACK_TRANS);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
@@ -386,8 +411,11 @@ void KaizenGui::RenderUI() {
if (fileDialogOpen) {
fileDialogOpen = false;
constexpr SDL_DialogFileFilter filters[] = {{"All files", "*"}, {"Nintendo 64 executable", "n64;z64;v64"}, {"Nintendo 64 executable archive", "rar;tar;zip;7z"}};
SDL_ShowOpenFileDialog([](void *userdata, const char * const *filelist, int) {
constexpr SDL_DialogFileFilter filters[] = {{"All files", "*"},
{"Nintendo 64 executable", "n64;z64;v64"},
{"Nintendo 64 executable archive", "rar;tar;zip;7z"}};
SDL_ShowOpenFileDialog(
[](void *userdata, const char *const *filelist, int) {
auto kaizen = static_cast<KaizenGui *>(userdata);
if (!filelist) {
@@ -405,14 +433,19 @@ void KaizenGui::RenderUI() {
std::thread fileWorker(&KaizenGui::FileWorker, kaizen);
fileWorker.detach();
}, this, window.getHandle(), filters, 3, nullptr, false);
},
this, window.getHandle(), filters, 3, nullptr, false);
}
if (minimized)
return;
if (core.romLoaded) {
std::unique_lock<std::mutex> lk(emuThread.presentMutex);
if (emuThread.readyForPresentation.wait_for(lk, 16.6667ms) == std::cv_status::no_timeout)
core.parallel.UpdateScreen<true>();
else
core.parallel.UpdateScreen<false>();
return;
}
@@ -422,6 +455,7 @@ void KaizenGui::RenderUI() {
void KaizenGui::LoadROM(const std::string &path) noexcept {
n64::Core &core = n64::Core::GetInstance();
core.LoadROM(path);
emuThread.create();
const auto gameNameDB = n64::Core::GetMem().rom.gameNameDB;
SDL_SetWindowTitle(window.getHandle(), ("Kaizen " KAIZEN_VERSION_STR " - " + gameNameDB).c_str());
}
@@ -443,18 +477,16 @@ void KaizenGui::run() {
minimized = false;
break;
default:
break;
}
QueryDevices(e);
HandleInput(e);
}
SDL_GetWindowSize(window.getHandle(), &width, &height);
emuThread.run();
RenderUI();
}
}
void KaizenGui::LoadTAS(const std::string &path) noexcept {
n64::Core::GetInstance().LoadTAS(fs::path(path));
}
void KaizenGui::LoadTAS(const std::string &path) noexcept { n64::Core::GetInstance().LoadTAS(fs::path(path)); }
+1 -1
View File
@@ -17,8 +17,8 @@ public:
bool minimized = false;
SettingsWindow settingsWindow;
RenderWidget vulkanWidget;
EmuThread emuThread;
RenderWidget vulkanWidget;
Debugger debugger;
SDL_Gamepad* gamepad = nullptr;
+2 -2
View File
@@ -4,9 +4,9 @@
#include <SDL3/SDL.h>
#include <imgui_impl_sdl3.h>
RenderWidget::RenderWidget(SDL_Window* window) {
RenderWidget::RenderWidget(SDL_Window* window, u8* rdram) {
wsiPlatform = std::make_shared<SDLWSIPlatform>(window);
windowInfo = std::make_shared<SDLParallelRdpWindowInfo>(window);
n64::Core& core = n64::Core::GetInstance();
core.parallel.Init(wsiPlatform, windowInfo, core.GetMem().GetRDRAMPtr());
core.parallel.Init(wsiPlatform, windowInfo, rdram);
}
+1 -1
View File
@@ -72,7 +72,7 @@ private:
class RenderWidget final {
public:
explicit RenderWidget(SDL_Window*);
explicit RenderWidget(SDL_Window*, u8* rdram);
std::shared_ptr<ParallelRDP::WindowInfo> windowInfo;
std::shared_ptr<SDLWSIPlatform> wsiPlatform;
+2 -1
View File
@@ -1,6 +1,5 @@
#pragma once
#include <filesystem>
#include <fstream>
#include <common.hpp>
#include <mini/ini.h>
#include <log.hpp>
@@ -40,7 +39,9 @@ struct Options {
if (!file.write(structure))
panic("Could not modify options on disk!");
}
private:
mINI::INIFile file;
mINI::INIStructure structure;
};