From: beeanyew Date: Fri, 21 May 2021 16:53:31 +0000 (+0200) Subject: Add support for automatic ROM range dumping to memory/file X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=5a120a647d9befd58710b38b65a13e06826ff6e8;p=pistorm Add support for automatic ROM range dumping to memory/file --- diff --git a/Makefile b/Makefile index 8ea8cb0..7396390 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,8 @@ MAINFILES = emulator.c \ platforms/amiga/piscsi/piscsi.c \ platforms/amiga/pistorm-dev/pistorm-dev.c \ platforms/amiga/net/pi-net.c \ - platforms/shared/rtc.c + platforms/shared/rtc.c \ + platforms/shared/common.c MUSASHIFILES = m68kcpu.c m68kdasm.c softfloat/softfloat.c softfloat/softfloat_fpsp.c MUSASHIGENCFILES = m68kops.c diff --git a/config_file/config_file.c b/config_file/config_file.c index 0b5d86a..a482041 100644 --- a/config_file/config_file.c +++ b/config_file/config_file.c @@ -51,6 +51,8 @@ const char *mapcmd_names[MAPCMD_NUM] = { "file", "ovl", "id", + "autodump_file", + "autodump_mem", }; int get_config_item_type(char *cmd) { @@ -191,7 +193,7 @@ void get_next_string(char *str, char *str_out, int *strpos, char separator) { } } -void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int addr, unsigned int size, int mirr_addr, char *filename, char *map_id) { +void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int addr, unsigned int size, int mirr_addr, char *filename, char *map_id, unsigned int autodump) { unsigned int index = 0, file_size = 0; FILE *in = NULL; @@ -232,8 +234,27 @@ void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int ad case MAPTYPE_ROM: in = fopen(filename, "rb"); if (!in) { - printf("[CFG] Failed to open file %s for ROM mapping. Using onboard ROM instead, if available.\n", filename); - goto mapping_failed; + if (!autodump) { + printf("[CFG] Failed to open file %s for ROM mapping. Using onboard ROM instead, if available.\n", filename); + goto mapping_failed; + } else if (autodump == MAPCMD_AUTODUMP_FILE) { + printf("[CFG] Could not open file %s for ROM mapping. Autodump flag is set, dumping to file.\n", filename); + dump_range_to_file(cfg->map_offset[index], cfg->map_size[index], filename); + in = fopen(filename, "rb"); + if (in == NULL) { + printf("[CFG] Could not open dumped file for reading. Using onboard ROM instead, if available.\n"); + goto mapping_failed; + } + } else if (autodump == MAPCMD_AUTODUMP_MEM) { + printf("[CFG] Could not open file %s for ROM mapping. Autodump flag is set, dumping to memory.\n", filename); + cfg->map_data[index] = dump_range_to_memory(cfg->map_offset[index], cfg->map_size[index]); + cfg->rom_size[index] = cfg->map_size[index]; + if (cfg->map_data[index] == NULL) { + printf("[CFG] Could not dump range to memory. Using onboard ROM instead, if available.\n"); + goto mapping_failed; + } + goto skip_file_ops; + } } fseek(in, 0, SEEK_END); file_size = (int)ftell(in); @@ -250,7 +271,9 @@ void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int ad } memset(cfg->map_data[index], 0x00, cfg->map_size[index]); fread(cfg->map_data[index], cfg->rom_size[index], 1, in); - fclose(in); + if (in) + fclose(in); +skip_file_ops: displayRomInfo(cfg->map_data[index], cfg->rom_size[index]); if (cfg->map_size[index] == cfg->rom_size[index]) m68k_add_rom_range(cfg->map_offset[index], cfg->map_high[index], cfg->map_data[index]); @@ -348,7 +371,7 @@ struct emulator_config *load_config_file(char *filename) { cfg->cpu_type = get_m68k_cpu_type(parse_line + str_pos); break; case CONFITEM_MAP: { - unsigned int maptype = 0, mapsize = 0, mapaddr = 0; + unsigned int maptype = 0, mapsize = 0, mapaddr = 0, autodump = 0; unsigned int mirraddr = ((unsigned int)-1); char mapfile[128], mapid[128]; memset(mapfile, 0x00, 128); @@ -393,12 +416,16 @@ struct emulator_config *load_config_file(char *filename) { get_next_string(parse_line, cur_cmd, &str_pos, ' '); mirraddr = get_int(cur_cmd); break; + case MAPCMD_AUTODUMP_FILE: + case MAPCMD_AUTODUMP_MEM: + autodump = get_map_cmd(cur_cmd); + break; default: printf("[CFG] Unknown/unhandled map argument %s on line %d.\n", cur_cmd, cur_line); break; } } - add_mapping(cfg, maptype, mapaddr, mapsize, mirraddr, mapfile, mapid); + add_mapping(cfg, maptype, mapaddr, mapsize, mirraddr, mapfile, mapid, autodump); break; } diff --git a/config_file/config_file.h b/config_file/config_file.h index 81d4755..889b0ac 100644 --- a/config_file/config_file.h +++ b/config_file/config_file.h @@ -30,6 +30,8 @@ typedef enum { MAPCMD_FILENAME, MAPCMD_OVL_REMAP, MAPCMD_MAP_ID, + MAPCMD_AUTODUMP_FILE, + MAPCMD_AUTODUMP_MEM, MAPCMD_NUM, } map_cmds; @@ -105,7 +107,7 @@ int handle_mapped_read(struct emulator_config *cfg, unsigned int addr, unsigned 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); int get_mapped_item_by_address(struct emulator_config *cfg, uint32_t address); -void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int addr, unsigned int size, int mirr_addr, char *filename, char *map_id); +void add_mapping(struct emulator_config *cfg, unsigned int type, unsigned int addr, unsigned int size, int mirr_addr, char *filename, char *map_id, unsigned int autodump); unsigned int get_int(char *str); #endif diff --git a/default.cfg b/default.cfg index e624922..0526656 100644 --- a/default.cfg +++ b/default.cfg @@ -2,6 +2,9 @@ cpu 68020 # Map 512KB kickstart ROM to default offset. map type=rom address=0xF80000 size=0x80000 file=kick.rom ovl=0 id=kickstart +# Comment out the line above and uncomment the line below to automatically copy the ROM contents to Pi RAM if the file isn't found +#map type=rom address=0xF80000 size=0x80000 file=kick.rom ovl=0 id=kickstart autodump_mem + # Want to map an extended ROM, such as CDTV or CD32? #map type=rom address=0xF00000 size=0x80000 file=cdtv.rom id=extended diff --git a/emulator.c b/emulator.c index 78ee1a5..0e01014 100644 --- a/emulator.c +++ b/emulator.c @@ -480,6 +480,11 @@ int main(int argc, char *argv[]) { switch_config: srand(clock()); + ps_setup_protocol(); + ps_reset_state_machine(); + ps_pulse_reset(); + usleep(1500); + if (load_new_config != 0) { uint8_t config_action = load_new_config - 1; load_new_config = 0; @@ -569,43 +574,11 @@ switch_config: InitGayle(); signal(SIGINT, sigint_handler); - /*setup_io(); - - //goto skip_everything; - - // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending - // on pi model - printf("Enable 200MHz GPCLK0 on GPIO4\n"); - gpio_enable_200mhz(); - - // reset cpld statemachine first - - write_reg(0x01); - usleep(100); - usleep(1500); - write_reg(0x00); - usleep(100); - - // reset amiga and statemachine - skip_everything:; - usleep(1500); - - m68k_init(); - printf("Setting CPU type to %d.\n", cpu_type); - m68k_set_cpu_type(cpu_type); - cpu_pulse_reset(); - - if (maprom == 1) { - m68k_set_reg(M68K_REG_PC, 0xF80002); - } else { - m68k_set_reg(M68K_REG_PC, 0x0); - }*/ - ps_setup_protocol(); ps_reset_state_machine(); ps_pulse_reset(); - usleep(1500); + m68k_init(); printf("Setting CPU type to %d.\n", cpu_type); m68k_set_cpu_type(cpu_type); diff --git a/platforms/amiga/pistorm-dev/pistorm-dev.c b/platforms/amiga/pistorm-dev/pistorm-dev.c index 332e42d..35e8e93 100644 --- a/platforms/amiga/pistorm-dev/pistorm-dev.c +++ b/platforms/amiga/pistorm-dev/pistorm-dev.c @@ -424,7 +424,7 @@ void handle_pistorm_dev_write(uint32_t addr_, uint32_t val, uint8_t type) { free(cfg->map_id[index]); cfg->map_type[index] = MAPTYPE_NONE; // Dirty hack, I am sleepy and lazy. - add_mapping(cfg, MAPTYPE_ROM, cfg->map_offset[index], cfg->map_size[index], 0, tmp_string, "kickstart"); + add_mapping(cfg, MAPTYPE_ROM, cfg->map_offset[index], cfg->map_size[index], 0, tmp_string, "kickstart", 0); pi_cmd_result = PI_RES_OK; do_reset = 1; } else { @@ -454,7 +454,7 @@ void handle_pistorm_dev_write(uint32_t addr_, uint32_t val, uint8_t type) { free(cfg->map_id[index]); cfg->map_type[index] = MAPTYPE_NONE; // Dirty hack, I am tired and lazy. - add_mapping(cfg, MAPTYPE_ROM, cfg->map_offset[index], cfg->map_size[index], 0, tmp_string, "extended"); + add_mapping(cfg, MAPTYPE_ROM, cfg->map_offset[index], cfg->map_size[index], 0, tmp_string, "extended", 0); pi_cmd_result = PI_RES_OK; do_reset = 1; } else { diff --git a/platforms/amiga/rtg/rtg.c b/platforms/amiga/rtg/rtg.c index 2f82392..564dd50 100644 --- a/platforms/amiga/rtg/rtg.c +++ b/platforms/amiga/rtg/rtg.c @@ -60,7 +60,7 @@ int init_rtg_data(struct emulator_config *cfg_) { } m68k_add_ram_range(PIGFX_RTG_BASE + PIGFX_REG_SIZE, 32 * SIZE_MEGA - PIGFX_REG_SIZE, rtg_mem); - add_mapping(cfg_, MAPTYPE_RAM_NOALLOC, PIGFX_RTG_BASE + PIGFX_REG_SIZE, 40 * SIZE_MEGA - PIGFX_REG_SIZE, -1, (char *)rtg_mem, "rtg_mem"); + add_mapping(cfg_, MAPTYPE_RAM_NOALLOC, PIGFX_RTG_BASE + PIGFX_REG_SIZE, 40 * SIZE_MEGA - PIGFX_REG_SIZE, -1, (char *)rtg_mem, "rtg_mem", 0); return 1; } diff --git a/platforms/platforms.h b/platforms/platforms.h index 62dd044..a88bda8 100644 --- a/platforms/platforms.h +++ b/platforms/platforms.h @@ -11,3 +11,6 @@ enum base_platforms { }; struct platform_config *make_platform_config(char *name, char *subsys); + +void dump_range_to_file(uint32_t addr, uint32_t size, char *filename); +uint8_t *dump_range_to_memory(uint32_t addr, uint32_t size); diff --git a/platforms/shared/common.c b/platforms/shared/common.c new file mode 100644 index 0000000..d9aa87c --- /dev/null +++ b/platforms/shared/common.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: MIT + +#include +#include +#include +#include +#include +#include "platforms/platforms.h" +#include "gpio/ps_protocol.h" + +void dump_range_to_file(uint32_t addr, uint32_t size, char *filename) { + FILE *out = fopen(filename, "wb+"); + if (out == NULL) { + printf ("[SHARED-DUMP_RANGE_TO_FILE] Failed to open %s for writing.\n", filename); + printf ("[SHARED-DUMP_RANGE_TO_FILE] Memory range has not been dumped to file.\n"); + return; + } + + for (uint32_t i = 0; i < size; i += 2) { + uint16_t in = be16toh(read16(addr + i)); + fwrite(&in, 2, 1, out); + } + + fclose(out); + printf ("[SHARED-DUMP_RANGE_TO_FILE] Memory range dumped to file %s.\n", filename); +} + +uint8_t *dump_range_to_memory(uint32_t addr, uint32_t size) { + uint8_t *mem = calloc(size, 1); + + if (mem == NULL) { + printf ("[SHARED-DUMP_RANGE_TO_MEMORY] Failed to allocate memory for dumped range.\n"); + return NULL; + } + + for (uint32_t i = 0; i < size; i += 2) { + *(uint16_t *)&mem[i] = (uint16_t)be16toh(read16(addr + i)); + } + + printf ("[SHARED-DUMP_RANGE_TO_FILE] Memory range copied to RAM.\n"); + return mem; +}