From eab44b9e0b9dcb5cf7fa7b6851fa5454041da930 Mon Sep 17 00:00:00 2001 From: David Guillen Fandos Date: Wed, 17 Mar 2021 21:05:49 +0100 Subject: [PATCH] Enable runtime dynarec enable/disable Added a more thorough cache cleanup for reset/mode-change too. Fixed the mmap initialization that ends up leaking memory. Minor x86 asm fixes for Android. --- common.h | 14 ++++----- cpu.h | 1 + cpu_threaded.c | 11 +++++++ gba_memory.c | 5 +--- libretro.c | 63 ++++++++++++++--------------------------- libretro_core_options.h | 2 +- main.c | 3 +- x86/x86_stub.S | 2 +- 8 files changed, 43 insertions(+), 58 deletions(-) diff --git a/common.h b/common.h index 1bfa150..ef6724a 100644 --- a/common.h +++ b/common.h @@ -31,6 +31,7 @@ #define PATH_SEPARATOR_CHAR '/' #endif +/* On x86 we pass arguments via registers instead of stack */ #ifdef X86_ARCH #define function_cc __attribute__((regparm(2))) #else @@ -55,8 +56,6 @@ // functions on PSP for vastly improved memstick performance. #ifdef PSP - #define fastcall - #include #include #include @@ -64,13 +63,8 @@ #include #include #include - - #define convert_palette(value) \ - value = ((value & 0x7FE0) << 1) | (value & 0x1F) - #include #else - typedef unsigned char u8; typedef signed char s8; typedef unsigned short int u16; @@ -79,10 +73,14 @@ typedef signed int s32; typedef unsigned long long int u64; typedef signed long long int s64; +#endif +#ifdef USE_BGR_FORMAT + #define convert_palette(value) \ + value = ((value & 0x7FE0) << 1) | (value & 0x1F) +#else #define convert_palette(value) \ value = ((value & 0x1F) << 11) | ((value & 0x03E0) << 1) | (value >> 10) - #endif #define GBA_SCREEN_WIDTH (240) diff --git a/cpu.h b/cpu.h index 2e4d71e..cde7b2f 100644 --- a/cpu.h +++ b/cpu.h @@ -174,6 +174,7 @@ extern u32 *rom_branch_hash[ROM_BRANCH_HASH_SIZE]; void flush_translation_cache_rom(void); void flush_translation_cache_ram(void); void dump_translation_cache(void); +void wipe_caches(void); extern u32 reg_mode[7][7]; extern u32 spsr[6]; diff --git a/cpu_threaded.c b/cpu_threaded.c index c66fa5e..519d7f0 100644 --- a/cpu_threaded.c +++ b/cpu_threaded.c @@ -3686,6 +3686,17 @@ void flush_translation_cache_rom(void) memset(rom_branch_hash, 0, sizeof(rom_branch_hash)); } +void wipe_caches(void) +{ + /* Ensure we wipe everything including the SMC mirrors */ + flush_translation_cache_rom(); + ewram_code_min = 0; + ewram_code_max = 0x3FFFF; + iwram_code_min = 0; + iwram_code_max = 0x7FFF; + flush_translation_cache_ram(); +} + #define cache_dump_prefix "" void dump_translation_cache(void) diff --git a/gba_memory.c b/gba_memory.c index e1cb9a4..948bcc5 100644 --- a/gba_memory.c +++ b/gba_memory.c @@ -3394,10 +3394,7 @@ void gba_load_state(const void* src) #ifdef HAVE_DYNAREC if (dynarec_enable) - { - flush_translation_cache_ram(); - flush_translation_cache_rom(); - } + wipe_caches(); #endif oam_update = 1; diff --git a/libretro.c b/libretro.c index 178b466..bc61977 100644 --- a/libretro.c +++ b/libretro.c @@ -408,7 +408,13 @@ void retro_get_system_av_info(struct retro_system_av_info* info) void retro_init(void) { -#if defined(_3DS) && defined(HAVE_DYNAREC) +#if defined(HAVE_DYNAREC) + #if defined(HAVE_MMAP) + rom_translation_cache = mmap(NULL, ROM_TRANSLATION_CACHE_SIZE, + PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0); + ram_translation_cache = mmap(NULL, RAM_TRANSLATION_CACHE_SIZE, + PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0); + #elif defined(_3DS) if (__ctr_svchax && !translation_caches_inited) { uint32_t currentHandle; @@ -430,10 +436,8 @@ void retro_init(void) ctr_flush_invalidate_cache(); translation_caches_inited = 1; } -#endif - -#if defined(VITA) && defined(HAVE_DYNAREC) - if(!translation_caches_inited){ + #elif defined(VITA) + if(!translation_caches_inited){ void* currentHandle; sceBlock = getVMBlock(); @@ -456,8 +460,8 @@ void retro_init(void) ram_translation_ptr = ram_translation_cache; sceKernelOpenVMDomain(); translation_caches_inited = 1; -} - + } + #endif #endif if (!gamepak_rom) @@ -641,16 +645,19 @@ static void check_variables(int started_from_load) if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) { - if (started_from_load) - { - if (strcmp(var.value, "disabled") == 0) - dynarec_enable = 0; - else if (strcmp(var.value, "enabled") == 0) - dynarec_enable = 1; - } + int prevvalue = dynarec_enable; + if (strcmp(var.value, "disabled") == 0) + dynarec_enable = 0; + else if (strcmp(var.value, "enabled") == 0) + dynarec_enable = 1; + + if (dynarec_enable != prevvalue) + wipe_caches(); } else dynarec_enable = 1; +#else + dynarec_enable = 0; #endif var.key = "gpsp_frameskip"; @@ -779,34 +786,6 @@ bool retro_load_game(const struct retro_game_info* info) check_variables(1); set_input_descriptors(); -#if defined(HAVE_DYNAREC) - if (dynarec_enable) - { -#if defined(HAVE_MMAP) - - rom_translation_cache = mmap(NULL, ROM_TRANSLATION_CACHE_SIZE, - PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0); - ram_translation_cache = mmap(NULL, RAM_TRANSLATION_CACHE_SIZE, - PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE, -1, 0); - - rom_translation_ptr = rom_translation_cache; - ram_translation_ptr = ram_translation_cache; -#elif defined(_3DS) - dynarec_enable = __ctr_svchax; - rom_translation_ptr = rom_translation_cache; - ram_translation_ptr = ram_translation_cache; -#elif defined(PSP) || defined(VITA) - dynarec_enable = 1; - rom_translation_ptr = rom_translation_cache; - ram_translation_ptr = ram_translation_cache; -#endif - } - else - dynarec_enable = 0; -#else - dynarec_enable = 0; -#endif - char filename_bios[MAX_PATH]; const char* dir = NULL; diff --git a/libretro_core_options.h b/libretro_core_options.h index 7e68022..b5b138d 100644 --- a/libretro_core_options.h +++ b/libretro_core_options.h @@ -147,7 +147,7 @@ struct retro_core_option_definition option_defs_us[] = { #if defined(HAVE_DYNAREC) { "gpsp_drc", - "Dynamic Recompiler (Restart)", + "Dynamic Recompiler", "Dynamically recompile CPU instructions to native instructions. Greatly improves performance, but may reduce accuracy.", { { "enabled", NULL }, diff --git a/main.c b/main.c index 83694b9..73371e4 100644 --- a/main.c +++ b/main.c @@ -114,8 +114,7 @@ void init_main(void) video_count = 960; #ifdef HAVE_DYNAREC - flush_translation_cache_rom(); - flush_translation_cache_ram(); + wipe_caches(); init_emitter(); #endif } diff --git a/x86/x86_stub.S b/x86/x86_stub.S index 8d76457..322547d 100644 --- a/x86/x86_stub.S +++ b/x86/x86_stub.S @@ -538,7 +538,7 @@ _execute_arm_translate: # (if the CPU is halted, do not start executing but # loop in the alert loop until it wakes up) - cmp $0, CPU_HALT_STATE(%ebx) + cmpl $0, CPU_HALT_STATE(%ebx) je 1f call alert_loop # Need to push something to the stack