]> git.sesse.net Git - pistorm/commitdiff
Add support for automatic ROM range dumping to memory/file
authorbeeanyew <beeanyew@gmail.com>
Fri, 21 May 2021 16:53:31 +0000 (18:53 +0200)
committerbeeanyew <beeanyew@gmail.com>
Fri, 21 May 2021 16:53:31 +0000 (18:53 +0200)
Makefile
config_file/config_file.c
config_file/config_file.h
default.cfg
emulator.c
platforms/amiga/pistorm-dev/pistorm-dev.c
platforms/amiga/rtg/rtg.c
platforms/platforms.h
platforms/shared/common.c [new file with mode: 0644]

index 8ea8cb08d94c986199704f9e23887271117737e6..7396390978801a62bfe07b711aa992f42095ec50 100644 (file)
--- 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
index 0b5d86a215eed8ddc536d08f49109c559f35cb37..a482041037b515aea652391f26e1acdbe6f48b76 100644 (file)
@@ -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;
       }
index 81d47550c2519b887ae973e444cf430391c8d658..889b0ac0b20cffe4755bf62bbfcced445f251058 100644 (file)
@@ -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
 
index e6249229ebb3cde1eb06b1624f8cc874f3910dc7..0526656e3343ef7f84a81e45f1db543081ddae80 100644 (file)
@@ -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
 
index 78ee1a5d18e5e2ad5df3bfbfed94e33bc94a5885..0e0101413baf639a5d64c98791897f0a816a4ba4 100644 (file)
@@ -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);
index 332e42dba8b7d9434c5447fa0e49facc8cc3be36..35e8e93432c1b6dceff4d9718b2cde2d5d01393b 100644 (file)
@@ -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 {
index 2f823921b6328e1c743e1f7ab7daf7606f49d454..564dd50adb708480b59175a2d29ed1d2bf819dad 100644 (file)
@@ -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;
 }
 
index 62dd04473d0d92d043169e57389a2392309a95eb..a88bda81c0df169d5b6178917d067c0d2145b4bb 100644 (file)
@@ -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 (file)
index 0000000..d9aa87c
--- /dev/null
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: MIT
+
+#include <time.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <endian.h>
+#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;
+}