From: beeanyew Date: Mon, 28 Dec 2020 15:13:25 +0000 (+0100) Subject: More tiny optimizations, some Z2/Z3 fixes X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=20b1a1fa0b8142b5be8e8dbbbbd6d71a9f2a931e;p=pistorm More tiny optimizations, some Z2/Z3 fixes Also disables keyboard input for now, until more investigating can be done. --- diff --git a/config_file/config_file.c b/config_file/config_file.c index 1d41335..76d0bb6 100644 --- a/config_file/config_file.c +++ b/config_file/config_file.c @@ -185,6 +185,7 @@ void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int ad cfg->map_type[index] = type; cfg->map_offset[index] = addr; cfg->map_size[index] = size; + cfg->map_high[index] = addr + size; cfg->map_mirror[index] = mirr_addr; if (strlen(map_id)) { cfg->map_id[index] = (char *)malloc(strlen(map_id) + 1); diff --git a/config_file/config_file.h b/config_file/config_file.h index 81f2956..2e8cbae 100644 --- a/config_file/config_file.h +++ b/config_file/config_file.h @@ -50,6 +50,7 @@ struct emulator_config { unsigned char map_type[MAX_NUM_MAPPED_ITEMS]; long map_offset[MAX_NUM_MAPPED_ITEMS]; + long map_high[MAX_NUM_MAPPED_ITEMS]; unsigned int map_size[MAX_NUM_MAPPED_ITEMS]; unsigned int rom_size[MAX_NUM_MAPPED_ITEMS]; unsigned char *map_data[MAX_NUM_MAPPED_ITEMS]; @@ -64,7 +65,7 @@ struct emulator_config { unsigned char mouse_enabled, keyboard_enabled; unsigned int loop_cycles; - unsigned int map_low, map_high; + unsigned int mapped_low, mapped_high; unsigned int custom_low, custom_high; }; @@ -79,13 +80,14 @@ struct platform_config { int (*register_write)(unsigned int addr, unsigned int value, unsigned char type); int (*platform_initial_setup)(struct emulator_config *cfg); + void (*handle_reset)(struct emulator_config *cfg); void (*setvar)(char *var, char *val); }; unsigned int get_m68k_cpu_type(char *name); struct emulator_config *load_config_file(char *filename); -int handle_mapped_read(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type, unsigned char mirror); -int handle_mapped_write(struct emulator_config *cfg, unsigned int addr, unsigned int value, unsigned char type, unsigned char mirror); +int handle_mapped_read(struct emulator_config *cfg, unsigned int addr, unsigned int *val, unsigned char type); +int handle_mapped_write(struct emulator_config *cfg, unsigned int addr, unsigned int value, unsigned char type); int get_named_mapped_item(struct emulator_config *cfg, char *name); unsigned int get_int(char *str); diff --git a/emulator.c b/emulator.c index 5c1c013..a019512 100644 --- a/emulator.c +++ b/emulator.c @@ -152,7 +152,7 @@ volatile uint32_t srdata2_old; //unsigned char g_kick[524288]; //unsigned char g_ram[FASTSIZE + 1]; /* RAM */ unsigned char toggle; -static volatile unsigned char ovl; +int ovl; static volatile unsigned char maprom; void sigint_handler(int sig_num) { @@ -383,7 +383,7 @@ int main(int argc, char *argv[]) { m68k_execute(loop_cycles); // FIXME: Rework this to use keyboard events instead. - while (get_key_char(&c)) { + /*while (get_key_char(&c)) { if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) { kb_hook_enabled = 1; printf("Keyboard hook enabled.\n"); @@ -412,7 +412,7 @@ int main(int argc, char *argv[]) { goto stop_cpu_emulation; } } - } + }*/ /* if (toggle == 1){ srdata = read_reg(); @@ -477,10 +477,10 @@ static unsigned int target = 0; break; \ } \ } \ - if (ovl || (address >= cfg->map_low && address < cfg->map_high)) { \ - if (handle_mapped_read(cfg, address, &target, a, ovl) != -1) \ + if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \ + if (handle_mapped_read(cfg, address, &target, a) != -1) \ return target; \ - } \ + } unsigned int m68k_read_memory_8(unsigned int address) { PLATFORM_CHECK_READ(OP_TYPE_BYTE); @@ -548,7 +548,7 @@ unsigned int m68k_read_memory_32(unsigned int address) { 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) { \ + if (custom_write_amiga(cfg, address, value, a) != -1) { \ return; \ } \ break; \ @@ -557,10 +557,10 @@ unsigned int m68k_read_memory_32(unsigned int address) { break; \ } \ } \ - if (address >= cfg->map_low && address < cfg->map_high) { \ - if (handle_mapped_write(cfg, address, value, OP_TYPE_BYTE, ovl) != -1) \ + if (address >= cfg->mapped_low && address < cfg->mapped_high) { \ + if (handle_mapped_write(cfg, address, value, a) != -1) \ return; \ - } \ + } void m68k_write_memory_8(unsigned int address, unsigned int value) { PLATFORM_CHECK_WRITE(OP_TYPE_BYTE); diff --git a/memory_mapped.c b/memory_mapped.c index ae9bf2a..bf42406 100644 --- a/memory_mapped.c +++ b/memory_mapped.c @@ -4,8 +4,10 @@ #include #define CHKRANGE(a, b, c) a >= (unsigned int)b && a < (unsigned int)(b + c) +#define CHKRANGE_ABS(a, b, c) a >= (unsigned int)b && a < (unsigned int) c static unsigned int target; +extern int ovl; extern const char *map_type_names[MAPTYPE_NUM]; const char *op_type_names[OP_TYPE_NUM] = { @@ -15,64 +17,35 @@ const char *op_type_names[OP_TYPE_NUM] = { "MEM", }; -inline 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 *read_addr = NULL; - char handle_regs = 0; - - //printf("Mapped read: %.8x\n", addr); for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) { if (cfg->map_type[i] == MAPTYPE_NONE) continue; - switch(cfg->map_type[i]) { - case MAPTYPE_ROM: - if (CHKRANGE(addr, cfg->map_offset[i], cfg->map_size[i])) - read_addr = cfg->map_data[i] + ((addr - cfg->map_offset[i]) % cfg->rom_size[i]); - else if (cfg->map_mirror[i] != -1 && mirror && CHKRANGE(addr, cfg->map_mirror[i], cfg->map_size[i])) - read_addr = cfg->map_data[i] + ((addr - cfg->map_mirror[i]) % cfg->rom_size[i]); - break; - case MAPTYPE_RAM: - if (CHKRANGE(addr, cfg->map_offset[i], cfg->map_size[i])) - read_addr = cfg->map_data[i] + (addr - cfg->map_offset[i]); - break; - case MAPTYPE_REGISTER: - if (CHKRANGE(addr, cfg->map_offset[i], cfg->map_size[i])) - handle_regs = 1; - break; - } - - if (!read_addr && !handle_regs) - continue; - - if (handle_regs) { - if (cfg->platform && cfg->platform->register_read) { - if (cfg->platform->register_read(addr, type, &target) != -1) { - *val = target; - return 1; - } + else if (ovl && cfg->map_type[i] == MAPTYPE_ROM) { + if (cfg->map_mirror[i] != -1 && CHKRANGE_ABS(addr, cfg->map_mirror[i], cfg->map_high[i])) { + read_addr = cfg->map_data[i] + ((addr - cfg->map_mirror[i]) % cfg->rom_size[i]); + goto read_value; } - return -1; } - else if (read_addr) { - //printf("[PC: %.8X] Read %s from %s (%.8X) (%d)\n", m68k_get_reg(NULL, M68K_REG_PC), op_type_names[type], map_type_names[cfg->map_type[i]], addr, mirror); - //printf("Readaddr: %.8lX (Base %.8lX\n", (uint64_t)(read_addr), (uint64_t)cfg->map_data[i]); - switch(type) { - case OP_TYPE_BYTE: - *val = read_addr[0]; - //printf("Read val: %.8lX (%d)\n", (uint64_t)val, *val); - return 1; - break; - case OP_TYPE_WORD: - *val = be16toh(((unsigned short *)read_addr)[0]); - //printf("Read val: %.8lX (%d)\n", (uint64_t)val, *val); - return 1; + else if (CHKRANGE_ABS(addr, cfg->map_offset[i], cfg->map_high[i])) { + switch(cfg->map_type[i]) { + case MAPTYPE_ROM: + read_addr = cfg->map_data[i] + ((addr - cfg->map_offset[i]) % cfg->rom_size[i]); + goto read_value; break; - case OP_TYPE_LONGWORD: - *val = be32toh(((unsigned int *)read_addr)[0]); - //printf("Read val: %.8lX (%d)\n", (uint64_t)val, *val); - return 1; + case MAPTYPE_RAM: + read_addr = cfg->map_data[i] + (addr - cfg->map_offset[i]); + goto read_value; break; - case OP_TYPE_MEM: + case MAPTYPE_REGISTER: + if (cfg->platform && cfg->platform->register_read) { + if (cfg->platform->register_read(addr, type, &target) != -1) { + *val = target; + return 1; + } + } return -1; break; } @@ -80,66 +53,75 @@ inline int handle_mapped_read(struct emulator_config *cfg, unsigned int addr, un } return -1; + +read_value:; + //printf("Read value from %.8X\n", addr); + switch(type) { + case OP_TYPE_BYTE: + *val = read_addr[0]; + return 1; + break; + case OP_TYPE_WORD: + *val = be16toh(((unsigned short *)read_addr)[0]); + return 1; + break; + case OP_TYPE_LONGWORD: + *val = be32toh(((unsigned int *)read_addr)[0]); + return 1; + break; + case OP_TYPE_MEM: + return -1; + break; + } + + return 1; } -inline 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 *write_addr = NULL; - char handle_regs = 0; - - //printf("Mapped write: %.8x\n", addr); - if (mirror) { } for (int i = 0; i < MAX_NUM_MAPPED_ITEMS; i++) { if (cfg->map_type[i] == MAPTYPE_NONE) continue; - switch(cfg->map_type[i]) { - case MAPTYPE_ROM: - if (CHKRANGE(addr, cfg->map_offset[i], cfg->map_size[i])) - return 1; - break; - case MAPTYPE_RAM: - if (CHKRANGE(addr, cfg->map_offset[i], cfg->map_size[i])) - write_addr = cfg->map_data[i] + (addr - cfg->map_offset[i]); - break; - case MAPTYPE_REGISTER: - if (CHKRANGE(addr, cfg->map_offset[i], cfg->map_size[i])) - handle_regs = 1; - break; - } - - if (!write_addr && !handle_regs) - continue; - - if (handle_regs) { - if (cfg->platform && cfg->platform->register_write) { - return cfg->platform->register_write(addr, value, type); - } - } - else if (write_addr) { - //printf("[PC: %.8X] Write %s to %s (%.8X) (%d)\n", m68k_get_reg(NULL, M68K_REG_PC), op_type_names[type], map_type_names[cfg->map_type[i]], addr, mirror); - //printf("Writeaddr: %.8lX (Base %.8lX\n", (uint64_t)(write_addr), (uint64_t)cfg->map_data[i]); - switch(type) { - case OP_TYPE_BYTE: - write_addr[0] = (unsigned char)value; - //printf("Write val: %.8X (%d)\n", (uint32_t)value, value); + else if (CHKRANGE_ABS(addr, cfg->map_offset[i], cfg->map_high[i])) { + switch(cfg->map_type[i]) { + case MAPTYPE_ROM: return 1; break; - case OP_TYPE_WORD: - ((short *)write_addr)[0] = htobe16(value); - //printf("Write val: %.8X (%d)\n", (uint32_t)value, value); - return 1; - break; - case OP_TYPE_LONGWORD: - ((int *)write_addr)[0] = htobe32(value); - //printf("Write val: %.8X (%d)\n", (uint32_t)value, value); - return 1; + case MAPTYPE_RAM: + write_addr = cfg->map_data[i] + (addr - cfg->map_offset[i]); + goto write_value; break; - case OP_TYPE_MEM: - return -1; + case MAPTYPE_REGISTER: + if (cfg->platform && cfg->platform->register_write) { + return cfg->platform->register_write(addr, value, type); + } break; } } } return -1; + +write_value:; + //printf("Write value to %.8X\n", addr); + switch(type) { + case OP_TYPE_BYTE: + write_addr[0] = (unsigned char)value; + return 1; + break; + case OP_TYPE_WORD: + ((short *)write_addr)[0] = htobe16(value); + return 1; + break; + case OP_TYPE_LONGWORD: + ((int *)write_addr)[0] = htobe32(value); + return 1; + break; + case OP_TYPE_MEM: + return -1; + break; + } + + return 1; } diff --git a/platforms/amiga/amiga-autoconf.c b/platforms/amiga/amiga-autoconf.c index 913d6c2..b27c0ec 100644 --- a/platforms/amiga/amiga-autoconf.c +++ b/platforms/amiga/amiga-autoconf.c @@ -66,6 +66,8 @@ unsigned char get_autoconf_size_ext(int size) { return AC_MEM_SIZE_EXT_64MB; } +extern void adjust_ranges_amiga(struct emulator_config *cfg); + unsigned int autoconfig_read_memory_z3_8(struct emulator_config *cfg, unsigned int address_) { int address = address_ - AC_Z3_BASE; int index = ac_z3_index[ac_z3_current_pic]; @@ -180,9 +182,12 @@ void autoconfig_write_memory_z3_8(struct emulator_config *cfg, unsigned int addr nib_latch = 0; printf("Address of Z3 autoconf RAM assigned to $%.8x\n", ac_base[ac_z3_current_pic]); cfg->map_offset[index] = ac_base[ac_z3_current_pic]; + cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index]; ac_z3_current_pic++; - if (ac_z3_current_pic == ac_z3_pic_count) + if (ac_z3_current_pic == ac_z3_pic_count) { ac_z3_done = 1; + adjust_ranges_amiga(cfg); + } } return; @@ -255,6 +260,7 @@ unsigned int autoconfig_read_memory_8(struct emulator_config *cfg, unsigned int void autoconfig_write_memory_8(struct emulator_config *cfg, unsigned int address_, unsigned int value) { int address = address_ - AC_Z2_BASE; int done = 0; + int index = ac_z2_index[ac_z2_current_pic]; unsigned int *base = NULL; @@ -292,9 +298,13 @@ void autoconfig_write_memory_8(struct emulator_config *cfg, unsigned int address if (done) { printf("Address of Z2 autoconf RAM assigned to $%.8x\n", ac_base[ac_z2_current_pic]); - cfg->map_offset[ac_z2_index[ac_z2_current_pic]] = ac_base[ac_z2_current_pic]; + cfg->map_offset[index] = ac_base[ac_z2_current_pic]; + cfg->map_high[index] = cfg->map_offset[index] + cfg->map_size[index]; + printf("Z2 PIC %d at $%.8lX-%.8lX, Size: %d MB\n", ac_z2_current_pic, cfg->map_offset[index], cfg->map_high[index], cfg->map_size[index] / SIZE_MEGA); ac_z2_current_pic++; - if (ac_z2_current_pic == ac_z2_pic_count) + if (ac_z2_current_pic == ac_z2_pic_count) { ac_z2_done = 1; + adjust_ranges_amiga(cfg); + } } } diff --git a/platforms/amiga/amiga-platform.c b/platforms/amiga/amiga-platform.c index 21a2c62..c2361e3 100644 --- a/platforms/amiga/amiga-platform.c +++ b/platforms/amiga/amiga-platform.c @@ -26,6 +26,9 @@ char *z3_autoconf_zap_id = "^3_autoconf_fast"; extern const char *op_type_names[OP_TYPE_NUM]; +#define min(a, b) (a < b) ? a : b +#define max(a, b) (a > b) ? a : b + 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) { @@ -65,7 +68,7 @@ inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, un return -1; } - printf("Write to Z2 autoconf area.\n"); + //printf("Write to Z2 autoconf area.\n"); autoconfig_write_memory_8(cfg, addr, val); return 1; } @@ -95,6 +98,45 @@ inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, un return -1; } +void adjust_ranges_amiga(struct emulator_config *cfg) { + cfg->mapped_high = 0; + cfg->mapped_low = 0; + cfg->custom_high = 0; + cfg->custom_low = 0; + + // Set up the min/max ranges for mapped reads/writes + if (gayle_emulation_enabled) { + cfg->mapped_low = GAYLEBASE; + cfg->mapped_high = GAYLEBASE + GAYLESIZE; + } + 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->mapped_low) || cfg->mapped_low == 0) + cfg->mapped_low = cfg->map_offset[i]; + if (cfg->map_offset[i] + cfg->map_size[i] > cfg->mapped_high) + cfg->mapped_high = cfg->map_offset[i] + cfg->map_size[i]; + } + } + + if (ac_z2_pic_count && !ac_z2_done) { + if (cfg->custom_low == 0) + cfg->custom_low = AC_Z2_BASE; + else + cfg->custom_low = min(cfg->custom_low, AC_Z2_BASE); + cfg->custom_high = max(cfg->custom_high, AC_Z2_BASE + AC_SIZE); + } + if (ac_z3_pic_count && !ac_z3_done) { + if (cfg->custom_low == 0) + cfg->custom_low = AC_Z3_BASE; + else + cfg->custom_low = min(cfg->custom_low, AC_Z3_BASE); + cfg->custom_high = max(cfg->custom_high, AC_Z3_BASE + AC_SIZE); + } + + printf("Platform custom range: %.8X-%.8X\n", cfg->custom_low, cfg->custom_high); + printf("Platform mapped range: %.8X-%.8X\n", cfg->mapped_low, cfg->mapped_high); +} + int setup_platform_amiga(struct emulator_config *cfg) { if (cfg) {} printf("Performing setup for Amiga platform.\n"); @@ -163,23 +205,7 @@ int setup_platform_amiga(struct emulator_config *cfg) { } } - // 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); + adjust_ranges_amiga(cfg); return 0; } @@ -205,12 +231,20 @@ void setvar_amiga(char *var, char *val) { } } +void handle_reset_amiga(struct emulator_config *cfg) { + ac_z3_done = 0; + ac_z2_done = 0; + + adjust_ranges_amiga(cfg); +} + void create_platform_amiga(struct platform_config *cfg, char *subsys) { cfg->register_read = handle_register_read_amiga; cfg->register_write = handle_register_write_amiga; cfg->custom_read = custom_read_amiga; cfg->custom_write = custom_write_amiga; cfg->platform_initial_setup = setup_platform_amiga; + cfg->handle_reset = handle_reset_amiga; cfg->setvar = setvar_amiga; cfg->id = PLATFORM_AMIGA;