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