psp gfx scaling/etc stuff
[picodrive.git] / Pico / Pico.c
index 73321bc..22bdb19 100644 (file)
@@ -16,14 +16,13 @@ int PicoOpt=0; // disable everything by default
 int PicoSkipFrame=0; // skip rendering frame?\r
 int PicoRegionOverride = 0; // override the region detection 0: Auto, 1: Japan NTSC, 2: Japan PAL, 4: US, 8: Europe\r
 int PicoAutoRgnOrder = 0;\r
-int emustatus = 0;\r
+int emustatus = 0; // rapid_ym2612, multi_ym_updates\r
 void (*PicoWriteSound)(int len) = 0; // called once per frame at the best time to send sound buffer (PsndOut) to hardware\r
 \r
 struct PicoSRAM SRam = {0,};\r
 int z80startCycle, z80stopCycle; // in 68k cycles\r
-//int z80ExtraCycles = 0;\r
 int PicoPad[2];  // Joypads, format is SACB RLDU\r
-int PicoMCD = 0; // mega CD status: scd_started, reset_pending\r
+int PicoMCD = 0; // mega CD status: scd_started\r
 \r
 // to be called once on emu init\r
 int PicoInit(void)\r
@@ -199,7 +198,7 @@ PICO_INTERNAL int CheckDMA(void)
   return burn;\r
 }\r
 \r
-static __inline void SekRun(int cyc)\r
+static __inline void SekRunM68k(int cyc)\r
 {\r
   int cyc_do;\r
   SekCycleAim+=cyc;\r
@@ -210,15 +209,13 @@ static __inline void SekRun(int cyc)
   // this means we do run-compare Cyclone vs Musashi\r
   SekCycleCnt+=CM_compareRun(cyc_do);\r
 #elif defined(EMU_C68K)\r
-  PicoCpu.cycles=cyc_do;\r
-  CycloneRun(&PicoCpu);\r
-  SekCycleCnt+=cyc_do-PicoCpu.cycles;\r
-#elif defined(EMU_A68K)\r
-  m68k_ICount=cyc_do;\r
-  M68000_RUN();\r
-  SekCycleCnt+=cyc_do-m68k_ICount;\r
+  PicoCpuCM68k.cycles=cyc_do;\r
+  CycloneRun(&PicoCpuCM68k);\r
+  SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;\r
 #elif defined(EMU_M68K)\r
   SekCycleCnt+=m68k_execute(cyc_do);\r
+#elif defined(EMU_F68K)\r
+  SekCycleCnt+=m68k_emulate(cyc_do);\r
 #endif\r
 }\r
 \r
@@ -230,41 +227,30 @@ static __inline void SekStep(void)
   // this means we do run-compare Cyclone vs Musashi\r
   SekCycleCnt+=CM_compareRun(1);\r
 #elif defined(EMU_C68K)\r
-  PicoCpu.cycles=1;\r
-  CycloneRun(&PicoCpu);\r
-  SekCycleCnt+=1-PicoCpu.cycles;\r
-#elif defined(EMU_A68K)\r
-  m68k_ICount=1;\r
-  M68000_RUN();\r
-  SekCycleCnt+=1-m68k_ICount;\r
+  PicoCpuCM68k.cycles=1;\r
+  CycloneRun(&PicoCpuCM68k);\r
+  SekCycleCnt+=1-PicoCpuCM68k.cycles;\r
 #elif defined(EMU_M68K)\r
   SekCycleCnt+=m68k_execute(1);\r
+#elif defined(EMU_F68K)\r
+  SekCycleCnt+=m68k_emulate(1);\r
 #endif\r
   SekCycleAim=realaim;\r
 }\r
 \r
 static int CheckIdle(void)\r
 {\r
-#if 1\r
-  unsigned char state[0x88];\r
-\r
-  memset(state,0,sizeof(state));\r
+  int i, state[0x22];\r
 \r
   // See if the state is the same after 2 steps:\r
-  SekState(state); SekStep(); SekStep(); SekState(state+0x44);\r
-  if (memcmp(state,state+0x44,0x44)==0) return 1;\r
-#else\r
-  unsigned char state[0x44];\r
-  static unsigned char oldstate[0x44];\r
-\r
-  SekState(state);\r
-  if(memcmp(state,oldstate,0x40)==0) return 1;\r
-  memcpy(oldstate, state, 0x40);\r
-#endif\r
+  SekState(state); SekStep(); SekStep(); SekState(state+0x11);\r
+  for (i = 0x10; i >= 0; i--)\r
+    if (state[i] != state[i+0x11]) return 0;\r
 \r
-  return 0;\r
+  return 1;\r
 }\r
 \r
+\r
 // to be called on 224 or line_sample scanlines only\r
 static __inline void getSamples(int y)\r
 {\r
@@ -287,159 +273,36 @@ static __inline void getSamples(int y)
 }\r
 \r
 \r
-#if 1*0\r
-int vint_delay = 205/*68*/, as_delay = 18/*148*/;\r
-\r
-// Accurate but slower frame which does hints\r
-static int PicoFrameHints(void)\r
-{\r
-  struct PicoVideo *pv=&Pico.video;\r
-  int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;\r
-  const int cycles_68k=488,cycles_z80=228; // both PAL and NTSC compile to same values\r
-  int skip=PicoSkipFrame || (PicoOpt&0x10);\r
-  int hint; // Hint counter\r
-\r
-  if(Pico.m.pal) { //\r
-    //cycles_68k = (int) ((double) OSC_PAL  /  7 / 50 / 312 + 0.4); // should compile to a constant (488)\r
-    //cycles_z80 = (int) ((double) OSC_PAL  / 15 / 50 / 312 + 0.4); // 228\r
-    lines  = 312;    // Steve Snake says there are 313 lines, but this seems to also work well\r
-    line_sample = 68;\r
-    if(pv->reg[1]&8) lines_vis = 240;\r
-  } else {\r
-    //cycles_68k = (int) ((double) OSC_NTSC /  7 / 60 / 262 + 0.4); // 488\r
-    //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228\r
-    lines  = 262;\r
-    line_sample = 93;\r
-  }\r
-\r
-  SekCyclesReset();\r
-  //z80ExtraCycles = 0;\r
-\r
-  if(PicoOpt&4)\r
-    z80CycleAim = 0;\r
-//    z80_resetCycles();\r
-\r
-  pv->status&=~0x88; // clear V-Int, come out of vblank\r
-\r
-  hint=pv->reg[10]; // Load H-Int counter\r
-  //dprintf("-hint: %i", hint);\r
-\r
-  //SekRun(as_delay);\r
-  SekRun(148);\r
-\r
-  for (y=0;y<lines;y++)\r
-  {\r
-    Pico.m.scanline=(short)y;\r
-\r
-    // VDP FIFO\r
-    pv->lwrite_cnt -= 12;\r
-    if (pv->lwrite_cnt <  0) pv->lwrite_cnt=0;\r
-    if (pv->lwrite_cnt == 0)\r
-      Pico.video.status|=0x200;\r
-\r
-    // pad delay (for 6 button pads)\r
-    if(PicoOpt&0x20) {\r
-      if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0;\r
-      if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0;\r
-    }\r
-\r
-    // H-Interrupts:\r
-    if(y <= lines_vis && --hint < 0) // y <= lines_vis: Comix Zone, Golden Axe\r
-    {\r
-      //dprintf("rhint:old @ %06x", SekPc);\r
-      hint=pv->reg[10]; // Reload H-Int counter\r
-      pv->pending_ints|=0x10;\r
-      if (pv->reg[0]&0x10) {\r
-        elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);\r
-        SekInterrupt(4);\r
-      }\r
-      //dprintf("hint_routine: %x", (*(unsigned short*)(Pico.ram+0x0B84)<<16)|*(unsigned short*)(Pico.ram+0x0B86));\r
-    }\r
-\r
-    // V-Interrupt:\r
-    if (y == lines_vis)\r
-    {\r
-      pv->status|=0x08; // go into vblank\r
-      //pv->status|=0x80; // V-Int happened\r
-      //if(!Pico.m.dma_bytes||(Pico.video.reg[0x17]&0x80)) {\r
-        // there must be a gap between H and V ints, also after vblank bit set (Mazin Saga, Bram Stoker's Dracula)\r
-        SekRun(68); SekCycleAim-=68; // 128; ?\r
-        SekCycleAim-=148;\r
-//       SekRun(vint_delay); SekCycleAim-=vint_delay; // 128; ?\r
-//     SekCycleAim-=as_delay;\r
-      //}\r
-      pv->pending_ints|=0x20;\r
-      if(pv->reg[1]&0x20) {\r
-        elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);\r
-        SekInterrupt(6);\r
-      }\r
-      if(Pico.m.z80Run && (PicoOpt&4)) // ?\r
-        z80_int();\r
-      //dprintf("zint: [%i|%i] zPC=%04x", Pico.m.scanline, SekCyclesDone(), mz80GetRegisterValue(NULL, 0));\r
-    }\r
-\r
-    // decide if we draw this line\r
-#if CAN_HANDLE_240_LINES\r
-    if(!skip && ((!(pv->reg[1]&8) && y<224) || ((pv->reg[1]&8) && y<240)) )\r
-#else\r
-    if(!skip && y<224)\r
-#endif\r
-      PicoLine(y);\r
-\r
-    if(PicoOpt&1)\r
-      sound_timers_and_dac(y);\r
-\r
-    // get samples from sound chips\r
-    if(y == 32 && PsndOut)\r
-      emustatus &= ~1;\r
-    else if((y == 224 || y == line_sample) && PsndOut)\r
-      getSamples(y);\r
-\r
-    // Run scanline:\r
-    if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());\r
-    SekRun(cycles_68k);\r
-    if ((PicoOpt&4) && Pico.m.z80Run) {\r
-      if (Pico.m.z80Run & 2) z80CycleAim+=cycles_z80;\r
-      else {\r
-        int cnt = SekCyclesDone() - z80startCycle;\r
-        cnt = (cnt>>1)-(cnt>>5);\r
-        //if (cnt > cycles_z80) printf("FIXME: z80 cycles: %i\n", cnt);\r
-        if (cnt > cycles_z80) cnt = cycles_z80;\r
-        Pico.m.z80Run |= 2;\r
-        z80CycleAim+=cnt;\r
-      }\r
-      total_z80+=z80_run(z80CycleAim-total_z80);\r
-    }\r
-  }\r
-\r
-  // draw a frame just after vblank in alternative render mode\r
-  if(!PicoSkipFrame && (PicoOpt&0x10))\r
-    PicoFrameFull();\r
-\r
-  return 0;\r
-}\r
-#else\r
 #include "PicoFrameHints.c"\r
-#endif\r
 \r
-// helper z80 runner\r
+// helper z80 runner. Runs only if z80 is enabled at this point\r
+// (z80WriteBusReq will handle the rest)\r
 static void PicoRunZ80Simple(int line_from, int line_to)\r
 {\r
-  int line_from_r=line_from, line_to_r=line_to, line = line_from;\r
+  int line_from_r=line_from, line_to_r=line_to, line=0;\r
   int line_sample = Pico.m.pal ? 68 : 93;\r
 \r
-  if(!(PicoOpt&4) || Pico.m.z80Run == 0) { line_from_r = line_to_r; line_to_r = 0; }\r
+  if (!(PicoOpt&4) || Pico.m.z80Run == 0) line_to_r = 0;\r
+  else {\r
+    extern const unsigned short vcounts[];\r
+    if (z80startCycle) {\r
+      line = vcounts[z80startCycle>>8];\r
+      if (line > line_from)\r
+        line_from_r = line;\r
+    }\r
+    z80startCycle = SekCyclesDone();\r
+  }\r
 \r
-  if(PicoOpt&1) {\r
+  if (PicoOpt&1) {\r
     // we have ym2612 enabled, so we have to run Z80 in lines, so we could update DAC and timers\r
-    for(; line < line_to; line++) {\r
+    for (line = line_from; line < line_to; line++) {\r
       sound_timers_and_dac(line);\r
-      if((line == 224 || line == line_sample) && PsndOut) getSamples(line);\r
-      if(line == 32 && PsndOut) emustatus &= ~1;\r
-      if(line >= line_from_r && line < line_to_r)\r
+      if ((line == 224 || line == line_sample) && PsndOut) getSamples(line);\r
+      if (line == 32 && PsndOut) emustatus &= ~1;\r
+      if (line >= line_from_r && line < line_to_r)\r
         z80_run(228);\r
     }\r
-  } else if(line_to_r-line_from_r > 0) {\r
+  } else if (line_to_r-line_from_r > 0) {\r
     z80_run(228*(line_to_r-line_from_r));\r
     // samples will be taken by caller\r
   }\r
@@ -453,7 +316,7 @@ static int PicoFrameSimple(void)
   int cycles_68k_vblock,cycles_68k_block;\r
 \r
   // split to 16 run calls for active scan, for vblank split to 2 (ntsc), 3 (pal 240), 4 (pal 224)\r
-  if (Pico.m.pal && (pv->reg[1]&8)) { // 240 lines\r
+  if (Pico.m.pal && (pv->reg[1]&8)) {\r
     if(pv->reg[1]&8) { // 240 lines\r
       cycles_68k_block  = 7329;  // (488*240+148)/16.0, -4\r
       cycles_68k_vblock = 11640; // (72*488-148-68)/3.0, 0\r
@@ -481,6 +344,7 @@ static int PicoFrameSimple(void)
   Pico.video.status|=0x200;\r
 \r
   Pico.m.scanline=-1;\r
+  z80startCycle=0;\r
 \r
   SekCyclesReset();\r
 \r
@@ -496,7 +360,7 @@ static int PicoFrameSimple(void)
     if (CheckIdle()) break;\r
 \r
     lines += lines_step;\r
-    SekRun(cycles_68k_block);\r
+    SekRunM68k(cycles_68k_block);\r
 \r
     PicoRunZ80Simple(line, lines);\r
     line=lines;\r
@@ -506,19 +370,12 @@ static int PicoFrameSimple(void)
   if(sects) {\r
     int c = sects*cycles_68k_block;\r
 \r
-    lines += sects*lines_step;\r
-    PicoRunZ80Simple(line, lines);\r
-    // this is for approriate line counter, etc\r
+    // this "run" is for approriate line counter, etc\r
     SekCycleCnt += c;\r
     SekCycleAim += c;\r
-  }\r
 \r
-  // here we render sound if ym2612 is disabled\r
-  if (!(PicoOpt&1) && PsndOut) {\r
-    int len = sound_render(0, PsndLen);\r
-    if (PicoWriteSound) PicoWriteSound(len);\r
-    // clear sound buffer\r
-    sound_clear();\r
+    lines += sects*lines_step;\r
+    PicoRunZ80Simple(line, lines);\r
   }\r
 \r
   // render screen\r
@@ -536,12 +393,23 @@ static int PicoFrameSimple(void)
       for (y=0;y<224;y++) PicoLine(y);\r
 #endif\r
     else PicoFrameFull();\r
+#ifdef DRAW_FINISH_FUNC\r
+    DRAW_FINISH_FUNC();\r
+#endif\r
+  }\r
+\r
+  // here we render sound if ym2612 is disabled\r
+  if (!(PicoOpt&1) && PsndOut) {\r
+    int len = sound_render(0, PsndLen);\r
+    if (PicoWriteSound) PicoWriteSound(len);\r
+    // clear sound buffer\r
+    sound_clear();\r
   }\r
 \r
   // a gap between flags set and vint\r
   pv->pending_ints|=0x20;\r
   pv->status|=8; // go into vblank\r
-  SekRun(68+4);\r
+  SekRunM68k(68+4);\r
 \r
   // ---- V-Blanking period ----\r
   // fix line counts\r
@@ -568,7 +436,7 @@ static int PicoFrameSimple(void)
   while (sects) {\r
     lines += lines_step;\r
 \r
-    SekRun(cycles_68k_vblock);\r
+    SekRunM68k(cycles_68k_vblock);\r
 \r
     PicoRunZ80Simple(line, lines);\r
     line=lines;\r
@@ -625,7 +493,7 @@ void PicoFrameDrawOnly(void)
 // callback to output message from emu\r
 void (*PicoMessage)(const char *msg)=NULL;\r
 \r
-#if defined(__DEBUG_PRINT) || defined(__GP2X__)\r
+#if 1 // defined(__DEBUG_PRINT)\r
 // tmp debug: dump some stuff\r
 #define bit(r, x) ((r>>x)&1)\r
 void z80_debug(char *dstr);\r
@@ -654,14 +522,18 @@ char *debugString(void)
   sprintf(dstrp, "sram range: %06x-%06x, reg: %02x\n", SRam.start, SRam.end, Pico.m.sram_reg); dstrp+=strlen(dstrp);\r
   sprintf(dstrp, "pend int: v:%i, h:%i, vdp status: %04x\n", bit(pv->pending_ints,5), bit(pv->pending_ints,4), pv->status);\r
   dstrp+=strlen(dstrp);\r
-#ifdef EMU_C68K\r
-  sprintf(dstrp, "M68k: PC: %06x, st_flg: %x, cycles: %u\n", SekPc, PicoCpu.state_flags, SekCyclesDoneT());\r
+#if defined(EMU_C68K)\r
+  sprintf(dstrp, "M68k: PC: %06x, st_flg: %x, cycles: %u\n", SekPc, PicoCpuCM68k.state_flags, SekCyclesDoneT());\r
   dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "d0=%08x, a0=%08x, osp=%08x, irql=%i\n", PicoCpu.d[0], PicoCpu.a[0], PicoCpu.osp, PicoCpu.irq); dstrp+=strlen(dstrp);\r
-  sprintf(dstrp, "d1=%08x, a1=%08x,  sr=%04x\n", PicoCpu.d[1], PicoCpu.a[1], CycloneGetSr(&PicoCpu)); dstrp+=strlen(dstrp);\r
+  sprintf(dstrp, "d0=%08x, a0=%08x, osp=%08x, irql=%i\n", PicoCpuCM68k.d[0], PicoCpuCM68k.a[0], PicoCpuCM68k.osp, PicoCpuCM68k.irq); dstrp+=strlen(dstrp);\r
+  sprintf(dstrp, "d1=%08x, a1=%08x,  sr=%04x\n", PicoCpuCM68k.d[1], PicoCpuCM68k.a[1], CycloneGetSr(&PicoCpuCM68k)); dstrp+=strlen(dstrp);\r
   for(r=2; r < 8; r++) {\r
-    sprintf(dstrp, "d%i=%08x, a%i=%08x\n", r, PicoCpu.d[r], r, PicoCpu.a[r]); dstrp+=strlen(dstrp);\r
+    sprintf(dstrp, "d%i=%08x, a%i=%08x\n", r, PicoCpuCM68k.d[r], r, PicoCpuCM68k.a[r]); dstrp+=strlen(dstrp);\r
   }\r
+#elif defined(EMU_M68K)\r
+  sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuMM68k.int_level>>8); dstrp+=strlen(dstrp);\r
+#elif defined(EMU_F68K)\r
+  sprintf(dstrp, "M68k: PC: %06x, cycles: %u, irql: %i\n", SekPc, SekCyclesDoneT(), PicoCpuFM68k.interrupts[0]); dstrp+=strlen(dstrp);\r
 #endif\r
   sprintf(dstrp, "z80Run: %i, pal: %i, frame#: %i\n", Pico.m.z80Run, Pico.m.pal, Pico.m.frame_count); dstrp+=strlen(dstrp);\r
   z80_debug(dstrp); dstrp+=strlen(dstrp);\r