From 29b549f7bfba53374958a8bfb33437131986677d Mon Sep 17 00:00:00 2001 From: notaz Date: Mon, 30 Sep 2024 21:08:28 +0300 Subject: [PATCH] 3ds: some random tuning saves some memory --- Makefile.libretro | 4 +++- frontend/3ds/sys/mman.h | 49 +++++++++++++++++++++++------------------ frontend/libretro.c | 34 +++++++++++++++++++++++++--- plugins/gpulib/gpu.h | 3 ++- 4 files changed, 64 insertions(+), 26 deletions(-) diff --git a/Makefile.libretro b/Makefile.libretro index 265ae696..67754703 100644 --- a/Makefile.libretro +++ b/Makefile.libretro @@ -318,7 +318,8 @@ else ifeq ($(platform), ctr) CFLAGS += -DARM11 -D_3DS -DNO_DYLIB -DGPU_UNAI_USE_FLOATMATH -DGPU_UNAI_USE_FLOAT_DIV_MULTINV CFLAGS += -march=armv6k -mtune=mpcore -mfloat-abi=hard -marm -mfpu=vfp -mtp=soft CFLAGS += -Wall -mword-relocations - CFLAGS += -fomit-frame-pointer -ffast-math -funroll-loops + CFLAGS += -fomit-frame-pointer + # CFLAGS += -funroll-loops # ~500K of bloat CFLAGS += -Ifrontend/3ds -I$(CTRULIB)/include CFLAGS += -Werror=implicit-function-declaration CFLAGS += -DHAVE_UNISTD_H @@ -334,6 +335,7 @@ else ifeq ($(platform), ctr) HAVE_NEON = 0 STATIC_LINKING = 1 NO_POSIX_MEMALIGN := 1 + NO_MMAP := 1 # for psxmem HAVE_PHYSICAL_CDROM = 0 # Xbox 360 diff --git a/frontend/3ds/sys/mman.h b/frontend/3ds/sys/mman.h index fdf5ac6a..4ba90db2 100644 --- a/frontend/3ds/sys/mman.h +++ b/frontend/3ds/sys/mman.h @@ -6,10 +6,7 @@ extern "C" { #endif #include -#include -#include #include -#include #include "3ds_utils.h" @@ -22,6 +19,9 @@ extern "C" { #define MAP_FAILED ((void *)-1) +void SysPrintf(const char *fmt, ...); + +#if 0 // not used static void* dynarec_cache = NULL; static void* dynarec_cache_mapping = NULL; @@ -47,7 +47,7 @@ static inline void* mmap(void *addr, size_t len, int prot, int flags, int fd, of } svcDuplicateHandle(¤tHandle, 0xFFFF8001); - svcControlProcessMemory(currentHandle, addr, dynarec_cache, + svcControlProcessMemory(currentHandle, (uintptr_t)addr, (uintptr_t)dynarec_cache, len, MEMOP_MAP, prot); svcCloseHandle(currentHandle); dynarec_cache_mapping = addr; @@ -70,22 +70,6 @@ static inline void* mmap(void *addr, size_t len, int prot, int flags, int fd, of return addr_out; } -static inline int mprotect(void *addr, size_t len, int prot) -{ - if(__ctr_svchax) - { - uint32_t currentHandle; - svcDuplicateHandle(¤tHandle, 0xFFFF8001); - svcControlProcessMemory(currentHandle, addr, NULL, - len, MEMOP_PROT, prot); - svcCloseHandle(currentHandle); - return 0; - } - - printf("mprotect called without svcControlProcessMemory access !\n"); - return -1; -} - static inline int munmap(void *addr, size_t len) { if((addr == dynarec_cache_mapping) && __ctr_svchax) @@ -93,7 +77,7 @@ static inline int munmap(void *addr, size_t len) uint32_t currentHandle; svcDuplicateHandle(¤tHandle, 0xFFFF8001); svcControlProcessMemory(currentHandle, - dynarec_cache, dynarec_cache_mapping, + (uintptr_t)dynarec_cache, (uintptr_t)dynarec_cache_mapping, len, MEMOP_UNMAP, 0b111); svcCloseHandle(currentHandle); dynarec_cache_mapping = NULL; @@ -104,6 +88,29 @@ static inline int munmap(void *addr, size_t len) return 0; } +#endif + +static inline int mprotect(void *addr, size_t len, int prot) +{ + if (__ctr_svchax) + { + uint32_t currentHandle = 0; + int r; + svcDuplicateHandle(¤tHandle, 0xFFFF8001); + r = svcControlProcessMemory(currentHandle, (uintptr_t)addr, 0, + len, MEMOP_PROT, prot); + svcCloseHandle(currentHandle); + if (r < 0) { + SysPrintf("svcControlProcessMemory failed for %p %u %x: %d\n", + addr, len, prot, r); + return -1; + } + return 0; + } + + SysPrintf("mprotect called without svcControlProcessMemory access!\n"); + return -1; +} #ifdef __cplusplus }; diff --git a/frontend/libretro.c b/frontend/libretro.c index 1ad39b51..90f40a62 100644 --- a/frontend/libretro.c +++ b/frontend/libretro.c @@ -179,7 +179,11 @@ static int negcon_linearity = 1; static bool axis_bounds_modifier; /* PSX max resolution is 640x512, but with enhancement it's 1024x512 */ +#ifdef GPU_NEON #define VOUT_MAX_WIDTH 1024 +#else +#define VOUT_MAX_WIDTH 640 +#endif #define VOUT_MAX_HEIGHT 512 //Dummy functions @@ -556,6 +560,21 @@ void pl_vita_munmap(void *ptr, size_t size, enum psxMapTag tag) } #endif +static void log_mem_usage(void) +{ +#ifdef _3DS + extern u32 __heap_size, __linear_heap_size, __stacksize__; + extern char __end__; // 3dsx.ld + u32 app_memory = *((volatile u32 *)0x1FF80040); + s64 mem_used = 0; + if (__ctr_svchax) + svcGetSystemInfo(&mem_used, 0, 1); + + SysPrintf("mem: %d/%d heap: %d linear: %d stack: %d exe: %d\n", (int)mem_used, app_memory, + __heap_size, __linear_heap_size, __stacksize__, (int)&__end__ - 0x100000); +#endif +} + static void *pl_mmap(unsigned int size) { return psxMap(0, size, 0, MAP_TAG_VRAM); @@ -1954,8 +1973,7 @@ bool retro_load_game(const struct retro_game_info *info) { size_t i; unsigned int cd_index = 0; - bool is_m3u = (strcasestr(info->path, ".m3u") != NULL); - bool is_exe = (strcasestr(info->path, ".exe") != NULL); + bool is_m3u, is_exe; int ret; struct retro_input_descriptor desc[] = { @@ -2015,6 +2033,8 @@ bool retro_load_game(const struct retro_game_info *info) LogErr("info->path required\n"); return false; } + is_m3u = (strcasestr(info->path, ".m3u") != NULL); + is_exe = (strcasestr(info->path, ".exe") != NULL); update_variables(false); @@ -2192,6 +2212,7 @@ bool retro_load_game(const struct retro_game_info *info) set_retro_memmap(); retro_set_audio_buff_status_cb(); + log_mem_usage(); if (check_unsatisfied_libcrypt()) show_notification("LibCrypt protected game with missing SBI detected", 3000, 3); @@ -3729,6 +3750,8 @@ void retro_init(void) struct retro_rumble_interface rumble; int ret; + log_mem_usage(); + msg_interface_version = 0; environ_cb(RETRO_ENVIRONMENT_GET_MESSAGE_INTERFACE_VERSION, &msg_interface_version); @@ -3766,12 +3789,17 @@ void retro_init(void) vout_buf = linearMemAlign(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2, 0x80); #elif defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L) && P_HAVE_POSIX_MEMALIGN if (posix_memalign(&vout_buf, 16, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2) != 0) - vout_buf = (void *) 0; + vout_buf = NULL; else memset(vout_buf, 0, VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT * 2); #else vout_buf = calloc(VOUT_MAX_WIDTH * VOUT_MAX_HEIGHT, 2); #endif + if (vout_buf == NULL) + { + LogErr("OOM for vout_buf.\n"); + exit(1); + } vout_buf_ptr = vout_buf; diff --git a/plugins/gpulib/gpu.h b/plugins/gpulib/gpu.h index bf420b9c..2083224e 100644 --- a/plugins/gpulib/gpu.h +++ b/plugins/gpulib/gpu.h @@ -39,7 +39,8 @@ extern "C" { #define LE16TOH(x) (x) #endif -#define BIT(x) (1 << (x)) +#undef BIT +#define BIT(x) (1u << (x)) #define PSX_GPU_STATUS_DHEIGHT BIT(19) #define PSX_GPU_STATUS_PAL BIT(20) -- 2.39.5