From 8426b35a8066a447e312de881053ca141361a3d4 Mon Sep 17 00:00:00 2001 From: Ethan O'Brien <77750390+ethanaobrien@users.noreply.github.com> Date: Sat, 12 Aug 2023 14:38:35 -0500 Subject: [PATCH] Cleanup emscripten libretro build target --- .gitignore | 2 ++ Common/CPUDetect.cpp | 2 +- Common/CommonFuncs.h | 2 +- Common/FakeCPUDetect.cpp | 2 +- Common/MemArenaPosix.cpp | 13 +++++++++++++ Common/TimeUtil.cpp | 6 ++++++ Core/MemMap.h | 2 +- libretro/Makefile | 24 +++++++++++++----------- libretro/libretro.cpp | 4 ++++ ppsspp_config.h | 4 ++-- 10 files changed, 44 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 0723838277..842b171bd1 100644 --- a/.gitignore +++ b/.gitignore @@ -117,6 +117,8 @@ debian/ppsspp/ # Libretro build *.o +*.tmp +*.a # YouCompleteMe file .ycm_extra_conf.pyc diff --git a/Common/CPUDetect.cpp b/Common/CPUDetect.cpp index aa43930ead..2628f91d46 100644 --- a/Common/CPUDetect.cpp +++ b/Common/CPUDetect.cpp @@ -17,7 +17,7 @@ // Reference : https://stackoverflow.com/questions/6121792/how-to-check-if-a-cpu-supports-the-sse3-instruction-set #include "ppsspp_config.h" -#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64) +#if (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) && !defined(__EMSCRIPTEN__) #include "ext/cpu_features/include/cpuinfo_x86.h" diff --git a/Common/CommonFuncs.h b/Common/CommonFuncs.h index 294cf4fb39..65b3344090 100644 --- a/Common/CommonFuncs.h +++ b/Common/CommonFuncs.h @@ -30,7 +30,7 @@ #include #include -#if PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64) +#if (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) && !defined(__EMSCRIPTEN__) #define Crash() {asm ("int $3");} #elif PPSSPP_PLATFORM(SWITCH) // TODO: Implement Crash() for Switch, lets not use breakpoint for the time being diff --git a/Common/FakeCPUDetect.cpp b/Common/FakeCPUDetect.cpp index e04ed356ed..e57799950a 100644 --- a/Common/FakeCPUDetect.cpp +++ b/Common/FakeCPUDetect.cpp @@ -18,7 +18,7 @@ #include "ppsspp_config.h" #if PPSSPP_ARCH(ARM) || PPSSPP_ARCH(ARM64) #define REAL_CPUDETECT_AVAIL 1 -#elif PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64) +#elif (PPSSPP_ARCH(X86) || PPSSPP_ARCH(AMD64)) && !defined(__EMSCRIPTEN__) #define REAL_CPUDETECT_AVAIL 1 #elif PPSSPP_ARCH(MIPS) || PPSSPP_ARCH(MIPS64) #define REAL_CPUDETECT_AVAIL 1 diff --git a/Common/MemArenaPosix.cpp b/Common/MemArenaPosix.cpp index 6956f9fcd8..7923b99b85 100644 --- a/Common/MemArenaPosix.cpp +++ b/Common/MemArenaPosix.cpp @@ -52,6 +52,7 @@ bool MemArena::NeedsProbing() { } bool MemArena::GrabMemSpace(size_t size) { +#ifndef NO_MMAP constexpr mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // Try a few times in case multiple instances are started near each other. @@ -98,15 +99,21 @@ bool MemArena::GrabMemSpace(size_t size) { ERROR_LOG(MEMMAP, "Failed to ftruncate %d (%s) to size %08x", (int)fd, ram_temp_file.c_str(), (int)size); // Should this be a failure? } +#endif return true; } void MemArena::ReleaseSpace() { +#ifndef NO_MMAP close(fd); +#endif } void *MemArena::CreateView(s64 offset, size_t size, void *base) { +#ifdef NO_MMAP + return (void*) base; +#else void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | // Do not sync memory to underlying file. Linux has this by default. #if defined(__DragonFly__) || defined(__FreeBSD__) @@ -119,10 +126,13 @@ void *MemArena::CreateView(s64 offset, size_t size, void *base) return 0; } return retval; +#endif } void MemArena::ReleaseView(s64 offset, void* view, size_t size) { +#ifndef NO_MMAP munmap(view, size); +#endif } u8* MemArena::Find4GBBase() { @@ -147,6 +157,9 @@ u8* MemArena::Find4GBBase() { // This has been known to fail lately though, see issue #12249. return hardcoded_ptr; } +#elif defined(NO_MMAP) + void* base = std::malloc(0x0A000000); + return static_cast(base); #else size_t size = 0x10000000; void* base = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED | MAP_NORESERVE, -1, 0); diff --git a/Common/TimeUtil.cpp b/Common/TimeUtil.cpp index ad07fbd18c..6cc507a016 100644 --- a/Common/TimeUtil.cpp +++ b/Common/TimeUtil.cpp @@ -9,6 +9,10 @@ #include #endif // HAVE_LIBNX +#ifdef __EMSCRIPTEN__ +#include +#endif // __EMSCRIPTEN__ + #ifdef _WIN32 #include "CommonWindows.h" #include @@ -58,6 +62,8 @@ void sleep_ms(int ms) { Sleep(ms); #elif defined(HAVE_LIBNX) svcSleepThread(ms * 1000000); +#elif defined(__EMSCRIPTEN__) + emscripten_sleep(ms); #else usleep(ms * 1000); #endif diff --git a/Core/MemMap.h b/Core/MemMap.h index 7a0af46257..67e17babcd 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -67,7 +67,7 @@ extern u32 g_PSPModel; // UWP has such limited memory management that we need to mask // even in 64-bit mode. Also, when using the sanitizer, we need to mask as well. -#if PPSSPP_ARCH(32BIT) || PPSSPP_PLATFORM(UWP) || USE_ASAN || PPSSPP_PLATFORM(IOS) +#if PPSSPP_ARCH(32BIT) || PPSSPP_PLATFORM(UWP) || USE_ASAN || PPSSPP_PLATFORM(IOS) || defined(__EMSCRIPTEN__) #define MASKED_PSP_MEMORY #endif diff --git a/libretro/Makefile b/libretro/Makefile index 4eb5be6e8e..4625c9da0f 100644 --- a/libretro/Makefile +++ b/libretro/Makefile @@ -300,16 +300,12 @@ else ifneq (,$(findstring armv,$(platform))) # emscripten else ifeq ($(platform), emscripten) + TARGET_ARCH = wasm32 + GLES = 1 TARGET := $(TARGET_NAME)_libretro_emscripten.bc - GLES := 1 - CPUFLAGS += - PLATCFLAGS += -DCC_resampler=mupen_CC_resampler -Dsinc_resampler=mupen_sinc_resampler \ - -Drglgen_symbol_map=mupen_rglgen_symbol_map -Dmain_exit=mupen_main_exit \ - -Dadler32=mupen_adler32 -Drarch_resampler_realloc=mupen_rarch_resampler_realloc \ - -Daudio_convert_s16_to_float_C=mupen_audio_convert_s16_to_float_C -Daudio_convert_float_to_s16_C=mupen_audio_convert_float_to_s16_C \ - -Daudio_convert_init_simd=mupen_audio_convert_init_simd -Drglgen_resolve_symbols_custom=mupen_rglgen_resolve_symbols_custom \ - -Drglgen_resolve_symbols=mupen_rglgen_resolve_symbols - PLATFORM_EXT = unix + PLATCFLAGS += -msimd128 -msse3 -mssse3 -msse4.1 -pthread -D__EMSCRIPTEN__ -DNO_MMAP + PLATFORM_EXT = emscripten + STATIC_LINKING = 1 # Windows MSVC all architectures else ifneq (,$(findstring windows_msvc2019,$(platform))) @@ -435,7 +431,11 @@ else GLFLAGS += -DHAVE_OPENGL endif -COREFLAGS += -D__LIBRETRO__ -DPPSSPP -DUSE_FFMPEG -DWITH_UPNP -DGLEW_STATIC -DGLEW_NO_GLU -DMINIUPNP_STATICLIB +COREFLAGS += -D__LIBRETRO__ -DPPSSPP -DWITH_UPNP -DGLEW_STATIC -DGLEW_NO_GLU -DMINIUPNP_STATICLIB +ifneq ($(platform), emscripten) + # ffmpeg does not work with emscripten + COREFLAGS += -DUSE_FFMPEG +endif ifeq ($(DEBUG), 1) ifneq (,$(findstring msvc,$(platform))) @@ -445,6 +445,8 @@ ifeq ($(DEBUG), 1) CPUOPTS += -O0 -g endif CPUOPTS += -D_DEBUG +else ifeq ($(platform), emscripten) + CPUOPTS += -O3 -DNDEBUG else CPUOPTS += -O2 -DNDEBUG endif @@ -494,7 +496,7 @@ all: $(TARGET) $(TARGET): $(OBJECTS) ifeq ($(STATIC_LINKING), 1) - $(AR) rcs $A $(OBJECTS) + $(AR) rcs $(TARGET) $(OBJECTS) else $(LD) $(LINKOUT)$@ $(OBJECTS) $(LDFLAGS) $(GL_LIB) endif diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index cbb1609d65..27a2f1a4f2 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -474,6 +474,7 @@ static void check_variables(CoreParameter &coreParam) g_Config.iLanguage = PSP_SYSTEMPARAM_LANGUAGE_CHINESE_SIMPLIFIED; } +#ifndef __EMSCRIPTEN__ var.key = "ppsspp_cpu_core"; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { @@ -491,6 +492,9 @@ static void check_variables(CoreParameter &coreParam) // to experiment in future iOS versions or something... g_Config.iCpuCore = (int)CPUCore::IR_JIT; } +#else + g_Config.iCpuCore = (int)CPUCore::INTERPRETER; +#endif var.key = "ppsspp_fast_memory"; if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) diff --git a/ppsspp_config.h b/ppsspp_config.h index 5f46f501af..e26b1220e1 100644 --- a/ppsspp_config.h +++ b/ppsspp_config.h @@ -8,7 +8,7 @@ #define PPSSPP_API(PPSSPP_FEATURE) (PPSSPP_API_##PPSSPP_FEATURE) // ARCH defines -#if defined(_M_IX86) || defined(__i386__) +#if defined(_M_IX86) || defined(__i386__) || defined (__EMSCRIPTEN__) #define PPSSPP_ARCH_X86 1 #define PPSSPP_ARCH_32BIT 1 //TODO: Remove this compat define @@ -17,7 +17,7 @@ #endif #endif -#if defined(_M_X64) || defined(__amd64__) || defined(__x86_64__) +#if (defined(_M_X64) || defined(__amd64__) || defined(__x86_64__)) && !defined(__EMSCRIPTEN__) #define PPSSPP_ARCH_AMD64 1 #if defined(__ILP32__) #define PPSSPP_ARCH_32BIT 1