PlayAgain's changes to PPSSPP 3039c4ea96e3430cf4dcad9d34f00e298ac9ace1 (and the submodules it pins) for a single-threaded Emscripten 6.0.12 build of the libretro core. Applied by build.sh to a plain tree (git archive of the commit and of each submodule it needs) with patch -p1. Building for wasm at all (upstream's platform=emscripten no longer compiles): - Common/CommonFuncs.h: no and no __rold/__rord under Emscripten, which PPSSPP calls x86 (ppsspp_config.h) but which has only the SSE headers, not clang's native ones. - Core/MIPS/MIPS.cpp: no MXCSR under Emscripten (there is no _mm_setcsr); WebAssembly always rounds to nearest, so the FPU rounding mode goes through fesetround, as on RISC-V. - ext/xxhash.h: with -msse* set, the SSE2 path (Emscripten's SSE headers) instead of the NEON one (Emscripten's arm_neon.h, which then pulls in clang's x86 and fails). - ext/aemu_postoffice (client/sock_impl_linux.c, server_cpp/native_socket_linux.cpp): no SO_NOSIGPIPE under Emscripten (it is not __linux__, but it has no such option either). - libretro/Makefile.common: no getauxval (cpu_features' hwcaps.c is left out) for Emscripten. - Common/MemArenaPosix.cpp: with NO_MMAP the PSP's address space is one malloc; it was sized for 32 MB of RAM and a homebrew ELF on a PSP-2000 gets 64 MB, written past the end of the block. It is now the whole 256 MB a masked address can reach (MEMVIEW32_MASK), as the mmap path reserves, so a stray guest access under fast memory stays inside it. VRAM's mirrors are not aliased without mmap (a NO_MMAP view is plain memory at its address). One thread (PPSSPP_NO_THREADS, defined in ppsspp_config.h for Emscripten without -pthread): - Common/Thread/ThreadManager.cpp: no worker threads; a task runs when it is enqueued. - Common/TimeUtil.cpp: sleeping does nothing (emscripten_sleep needs ASYNCIFY; the host paces). - Core/System.cpp: the boot's loader runs in PSP_InitStart instead of on a loader thread. - Core/HLE/sceIo.cpp: no IO thread; the async IO manager runs each operation as it is scheduled. - Core/Dialog/PSPSaveDialog.cpp: the savedata dialog's IO runs in place. - Core/Debugger/MemBlockInfo.cpp: no flush thread; pending notifies flush in place. The libretro front-end on wasm: - libretro/libretro.cpp: the IR interpreter instead of the plain interpreter, and no JIT (SYSPROP_CAN_JIT false: the vertex decoder JIT would write x86 code). --- a/Common/CommonFuncs.h +++ b/Common/CommonFuncs.h @@ -71,7 +71,7 @@ #if defined(_MSC_VER) #include -#elif (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) +#elif (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) && !defined(__EMSCRIPTEN__) #include #endif @@ -90,7 +90,7 @@ inline u32 __rotl(u32 x, int shift) { #if defined(_MSC_VER) return _rotl(x, shift); -#elif (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) +#elif (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) && !defined(__EMSCRIPTEN__) return __rold(x, shift); #else shift &= 31; @@ -113,7 +113,7 @@ inline u32 __rotr(u32 x, int shift) { #if defined(_MSC_VER) return _rotr(x, shift); -#elif (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) +#elif (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) && !defined(__EMSCRIPTEN__) return __rord(x, shift); #else shift &= 31; --- a/Common/MemArenaPosix.cpp +++ b/Common/MemArenaPosix.cpp @@ -174,7 +174,10 @@ return hardcoded_ptr; } #elif defined(NO_MMAP) - void* base = std::malloc(0x0A000000); + // PlayAgain: all of MEMVIEW32_MASK (0x0FFFFFFF on a 32-bit build), as the mmap path reserves: + // 0x0A000000 only covered 32 MB of RAM, and with fast memory a stray guest address is masked, + // not checked, so anything short of the mask lets it land in the heap after this block. + void* base = std::malloc(0x10000000); return static_cast(base); #else // Keep the range reserved and map the views over it, so nothing else can be allocated in the gaps --- a/Common/Thread/ThreadManager.cpp +++ b/Common/Thread/ThreadManager.cpp @@ -206,6 +206,13 @@ if (IsInitialized()) { Teardown(); } + +#ifdef PPSSPP_NO_THREADS + // No threads to start: one "looper", and every task runs when it's enqueued. + numComputeThreads_ = 1; + numThreads_ = 1; + return; +#endif numComputeThreads_ = numRealCores * numLogicalCoresPerCpu; // Double it for the IO blocking threads. @@ -225,6 +232,11 @@ } void ThreadManager::EnqueueTask(Task *task) { +#ifdef PPSSPP_NO_THREADS + task->Run(); + task->Release(); + return; +#endif if (task->Type() == TaskType::DEDICATED_THREAD) { std::thread th([=](Task *task) { SetCurrentThreadName("DedicatedThreadTask"); @@ -292,6 +304,11 @@ void ThreadManager::EnqueueTaskOnThread(int threadNum, Task *task) { _assert_msg_(task->Type() != TaskType::DEDICATED_THREAD, "Dedicated thread tasks can't be put on specific threads"); +#ifdef PPSSPP_NO_THREADS + task->Run(); + task->Release(); + return; +#endif _assert_msg_(threadNum >= 0 && threadNum < (int)global_->threads_.size(), "Bad threadnum %d(/%d) or not initialized", threadNum, (int)global_->threads_.size()); TaskThreadContext *thread = global_->threads_[threadNum]; @@ -309,5 +326,8 @@ } bool ThreadManager::IsInitialized() const { +#ifdef PPSSPP_NO_THREADS + return numThreads_ != 0; +#endif return !global_->threads_.empty(); } --- a/Common/TimeUtil.cpp +++ b/Common/TimeUtil.cpp @@ -242,6 +242,8 @@ Sleep(ms); #elif defined(HAVE_LIBNX) svcSleepThread(ms * 1000000); +#elif defined(PPSSPP_NO_THREADS) + // Nothing to wait for on the only thread; the host paces frames. #elif defined(__EMSCRIPTEN__) emscripten_sleep(ms); #else @@ -260,6 +262,8 @@ Sleep(us / 1000); #elif defined(HAVE_LIBNX) svcSleepThread(us * 1000); +#elif defined(PPSSPP_NO_THREADS) + // Nothing to wait for on the only thread; the host paces frames. #elif defined(__EMSCRIPTEN__) emscripten_sleep(us / 1000); #else @@ -317,6 +321,8 @@ // On other platforms, we just do a conversion with more input precision than in sleep_ms which is restricted to whole milliseconds. #elif defined(HAVE_LIBNX) svcSleepThread((int64_t)(seconds * 1000000000.0)); +#elif defined(PPSSPP_NO_THREADS) + // Nothing to wait for on the only thread; the host paces frames. #elif defined(__EMSCRIPTEN__) emscripten_sleep(seconds * 1000.0); #else --- a/Core/Debugger/MemBlockInfo.cpp +++ b/Core/Debugger/MemBlockInfo.cpp @@ -564,11 +564,15 @@ } if (needFlush) { +#ifdef PPSSPP_NO_THREADS + FlushPendingMemInfo(); +#else { std::lock_guard guard(flushLock); flushThreadPending = true; } flushCond.notify_one(); +#endif } if (!(flags & MemBlockFlags::SKIP_MEMCHECK)) { @@ -621,11 +625,15 @@ } if (needsFlush) { +#ifdef PPSSPP_NO_THREADS + FlushPendingMemInfo(); +#else { std::lock_guard guard(flushLock); flushThreadPending = true; } flushCond.notify_one(); +#endif } } @@ -767,9 +775,15 @@ pendingNotifyMinAddr2 = 0xFFFFFFFF; pendingNotifyMaxAddr2 = 0; +#ifdef PPSSPP_NO_THREADS + // No flush thread: the notifies flush in place when enough are pending. + flushThreadRunning = false; + flushThreadPending = false; +#else flushThreadRunning = true; flushThreadPending = false; flushThread = std::thread(&FlushMemInfoThread); +#endif } void MemBlockInfoShutdown() { @@ -789,7 +803,8 @@ flushThreadPending = true; } flushCond.notify_one(); - flushThread.join(); + if (flushThread.joinable()) + flushThread.join(); } void MemBlockInfoDoState(PointerWrap &p) { --- a/Core/Dialog/PSPSaveDialog.cpp +++ b/Core/Dialog/PSPSaveDialog.cpp @@ -1259,12 +1259,17 @@ } ioThreadStatus = SAVEIO_PENDING; +#ifdef PPSSPP_NO_THREADS + // No thread: the IO is done before the dialog looks for it. + ExecuteIOAction(); +#else ioThread = std::thread([this]() { SetCurrentThreadName("SaveIO"); AndroidJNIThreadContext jniContext; this->ExecuteIOAction(); }); +#endif } // Takes the IO thread's results back. Without wait, only if it's done: returns whether it was. --- a/Core/HLE/sceIo.cpp +++ b/Core/HLE/sceIo.cpp @@ -673,10 +673,16 @@ memset(fds, 0, sizeof(fds)); g_fatmsOemValue = 0; +#ifdef PPSSPP_NO_THREADS + // No IO thread: the manager runs each operation as it's scheduled. + ioManagerThreadEnabled = false; + ioManager.SetThreadEnabled(false); +#else ioManagerThreadEnabled = true; ioManager.SetThreadEnabled(true); Core_ListenLifecycle(&__IoWakeManager); ioManagerThread = std::thread(&__IoManagerThread); +#endif __KernelRegisterWaitTypeFuncs(WAITTYPE_ASYNCIO, __IoAsyncBeginCallback, __IoAsyncEndCallback); @@ -698,6 +704,9 @@ ioManagerThread.join(); ioManager.Shutdown(); } +#ifdef PPSSPP_NO_THREADS + ioManager.Shutdown(); +#endif for (int i = 0; i < PSP_COUNT_FDS; ++i) { asyncParams[i].op = IoAsyncOp::NONE; --- a/Core/MIPS/MIPS.cpp +++ b/Core/MIPS/MIPS.cpp @@ -96,7 +96,7 @@ // to read the register. Our bits are cleared in it, in case a JIT's asm stub had already set them. // Only the CPU thread calls these. static bool g_hostModeSaved = false; -#if PPSSPP_ARCH(SSE2) +#if PPSSPP_ARCH(SSE2) && !defined(__EMSCRIPTEN__) static u32 g_savedHostMode; #elif PPSSPP_ARCH(ARM64) static u64 g_savedHostMode; @@ -113,7 +113,7 @@ } int rmode = fcr1Bits & 3; bool ftz = (fcr1Bits & 0x01000000) != 0; -#if PPSSPP_ARCH(SSE2) +#if PPSSPP_ARCH(SSE2) && !defined(__EMSCRIPTEN__) if (!g_hostModeSaved) { g_savedHostMode = _mm_getcsr() & ~0xE040; g_hostModeSaved = true; @@ -163,7 +163,7 @@ void RestoreHostRoundingMode(bool force) { if (!g_hostModeSaved) { if (force) { -#if PPSSPP_ARCH(SSE2) +#if PPSSPP_ARCH(SSE2) && !defined(__EMSCRIPTEN__) _mm_setcsr(_mm_getcsr() & ~0xE040); #elif PPSSPP_ARCH(ARM64) ARM64WriteFPCR(ARM64ReadFPCR() & ~(7 << 22)); @@ -176,7 +176,7 @@ return; } g_hostModeSaved = false; -#if PPSSPP_ARCH(SSE2) +#if PPSSPP_ARCH(SSE2) && !defined(__EMSCRIPTEN__) _mm_setcsr(g_savedHostMode); #elif PPSSPP_ARCH(ARM64) ARM64WriteFPCR(g_savedHostMode); --- a/Core/System.cpp +++ b/Core/System.cpp @@ -101,6 +101,9 @@ // For background loading thread. static std::mutex loadingLock; static std::thread g_loadingThread; +#ifdef PPSSPP_NO_THREADS +static bool g_loaderRanInPlace = false; +#endif bool g_coreCollectDebugStats = false; static int g_coreCollectDebugStatsCounter = 0; @@ -849,7 +852,7 @@ Core_NotifyLifecycle(CoreLifecycle::STARTING); - g_loadingThread = std::thread([errorString]() { + auto loader = [errorString]() { SetCurrentThreadName("ExecLoader"); AndroidJNIThreadContext jniContext; @@ -925,7 +928,15 @@ // The GPU is created by PSP_InitUpdate, on the thread that owns the graphics device. g_loaderDone = true; - }); + }; + +#ifdef PPSSPP_NO_THREADS + // No thread to load on: load now, and let PSP_InitUpdate finish the boot. + loader(); + g_loaderRanInPlace = true; +#else + g_loadingThread = std::thread(loader); +#endif return true; } @@ -936,11 +947,16 @@ return g_bootState; } +#ifdef PPSSPP_NO_THREADS + if (g_loaderRanInPlace) { + g_loaderRanInPlace = false; +#else if (g_loadingThread.joinable()) { if (!g_loaderDone) { return BootState::Booting; } g_loadingThread.join(); +#endif // The loader thread has already shut the CPU down if it failed, and so has InitGPU. if (g_loaderFailed || !InitGPU(&g_CoreParameter.errorString)) { @@ -1024,9 +1040,15 @@ _assert_(g_bootState != BootState::Failed); +#ifdef PPSSPP_NO_THREADS + if (g_loaderRanInPlace) { + // Exiting during boot, before PSP_InitUpdate got to create the GPU. + g_loaderRanInPlace = false; +#else if (g_loadingThread.joinable()) { // Exiting during boot, before PSP_InitUpdate got to create the GPU. g_loadingThread.join(); +#endif if (g_loaderFailed) { Core_NotifyLifecycle(CoreLifecycle::START_COMPLETE); g_bootState = BootState::Off; --- a/ext/aemu_postoffice/client/sock_impl_linux.c +++ b/ext/aemu_postoffice/client/sock_impl_linux.c @@ -155,7 +155,7 @@ setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sockopt, sizeof(sockopt)); setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); - #ifndef __linux__ + #if !defined(__linux__) && !defined(__EMSCRIPTEN__) sockopt = 1; setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &sockopt, sizeof(sockopt)); #endif --- a/ext/aemu_postoffice/server_cpp/native_socket_linux.cpp +++ b/ext/aemu_postoffice/server_cpp/native_socket_linux.cpp @@ -163,7 +163,7 @@ setsockopt(accept_result, SOL_SOCKET, SO_SNDBUF, &sockopt, sizeof(sockopt)); setsockopt(accept_result, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); - #ifndef __linux__ + #if !defined(__linux__) && !defined(__EMSCRIPTEN__) sockopt = 1; setsockopt(accept_result, SOL_SOCKET, SO_NOSIGPIPE, &sockopt, sizeof(sockopt)); #endif --- a/ext/xxhash.h +++ b/ext/xxhash.h @@ -3878,7 +3878,7 @@ # if defined(__ARM_NEON__) || defined(__ARM_NEON) \ || (defined(_M_ARM) && _M_ARM >= 7) \ || defined(_M_ARM64) || defined(_M_ARM64EC) \ - || (defined(__wasm_simd128__) && XXH_HAS_INCLUDE()) /* WASM SIMD128 via SIMDe */ + || (defined(__wasm_simd128__) && !defined(__SSE2__) && XXH_HAS_INCLUDE()) /* WASM SIMD128 via SIMDe */ # define inline __inline__ /* circumvent a clang bug */ # include # undef inline @@ -4009,7 +4009,7 @@ # if ( \ defined(__ARM_NEON__) || defined(__ARM_NEON) /* gcc */ \ || defined(_M_ARM) || defined(_M_ARM64) || defined(_M_ARM64EC) /* msvc */ \ - || (defined(__wasm_simd128__) && XXH_HAS_INCLUDE()) /* wasm simd128 via SIMDe */ \ + || (defined(__wasm_simd128__) && !defined(__SSE2__) && XXH_HAS_INCLUDE()) /* wasm simd128 via SIMDe */ \ ) && ( \ defined(_WIN32) || defined(__LITTLE_ENDIAN__) /* little endian only */ \ || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) \ --- a/libretro/Makefile.common +++ b/libretro/Makefile.common @@ -359,7 +359,7 @@ ifeq ($(PLATFORM_EXT), android) COREFLAGS += -DHAVE_DLFCN_H -else ifneq ($(PLATFORM_EXT), win32) +else ifeq (,$(filter win32 emscripten,$(PLATFORM_EXT))) COREFLAGS += -DHAVE_STRONG_GETAUXVAL endif ifneq (,$(findstring osx,$(platform))) @@ -383,7 +383,7 @@ $(EXTDIR)/cpu_features/src/stack_line_reader.c \ $(EXTDIR)/cpu_features/src/string_view.c -ifneq ($(PLATFORM_EXT), win32) +ifeq (,$(filter win32 emscripten,$(PLATFORM_EXT))) SOURCES_C += \ $(EXTDIR)/cpu_features/src/hwcaps.c endif --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -596,7 +596,8 @@ g_Config.iCpuCore = (int)CPUCore::IR_INTERPRETER; } #else - g_Config.iCpuCore = (int)CPUCore::INTERPRETER; + // PlayAgain: the IR interpreter, which is faster than the plain one and needs no JIT. + g_Config.iCpuCore = (int)CPUCore::IR_INTERPRETER; #endif var.key = "ppsspp_fast_memory"; @@ -2097,6 +2098,9 @@ #if PPSSPP_PLATFORM(IOS) bool can_jit; return (environ_cb(RETRO_ENVIRONMENT_GET_JIT_CAPABLE, &can_jit) && can_jit); +#elif defined(__EMSCRIPTEN__) + // PlayAgain: WebAssembly can't run the x86 code the JITs would write. + return false; #else return true; #endif --- a/ppsspp_config.h +++ b/ppsspp_config.h @@ -148,3 +148,9 @@ #if (PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS)) && !defined(__LIBRETRO__) #define PPSSPP_API_METAL 1 #endif + +// PlayAgain: an Emscripten build without -pthread has no threads. Where PPSSPP would start one, the +// work runs in place on the caller's thread instead, and sleeping does nothing (the host paces frames). +#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__) +#define PPSSPP_NO_THREADS 1 +#endif