cdrom: change pause timing again
[pcsx_rearmed.git] / plugins / dfsound / registers.c
index bcac4d9..6d72d3c 100644 (file)
@@ -60,7 +60,7 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,
  if (val == 0 && (r & 0xff8) == 0xd88)\r
   return;\r
 \r
- do_samples_if_needed(cycles, 0);\r
+ do_samples_if_needed(cycles, 0, 16);\r
 \r
  if(r>=0x0c00 && r<0x0d80)                             // some channel info?\r
   {\r
@@ -132,7 +132,7 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,
    {\r
     //-------------------------------------------------//\r
     case H_SPUaddr:\r
-      spu.spuAddr = (unsigned long) val<<3;\r
+      spu.spuAddr = (unsigned int)val << 3;\r
       //check_irq_io(spu.spuAddr);\r
       break;\r
     //-------------------------------------------------//\r
@@ -144,8 +144,9 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,
       break;\r
     //-------------------------------------------------//\r
     case H_SPUctrl:\r
+      spu.spuStat = (spu.spuStat & ~0xbf) | (val & 0x3f) | ((val << 2) & 0x80);\r
+      spu.spuStat &= ~STAT_IRQ | val;\r
       if (!(spu.spuCtrl & CTRL_IRQ)) {\r
-        spu.spuStat&=~STAT_IRQ;\r
         if (val & CTRL_IRQ)\r
          schedule_next_irq();\r
       }\r
@@ -153,7 +154,7 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,
       break;\r
     //-------------------------------------------------//\r
     case H_SPUstat:\r
-      spu.spuStat=val&0xf800;\r
+      //spu.spuStat=val&0xf800;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUReverbAddr:\r
@@ -167,11 +168,11 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,
       goto upd_irq;\r
     //-------------------------------------------------//\r
     case H_SPUrvolL:\r
-      spu.rvb->VolLeft=val;\r
+      spu.rvb->VolLeft = (int16_t)val;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUrvolR:\r
-      spu.rvb->VolRight=val;\r
+      spu.rvb->VolRight = (int16_t)val;\r
       break;\r
     //-------------------------------------------------//\r
 \r
@@ -213,28 +214,44 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,
 */\r
     //-------------------------------------------------//\r
     case H_SPUon1:\r
+      spu.last_keyon_cycles = cycles;\r
+      do_samples_if_needed(cycles, 0, 2);\r
       SoundOn(0,16,val);\r
       break;\r
     //-------------------------------------------------//\r
-     case H_SPUon2:\r
+    case H_SPUon2:\r
+      spu.last_keyon_cycles = cycles;\r
+      do_samples_if_needed(cycles, 0, 2);\r
       SoundOn(16,24,val);\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUoff1:\r
+      if (cycles - spu.last_keyon_cycles < 786u) {\r
+       if (val & regAreaGet(H_SPUon1))\r
+        log_unhandled("koff1 %04x %d\n", val, cycles - spu.last_keyon_cycles);\r
+       val &= ~regAreaGet(H_SPUon1);\r
+      }\r
+      do_samples_if_needed(cycles, 0, 2);\r
       SoundOff(0,16,val);\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUoff2:\r
+      if (cycles - spu.last_keyon_cycles < 786u) {\r
+       if (val & regAreaGet(H_SPUon1))\r
+        log_unhandled("koff2 %04x %d\n", val, cycles - spu.last_keyon_cycles);\r
+       val &= ~regAreaGet(H_SPUon2);\r
+      }\r
+      do_samples_if_needed(cycles, 0, 2);\r
       SoundOff(16,24,val);\r
       break;\r
     //-------------------------------------------------//\r
     case H_CDLeft:\r
       spu.iLeftXAVol=(int16_t)val;\r
-      if(spu.cddavCallback) spu.cddavCallback(0,(int16_t)val);\r
+      //if(spu.cddavCallback) spu.cddavCallback(0,(int16_t)val);\r
       break;\r
     case H_CDRight:\r
       spu.iRightXAVol=(int16_t)val;\r
-      if(spu.cddavCallback) spu.cddavCallback(1,(int16_t)val);\r
+      //if(spu.cddavCallback) spu.cddavCallback(1,(int16_t)val);\r
       break;\r
     //-------------------------------------------------//\r
     case H_FMod1:\r
@@ -309,7 +326,7 @@ rvbd:
 // READ REGISTER: called by main emu\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-unsigned short CALLBACK SPUreadRegister(unsigned long reg)\r
+unsigned short CALLBACK SPUreadRegister(unsigned long reg, unsigned int cycles)\r
 {\r
  const unsigned long r = reg & 0xffe;\r
         \r
@@ -319,12 +336,13 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
     {\r
      case 12:                                          // get adsr vol\r
       {\r
-       const int ch=(r>>4)-0xc0;\r
-       if(spu.dwNewChannel&(1<<ch)) return 1;          // we are started, but not processed? return 1\r
-       if((spu.dwChannelsAudible&(1<<ch)) &&           // same here... we haven't decoded one sample yet, so no envelope yet. return 1 as well\r
-          !spu.s_chan[ch].ADSRX.EnvelopeVol)\r
-        return 1;\r
-       return (unsigned short)(spu.s_chan[ch].ADSRX.EnvelopeVol>>16);\r
+       // this used to return 1 immediately after keyon to deal with\r
+       // some poor timing, but that causes Rayman 2 to lose track of\r
+       // it's channels on busy scenes and start looping some of them forever\r
+       const int ch = (r>>4) - 0xc0;\r
+       if (spu.s_chan[ch].bStarting)\r
+        do_samples_if_needed(cycles, 0, 2);\r
+       return (unsigned short)(spu.s_chan[ch].ADSRX.EnvelopeVol >> 16);\r
       }\r
 \r
      case 14:                                          // get loop address\r
@@ -348,7 +366,7 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
      return spu.spuCtrl;\r
 \r
     case H_SPUstat:\r
-     return (spu.spuStat & ~0x3F) | (spu.spuCtrl & 0x3F);\r
+     return spu.spuStat;\r
         \r
     case H_SPUaddr:\r
      return (unsigned short)(spu.spuAddr>>3);\r
@@ -404,6 +422,7 @@ static void SoundOn(int start,int end,unsigned short val)
    if((val&1) && regAreaGetCh(ch, 6))                  // mmm... start has to be set before key on !?!\r
     {\r
      spu.s_chan[ch].bIgnoreLoop = 0;\r
+     spu.s_chan[ch].bStarting = 1;\r
      spu.dwNewChannel|=(1<<ch);\r
     }\r
   }\r
@@ -416,7 +435,7 @@ static void SoundOn(int start,int end,unsigned short val)
 static void SoundOff(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
- for(ch=start;ch<end;ch++,val>>=1)                     // loop channels\r
+ for (ch = start; val && ch < end; ch++, val >>= 1)    // loop channels\r
   {\r
    if(val&1)\r
     {\r
@@ -542,7 +561,6 @@ static void SetPitch(int ch,unsigned short val)               // SET PITCH
  spu.s_chan[ch].iRawPitch = NP;\r
  spu.s_chan[ch].sinc = NP << 4;\r
  spu.s_chan[ch].sinc_inv = 0;\r
- spu.s_chan[ch].bNewPitch = 1;\r
 \r
  // don't mess spu.dwChannelsAudible as adsr runs independently\r
 }\r
@@ -560,3 +578,5 @@ static void ReverbOn(int start,int end,unsigned short val)
    spu.s_chan[ch].bReverb=val&1;                       // -> reverb on/off\r
   }\r
 }\r
+\r
+// vim:shiftwidth=1:expandtab\r