Added 0.030 of PicoDrive and moved license files into root
[cyclone68000.git] / Pico / Memory.cpp
diff --git a/Pico/Memory.cpp b/Pico/Memory.cpp
new file mode 100644 (file)
index 0000000..64c1815
--- /dev/null
@@ -0,0 +1,251 @@
+\r
+#include "PicoInt.h"\r
+\r
+typedef unsigned char  u8;\r
+typedef unsigned short u16;\r
+typedef unsigned int   u32;\r
+\r
+static int PicoMemBase(u32 pc)\r
+{\r
+  int membase=0;\r
+\r
+  if (pc<Pico.romsize)\r
+  {\r
+    membase=(int)Pico.rom; // Program Counter in Rom\r
+  }\r
+  else if ((pc&0xe00000)==0xe00000)\r
+  {\r
+    membase=(int)Pico.ram-(pc&0xff0000); // Program Counter in Ram\r
+  }\r
+  else\r
+  {\r
+    // Error - Program Counter is invalid\r
+    membase=(int)Pico.rom;\r
+  }\r
+\r
+  return membase;\r
+}\r
+\r
+#ifdef EMU_A68K\r
+extern "C" u8 *OP_ROM=NULL,*OP_RAM=NULL;\r
+\r
+static void CPU_CALL PicoCheckPc(u32 pc)\r
+{\r
+  OP_ROM=(u8 *)PicoMemBase(pc);\r
+\r
+  // don't bother calling us back unless it's outside the 64k segment\r
+  M68000_regs.AsmBank=(pc>>16);\r
+}\r
+#endif\r
+\r
+#ifdef EMU_C68K\r
+static u32 PicoCheckPc(u32 pc)\r
+{\r
+  pc-=PicoCpu.membase; // Get real pc\r
+  pc&=0xffffff;\r
+\r
+  PicoCpu.membase=PicoMemBase(pc);\r
+\r
+  return PicoCpu.membase+pc;\r
+}\r
+#endif\r
+\r
+#ifdef EMU_NULL\r
+static u32 PicoCheckPc(u32) { return 0; }\r
+#endif\r
+\r
+\r
+int PicoInitPc(u32 pc)\r
+{\r
+  PicoCheckPc(pc);\r
+  return 0;\r
+}\r
+\r
+// -----------------------------------------------------------------\r
+static int PadRead(int i)\r
+{\r
+  int pad=0,value=0;\r
+  pad=~PicoPad[i]; // Get inverse of pad\r
+\r
+  if (Pico.m.padSelect[i]) value=0x40|(pad&0x3f); // 01CB RLDU\r
+  else value=((pad&0xc0)>>2)|(pad&3); // 00SA 00DU\r
+\r
+  return (value<<8)|value; // Mirror bytes\r
+}\r
+\r
+static u32 OtherRead16(u32 a)\r
+{\r
+  u32 d=0;\r
+\r
+  if ((a&0xffe000)==0xa00000)\r
+  {\r
+    // Z80 ram\r
+    d=*(u16 *)(Pico.zram+(a&0x1fff));\r
+\r
+    if (Pico.m.rotate&2) { d=(Pico.m.rotate>>2)&0xff; d|=d<<8; } // Fake z80\r
+    Pico.m.rotate++;\r
+\r
+    goto end;\r
+  }\r
+\r
+  if ((a&0xffe000)==0xa00000)\r
+  {\r
+    // Fake Z80 ram\r
+    d=((Pico.m.rotate++)>>2)&0xff; d|=d<<8;\r
+    goto end;\r
+  }\r
+  \r
+  if (a==0xa04000) { d=Pico.m.rotate&3; Pico.m.rotate++; goto end; } // Fudge\r
+  if (a==0xa10000) { d=Pico.m.hardware; goto end; }  // Hardware value\r
+  if (a==0xa10002) { d=PadRead(0); goto end; }\r
+  if (a==0xa10004) { d=PadRead(1); goto end; }\r
+  if (a==0xa11100) { d=((Pico.m.rotate++)&4)<<6; goto end; } // Fudge z80 reset\r
+\r
+  if ((a&0xffffe0)==0xc00000) { d=PicoVideoRead(a); goto end; }\r
+\r
+end:\r
+  return d;\r
+}\r
+\r
+static void OtherWrite8(u32 a,u32 d)\r
+{\r
+  if ((a&0xffe000)==0xa00000) { Pico.zram[(a^1)&0x1fff]=(u8)d; return; } // Z80 ram\r
+  if ((a&0xfffffc)==0xa04000) { PsndFm(a,d); return; } // FM Sound\r
+\r
+  if (a==0xa11100) { Pico.m.z80Run=(u8)(d^1); return; }\r
+  if (a==0xa10003) { Pico.m.padSelect[0]=(u8)((d>>6)&1); return; } // Joypad 1 select\r
+  if (a==0xa10005) { Pico.m.padSelect[1]=(u8)((d>>6)&1); return; } // Joypad 2 select\r
+\r
+  if ((a&0xffffe0)==0xc00000) { PicoVideoWrite(a,d|(d<<8)); return; } // Byte access gets mirrored\r
+}\r
+\r
+static void OtherWrite16(u32 a,u32 d)\r
+{\r
+  if ((a&0xffffe0)==0xc00000) { PicoVideoWrite(a,d); return; }\r
+  if ((a&0xffe000)==0xa00000) { *(u16 *)(Pico.zram+(a&0x1ffe))=(u16)d; return; } // Z80 ram\r
+\r
+  OtherWrite8(a,  d>>8);\r
+  OtherWrite8(a+1,d&0xff);\r
+}\r
+\r
+// -----------------------------------------------------------------\r
+//                     Read Rom and read Ram\r
+\r
+static u8 CPU_CALL PicoRead8(u32 a)\r
+{\r
+  u32 d=0;\r
+  a&=0xffffff;\r
+\r
+  if (a<Pico.romsize) return *(u8 *)(Pico.rom+(a^1)); // Rom\r
+  if ((a&0xe00000)==0xe00000) return *(u8 *)(Pico.ram+((a^1)&0xffff)); // Ram\r
+\r
+  d=OtherRead16(a&~1); if ((a&1)==0) d>>=8;\r
+  return (u8)d;\r
+}\r
+\r
+u16 CPU_CALL PicoRead16(u32 a)\r
+{\r
+  a&=0xfffffe;\r
+\r
+  if (a<Pico.romsize) { u16 *pm=(u16 *)(Pico.rom+a); return pm[0]; } // Rom\r
+  if ((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xffff)); return pm[0]; } // Ram\r
+\r
+  return (u16)OtherRead16(a);\r
+}\r
+\r
+u32 CPU_CALL PicoRead32(u32 a)\r
+{\r
+  a&=0xfffffe;\r
+\r
+  if (a<Pico.romsize) { u16 *pm=(u16 *)(Pico.rom+a); return (pm[0]<<16)|pm[1]; } // Rom\r
+  if ((a&0xe00000)==0xe00000) { u16 *pm=(u16 *)(Pico.ram+(a&0xffff)); return (pm[0]<<16)|pm[1]; } // Ram\r
+\r
+  return (OtherRead16(a)<<16)|OtherRead16(a+2);\r
+}\r
+\r
+// -----------------------------------------------------------------\r
+//                            Write Ram\r
+\r
+static void CPU_CALL PicoWrite8(u32 a,u8 d)\r
+{\r
+  if ((a&0xe00000)==0xe00000) { u8 *pm=(u8 *)(Pico.ram+((a^1)&0xffff)); pm[0]=d; return; } // Ram\r
+\r
+  a&=0xffffff;\r
+  OtherWrite8(a,d);\r
+}\r
+\r
+static void CPU_CALL PicoWrite16(u32 a,u16 d)\r
+{\r
+  if ((a&0xe00000)==0xe00000) { *(u16 *)(Pico.ram+(a&0xfffe))=d; return; } // Ram\r
+\r
+  a&=0xfffffe;\r
+  OtherWrite16(a,d);\r
+}\r
+\r
+static void CPU_CALL PicoWrite32(u32 a,u32 d)\r
+{\r
+  if ((a&0xe00000)==0xe00000)\r
+  {\r
+    // Ram:\r
+    u16 *pm=(u16 *)(Pico.ram+(a&0xfffe));\r
+    pm[0]=(u16)(d>>16); pm[1]=(u16)d;\r
+    return;\r
+  }\r
+\r
+  a&=0xfffffe;\r
+  OtherWrite16(a,  (u16)(d>>16));\r
+  OtherWrite16(a+2,(u16)d);\r
+}\r
+\r
+\r
+// -----------------------------------------------------------------\r
+int PicoMemInit()\r
+{\r
+#ifdef EMU_C68K\r
+  // Setup memory callbacks:\r
+  PicoCpu.checkpc=PicoCheckPc;\r
+  PicoCpu.fetch8 =PicoCpu.read8 =PicoRead8;\r
+  PicoCpu.fetch16=PicoCpu.read16=PicoRead16;\r
+  PicoCpu.fetch32=PicoCpu.read32=PicoRead32;\r
+  PicoCpu.write8 =PicoWrite8;\r
+  PicoCpu.write16=PicoWrite16;\r
+  PicoCpu.write32=PicoWrite32;\r
+#endif\r
+  return 0;\r
+}\r
+\r
+#ifdef EMU_A68K\r
+struct A68KInter\r
+{\r
+  u32 unknown;\r
+  u8  (__fastcall *Read8) (u32 a);\r
+  u16 (__fastcall *Read16)(u32 a);\r
+  u32 (__fastcall *Read32)(u32 a);\r
+  void (__fastcall *Write8)  (u32 a,u8 d);\r
+  void (__fastcall *Write16) (u32 a,u16 d);\r
+  void (__fastcall *Write32) (u32 a,u32 d);\r
+  void (__fastcall *ChangePc)(u32 a);\r
+  u8  (__fastcall *PcRel8) (u32 a);\r
+  u16 (__fastcall *PcRel16)(u32 a);\r
+  u32 (__fastcall *PcRel32)(u32 a);\r
+  u16 (__fastcall *Dir16)(u32 a);\r
+  u32 (__fastcall *Dir32)(u32 a);\r
+};\r
+\r
+extern "C" struct A68KInter a68k_memory_intf=\r
+{\r
+  0,\r
+  PicoRead8,\r
+  PicoRead16,\r
+  PicoRead32,\r
+  PicoWrite8,\r
+  PicoWrite16,\r
+  PicoWrite32,\r
+  PicoCheckPc,\r
+  PicoRead8,\r
+  PicoRead16,\r
+  PicoRead32,\r
+  PicoRead16, // unused\r
+  PicoRead32, // unused\r
+};\r
+#endif\r