amalgamation
[picodrive.git] / Pico / Memory.c
index 3157a63..01e92f4 100644 (file)
@@ -7,23 +7,25 @@
 // For commercial use, separate licencing terms must be obtained.\r
 \r
 \r
-//#define __debug_io\r
+#define __debug_io\r
 \r
 #include "PicoInt.h"\r
 \r
-#include "sound/sound.h"\r
 #include "sound/ym2612.h"\r
 #include "sound/sn76496.h"\r
 \r
+#ifndef UTYPES_DEFINED\r
 typedef unsigned char  u8;\r
 typedef unsigned short u16;\r
 typedef unsigned int   u32;\r
+#define UTYPES_DEFINED\r
+#endif\r
 \r
 extern unsigned int lastSSRamWrite; // used by serial SRAM code\r
 \r
 #ifdef _ASM_MEMORY_C\r
-u  PicoRead8(u32 a);\r
-u16  PicoRead16(u32 a);\r
+u32  PicoRead8(u32 a);\r
+u32  PicoRead16(u32 a);\r
 void PicoWriteRomHW_SSF2(u32 a,u32 d);\r
 void PicoWriteRomHW_in1 (u32 a,u32 d);\r
 #endif\r
@@ -69,9 +71,13 @@ static u32 CPU_CALL PicoCheckPc(u32 pc)
   u32 ret=0;\r
 #if defined(EMU_C68K)\r
   pc-=PicoCpu.membase; // Get real pc\r
-  pc&=0xfffffe;\r
+//  pc&=0xfffffe;\r
+  pc&=~1;\r
+  if ((pc<<8) == 0)\r
+    return (int)Pico.rom + Pico.romsize; // common crash condition, can happen if acc timing is off\r
 \r
-  PicoCpu.membase=PicoMemBase(pc);\r
+  PicoCpu.membase=PicoMemBase(pc&0x00ffffff);\r
+  PicoCpu.membase-=pc&0xff000000;\r
 \r
   ret = PicoCpu.membase+pc;\r
 #elif defined(EMU_A68K)\r
@@ -84,14 +90,14 @@ static u32 CPU_CALL PicoCheckPc(u32 pc)
 }\r
 \r
 \r
-int PicoInitPc(u32 pc)\r
+PICO_INTERNAL int PicoInitPc(u32 pc)\r
 {\r
   PicoCheckPc(pc);\r
   return 0;\r
 }\r
 \r
 #ifndef _ASM_MEMORY_C\r
-void PicoMemReset()\r
+PICO_INTERNAL_ASM void PicoMemReset(void)\r
 {\r
 }\r
 #endif\r
@@ -192,6 +198,7 @@ static void OtherWrite8End(u32 a,u32 d,int realsize)
   //if(a==0x200000) dprintf("cc : %02x @ %06x [%i|%i]", d, SekPc, SekCyclesDoneT(), SekCyclesDone());\r
   //if(a==0x200001) dprintf("w8 : %02x @ %06x [%i]", d, SekPc, SekCyclesDoneT());\r
   if(a >= SRam.start && a <= SRam.end) {\r
+    dprintf("sram w%i: %06x, %08x @%06x", realsize, a&0xffffff, d, SekPc);\r
     unsigned int sreg = Pico.m.sram_reg;\r
     if(!(sreg & 0x10)) {\r
       // not detected SRAM\r
@@ -229,7 +236,9 @@ static void OtherWrite8End(u32 a,u32 d,int realsize)
 #else\r
   // sram access register\r
   if(a == 0xA130F1) {\r
-    Pico.m.sram_reg = (u8)(d&3);\r
+    dprintf("sram reg=%02x", d);\r
+    Pico.m.sram_reg &= ~3;\r
+    Pico.m.sram_reg |= (u8)(d&3);\r
     return;\r
   }\r
 #endif\r
@@ -259,7 +268,7 @@ static void OtherWrite8End(u32 a,u32 d,int realsize)
 //                     Read Rom and read Ram\r
 \r
 #ifndef _ASM_MEMORY_C\r
-u8 CPU_CALL PicoRead8(u32 a)\r
+PICO_INTERNAL_ASM u32 CPU_CALL PicoRead8(u32 a)\r
 {\r
   u32 d=0;\r
 \r
@@ -302,17 +311,17 @@ u8 CPU_CALL PicoRead8(u32 a)
   dprintf("r8 : %06x,   %02x @%06x", a&0xffffff, (u8)d, SekPc);\r
 #endif\r
 #if defined(EMU_C68K) && defined(EMU_M68K)\r
-  if(a>=Pico.romsize&&(ppop&0x3f)!=0x3a&&(ppop&0x3f)!=0x3b) {\r
+  if(a>=Pico.romsize/*&&(ppop&0x3f)!=0x3a&&(ppop&0x3f)!=0x3b*/) {\r
     lastread_a = a;\r
     lastread_d[lrp_cyc++&15] = (u8)d;\r
   }\r
 #endif\r
-  return (u8)d;\r
+  return d;\r
 }\r
 \r
-u16 CPU_CALL PicoRead16(u32 a)\r
+PICO_INTERNAL_ASM u32 CPU_CALL PicoRead16(u32 a)\r
 {\r
-  u16 d=0;\r
+  u32 d=0;\r
 \r
   if ((a&0xe00000)==0xe00000) { d=*(u16 *)(Pico.ram+(a&0xfffe)); goto end; } // Ram\r
 \r
@@ -321,14 +330,14 @@ u16 CPU_CALL PicoRead16(u32 a)
 #if !(defined(EMU_C68K) && defined(EMU_M68K))\r
   // sram\r
   if(a >= SRam.start && a <= SRam.end && (Pico.m.sram_reg & 1)) {\r
-    d = (u16) SRAMRead(a);\r
+    d = SRAMRead(a);\r
     goto end;\r
   }\r
 #endif\r
 \r
   if (a<Pico.romsize) { d = *(u16 *)(Pico.rom+a); goto end; } // Rom\r
 \r
-  d = (u16)OtherRead16(a, 16);\r
+  d = OtherRead16(a, 16);\r
 \r
   end:\r
   //if ((a&0xe0ffff)==0xe0AF0E+0x69c||(a&0xe0ffff)==0xe0A9A8+0x69c||(a&0xe0ffff)==0xe0A9AA+0x69c||(a&0xe0ffff)==0xe0A9AC+0x69c)\r
@@ -338,7 +347,7 @@ u16 CPU_CALL PicoRead16(u32 a)
   dprintf("r16: %06x, %04x  @%06x", a&0xffffff, d, SekPc);\r
 #endif\r
 #if defined(EMU_C68K) && defined(EMU_M68K)\r
-  if(a>=Pico.romsize&&(ppop&0x3f)!=0x3a&&(ppop&0x3f)!=0x3b) {\r
+  if(a>=Pico.romsize/*&&(ppop&0x3f)!=0x3a&&(ppop&0x3f)!=0x3b*/) {\r
     lastread_a = a;\r
     lastread_d[lrp_cyc++&15] = d;\r
   }\r
@@ -346,7 +355,7 @@ u16 CPU_CALL PicoRead16(u32 a)
   return d;\r
 }\r
 \r
-u32 CPU_CALL PicoRead32(u32 a)\r
+PICO_INTERNAL_ASM u32 CPU_CALL PicoRead32(u32 a)\r
 {\r
   u32 d=0;\r
 \r
@@ -369,7 +378,7 @@ u32 CPU_CALL PicoRead32(u32 a)
   dprintf("r32: %06x, %08x @%06x", a&0xffffff, d, SekPc);\r
 #endif\r
 #if defined(EMU_C68K) && defined(EMU_M68K)\r
-  if(a>=Pico.romsize&&(ppop&0x3f)!=0x3a&&(ppop&0x3f)!=0x3b) {\r
+  if(a>=Pico.romsize/*&&(ppop&0x3f)!=0x3a&&(ppop&0x3f)!=0x3b*/) {\r
     lastread_a = a;\r
     lastread_d[lrp_cyc++&15] = d;\r
   }\r
@@ -392,10 +401,7 @@ static void CPU_CALL PicoWrite8(u32 a,u8 d)
   //if ((a&0xe0ffff)==0xe0a9ba+0x69c)\r
   //  dprintf("w8 : %06x,   %02x @%06x", a&0xffffff, d, SekPc);\r
 \r
-\r
-  if ((a&0xe00000)==0xe00000) {\r
-         if((a&0xffff)==0xf62a) dprintf("(f62a) = %02x [%i|%i] @ %x", d, Pico.m.scanline, SekCyclesDone(), SekPc);\r
-          u8 *pm=(u8 *)(Pico.ram+((a^1)&0xffff)); pm[0]=d; return; } // Ram\r
+  if ((a&0xe00000)==0xe00000) { *(u8 *)(Pico.ram+((a^1)&0xffff))=d; return; } // Ram\r
 \r
   a&=0xffffff;\r
   OtherWrite8(a,d,8);\r
@@ -442,7 +448,7 @@ static void CPU_CALL PicoWrite32(u32 a,u32 d)
 \r
 \r
 // -----------------------------------------------------------------\r
-void PicoMemSetup()\r
+PICO_INTERNAL void PicoMemSetup(void)\r
 {\r
 #ifdef EMU_C68K\r
   // Setup memory callbacks:\r
@@ -498,39 +504,78 @@ unsigned int  m68k_read_pcrelative_CD16(unsigned int a);
 unsigned int  m68k_read_pcrelative_CD32(unsigned int a);\r
 \r
 // these are allowed to access RAM\r
-unsigned int  m68k_read_pcrelative_8 (unsigned int a) {\r
+static unsigned int  m68k_read_8 (unsigned int a, int do_fake) {\r
   a&=0xffffff;\r
   if(PicoMCD&1) return m68k_read_pcrelative_CD8(a);\r
   if(a<Pico.romsize)         return *(u8 *)(Pico.rom+(a^1)); // Rom\r
+#ifdef EMU_C68K\r
+  if(do_fake&&((ppop&0x3f)==0x3a||(ppop&0x3f)==0x3b)) return lastread_d[lrp_mus++&15];\r
+#endif\r
   if((a&0xe00000)==0xe00000) return *(u8 *)(Pico.ram+((a^1)&0xffff)); // Ram\r
-  return 0;//(u8)  lastread_d;\r
+  return 0;\r
 }\r
-unsigned int  m68k_read_pcrelative_16(unsigned int a) {\r
+static unsigned int  m68k_read_16(unsigned int a, int do_fake) {\r
   a&=0xffffff;\r
   if(PicoMCD&1) return m68k_read_pcrelative_CD16(a);\r
   if(a<Pico.romsize)         return *(u16 *)(Pico.rom+(a&~1)); // Rom\r
+#ifdef EMU_C68K\r
+  if(do_fake&&((ppop&0x3f)==0x3a||(ppop&0x3f)==0x3b)) return lastread_d[lrp_mus++&15];\r
+#endif\r
   if((a&0xe00000)==0xe00000) return *(u16 *)(Pico.ram+(a&0xfffe)); // Ram\r
-  return 0;//(u16) lastread_d;\r
+  return 0;\r
 }\r
-unsigned int  m68k_read_pcrelative_32(unsigned int a) {\r
+static unsigned int  m68k_read_32(unsigned int a, int do_fake) {\r
   a&=0xffffff;\r
   if(PicoMCD&1) return m68k_read_pcrelative_CD32(a);\r
   if(a<Pico.romsize)         { u16 *pm=(u16 *)(Pico.rom+(a&~1));     return (pm[0]<<16)|pm[1]; }\r
+#ifdef EMU_C68K\r
+  if(do_fake&&((ppop&0x3f)==0x3a||(ppop&0x3f)==0x3b)) return lastread_d[lrp_mus++&15];\r
+#endif\r
   if((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xfffe)); return (pm[0]<<16)|pm[1]; } // Ram\r
-  return 0;//lastread_d;\r
+  return 0;\r
 }\r
 \r
-unsigned int m68k_read_immediate_16(unsigned int a)    { return m68k_read_pcrelative_16(a); }\r
-unsigned int m68k_read_immediate_32(unsigned int a)    { return m68k_read_pcrelative_32(a); }\r
-unsigned int m68k_read_disassembler_8 (unsigned int a) { return m68k_read_pcrelative_8 (a); }\r
-unsigned int m68k_read_disassembler_16(unsigned int a) { return m68k_read_pcrelative_16(a); }\r
-unsigned int m68k_read_disassembler_32(unsigned int a) { return m68k_read_pcrelative_32(a); }\r
+unsigned int m68k_read_pcrelative_8 (unsigned int a)   { return m68k_read_8 (a, 1); }\r
+unsigned int m68k_read_pcrelative_16(unsigned int a)   { return m68k_read_16(a, 1); }\r
+unsigned int m68k_read_pcrelative_32(unsigned int a)   { return m68k_read_32(a, 1); }\r
+unsigned int m68k_read_immediate_16(unsigned int a)    { return m68k_read_16(a, 0); }\r
+unsigned int m68k_read_immediate_32(unsigned int a)    { return m68k_read_32(a, 0); }\r
+unsigned int m68k_read_disassembler_8 (unsigned int a) { return m68k_read_8 (a, 0); }\r
+unsigned int m68k_read_disassembler_16(unsigned int a) { return m68k_read_16(a, 0); }\r
+unsigned int m68k_read_disassembler_32(unsigned int a) { return m68k_read_32(a, 0); }\r
 \r
 #ifdef EMU_C68K\r
 // ROM only\r
-unsigned int  m68k_read_memory_8(unsigned int a)  { if(a<Pico.romsize) return  *(u8 *) (Pico.rom+(a^1)); return (u8)  lastread_d[lrp_mus++&15]; }\r
-unsigned int  m68k_read_memory_16(unsigned int a) { if(a<Pico.romsize) return  *(u16 *)(Pico.rom+(a&~1));return (u16) lastread_d[lrp_mus++&15]; }\r
-unsigned int  m68k_read_memory_32(unsigned int a) { if(a<Pico.romsize) {u16 *pm=(u16 *)(Pico.rom+(a&~1));return (pm[0]<<16)|pm[1];} return lastread_d[lrp_mus++&15]; }\r
+unsigned int m68k_read_memory_8(unsigned int a)\r
+{\r
+  u8 d;\r
+  if(a<Pico.romsize) d = *(u8 *) (Pico.rom+(a^1));\r
+  else d = (u8) lastread_d[lrp_mus++&15];\r
+#ifdef __debug_io\r
+  dprintf("r8_mu : %06x,   %02x @%06x", a&0xffffff, d, SekPc);\r
+#endif\r
+  return d;\r
+}\r
+unsigned int m68k_read_memory_16(unsigned int a)\r
+{\r
+  u16 d;\r
+  if(a<Pico.romsize) d = *(u16 *)(Pico.rom+(a&~1));\r
+  else d = (u16) lastread_d[lrp_mus++&15];\r
+#ifdef __debug_io\r
+  dprintf("r16_mu: %06x, %04x @%06x", a&0xffffff, d, SekPc);\r
+#endif\r
+  return d;\r
+}\r
+unsigned int m68k_read_memory_32(unsigned int a)\r
+{\r
+  u32 d;\r
+  if(a<Pico.romsize) {u16 *pm=(u16 *)(Pico.rom+(a&~1));d=(pm[0]<<16)|pm[1];}\r
+  else d = lastread_d[lrp_mus++&15];\r
+#ifdef __debug_io\r
+  dprintf("r32_mu: %06x, %08x @%06x", a&0xffffff, d, SekPc);\r
+#endif\r
+  return d;\r
+}\r
 \r
 // ignore writes, Cyclone already done that\r
 void m68k_write_memory_8(unsigned int address, unsigned int value)  { lastwrite_mus_d[lwp_mus++&15] = value; }\r
@@ -580,7 +625,7 @@ void m68k_write_memory_32(unsigned int address, unsigned int value)
 // -----------------------------------------------------------------\r
 //                        z80 memhandlers\r
 \r
-unsigned char z80_read(unsigned short a)\r
+PICO_INTERNAL unsigned char z80_read(unsigned short a)\r
 {\r
   u8 ret = 0;\r
 \r
@@ -608,14 +653,14 @@ end:
   return ret;\r
 }\r
 \r
-unsigned short z80_read16(unsigned short a)\r
+PICO_INTERNAL unsigned short z80_read16(unsigned short a)\r
 {\r
   //dprintf("z80_read16");\r
 \r
   return (u16) ( (u16)z80_read(a) | ((u16)z80_read((u16)(a+1))<<8) );\r
 }\r
 \r
-void z80_write(unsigned char data, unsigned short a)\r
+PICO_INTERNAL_ASM void z80_write(unsigned char data, unsigned short a)\r
 {\r
   //if (a<0x4000)\r
   //  dprintf("z80 w8 : %06x,   %02x @%04x", a, data, mz80GetRegisterValue(NULL, 0));\r
@@ -654,7 +699,7 @@ void z80_write(unsigned char data, unsigned short a)
   if (a<0x4000) { Pico.zram[a&0x1fff]=data; return; }\r
 }\r
 \r
-void z80_write16(unsigned short data, unsigned short a)\r
+PICO_INTERNAL void z80_write16(unsigned short data, unsigned short a)\r
 {\r
   //dprintf("z80_write16");\r
 \r