amalgamation
[picodrive.git] / Pico / cd / Pico.c
index cba7250..5dcb39c 100644 (file)
@@ -1,20 +1,37 @@
-// This is part of Pico Library
-
-// (c) Copyright 2004 Dave, All rights reserved.
 // (c) Copyright 2007 notaz, All rights reserved.
-// Free for non-commercial use.
-
-// For commercial use, separate licencing terms must be obtained.
 
 
 #include "../PicoInt.h"
-#include "../sound/sound.h"
 
 
 extern unsigned char formatted_bram[4*0x10];
+extern unsigned int s68k_poll_adclk;
+
+void (*PicoMCDopenTray)(void) = NULL;
+int  (*PicoMCDcloseTray)(void) = NULL;
+
+#define dump_ram(ram,fname) \
+{ \
+  int i, d; \
+  FILE *f; \
+\
+  for (i = 0; i < sizeof(ram); i+=2) { \
+    d = (ram[i]<<8) | ram[i+1]; \
+    *(unsigned short *)(ram+i) = d; \
+  } \
+  f = fopen(fname, "wb"); \
+  if (f) { \
+    fwrite(ram, 1, sizeof(ram), f); \
+    fclose(f); \
+  } \
+  for (i = 0; i < sizeof(ram); i+=2) { \
+    d = (ram[i]<<8) | ram[i+1]; \
+    *(unsigned short *)(ram+i) = d; \
+  } \
+}
 
 
-int PicoInitMCD(void)
+PICO_INTERNAL int PicoInitMCD(void)
 {
   SekInitS68k();
   Init_CD_Driver();
@@ -23,40 +40,50 @@ int PicoInitMCD(void)
 }
 
 
-void PicoExitMCD(void)
+PICO_INTERNAL void PicoExitMCD(void)
 {
   End_CD_Driver();
+
+  //dump_ram(Pico_mcd->prg_ram, "prg.bin");
+  //dump_ram(Pico.ram, "ram.bin");
 }
 
-int PicoResetMCD(int hard)
+PICO_INTERNAL int PicoResetMCD(int hard)
 {
-  memset(Pico_mcd->prg_ram,    0, sizeof(Pico_mcd->prg_ram));
-  memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
-  memset(Pico_mcd->pcm_ram,    0, sizeof(Pico_mcd->pcm_ram));
   if (hard) {
-         int fmt_size = sizeof(formatted_bram);
-         memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
-         memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size);
+    int fmt_size = sizeof(formatted_bram);
+    memset(Pico_mcd->prg_ram,    0, sizeof(Pico_mcd->prg_ram));
+    memset(Pico_mcd->word_ram2M, 0, sizeof(Pico_mcd->word_ram2M));
+    memset(Pico_mcd->pcm_ram,    0, sizeof(Pico_mcd->pcm_ram));
+    memset(Pico_mcd->bram, 0, sizeof(Pico_mcd->bram));
+    memcpy(Pico_mcd->bram + sizeof(Pico_mcd->bram) - fmt_size, formatted_bram, fmt_size);
   }
   memset(Pico_mcd->s68k_regs, 0, sizeof(Pico_mcd->s68k_regs));
   memset(&Pico_mcd->pcm, 0, sizeof(Pico_mcd->pcm));
+  memset(&Pico_mcd->m, 0, sizeof(Pico_mcd->m));
 
   *(unsigned int *)(Pico_mcd->bios + 0x70) = 0xffffffff; // reset hint vector (simplest way to implement reg6)
-  Pico_mcd->m.state_flags |= 2; // s68k reset pending
+  Pico_mcd->m.state_flags |= 1; // s68k reset pending
   Pico_mcd->s68k_regs[3] = 1; // 2M word RAM mode with m68k access after reset
-  Pico_mcd->m.counter75hz = 0;
 
-  LC89510_Reset();
   Reset_CD();
+  LC89510_Reset();
   gfx_cd_reset();
 #ifdef _ASM_CD_MEMORY_C
   PicoMemResetCD(1);
+  //PicoMemResetCDdecode(1); // don't have to call this in 2M mode
 #endif
 
+  // use SRam.data for RAM cart
+  if (SRam.data) free(SRam.data);
+  SRam.data = NULL;
+  if (PicoOpt&0x8000)
+    SRam.data = calloc(1, 0x12000);
+
   return 0;
 }
 
-static __inline void SekRun(int cyc)
+static __inline void SekRunM68k(int cyc)
 {
   int cyc_do;
   SekCycleAim+=cyc;
@@ -89,7 +116,9 @@ static __inline void SekRunS68k(int cyc)
 #define PS_STEP_M68K ((488<<16)/20) // ~24
 //#define PS_STEP_S68K 13
 
-#ifndef _ASM_CD_PICO_C
+#ifdef _ASM_CD_PICO_C
+void SekRunPS(int cyc_m68k, int cyc_s68k);
+#else
 static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
 {
   int cycn, cycn_s68k, cyc_do;
@@ -132,8 +161,6 @@ static __inline void SekRunPS(int cyc_m68k, int cyc_s68k)
 
 //fprintf(stderr, "==    end %3i/%3i    ==\n", d_cm, d_cs);
 }
-#else
-void SekRunPS(int cyc_m68k, int cyc_s68k);
 #endif
 
 
@@ -184,6 +211,20 @@ static __inline void update_chips(void)
        // update gfx chip
        if (Pico_mcd->rot_comp.Reg_58 & 0x8000)
                gfx_cd_update();
+
+       // delayed setting of DMNA bit (needed for Silpheed)
+       if (Pico_mcd->m.state_flags & 2) {
+               Pico_mcd->m.state_flags &= ~2;
+               if (!(Pico_mcd->s68k_regs[3] & 4)) {
+                       Pico_mcd->s68k_regs[3] |=  2;
+                       Pico_mcd->s68k_regs[3] &= ~1;
+#ifdef USE_POLL_DETECT
+                       if ((s68k_poll_adclk&0xfe) == 2) {
+                               SekSetStopS68k(0); s68k_poll_adclk = 0;
+                       }
+#endif
+               }
+       }
 }
 
 
@@ -249,7 +290,7 @@ static int PicoFrameHintsMCD(void)
     {
       //dprintf("vint: @ %06x [%i|%i]", SekPc, y, SekCycleCnt);
       pv->status|=0x88; // V-Int happened, go into vblank
-      SekRun(128); SekCycleAim-=128; // there must be a gap between H and V ints, also after vblank bit set (Mazin Saga, Bram Stoker's Dracula)
+      SekRunM68k(128); SekCycleAim-=128; // there must be a gap between H and V ints, also after vblank bit set (Mazin Saga, Bram Stoker's Dracula)
       /*if(Pico.m.z80Run && (PicoOpt&4)) {
         z80CycleAim+=cycles_z80/2;
         total_z80+=z80_run(z80CycleAim-total_z80);
@@ -283,18 +324,25 @@ static int PicoFrameHintsMCD(void)
 
     // Run scanline:
       //dprintf("m68k starting exec @ %06x", SekPc);
-    if(Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
-    if((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) {
+    if (Pico.m.dma_bytes) SekCycleCnt+=CheckDMA();
+    if ((PicoOpt & 0x2000) && (Pico_mcd->m.busreq&3) == 1) {
       SekRunPS(cycles_68k, cycles_s68k); // "better/perfect sync"
     } else {
-      SekRun(cycles_68k);
+      SekRunM68k(cycles_68k);
       if ((Pico_mcd->m.busreq&3) == 1) // no busreq/no reset
         SekRunS68k(cycles_s68k);
     }
 
-    if((PicoOpt&4) && Pico.m.z80Run) {
-      Pico.m.z80Run|=2;
-      z80CycleAim+=cycles_z80;
+    if ((PicoOpt&4) && Pico.m.z80Run) {
+      if (Pico.m.z80Run & 2) z80CycleAim+=cycles_z80;
+      else {
+        int cnt = SekCyclesDone() - z80startCycle;
+        cnt = (cnt>>1)-(cnt>>5);
+        //if (cnt > cycles_z80) printf("FIXME: z80 cycles: %i\n", cnt);
+        if (cnt > cycles_z80) cnt = cycles_z80;
+        Pico.m.z80Run |= 2;
+        z80CycleAim+=cnt;
+      }
       total_z80+=z80_run(z80CycleAim-total_z80);
     }
 
@@ -302,14 +350,14 @@ static int PicoFrameHintsMCD(void)
   }
 
   // draw a frame just after vblank in alternative render mode
-  if(!PicoSkipFrame && (PicoOpt&0x10))
+  if (!PicoSkipFrame && (PicoOpt&0x10))
     PicoFrameFull();
 
   return 0;
 }
 
 
-int PicoFrameMCD(void)
+PICO_INTERNAL int PicoFrameMCD(void)
 {
   if(!(PicoOpt&0x10))
     PicoFrameStart();