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