Skip to content

Commit

Permalink
Further harder rom loading, keep data in memory when actively emulati…
Browse files Browse the repository at this point in the history
…ng 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.
  • Loading branch information
coornio committed Aug 27, 2024
1 parent f99521c commit 860736d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 32 deletions.
14 changes: 5 additions & 9 deletions src/GuestClass/EmuCores/EmuCores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> ifs(HDM.getFilePath(), std::ios::binary);
ifs.read(reinterpret_cast<char*>(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) {
Expand Down
2 changes: 1 addition & 1 deletion src/GuestClass/EmuCores/EmuCores.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 12 additions & 18 deletions src/HostClass/HomeDirManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include <fstream>
#include <iostream>

#include "HomeDirManager.hpp"
#include "../Assistants/BasicLogger.hpp"
Expand All @@ -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() {
Expand All @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions src/HostClass/HomeDirManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <cstdint>
#include <string>
#include <vector>
#include <filesystem>
#include <string_view>

Expand All @@ -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<char> mFileData{};

public:
HomeDirManager(const std::string_view);

Expand All @@ -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; }

Expand Down
2 changes: 1 addition & 1 deletion src/HostClass/HostFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down

0 comments on commit 860736d

Please sign in to comment.