From: kub <derkub@gmail.com>
Date: Mon, 23 Dec 2024 11:17:51 +0000 (+0100)
Subject: core 68k, fix cyclone code execution from overlaid rom (sram/megasd)
X-Git-Tag: v2.04~98
X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3bbce6228136e13457df9809d8cedf5bd093a722;p=picodrive.git

core 68k, fix cyclone code execution from overlaid rom (sram/megasd)
---

diff --git a/pico/cart.c b/pico/cart.c
index 2d4ea6fd..aaf91fff 100644
--- a/pico/cart.c
+++ b/pico/cart.c
@@ -828,7 +828,7 @@ int PicoCartInsert(unsigned char *rom, unsigned int romsize, const char *carthw_
   // This will hang the emu, but will prevent nasty crashes.
   // note: 4 bytes are padded to every ROM
   if (rom != NULL)
-    *(u32 *)(rom+romsize) = CPU_BE2(0x4EFAFFFE);
+    *(u32 *)(rom+romsize) = CPU_BE2(0x6000FFFE);
 
   Pico.rom=rom;
   Pico.romsize=romsize;
diff --git a/pico/m68kif_cyclone.s b/pico/m68kif_cyclone.s
index 55e996a4..c321391a 100644
--- a/pico/m68kif_cyclone.s
+++ b/pico/m68kif_cyclone.s
@@ -44,7 +44,6 @@ crashed:
     stmfd   sp!,{lr}
     mov     r1, r7
     bl      cyclone_crashed
-    ldr     r0, [r7, #0x40]  @ reload PC + membase
     ldmfd   sp!,{pc}
 
 
diff --git a/pico/memory.c b/pico/memory.c
index c79018d9..5c3600c2 100644
--- a/pico/memory.c
+++ b/pico/memory.c
@@ -294,12 +294,22 @@ void log_io(unsigned int addr, int bits, int rw);
 #endif
 
 #if defined(EMU_C68K)
-void cyclone_crashed(u32 pc, struct Cyclone *context)
+u32 cyclone_crashed(u32 pc, struct Cyclone *context)
 {
-    elprintf(EL_STATUS|EL_ANOMALY, "%c68k crash detected @ %06x",
-      context == &PicoCpuCM68k ? 'm' : 's', pc);
+    // check for underlying ROM, in case of on-cart hw overlaying part of ROM
+    // NB assumes code isn't executed from the overlay, but I've never seen this
+    u32 pc24 = pc & 0xffffff;
+    if (pc24 >= Pico.romsize) {
+      // no ROM, so it's probably an illegal access
+      pc24 = Pico.romsize;
+      elprintf(EL_STATUS|EL_ANOMALY, "%c68k crash detected @ %06x",
+        context == &PicoCpuCM68k ? 'm' : 's', pc);
+    }
+
     context->membase = (u32)Pico.rom;
-    context->pc = (u32)Pico.rom + Pico.romsize;
+    context->pc = (u32)Pico.rom + pc24;
+
+    return context->pc;
 }
 #endif