b091efcc96fd0b9d8e6937520d9de861b966a84f
[pcsx_rearmed.git] / plugins / dfsound / spu.c
1 /***************************************************************************
2                             spu.c  -  description
3                              -------------------
4     begin                : Wed May 15 2002
5     copyright            : (C) 2002 by Pete Bernert
6     email                : BlackDove@addcom.de
7
8  Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2012,2014,2015
9
10  ***************************************************************************/
11 /***************************************************************************
12  *                                                                         *
13  *   This program is free software; you can redistribute it and/or modify  *
14  *   it under the terms of the GNU General Public License as published by  *
15  *   the Free Software Foundation; either version 2 of the License, or     *
16  *   (at your option) any later version. See also the license.txt file for *
17  *   additional informations.                                              *
18  *                                                                         *
19  ***************************************************************************/
20
21 #ifndef _WIN32
22 #include <sys/time.h> // gettimeofday in xa.c
23 #define THREAD_ENABLED 1
24 #endif
25 #include "stdafx.h"
26
27 #define _IN_SPU
28
29 #include "externals.h"
30 #include "registers.h"
31 #include "out.h"
32 #include "arm_features.h"
33 #include "spu_config.h"
34
35 #ifdef __ARM_ARCH_7A__
36  #define ssat32_to_16(v) \
37   asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
38 #else
39  #define ssat32_to_16(v) do { \
40   if (v < -32768) v = -32768; \
41   else if (v > 32767) v = 32767; \
42  } while (0)
43 #endif
44
45 #define PSXCLK  33868800        /* 33.8688 MHz */
46
47 // intended to be ~1 frame
48 #define IRQ_NEAR_BLOCKS 32
49
50 /*
51 #if defined (USEMACOSX)
52 static char * libraryName     = N_("Mac OS X Sound");
53 #elif defined (USEALSA)
54 static char * libraryName     = N_("ALSA Sound");
55 #elif defined (USEOSS)
56 static char * libraryName     = N_("OSS Sound");
57 #elif defined (USESDL)
58 static char * libraryName     = N_("SDL Sound");
59 #elif defined (USEPULSEAUDIO)
60 static char * libraryName     = N_("PulseAudio Sound");
61 #else
62 static char * libraryName     = N_("NULL Sound");
63 #endif
64
65 static char * libraryInfo     = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
66 */
67
68 // globals
69
70 SPUInfo         spu;
71 SPUConfig       spu_config;
72
73 // MAIN infos struct for each channel
74
75 SPUCHAN         s_chan[MAXCHAN+1];                     // channel + 1 infos (1 is security for fmod handling)
76 REVERBInfo      rvb;
77
78 #ifdef THREAD_ENABLED
79
80 #include <pthread.h>
81 #include <semaphore.h>
82 #include <unistd.h>
83
84 // worker thread state
85 static struct {
86  unsigned int pending:1;
87  unsigned int exit_thread:1;
88  int ns_to;
89  int ctrl;
90  int decode_pos;
91  int silentch;
92  int *sRVBStart;
93  unsigned char *ram;
94  unsigned int chmask;
95  unsigned int r_chan_end;
96  unsigned int r_decode_dirty;
97  pthread_t thread;
98  sem_t sem_avail;
99  sem_t sem_done;
100  struct {
101   int spos;
102   int sbpos;
103   int sinc;
104   int start;
105   int loop;
106   int ns_to;
107   ADSRInfoEx adsr;
108   // might want to add vol and fmod flags..
109  } ch[24];
110 } *worker;
111
112 #else
113 static const void * const worker = NULL;
114 #endif
115
116 // certain globals (were local before, but with the new timeproc I need em global)
117
118 static const int f[8][2] = {   {    0,  0  },
119                         {   60,  0  },
120                         {  115, -52 },
121                         {   98, -55 },
122                         {  122, -60 } };
123 int ChanBuf[NSSIZE];
124 int SSumLR[NSSIZE*2];
125 int iFMod[NSSIZE];
126
127 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
128
129 ////////////////////////////////////////////////////////////////////////
130 // CODE AREA
131 ////////////////////////////////////////////////////////////////////////
132
133 // dirty inline func includes
134
135 #include "reverb.c"
136 #include "adsr.c"
137
138 ////////////////////////////////////////////////////////////////////////
139 // helpers for simple interpolation
140
141 //
142 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
143 //
144 // instead of having n equal sample values in a row like:
145 //       ____
146 //           |____
147 //
148 // we compare the current delta change with the next delta change.
149 //
150 // if curr_delta is positive,
151 //
152 //  - and next delta is smaller (or changing direction):
153 //         \.
154 //          -__
155 //
156 //  - and next delta significant (at least twice) bigger:
157 //         --_
158 //            \.
159 //
160 //  - and next delta is nearly same:
161 //          \.
162 //           \.
163 //
164 //
165 // if curr_delta is negative,
166 //
167 //  - and next delta is smaller (or changing direction):
168 //          _--
169 //         /
170 //
171 //  - and next delta significant (at least twice) bigger:
172 //            /
173 //         __- 
174 //
175 //  - and next delta is nearly same:
176 //           /
177 //          /
178 //
179
180 static void InterpolateUp(int *SB, int sinc)
181 {
182  if(SB[32]==1)                                         // flag == 1? calc step and set flag... and don't change the value in this pass
183   {
184    const int id1=SB[30]-SB[29];                        // curr delta to next val
185    const int id2=SB[31]-SB[30];                        // and next delta to next-next val :)
186
187    SB[32]=0;
188
189    if(id1>0)                                           // curr delta positive
190     {
191      if(id2<id1)
192       {SB[28]=id1;SB[32]=2;}
193      else
194      if(id2<(id1<<1))
195       SB[28]=(id1*sinc)>>16;
196      else
197       SB[28]=(id1*sinc)>>17;
198     }
199    else                                                // curr delta negative
200     {
201      if(id2>id1)
202       {SB[28]=id1;SB[32]=2;}
203      else
204      if(id2>(id1<<1))
205       SB[28]=(id1*sinc)>>16;
206      else
207       SB[28]=(id1*sinc)>>17;
208     }
209   }
210  else
211  if(SB[32]==2)                                         // flag 1: calc step and set flag... and don't change the value in this pass
212   {
213    SB[32]=0;
214
215    SB[28]=(SB[28]*sinc)>>17;
216    //if(sinc<=0x8000)
217    //     SB[29]=SB[30]-(SB[28]*((0x10000/sinc)-1));
218    //else
219    SB[29]+=SB[28];
220   }
221  else                                                  // no flags? add bigger val (if possible), calc smaller step, set flag1
222   SB[29]+=SB[28];
223 }
224
225 //
226 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
227 //
228
229 static void InterpolateDown(int *SB, int sinc)
230 {
231  if(sinc>=0x20000L)                                 // we would skip at least one val?
232   {
233    SB[29]+=(SB[30]-SB[29])/2;                                  // add easy weight
234    if(sinc>=0x30000L)                               // we would skip even more vals?
235     SB[29]+=(SB[31]-SB[30])/2;                                 // add additional next weight
236   }
237 }
238
239 ////////////////////////////////////////////////////////////////////////
240 // helpers for gauss interpolation
241
242 #define gval0 (((short*)(&SB[29]))[gpos&3])
243 #define gval(x) ((int)((short*)(&SB[29]))[(gpos+x)&3])
244
245 #include "gauss_i.h"
246
247 ////////////////////////////////////////////////////////////////////////
248
249 #include "xa.c"
250
251 static void do_irq(void)
252 {
253  //if(!(spu.spuStat & STAT_IRQ))
254  {
255   spu.spuStat |= STAT_IRQ;                             // asserted status?
256   if(spu.irqCallback) spu.irqCallback();
257  }
258 }
259
260 static int check_irq(int ch, unsigned char *pos)
261 {
262  if((spu.spuCtrl & CTRL_IRQ) && pos == spu.pSpuIrq)
263  {
264   //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC);
265   do_irq();
266   return 1;
267  }
268  return 0;
269 }
270
271 ////////////////////////////////////////////////////////////////////////
272 // START SOUND... called by main thread to setup a new sound on a channel
273 ////////////////////////////////////////////////////////////////////////
274
275 INLINE void StartSound(int ch)
276 {
277  StartADSR(ch);
278  StartREVERB(ch);
279
280  s_chan[ch].prevflags=2;
281
282  s_chan[ch].SB[26]=0;                                  // init mixing vars
283  s_chan[ch].SB[27]=0;
284  s_chan[ch].iSBPos=27;
285
286  s_chan[ch].SB[28]=0;
287  s_chan[ch].SB[29]=0;                                  // init our interpolation helpers
288  s_chan[ch].SB[30]=0;
289  s_chan[ch].SB[31]=0;
290  s_chan[ch].spos=0;
291
292  spu.dwNewChannel&=~(1<<ch);                           // clear new channel bit
293  spu.dwChannelOn|=1<<ch;
294  spu.dwChannelDead&=~(1<<ch);
295 }
296
297 ////////////////////////////////////////////////////////////////////////
298 // ALL KIND OF HELPERS
299 ////////////////////////////////////////////////////////////////////////
300
301 INLINE int FModChangeFrequency(int *SB, int pitch, int ns)
302 {
303  unsigned int NP=pitch;
304  int sinc;
305
306  NP=((32768L+iFMod[ns])*NP)>>15;
307
308  if(NP>0x3fff) NP=0x3fff;
309  if(NP<0x1)    NP=0x1;
310
311  sinc=NP<<4;                                           // calc frequency
312  if(spu_config.iUseInterpolation==1)                   // freq change in simple interpolation mode
313   SB[32]=1;
314  iFMod[ns]=0;
315
316  return sinc;
317 }                    
318
319 ////////////////////////////////////////////////////////////////////////
320
321 INLINE void StoreInterpolationVal(int *SB, int sinc, int fa, int fmod_freq)
322 {
323  if(fmod_freq)                                         // fmod freq channel
324   SB[29]=fa;
325  else
326   {
327    ssat32_to_16(fa);
328
329    if(spu_config.iUseInterpolation>=2)                 // gauss/cubic interpolation
330     {
331      int gpos = SB[28];
332      gval0 = fa;
333      gpos = (gpos+1) & 3;
334      SB[28] = gpos;
335     }
336    else
337    if(spu_config.iUseInterpolation==1)                 // simple interpolation
338     {
339      SB[28] = 0;
340      SB[29] = SB[30];                                  // -> helpers for simple linear interpolation: delay real val for two slots, and calc the two deltas, for a 'look at the future behaviour'
341      SB[30] = SB[31];
342      SB[31] = fa;
343      SB[32] = 1;                                       // -> flag: calc new interolation
344     }
345    else SB[29]=fa;                                     // no interpolation
346   }
347 }
348
349 ////////////////////////////////////////////////////////////////////////
350
351 INLINE int iGetInterpolationVal(int *SB, int sinc, int spos, int fmod_freq)
352 {
353  int fa;
354
355  if(fmod_freq) return SB[29];
356
357  switch(spu_config.iUseInterpolation)
358   {
359    //--------------------------------------------------//
360    case 3:                                             // cubic interpolation
361     {
362      long xd;int gpos;
363      xd = (spos >> 1)+1;
364      gpos = SB[28];
365
366      fa  = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
367      fa *= (xd - (2<<15)) / 6;
368      fa >>= 15;
369      fa += gval(2) - gval(1) - gval(1) + gval0;
370      fa *= (xd - (1<<15)) >> 1;
371      fa >>= 15;
372      fa += gval(1) - gval0;
373      fa *= xd;
374      fa >>= 15;
375      fa = fa + gval0;
376
377     } break;
378    //--------------------------------------------------//
379    case 2:                                             // gauss interpolation
380     {
381      int vl, vr;int gpos;
382      vl = (spos >> 6) & ~3;
383      gpos = SB[28];
384      vr=(gauss[vl]*(int)gval0)&~2047;
385      vr+=(gauss[vl+1]*gval(1))&~2047;
386      vr+=(gauss[vl+2]*gval(2))&~2047;
387      vr+=(gauss[vl+3]*gval(3))&~2047;
388      fa = vr>>11;
389     } break;
390    //--------------------------------------------------//
391    case 1:                                             // simple interpolation
392     {
393      if(sinc<0x10000L)                                 // -> upsampling?
394           InterpolateUp(SB, sinc);                     // --> interpolate up
395      else InterpolateDown(SB, sinc);                   // --> else down
396      fa=SB[29];
397     } break;
398    //--------------------------------------------------//
399    default:                                            // no interpolation
400     {
401      fa=SB[29];
402     } break;
403    //--------------------------------------------------//
404   }
405
406  return fa;
407 }
408
409 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
410 {
411  int nSample;
412  int fa, s_1, s_2, d, s;
413
414  s_1 = dest[27];
415  s_2 = dest[26];
416
417  for (nSample = 0; nSample < 28; src++)
418  {
419   d = (int)*src;
420   s = (int)(signed short)((d & 0x0f) << 12);
421
422   fa = s >> shift_factor;
423   fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
424   s_2=s_1;s_1=fa;
425
426   dest[nSample++] = fa;
427
428   s = (int)(signed short)((d & 0xf0) << 8);
429   fa = s >> shift_factor;
430   fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
431   s_2=s_1;s_1=fa;
432
433   dest[nSample++] = fa;
434  }
435 }
436
437 static int decode_block(int ch, int *SB)
438 {
439  unsigned char *start;
440  int predict_nr, shift_factor, flags;
441  int ret = 0;
442
443  start = s_chan[ch].pCurr;                 // set up the current pos
444  if (start == spu.spuMemC)                 // ?
445   ret = 1;
446
447  if (s_chan[ch].prevflags & 1)             // 1: stop/loop
448  {
449   if (!(s_chan[ch].prevflags & 2))
450    ret = 1;
451
452   start = s_chan[ch].pLoop;
453  }
454  else
455   check_irq(ch, start);                    // hack, see check_irq below..
456
457  predict_nr = (int)start[0];
458  shift_factor = predict_nr & 0xf;
459  predict_nr >>= 4;
460
461  decode_block_data(SB, start + 2, predict_nr, shift_factor);
462
463  flags = start[1];
464  if (flags & 4)
465   s_chan[ch].pLoop = start;                // loop adress
466
467  start += 16;
468
469  if (flags & 1) {                          // 1: stop/loop
470   start = s_chan[ch].pLoop;
471   check_irq(ch, start);                    // hack.. :(
472  }
473
474  if (start - spu.spuMemC >= 0x80000)
475   start = spu.spuMemC;
476
477  s_chan[ch].pCurr = start;                 // store values for next cycle
478  s_chan[ch].prevflags = flags;
479
480  return ret;
481 }
482
483 // do block, but ignore sample data
484 static int skip_block(int ch)
485 {
486  unsigned char *start = s_chan[ch].pCurr;
487  int flags;
488  int ret = 0;
489
490  if (s_chan[ch].prevflags & 1) {
491   if (!(s_chan[ch].prevflags & 2))
492    ret = 1;
493
494   start = s_chan[ch].pLoop;
495  }
496  else
497   check_irq(ch, start);
498
499  flags = start[1];
500  if (flags & 4)
501   s_chan[ch].pLoop = start;
502
503  start += 16;
504
505  if (flags & 1) {
506   start = s_chan[ch].pLoop;
507   check_irq(ch, start);
508  }
509
510  s_chan[ch].pCurr = start;
511  s_chan[ch].prevflags = flags;
512
513  return ret;
514 }
515
516 #ifdef THREAD_ENABLED
517
518 static int decode_block_work(int ch, int *SB)
519 {
520  int predict_nr, shift_factor, flags;
521  const unsigned char *ram = worker->ram;
522  int start = worker->ch[ch].start;
523  int loop = worker->ch[ch].loop;
524
525  predict_nr = ram[start];
526  shift_factor = predict_nr & 0xf;
527  predict_nr >>= 4;
528
529  decode_block_data(SB, ram + start + 2, predict_nr, shift_factor);
530
531  flags = ram[start + 1];
532  if (flags & 4)
533   loop = start;                            // loop adress
534
535  start += 16;
536
537  if (flags & 1)                            // 1: stop/loop
538   start = loop;
539
540  worker->ch[ch].start = start & 0x7ffff;
541  worker->ch[ch].loop = loop;
542
543  return 0;
544 }
545
546 #endif
547
548 // if irq is going to trigger sooner than in upd_samples, set upd_samples
549 static void scan_for_irq(int ch, unsigned int *upd_samples)
550 {
551  int pos, sinc, sinc_inv, end;
552  unsigned char *block;
553  int flags;
554
555  block = s_chan[ch].pCurr;
556  pos = s_chan[ch].spos;
557  sinc = s_chan[ch].sinc;
558  end = pos + *upd_samples * sinc;
559
560  pos += (28 - s_chan[ch].iSBPos) << 16;
561  while (pos < end)
562  {
563   if (block == spu.pSpuIrq)
564    break;
565   flags = block[1];
566   block += 16;
567   if (flags & 1) {                          // 1: stop/loop
568    block = s_chan[ch].pLoop;
569    if (block == spu.pSpuIrq)                // hack.. (see decode_block)
570     break;
571   }
572   pos += 28 << 16;
573  }
574
575  if (pos < end)
576  {
577   sinc_inv = s_chan[ch].sinc_inv;
578   if (sinc_inv == 0)
579    sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
580
581   pos -= s_chan[ch].spos;
582   *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
583   //xprintf("ch%02d: irq sched: %3d %03d\n",
584   // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
585  }
586 }
587
588 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
589 static noinline int do_samples_##name(int (*decode_f)(int ch, int *SB), int ch, \
590  int ns_to, int *SB, int sinc, int *spos, int *sbpos) \
591 {                                            \
592  int ns, d, fa;                              \
593  int ret = ns_to;                            \
594  interp_start;                               \
595                                              \
596  for (ns = 0; ns < ns_to; ns++)              \
597  {                                           \
598   fmod_code;                                 \
599                                              \
600   *spos += sinc;                             \
601   while (*spos >= 0x10000)                   \
602   {                                          \
603    fa = SB[(*sbpos)++];                      \
604    if (*sbpos >= 28)                         \
605    {                                         \
606     *sbpos = 0;                              \
607     d = decode_f(ch, SB);                    \
608     if (d && ns < ret)                       \
609      ret = ns;                               \
610    }                                         \
611                                              \
612    interp1_code;                             \
613    *spos -= 0x10000;                         \
614   }                                          \
615                                              \
616   interp2_code;                              \
617  }                                           \
618                                              \
619  interp_end;                                 \
620                                              \
621  return ret;                                 \
622 }
623
624 #define fmod_recv_check \
625   if(s_chan[ch].bFMod==1 && iFMod[ns]) \
626     sinc = FModChangeFrequency(SB, s_chan[ch].iRawPitch, ns)
627
628 make_do_samples(default, fmod_recv_check, ,
629   StoreInterpolationVal(SB, sinc, fa, s_chan[ch].bFMod==2),
630   ChanBuf[ns] = iGetInterpolationVal(SB, sinc, *spos, s_chan[ch].bFMod==2), )
631 make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa)
632
633 #define simple_interp_store \
634   SB[28] = 0; \
635   SB[29] = SB[30]; \
636   SB[30] = SB[31]; \
637   SB[31] = fa; \
638   SB[32] = 1
639
640 #define simple_interp_get \
641   if(sinc<0x10000)                /* -> upsampling? */ \
642        InterpolateUp(SB, sinc);   /* --> interpolate up */ \
643   else InterpolateDown(SB, sinc); /* --> else down */ \
644   ChanBuf[ns] = SB[29]
645
646 make_do_samples(simple, , ,
647   simple_interp_store, simple_interp_get, )
648
649 static int do_samples_skip(int ch, int ns_to)
650 {
651  int ret = ns_to, ns, d;
652
653  s_chan[ch].spos += s_chan[ch].iSBPos << 16;
654
655  for (ns = 0; ns < ns_to; ns++)
656  {
657   s_chan[ch].spos += s_chan[ch].sinc;
658   while (s_chan[ch].spos >= 28*0x10000)
659   {
660    d = skip_block(ch);
661    if (d && ns < ret)
662     ret = ns;
663    s_chan[ch].spos -= 28*0x10000;
664   }
665  }
666
667  s_chan[ch].iSBPos = s_chan[ch].spos >> 16;
668  s_chan[ch].spos &= 0xffff;
669
670  return ret;
671 }
672
673 static void do_lsfr_samples(int ns_to, int ctrl,
674  unsigned int *dwNoiseCount, unsigned int *dwNoiseVal)
675 {
676  unsigned int counter = *dwNoiseCount;
677  unsigned int val = *dwNoiseVal;
678  unsigned int level, shift, bit;
679  int ns;
680
681  // modified from DrHell/shalma, no fraction
682  level = (ctrl >> 10) & 0x0f;
683  level = 0x8000 >> level;
684
685  for (ns = 0; ns < ns_to; ns++)
686  {
687   counter += 2;
688   if (counter >= level)
689   {
690    counter -= level;
691    shift = (val >> 10) & 0x1f;
692    bit = (0x69696969 >> shift) & 1;
693    bit ^= (val >> 15) & 1;
694    val = (val << 1) | bit;
695   }
696
697   ChanBuf[ns] = (signed short)val;
698  }
699
700  *dwNoiseCount = counter;
701  *dwNoiseVal = val;
702 }
703
704 static int do_samples_noise(int ch, int ns_to)
705 {
706  int ret;
707
708  ret = do_samples_skip(ch, ns_to);
709
710  do_lsfr_samples(ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
711
712  return ret;
713 }
714
715 #ifdef HAVE_ARMV5
716 // asm code; lv and rv must be 0-3fff
717 extern void mix_chan(int start, int count, int lv, int rv);
718 extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb);
719 #else
720 static void mix_chan(int start, int count, int lv, int rv)
721 {
722  int *dst = SSumLR + start * 2;
723  const int *src = ChanBuf + start;
724  int l, r;
725
726  while (count--)
727   {
728    int sval = *src++;
729
730    l = (sval * lv) >> 14;
731    r = (sval * rv) >> 14;
732    *dst++ += l;
733    *dst++ += r;
734   }
735 }
736
737 static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb)
738 {
739  int *dst = SSumLR + start * 2;
740  int *drvb = rvb + start * 2;
741  const int *src = ChanBuf + start;
742  int l, r;
743
744  while (count--)
745   {
746    int sval = *src++;
747
748    l = (sval * lv) >> 14;
749    r = (sval * rv) >> 14;
750    *dst++ += l;
751    *dst++ += r;
752    *drvb++ += l;
753    *drvb++ += r;
754   }
755 }
756 #endif
757
758 // 0x0800-0x0bff  Voice 1
759 // 0x0c00-0x0fff  Voice 3
760 static noinline void do_decode_bufs(unsigned short *mem, int which,
761  int count, int decode_pos)
762 {
763  unsigned short *dst = &mem[0x800/2 + which*0x400/2];
764  const int *src = ChanBuf;
765  int cursor = decode_pos;
766
767  while (count-- > 0)
768   {
769    cursor &= 0x1ff;
770    dst[cursor] = *src++;
771    cursor++;
772   }
773
774  // decode_pos is updated and irqs are checked later, after voice loop
775 }
776
777 static void do_silent_chans(int ns_to, int silentch)
778 {
779  int ch;
780
781  for (ch = 0; ch < MAXCHAN; ch++)
782   {
783    if (!(silentch & (1<<ch))) continue;            // already handled
784    if (spu.dwChannelDead & (1<<ch)) continue;
785    if (s_chan[ch].pCurr > spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq)
786     continue;
787
788    s_chan[ch].spos += s_chan[ch].iSBPos << 16;
789    s_chan[ch].iSBPos = 0;
790
791    s_chan[ch].spos += s_chan[ch].sinc * ns_to;
792    while (s_chan[ch].spos >= 28 * 0x10000)
793     {
794      unsigned char *start = s_chan[ch].pCurr;
795
796      skip_block(ch);
797      if (start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000)
798       {
799        // looping on self or stopped(?)
800        spu.dwChannelDead |= 1<<ch;
801        s_chan[ch].spos = 0;
802        break;
803       }
804
805      s_chan[ch].spos -= 28 * 0x10000;
806     }
807   }
808 }
809
810 static void do_channels(int ns_to)
811 {
812  unsigned int mask;
813  int *SB, sinc;
814  int ch, d;
815
816  InitREVERB(ns_to);
817
818  mask = spu.dwChannelOn & 0xffffff;
819  for (ch = 0; mask != 0; ch++, mask >>= 1)         // loop em all...
820   {
821    if (!(mask & 1)) continue;                      // channel not playing? next
822
823    SB = s_chan[ch].SB;
824    sinc = s_chan[ch].sinc;
825
826    if (s_chan[ch].bNoise)
827     d = do_samples_noise(ch, ns_to);
828    else if (s_chan[ch].bFMod == 2
829          || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0))
830     d = do_samples_noint(decode_block, ch, ns_to,
831           SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos);
832    else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1)
833     d = do_samples_simple(decode_block, ch, ns_to,
834           SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos);
835    else
836     d = do_samples_default(decode_block, ch, ns_to,
837           SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos);
838
839    d = MixADSR(&s_chan[ch].ADSRX, d);
840    if (d < ns_to) {
841     spu.dwChannelOn &= ~(1 << ch);
842     s_chan[ch].ADSRX.EnvelopeVol = 0;
843     memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
844    }
845
846    if (ch == 1 || ch == 3)
847     {
848      do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos);
849      spu.decode_dirty_ch |= 1 << ch;
850     }
851
852    if (s_chan[ch].bFMod == 2)                      // fmod freq channel
853     memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
854    if (s_chan[ch].bRVBActive)
855     mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, spu.sRVBStart);
856    else
857     mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
858   }
859 }
860
861 static void do_samples_finish(int ns_to, int silentch, int decode_pos);
862
863 // optional worker thread handling
864
865 #ifdef THREAD_ENABLED
866
867 static void queue_channel_work(int ns_to, int silentch)
868 {
869  unsigned int mask;
870  int ch;
871
872  worker->ns_to = ns_to;
873  worker->ctrl = spu.spuCtrl;
874  worker->decode_pos = spu.decode_pos;
875  worker->silentch = silentch;
876  worker->sRVBStart = spu.sRVBStart;
877  worker->ram = spu.spuMemC;
878
879  mask = worker->chmask = spu.dwChannelOn & 0xffffff;
880  for (ch = 0; mask != 0; ch++, mask >>= 1)
881   {
882    if (!(mask & 1)) continue;
883
884    worker->ch[ch].spos = s_chan[ch].spos;
885    worker->ch[ch].sbpos = s_chan[ch].iSBPos;
886    worker->ch[ch].sinc = s_chan[ch].sinc;
887    worker->ch[ch].adsr = s_chan[ch].ADSRX;
888    worker->ch[ch].start = s_chan[ch].pCurr - spu.spuMemC;
889    worker->ch[ch].loop = s_chan[ch].pLoop - spu.spuMemC;
890    if (s_chan[ch].prevflags & 1)
891     worker->ch[ch].start = worker->ch[ch].loop;
892
893    worker->ch[ch].ns_to = do_samples_skip(ch, ns_to);
894   }
895
896  worker->pending = 1;
897  sem_post(&worker->sem_avail);
898 }
899
900 static void do_channel_work(void)
901 {
902  unsigned int mask, endmask = 0;
903  unsigned int decode_dirty_ch = 0;
904  int *SB, sinc, spos, sbpos;
905  int d, ch, ns_to;
906
907  ns_to = worker->ns_to;
908  memset(worker->sRVBStart, 0, ns_to * sizeof(worker->sRVBStart[0]) * 2);
909
910  mask = worker->chmask;
911  for (ch = 0; mask != 0; ch++, mask >>= 1)
912   {
913    if (!(mask & 1)) continue;
914
915    d = worker->ch[ch].ns_to;
916    spos = worker->ch[ch].spos;
917    sbpos = worker->ch[ch].sbpos;
918    sinc = worker->ch[ch].sinc;
919    SB = s_chan[ch].SB;
920
921    if (s_chan[ch].bNoise)
922     do_lsfr_samples(d, worker->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
923    else if (s_chan[ch].bFMod == 2
924          || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0))
925     do_samples_noint(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
926    else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1)
927     do_samples_simple(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
928    else
929     do_samples_default(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
930
931    d = MixADSR(&worker->ch[ch].adsr, d);
932    if (d < ns_to) {
933     endmask |= 1 << ch;
934     worker->ch[ch].adsr.EnvelopeVol = 0;
935     memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
936    }
937
938    if (ch == 1 || ch == 3)
939     {
940      do_decode_bufs((void *)worker->ram, ch/2, ns_to, worker->decode_pos);
941      decode_dirty_ch |= 1 << ch;
942     }
943
944    if (s_chan[ch].bFMod == 2)                      // fmod freq channel
945     memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
946    if (s_chan[ch].bRVBActive)
947     mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, worker->sRVBStart);
948    else
949     mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
950   }
951
952   worker->r_chan_end = endmask;
953   worker->r_decode_dirty = decode_dirty_ch;
954 }
955
956 static void sync_worker_thread(void)
957 {
958  unsigned int mask;
959  int ch;
960
961  if (!worker->pending)
962   return;
963
964  sem_wait(&worker->sem_done);
965  worker->pending = 0;
966
967  mask = worker->chmask;
968  for (ch = 0; mask != 0; ch++, mask >>= 1) {
969   if (!(mask & 1)) continue;
970
971   // be sure there was no keyoff while thread was working
972   if (s_chan[ch].ADSRX.State != ADSR_RELEASE)
973     s_chan[ch].ADSRX.State = worker->ch[ch].adsr.State;
974   s_chan[ch].ADSRX.EnvelopeVol = worker->ch[ch].adsr.EnvelopeVol;
975  }
976
977  spu.dwChannelOn &= ~worker->r_chan_end;
978  spu.decode_dirty_ch |= worker->r_decode_dirty;
979
980  do_samples_finish(worker->ns_to, worker->silentch,
981   worker->decode_pos);
982 }
983
984 #else
985
986 static void queue_channel_work(int ns_to, int silentch) {}
987 static void sync_worker_thread(void) {}
988
989 #endif // THREAD_ENABLED
990
991 ////////////////////////////////////////////////////////////////////////
992 // MAIN SPU FUNCTION
993 // here is the main job handler...
994 ////////////////////////////////////////////////////////////////////////
995
996 void do_samples(unsigned int cycles_to, int do_sync)
997 {
998  unsigned int mask;
999  int ch, ns_to;
1000  int silentch;
1001  int cycle_diff;
1002
1003  cycle_diff = cycles_to - spu.cycles_played;
1004  if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
1005   {
1006    //xprintf("desync %u %d\n", cycles_to, cycle_diff);
1007    spu.cycles_played = cycles_to;
1008    return;
1009   }
1010
1011  if (cycle_diff < 2 * 768)
1012   return;
1013
1014  ns_to = (cycle_diff / 768 + 1) & ~1;
1015  if (ns_to > NSSIZE) {
1016   // should never happen
1017   //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
1018   ns_to = NSSIZE;
1019  }
1020
1021   //////////////////////////////////////////////////////
1022   // special irq handling in the decode buffers (0x0000-0x1000)
1023   // we know:
1024   // the decode buffers are located in spu memory in the following way:
1025   // 0x0000-0x03ff  CD audio left
1026   // 0x0400-0x07ff  CD audio right
1027   // 0x0800-0x0bff  Voice 1
1028   // 0x0c00-0x0fff  Voice 3
1029   // and decoded data is 16 bit for one sample
1030   // we assume:
1031   // even if voices 1/3 are off or no cd audio is playing, the internal
1032   // play positions will move on and wrap after 0x400 bytes.
1033   // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
1034   // increase this pointer on each sample by 2 bytes. If this pointer
1035   // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
1036   // an IRQ.
1037
1038   if (unlikely((spu.spuCtrl & CTRL_IRQ)
1039        && spu.pSpuIrq < spu.spuMemC+0x1000))
1040    {
1041     int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1042     int left = (irq_pos - spu.decode_pos) & 0x1ff;
1043     if (0 < left && left <= ns_to)
1044      {
1045       //xprintf("decoder irq %x\n", spu.decode_pos);
1046       do_irq();
1047      }
1048    }
1049
1050   if (worker != NULL)
1051    sync_worker_thread();
1052
1053   mask = spu.dwNewChannel & 0xffffff;
1054   for (ch = 0; mask != 0; ch++, mask >>= 1) {
1055    if (mask & 1)
1056     StartSound(ch);
1057   }
1058
1059   silentch = ~spu.dwChannelOn & 0xffffff;
1060
1061   if (spu.dwChannelOn == 0) {
1062    InitREVERB(ns_to);
1063    do_samples_finish(ns_to, silentch, spu.decode_pos);
1064   }
1065   else {
1066    if (do_sync || worker == NULL || !spu_config.iUseThread) {
1067     do_channels(ns_to);
1068     do_samples_finish(ns_to, silentch, spu.decode_pos);
1069    }
1070    else {
1071     queue_channel_work(ns_to, silentch);
1072    }
1073   }
1074
1075   // advance "stopped" channels that can cause irqs
1076   // (all chans are always playing on the real thing..)
1077   if (spu.spuCtrl & CTRL_IRQ)
1078    do_silent_chans(ns_to, silentch);
1079
1080   spu.cycles_played += ns_to * 768;
1081   spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
1082 }
1083
1084 static void do_samples_finish(int ns_to, int silentch, int decode_pos)
1085 {
1086   int volmult = spu_config.iVolume;
1087   int ns;
1088   int d;
1089
1090   if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
1091    {
1092     memset(&spu.spuMem[0x800/2], 0, 0x400);
1093     spu.decode_dirty_ch &= ~(1<<1);
1094    }
1095   if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
1096    {
1097     memset(&spu.spuMem[0xc00/2], 0, 0x400);
1098     spu.decode_dirty_ch &= ~(1<<3);
1099    }
1100
1101   //---------------------------------------------------//
1102   // mix XA infos (if any)
1103
1104   MixXA(ns_to, decode_pos);
1105   
1106   ///////////////////////////////////////////////////////
1107   // mix all channels (including reverb) into one buffer
1108
1109   if(spu_config.iUseReverb)
1110    REVERBDo(ns_to);
1111
1112   if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
1113    {
1114     memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
1115     spu.pS += ns_to * 2;
1116    }
1117   else
1118   for (ns = 0; ns < ns_to * 2; )
1119    {
1120     d = SSumLR[ns]; SSumLR[ns] = 0;
1121     d = d * volmult >> 10;
1122     ssat32_to_16(d);
1123     *spu.pS++ = d;
1124     ns++;
1125
1126     d = SSumLR[ns]; SSumLR[ns] = 0;
1127     d = d * volmult >> 10;
1128     ssat32_to_16(d);
1129     *spu.pS++ = d;
1130     ns++;
1131    }
1132 }
1133
1134 void schedule_next_irq(void)
1135 {
1136  unsigned int upd_samples;
1137  int ch;
1138
1139  if (spu.scheduleCallback == NULL)
1140   return;
1141
1142  upd_samples = 44100 / 50;
1143
1144  for (ch = 0; ch < MAXCHAN; ch++)
1145  {
1146   if (spu.dwChannelDead & (1 << ch))
1147    continue;
1148   if ((unsigned long)(spu.pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
1149     && (unsigned long)(spu.pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
1150    continue;
1151
1152   scan_for_irq(ch, &upd_samples);
1153  }
1154
1155  if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
1156  {
1157   int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1158   int left = (irq_pos - spu.decode_pos) & 0x1ff;
1159   if (0 < left && left < upd_samples) {
1160    //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
1161    upd_samples = left;
1162   }
1163  }
1164
1165  if (upd_samples < 44100 / 50)
1166   spu.scheduleCallback(upd_samples * 768);
1167 }
1168
1169 // SPU ASYNC... even newer epsxe func
1170 //  1 time every 'cycle' cycles... harhar
1171
1172 // rearmed: called dynamically now
1173
1174 void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
1175 {
1176  do_samples(cycle, 0);
1177
1178  if (spu.spuCtrl & CTRL_IRQ)
1179   schedule_next_irq();
1180
1181  if (flags & 1) {
1182   out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
1183   spu.pS = (short *)spu.pSpuBuffer;
1184
1185   if (spu_config.iTempo) {
1186    if (!out_current->busy())
1187     // cause more samples to be generated
1188     // (and break some games because of bad sync)
1189     spu.cycles_played -= 44100 / 60 / 2 * 768;
1190   }
1191  }
1192 }
1193
1194 // SPU UPDATE... new epsxe func
1195 //  1 time every 32 hsync lines
1196 //  (312/32)x50 in pal
1197 //  (262/32)x60 in ntsc
1198
1199 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
1200 // leave that func in the linux port, until epsxe linux is using
1201 // the async function as well
1202
1203 void CALLBACK SPUupdate(void)
1204 {
1205 }
1206
1207 // XA AUDIO
1208
1209 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
1210 {
1211  if(!xap)       return;
1212  if(!xap->freq) return;                                // no xa freq ? bye
1213
1214  FeedXA(xap);                                          // call main XA feeder
1215 }
1216
1217 // CDDA AUDIO
1218 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
1219 {
1220  if (!pcm)      return -1;
1221  if (nbytes<=0) return -1;
1222
1223  return FeedCDDA((unsigned char *)pcm, nbytes);
1224 }
1225
1226 // to be called after state load
1227 void ClearWorkingState(void)
1228 {
1229  memset(SSumLR,0,sizeof(SSumLR));                      // init some mixing buffers
1230  memset(iFMod,0,sizeof(iFMod));     
1231  spu.pS=(short *)spu.pSpuBuffer;                       // setup soundbuffer pointer
1232 }
1233
1234 // SETUPSTREAMS: init most of the spu buffers
1235 void SetupStreams(void)
1236
1237  int i;
1238
1239  spu.pSpuBuffer = (unsigned char *)malloc(32768);      // alloc mixing buffer
1240  spu.sRVBStart = (int *)malloc(NSSIZE*2*4);            // alloc reverb buffer
1241  memset(spu.sRVBStart,0,NSSIZE*2*4);
1242
1243  spu.XAStart =                                         // alloc xa buffer
1244   (uint32_t *)malloc(44100 * sizeof(uint32_t));
1245  spu.XAEnd   = spu.XAStart + 44100;
1246  spu.XAPlay  = spu.XAStart;
1247  spu.XAFeed  = spu.XAStart;
1248
1249  spu.CDDAStart =                                       // alloc cdda buffer
1250   (uint32_t *)malloc(CDDA_BUFFER_SIZE);
1251  spu.CDDAEnd   = spu.CDDAStart + 16384;
1252  spu.CDDAPlay  = spu.CDDAStart;
1253  spu.CDDAFeed  = spu.CDDAStart;
1254
1255  for(i=0;i<MAXCHAN;i++)                                // loop sound channels
1256   {
1257    s_chan[i].ADSRX.SustainLevel = 0xf;                 // -> init sustain
1258    s_chan[i].ADSRX.SustainIncrease = 1;
1259    s_chan[i].pLoop=spu.spuMemC;
1260    s_chan[i].pCurr=spu.spuMemC;
1261   }
1262
1263  ClearWorkingState();
1264
1265  spu.bSpuInit=1;                                       // flag: we are inited
1266 }
1267
1268 // REMOVESTREAMS: free most buffer
1269 void RemoveStreams(void)
1270
1271  free(spu.pSpuBuffer);                                 // free mixing buffer
1272  spu.pSpuBuffer = NULL;
1273  free(spu.sRVBStart);                                  // free reverb buffer
1274  spu.sRVBStart = NULL;
1275  free(spu.XAStart);                                    // free XA buffer
1276  spu.XAStart = NULL;
1277  free(spu.CDDAStart);                                  // free CDDA buffer
1278  spu.CDDAStart = NULL;
1279 }
1280
1281 #ifdef THREAD_ENABLED
1282
1283 static void *spu_worker_thread(void *unused)
1284 {
1285  while (1) {
1286   sem_wait(&worker->sem_avail);
1287   if (worker->exit_thread)
1288    break;
1289
1290   do_channel_work();
1291
1292   sem_post(&worker->sem_done);
1293  }
1294
1295  return NULL;
1296 }
1297
1298 static void init_spu_thread(void)
1299 {
1300  int ret;
1301
1302  if (sysconf(_SC_NPROCESSORS_ONLN) <= 1)
1303   return;
1304
1305  worker = calloc(1, sizeof(*worker));
1306  if (worker == NULL)
1307   return;
1308  ret = sem_init(&worker->sem_avail, 0, 0);
1309  if (ret != 0)
1310   goto fail_sem_avail;
1311  ret = sem_init(&worker->sem_done, 0, 0);
1312  if (ret != 0)
1313   goto fail_sem_done;
1314
1315  ret = pthread_create(&worker->thread, NULL, spu_worker_thread, NULL);
1316  if (ret != 0)
1317   goto fail_thread;
1318
1319  return;
1320
1321 fail_thread:
1322  sem_destroy(&worker->sem_done);
1323 fail_sem_done:
1324  sem_destroy(&worker->sem_avail);
1325 fail_sem_avail:
1326  free(worker);
1327  worker = NULL;
1328 }
1329
1330 static void exit_spu_thread(void)
1331 {
1332  if (worker == NULL)
1333   return;
1334  worker->exit_thread = 1;
1335  sem_post(&worker->sem_avail);
1336  pthread_join(worker->thread, NULL);
1337  sem_destroy(&worker->sem_done);
1338  sem_destroy(&worker->sem_avail);
1339  free(worker);
1340  worker = NULL;
1341 }
1342
1343 #else // if !THREAD_ENABLED
1344
1345 static void init_spu_thread(void)
1346 {
1347 }
1348
1349 static void exit_spu_thread(void)
1350 {
1351 }
1352
1353 #endif
1354
1355 // SPUINIT: this func will be called first by the main emu
1356 long CALLBACK SPUinit(void)
1357 {
1358  spu.spuMemC = (unsigned char *)spu.spuMem;            // just small setup
1359  memset((void *)&rvb, 0, sizeof(REVERBInfo));
1360  InitADSR();
1361
1362  spu.spuAddr = 0xffffffff;
1363  spu.decode_pos = 0;
1364  memset((void *)s_chan, 0, sizeof(s_chan));
1365  spu.pSpuIrq = spu.spuMemC;
1366
1367  SetupStreams();                                       // prepare streaming
1368
1369  if (spu_config.iVolume == 0)
1370   spu_config.iVolume = 768; // 1024 is 1.0
1371
1372  init_spu_thread();
1373
1374  return 0;
1375 }
1376
1377 // SPUOPEN: called by main emu after init
1378 long CALLBACK SPUopen(void)
1379 {
1380  if (spu.bSPUIsOpen) return 0;                         // security for some stupid main emus
1381
1382  SetupSound();                                         // setup sound (before init!)
1383
1384  spu.bSPUIsOpen = 1;
1385
1386  return PSE_SPU_ERR_SUCCESS;
1387 }
1388
1389 // SPUCLOSE: called before shutdown
1390 long CALLBACK SPUclose(void)
1391 {
1392  if (!spu.bSPUIsOpen) return 0;                        // some security
1393
1394  spu.bSPUIsOpen = 0;                                   // no more open
1395
1396  out_current->finish();                                // no more sound handling
1397
1398  return 0;
1399 }
1400
1401 // SPUSHUTDOWN: called by main emu on final exit
1402 long CALLBACK SPUshutdown(void)
1403 {
1404  SPUclose();
1405  RemoveStreams();                                      // no more streaming
1406  spu.bSpuInit=0;
1407
1408  exit_spu_thread();
1409
1410  return 0;
1411 }
1412
1413 // SPUTEST: we don't test, we are always fine ;)
1414 long CALLBACK SPUtest(void)
1415 {
1416  return 0;
1417 }
1418
1419 // SPUCONFIGURE: call config dialog
1420 long CALLBACK SPUconfigure(void)
1421 {
1422 #ifdef _MACOSX
1423  DoConfiguration();
1424 #else
1425 // StartCfgTool("CFG");
1426 #endif
1427  return 0;
1428 }
1429
1430 // SPUABOUT: show about window
1431 void CALLBACK SPUabout(void)
1432 {
1433 #ifdef _MACOSX
1434  DoAbout();
1435 #else
1436 // StartCfgTool("ABOUT");
1437 #endif
1438 }
1439
1440 // SETUP CALLBACKS
1441 // this functions will be called once, 
1442 // passes a callback that should be called on SPU-IRQ/cdda volume change
1443 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1444 {
1445  spu.irqCallback = callback;
1446 }
1447
1448 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1449 {
1450  spu.cddavCallback = CDDAVcallback;
1451 }
1452
1453 void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1454 {
1455  spu.scheduleCallback = callback;
1456 }
1457
1458 // COMMON PLUGIN INFO FUNCS
1459 /*
1460 char * CALLBACK PSEgetLibName(void)
1461 {
1462  return _(libraryName);
1463 }
1464
1465 unsigned long CALLBACK PSEgetLibType(void)
1466 {
1467  return  PSE_LT_SPU;
1468 }
1469
1470 unsigned long CALLBACK PSEgetLibVersion(void)
1471 {
1472  return (1 << 16) | (6 << 8);
1473 }
1474
1475 char * SPUgetLibInfos(void)
1476 {
1477  return _(libraryInfo);
1478 }
1479 */
1480
1481 // debug
1482 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1483 {
1484  int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1485
1486  for(;ch<MAXCHAN;ch++)
1487  {
1488   if (!(spu.dwChannelOn & (1<<ch)))
1489    continue;
1490   if (s_chan[ch].bFMod == 2)
1491    fmod_chans |= 1 << ch;
1492   if (s_chan[ch].bNoise)
1493    noise_chans |= 1 << ch;
1494   if((spu.spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= spu.pSpuIrq && s_chan[ch].pLoop <= spu.pSpuIrq)
1495    irq_chans |= 1 << ch;
1496  }
1497
1498  *chans_out = spu.dwChannelOn;
1499  *run_chans = ~spu.dwChannelOn & ~spu.dwChannelDead & irq_chans;
1500  *fmod_chans_out = fmod_chans;
1501  *noise_chans_out = noise_chans;
1502 }
1503
1504 // vim:shiftwidth=1:expandtab