From: Rune Holm Date: Fri, 11 Jun 2021 17:37:57 +0000 (+0100) Subject: Created address translation fast path for code reads, separated slow path into separa... X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=3203ac85909c4feadb96bdf56b373785f9c81210;p=pistorm Created address translation fast path for code reads, separated slow path into separate function so the fast path is more likely to be inlined. 10-20% performance improvement --- diff --git a/emulator.c b/emulator.c index 8027617..1be7bcd 100644 --- a/emulator.c +++ b/emulator.c @@ -4,6 +4,7 @@ #include "emulator.h" #include "platforms/platforms.h" #include "input/input.h" +#include "m68kcpu.h" #include "platforms/amiga/Gayle.h" #include "platforms/amiga/amiga-registers.h" @@ -43,6 +44,7 @@ unsigned char write_ranges; unsigned int write_addr[8]; unsigned int write_upper[8]; unsigned char *write_data[8]; +address_translation_cache code_translation_cache = {0}; int kb_hook_enabled = 0; int mouse_hook_enabled = 0; diff --git a/m68kcpu.c b/m68kcpu.c index 7883bdd..4a4fc39 100644 --- a/m68kcpu.c +++ b/m68kcpu.c @@ -1208,7 +1208,7 @@ inline unsigned int m68k_read_immediate_16(unsigned int address) { } } #endif - + return m68k_read_memory_16(address); } inline unsigned int m68k_read_immediate_32(unsigned int address) { @@ -1230,7 +1230,7 @@ inline unsigned int m68k_read_pcrelative_8(unsigned int address) { return read_data[i][address - read_addr[i]]; } } - + return m68k_read_memory_8(address); } inline unsigned int m68k_read_pcrelative_16(unsigned int address) { @@ -1253,8 +1253,65 @@ inline unsigned int m68k_read_pcrelative_32(unsigned int address) { } #endif + +uint m68ki_read_imm6_addr_slowpath(uint32_t address, address_translation_cache *cache) +{ + for (int i = 0; i < read_ranges; i++) { + if(address >= read_addr[i] && address < read_upper[i]) { + cache->lower = read_addr[i]; + cache->upper = read_upper[i]; + cache->data = read_data[i]; + REG_PC += 2; + return be16toh(((unsigned short *)(read_data[i] + (address - read_addr[i])))[0]); + } + } + + m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */ + m68ki_cpu.mmu_tmp_fc = FLAG_S | FUNCTION_CODE_USER_PROGRAM; + m68ki_cpu.mmu_tmp_rw = 1; + m68ki_cpu.mmu_tmp_sz = M68K_SZ_WORD; + m68ki_check_address_error(REG_PC, MODE_READ, FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */ + +#if M68K_EMULATE_PREFETCH +{ + uint result; + if(REG_PC != CPU_PREF_ADDR) + { + CPU_PREF_DATA = m68ki_ic_readimm16(REG_PC); + CPU_PREF_ADDR = m68ki_cpu.mmu_tmp_buserror_occurred ? ((uint32)~0) : REG_PC; + } + result = MASK_OUT_ABOVE_16(CPU_PREF_DATA); + REG_PC += 2; + if (!m68ki_cpu.mmu_tmp_buserror_occurred) { + // prefetch only if no bus error occurred in opcode fetch + CPU_PREF_DATA = m68ki_ic_readimm16(REG_PC); + CPU_PREF_ADDR = m68ki_cpu.mmu_tmp_buserror_occurred ? ((uint32)~0) : REG_PC; + // ignore bus error on prefetch + m68ki_cpu.mmu_tmp_buserror_occurred = 0; + } + return result; +} +#else + + uint32_t address = ADDRESS_68K(REG_PC); + REG_PC += 2; + + for (int i = 0; i < read_ranges; i++) { + if(address >= read_addr[i] && address < read_upper[i]) { + return be16toh(((unsigned short *)(read_data[i] + (address - read_addr[i])))[0]); + } + } + + return m68k_read_immediate_16(address); +#endif /* M68K_EMULATE_PREFETCH */ +} + + + void m68k_add_ram_range(uint32_t addr, uint32_t upper, unsigned char *ptr) { + code_translation_cache.lower = 0; + code_translation_cache.upper = 0; if ((addr == 0 && upper == 0) || upper < addr) return; @@ -1300,6 +1357,8 @@ void m68k_add_ram_range(uint32_t addr, uint32_t upper, unsigned char *ptr) void m68k_add_rom_range(uint32_t addr, uint32_t upper, unsigned char *ptr) { + code_translation_cache.lower = 0; + code_translation_cache.upper = 0; if ((addr == 0 && upper == 0) || upper < addr) return; diff --git a/m68kcpu.h b/m68kcpu.h index 0be9551..38d6b9f 100644 --- a/m68kcpu.h +++ b/m68kcpu.h @@ -1075,6 +1075,13 @@ char* m68ki_disassemble_quick(unsigned int pc, unsigned int cpu_type); /* ---------------------------- Read Immediate ---------------------------- */ +typedef struct +{ + unsigned int lower; + unsigned int upper; + unsigned char *data; +} address_translation_cache; + extern unsigned char read_ranges; extern unsigned int read_addr[8]; @@ -1085,6 +1092,8 @@ extern unsigned int write_addr[8]; extern unsigned int write_upper[8]; extern unsigned char *write_data[8]; +extern address_translation_cache code_translation_cache; + // clear the instruction cache inline void m68ki_ic_clear() { @@ -1152,56 +1161,24 @@ static inline uint32 m68ki_ic_readimm16(uint32 address) /* Handles all immediate reads, does address error check, function code setting, * and prefetching if they are enabled in m68kconf.h */ -static inline uint m68ki_read_imm_16(void) -{ - uint32_t address = ADDRESS_68K(REG_PC); - for (int i = 0; i < read_ranges; i++) { - if(address >= read_addr[i] && address < read_upper[i]) { - REG_PC += 2; - return be16toh(((unsigned short *)(read_data[i] + (address - read_addr[i])))[0]); - } - } +uint m68ki_read_imm6_addr_slowpath(uint32_t address, address_translation_cache *cache); - m68ki_set_fc(FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */ - m68ki_cpu.mmu_tmp_fc = FLAG_S | FUNCTION_CODE_USER_PROGRAM; - m68ki_cpu.mmu_tmp_rw = 1; - m68ki_cpu.mmu_tmp_sz = M68K_SZ_WORD; - m68ki_check_address_error(REG_PC, MODE_READ, FLAG_S | FUNCTION_CODE_USER_PROGRAM); /* auto-disable (see m68kcpu.h) */ -#if M68K_EMULATE_PREFETCH -{ - uint result; - if(REG_PC != CPU_PREF_ADDR) - { - CPU_PREF_DATA = m68ki_ic_readimm16(REG_PC); - CPU_PREF_ADDR = m68ki_cpu.mmu_tmp_buserror_occurred ? ((uint32)~0) : REG_PC; - } - result = MASK_OUT_ABOVE_16(CPU_PREF_DATA); - REG_PC += 2; - if (!m68ki_cpu.mmu_tmp_buserror_occurred) { - // prefetch only if no bus error occurred in opcode fetch - CPU_PREF_DATA = m68ki_ic_readimm16(REG_PC); - CPU_PREF_ADDR = m68ki_cpu.mmu_tmp_buserror_occurred ? ((uint32)~0) : REG_PC; - // ignore bus error on prefetch - m68ki_cpu.mmu_tmp_buserror_occurred = 0; - } - return result; -} -#else +static inline uint m68ki_read_imm_16(void) +{ uint32_t address = ADDRESS_68K(REG_PC); - REG_PC += 2; - for (int i = 0; i < read_ranges; i++) { - if(address >= read_addr[i] && address < read_upper[i]) { - return be16toh(((unsigned short *)(read_data[i] + (address - read_addr[i])))[0]); - } - } - - return m68k_read_immediate_16(address); -#endif /* M68K_EMULATE_PREFETCH */ + address_translation_cache *cache = &code_translation_cache; + if(address >= cache->lower && address < cache->upper) + { + REG_PC += 2; + return be16toh(((unsigned short *)(cache->data + (address - cache->lower)))[0]); + } + return m68ki_read_imm6_addr_slowpath(address, cache); } + static inline uint m68ki_read_imm_8(void) { /* map read immediate 8 to read immediate 16 */