spu: remove some hacks
[pcsx_rearmed.git] / plugins / dfsound / registers.c
index 983af8c..20a7c14 100644 (file)
 \r
 #include "externals.h"\r
 #include "registers.h"\r
-#include "regs.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
+static void SoundOn(int start,int end,unsigned short val);\r
+static void SoundOff(int start,int end,unsigned short val);\r
+static void FModOn(int start,int end,unsigned short val);\r
+static void NoiseOn(int start,int end,unsigned short val);\r
+static void SetVolumeL(unsigned char ch,short vol);\r
+static void SetVolumeR(unsigned char ch,short vol);\r
+static void SetPitch(int ch,unsigned short val);\r
+static void ReverbOn(int start,int end,unsigned short val);\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
 // 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 = regArea[rofs] != val;\r
+ 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
@@ -66,11 +74,10 @@ 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
-       // Brain Dead 13 - align to 16 boundary\r
-       s_chan[ch].pStart= spuMemC+(unsigned long)((val<<3)&~0xf);\r
+       // taken from regArea later\r
        break;\r
      //------------------------------------------------// level with pre-calcs\r
      case 8:\r
@@ -82,35 +89,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
@@ -125,39 +103,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
@@ -165,15 +110,10 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
        break;\r
      //------------------------------------------------//\r
      case 14:                                          // loop?\r
-       //WaitForSingleObject(s_chan[ch].hMutex,2000);        // -> no multithread fuckups\r
-       s_chan[ch].pLoop=spuMemC+((unsigned long)((val<<3)&~0xf));\r
-       //s_chan[ch].bIgnoreLoop=1;\r
-       //ReleaseMutex(s_chan[ch].hMutex);                    // -> oki, on with the thread\r
-       dwChannelDead&=~(1<<ch);\r
-       break;\r
+       s_chan[ch].pLoop=spuMemC+((val&~1)<<3);\r
+       goto upd_irq;\r
      //------------------------------------------------//\r
     }\r
-   iSpuAsyncWait=0;\r
    return;\r
   }\r
 \r
@@ -191,8 +131,11 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val)
       break;\r
     //-------------------------------------------------//\r
     case H_SPUctrl:\r
-      if(!(spuCtrl & CTRL_IRQ))\r
+      if (!(spuCtrl & CTRL_IRQ)) {\r
         spuStat&=~STAT_IRQ;\r
+        if (val & CTRL_IRQ)\r
+         schedule_next_irq();\r
+      }\r
       spuCtrl=val;\r
       break;\r
     //-------------------------------------------------//\r
@@ -212,13 +155,12 @@ 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
+      goto upd_irq;\r
     //-------------------------------------------------//\r
     case H_SPUrvolL:\r
       rvb.VolLeft=val;\r
@@ -304,44 +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
- if ((r & ~0x3f) == H_Reverb)\r
-  rvb.dirty = 1; // recalculate on next update\r
+upd_irq:\r
+ if (spuCtrl & CTRL_IRQ)\r
+  schedule_next_irq();\r
+ return;\r
 \r
- iSpuAsyncWait=0;\r
+rvbd:\r
+ rvb.dirty = 1; // recalculate on next update\r
 }\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
@@ -352,8 +298,6 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
 {\r
  const unsigned long r=reg&0xfff;\r
         \r
- iSpuAsyncWait=0;\r
-\r
  if(r>=0x0c00 && r<0x0d80)\r
   {\r
    switch(r&0x0f)\r
@@ -413,7 +357,7 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg)
 // SOUND ON register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void SoundOn(int start,int end,unsigned short val)     // SOUND ON PSX COMAND\r
+static void SoundOn(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
 \r
@@ -421,13 +365,12 @@ void SoundOn(int start,int end,unsigned short val)     // SOUND ON PSX COMAND
   {\r
    if((val&1) && regAreaGet(ch,6))                     // mmm... start has to be set before key on !?!\r
     {\r
-     s_chan[ch].bIgnoreLoop=0;\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].prevflags=2;\r
 \r
      dwNewChannel|=(1<<ch);                            // bitfield for faster testing\r
      dwChannelOn|=1<<ch;\r
@@ -440,7 +383,7 @@ void SoundOn(int start,int end,unsigned short val)     // SOUND ON PSX COMAND
 // SOUND OFF register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void SoundOff(int start,int end,unsigned short val)    // SOUND OFF PSX COMMAND\r
+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
@@ -460,7 +403,7 @@ void SoundOff(int start,int end,unsigned short val)    // SOUND OFF PSX COMMAND
 // FMOD register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void FModOn(int start,int end,unsigned short val)      // FMOD ON PSX COMMAND\r
+static void FModOn(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
 \r
@@ -487,7 +430,7 @@ void FModOn(int start,int end,unsigned short val)      // FMOD ON PSX COMMAND
 // NOISE register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void NoiseOn(int start,int end,unsigned short val)     // NOISE ON PSX COMMAND\r
+static void NoiseOn(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
 \r
@@ -504,7 +447,7 @@ void NoiseOn(int start,int end,unsigned short val)     // NOISE ON PSX COMMAND
 // please note: sweep and phase invert are wrong... but I've never seen\r
 // them used\r
 \r
-void SetVolumeL(unsigned char ch,short vol)            // LEFT VOLUME\r
+static void SetVolumeL(unsigned char ch,short vol)     // LEFT VOLUME\r
 {\r
  if(vol&0x8000)                                        // sweep?\r
   {\r
@@ -530,7 +473,7 @@ void SetVolumeL(unsigned char ch,short vol)            // LEFT VOLUME
 // RIGHT VOLUME register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void SetVolumeR(unsigned char ch,short vol)            // RIGHT VOLUME\r
+static void SetVolumeR(unsigned char ch,short vol)     // RIGHT VOLUME\r
 {\r
  if(vol&0x8000)                                        // comments... see above :)\r
   {\r
@@ -556,24 +499,23 @@ void SetVolumeR(unsigned char ch,short vol)            // RIGHT VOLUME
 // PITCH register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void SetPitch(int ch,unsigned short val)               // SET PITCH\r
+static void SetPitch(int ch,unsigned short val)               // SET PITCH\r
 {\r
  int NP;\r
  if(val>0x3fff) NP=0x3fff;                             // get pitch val\r
  else           NP=val;\r
 \r
  s_chan[ch].iRawPitch=NP;\r
-\r
- NP=(44100L*NP)/4096L;                                 // calc frequency\r
- if(NP<1) NP=1;                                        // some security\r
- s_chan[ch].iActFreq=NP;                               // store frequency\r
+ s_chan[ch].sinc=(NP<<4)|8;\r
+ s_chan[ch].sinc_inv=0;\r
+ if(iUseInterpolation==1) s_chan[ch].SB[32]=1;         // -> freq change in simple interpolation mode: set flag\r
 }\r
 \r
 ////////////////////////////////////////////////////////////////////////\r
 // REVERB register write\r
 ////////////////////////////////////////////////////////////////////////\r
 \r
-void ReverbOn(int start,int end,unsigned short val)    // REVERB ON PSX COMMAND\r
+static void ReverbOn(int start,int end,unsigned short val)\r
 {\r
  int ch;\r
 \r