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