]> git.sesse.net Git - pistorm/blobdiff - platforms/amiga/amiga-platform.c
Fixes to allow a314 configuration on non default paths
[pistorm] / platforms / amiga / amiga-platform.c
index 5668bb8bdc2bfe016a5318002c5fe4aaebb2ec9b..160d2f90c9afa9e5577b65e0cff08fb878246a8c 100644 (file)
@@ -1,3 +1,5 @@
+// SPDX-License-Identifier: MIT
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "net/pi-net.h"
 #include "piscsi/piscsi-enums.h"
 #include "piscsi/piscsi.h"
+#include "pistorm-dev/pistorm-dev-enums.h"
+#include "pistorm-dev/pistorm-dev.h"
 #include "platforms/platforms.h"
 #include "platforms/shared/rtc.h"
 #include "rtg/rtg.h"
+#include "a314/a314.h"
+
+//#define DEBUG_AMIGA_PLATFORM
+
+#ifdef DEBUG_AMIGA_PLATFORM
+#define DEBUG printf
+#else
+#define DEBUG(...)
+#endif
 
 int handle_register_read_amiga(unsigned int addr, unsigned char type, unsigned int *val);
 int handle_register_write_amiga(unsigned int addr, unsigned int value, unsigned char type);
-int init_rtg_data();
 
 extern int ac_z2_current_pic;
 extern int ac_z2_done;
@@ -39,26 +51,42 @@ extern const char *op_type_names[OP_TYPE_NUM];
 extern uint8_t cdtv_mode;
 extern uint8_t rtc_type;
 extern unsigned char cdtv_sram[32 * SIZE_KILO];
+extern unsigned int a314_base;
 
 #define min(a, b) (a < b) ? a : b
 #define max(a, b) (a > b) ? a : b
 
-static uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0;
+uint8_t rtg_enabled = 0, piscsi_enabled = 0, pinet_enabled = 0, kick13_mode = 0, pistorm_dev_enabled = 1;
+uint8_t a314_emulation_enabled = 0;
 
-extern uint32_t piscsi_base;
+extern uint32_t piscsi_base, pistorm_dev_base;
 
 extern void stop_cpu_emulation(uint8_t disasm_cur);
 
 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;
+    if (kick13_mode)
+        ac_z3_done = 1;
+
+    if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
+        if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
+            if (type == OP_TYPE_BYTE) {
+                *val = autoconfig_read_memory_8(cfg, addr - AC_Z2_BASE);
+                return 1;
+            }
+            printf("Unexpected %s read from Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
             return -1;
         }
-
-        if (type == OP_TYPE_BYTE) {
-            *val = autoconfig_read_memory_8(cfg, addr);
-            return 1;
+        if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
+            uint32_t addr_ = addr - AC_Z2_BASE;
+            if (addr_ & 0x02) {
+                addr_ = (addr_ - 2) + 0x100;
+            }
+            if (type == OP_TYPE_BYTE) {
+                *val = autoconfig_read_memory_z3_8(cfg, addr_ - AC_Z2_BASE);
+                return 1;
+            }
+            printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
+            return -1;
         }
     }
     if (!ac_z3_done && addr >= AC_Z3_BASE && addr < AC_Z3_BASE + AC_SIZE) {
@@ -68,36 +96,77 @@ inline int custom_read_amiga(struct emulator_config *cfg, unsigned int addr, uns
         }
 
         if (type == OP_TYPE_BYTE) {
-            *val = autoconfig_read_memory_z3_8(cfg, addr);
+            *val = autoconfig_read_memory_z3_8(cfg, addr - AC_Z3_BASE);
             return 1;
         }
-        else {
-            printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
-            //stop_emulation();
-        }
+        printf("Unexpected %s read from Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z3_BASE);
+        return -1;
+    }
+
+    if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
+        *val = handle_pistorm_dev_read(addr, type);
+        return 1;
     }
 
-    if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
+    if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
         //printf("[Amiga-Custom] %s read from PISCSI base @$%.8X.\n", op_type_names[type], addr);
         //stop_cpu_emulation(1);
         *val = handle_piscsi_read(addr, type);
         return 1;
     }
 
+    if (a314_emulation_enabled && addr >= a314_base && addr < a314_base + (64 * SIZE_KILO)) {
+        //printf("%s read from A314 @$%.8X\n", op_type_names[type], addr);
+        switch (type) {
+            case OP_TYPE_BYTE:
+                *val = a314_read_memory_8(addr - a314_base);
+                return 1;
+                break;
+            case OP_TYPE_WORD:
+                *val = a314_read_memory_16(addr - a314_base);
+                return 1;                
+                break;
+            case OP_TYPE_LONGWORD:
+                *val = a314_read_memory_32(addr - a314_base);
+                return 1;
+                break;
+            default:
+                break;
+        }
+        return 1;
+    }
+
     return -1;
 }
 
 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) {
-                ac_z2_done = 1;
-                return -1;
+    if (kick13_mode)
+        ac_z3_done = 1;
+
+    if ((!ac_z2_done || !ac_z3_done) && addr >= AC_Z2_BASE && addr < AC_Z2_BASE + AC_SIZE) {
+        if (!ac_z2_done && ac_z2_current_pic < ac_z2_pic_count) {
+            if (type == OP_TYPE_BYTE) {
+                autoconfig_write_memory_8(cfg, addr - AC_Z2_BASE, val);
+                return 1;
             }
-
-            //printf("Write to Z2 autoconf area.\n");
-            autoconfig_write_memory_8(cfg, addr, val);
-            return 1;
+            printf("Unexpected %s write to Z2 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
+            return -1;
+        }
+        if (!ac_z3_done && ac_z3_current_pic < ac_z3_pic_count) {
+            uint32_t addr_ = addr - AC_Z2_BASE;
+            if (addr_ & 0x02) {
+                addr_ = (addr_ - 2) + 0x100;
+            }
+            if (type == OP_TYPE_BYTE) {
+                autoconfig_write_memory_z3_8(cfg, addr_ - AC_Z2_BASE, val);
+                return 1;
+            }
+            else if (type == OP_TYPE_WORD) {
+                autoconfig_write_memory_z3_16(cfg, addr_ - AC_Z2_BASE, val);
+                return 1;
+            }
+            printf("Unexpected %s write to Z3 autoconf addr %.X\n", op_type_names[type], addr - AC_Z2_BASE);
+            return -1;
         }
     }
 
@@ -109,11 +178,11 @@ inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, un
             }
 
             //printf("Write to autoconf area.\n");
-            autoconfig_write_memory_z3_8(cfg, addr, val);
+            autoconfig_write_memory_z3_8(cfg, addr - AC_Z3_BASE, val);
             return 1;
         }
         else if (type == OP_TYPE_WORD) {
-            autoconfig_write_memory_z3_16(cfg, addr, val);
+            autoconfig_write_memory_z3_16(cfg, addr - AC_Z3_BASE, val);
             return 1;
         }
         else {
@@ -122,12 +191,39 @@ inline int custom_write_amiga(struct emulator_config *cfg, unsigned int addr, un
         }
     }
 
-    if (addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
+    if (pistorm_dev_enabled && addr >= pistorm_dev_base && addr < pistorm_dev_base + (64 * SIZE_KILO)) {
+        handle_pistorm_dev_write(addr, val, type);
+        return 1;
+    }
+
+    if (piscsi_enabled && addr >= piscsi_base && addr < piscsi_base + (64 * SIZE_KILO)) {
         //printf("[Amiga-Custom] %s write to PISCSI base @$%.8x: %.8X\n", op_type_names[type], addr, val);
         handle_piscsi_write(addr, val, type);
         return 1;
     }
 
+    if (a314_emulation_enabled && addr >= a314_base && addr < a314_base + (64 * SIZE_KILO)) {
+        //printf("%s write to A314 @$%.8X: %d\n", op_type_names[type], addr, val);
+        switch (type) {
+            case OP_TYPE_BYTE:
+                a314_write_memory_8(addr - a314_base, val);
+                return 1;
+                break;
+            case OP_TYPE_WORD:
+                // Not implemented in a314.cc
+                //a314_write_memory_16(addr, val);
+                return -1;
+                break;
+            case OP_TYPE_LONGWORD:
+                // Not implemented in a314.cc
+                // a314_write_memory_32(addr, val);
+                return -1;
+                break;
+            default:
+                break;
+        }
+    }
+
     return -1;
 }
 
@@ -147,20 +243,20 @@ void adjust_ranges_amiga(struct emulator_config *cfg) {
         }
     }
 
-    if (ac_z2_pic_count && !ac_z2_done) {
+    if ((ac_z2_pic_count || ac_z3_pic_count) && (!ac_z2_done || !ac_z3_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 (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);
-    }
+    }*/
     if (rtg_enabled) {
         if (cfg->custom_low == 0)
             cfg->custom_low = PIGFX_RTG_BASE;
@@ -220,7 +316,7 @@ int setup_platform_amiga(struct emulator_config *cfg) {
         cfg->map_id[index][0] = '^';
         int resize_data = 0;
         if (cfg->map_size[index] > 8 * SIZE_MEGA) {
-            printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizng to 8MB.\n");
+            printf("Attempted to configure more than 8MB of Z2 Fast RAM, downsizing to 8MB.\n");
             resize_data = 8 * SIZE_MEGA;
         }
         else if(cfg->map_size[index] != 2 * SIZE_MEGA && cfg->map_size[index] != 4 * SIZE_MEGA && cfg->map_size[index] != 8 * SIZE_MEGA) {
@@ -239,9 +335,10 @@ int setup_platform_amiga(struct emulator_config *cfg) {
             cfg->map_data[index] = (unsigned char *)malloc(cfg->map_size[index]);
         }
         printf("%dMB of Z2 Fast RAM configured at $%lx\n", cfg->map_size[index] / SIZE_MEGA, cfg->map_offset[index]);
-        ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
-        ac_z2_index[ac_z2_pic_count] = index;
-        ac_z2_pic_count++;
+        add_z2_pic(ACTYPE_MAPFAST_Z2, index);
+        //ac_z2_type[ac_z2_pic_count] = ACTYPE_MAPFAST_Z2;
+        //ac_z2_index[ac_z2_pic_count] = index;
+        //ac_z2_pic_count++;
     }
     else
         printf("No Z2 Fast RAM configured.\n");
@@ -293,14 +390,20 @@ int setup_platform_amiga(struct emulator_config *cfg) {
         }
     }
 
+    if (pistorm_dev_enabled) {
+        add_z2_pic(ACTYPE_PISTORM_DEV, 0);
+    }
+
     return 0;
 }
 
+#define CHKVAR(a) (strcmp(var, a) == 0)
+
 void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
     if (!var)
         return;
 
-    if (strcmp(var, "enable_rtc_emulation") == 0) {
+    if CHKVAR("enable_rtc_emulation") {
         int8_t rtc_enabled = 0;
         if (!val || strlen(val) == 0)
             rtc_enabled = 1;
@@ -311,20 +414,20 @@ void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
             configure_rtc_emulation_amiga(rtc_enabled);
         }
     }
-    if (strcmp(var, "hdd0") == 0) {
+    if CHKVAR("hdd0") {
         if (val && strlen(val) != 0)
             set_hard_drive_image_file_amiga(0, val);
     }
-    if (strcmp(var, "hdd1") == 0) {
+    if CHKVAR("hdd1") {
         if (val && strlen(val) != 0)
             set_hard_drive_image_file_amiga(1, val);
     }
-    if (strcmp(var, "cdtv") == 0) {
+    if CHKVAR("cdtv") {
         printf("[AMIGA] CDTV mode enabled.\n");
         cdtv_mode = 1;
     }
-    if (strcmp(var, "rtg") == 0) {
-        if (init_rtg_data()) {
+    if (CHKVAR("rtg") && !rtg_enabled) {
+        if (init_rtg_data(cfg)) {
             printf("[AMIGA] RTG Enabled.\n");
             rtg_enabled = 1;
             adjust_ranges_amiga(cfg);
@@ -332,50 +435,73 @@ void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
         else
             printf("[AMIGA} Failed to enable RTG.\n");
     }
+    if CHKVAR("kick13") {
+        printf("[AMIGA] Kickstart 1.3 mode enabled, Z3 PICs will not be enumerated.\n");
+        kick13_mode = 1;
+    }
+    if CHKVAR("a314") {
+        int32_t res = a314_init();
+        if (res != 0) {
+            printf("[AMIGA] Failed to enable A314 emulation, error return code: %d.\n", res);
+        } else {
+            printf("[AMIGA] A314 emulation enabled.\n");
+            add_z2_pic(ACTYPE_A314, 0);
+            a314_emulation_enabled = 1;
+        }
+    }
+    if CHKVAR("a314_conf") {
+        if (val && strlen(val) != 0) {
+            a314_set_config_file(val);
+        }
+    }
 
     // PiSCSI stuff
-    if (strcmp(var, "piscsi") == 0) {
+    if (CHKVAR("piscsi") && !piscsi_enabled) {
         printf("[AMIGA] PISCSI Interface Enabled.\n");
         piscsi_enabled = 1;
         piscsi_init();
-        ac_z2_type[ac_z2_pic_count] = ACTYPE_PISCSI;
-        ac_z2_pic_count++;
+        add_z2_pic(ACTYPE_PISCSI, 0);
         adjust_ranges_amiga(cfg);
     }
     if (piscsi_enabled) {
-        if (strcmp(var, "piscsi0") == 0) {
+        if CHKVAR("piscsi0") {
             piscsi_map_drive(val, 0);
         }
-        if (strcmp(var, "piscsi1") == 0) {
+        if CHKVAR("piscsi1") {
             piscsi_map_drive(val, 1);
         }
-        if (strcmp(var, "piscsi2") == 0) {
+        if CHKVAR("piscsi2") {
             piscsi_map_drive(val, 2);
         }
-        if (strcmp(var, "piscsi3") == 0) {
+        if CHKVAR("piscsi3") {
             piscsi_map_drive(val, 3);
         }
-        if (strcmp(var, "piscsi4") == 0) {
+        if CHKVAR("piscsi4") {
             piscsi_map_drive(val, 4);
         }
-        if (strcmp(var, "piscsi5") == 0) {
+        if CHKVAR("piscsi5") {
             piscsi_map_drive(val, 5);
         }
-        if (strcmp(var, "piscsi6") == 0) {
+        if CHKVAR("piscsi6") {
             piscsi_map_drive(val, 6);
         }
     }
 
     // Pi-Net stuff
-    if (strcmp(var, "pi-net") == 0) {
+    if (CHKVAR("pi-net")&& !pinet_enabled) {
         printf("[AMIGA] PI-NET Interface Enabled.\n");
         pinet_enabled = 1;
         pinet_init(val);
         adjust_ranges_amiga(cfg);
     }
 
+    if CHKVAR("no-pistorm-dev") {
+        pistorm_dev_enabled = 0;
+        printf("[AMIGA] Disabling PiStorm interaction device.\n");
+    }
+
     // RTC stuff
-    if (strcmp(var, "rtc_type") == 0) {
+    if CHKVAR("rtc_type") {
         if (val && strlen(val) != 0) {
             if (strcmp(val, "msm") == 0) {
                 printf("[AMIGA] RTC type set to MSM.\n");
@@ -390,11 +516,14 @@ void setvar_amiga(struct emulator_config *cfg, char *var, char *val) {
 }
 
 void handle_reset_amiga(struct emulator_config *cfg) {
-    ac_z3_done = 0;
-    ac_z2_done = 0;
+    ac_z2_done = (ac_z2_pic_count == 0);
+    ac_z3_done = (ac_z3_pic_count == 0);
     ac_z2_current_pic = 0;
     ac_z3_current_pic = 0;
 
+    DEBUG("[AMIGA] Reset handler.\n");
+    DEBUG("[AMIGA] AC done - Z2: %d Z3: %d.\n", ac_z2_done, ac_z3_done);
+
     if (piscsi_enabled)
         piscsi_refresh_drives();
 
@@ -414,6 +543,28 @@ void shutdown_platform_amiga(struct emulator_config *cfg) {
             printf("Failed to write CDTV SRAM to disk.\n");
         }
     }
+    if (cfg->platform->subsys) {
+        free(cfg->platform->subsys);
+    }
+    if (piscsi_enabled) {
+        piscsi_shutdown();
+        piscsi_enabled = 0;
+    }
+    if (rtg_enabled) {
+        shutdown_rtg();
+        rtg_enabled = 0;
+    }
+    if (pinet_enabled) {
+        pinet_enabled = 0;
+    }
+    if (a314_emulation_enabled) {
+        a314_emulation_enabled = 0;
+    }
+
+    cdtv_mode = 0;
+
+    autoconfig_reset_all();
+    printf("[AMIGA] Platform shutdown completed.\n");
 }
 
 void create_platform_amiga(struct platform_config *cfg, char *subsys) {
@@ -431,7 +582,7 @@ void create_platform_amiga(struct platform_config *cfg, char *subsys) {
     if (subsys) {
         cfg->subsys = malloc(strlen(subsys) + 1);
         strcpy(cfg->subsys, subsys);
-        for (int i = 0; i < strlen(cfg->subsys); i++) {
+        for (unsigned int i = 0; i < strlen(cfg->subsys); i++) {
             cfg->subsys[i] = tolower(cfg->subsys[i]);
         }
     }