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