Skip to content

Commit

Permalink
Add Windows-only support for a shared memory mapping on Kernel & User…
Browse files Browse the repository at this point in the history
… Memory
  • Loading branch information
Ken Murdock committed Nov 23, 2023
1 parent 54c9d5d commit e0ee5f4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/core/psxemulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class Emulator {
typedef Setting<bool, TYPESTRING("Mcd2Inserted"), true> SettingMcd2Inserted;
typedef Setting<bool, TYPESTRING("Dynarec"), true> SettingDynarec;
typedef Setting<bool, TYPESTRING("8Megs"), false> Setting8MB;
typedef Setting<bool, TYPESTRING("SharedMemoryMap"), false> SettingSharedMemoryMap;
typedef Setting<int, TYPESTRING("GUITheme"), 0> SettingGUITheme;
typedef Setting<int, TYPESTRING("Dither"), 1> SettingDither;
typedef Setting<bool, TYPESTRING("UseCachedDithering"), false> SettingCachedDithering;
Expand All @@ -188,8 +189,8 @@ class Emulator {

Settings<SettingMcd1, SettingMcd2, SettingBios, SettingPpfDir, SettingPsxExe,
SettingXa, SettingSpuIrq, SettingBnWMdec, SettingScaler, SettingAutoVideo, SettingVideo, SettingFastBoot,
SettingDebugSettings, SettingRCntFix, SettingIsoPath, SettingLocale, SettingMcd1Inserted,
SettingMcd2Inserted, SettingDynarec, Setting8MB, SettingGUITheme, SettingDither, SettingCachedDithering,
SettingDebugSettings, SettingRCntFix, SettingIsoPath, SettingLocale, SettingMcd1Inserted, SettingMcd2Inserted,
SettingDynarec, Setting8MB, SettingSharedMemoryMap,SettingGUITheme, SettingDither, SettingCachedDithering,
SettingGLErrorReporting, SettingGLErrorReportingSeverity, SettingFullCaching, SettingHardwareRenderer,
SettingShownAutoUpdateConfig, SettingAutoUpdate, SettingMSAA, SettingLinearFiltering, SettingKioskMode,
SettingMcd1Pocketstation, SettingMcd2Pocketstation, SettingBiosBrowsePath, SettingEXP1Filepath,
Expand Down
53 changes: 53 additions & 0 deletions src/core/psxmem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,46 @@ int PCSX::Memory::init() {
m_readLUT = (uint8_t **)calloc(0x10000, sizeof(void *));
m_writeLUT = (uint8_t **)calloc(0x10000, sizeof(void *));

#ifdef _WIN32
size_t wramSize = 0x00800000;
bool doFallbackAlloc = true;

// Try to create a shared memory mapping, if enabled
const bool memoryMappingEnabled = PCSX::g_emulator->settings.get<PCSX::Emulator::SettingSharedMemoryMap>();
if (memoryMappingEnabled) {
// Create the name of our memory mapping handle which we can search for externally
auto handleName = fmt::format("pcsx-redux-{}", GetCurrentProcessId());
// Create the memory mapping handle
m_wramFileHandle = CreateFileMappingA(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0,
static_cast<uint32_t>(wramSize), handleName.c_str());
if (m_wramFileHandle != INVALID_HANDLE_VALUE) {
// Create a view of the memory mapping
void *base_pointer = MapViewOfFileEx(m_wramFileHandle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0,
static_cast<uint32_t>(wramSize), nullptr);
// Validate success and assign the view to m_wram
if (base_pointer != nullptr) {
doFallbackAlloc = false;
m_wram = (uint8_t *)base_pointer;
} else {
CloseHandle(m_wramFileHandle);
m_wramFileHandle = nullptr;
g_system->message("MapViewOfFileEx failed, falling back to memory alloc, last error: %d\n",
(int)GetLastError());
}
} else {
m_wramFileHandle = nullptr;
g_system->message("CreateFileMappingA failed, falling back to memory alloc, last error: %d\n",
(int)GetLastError());
}
}

if (doFallbackAlloc) {
m_wram = (uint8_t *)calloc(wramSize, 1);
}
#else
m_wram = (uint8_t *)calloc(0x00800000, 1);
#endif

Check notice on line 131 in src/core/psxmem.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Complex Method

PCSX::Memory::init increases in cyclomatic complexity from 9 to 13, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
m_exp1 = (uint8_t *)calloc(0x00800000, 1);
m_hard = (uint8_t *)calloc(0x00010000, 1);
m_bios = (uint8_t *)calloc(0x00080000, 1);
Expand Down Expand Up @@ -239,7 +278,21 @@ The distributed OpenBIOS.bin file can be an appropriate BIOS replacement.
}

void PCSX::Memory::shutdown() {
#ifdef _WIN32
if (m_wramFileHandle != nullptr) {
bool result = static_cast<bool>(UnmapViewOfFile(m_wram));
if (!result) {
g_system->printf("Failed to unmap view of Kernel & User Memory.\n");
}
m_wram = nullptr;
CloseHandle(m_wramFileHandle);
m_wramFileHandle = nullptr;
} else {
free(m_wram);
}
#else
free(m_wram);
#endif
free(m_exp1);
free(m_hard);
free(m_bios);
Expand Down
5 changes: 5 additions & 0 deletions src/core/psxmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ class Memory {
friend class MemoryAsFile;
IO<MemoryAsFile> m_memoryAsFile;

#ifdef _WIN32
// Used to wrap wram in a shared memory map
void *m_wramFileHandle = nullptr;
#endif

int m_writeok = 1;
uint32_t m_biosCRC = 0;

Expand Down
6 changes: 6 additions & 0 deletions src/gui/gui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,12 @@ this setting may not have any effect for you.)"));
ImGuiHelpers::ShowHelpMarker(_(R"(Emulates an installed 8MB system,
instead of the normal 2MB. Useful for working
with development binaries and games.)"));
changed |= ImGui::Checkbox(_("Shared Memory Map"), &settings.get<Emulator::SettingSharedMemoryMap>().value);
ImGuiHelpers::ShowHelpMarker(_(R"(Wraps kernel & user memory in a named memory mapping,

Check warning on line 1904 in src/gui/gui.cc

View check run for this annotation

Codecov / codecov/patch

src/gui/gui.cc#L1903-L1904

Added lines #L1903 - L1904 were not covered by tests
allowing it to be modified from external processes.
Available on Windows platforms only, and requires a restart of the emulator to take effect.
The mapping name is 'pcsx-redux-[process ID]', e.g. 'pcsx-redux-36722'.
See Win32 app documentation for 'Creating Named Shared Memory' for more information.)"));

Check notice on line 1908 in src/gui/gui.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Complex Method

PCSX::GUI::configure already has high cyclomatic complexity, and now it increases in Lines of Code from 260 to 266. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
changed |=
ImGui::Checkbox(_("OpenGL GPU *ALPHA STATE*"), &settings.get<Emulator::SettingHardwareRenderer>().value);
ImGuiHelpers::ShowHelpMarker(_(R"(Enables the OpenGL GPU renderer.
Expand Down
7 changes: 7 additions & 0 deletions src/main/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ int pcsxMain(int argc, char **argv) {
emuSettings.get<PCSX::Emulator::Setting8MB>() = false;
}

if (args.get<bool>("sharedmemorymap")) {
emuSettings.get<PCSX::Emulator::SettingSharedMemoryMap>() = true;

Check warning on line 270 in src/main/main.cc

View check run for this annotation

Codecov / codecov/patch

src/main/main.cc#L270

Added line #L270 was not covered by tests
}
if (args.get<bool>("no-sharedmemorymap")) {
emuSettings.get<PCSX::Emulator::SettingSharedMemoryMap>() = false;

Check warning on line 273 in src/main/main.cc

View check run for this annotation

Codecov / codecov/patch

src/main/main.cc#L273

Added line #L273 was not covered by tests
}

Check warning on line 275 in src/main/main.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

pcsxMain increases in cyclomatic complexity from 50 to 52, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
if (args.get<bool>("fastboot")) {
emuSettings.get<PCSX::Emulator::SettingFastBoot>() = true;
}
Expand Down

0 comments on commit e0ee5f4

Please sign in to comment.