1 /***************************************************************************
4 begin : Wed May 15 2002
5 copyright : (C) 2002 by Pete Bernert
6 email : BlackDove@addcom.de
8 Portions (C) GraÅžvydas "notaz" Ignotas, 2010-2012,2014,2015
10 ***************************************************************************/
11 /***************************************************************************
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. *
19 ***************************************************************************/
22 #include <sys/time.h> // gettimeofday in xa.c
23 #define THREAD_ENABLED 1
29 #include "externals.h"
30 #include "registers.h"
32 #include "arm_features.h"
33 #include "spu_config.h"
35 #ifdef __ARM_ARCH_7A__
36 #define ssat32_to_16(v) \
37 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
39 #define ssat32_to_16(v) do { \
40 if (v < -32768) v = -32768; \
41 else if (v > 32767) v = 32767; \
45 #define PSXCLK 33868800 /* 33.8688 MHz */
47 // intended to be ~1 frame
48 #define IRQ_NEAR_BLOCKS 32
51 #if defined (USEMACOSX)
52 static char * libraryName = N_("Mac OS X Sound");
53 #elif defined (USEALSA)
54 static char * libraryName = N_("ALSA Sound");
55 #elif defined (USEOSS)
56 static char * libraryName = N_("OSS Sound");
57 #elif defined (USESDL)
58 static char * libraryName = N_("SDL Sound");
59 #elif defined (USEPULSEAUDIO)
60 static char * libraryName = N_("PulseAudio Sound");
62 static char * libraryName = N_("NULL Sound");
65 static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
73 // MAIN infos struct for each channel
79 // worker thread state
80 static struct spu_worker {
81 unsigned int pending:1;
82 unsigned int exit_thread:1;
88 unsigned int r_chan_end;
89 unsigned int r_decode_dirty;
98 // might want to add vol and fmod flags..
103 static const void * const worker = NULL;
106 // certain globals (were local before, but with the new timeproc I need em global)
108 static int iFMod[NSSIZE];
112 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
114 ////////////////////////////////////////////////////////////////////////
116 ////////////////////////////////////////////////////////////////////////
118 // dirty inline func includes
123 ////////////////////////////////////////////////////////////////////////
124 // helpers for simple interpolation
127 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
129 // instead of having n equal sample values in a row like:
133 // we compare the current delta change with the next delta change.
135 // if curr_delta is positive,
137 // - and next delta is smaller (or changing direction):
141 // - and next delta significant (at least twice) bigger:
145 // - and next delta is nearly same:
150 // if curr_delta is negative,
152 // - and next delta is smaller (or changing direction):
156 // - and next delta significant (at least twice) bigger:
160 // - and next delta is nearly same:
165 static void InterpolateUp(int *SB, int sinc)
167 if(SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
169 const int id1=SB[30]-SB[29]; // curr delta to next val
170 const int id2=SB[31]-SB[30]; // and next delta to next-next val :)
174 if(id1>0) // curr delta positive
177 {SB[28]=id1;SB[32]=2;}
180 SB[28]=(id1*sinc)>>16;
182 SB[28]=(id1*sinc)>>17;
184 else // curr delta negative
187 {SB[28]=id1;SB[32]=2;}
190 SB[28]=(id1*sinc)>>16;
192 SB[28]=(id1*sinc)>>17;
196 if(SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
200 SB[28]=(SB[28]*sinc)>>17;
202 // SB[29]=SB[30]-(SB[28]*((0x10000/sinc)-1));
206 else // no flags? add bigger val (if possible), calc smaller step, set flag1
211 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
214 static void InterpolateDown(int *SB, int sinc)
216 if(sinc>=0x20000L) // we would skip at least one val?
218 SB[29]+=(SB[30]-SB[29])/2; // add easy weight
219 if(sinc>=0x30000L) // we would skip even more vals?
220 SB[29]+=(SB[31]-SB[30])/2; // add additional next weight
224 ////////////////////////////////////////////////////////////////////////
225 // helpers for gauss interpolation
227 #define gval0 (((short*)(&SB[29]))[gpos&3])
228 #define gval(x) ((int)((short*)(&SB[29]))[(gpos+x)&3])
232 ////////////////////////////////////////////////////////////////////////
236 static void do_irq(void)
238 //if(!(spu.spuStat & STAT_IRQ))
240 spu.spuStat |= STAT_IRQ; // asserted status?
241 if(spu.irqCallback) spu.irqCallback();
245 static int check_irq(int ch, unsigned char *pos)
247 if((spu.spuCtrl & CTRL_IRQ) && pos == spu.pSpuIrq)
249 //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC);
256 ////////////////////////////////////////////////////////////////////////
257 // START SOUND... called by main thread to setup a new sound on a channel
258 ////////////////////////////////////////////////////////////////////////
260 INLINE void StartSound(int ch)
262 SPUCHAN *s_chan = &spu.s_chan[ch];
269 s_chan->SB[26]=0; // init mixing vars
274 s_chan->SB[29]=0; // init our interpolation helpers
279 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
280 spu.dwChannelOn|=1<<ch;
281 spu.dwChannelDead&=~(1<<ch);
284 ////////////////////////////////////////////////////////////////////////
285 // ALL KIND OF HELPERS
286 ////////////////////////////////////////////////////////////////////////
288 INLINE int FModChangeFrequency(int *SB, int pitch, int ns)
290 unsigned int NP=pitch;
293 NP=((32768L+iFMod[ns])*NP)>>15;
295 if(NP>0x3fff) NP=0x3fff;
298 sinc=NP<<4; // calc frequency
299 if(spu_config.iUseInterpolation==1) // freq change in simple interpolation mode
306 ////////////////////////////////////////////////////////////////////////
308 INLINE void StoreInterpolationVal(int *SB, int sinc, int fa, int fmod_freq)
310 if(fmod_freq) // fmod freq channel
316 if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation
324 if(spu_config.iUseInterpolation==1) // simple interpolation
327 SB[29] = SB[30]; // -> helpers for simple linear interpolation: delay real val for two slots, and calc the two deltas, for a 'look at the future behaviour'
330 SB[32] = 1; // -> flag: calc new interolation
332 else SB[29]=fa; // no interpolation
336 ////////////////////////////////////////////////////////////////////////
338 INLINE int iGetInterpolationVal(int *SB, int sinc, int spos, int fmod_freq)
342 if(fmod_freq) return SB[29];
344 switch(spu_config.iUseInterpolation)
346 //--------------------------------------------------//
347 case 3: // cubic interpolation
353 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
354 fa *= (xd - (2<<15)) / 6;
356 fa += gval(2) - gval(1) - gval(1) + gval0;
357 fa *= (xd - (1<<15)) >> 1;
359 fa += gval(1) - gval0;
365 //--------------------------------------------------//
366 case 2: // gauss interpolation
369 vl = (spos >> 6) & ~3;
371 vr=(gauss[vl]*(int)gval0)&~2047;
372 vr+=(gauss[vl+1]*gval(1))&~2047;
373 vr+=(gauss[vl+2]*gval(2))&~2047;
374 vr+=(gauss[vl+3]*gval(3))&~2047;
377 //--------------------------------------------------//
378 case 1: // simple interpolation
380 if(sinc<0x10000L) // -> upsampling?
381 InterpolateUp(SB, sinc); // --> interpolate up
382 else InterpolateDown(SB, sinc); // --> else down
385 //--------------------------------------------------//
386 default: // no interpolation
390 //--------------------------------------------------//
396 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
398 static const int f[16][2] = {
406 int fa, s_1, s_2, d, s;
411 for (nSample = 0; nSample < 28; src++)
414 s = (int)(signed short)((d & 0x0f) << 12);
416 fa = s >> shift_factor;
417 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
420 dest[nSample++] = fa;
422 s = (int)(signed short)((d & 0xf0) << 8);
423 fa = s >> shift_factor;
424 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
427 dest[nSample++] = fa;
431 static int decode_block(int ch, int *SB)
433 SPUCHAN *s_chan = &spu.s_chan[ch];
434 unsigned char *start;
435 int predict_nr, shift_factor, flags;
438 start = s_chan->pCurr; // set up the current pos
439 if (start == spu.spuMemC) // ?
442 if (s_chan->prevflags & 1) // 1: stop/loop
444 if (!(s_chan->prevflags & 2))
447 start = s_chan->pLoop;
450 check_irq(ch, start); // hack, see check_irq below..
452 predict_nr = start[0];
453 shift_factor = predict_nr & 0xf;
456 decode_block_data(SB, start + 2, predict_nr, shift_factor);
460 s_chan->pLoop = start; // loop adress
464 if (flags & 1) { // 1: stop/loop
465 start = s_chan->pLoop;
466 check_irq(ch, start); // hack.. :(
469 if (start - spu.spuMemC >= 0x80000)
472 s_chan->pCurr = start; // store values for next cycle
473 s_chan->prevflags = flags;
478 // do block, but ignore sample data
479 static int skip_block(int ch)
481 SPUCHAN *s_chan = &spu.s_chan[ch];
482 unsigned char *start = s_chan->pCurr;
486 if (s_chan->prevflags & 1) {
487 if (!(s_chan->prevflags & 2))
490 start = s_chan->pLoop;
493 check_irq(ch, start);
497 s_chan->pLoop = start;
502 start = s_chan->pLoop;
503 check_irq(ch, start);
506 s_chan->pCurr = start;
507 s_chan->prevflags = flags;
512 #ifdef THREAD_ENABLED
514 static int decode_block_work(int ch, int *SB)
516 const unsigned char *ram = spu.spuMemC;
517 int predict_nr, shift_factor, flags;
518 int start = worker->ch[ch].start;
519 int loop = worker->ch[ch].loop;
521 predict_nr = ram[start];
522 shift_factor = predict_nr & 0xf;
525 decode_block_data(SB, ram + start + 2, predict_nr, shift_factor);
527 flags = ram[start + 1];
529 loop = start; // loop adress
533 if (flags & 1) // 1: stop/loop
536 worker->ch[ch].start = start & 0x7ffff;
537 worker->ch[ch].loop = loop;
544 // if irq is going to trigger sooner than in upd_samples, set upd_samples
545 static void scan_for_irq(int ch, unsigned int *upd_samples)
547 SPUCHAN *s_chan = &spu.s_chan[ch];
548 int pos, sinc, sinc_inv, end;
549 unsigned char *block;
552 block = s_chan->pCurr;
555 end = pos + *upd_samples * sinc;
557 pos += (28 - s_chan->iSBPos) << 16;
560 if (block == spu.pSpuIrq)
564 if (flags & 1) { // 1: stop/loop
565 block = s_chan->pLoop;
566 if (block == spu.pSpuIrq) // hack.. (see decode_block)
574 sinc_inv = s_chan->sinc_inv;
576 sinc_inv = s_chan->sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
579 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
580 //xprintf("ch%02d: irq sched: %3d %03d\n",
581 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
585 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
586 static noinline int do_samples_##name(int (*decode_f)(int ch, int *SB), int ch, \
587 int ns_to, int *SB, int sinc, int *spos, int *sbpos) \
593 for (ns = 0; ns < ns_to; ns++) \
598 while (*spos >= 0x10000) \
600 fa = SB[(*sbpos)++]; \
604 d = decode_f(ch, SB); \
621 #define fmod_recv_check \
622 if(spu.s_chan[ch].bFMod==1 && iFMod[ns]) \
623 sinc = FModChangeFrequency(SB, spu.s_chan[ch].iRawPitch, ns)
625 make_do_samples(default, fmod_recv_check, ,
626 StoreInterpolationVal(SB, sinc, fa, spu.s_chan[ch].bFMod==2),
627 ChanBuf[ns] = iGetInterpolationVal(SB, sinc, *spos, spu.s_chan[ch].bFMod==2), )
628 make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa)
630 #define simple_interp_store \
637 #define simple_interp_get \
638 if(sinc<0x10000) /* -> upsampling? */ \
639 InterpolateUp(SB, sinc); /* --> interpolate up */ \
640 else InterpolateDown(SB, sinc); /* --> else down */ \
643 make_do_samples(simple, , ,
644 simple_interp_store, simple_interp_get, )
646 static int do_samples_skip(int ch, int ns_to)
648 SPUCHAN *s_chan = &spu.s_chan[ch];
649 int ret = ns_to, ns, d;
651 s_chan->spos += s_chan->iSBPos << 16;
653 for (ns = 0; ns < ns_to; ns++)
655 s_chan->spos += s_chan->sinc;
656 while (s_chan->spos >= 28*0x10000)
661 s_chan->spos -= 28*0x10000;
665 s_chan->iSBPos = s_chan->spos >> 16;
666 s_chan->spos &= 0xffff;
671 static void do_lsfr_samples(int ns_to, int ctrl,
672 unsigned int *dwNoiseCount, unsigned int *dwNoiseVal)
674 unsigned int counter = *dwNoiseCount;
675 unsigned int val = *dwNoiseVal;
676 unsigned int level, shift, bit;
679 // modified from DrHell/shalma, no fraction
680 level = (ctrl >> 10) & 0x0f;
681 level = 0x8000 >> level;
683 for (ns = 0; ns < ns_to; ns++)
686 if (counter >= level)
689 shift = (val >> 10) & 0x1f;
690 bit = (0x69696969 >> shift) & 1;
691 bit ^= (val >> 15) & 1;
692 val = (val << 1) | bit;
695 ChanBuf[ns] = (signed short)val;
698 *dwNoiseCount = counter;
702 static int do_samples_noise(int ch, int ns_to)
706 ret = do_samples_skip(ch, ns_to);
708 do_lsfr_samples(ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
714 // asm code; lv and rv must be 0-3fff
715 extern void mix_chan(int start, int count, int lv, int rv);
716 extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb);
718 static void mix_chan(int start, int count, int lv, int rv)
720 int *dst = SSumLR + start * 2;
721 const int *src = ChanBuf + start;
728 l = (sval * lv) >> 14;
729 r = (sval * rv) >> 14;
735 static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb)
737 int *dst = SSumLR + start * 2;
738 int *drvb = rvb + start * 2;
739 const int *src = ChanBuf + start;
746 l = (sval * lv) >> 14;
747 r = (sval * rv) >> 14;
756 // 0x0800-0x0bff Voice 1
757 // 0x0c00-0x0fff Voice 3
758 static noinline void do_decode_bufs(unsigned short *mem, int which,
759 int count, int decode_pos)
761 unsigned short *dst = &mem[0x800/2 + which*0x400/2];
762 const int *src = ChanBuf;
763 int cursor = decode_pos;
768 dst[cursor] = *src++;
772 // decode_pos is updated and irqs are checked later, after voice loop
775 static void do_silent_chans(int ns_to, int silentch)
781 mask = silentch & 0xffffff;
782 for (ch = 0; mask != 0; ch++, mask >>= 1)
784 if (!(mask & 1)) continue;
785 if (spu.dwChannelDead & (1<<ch)) continue;
787 s_chan = &spu.s_chan[ch];
788 if (s_chan->pCurr > spu.pSpuIrq && s_chan->pLoop > spu.pSpuIrq)
791 s_chan->spos += s_chan->iSBPos << 16;
794 s_chan->spos += s_chan->sinc * ns_to;
795 while (s_chan->spos >= 28 * 0x10000)
797 unsigned char *start = s_chan->pCurr;
800 if (start == s_chan->pCurr || start - spu.spuMemC < 0x1000)
802 // looping on self or stopped(?)
803 spu.dwChannelDead |= 1<<ch;
808 s_chan->spos -= 28 * 0x10000;
813 static void do_channels(int ns_to)
822 mask = spu.dwChannelOn & 0xffffff;
823 for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all...
825 if (!(mask & 1)) continue; // channel not playing? next
827 s_chan = &spu.s_chan[ch];
832 d = do_samples_noise(ch, ns_to);
833 else if (s_chan->bFMod == 2
834 || (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 0))
835 d = do_samples_noint(decode_block, ch, ns_to,
836 SB, sinc, &s_chan->spos, &s_chan->iSBPos);
837 else if (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 1)
838 d = do_samples_simple(decode_block, ch, ns_to,
839 SB, sinc, &s_chan->spos, &s_chan->iSBPos);
841 d = do_samples_default(decode_block, ch, ns_to,
842 SB, sinc, &s_chan->spos, &s_chan->iSBPos);
844 d = MixADSR(&s_chan->ADSRX, d);
846 spu.dwChannelOn &= ~(1 << ch);
847 s_chan->ADSRX.EnvelopeVol = 0;
848 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
851 if (ch == 1 || ch == 3)
853 do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos);
854 spu.decode_dirty_ch |= 1 << ch;
857 if (s_chan->bFMod == 2) // fmod freq channel
858 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
859 if (s_chan->bRVBActive)
860 mix_chan_rvb(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, spu.sRVBStart);
862 mix_chan(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume);
866 static void do_samples_finish(int ns_to, int silentch, int decode_pos);
868 // optional worker thread handling
870 #ifdef THREAD_ENABLED
872 static void thread_work_start(void);
873 static void thread_work_wait_sync(void);
875 static void queue_channel_work(int ns_to, int silentch)
877 const SPUCHAN *s_chan;
881 worker->ns_to = ns_to;
882 worker->ctrl = spu.spuCtrl;
883 worker->decode_pos = spu.decode_pos;
884 worker->silentch = silentch;
886 mask = worker->chmask = spu.dwChannelOn & 0xffffff;
887 for (ch = 0; mask != 0; ch++, mask >>= 1)
889 if (!(mask & 1)) continue;
891 s_chan = &spu.s_chan[ch];
892 worker->ch[ch].spos = s_chan->spos;
893 worker->ch[ch].sbpos = s_chan->iSBPos;
894 worker->ch[ch].sinc = s_chan->sinc;
895 worker->ch[ch].adsr = s_chan->ADSRX;
896 worker->ch[ch].start = s_chan->pCurr - spu.spuMemC;
897 worker->ch[ch].loop = s_chan->pLoop - spu.spuMemC;
898 if (s_chan->prevflags & 1)
899 worker->ch[ch].start = worker->ch[ch].loop;
901 worker->ch[ch].ns_to = do_samples_skip(ch, ns_to);
908 static void do_channel_work(void)
910 unsigned int mask, endmask = 0;
911 unsigned int decode_dirty_ch = 0;
912 int *SB, sinc, spos, sbpos;
916 ns_to = worker->ns_to;
917 memset(spu.sRVBStart, 0, ns_to * sizeof(spu.sRVBStart[0]) * 2);
919 mask = worker->chmask;
920 for (ch = 0; mask != 0; ch++, mask >>= 1)
922 if (!(mask & 1)) continue;
924 d = worker->ch[ch].ns_to;
925 spos = worker->ch[ch].spos;
926 sbpos = worker->ch[ch].sbpos;
927 sinc = worker->ch[ch].sinc;
929 s_chan = &spu.s_chan[ch];
933 do_lsfr_samples(d, worker->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
934 else if (s_chan->bFMod == 2
935 || (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 0))
936 do_samples_noint(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
937 else if (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 1)
938 do_samples_simple(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
940 do_samples_default(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
942 d = MixADSR(&worker->ch[ch].adsr, d);
945 worker->ch[ch].adsr.EnvelopeVol = 0;
946 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
949 if (ch == 1 || ch == 3)
951 do_decode_bufs(spu.spuMem, ch/2, ns_to, worker->decode_pos);
952 decode_dirty_ch |= 1 << ch;
955 if (s_chan->bFMod == 2) // fmod freq channel
956 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
957 if (s_chan->bRVBActive)
958 mix_chan_rvb(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, spu.sRVBStart);
960 mix_chan(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume);
963 worker->r_chan_end = endmask;
964 worker->r_decode_dirty = decode_dirty_ch;
967 static void sync_worker_thread(void)
972 if (!worker->pending)
975 thread_work_wait_sync();
978 mask = worker->chmask;
979 for (ch = 0; mask != 0; ch++, mask >>= 1) {
980 if (!(mask & 1)) continue;
982 // be sure there was no keyoff while thread was working
983 if (spu.s_chan[ch].ADSRX.State != ADSR_RELEASE)
984 spu.s_chan[ch].ADSRX.State = worker->ch[ch].adsr.State;
985 spu.s_chan[ch].ADSRX.EnvelopeVol = worker->ch[ch].adsr.EnvelopeVol;
988 spu.dwChannelOn &= ~worker->r_chan_end;
989 spu.decode_dirty_ch |= worker->r_decode_dirty;
991 do_samples_finish(worker->ns_to, worker->silentch,
997 static void queue_channel_work(int ns_to, int silentch) {}
998 static void sync_worker_thread(void) {}
1000 #endif // THREAD_ENABLED
1002 ////////////////////////////////////////////////////////////////////////
1003 // MAIN SPU FUNCTION
1004 // here is the main job handler...
1005 ////////////////////////////////////////////////////////////////////////
1007 void do_samples(unsigned int cycles_to, int do_sync)
1014 cycle_diff = cycles_to - spu.cycles_played;
1015 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
1017 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
1018 spu.cycles_played = cycles_to;
1022 if (cycle_diff < 2 * 768)
1025 ns_to = (cycle_diff / 768 + 1) & ~1;
1026 if (ns_to > NSSIZE) {
1027 // should never happen
1028 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
1032 //////////////////////////////////////////////////////
1033 // special irq handling in the decode buffers (0x0000-0x1000)
1035 // the decode buffers are located in spu memory in the following way:
1036 // 0x0000-0x03ff CD audio left
1037 // 0x0400-0x07ff CD audio right
1038 // 0x0800-0x0bff Voice 1
1039 // 0x0c00-0x0fff Voice 3
1040 // and decoded data is 16 bit for one sample
1042 // even if voices 1/3 are off or no cd audio is playing, the internal
1043 // play positions will move on and wrap after 0x400 bytes.
1044 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
1045 // increase this pointer on each sample by 2 bytes. If this pointer
1046 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
1049 if (unlikely((spu.spuCtrl & CTRL_IRQ)
1050 && spu.pSpuIrq < spu.spuMemC+0x1000))
1052 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1053 int left = (irq_pos - spu.decode_pos) & 0x1ff;
1054 if (0 < left && left <= ns_to)
1056 //xprintf("decoder irq %x\n", spu.decode_pos);
1062 sync_worker_thread();
1064 mask = spu.dwNewChannel & 0xffffff;
1065 for (ch = 0; mask != 0; ch++, mask >>= 1) {
1070 silentch = ~spu.dwChannelOn & 0xffffff;
1072 if (spu.dwChannelOn == 0) {
1074 do_samples_finish(ns_to, silentch, spu.decode_pos);
1077 if (do_sync || worker == NULL || !spu_config.iUseThread) {
1079 do_samples_finish(ns_to, silentch, spu.decode_pos);
1082 queue_channel_work(ns_to, silentch);
1086 // advance "stopped" channels that can cause irqs
1087 // (all chans are always playing on the real thing..)
1088 if (spu.spuCtrl & CTRL_IRQ)
1089 do_silent_chans(ns_to, silentch);
1091 spu.cycles_played += ns_to * 768;
1092 spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
1095 static void do_samples_finish(int ns_to, int silentch, int decode_pos)
1097 int volmult = spu_config.iVolume;
1101 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
1103 memset(&spu.spuMem[0x800/2], 0, 0x400);
1104 spu.decode_dirty_ch &= ~(1<<1);
1106 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
1108 memset(&spu.spuMem[0xc00/2], 0, 0x400);
1109 spu.decode_dirty_ch &= ~(1<<3);
1112 //---------------------------------------------------//
1113 // mix XA infos (if any)
1115 MixXA(ns_to, decode_pos);
1117 ///////////////////////////////////////////////////////
1118 // mix all channels (including reverb) into one buffer
1120 if(spu_config.iUseReverb)
1123 if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
1125 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
1126 spu.pS += ns_to * 2;
1129 for (ns = 0; ns < ns_to * 2; )
1131 d = SSumLR[ns]; SSumLR[ns] = 0;
1132 d = d * volmult >> 10;
1137 d = SSumLR[ns]; SSumLR[ns] = 0;
1138 d = d * volmult >> 10;
1145 void schedule_next_irq(void)
1147 unsigned int upd_samples;
1150 if (spu.scheduleCallback == NULL)
1153 upd_samples = 44100 / 50;
1155 for (ch = 0; ch < MAXCHAN; ch++)
1157 if (spu.dwChannelDead & (1 << ch))
1159 if ((unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
1160 && (unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
1163 scan_for_irq(ch, &upd_samples);
1166 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
1168 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1169 int left = (irq_pos - spu.decode_pos) & 0x1ff;
1170 if (0 < left && left < upd_samples) {
1171 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
1176 if (upd_samples < 44100 / 50)
1177 spu.scheduleCallback(upd_samples * 768);
1180 // SPU ASYNC... even newer epsxe func
1181 // 1 time every 'cycle' cycles... harhar
1183 // rearmed: called dynamically now
1185 void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
1187 do_samples(cycle, 0);
1189 if (spu.spuCtrl & CTRL_IRQ)
1190 schedule_next_irq();
1193 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
1194 spu.pS = (short *)spu.pSpuBuffer;
1196 if (spu_config.iTempo) {
1197 if (!out_current->busy())
1198 // cause more samples to be generated
1199 // (and break some games because of bad sync)
1200 spu.cycles_played -= 44100 / 60 / 2 * 768;
1205 // SPU UPDATE... new epsxe func
1206 // 1 time every 32 hsync lines
1207 // (312/32)x50 in pal
1208 // (262/32)x60 in ntsc
1210 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
1211 // leave that func in the linux port, until epsxe linux is using
1212 // the async function as well
1214 void CALLBACK SPUupdate(void)
1220 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
1223 if(!xap->freq) return; // no xa freq ? bye
1225 FeedXA(xap); // call main XA feeder
1229 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
1231 if (!pcm) return -1;
1232 if (nbytes<=0) return -1;
1234 return FeedCDDA((unsigned char *)pcm, nbytes);
1237 // to be called after state load
1238 void ClearWorkingState(void)
1240 memset(SSumLR, 0, NSSIZE * 2 * 4); // init some mixing buffers
1241 memset(iFMod, 0, sizeof(iFMod));
1242 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
1245 // SETUPSTREAMS: init most of the spu buffers
1246 void SetupStreams(void)
1250 spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer
1251 spu.sRVBStart = calloc(NSSIZE * 2, sizeof(spu.sRVBStart[0]));
1252 SSumLR = calloc(NSSIZE * 2, sizeof(SSumLR[0]));
1254 spu.XAStart = // alloc xa buffer
1255 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1256 spu.XAEnd = spu.XAStart + 44100;
1257 spu.XAPlay = spu.XAStart;
1258 spu.XAFeed = spu.XAStart;
1260 spu.CDDAStart = // alloc cdda buffer
1261 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
1262 spu.CDDAEnd = spu.CDDAStart + 16384;
1263 spu.CDDAPlay = spu.CDDAStart;
1264 spu.CDDAFeed = spu.CDDAStart;
1266 for(i=0;i<MAXCHAN;i++) // loop sound channels
1268 spu.s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1269 spu.s_chan[i].ADSRX.SustainIncrease = 1;
1270 spu.s_chan[i].pLoop=spu.spuMemC;
1271 spu.s_chan[i].pCurr=spu.spuMemC;
1274 ClearWorkingState();
1276 spu.bSpuInit=1; // flag: we are inited
1279 // REMOVESTREAMS: free most buffer
1280 void RemoveStreams(void)
1282 free(spu.pSpuBuffer); // free mixing buffer
1283 spu.pSpuBuffer = NULL;
1284 free(spu.sRVBStart); // free reverb buffer
1285 spu.sRVBStart = NULL;
1288 free(spu.XAStart); // free XA buffer
1290 free(spu.CDDAStart); // free CDDA buffer
1291 spu.CDDAStart = NULL;
1294 #if defined(C64X_DSP)
1296 /* special code for TI C64x DSP */
1297 #include "spu_c64x.c"
1299 #elif defined(THREAD_ENABLED)
1301 #include <pthread.h>
1302 #include <semaphore.h>
1311 /* generic pthread implementation */
1313 static void thread_work_start(void)
1315 sem_post(&t.sem_avail);
1318 static void thread_work_wait_sync(void)
1320 sem_wait(&t.sem_done);
1323 static void *spu_worker_thread(void *unused)
1326 sem_wait(&t.sem_avail);
1327 if (worker->exit_thread)
1332 sem_post(&t.sem_done);
1338 static void init_spu_thread(void)
1342 if (sysconf(_SC_NPROCESSORS_ONLN) <= 1)
1345 worker = calloc(1, sizeof(*worker));
1348 ret = sem_init(&t.sem_avail, 0, 0);
1350 goto fail_sem_avail;
1351 ret = sem_init(&t.sem_done, 0, 0);
1355 ret = pthread_create(&t.thread, NULL, spu_worker_thread, NULL);
1362 sem_destroy(&t.sem_done);
1364 sem_destroy(&t.sem_avail);
1370 static void exit_spu_thread(void)
1374 worker->exit_thread = 1;
1375 sem_post(&t.sem_avail);
1376 pthread_join(t.thread, NULL);
1377 sem_destroy(&t.sem_done);
1378 sem_destroy(&t.sem_avail);
1383 #else // if !THREAD_ENABLED
1385 static void init_spu_thread(void)
1389 static void exit_spu_thread(void)
1395 // SPUINIT: this func will be called first by the main emu
1396 long CALLBACK SPUinit(void)
1398 spu.spuMemC = calloc(1, 512 * 1024);
1399 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1402 spu.s_chan = calloc(MAXCHAN+1, sizeof(spu.s_chan[0])); // channel + 1 infos (1 is security for fmod handling)
1406 spu.pSpuIrq = spu.spuMemC;
1408 SetupStreams(); // prepare streaming
1410 if (spu_config.iVolume == 0)
1411 spu_config.iVolume = 768; // 1024 is 1.0
1418 // SPUOPEN: called by main emu after init
1419 long CALLBACK SPUopen(void)
1421 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
1423 SetupSound(); // setup sound (before init!)
1427 return PSE_SPU_ERR_SUCCESS;
1430 // SPUCLOSE: called before shutdown
1431 long CALLBACK SPUclose(void)
1433 if (!spu.bSPUIsOpen) return 0; // some security
1435 spu.bSPUIsOpen = 0; // no more open
1437 out_current->finish(); // no more sound handling
1442 // SPUSHUTDOWN: called by main emu on final exit
1443 long CALLBACK SPUshutdown(void)
1454 RemoveStreams(); // no more streaming
1460 // SPUTEST: we don't test, we are always fine ;)
1461 long CALLBACK SPUtest(void)
1466 // SPUCONFIGURE: call config dialog
1467 long CALLBACK SPUconfigure(void)
1472 // StartCfgTool("CFG");
1477 // SPUABOUT: show about window
1478 void CALLBACK SPUabout(void)
1483 // StartCfgTool("ABOUT");
1488 // this functions will be called once,
1489 // passes a callback that should be called on SPU-IRQ/cdda volume change
1490 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1492 spu.irqCallback = callback;
1495 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1497 spu.cddavCallback = CDDAVcallback;
1500 void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1502 spu.scheduleCallback = callback;
1505 // COMMON PLUGIN INFO FUNCS
1507 char * CALLBACK PSEgetLibName(void)
1509 return _(libraryName);
1512 unsigned long CALLBACK PSEgetLibType(void)
1517 unsigned long CALLBACK PSEgetLibVersion(void)
1519 return (1 << 16) | (6 << 8);
1522 char * SPUgetLibInfos(void)
1524 return _(libraryInfo);
1529 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1531 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1533 if (spu.s_chan == NULL)
1536 for(;ch<MAXCHAN;ch++)
1538 if (!(spu.dwChannelOn & (1<<ch)))
1540 if (spu.s_chan[ch].bFMod == 2)
1541 fmod_chans |= 1 << ch;
1542 if (spu.s_chan[ch].bNoise)
1543 noise_chans |= 1 << ch;
1544 if((spu.spuCtrl&CTRL_IRQ) && spu.s_chan[ch].pCurr <= spu.pSpuIrq && spu.s_chan[ch].pLoop <= spu.pSpuIrq)
1545 irq_chans |= 1 << ch;
1548 *chans_out = spu.dwChannelOn;
1549 *run_chans = ~spu.dwChannelOn & ~spu.dwChannelDead & irq_chans;
1550 *fmod_chans_out = fmod_chans;
1551 *noise_chans_out = noise_chans;
1554 // vim:shiftwidth=1:expandtab