Skip to content

Commit

Permalink
Refactor main callbacks and Host class to mesh together better, perfo…
Browse files Browse the repository at this point in the history
…rmance up to 6.85m and some targets currently achieved!

Also fix line endings on a bunch of files to LF rather than CRLF
  • Loading branch information
coornio committed Aug 26, 2024
1 parent cd718ef commit 483fddc
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 185 deletions.
10 changes: 0 additions & 10 deletions CubeChip (SDL).vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@
<ClCompile Include="src\GuestClass\EmuCores\EmuCores.cpp" />
<ClCompile Include="src\GuestClass\GameFileChecker.cpp" />
<ClCompile Include="src\GuestClass\HexInput.cpp" />
<ClCompile Include="src\GuestClass\to_be_removed\GuestFunctions.cpp" />
<ClCompile Include="src\GuestClass\to_be_removed\Init.cpp" />
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_Classic8.cpp" />
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_Gigachip.cpp" />
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_LegacySC.cpp" />
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_Megachip.cpp" />
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_ModernXO.cpp" />
<ClCompile Include="src\HostClass\BasicAudioSpec.cpp" />
<ClCompile Include="src\HostClass\BasicVideoSpec.cpp" />
<ClCompile Include="src\HostClass\HostFunctions.cpp" />
Expand All @@ -56,9 +49,6 @@
<ClInclude Include="src\GuestClass\EmuCores\EmuCores.hpp" />
<ClInclude Include="src\GuestClass\GameFileChecker.hpp" />
<ClInclude Include="src\GuestClass\HexInput.hpp" />
<ClInclude Include="src\GuestClass\to_be_removed\Enums.hpp" />
<ClInclude Include="src\GuestClass\to_be_removed\Guest.hpp" />
<ClInclude Include="src\GuestClass\to_be_removed\InstructionSets\Interface.hpp" />
<ClInclude Include="src\HostClass\BasicAudioSpec.hpp" />
<ClInclude Include="src\HostClass\BasicVideoSpec.hpp" />
<ClInclude Include="src\HostClass\HomeDirManager.hpp" />
Expand Down
30 changes: 0 additions & 30 deletions CubeChip (SDL).vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,6 @@
<ClCompile Include="src\GuestClass\GameFileChecker.cpp">
<Filter>Source Files\VM Guest</Filter>
</ClCompile>
<ClCompile Include="src\GuestClass\to_be_removed\GuestFunctions.cpp">
<Filter>to_be_removed</Filter>
</ClCompile>
<ClCompile Include="src\GuestClass\to_be_removed\Init.cpp">
<Filter>to_be_removed</Filter>
</ClCompile>
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_Classic8.cpp">
<Filter>to_be_removed\InstructionSets</Filter>
</ClCompile>
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_Gigachip.cpp">
<Filter>to_be_removed\InstructionSets</Filter>
</ClCompile>
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_LegacySC.cpp">
<Filter>to_be_removed\InstructionSets</Filter>
</ClCompile>
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_Megachip.cpp">
<Filter>to_be_removed\InstructionSets</Filter>
</ClCompile>
<ClCompile Include="src\GuestClass\to_be_removed\InstructionSets\_ModernXO.cpp">
<Filter>to_be_removed\InstructionSets</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\HostClass\BasicAudioSpec.hpp">
Expand Down Expand Up @@ -176,14 +155,5 @@
<ClInclude Include="src\GuestClass\GameFileChecker.hpp">
<Filter>Header Files\VM Guest</Filter>
</ClInclude>
<ClInclude Include="src\GuestClass\to_be_removed\Enums.hpp">
<Filter>to_be_removed</Filter>
</ClInclude>
<ClInclude Include="src\GuestClass\to_be_removed\Guest.hpp">
<Filter>to_be_removed</Filter>
</ClInclude>
<ClInclude Include="src\GuestClass\to_be_removed\InstructionSets\Interface.hpp">
<Filter>to_be_removed\InstructionSets</Filter>
</ClInclude>
</ItemGroup>
</Project>
92 changes: 43 additions & 49 deletions src/CubeChip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@

#include "Assistants/FrameLimiter.hpp"
#include <SDL3/SDL.h>
#include <SDL3/SDL_init.h>

#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL_main.h>

#include <optional>
#include <memory>
#include <mutex>

Expand All @@ -22,18 +20,7 @@

#include "HostClass/Host.hpp"

struct {
std::optional<HomeDirManager> HDM;
std::optional<BasicVideoSpec> BVS;
std::optional<BasicAudioSpec> BAS;
FrameLimiter Limiter;
std::unique_ptr<VM_Host> Host;
std::mutex Mut;
} global;

SDL_AppResult SDL_AppInit(void **appstate, int argc, char* argv[]) {

SDL_InitSubSystem(SDL_INIT_EVENTS);
SDL_AppResult SDL_AppInit(void **Host, int argc, char* argv[]) {

// VS OTHER
#if !defined(NDEBUG) || defined(DEBUG)
Expand Down Expand Up @@ -61,51 +48,58 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char* argv[]) {
#endif
SDL_SetHint(SDL_HINT_APP_NAME, "CubeChip");

global.Mut.lock();

try {
global.HDM.emplace("CubeChip_SDL");
global.BVS.emplace();
global.BAS.emplace();
} catch (...) { return SDL_APP_FAILURE; }
*Host = VM_Host::initialize(argc <= 1 ? nullptr : argv[1]);

global.Host = std::make_unique<VM_Host>(
argc <= 1 ? nullptr : argv[1],
*global.HDM, *global.BVS, *global.BAS
);
global.Host->prepareGuest(global.Limiter);

global.Mut.unlock();

return SDL_APP_CONTINUE;
if (Host) { return SDL_APP_CONTINUE; }
else { return SDL_APP_FAILURE; }
}

SDL_AppResult SDL_AppIterate(void* appstate) {
global.Mut.lock();
SDL_AppResult SDL_AppIterate(void* Host_ptr) {
auto& Host{ *static_cast<VM_Host*>(Host_ptr) };

if (!global.Host->runFrame(global.Limiter)) {
global.Mut.unlock();
return SDL_APP_SUCCESS;
}

global.Host->BVS.renderPresent();
Host.Mutex.lock();
switch (Host.runFrame()) {
case SDL_APP_SUCCESS:
Host.Mutex.unlock();
return SDL_APP_SUCCESS;

global.Mut.unlock();
case SDL_APP_CONTINUE:
Host.Mutex.unlock();
return SDL_APP_CONTINUE;

return SDL_APP_CONTINUE;
case SDL_APP_FAILURE:
Host.Mutex.unlock();
return SDL_APP_FAILURE;
}
}

SDL_AppResult SDL_AppEvent(void *appstate, const SDL_Event *event) {
global.Mut.lock();

if (!global.Host->handleEventSDL(global.Limiter, event)) {
global.Mut.unlock();
return SDL_APP_SUCCESS;
SDL_AppResult SDL_AppEvent(void* Host_ptr, const SDL_Event* Event) {
auto& Host{ *static_cast<VM_Host*>(Host_ptr) };

switch (Event->type) {
case SDL_EVENT_QUIT:
return SDL_APP_SUCCESS;

case SDL_EVENT_DROP_FILE:
Host.Mutex.lock();
Host.loadGameFile(Event->drop.data, true);
Host.Mutex.unlock();
break;

case SDL_EVENT_WINDOW_MINIMIZED:
Host.Mutex.lock();
Host.pauseSystem(true);
Host.Mutex.unlock();
break;

case SDL_EVENT_WINDOW_RESTORED:
Host.Mutex.lock();
Host.pauseSystem(false);
Host.Mutex.unlock();
break;
}

global.Mut.unlock();

return SDL_APP_CONTINUE;
}

void SDL_AppQuit(void *appstate) {}
void SDL_AppQuit(void*) {}
1 change: 0 additions & 1 deletion src/HostClass/BasicAudioSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ BasicAudioSpec::BasicAudioSpec()
}

BasicAudioSpec::~BasicAudioSpec() {
if (stream) SDL_DestroyAudioStream(stream);
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}

Expand Down
3 changes: 0 additions & 3 deletions src/HostClass/BasicVideoSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ BasicVideoSpec::BasicVideoSpec()
}

BasicVideoSpec::~BasicVideoSpec() {
quitTexture();
quitRenderer();
quitWindow();
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}

Expand Down
2 changes: 2 additions & 0 deletions src/HostClass/HomeDirManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ bool HomeDirManager::validateGameFile(const char* inputPath) noexcept {
const auto tempStem{ gamePath.stem().string() };
const auto tempExts{ gamePath.extension().string() };
const auto tempSHA1{ SHA1::from_file(tempPath) };
const auto tempTime{ fs::last_write_time(gamePath) };

const bool gameApproved{ checkGame(tempSize, tempExts, tempSHA1) };

Expand All @@ -88,6 +89,7 @@ bool HomeDirManager::validateGameFile(const char* inputPath) noexcept {
mFileStem = tempStem;
mFileExts = tempExts;
mFileSHA1 = tempSHA1;
mFileTime = tempTime;
mFileSize = tempSize;
}

Expand Down
44 changes: 23 additions & 21 deletions src/HostClass/Host.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,44 @@

#pragma once

#include <memory>
#include <mutex>

#include <SDL3/SDL_init.h>

class HomeDirManager;
class BasicVideoSpec;
class BasicAudioSpec;

class FrameLimiter;
class EmuInterface;

union SDL_Event;

class VM_Host final {

std::unique_ptr<EmuInterface>
iGuest;

bool _doBench{};
std::unique_ptr<FrameLimiter> Limiter;
std::unique_ptr<HomeDirManager> HDM;
std::unique_ptr<BasicVideoSpec> BVS;
std::unique_ptr<BasicAudioSpec> BAS;

[[nodiscard]]
bool doBench() const noexcept;
void doBench(bool) noexcept;
bool runBenchmark{};

bool initGameCore();
void replaceGuest(const bool);

VM_Host(const char* const);
VM_Host(const VM_Host&) = delete;
VM_Host& operator=(const VM_Host&) = delete;

public:
HomeDirManager& HDM;
BasicVideoSpec& BVS;
BasicAudioSpec& BAS;

explicit VM_Host(
const char* const,
HomeDirManager&,
BasicVideoSpec&,
BasicAudioSpec&
);
~VM_Host();

void prepareGuest(FrameLimiter&);
bool handleEventSDL(FrameLimiter&, const SDL_Event*);
bool runFrame(FrameLimiter& Limiter);
std::mutex Mutex;

static VM_Host* initialize(const char* const);

void pauseSystem(const bool state) const noexcept;
void loadGameFile(const char* const, const bool = false);

SDL_AppResult runFrame();
};
Loading

0 comments on commit 483fddc

Please sign in to comment.