From 860736d971414cd200201b33dfc01ba10f97a46e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A3=CF=84=CE=AD=CF=86=CE=B1=CE=BD=CE=BF=CF=82=20=22Coor?= =?UTF-8?q?nio/8924th=22=20=CE=92=CE=BB=CE=B1=CF=83=CF=84=CF=8C=CF=82?= <8924th@gmail.com> Date: Tue, 27 Aug 2024 03:32:12 +0300 Subject: [PATCH] Further harder rom loading, keep data in memory when actively emulating and reload through it if needed. Data is deallocated on Esc, and the SHA1 is now calculated via the cached data instead of parsing the file a second time. --- src/GuestClass/EmuCores/EmuCores.cpp | 14 +++++-------- src/GuestClass/EmuCores/EmuCores.hpp | 2 +- src/HostClass/HomeDirManager.cpp | 30 +++++++++++----------------- src/HostClass/HomeDirManager.hpp | 8 +++++--- src/HostClass/HostFunctions.cpp | 2 +- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/GuestClass/EmuCores/EmuCores.cpp b/src/GuestClass/EmuCores/EmuCores.cpp index 01767cd..69f0b69 100644 --- a/src/GuestClass/EmuCores/EmuCores.cpp +++ b/src/GuestClass/EmuCores/EmuCores.cpp @@ -56,15 +56,11 @@ void Chip8_CoreInterface::instructionErrorML(const u32 HI, const u32 LO) { setInterrupt(Interrupt::ERROR); } -bool Chip8_CoreInterface::copyGameToMemory(u8* dest, const u32 offset) { - std::basic_ifstream ifs(HDM.getFilePath(), std::ios::binary); - ifs.read(reinterpret_cast(dest + offset), HDM.getFileSize()); - if (ifs.fail()) { - blog.newEntry(BLOG::ERROR, "Failed to copy rom data to memory, aborting."); - return false; - } else { - return true; - } +void Chip8_CoreInterface::copyGameToMemory(u8* dest, const u32 offset) { + std::copy_n( + std::execution::unseq, + HDM.getFileData(), HDM.getFileSize(), dest + offset + ); } void Chip8_CoreInterface::copyFontToMemory(u8* dest, const u32 offset, const u32 size) { diff --git a/src/GuestClass/EmuCores/EmuCores.hpp b/src/GuestClass/EmuCores/EmuCores.hpp index 10ddcc4..7a6c4fc 100644 --- a/src/GuestClass/EmuCores/EmuCores.hpp +++ b/src/GuestClass/EmuCores/EmuCores.hpp @@ -120,7 +120,7 @@ class Chip8_CoreInterface : public EmuInterface { void instructionError(const u32 HI, const u32 LO); void instructionErrorML(const u32 HI, const u32 LO); - bool copyGameToMemory(u8* dest, const u32 offset); + void copyGameToMemory(u8* dest, const u32 offset); void copyFontToMemory(u8* dest, const u32 offset, const u32 size); public: diff --git a/src/HostClass/HomeDirManager.cpp b/src/HostClass/HomeDirManager.cpp index b6b7db9..4a1110d 100644 --- a/src/HostClass/HomeDirManager.cpp +++ b/src/HostClass/HomeDirManager.cpp @@ -5,6 +5,7 @@ */ #include +#include #include "HomeDirManager.hpp" #include "../Assistants/BasicLogger.hpp" @@ -29,14 +30,10 @@ HomeDirManager::HomeDirManager(const std::string_view homeDirName) try } void HomeDirManager::clearCachedFileData() noexcept { - //mFilePath.clear(); - //mFileName.clear(); - //mFileStem.clear(); - //mFileExts.clear(); mFilePath.clear(); mFileSHA1.clear(); + mFileData.resize(0); mFileTime = {}; - mFileSize = {}; } void HomeDirManager::addDirectory() { @@ -63,32 +60,29 @@ bool HomeDirManager::validateGameFile(const FilePath gamePath) noexcept { blog.newEntry(BLOG::WARN, "Provided path is not to a file!"); return false; } - - const auto tempSize{ fs::file_size(gamePath, error) }; - if (error) { - blog.newEntry(BLOG::ERROR, "Unable to read file!"); - return false; - } - if (tempSize == 0) { - blog.newEntry(BLOG::WARN, "File must not be empty!"); - return false; - } const auto tempTime{ HDM::getFileTime(gamePath) }; - const auto tempSHA1{ SHA1::from_file(gamePath.string()) }; + + std::ifstream ifs(gamePath, std::ios::binary); + mFileData.assign(std::istreambuf_iterator(ifs), {}); if (tempTime != HDM::getFileTime(gamePath)) { blog.newEntry(BLOG::WARN, "File was modified while reading!"); return false; } - const bool gameApproved{ checkGame(tempSize, gamePath.extension().string(), tempSHA1) }; + if (!getFileSize()) { + blog.newEntry(BLOG::WARN, "File must not be empty!"); + return false; + } + + const auto tempSHA1{ SHA1::from_span(mFileData) }; + const bool gameApproved{ checkGame(getFileSize(), gamePath.extension().string(), tempSHA1)}; if (gameApproved) { mFilePath = gamePath; mFileSHA1 = tempSHA1; mFileTime = tempTime; - mFileSize = tempSize; } if (gameApproved) { diff --git a/src/HostClass/HomeDirManager.hpp b/src/HostClass/HomeDirManager.hpp index 6cbcdd7..f2027d2 100644 --- a/src/HostClass/HomeDirManager.hpp +++ b/src/HostClass/HomeDirManager.hpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -17,15 +18,15 @@ class HomeDirManager final : public BasicHome { using GameValidator = bool (*)(std::uint64_t, std::string_view, std::string_view); using FileModTime = std::filesystem::file_time_type; using FilePath = std::filesystem::path; - using FileSize = std::uintmax_t; FilePath mFilePath{}; std::string mFileSHA1{}; FileModTime mFileTime{}; - FileSize mFileSize{}; GameValidator checkGame{}; + std::vector mFileData{}; + public: HomeDirManager(const std::string_view); @@ -37,9 +38,10 @@ class HomeDirManager final : public BasicHome { auto getFileName() const noexcept { return mFilePath.filename().string(); } auto getFileStem() const noexcept { return mFilePath.stem().string(); } auto getFileExts() const noexcept { return mFilePath.extension().string(); } + auto getFileSize() const noexcept { return mFileData.size(); } + auto getFileData() const noexcept { return mFileData.data(); } auto getFileSHA1() const noexcept { return mFileSHA1; } auto getFileTime() const noexcept { return mFileTime; } - auto getFileSize() const noexcept { return mFileSize; } void setValidator(GameValidator func) noexcept { checkGame = func; } diff --git a/src/HostClass/HostFunctions.cpp b/src/HostClass/HostFunctions.cpp index 2a716da..ffd8405 100644 --- a/src/HostClass/HostFunctions.cpp +++ b/src/HostClass/HostFunctions.cpp @@ -68,7 +68,7 @@ SDL_AppResult VM_Host::runFrame() { return SDL_APP_CONTINUE; } if (kb.isPressed(KEY(BACKSPACE))) { - loadGameFile(HDM->getFullPath()); + replaceGuest(false); return SDL_APP_CONTINUE; } if (kb.isPressed(KEY(RSHIFT))) {