spu: get rid of bStop, clean up
[pcsx_rearmed.git] / plugins / dfsound / registers.c
index 1a51cd7..bc99d9a 100644 (file)
 \r
 #include "externals.h"\r
 #include "registers.h"\r
-\r
-/*\r
-// adsr time values (in ms) by James Higgs ... see the end of\r
-// the adsr.c source for details\r
-\r
-#define ATTACK_MS     514L\r
-#define DECAYHALF_MS  292L\r
-#define DECAY_MS      584L\r
-#define SUSTAIN_MS    450L\r
-#define RELEASE_MS    446L\r
-*/\r
-\r
-// we have a timebase of 1.020408f ms, not 1 ms... so adjust adsr defines\r
-#define ATTACK_MS      494L\r
-#define DECAYHALF_MS   286L\r
-#define DECAY_MS       572L\r
-#define SUSTAIN_MS     441L\r
-#define RELEASE_MS     437L\r
+#include "spu_config.h"\r
 \r
 static void SoundOn(int start,int end,unsigned short val);\r
 static void SoundOff(int start,int end,unsigned short val);\r
@@ -53,10 +36,28 @@ static void ReverbOn(int start,int end,unsigned short val);
 // WRITE REGISTERS: called by main emu\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)\r
+static const uint32_t ignore_dupe[8] = {\r
+ // ch 0-15  c40         c80         cc0\r
+ 0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f, 0x7f7f7f7f,\r
+ // ch 16-24 d40         control     reverb\r
+ 0x7f7f7f7f, 0x7f7f7f7f, 0xff05ff0f, 0xffffffff\r
+};\r
+\r
+void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val,\r
+ unsigned int cycles)\r
 {\r
- const unsigned long r=reg&0xfff;\r
- regArea[(r-0xc00)>>1] = val;\r
+ int r = reg & 0xfff;\r
+ int rofs = (r - 0xc00) >> 1;\r
+ int changed = spu.regArea[rofs] != val;\r
+ spu.regArea[rofs] = val;\r
+\r
+ if (!changed && (ignore_dupe[rofs >> 5] & (1 << (rofs & 0x1f))))\r
+  return;\r
+ // zero keyon/keyoff?\r
+ if (val == 0 && (r & 0xff8) == 0xd88)\r
+  return;\r
+\r
+ do_samples_if_needed(cycles);\r
 \r
  if(r>=0x0c00 && r<0x0d80)                             // some channel info?\r
   {\r
@@ -74,7 +75,7 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
      //------------------------------------------------// pitch\r
      case 4:                                           \r
        SetPitch(ch,val);\r
-       break;\r
+       goto upd_irq;\r
      //------------------------------------------------// start\r
      case 6:      \r
        // taken from regArea later\r
@@ -89,35 +90,6 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
         s_chan[ch].ADSRX.DecayRate=(lval>>4) & 0x000f;\r
         s_chan[ch].ADSRX.SustainLevel=lval & 0x000f;\r
         //---------------------------------------------//\r
-#if 0\r
-        if(!iDebugMode) break;\r
-        //---------------------------------------------// stuff below is only for debug mode\r
-\r
-        s_chan[ch].ADSR.AttackModeExp=(lval&0x8000)?1:0;        //0x007f\r
-\r
-        lx=(((lval>>8) & 0x007f)>>2);                  // attack time to run from 0 to 100% volume\r
-        lx=min(31,lx);                                 // no overflow on shift!\r
-        if(lx) \r
-         { \r
-          lx = (1<<lx);\r
-          if(lx<2147483) lx=(lx*ATTACK_MS)/10000L;     // another overflow check\r
-          else           lx=(lx/10000L)*ATTACK_MS;\r
-          if(!lx) lx=1;\r
-         }\r
-        s_chan[ch].ADSR.AttackTime=lx;                \r
-\r
-        s_chan[ch].ADSR.SustainLevel=                 // our adsr vol runs from 0 to 1024, so scale the sustain level\r
-         (1024*((lval) & 0x000f))/15;\r
-\r
-        lx=(lval>>4) & 0x000f;                         // decay:\r
-        if(lx)                                         // our const decay value is time it takes from 100% to 0% of volume\r
-         {\r
-          lx = ((1<<(lx))*DECAY_MS)/10000L;\r
-          if(!lx) lx=1;\r
-         }\r
-        s_chan[ch].ADSR.DecayTime =                   // so calc how long does it take to run from 100% to the wanted sus level\r
-         (lx*(1024-s_chan[ch].ADSR.SustainLevel))/1024;\r
-#endif\r
        }\r
       break;\r
      //------------------------------------------------// adsr times with pre-calcs\r
@@ -132,39 +104,6 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
        s_chan[ch].ADSRX.ReleaseModeExp = (lval&0x0020)?1:0;\r
        s_chan[ch].ADSRX.ReleaseRate = lval & 0x001f;\r
        //----------------------------------------------//\r
-#if 0\r
-       if(!iDebugMode) break;\r
-       //----------------------------------------------// stuff below is only for debug mode\r
-\r
-       s_chan[ch].ADSR.SustainModeExp = (lval&0x8000)?1:0;\r
-       s_chan[ch].ADSR.ReleaseModeExp = (lval&0x0020)?1:0;\r
-                   \r
-       lx=((((lval>>6) & 0x007f)>>2));                 // sustain time... often very high\r
-       lx=min(31,lx);                                  // values are used to hold the volume\r
-       if(lx)                                          // until a sound stop occurs\r
-        {                                              // the highest value we reach (due to \r
-         lx = (1<<lx);                                 // overflow checking) is: \r
-         if(lx<2147483) lx=(lx*SUSTAIN_MS)/10000L;     // 94704 seconds = 1578 minutes = 26 hours... \r
-         else           lx=(lx/10000L)*SUSTAIN_MS;     // should be enuff... if the stop doesn't \r
-         if(!lx) lx=1;                                 // come in this time span, I don't care :)\r
-        }\r
-       s_chan[ch].ADSR.SustainTime = lx;\r
-\r
-       lx=(lval & 0x001f);\r
-       s_chan[ch].ADSR.ReleaseVal     =lx;\r
-       if(lx)                                          // release time from 100% to 0%\r
-        {                                              // note: the release time will be\r
-         lx = (1<<lx);                                 // adjusted when a stop is coming,\r
-         if(lx<2147483) lx=(lx*RELEASE_MS)/10000L;     // so at this time the adsr vol will \r
-         else           lx=(lx/10000L)*RELEASE_MS;     // run from (current volume) to 0%\r
-         if(!lx) lx=1;\r
-        }\r
-       s_chan[ch].ADSR.ReleaseTime=lx;\r
-\r
-       if(lval & 0x4000)                               // add/dec flag\r
-            s_chan[ch].ADSR.SustainModeDec=-1;\r
-       else s_chan[ch].ADSR.SustainModeDec=1;\r
-#endif\r
       }\r
      break;\r
      //------------------------------------------------// adsr volume... mmm have to investigate this\r
@@ -172,12 +111,8 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
        break;\r
      //------------------------------------------------//\r
      case 14:                                          // loop?\r
-       s_chan[ch].pLoop=spuMemC+((val&~1)<<3);\r
-       if(s_chan[ch].bJump)\r
-        // real machine would be most likely still doing the last block and use new value for the jump;\r
-        // but we decode ahead a bit and already did the jump part, so compensate for that now.\r
-        s_chan[ch].pCurr=s_chan[ch].pLoop;\r
-       break;\r
+       s_chan[ch].pLoop=spu.spuMemC+((val&~1)<<3);\r
+       goto upd_irq;\r
      //------------------------------------------------//\r
     }\r
    return;\r
@@ -187,23 +122,26 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
    {\r
     //-------------------------------------------------//\r
     case H_SPUaddr:\r
-      spuAddr = (unsigned long) val<<3;\r
+      spu.spuAddr = (unsigned long) val<<3;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUdata:\r
-      spuMem[spuAddr>>1] = val;\r
-      spuAddr+=2;\r
-      if(spuAddr>0x7ffff) spuAddr=0;\r
+      spu.spuMem[spu.spuAddr>>1] = val;\r
+      spu.spuAddr+=2;\r
+      if(spu.spuAddr>0x7ffff) spu.spuAddr=0;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUctrl:\r
-      if(!(spuCtrl & CTRL_IRQ))\r
-        spuStat&=~STAT_IRQ;\r
-      spuCtrl=val;\r
+      if (!(spu.spuCtrl & CTRL_IRQ)) {\r
+        spu.spuStat&=~STAT_IRQ;\r
+        if (val & CTRL_IRQ)\r
+         schedule_next_irq();\r
+      }\r
+      spu.spuCtrl=val;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUstat:\r
-      spuStat=val & 0xf800;\r
+      spu.spuStat=val&0xf800;\r
       break;\r
     //-------------------------------------------------//\r
     case H_SPUReverbAddr:\r
@@ -218,13 +156,11 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
           rvb.CurrAddr=rvb.StartAddr;\r
          }\r
        }\r
-      rvb.dirty = 1;\r
-      break;\r
+      goto rvbd;\r
     //-------------------------------------------------//\r
     case H_SPUirqAddr:\r
-      spuIrq = val;\r
-      pSpuIrq=spuMemC+(((unsigned long) val<<3)&~0xf);\r
-      break;\r
+      spu.pSpuIrq=spu.spuMemC+(((unsigned long) val<<3)&~0xf);\r
+      goto upd_irq;\r
     //-------------------------------------------------//\r
     case H_SPUrvolL:\r
       rvb.VolLeft=val;\r
@@ -278,12 +214,12 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
       break;\r
     //-------------------------------------------------//\r
     case H_CDLeft:\r
-      iLeftXAVol=val  & 0x7fff;\r
-      if(cddavCallback) cddavCallback(0,val);\r
+      spu.iLeftXAVol=val  & 0x7fff;\r
+      if(spu.cddavCallback) spu.cddavCallback(0,val);\r
       break;\r
     case H_CDRight:\r
-      iRightXAVol=val & 0x7fff;\r
-      if(cddavCallback) cddavCallback(1,val);\r
+      spu.iRightXAVol=val & 0x7fff;\r
+      if(spu.cddavCallback) spu.cddavCallback(1,val);\r
       break;\r
     //-------------------------------------------------//\r
     case H_FMod1:\r
@@ -310,42 +246,48 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
       ReverbOn(16,24,val);\r
       break;\r
     //-------------------------------------------------//\r
-    case H_Reverb+0   : rvb.FB_SRC_A=val*4;            break;\r
-    case H_Reverb+2   : rvb.FB_SRC_B=val*4;            break;\r
-    case H_Reverb+4   : rvb.IIR_ALPHA=(short)val;      break;\r
-    case H_Reverb+6   : rvb.ACC_COEF_A=(short)val;     break;\r
-    case H_Reverb+8   : rvb.ACC_COEF_B=(short)val;     break;\r
-    case H_Reverb+10  : rvb.ACC_COEF_C=(short)val;     break;\r
-    case H_Reverb+12  : rvb.ACC_COEF_D=(short)val;     break;\r
-    case H_Reverb+14  : rvb.IIR_COEF=(short)val;       break;\r
-    case H_Reverb+16  : rvb.FB_ALPHA=(short)val;       break;\r
-    case H_Reverb+18  : rvb.FB_X=(short)val;           break;\r
-    case H_Reverb+20  : rvb.IIR_DEST_A0=val*4;         break;\r
-    case H_Reverb+22  : rvb.IIR_DEST_A1=val*4;         break;\r
-    case H_Reverb+24  : rvb.ACC_SRC_A0=val*4;          break;\r
-    case H_Reverb+26  : rvb.ACC_SRC_A1=val*4;          break;\r
-    case H_Reverb+28  : rvb.ACC_SRC_B0=val*4;          break;\r
-    case H_Reverb+30  : rvb.ACC_SRC_B1=val*4;          break;\r
-    case H_Reverb+32  : rvb.IIR_SRC_A0=val*4;          break;\r
-    case H_Reverb+34  : rvb.IIR_SRC_A1=val*4;          break;\r
-    case H_Reverb+36  : rvb.IIR_DEST_B0=val*4;         break;\r
-    case H_Reverb+38  : rvb.IIR_DEST_B1=val*4;         break;\r
-    case H_Reverb+40  : rvb.ACC_SRC_C0=val*4;          break;\r
-    case H_Reverb+42  : rvb.ACC_SRC_C1=val*4;          break;\r
-    case H_Reverb+44  : rvb.ACC_SRC_D0=val*4;          break;\r
-    case H_Reverb+46  : rvb.ACC_SRC_D1=val*4;          break;\r
-    case H_Reverb+48  : rvb.IIR_SRC_B1=val*4;          break;\r
-    case H_Reverb+50  : rvb.IIR_SRC_B0=val*4;          break;\r
-    case H_Reverb+52  : rvb.MIX_DEST_A0=val*4;         break;\r
-    case H_Reverb+54  : rvb.MIX_DEST_A1=val*4;         break;\r
-    case H_Reverb+56  : rvb.MIX_DEST_B0=val*4;         break;\r
-    case H_Reverb+58  : rvb.MIX_DEST_B1=val*4;         break;\r
-    case H_Reverb+60  : rvb.IN_COEF_L=(short)val;      break;\r
-    case H_Reverb+62  : rvb.IN_COEF_R=(short)val;      break;\r
+    case H_Reverb+0   : rvb.FB_SRC_A=val*4;         goto rvbd;\r
+    case H_Reverb+2   : rvb.FB_SRC_B=val*4;         goto rvbd;\r
+    case H_Reverb+4   : rvb.IIR_ALPHA=(short)val;   goto rvbd;\r
+    case H_Reverb+6   : rvb.ACC_COEF_A=(short)val;  goto rvbd;\r
+    case H_Reverb+8   : rvb.ACC_COEF_B=(short)val;  goto rvbd;\r
+    case H_Reverb+10  : rvb.ACC_COEF_C=(short)val;  goto rvbd;\r
+    case H_Reverb+12  : rvb.ACC_COEF_D=(short)val;  goto rvbd;\r
+    case H_Reverb+14  : rvb.IIR_COEF=(short)val;    goto rvbd;\r
+    case H_Reverb+16  : rvb.FB_ALPHA=(short)val;    goto rvbd;\r
+    case H_Reverb+18  : rvb.FB_X=(short)val;        goto rvbd;\r
+    case H_Reverb+20  : rvb.IIR_DEST_A0=val*4;      goto rvbd;\r
+    case H_Reverb+22  : rvb.IIR_DEST_A1=val*4;      goto rvbd;\r
+    case H_Reverb+24  : rvb.ACC_SRC_A0=val*4;       goto rvbd;\r
+    case H_Reverb+26  : rvb.ACC_SRC_A1=val*4;       goto rvbd;\r
+    case H_Reverb+28  : rvb.ACC_SRC_B0=val*4;       goto rvbd;\r
+    case H_Reverb+30  : rvb.ACC_SRC_B1=val*4;       goto rvbd;\r
+    case H_Reverb+32  : rvb.IIR_SRC_A0=val*4;       goto rvbd;\r
+    case H_Reverb+34  : rvb.IIR_SRC_A1=val*4;       goto rvbd;\r
+    case H_Reverb+36  : rvb.IIR_DEST_B0=val*4;      goto rvbd;\r
+    case H_Reverb+38  : rvb.IIR_DEST_B1=val*4;      goto rvbd;\r
+    case H_Reverb+40  : rvb.ACC_SRC_C0=val*4;       goto rvbd;\r
+    case H_Reverb+42  : rvb.ACC_SRC_C1=val*4;       goto rvbd;\r
+    case H_Reverb+44  : rvb.ACC_SRC_D0=val*4;       goto rvbd;\r
+    case H_Reverb+46  : rvb.ACC_SRC_D1=val*4;       goto rvbd;\r
+    case H_Reverb+48  : rvb.IIR_SRC_B1=val*4;       goto rvbd;\r
+    case H_Reverb+50  : rvb.IIR_SRC_B0=val*4;       goto rvbd;\r
+    case H_Reverb+52  : rvb.MIX_DEST_A0=val*4;      goto rvbd;\r
+    case H_Reverb+54  : rvb.MIX_DEST_A1=val*4;      goto rvbd;\r
+    case H_Reverb+56  : rvb.MIX_DEST_B0=val*4;      goto rvbd;\r
+    case H_Reverb+58  : rvb.MIX_DEST_B1=val*4;      goto rvbd;\r
+    case H_Reverb+60  : rvb.IN_COEF_L=(short)val;   goto rvbd;\r
+    case H_Reverb+62  : rvb.IN_COEF_R=(short)val;   goto rvbd;\r
    }\r
+ return;\r
+\r
+upd_irq:\r
+ if (spu.spuCtrl & CTRL_IRQ)\r
+  schedule_next_irq();\r
+ return;\r
 \r
- if ((r & ~0x3f) == H_Reverb)\r
 rvb.dirty = 1; // recalculate on next update\r
+rvbd:\r
+ rvb.dirty = 1; // recalculate on next update\r
 }\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
@@ -363,8 +305,8 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
      case 12:                                          // get adsr vol\r
       {\r
        const int ch=(r>>4)-0xc0;\r
-       if(dwNewChannel&(1<<ch)) return 1;              // we are started, but not processed? return 1\r
-       if((dwChannelOn&(1<<ch)) &&                     // same here... we haven't decoded one sample yet, so no envelope yet. return 1 as well\r
+       if(spu.dwNewChannel&(1<<ch)) return 1;          // we are started, but not processed? return 1\r
+       if((spu.dwChannelOn&(1<<ch)) &&                 // same here... we haven't decoded one sample yet, so no envelope yet. return 1 as well\r
           !s_chan[ch].ADSRX.EnvelopeVol)\r
         return 1;\r
        return (unsigned short)(s_chan[ch].ADSRX.EnvelopeVol>>16);\r
@@ -373,7 +315,7 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
      case 14:                                          // get loop address\r
       {\r
        const int ch=(r>>4)-0xc0;\r
-       return (unsigned short)((s_chan[ch].pLoop-spuMemC)>>3);\r
+       return (unsigned short)((s_chan[ch].pLoop-spu.spuMemC)>>3);\r
       }\r
     }\r
   }\r
@@ -381,25 +323,22 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
  switch(r)\r
   {\r
     case H_SPUctrl:\r
-     return spuCtrl;\r
+     return spu.spuCtrl;\r
 \r
     case H_SPUstat:\r
-     return spuStat;\r
+     return spu.spuStat;\r
         \r
     case H_SPUaddr:\r
-     return (unsigned short)(spuAddr>>3);\r
+     return (unsigned short)(spu.spuAddr>>3);\r
 \r
     case H_SPUdata:\r
      {\r
-      unsigned short s=spuMem[spuAddr>>1];\r
-      spuAddr+=2;\r
-      if(spuAddr>0x7ffff) spuAddr=0;\r
+      unsigned short s=spu.spuMem[spu.spuAddr>>1];\r
+      spu.spuAddr+=2;\r
+      if(spu.spuAddr>0x7ffff) spu.spuAddr=0;\r
       return s;\r
      }\r
 \r
-    case H_SPUirqAddr:\r
-     return spuIrq;\r
-\r
     //case H_SPUIsOn1:\r
     // return IsSoundOn(0,16);\r
 \r
@@ -408,7 +347,7 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
  \r
   }\r
 \r
- return regArea[(r-0xc00)>>1];\r
+ return spu.regArea[(r-0xc00)>>1];\r
 }\r
  \r
 ////////////////////////////////////////////////////////////////////////\r
@@ -423,16 +362,9 @@ static void SoundOn(int start,int end,unsigned short val)
   {\r
    if((val&1) && regAreaGet(ch,6))                     // mmm... start has to be set before key on !?!\r
     {\r
-     // do this here, not in StartSound\r
-     // - fixes fussy timing issues\r
-     s_chan[ch].bStop=0;\r
-     s_chan[ch].pCurr=spuMemC+((regAreaGet(ch,6)&~1)<<3); // must be block aligned\r
-     s_chan[ch].pLoop=spuMemC+((regAreaGet(ch,14)&~1)<<3);\r
-     s_chan[ch].bJump=0;\r
-\r
-     dwNewChannel|=(1<<ch);                            // bitfield for faster testing\r
-     dwChannelOn|=1<<ch;\r
-     dwChannelDead&=~(1<<ch);\r
+     s_chan[ch].pCurr=spu.spuMemC+((regAreaGet(ch,6)&~1)<<3); // must be block aligned\r
+     s_chan[ch].pLoop=spu.spuMemC+((regAreaGet(ch,14)&~1)<<3);\r
+     spu.dwNewChannel|=(1<<ch);\r
     }\r
   }\r
 }\r
@@ -446,13 +378,13 @@ static void SoundOff(int start,int end,unsigned short val)
  int ch;\r
  for(ch=start;ch<end;ch++,val>>=1)                     // loop channels\r
   {\r
-   if(val&1)                                           // && s_chan[i].bOn)  mmm...\r
+   if(val&1)\r
     {\r
-     s_chan[ch].bStop=1;\r
+     s_chan[ch].ADSRX.State = ADSR_RELEASE;\r
 \r
      // Jungle Book - Rhythm 'n Groove\r
      // - turns off buzzing sound (loop hangs)\r
-     dwNewChannel &= ~(1<<ch);\r
+     spu.dwNewChannel &= ~(1<<ch);\r
     }                                                  \r
   }\r
 }\r
@@ -565,7 +497,8 @@ static void SetPitch(int ch,unsigned short val)               // SET PITCH
 \r
  s_chan[ch].iRawPitch=NP;\r
  s_chan[ch].sinc=(NP<<4)|8;\r
- if(iUseInterpolation==1) s_chan[ch].SB[32]=1;         // -> freq change in simple interpolation mode: set flag\r
+ s_chan[ch].sinc_inv=0;\r
+ if(spu_config.iUseInterpolation==1) s_chan[ch].SB[32]=1; // -> freq change in simple interpolation mode: set flag\r
 }\r
 \r
 ////////////////////////////////////////////////////////////////////////\r