Skip to content

Commit

Permalink
Code clean up and misc tweaks
Browse files Browse the repository at this point in the history
* Imagine: Fix assert failure calling FSPicker::setEmptyPath() when view rectangle isn't set
* Imagine: Don't modify coordinates in Rect2::fitIn() if the source size is larger
* EmuFramework: Add support for setting content zoom up to 200%
* EmuFramework: Add EmuApp::setupStaticBackupMemoryFile() and EmuSystem::contentNameExt()
* EmuFramework: Clean up and tweak various Mednafen utility functions
* EmuFramework: Move system-specific Mednafen source code into respective project folders
  • Loading branch information
Robert Broglia committed Dec 8, 2023
1 parent cbe3339 commit 285d4ee
Show file tree
Hide file tree
Showing 198 changed files with 176 additions and 137 deletions.
1 change: 1 addition & 0 deletions EmuFramework/include/emuframework/EmuApp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public:
void printScreenshotResult(bool success);
FS::PathString contentSavePath(std::string_view name) const;
FS::PathString contentSaveFilePath(std::string_view ext) const;
void setupStaticBackupMemoryFile(FileIO &, std::string_view ext, size_t staticSize, uint8_t initValue = 0) const;
void readState(std::span<uint8_t> buff);
size_t writeState(std::span<uint8_t> buff, SaveStateFlags = {});
DynArray<uint8_t> saveState();
Expand Down
8 changes: 7 additions & 1 deletion EmuFramework/include/emuframework/EmuSystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ public:
FS::PathString contentFilePath(std::string_view ext) const;
const auto &contentLocation() const { return contentLocation_; }
const char *contentLocationPtr() { return contentLocation_.data(); }
FS::FileString contentNameExt(std::string_view ext) const
{
FS::FileString name{contentName_};
name += ext;
return name;
}
const auto &contentName() const { return contentName_; }
FS::FileString contentFileName() const;
std::string contentDisplayName() const;
Expand Down Expand Up @@ -328,7 +334,7 @@ public:
void onBackupMemoryWritten(BackupMemoryDirtyFlags flags = 0xFF);
bool updateBackupMemoryCounter();
bool usesBackupMemory() const;
FileIO staticBackupMemoryFile(CStringView uri, size_t staticSize, uint8_t initValue = 0) const;
FileIO openStaticBackupMemoryFile(CStringView uri, size_t staticSize, uint8_t initValue = 0) const;
void sessionOptionSet();
void resetSessionOptionsSet() { sessionOptionsSet = false; }
bool sessionOptionsAreSet() const { return sessionOptionsSet; }
Expand Down
11 changes: 10 additions & 1 deletion EmuFramework/src/EmuApp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ constexpr bool imageEffectPixelFormatIsValid(uint8_t val)
constexpr bool optionImageZoomIsValid(uint8_t val)
{
return val == optionImageZoomIntegerOnly || val == optionImageZoomIntegerOnlyY
|| (val >= 10 && val <= 100);
|| (val >= 10 && val <= 200);
}

EmuApp::EmuApp(ApplicationInitParams initParams, ApplicationContext &ctx):
Expand Down Expand Up @@ -1120,6 +1120,15 @@ FS::PathString EmuApp::contentSaveFilePath(std::string_view ext) const
return system().contentSaveFilePath(ext);
}

void EmuApp::setupStaticBackupMemoryFile(FileIO &io, std::string_view ext, size_t size, uint8_t initValue) const
{
if(io)
return;
io = system().openStaticBackupMemoryFile(system().contentSaveFilePath(ext), size, initValue);
if(!io) [[unlikely]]
throw std::runtime_error(std::format("Error opening {}, please verify save path has write access", system().contentNameExt(ext)));
}

void EmuApp::readState(std::span<uint8_t> buff)
{
syncEmulationThread();
Expand Down
8 changes: 4 additions & 4 deletions EmuFramework/src/EmuSystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ FS::PathString EmuSystem::contentSavePath(std::string_view name) const
FS::PathString EmuSystem::contentSaveFilePath(std::string_view ext) const
{
assert(!contentName_.empty());
return FS::uriString(contentSaveDirectory(), FS::FileString{contentName()}.append(ext));
return FS::uriString(contentSaveDirectory(), contentNameExt(ext));
}

void EmuSystem::setUserSaveDirectory(CStringView path)
Expand Down Expand Up @@ -262,7 +262,7 @@ FS::PathString EmuSystem::userPath(std::string_view userDir) const
FS::PathString EmuSystem::userFilePath(std::string_view userDir, std::string_view ext) const
{
assert(!contentName_.empty());
return userPath(userDir, FS::FileString{contentName()}.append(ext));
return userPath(userDir, contentNameExt(ext));
}

void EmuSystem::closeRuntimeSystem(EmuApp &app)
Expand Down Expand Up @@ -477,7 +477,7 @@ FS::PathString EmuSystem::contentDirectory(std::string_view name) const
FS::PathString EmuSystem::contentFilePath(std::string_view ext) const
{
assert(!contentName_.empty());
return contentDirectory(FS::FileString{contentName()}.append(ext));
return contentDirectory(contentNameExt(ext));
}

std::string EmuSystem::contentDisplayName() const
Expand Down Expand Up @@ -564,7 +564,7 @@ bool EmuSystem::updateBackupMemoryCounter()
return false;
}

FileIO EmuSystem::staticBackupMemoryFile(CStringView uri, size_t size, uint8_t initValue) const
FileIO EmuSystem::openStaticBackupMemoryFile(CStringView uri, size_t size, uint8_t initValue) const
{
if(!size) [[unlikely]]
return {};
Expand Down
4 changes: 2 additions & 2 deletions EmuFramework/src/EmuVideoLayer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void EmuVideoLayer::place(IG::WindowRect viewRect, IG::WindowRect displayRect, E
contentRect_.x2 = x * scaleFactor;
contentRect_.y2 = y * scaleFactor;
}
if(zoom <= 100 || zoom == optionImageZoomIntegerOnlyY)
if(zoom <= 200 || zoom == optionImageZoomIntegerOnlyY)
{
auto aR = evalAspectRatio(viewportAspectRatio < 1.f ? portraitAspectRatio : landscapeAspectRatio)
* sys.videoAspectRatioScale();
Expand All @@ -121,7 +121,7 @@ void EmuVideoLayer::place(IG::WindowRect viewRect, IG::WindowRect displayRect, E
}
contentRect_.x2 = size.x;
contentRect_.y2 = size.y;
if(zoom < 100)
if(zoom != 100)
{
auto scaler = zoom / 100.f;
contentRect_.x2 *= scaler;
Expand Down
4 changes: 2 additions & 2 deletions EmuFramework/src/gui/VideoOptionView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ VideoOptionView::VideoOptionView(ViewAttachParams attach, bool customMenu):
{"Custom Value", &defaultFace(),
[this](const Input::Event &e)
{
app().pushAndShowNewCollectValueRangeInputView<int, 10, 100>(attachParams(), e, "Input 10 to 100", "",
app().pushAndShowNewCollectValueRangeInputView<int, 10, 200>(attachParams(), e, "Input 10 to 200", "",
[this](EmuApp &app, auto val)
{
app.setVideoZoom(val);
Expand All @@ -443,7 +443,7 @@ VideoOptionView::VideoOptionView(ViewAttachParams attach, bool customMenu):
{
.onSetDisplayString = [this](auto idx, Gfx::Text &t)
{
if(app().videoZoom() <= 100)
if(app().videoZoom() <= 200)
{
t.resetString(std::format("{}%", app().videoZoom()));
return true;
Expand Down
1 change: 0 additions & 1 deletion EmuFramework/src/shared/mednafen-emuex/ArchiveVFS.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace Mednafen
class ArchiveVFS : public VirtualFS
{
public:
ArchiveVFS() = default;
ArchiveVFS(IG::FS::ArchiveIterator);
Stream* open(const std::string& path, const uint32 mode, const int do_lock = false, const bool throw_on_noent = true, const CanaryType canary = CanaryType::open) override;
bool mkdir(const std::string& path, const bool throw_on_exist = false) override;
Expand Down
47 changes: 47 additions & 0 deletions EmuFramework/src/shared/mednafen-emuex/MDFNUtils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <mednafen/git.h>
#include <mednafen/FileStream.h>
#include <mednafen/MemoryStream.h>
#include <mednafen/cdrom/CDInterface.h>
#include <main/MainSystem.hh>
#include <string_view>

Expand Down Expand Up @@ -67,6 +68,18 @@ inline FS::FileString stateFilenameMDFN(const Mednafen::MDFNGI &gameInfo, int sl
return format<FS::FileString>("{}.{}.nc{}", name, Mednafen::md5_context::asciistr(gameInfo.MD5, 0), saveSlotChar);
}

inline FS::FileString saveExtMDFN(std::string_view ext, bool skipMD5)
{
IG::FileString str{'.'};
if(!skipMD5)
{
str += Mednafen::md5_context::asciistr(Mednafen::MDFNGameInfo->MD5, 0);
str += '.';
}
str += ext;
return str;
}

inline std::string savePathMDFN(const EmuApp &app, int id1, const char *cd1, bool skipMD5)
{
assert(cd1);
Expand Down Expand Up @@ -190,4 +203,38 @@ inline size_t writeStateMDFN(std::span<uint8_t> buff, SaveStateFlags flags)
}
}

inline void writeCDMD5(Mednafen::MDFNGI &mdfnGameInfo, Mednafen::CDInterface &cdInterface)
{
Mednafen::CDUtility::TOC toc;
Mednafen::md5_context layout_md5;

cdInterface.ReadTOC(&toc);

layout_md5.starts();

layout_md5.update_u32_as_lsb(toc.first_track);
layout_md5.update_u32_as_lsb(toc.last_track);
layout_md5.update_u32_as_lsb(toc.tracks[100].lba);

for(uint32 track = toc.first_track; track <= toc.last_track; track++)
{
layout_md5.update_u32_as_lsb(toc.tracks[track].lba);
layout_md5.update_u32_as_lsb(toc.tracks[track].control & 0x4);
}

uint8 LayoutMD5[16];
layout_md5.finish(LayoutMD5);

memcpy(mdfnGameInfo.MD5, LayoutMD5, 16);
}

inline void clearCDInterfaces(std::vector<Mednafen::CDInterface *> &ifaces)
{
if(!ifaces.size())
return;
assert(ifaces.size() == 1);
delete ifaces[0];
ifaces.clear();
}

}
54 changes: 54 additions & 0 deletions EmuFramework/src/shared/mednafen-emuex/MThreading.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,27 @@
#include <mednafen/types.h>
#include <mednafen/MThreading.h>
#include <imagine/util/utility.h>
#include <imagine/thread/Semaphore.hh>
#include <imagine/logger/logger.h>
#include <thread>
#include <mutex>
#include <condition_variable>

namespace Mednafen::MThreading
{

constexpr IG::SystemLogger log{"MDFNThreading"};

struct Thread : public std::thread
{
using thread::thread;
};
struct Mutex : public std::mutex {};
struct Cond : public std::condition_variable {};
struct Sem : public std::counting_semaphore<0x80000>
{
using counting_semaphore<0x80000>::counting_semaphore;
};

Thread* Thread_Create(int (*fn)(void *), void *data, const char* debug_name)
{
Expand Down Expand Up @@ -100,4 +108,50 @@ bool Cond_Wait(Cond* cond, Mutex* mutex) noexcept
return true;
}


Sem* Sem_Create(void)
{
return new Sem{0};
}

void Sem_Destroy(Sem* sem) noexcept
{
delete sem;
}

bool Sem_Wait(Sem* sem) noexcept
{
assumeExpr(sem);
sem->acquire();
return true;
}

bool Sem_TimedWait(Sem* sem, unsigned ms) noexcept
{
assumeExpr(sem);
bool acquired = sem->try_acquire_for(std::chrono::milliseconds{ms});
if(!acquired)
{
//log.debug("Semaphore:{} waited on longer than {}ms", (void*)sem, ms);
}
return acquired;
}

bool Sem_Post(Sem* sem) noexcept
{
assumeExpr(sem);
sem->release();
return true;
}

}

namespace Mednafen::Time
{

void SleepMS(uint32) noexcept
{
sched_yield();
}

}
4 changes: 2 additions & 2 deletions EmuFramework/src/shared/mednafen-emuex/StreamImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ static std::pair<IG::OpenFlags, uint8_t> modeToAttribs(uint32 mode)
FileStream::FileStream(const std::string& path, const uint32 mode, const int do_lock, const uint32 buffer_size)
try:
io{EmuEx::gAppContext().openFileUri(path, IG::IOAccessHint::Sequential, modeToAttribs(mode).first)},
attribs{modeToAttribs(mode).second}
attribs{modeToAttribs(mode).second}
{
assert(!do_lock);
assert(!do_lock);
}
catch(std::system_error &err)
{
Expand Down
5 changes: 1 addition & 4 deletions GBA.emu/src/main/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ void GbaSystem::loadBackupMemory(EmuApp &app)
{
if(coreOptions.saveType == GBA_SAVE_NONE)
return;
if(!saveFileIO)
saveFileIO = staticBackupMemoryFile(app.contentSaveFilePath(".sav"), saveMemorySize(), 0xFF);
if(!saveFileIO)
throw std::runtime_error("Error accessing .sav file, please verify it has write access");
app.setupStaticBackupMemoryFile(saveFileIO, ".sav", saveMemorySize(), 0xFF);
auto buff = saveFileIO.buffer(IOBufferMode::Release);
if(buff.isMappedFile())
saveFileIO = {};
Expand Down
10 changes: 2 additions & 8 deletions GBC.emu/src/main/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,14 @@ void GbcSystem::loadBackupMemory(EmuApp &app)
sram.size())
{
logMsg("loading sram");
if(!saveFileIO)
saveFileIO = staticBackupMemoryFile(app.contentSaveFilePath(".sav"), sram.size(), 0xFF);
if(!saveFileIO)
throw std::runtime_error("Error accessing .sav file, please verify it has write access");
app.setupStaticBackupMemoryFile(saveFileIO, ".sav", sram.size(), 0xFF);
saveFileIO.read(sram, 0);
}
if(auto timeOpt = gbEmu.rtcTime();
timeOpt)
{
logMsg("loading rtc");
if(!rtcFileIO)
rtcFileIO = staticBackupMemoryFile(app.contentSaveFilePath(".rtc"), 4);
if(!rtcFileIO)
throw std::runtime_error("Error accessing .rtc file, please verify it has write access");
app.setupStaticBackupMemoryFile(rtcFileIO, ".rtc", 4);
auto rtcData = rtcFileIO.get<std::array<uint8_t, 4>>(0);
gbEmu.setRtcTime(rtcData[0] << 24 | rtcData[1] << 16 | rtcData[2] << 8 | rtcData[3]);
}
Expand Down
7 changes: 2 additions & 5 deletions Lynx.emu/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ SRC += main/Main.cc \
main/options.cc \
main/input.cc \
main/EmuMenuViews.cc \
$(MDFN_COMMON_SRC)

MDFN_SRC := lynx/c65c02.cpp \
$(MDFN_COMMON_SRC) \
lynx/c65c02.cpp \
lynx/cart.cpp \
lynx/lynxdec.cpp \
lynx/memmap.cpp \
Expand All @@ -27,8 +26,6 @@ lynx/rom.cpp \
lynx/susie.cpp \
lynx/system.cpp

SRC += $(addprefix mednafen/,$(MDFN_SRC))

include $(EMUFRAMEWORK_PATH)/package/emuframework.mk
include $(IMAGINE_PATH)/make/package/zlib.mk

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 2 additions & 16 deletions NEO.emu/src/main/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,25 +228,11 @@ size_t NeoSystem::writeState(std::span<uint8_t> buff, SaveStateFlags flags)
}
}

static auto nvramPath(EmuApp &app)
{
return app.contentSaveFilePath(".nv");
}

static auto memcardPath(EmuApp &app)
{
return app.contentSaveFilePath(".memcard");
}

void NeoSystem::loadBackupMemory(EmuApp &app)
{
logMsg("loading nvram & memcard");
if(!nvramFileIO)
nvramFileIO = staticBackupMemoryFile(nvramPath(app), 0x10000);
if(!memcardFileIO)
memcardFileIO = staticBackupMemoryFile(memcardPath(app), 0x800);
if(!nvramFileIO || !memcardFileIO)
throw std::runtime_error("Error accessing .nv or .memcard file, please verify it has write access");
app.setupStaticBackupMemoryFile(nvramFileIO, ".nv", 0x10000);
app.setupStaticBackupMemoryFile(memcardFileIO, ".memcard", 0x800);
nvramFileIO.read(memory.sram, 0x10000, 0);
memcardFileIO.read(memory.memcard, 0x800, 0);
}
Expand Down
Loading

0 comments on commit 285d4ee

Please sign in to comment.