From 7096342f719470fff7b18111f1b7513ad3cd4617 Mon Sep 17 00:00:00 2001 From: beeanyew Date: Sat, 26 Dec 2020 22:54:10 +0100 Subject: [PATCH] Some minor optimizations, hopefully fewer jumps and such --- .gitignore | 1 + config_file/config_file.h | 3 + emulator.c | 107 ++++++++++++++---------------- memory_mapped.c | 4 +- platforms/amiga/amiga-platform.c | 26 +++++++- platforms/amiga/amiga-registers.c | 84 +++++++++++------------ platforms/amiga/amiga-registers.h | 8 +++ 7 files changed, 128 insertions(+), 105 deletions(-) diff --git a/.gitignore b/.gitignore index da773f7..cccd3bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.o +*.img /m68kmake /m68kmake.exe /m68kops.c diff --git a/config_file/config_file.h b/config_file/config_file.h index c66fd80..81f2956 100644 --- a/config_file/config_file.h +++ b/config_file/config_file.h @@ -64,10 +64,13 @@ struct emulator_config { unsigned char mouse_enabled, keyboard_enabled; unsigned int loop_cycles; + unsigned int map_low, map_high; + unsigned int custom_low, custom_high; }; struct platform_config { char *subsys; + unsigned char id; int (*custom_read)(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type); int (*custom_write)(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type); diff --git a/emulator.c b/emulator.c index 39648f3..5c1c013 100644 --- a/emulator.c +++ b/emulator.c @@ -21,6 +21,8 @@ #include "platforms/platforms.h" #include "input/input.h" +#include "platforms/amiga/amiga-registers.h" + //#define BCM2708_PERI_BASE 0x20000000 //pi0-1 //#define BCM2708_PERI_BASE 0xFE000000 //pi4 #define BCM2708_PERI_BASE 0x3F000000 // pi3 @@ -461,16 +463,27 @@ int cpu_irq_ack(int level) { static unsigned int target = 0; -unsigned int m68k_read_memory_8(unsigned int address) { - if (cfg->platform->custom_read && cfg->platform->custom_read(cfg, address, &target, OP_TYPE_BYTE) != -1) { - return target; - } +#define PLATFORM_CHECK_READ(a) \ + if (address >= cfg->custom_low && address < cfg->custom_high) { \ + unsigned int target = 0; \ + switch(cfg->platform->id) { \ + case PLATFORM_AMIGA: { \ + if (custom_read_amiga(cfg, address, &target, a) != -1) { \ + return target; \ + } \ + break; \ + } \ + default: \ + break; \ + } \ + } \ + if (ovl || (address >= cfg->map_low && address < cfg->map_high)) { \ + if (handle_mapped_read(cfg, address, &target, a, ovl) != -1) \ + return target; \ + } \ - if (cfg) { - int ret = handle_mapped_read(cfg, address, &target, OP_TYPE_BYTE, ovl); - if (ret != -1) - return target; - } +unsigned int m68k_read_memory_8(unsigned int address) { + PLATFORM_CHECK_READ(OP_TYPE_BYTE); address &=0xFFFFFF; // if (address < 0xffffff) { @@ -481,15 +494,7 @@ unsigned int m68k_read_memory_8(unsigned int address) { } unsigned int m68k_read_memory_16(unsigned int address) { - if (cfg->platform->custom_read && cfg->platform->custom_read(cfg, address, &target, OP_TYPE_WORD) != -1) { - return target; - } - - if (cfg) { - int ret = handle_mapped_read(cfg, address, &target, OP_TYPE_WORD, ovl); - if (ret != -1) - return target; - } + PLATFORM_CHECK_READ(OP_TYPE_WORD); if (mouse_hook_enabled) { if (address == JOY0DAT) { @@ -527,15 +532,7 @@ unsigned int m68k_read_memory_16(unsigned int address) { } unsigned int m68k_read_memory_32(unsigned int address) { - if (cfg->platform->custom_read && cfg->platform->custom_read(cfg, address, &target, OP_TYPE_LONGWORD) != -1) { - return target; - } - - if (cfg) { - int ret = handle_mapped_read(cfg, address, &target, OP_TYPE_LONGWORD, ovl); - if (ret != -1) - return target; - } + PLATFORM_CHECK_READ(OP_TYPE_LONGWORD); // if (address < 0xffffff) { address &=0xFFFFFF; @@ -547,16 +544,26 @@ unsigned int m68k_read_memory_32(unsigned int address) { // return 1; } -void m68k_write_memory_8(unsigned int address, unsigned int value) { - if (cfg->platform->custom_write && cfg->platform->custom_write(cfg, address, value, OP_TYPE_BYTE) != -1) { - return; - } +#define PLATFORM_CHECK_WRITE(a) \ + if (address >= cfg->custom_low && address < cfg->custom_high) { \ + switch(cfg->platform->id) { \ + case PLATFORM_AMIGA: { \ + if (custom_write_amiga(cfg, address, value, OP_TYPE_BYTE) != -1) { \ + return; \ + } \ + break; \ + } \ + default: \ + break; \ + } \ + } \ + if (address >= cfg->map_low && address < cfg->map_high) { \ + if (handle_mapped_write(cfg, address, value, OP_TYPE_BYTE, ovl) != -1) \ + return; \ + } \ - if (cfg) { - int ret = handle_mapped_write(cfg, address, value, OP_TYPE_BYTE, ovl); - if (ret != -1) - return; - } +void m68k_write_memory_8(unsigned int address, unsigned int value) { + PLATFORM_CHECK_WRITE(OP_TYPE_BYTE); if (address == 0xbfe001) { ovl = (value & (1 << 0)); @@ -573,15 +580,7 @@ void m68k_write_memory_8(unsigned int address, unsigned int value) { } void m68k_write_memory_16(unsigned int address, unsigned int value) { - if (cfg->platform->custom_write && cfg->platform->custom_write(cfg, address, value, OP_TYPE_WORD) != -1) { - return; - } - - if (cfg) { - int ret = handle_mapped_write(cfg, address, value, OP_TYPE_WORD, ovl); - if (ret != -1) - return; - } + PLATFORM_CHECK_WRITE(OP_TYPE_WORD); // if (address < 0xffffff) { address &=0xFFFFFF; @@ -592,15 +591,7 @@ void m68k_write_memory_16(unsigned int address, unsigned int value) { } void m68k_write_memory_32(unsigned int address, unsigned int value) { - if (cfg->platform->custom_write && cfg->platform->custom_write(cfg, address, value, OP_TYPE_LONGWORD) != -1) { - return; - } - - if (cfg) { - int ret = handle_mapped_write(cfg, address, value, OP_TYPE_LONGWORD, ovl); - if (ret != -1) - return; - } + PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD); // if (address < 0xffffff) { address &=0xFFFFFF; @@ -612,7 +603,7 @@ void m68k_write_memory_32(unsigned int address, unsigned int value) { // return; } -void write16(uint32_t address, uint32_t data) { +inline void write16(uint32_t address, uint32_t data) { uint32_t addr_h_s = (address & 0x0000ffff) << 8; uint32_t addr_h_r = (~address & 0x0000ffff) << 8; uint32_t addr_l_s = (address >> 16) << 8; @@ -650,7 +641,7 @@ void write16(uint32_t address, uint32_t data) { // asm volatile ("dmb" ::: "memory"); } -void write8(uint32_t address, uint32_t data) { +inline void write8(uint32_t address, uint32_t data) { if ((address & 1) == 0) data = data + (data << 8); // EVEN, A0=0,UDS else @@ -692,7 +683,7 @@ void write8(uint32_t address, uint32_t data) { // asm volatile ("dmb" ::: "memory"); } -uint32_t read16(uint32_t address) { +inline uint32_t read16(uint32_t address) { volatile int val; uint32_t addr_h_s = (address & 0x0000ffff) << 8; uint32_t addr_h_r = (~address & 0x0000ffff) << 8; @@ -729,7 +720,7 @@ uint32_t read16(uint32_t address) { return (val >> 8) & 0xffff; } -uint32_t read8(uint32_t address) { +inline uint32_t read8(uint32_t address) { int val; uint32_t addr_h_s = (address & 0x0000ffff) << 8; uint32_t addr_h_r = (~address & 0x0000ffff) << 8; diff --git a/memory_mapped.c b/memory_mapped.c index e2dd9a4..ae9bf2a 100644 --- a/memory_mapped.c +++ b/memory_mapped.c @@ -15,7 +15,7 @@ const char *op_type_names[OP_TYPE_NUM] = { "MEM", }; -int handle_mapped_read(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type, unsigned char mirror) { +inline int handle_mapped_read(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type, unsigned char mirror) { unsigned char *read_addr = NULL; char handle_regs = 0; @@ -82,7 +82,7 @@ int handle_mapped_read(struct emulator_config *cfg, unsigned int addr, unsigned return -1; } -int handle_mapped_write(struct emulator_config *cfg, unsigned int addr, unsigned int value, unsigned char type, unsigned char mirror) { +inline int handle_mapped_write(struct emulator_config *cfg, unsigned int addr, unsigned int value, unsigned char type, unsigned char mirror) { unsigned char *write_addr = NULL; char handle_regs = 0; diff --git a/platforms/amiga/amiga-platform.c b/platforms/amiga/amiga-platform.c index 4fe96e1..21a2c62 100644 --- a/platforms/amiga/amiga-platform.c +++ b/platforms/amiga/amiga-platform.c @@ -17,6 +17,7 @@ extern int ac_z3_pic_count; extern int ac_z3_done; extern int ac_z3_type[AC_PIC_LIMIT]; extern int ac_z3_index[AC_PIC_LIMIT]; +extern int gayle_emulation_enabled; char *z2_autoconf_id = "z2_autoconf_fast"; char *z2_autoconf_zap_id = "^2_autoconf_fast"; @@ -25,7 +26,7 @@ char *z3_autoconf_zap_id = "^3_autoconf_fast"; extern const char *op_type_names[OP_TYPE_NUM]; -int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) { +inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type) { if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) { if (ac_z2_pic_count == 0) { ac_z2_done = 1; @@ -56,7 +57,7 @@ int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned i return -1; } -int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) { +inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type) { if (!ac_z2_done && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) { if (type == OP_TYPE_BYTE) { if (ac_z2_pic_count == 0) { @@ -156,11 +157,29 @@ int setup_platform_amiga(struct emulator_config *cfg) { index = get_named_mapped_item(cfg, z3_autoconf_id); if (index != -1) goto more_z3_fast; - for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i ++) { + for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) { if (cfg->map_id[i] && strcmp(cfg->map_id[i], z3_autoconf_zap_id) == 0) { cfg->map_id[i][0] = z3_autoconf_id[0]; } } + + // Set up the min/max ranges for mapped reads/writes + for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) { + if (cfg->map_type[i] != MAPTYPE_NONE) { + if ((cfg->map_offset[i] != 0 && cfg->map_offset[i] < cfg->map_low) || cfg->map_low == 0) + cfg->map_low = cfg->map_offset[i]; + if (cfg->map_offset[i] + cfg->map_size[i] > cfg->map_high) + cfg->map_high = cfg->map_offset[i] + cfg->map_size[i]; + } + } + + if (gayle_emulation_enabled) { + cfg->custom_low = GAYLEBASE; + cfg->custom_high = GAYLEBASE + GAYLESIZE; + } + + printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high); + printf("Platform mapped range: %.8X-%.8X\n", cfg->map_low, cfg->map_high); return 0; } @@ -194,6 +213,7 @@ void create_platform_amiga(struct platform_config *cfg, char *subsys) { cfg->platform_initial_setup = setup_platform_amiga; cfg->setvar = setvar_amiga; + cfg->id = PLATFORM_AMIGA; if (subsys) { cfg->subsys = malloc(strlen(subsys) + 1); diff --git a/platforms/amiga/amiga-registers.c b/platforms/amiga/amiga-registers.c index 96aa653..d5e3b69 100644 --- a/platforms/amiga/amiga-registers.c +++ b/platforms/amiga/amiga-registers.c @@ -1,13 +1,9 @@ #include "../../Gayle.h" #include "../../config_file/config_file.h" - -#define GAYLEBASE 0xD80000 // D7FFFF -#define GAYLESIZE 0x6FFFF - -#define CLOCKBASE 0xDC0000 -#define CLOCKSIZE 0x010000 +#include "amiga-registers.h" uint8_t rtc_emulation_enabled = 1; +extern int gayle_emulation_enabled; void configure_rtc_emulation_amiga(uint8_t enabled) { if (enabled == rtc_emulation_enabled) @@ -18,50 +14,54 @@ void configure_rtc_emulation_amiga(uint8_t enabled) { } int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val) { - if (!rtc_emulation_enabled && addr >= CLOCKBASE && addr < CLOCKBASE + CLOCKSIZE) - return -1; - if (addr >= GAYLEBASE && addr < GAYLEBASE + GAYLESIZE) { - switch(type) { - case OP_TYPE_BYTE: - *val = readGayleB(addr); - return 1; - break; - case OP_TYPE_WORD: - *val = readGayle(addr); - return 1; - break; - case OP_TYPE_LONGWORD: - *val = readGayleL(addr); - return 1; - break; - case OP_TYPE_MEM: + if (gayle_emulation_enabled) { + if (!rtc_emulation_enabled && addr >= CLOCKBASE && addr < CLOCKBASE + CLOCKSIZE) return -1; - break; + if (addr >= GAYLEBASE && addr < GAYLEBASE + GAYLESIZE) { + switch(type) { + case OP_TYPE_BYTE: + *val = readGayleB(addr); + return 1; + break; + case OP_TYPE_WORD: + *val = readGayle(addr); + return 1; + break; + case OP_TYPE_LONGWORD: + *val = readGayleL(addr); + return 1; + break; + case OP_TYPE_MEM: + return -1; + break; + } } } return -1; } int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type) { - if (!rtc_emulation_enabled && addr >= CLOCKBASE && addr < CLOCKBASE + CLOCKSIZE) - return -1; - if (addr >= GAYLEBASE && addr < GAYLEBASE + GAYLESIZE) { - switch(type) { - case OP_TYPE_BYTE: - writeGayleB(addr, value); - return 1; - break; - case OP_TYPE_WORD: - writeGayle(addr, value); - return 1; - break; - case OP_TYPE_LONGWORD: - writeGayleL(addr, value); - return 1; - break; - case OP_TYPE_MEM: + if (gayle_emulation_enabled) { + if (!rtc_emulation_enabled && addr >= CLOCKBASE && addr < CLOCKBASE + CLOCKSIZE) return -1; - break; + if (addr >= GAYLEBASE && addr < GAYLEBASE + GAYLESIZE) { + switch(type) { + case OP_TYPE_BYTE: + writeGayleB(addr, value); + return 1; + break; + case OP_TYPE_WORD: + writeGayle(addr, value); + return 1; + break; + case OP_TYPE_LONGWORD: + writeGayleL(addr, value); + return 1; + break; + case OP_TYPE_MEM: + return -1; + break; + } } } return -1; diff --git a/platforms/amiga/amiga-registers.h b/platforms/amiga/amiga-registers.h index df010da..53a7b90 100644 --- a/platforms/amiga/amiga-registers.h +++ b/platforms/amiga/amiga-registers.h @@ -1,2 +1,10 @@ void configure_rtc_emulation_amiga(uint8_t enabled); void set_hard_drive_image_file_amiga(uint8_t index, char *filename); +int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type); +int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, unsigned int val, unsigned char type); + +#define GAYLEBASE 0xD80000 +#define GAYLESIZE 0x70000 + +#define CLOCKBASE 0xDC0000 +#define CLOCKSIZE 0x010000 -- 2.39.2