From a2b0521dbdd754e129639bc210003cde57775cf5 Mon Sep 17 00:00:00 2001 From: Rune Holm Date: Fri, 18 Jun 2021 20:19:41 +0100 Subject: [PATCH] move address translation cache fields from global variables to the m68k struct, as this way we can pass around one pointer and access them all in the future. This happens to win 5-10% performance, because now the code generator can generate a single global variable pointer to get to all of the translation cache/range fields at once. --- emulator.c | 10 ---- m68kcpu.c | 132 +++++++++++++++++++++++++++-------------------------- m68kcpu.h | 96 +++++++++++++++++++------------------- 3 files changed, 117 insertions(+), 121 deletions(-) diff --git a/emulator.c b/emulator.c index d70ad211..19a14677 100644 --- a/emulator.c +++ b/emulator.c @@ -38,16 +38,6 @@ #define KEY_POLL_INTERVAL_MSEC 5000 -unsigned char read_ranges; -unsigned int read_addr[8]; -unsigned int read_upper[8]; -unsigned char *read_data[8]; -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; int cpu_emulation_running = 1; diff --git a/m68kcpu.c b/m68kcpu.c index bdcb1039..b91be0a0 100644 --- a/m68kcpu.c +++ b/m68kcpu.c @@ -1205,9 +1205,9 @@ void m68k_set_context(void* src) /* Read data immediately following the PC */ inline unsigned int m68k_read_immediate_16(unsigned int address) { #if M68K_EMULATE_PREFETCH == OPT_ON - 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]); + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { + return be16toh(((unsigned short *)(m68ki_cpu.read_data[i] + (address - m68ki_cpu.read_addr[i])))[0]); } } #endif @@ -1216,9 +1216,9 @@ inline unsigned int m68k_read_immediate_16(unsigned int address) { } inline unsigned int m68k_read_immediate_32(unsigned int address) { #if M68K_EMULATE_PREFETCH == OPT_ON - for (int i = 0; i < read_ranges; i++) { - if(address >= read_addr[i] && address < read_upper[i]) { - return be32toh(((unsigned int *)(read_data[i] + (address - read_addr[i])))[0]); + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { + return be32toh(((unsigned int *)(m68ki_cpu.read_data[i] + (address - m68ki_cpu.read_addr[i])))[0]); } } #endif @@ -1228,27 +1228,27 @@ inline unsigned int m68k_read_immediate_32(unsigned int address) { /* Read data relative to the PC */ inline unsigned int m68k_read_pcrelative_8(unsigned int address) { - for (int i = 0; i < read_ranges; i++) { - if(address >= read_addr[i] && address < read_upper[i]) { - return read_data[i][address - read_addr[i]]; + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { + return m68ki_cpu.read_data[i][address - m68ki_cpu.read_addr[i]]; } } return m68k_read_memory_8(address); } inline unsigned int m68k_read_pcrelative_16(unsigned int address) { - 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]); + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { + return be16toh(((unsigned short *)(m68ki_cpu.read_data[i] + (address - m68ki_cpu.read_addr[i])))[0]); } } return m68k_read_memory_16(address); } inline unsigned int m68k_read_pcrelative_32(unsigned int address) { - for (int i = 0; i < read_ranges; i++) { - if(address >= read_addr[i] && address < read_upper[i]) { - return be32toh(((unsigned int *)(read_data[i] + (address - read_addr[i])))[0]); + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { + return be32toh(((unsigned int *)(m68ki_cpu.read_data[i] + (address - m68ki_cpu.read_addr[i])))[0]); } } @@ -1261,13 +1261,13 @@ uint m68ki_read_imm6_addr_slowpath(uint32_t pc, address_translation_cache *cache { uint32_t address = ADDRESS_68K(pc); uint32_t pc_address_diff = pc - address; - for (int i = 0; i < read_ranges; i++) { - if(address >= read_addr[i] && address < read_upper[i]) { - cache->lower = read_addr[i] + pc_address_diff; - cache->upper = read_upper[i] + pc_address_diff; - cache->offset = read_data[i] - cache->lower; + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { + cache->lower = m68ki_cpu.read_addr[i] + pc_address_diff; + cache->upper = m68ki_cpu.read_upper[i] + pc_address_diff; + cache->offset = m68ki_cpu.read_data[i] - cache->lower; REG_PC += 2; - return be16toh(((unsigned short *)(read_data[i] + (address - read_addr[i])))[0]); + return be16toh(((unsigned short *)(m68ki_cpu.read_data[i] + (address - m68ki_cpu.read_addr[i])))[0]); } } @@ -1307,45 +1307,45 @@ uint m68ki_read_imm6_addr_slowpath(uint32_t pc, address_translation_cache *cache void m68k_add_ram_range(uint32_t addr, uint32_t upper, unsigned char *ptr) { - code_translation_cache.lower = 0; - code_translation_cache.upper = 0; + m68ki_cpu.code_translation_cache.lower = 0; + m68ki_cpu.code_translation_cache.upper = 0; if ((addr == 0 && upper == 0) || upper < addr) return; - for (int i = 0; i < write_ranges; i++) { - if (write_addr[i] == addr) { + for (int i = 0; i < m68ki_cpu.write_ranges; i++) { + if (m68ki_cpu.write_addr[i] == addr) { uint8_t changed = 0; - if (write_upper[i] != upper) { - write_upper[i] = upper; + if (m68ki_cpu.write_upper[i] != upper) { + m68ki_cpu.write_upper[i] = upper; changed = 1; } - if (write_data[i] != ptr) { - write_data[i] = ptr; + if (m68ki_cpu.write_data[i] != ptr) { + m68ki_cpu.write_data[i] = ptr; changed = 1; } if (changed) { - printf("[MUSASHI] Adjusted mapped write range %d: %.8X-%.8X (%p)\n", write_ranges, addr, upper, ptr); + printf("[MUSASHI] Adjusted mapped write range %d: %.8X-%.8X (%p)\n", m68ki_cpu.write_ranges, addr, upper, ptr); } return; } } - if (read_ranges + 1 < 8) { - read_addr[read_ranges] = addr; - read_upper[read_ranges] = upper; - read_data[read_ranges] = ptr; - read_ranges++; - printf("[MUSASHI] Mapped read range %d: %.8X-%.8X (%p)\n", read_ranges, addr, upper, ptr); + if (m68ki_cpu.read_ranges + 1 < 8) { + m68ki_cpu.read_addr[m68ki_cpu.read_ranges] = addr; + m68ki_cpu.read_upper[m68ki_cpu.read_ranges] = upper; + m68ki_cpu.read_data[m68ki_cpu.read_ranges] = ptr; + m68ki_cpu.read_ranges++; + printf("[MUSASHI] Mapped read range %d: %.8X-%.8X (%p)\n", m68ki_cpu.read_ranges, addr, upper, ptr); } else { printf("Can't Musashi map more than eight RAM/ROM read ranges.\n"); } - if (write_ranges + 1 < 8) { - write_addr[write_ranges] = addr; - write_upper[write_ranges] = upper; - write_data[write_ranges] = ptr; - write_ranges++; - printf("[MUSASHI] Mapped write range %d: %.8X-%.8X (%p)\n", write_ranges, addr, upper, ptr); + if (m68ki_cpu.write_ranges + 1 < 8) { + m68ki_cpu.write_addr[m68ki_cpu.write_ranges] = addr; + m68ki_cpu.write_upper[m68ki_cpu.write_ranges] = upper; + m68ki_cpu.write_data[m68ki_cpu.write_ranges] = ptr; + m68ki_cpu.write_ranges++; + printf("[MUSASHI] Mapped write range %d: %.8X-%.8X (%p)\n", m68ki_cpu.write_ranges, addr, upper, ptr); } else { printf("Can't Musashi map more than eight RAM write ranges.\n"); @@ -1354,35 +1354,35 @@ 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; + m68ki_cpu.code_translation_cache.lower = 0; + m68ki_cpu.code_translation_cache.upper = 0; if ((addr == 0 && upper == 0) || upper < addr) return; - for (int i = 0; i < read_ranges; i++) { - if (read_addr[i] == addr) { + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if (m68ki_cpu.read_addr[i] == addr) { uint8_t changed = 0; - if (read_upper[i] != upper) { - read_upper[i] = upper; + if (m68ki_cpu.read_upper[i] != upper) { + m68ki_cpu.read_upper[i] = upper; changed = 1; } - if (read_data[i] != ptr) { - read_data[i] = ptr; + if (m68ki_cpu.read_data[i] != ptr) { + m68ki_cpu.read_data[i] = ptr; changed = 1; } if (changed) { - printf("[MUSASHI] Adjusted mapped read range %d: %.8X-%.8X (%p)\n", read_ranges, addr, upper, ptr); + printf("[MUSASHI] Adjusted mapped read range %d: %.8X-%.8X (%p)\n", m68ki_cpu.read_ranges, addr, upper, ptr); } return; } } - if (read_ranges + 1 < 8) { - read_addr[read_ranges] = addr; - read_upper[read_ranges] = upper; - read_data[read_ranges] = ptr; - read_ranges++; - printf("[MUSASHI] Mapped read range %d: %.8X-%.8X (%p)\n", read_ranges, addr, upper, ptr); + if (m68ki_cpu.read_ranges + 1 < 8) { + m68ki_cpu.read_addr[m68ki_cpu.read_ranges] = addr; + m68ki_cpu.read_upper[m68ki_cpu.read_ranges] = upper; + m68ki_cpu.read_data[m68ki_cpu.read_ranges] = ptr; + m68ki_cpu.read_ranges++; + printf("[MUSASHI] Mapped read range %d: %.8X-%.8X (%p)\n", m68ki_cpu.read_ranges, addr, upper, ptr); } else { printf("Can't Musashi map more than eight RAM/ROM read ranges.\n"); @@ -1393,15 +1393,17 @@ void m68k_clear_ranges() { printf("[MUSASHI] Clearing all reads/write memory ranges.\n"); for (int i = 0; i < 8; i++) { - read_upper[i] = 0; - read_addr[i] = 0; - read_data[i] = NULL; - write_upper[i] = 0; - write_addr[i] = 0; - write_data[i] = NULL; + m68ki_cpu.read_upper[i] = 0; + m68ki_cpu.read_addr[i] = 0; + m68ki_cpu.read_data[i] = NULL; + m68ki_cpu.write_upper[i] = 0; + m68ki_cpu.write_addr[i] = 0; + m68ki_cpu.write_data[i] = NULL; } - write_ranges = 0; - read_ranges = 0; + m68ki_cpu.write_ranges = 0; + m68ki_cpu.read_ranges = 0; + m68ki_cpu.code_translation_cache.lower = 0; + m68ki_cpu.code_translation_cache.upper = 0; } /* ======================================================================== */ diff --git a/m68kcpu.h b/m68kcpu.h index d943f94c..b5b91974 100644 --- a/m68kcpu.h +++ b/m68kcpu.h @@ -937,6 +937,15 @@ typedef union double f; } fp_reg; +typedef struct +{ + unsigned int lower; + unsigned int upper; + unsigned char *offset; +} address_translation_cache; + + + typedef struct { uint cpu_type; /* CPU Type: 68000, 68008, 68010, 68EC020, 68020, 68EC030, 68030, 68EC040, or 68040 */ @@ -1042,6 +1051,19 @@ typedef struct void (*set_fc_callback)(unsigned int new_fc); /* Called when the CPU function code changes */ void (*instr_hook_callback)(unsigned int pc); /* Called every instruction cycle prior to execution */ + /* address translation caches */ + + unsigned char read_ranges; + unsigned int read_addr[8]; + unsigned int read_upper[8]; + unsigned char *read_data[8]; + 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; + + } m68ki_cpu_core; @@ -1075,24 +1097,6 @@ char* m68ki_disassemble_quick(unsigned int pc, unsigned int cpu_type); /* ---------------------------- Read Immediate ---------------------------- */ -typedef struct -{ - unsigned int lower; - unsigned int upper; - unsigned char *offset; -} address_translation_cache; - - -extern unsigned char read_ranges; -extern unsigned int read_addr[8]; -extern unsigned int read_upper[8]; -extern unsigned char *read_data[8]; -extern unsigned char write_ranges; -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() @@ -1169,13 +1173,13 @@ static inline uint m68ki_read_imm_16(void) { uint32_t pc = REG_PC; - address_translation_cache *cache = &code_translation_cache; - if(pc >= cache->lower && pc < cache->upper) - { - REG_PC += 2; - return be16toh(((unsigned short *)(cache->offset + pc))[0]); - } - return m68ki_read_imm6_addr_slowpath(pc, cache); + address_translation_cache *cache = &m68ki_cpu.code_translation_cache; + if(pc >= cache->lower && pc < cache->upper) + { + REG_PC += 2; + return be16toh(((unsigned short *)(cache->offset + pc))[0]); + } + return m68ki_read_imm6_addr_slowpath(pc, cache); } static inline uint m68ki_read_imm_8(void) @@ -1193,10 +1197,10 @@ static inline uint m68ki_read_imm_32(void) #endif #endif uint32_t address = ADDRESS_68K(REG_PC); - for (int i = 0; i < read_ranges; i++) { - if(address >= read_addr[i] && address < read_upper[i]) { + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { REG_PC += 4; - return be32toh(((unsigned int *)(read_data[i] + (address - read_addr[i])))[0]); + return be32toh(((unsigned int *)(m68ki_cpu.read_data[i] + (address - m68ki_cpu.read_addr[i])))[0]); } } @@ -1254,9 +1258,9 @@ static inline uint m68ki_read_8_fc(uint address, uint fc) address = pmmu_translate_addr(address,1); #endif - for (int i = 0; i < read_ranges; i++) { - if(address >= read_addr[i] && address < read_upper[i]) { - return read_data[i][address - read_addr[i]]; + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { + return m68ki_cpu.read_data[i][address - m68ki_cpu.read_addr[i]]; } } @@ -1275,9 +1279,9 @@ static inline uint m68ki_read_16_fc(uint address, uint fc) address = pmmu_translate_addr(address,1); #endif - 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]); + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { + return be16toh(((unsigned short *)(m68ki_cpu.read_data[i] + (address - m68ki_cpu.read_addr[i])))[0]); } } @@ -1296,9 +1300,9 @@ static inline uint m68ki_read_32_fc(uint address, uint fc) address = pmmu_translate_addr(address,1); #endif - for (int i = 0; i < read_ranges; i++) { - if(address >= read_addr[i] && address < read_upper[i]) { - return be32toh(((unsigned int *)(read_data[i] + (address - read_addr[i])))[0]); + for (int i = 0; i < m68ki_cpu.read_ranges; i++) { + if(address >= m68ki_cpu.read_addr[i] && address < m68ki_cpu.read_upper[i]) { + return be32toh(((unsigned int *)(m68ki_cpu.read_data[i] + (address - m68ki_cpu.read_addr[i])))[0]); } } @@ -1317,9 +1321,9 @@ static inline void m68ki_write_8_fc(uint address, uint fc, uint value) address = pmmu_translate_addr(address,0); #endif - for (int i = 0; i < write_ranges; i++) { - if(address >= write_addr[i] && address < write_upper[i]) { - write_data[i][address - write_addr[i]] = (unsigned char)value; + for (int i = 0; i < m68ki_cpu.write_ranges; i++) { + if(address >= m68ki_cpu.write_addr[i] && address < m68ki_cpu.write_upper[i]) { + m68ki_cpu.write_data[i][address - m68ki_cpu.write_addr[i]] = (unsigned char)value; return; } } @@ -1339,9 +1343,9 @@ static inline void m68ki_write_16_fc(uint address, uint fc, uint value) address = pmmu_translate_addr(address,0); #endif - for (int i = 0; i < write_ranges; i++) { - if(address >= write_addr[i] && address < write_upper[i]) { - ((short *)(write_data[i] + (address - write_addr[i])))[0] = htobe16(value); + for (int i = 0; i < m68ki_cpu.write_ranges; i++) { + if(address >= m68ki_cpu.write_addr[i] && address < m68ki_cpu.write_upper[i]) { + ((short *)(m68ki_cpu.write_data[i] + (address - m68ki_cpu.write_addr[i])))[0] = htobe16(value); return; } } @@ -1361,9 +1365,9 @@ static inline void m68ki_write_32_fc(uint address, uint fc, uint value) address = pmmu_translate_addr(address,0); #endif - for (int i = 0; i < write_ranges; i++) { - if(address >= write_addr[i] && address < write_upper[i]) { - ((int *)(write_data[i] + (address - write_addr[i])))[0] = htobe32(value); + for (int i = 0; i < m68ki_cpu.write_ranges; i++) { + if(address >= m68ki_cpu.write_addr[i] && address < m68ki_cpu.write_upper[i]) { + ((int *)(m68ki_cpu.write_data[i] + (address - m68ki_cpu.write_addr[i])))[0] = htobe32(value); return; } }