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 ***************************************************************************/
25 #include "externals.h"
26 #include "registers.h"
28 #include "spu_config.h"
31 #include "arm_features.h"
35 #define ssat32_to_16(v) \
36 asm("ssat %0,#16,%1" : "=r" (v) : "r" (v))
38 #define ssat32_to_16(v) do { \
39 if (v < -32768) v = -32768; \
40 else if (v > 32767) v = 32767; \
44 #define PSXCLK 33868800 /* 33.8688 MHz */
46 // intended to be ~1 frame
47 #define IRQ_NEAR_BLOCKS 32
50 #if defined (USEMACOSX)
51 static char * libraryName = N_("Mac OS X Sound");
52 #elif defined (USEALSA)
53 static char * libraryName = N_("ALSA Sound");
54 #elif defined (USEOSS)
55 static char * libraryName = N_("OSS Sound");
56 #elif defined (USESDL)
57 static char * libraryName = N_("SDL Sound");
58 #elif defined (USEPULSEAUDIO)
59 static char * libraryName = N_("PulseAudio Sound");
61 static char * libraryName = N_("NULL Sound");
64 static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete Bernert and the P.E.Op.S. team\n");
72 static int iFMod[NSSIZE];
73 static int RVB[NSSIZE * 2];
76 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
78 ////////////////////////////////////////////////////////////////////////
80 ////////////////////////////////////////////////////////////////////////
82 // dirty inline func includes
87 ////////////////////////////////////////////////////////////////////////
88 // helpers for simple interpolation
91 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
93 // instead of having n equal sample values in a row like:
97 // we compare the current delta change with the next delta change.
99 // if curr_delta is positive,
101 // - and next delta is smaller (or changing direction):
105 // - and next delta significant (at least twice) bigger:
109 // - and next delta is nearly same:
114 // if curr_delta is negative,
116 // - and next delta is smaller (or changing direction):
120 // - and next delta significant (at least twice) bigger:
124 // - and next delta is nearly same:
129 static void InterpolateUp(int *SB, int sinc)
131 if(SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
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 :)
138 if(id1>0) // curr delta positive
141 {SB[28]=id1;SB[32]=2;}
144 SB[28]=(id1*sinc)>>16;
146 SB[28]=(id1*sinc)>>17;
148 else // curr delta negative
151 {SB[28]=id1;SB[32]=2;}
154 SB[28]=(id1*sinc)>>16;
156 SB[28]=(id1*sinc)>>17;
160 if(SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
164 SB[28]=(SB[28]*sinc)>>17;
166 // SB[29]=SB[30]-(SB[28]*((0x10000/sinc)-1));
170 else // no flags? add bigger val (if possible), calc smaller step, set flag1
175 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
178 static void InterpolateDown(int *SB, int sinc)
180 if(sinc>=0x20000L) // we would skip at least one val?
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
188 ////////////////////////////////////////////////////////////////////////
189 // helpers for gauss interpolation
191 #define gval0 (((short*)(&SB[29]))[gpos&3])
192 #define gval(x) ((int)((short*)(&SB[29]))[(gpos+x)&3])
196 ////////////////////////////////////////////////////////////////////////
200 static void do_irq(void)
202 //if(!(spu.spuStat & STAT_IRQ))
204 spu.spuStat |= STAT_IRQ; // asserted status?
205 if(spu.irqCallback) spu.irqCallback();
209 static int check_irq(int ch, unsigned char *pos)
211 if((spu.spuCtrl & (CTRL_ON|CTRL_IRQ)) == (CTRL_ON|CTRL_IRQ) && pos == spu.pSpuIrq)
213 //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC);
220 void check_irq_io(unsigned int addr)
222 unsigned int irq_addr = regAreaGet(H_SPUirqAddr) << 3;
224 if((spu.spuCtrl & (CTRL_ON|CTRL_IRQ)) == (CTRL_ON|CTRL_IRQ) && addr == irq_addr)
226 //printf("io irq %04x\n", irq_addr);
231 ////////////////////////////////////////////////////////////////////////
232 // START SOUND... called by main thread to setup a new sound on a channel
233 ////////////////////////////////////////////////////////////////////////
235 static void StartSoundSB(int *SB)
237 SB[26]=0; // init mixing vars
241 SB[29]=0; // init our interpolation helpers
246 static void StartSoundMain(int ch)
248 SPUCHAN *s_chan = &spu.s_chan[ch];
257 s_chan->pCurr = spu.spuMemC + ((regAreaGetCh(ch, 6) & ~1) << 3);
259 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
260 spu.dwChannelDead&=~(1<<ch);
261 spu.dwChannelsAudible|=1<<ch;
264 static void StartSound(int ch)
267 StartSoundSB(spu.SB + ch * SB_SIZE);
270 ////////////////////////////////////////////////////////////////////////
271 // ALL KIND OF HELPERS
272 ////////////////////////////////////////////////////////////////////////
274 INLINE int FModChangeFrequency(int *SB, int pitch, int ns)
276 unsigned int NP=pitch;
279 NP=((32768L+iFMod[ns])*NP)>>15;
281 if(NP>0x3fff) NP=0x3fff;
284 sinc=NP<<4; // calc frequency
286 SB[32]=1; // reset interpolation
291 ////////////////////////////////////////////////////////////////////////
293 INLINE void StoreInterpolationVal(int *SB, int sinc, int fa, int fmod_freq)
295 if(fmod_freq) // fmod freq channel
301 if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation
309 if(spu_config.iUseInterpolation==1) // simple interpolation
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'
315 SB[32] = 1; // -> flag: calc new interolation
317 else SB[29]=fa; // no interpolation
321 ////////////////////////////////////////////////////////////////////////
323 INLINE int iGetInterpolationVal(int *SB, int sinc, int spos, int fmod_freq)
327 if(fmod_freq) return SB[29];
329 switch(spu_config.iUseInterpolation)
331 //--------------------------------------------------//
332 case 3: // cubic interpolation
338 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
339 fa *= (xd - (2<<15)) / 6;
341 fa += gval(2) - gval(1) - gval(1) + gval0;
342 fa *= (xd - (1<<15)) >> 1;
344 fa += gval(1) - gval0;
350 //--------------------------------------------------//
351 case 2: // gauss interpolation
354 vl = (spos >> 6) & ~3;
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;
362 //--------------------------------------------------//
363 case 1: // simple interpolation
365 if(sinc<0x10000L) // -> upsampling?
366 InterpolateUp(SB, sinc); // --> interpolate up
367 else InterpolateDown(SB, sinc); // --> else down
370 //--------------------------------------------------//
371 default: // no interpolation
375 //--------------------------------------------------//
381 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
383 static const int f[16][2] = {
391 int fa, s_1, s_2, d, s;
396 for (nSample = 0; nSample < 28; src++)
399 s = (int)(signed short)((d & 0x0f) << 12);
401 fa = s >> shift_factor;
402 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
405 dest[nSample++] = fa;
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);
412 dest[nSample++] = fa;
416 static int decode_block(void *unused, int ch, int *SB)
418 SPUCHAN *s_chan = &spu.s_chan[ch];
419 unsigned char *start;
420 int predict_nr, shift_factor, flags;
423 start = s_chan->pCurr; // set up the current pos
424 if (start == spu.spuMemC) // ?
427 if (s_chan->prevflags & 1) // 1: stop/loop
429 if (!(s_chan->prevflags & 2))
432 start = s_chan->pLoop;
435 check_irq(ch, start);
437 predict_nr = start[0];
438 shift_factor = predict_nr & 0xf;
441 decode_block_data(SB, start + 2, predict_nr, shift_factor);
444 if (flags & 4 && !s_chan->bIgnoreLoop)
445 s_chan->pLoop = start; // loop adress
449 s_chan->pCurr = start; // store values for next cycle
450 s_chan->prevflags = flags;
455 // do block, but ignore sample data
456 static int skip_block(int ch)
458 SPUCHAN *s_chan = &spu.s_chan[ch];
459 unsigned char *start = s_chan->pCurr;
463 if (s_chan->prevflags & 1) {
464 if (!(s_chan->prevflags & 2))
467 start = s_chan->pLoop;
470 check_irq(ch, start);
473 if (flags & 4 && !s_chan->bIgnoreLoop)
474 s_chan->pLoop = start;
478 s_chan->pCurr = start;
479 s_chan->prevflags = flags;
484 // if irq is going to trigger sooner than in upd_samples, set upd_samples
485 static void scan_for_irq(int ch, unsigned int *upd_samples)
487 SPUCHAN *s_chan = &spu.s_chan[ch];
488 int pos, sinc, sinc_inv, end;
489 unsigned char *block;
492 block = s_chan->pCurr;
495 end = pos + *upd_samples * sinc;
497 pos += (28 - s_chan->iSBPos) << 16;
500 if (block == spu.pSpuIrq)
504 if (flags & 1) { // 1: stop/loop
505 block = s_chan->pLoop;
512 sinc_inv = s_chan->sinc_inv;
514 sinc_inv = s_chan->sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
517 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
518 //xprintf("ch%02d: irq sched: %3d %03d\n",
519 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
523 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
524 static noinline int do_samples_##name( \
525 int (*decode_f)(void *context, int ch, int *SB), void *ctx, \
526 int ch, int ns_to, int *SB, int sinc, int *spos, int *sbpos) \
532 for (ns = 0; ns < ns_to; ns++) \
537 while (*spos >= 0x10000) \
539 fa = SB[(*sbpos)++]; \
543 d = decode_f(ctx, ch, SB); \
560 #define fmod_recv_check \
561 if(spu.s_chan[ch].bFMod==1 && iFMod[ns]) \
562 sinc = FModChangeFrequency(SB, spu.s_chan[ch].iRawPitch, ns)
564 make_do_samples(default, fmod_recv_check, ,
565 StoreInterpolationVal(SB, sinc, fa, spu.s_chan[ch].bFMod==2),
566 ChanBuf[ns] = iGetInterpolationVal(SB, sinc, *spos, spu.s_chan[ch].bFMod==2), )
567 make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa)
569 #define simple_interp_store \
576 #define simple_interp_get \
577 if(sinc<0x10000) /* -> upsampling? */ \
578 InterpolateUp(SB, sinc); /* --> interpolate up */ \
579 else InterpolateDown(SB, sinc); /* --> else down */ \
582 make_do_samples(simple, , ,
583 simple_interp_store, simple_interp_get, )
585 static int do_samples_skip(int ch, int ns_to)
587 SPUCHAN *s_chan = &spu.s_chan[ch];
588 int spos = s_chan->spos;
589 int sinc = s_chan->sinc;
590 int ret = ns_to, ns, d;
592 spos += s_chan->iSBPos << 16;
594 for (ns = 0; ns < ns_to; ns++)
597 while (spos >= 28*0x10000)
606 s_chan->iSBPos = spos >> 16;
607 s_chan->spos = spos & 0xffff;
612 static void do_lsfr_samples(int ns_to, int ctrl,
613 unsigned int *dwNoiseCount, unsigned int *dwNoiseVal)
615 unsigned int counter = *dwNoiseCount;
616 unsigned int val = *dwNoiseVal;
617 unsigned int level, shift, bit;
620 // modified from DrHell/shalma, no fraction
621 level = (ctrl >> 10) & 0x0f;
622 level = 0x8000 >> level;
624 for (ns = 0; ns < ns_to; ns++)
627 if (counter >= level)
630 shift = (val >> 10) & 0x1f;
631 bit = (0x69696969 >> shift) & 1;
632 bit ^= (val >> 15) & 1;
633 val = (val << 1) | bit;
636 ChanBuf[ns] = (signed short)val;
639 *dwNoiseCount = counter;
643 static int do_samples_noise(int ch, int ns_to)
647 ret = do_samples_skip(ch, ns_to);
649 do_lsfr_samples(ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
655 // asm code; lv and rv must be 0-3fff
656 extern void mix_chan(int *SSumLR, int count, int lv, int rv);
657 extern void mix_chan_rvb(int *SSumLR, int count, int lv, int rv, int *rvb);
659 static void mix_chan(int *SSumLR, int count, int lv, int rv)
661 const int *src = ChanBuf;
668 l = (sval * lv) >> 14;
669 r = (sval * rv) >> 14;
675 static void mix_chan_rvb(int *SSumLR, int count, int lv, int rv, int *rvb)
677 const int *src = ChanBuf;
686 l = (sval * lv) >> 14;
687 r = (sval * rv) >> 14;
696 // 0x0800-0x0bff Voice 1
697 // 0x0c00-0x0fff Voice 3
698 static noinline void do_decode_bufs(unsigned short *mem, int which,
699 int count, int decode_pos)
701 unsigned short *dst = &mem[0x800/2 + which*0x400/2];
702 const int *src = ChanBuf;
703 int cursor = decode_pos;
708 dst[cursor] = *src++;
712 // decode_pos is updated and irqs are checked later, after voice loop
715 static void do_silent_chans(int ns_to, int silentch)
721 mask = silentch & 0xffffff;
722 for (ch = 0; mask != 0; ch++, mask >>= 1)
724 if (!(mask & 1)) continue;
725 if (spu.dwChannelDead & (1<<ch)) continue;
727 s_chan = &spu.s_chan[ch];
728 if (s_chan->pCurr > spu.pSpuIrq && s_chan->pLoop > spu.pSpuIrq)
731 s_chan->spos += s_chan->iSBPos << 16;
734 s_chan->spos += s_chan->sinc * ns_to;
735 while (s_chan->spos >= 28 * 0x10000)
737 unsigned char *start = s_chan->pCurr;
740 if (start == s_chan->pCurr || start - spu.spuMemC < 0x1000)
742 // looping on self or stopped(?)
743 spu.dwChannelDead |= 1<<ch;
748 s_chan->spos -= 28 * 0x10000;
753 static void do_channels(int ns_to)
760 do_rvb = spu.rvb->StartAddr && spu_config.iUseReverb;
762 memset(RVB, 0, ns_to * sizeof(RVB[0]) * 2);
764 mask = spu.dwNewChannel & 0xffffff;
765 for (ch = 0; mask != 0; ch++, mask >>= 1) {
770 mask = spu.dwChannelsAudible & 0xffffff;
771 for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all...
773 if (!(mask & 1)) continue; // channel not playing? next
775 s_chan = &spu.s_chan[ch];
776 SB = spu.SB + ch * SB_SIZE;
778 if (spu.s_chan[ch].bNewPitch)
779 SB[32] = 1; // reset interpolation
780 spu.s_chan[ch].bNewPitch = 0;
783 d = do_samples_noise(ch, ns_to);
784 else if (s_chan->bFMod == 2
785 || (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 0))
786 d = do_samples_noint(decode_block, NULL, ch, ns_to,
787 SB, sinc, &s_chan->spos, &s_chan->iSBPos);
788 else if (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 1)
789 d = do_samples_simple(decode_block, NULL, ch, ns_to,
790 SB, sinc, &s_chan->spos, &s_chan->iSBPos);
792 d = do_samples_default(decode_block, NULL, ch, ns_to,
793 SB, sinc, &s_chan->spos, &s_chan->iSBPos);
795 d = MixADSR(&s_chan->ADSRX, d);
797 spu.dwChannelsAudible &= ~(1 << ch);
798 s_chan->ADSRX.State = ADSR_RELEASE;
799 s_chan->ADSRX.EnvelopeVol = 0;
800 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
803 if (ch == 1 || ch == 3)
805 do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos);
806 spu.decode_dirty_ch |= 1 << ch;
809 if (s_chan->bFMod == 2) // fmod freq channel
810 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
811 if (s_chan->bRVBActive && do_rvb)
812 mix_chan_rvb(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, RVB);
814 mix_chan(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume);
817 MixXA(spu.SSumLR, RVB, ns_to, spu.decode_pos);
819 if (spu.rvb->StartAddr) {
821 REVERBDo(spu.SSumLR, RVB, ns_to, spu.rvb->CurrAddr);
823 spu.rvb->CurrAddr += ns_to / 2;
824 while (spu.rvb->CurrAddr >= 0x40000)
825 spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr;
829 static void do_samples_finish(int *SSumLR, int ns_to,
830 int silentch, int decode_pos);
832 // optional worker thread handling
834 #if P_HAVE_PTHREAD || defined(WANT_THREAD_CODE)
836 // worker thread state
837 static struct spu_worker {
840 unsigned int exit_thread;
841 unsigned int i_ready;
842 unsigned int i_reaped;
843 unsigned int last_boot_cnt; // dsp
844 unsigned int ram_dirty;
846 // aligning for C64X_DSP
847 unsigned int _pad0[128/4];
852 unsigned int active; // dsp
853 unsigned int boot_cnt;
855 unsigned int _pad1[128/4];
862 unsigned int channels_new;
863 unsigned int channels_on;
864 unsigned int channels_silent;
873 unsigned short ns_to;
874 unsigned short bNoise:1;
875 unsigned short bFMod:2;
876 unsigned short bRVBActive:1;
877 unsigned short bNewPitch:1;
880 int SSumLR[NSSIZE * 2];
884 #define WORK_MAXCNT (sizeof(worker->i) / sizeof(worker->i[0]))
885 #define WORK_I_MASK (WORK_MAXCNT - 1)
887 static void thread_work_start(void);
888 static void thread_work_wait_sync(struct work_item *work, int force);
889 static void thread_sync_caches(void);
890 static int thread_get_i_done(void);
892 static int decode_block_work(void *context, int ch, int *SB)
894 const unsigned char *ram = spu.spuMemC;
895 int predict_nr, shift_factor, flags;
896 struct work_item *work = context;
897 int start = work->ch[ch].start;
898 int loop = work->ch[ch].loop;
900 predict_nr = ram[start];
901 shift_factor = predict_nr & 0xf;
904 decode_block_data(SB, ram + start + 2, predict_nr, shift_factor);
906 flags = ram[start + 1];
908 loop = start; // loop adress
912 if (flags & 1) // 1: stop/loop
915 work->ch[ch].start = start & 0x7ffff;
916 work->ch[ch].loop = loop;
921 static void queue_channel_work(int ns_to, unsigned int silentch)
923 struct work_item *work;
928 work = &worker->i[worker->i_ready & WORK_I_MASK];
930 work->ctrl = spu.spuCtrl;
931 work->decode_pos = spu.decode_pos;
932 work->channels_silent = silentch;
934 mask = work->channels_new = spu.dwNewChannel & 0xffffff;
935 for (ch = 0; mask != 0; ch++, mask >>= 1) {
940 mask = work->channels_on = spu.dwChannelsAudible & 0xffffff;
941 spu.decode_dirty_ch |= mask & 0x0a;
943 for (ch = 0; mask != 0; ch++, mask >>= 1)
945 if (!(mask & 1)) continue;
947 s_chan = &spu.s_chan[ch];
948 work->ch[ch].spos = s_chan->spos;
949 work->ch[ch].sbpos = s_chan->iSBPos;
950 work->ch[ch].sinc = s_chan->sinc;
951 work->ch[ch].adsr = s_chan->ADSRX;
952 work->ch[ch].vol_l = s_chan->iLeftVolume;
953 work->ch[ch].vol_r = s_chan->iRightVolume;
954 work->ch[ch].start = s_chan->pCurr - spu.spuMemC;
955 work->ch[ch].loop = s_chan->pLoop - spu.spuMemC;
956 work->ch[ch].bNoise = s_chan->bNoise;
957 work->ch[ch].bFMod = s_chan->bFMod;
958 work->ch[ch].bRVBActive = s_chan->bRVBActive;
959 work->ch[ch].bNewPitch = s_chan->bNewPitch;
960 if (s_chan->prevflags & 1)
961 work->ch[ch].start = work->ch[ch].loop;
963 d = do_samples_skip(ch, ns_to);
964 work->ch[ch].ns_to = d;
966 // note: d is not accurate on skip
967 d = SkipADSR(&s_chan->ADSRX, d);
969 spu.dwChannelsAudible &= ~(1 << ch);
970 s_chan->ADSRX.State = ADSR_RELEASE;
971 s_chan->ADSRX.EnvelopeVol = 0;
973 s_chan->bNewPitch = 0;
977 if (spu.rvb->StartAddr) {
978 if (spu_config.iUseReverb)
979 work->rvb_addr = spu.rvb->CurrAddr;
981 spu.rvb->CurrAddr += ns_to / 2;
982 while (spu.rvb->CurrAddr >= 0x40000)
983 spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr;
990 static void do_channel_work(struct work_item *work)
993 int *SB, sinc, spos, sbpos;
999 memset(RVB, 0, ns_to * sizeof(RVB[0]) * 2);
1001 mask = work->channels_new;
1002 for (ch = 0; mask != 0; ch++, mask >>= 1) {
1004 StartSoundSB(spu.SB + ch * SB_SIZE);
1007 mask = work->channels_on;
1008 for (ch = 0; mask != 0; ch++, mask >>= 1)
1010 if (!(mask & 1)) continue;
1012 d = work->ch[ch].ns_to;
1013 spos = work->ch[ch].spos;
1014 sbpos = work->ch[ch].sbpos;
1015 sinc = work->ch[ch].sinc;
1017 SB = spu.SB + ch * SB_SIZE;
1018 if (work->ch[ch].bNewPitch)
1019 SB[32] = 1; // reset interpolation
1021 if (work->ch[ch].bNoise)
1022 do_lsfr_samples(d, work->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
1023 else if (work->ch[ch].bFMod == 2
1024 || (work->ch[ch].bFMod == 0 && spu_config.iUseInterpolation == 0))
1025 do_samples_noint(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos);
1026 else if (work->ch[ch].bFMod == 0 && spu_config.iUseInterpolation == 1)
1027 do_samples_simple(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos);
1029 do_samples_default(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos);
1031 d = MixADSR(&work->ch[ch].adsr, d);
1033 work->ch[ch].adsr.EnvelopeVol = 0;
1034 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
1037 if (ch == 1 || ch == 3)
1038 do_decode_bufs(spu.spuMem, ch/2, ns_to, work->decode_pos);
1040 if (work->ch[ch].bFMod == 2) // fmod freq channel
1041 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
1042 if (work->ch[ch].bRVBActive && work->rvb_addr)
1043 mix_chan_rvb(work->SSumLR, ns_to,
1044 work->ch[ch].vol_l, work->ch[ch].vol_r, RVB);
1046 mix_chan(work->SSumLR, ns_to, work->ch[ch].vol_l, work->ch[ch].vol_r);
1050 REVERBDo(work->SSumLR, RVB, ns_to, work->rvb_addr);
1053 static void sync_worker_thread(int force)
1055 struct work_item *work;
1056 int done, used_space;
1058 // rvb offsets will change, thread may be using them
1059 force |= spu.rvb->dirty && spu.rvb->StartAddr;
1061 done = thread_get_i_done() - worker->i_reaped;
1062 used_space = worker->i_ready - worker->i_reaped;
1064 //printf("done: %d use: %d dsp: %u/%u\n", done, used_space,
1065 // worker->boot_cnt, worker->i_done);
1067 while ((force && used_space > 0) || used_space >= WORK_MAXCNT || done > 0) {
1068 work = &worker->i[worker->i_reaped & WORK_I_MASK];
1069 thread_work_wait_sync(work, force);
1071 MixXA(work->SSumLR, RVB, work->ns_to, work->decode_pos);
1072 do_samples_finish(work->SSumLR, work->ns_to,
1073 work->channels_silent, work->decode_pos);
1076 done = thread_get_i_done() - worker->i_reaped;
1077 used_space = worker->i_ready - worker->i_reaped;
1080 thread_sync_caches();
1085 static void queue_channel_work(int ns_to, int silentch) {}
1086 static void sync_worker_thread(int force) {}
1088 static const void * const worker = NULL;
1090 #endif // P_HAVE_PTHREAD || defined(WANT_THREAD_CODE)
1092 ////////////////////////////////////////////////////////////////////////
1093 // MAIN SPU FUNCTION
1094 // here is the main job handler...
1095 ////////////////////////////////////////////////////////////////////////
1097 void do_samples(unsigned int cycles_to, int do_direct)
1099 unsigned int silentch;
1103 cycle_diff = cycles_to - spu.cycles_played;
1104 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
1106 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
1107 spu.cycles_played = cycles_to;
1111 silentch = ~(spu.dwChannelsAudible | spu.dwNewChannel) & 0xffffff;
1113 do_direct |= (silentch == 0xffffff);
1115 sync_worker_thread(do_direct);
1117 if (cycle_diff < 2 * 768)
1120 ns_to = (cycle_diff / 768 + 1) & ~1;
1121 if (ns_to > NSSIZE) {
1122 // should never happen
1123 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
1127 //////////////////////////////////////////////////////
1128 // special irq handling in the decode buffers (0x0000-0x1000)
1130 // the decode buffers are located in spu memory in the following way:
1131 // 0x0000-0x03ff CD audio left
1132 // 0x0400-0x07ff CD audio right
1133 // 0x0800-0x0bff Voice 1
1134 // 0x0c00-0x0fff Voice 3
1135 // and decoded data is 16 bit for one sample
1137 // even if voices 1/3 are off or no cd audio is playing, the internal
1138 // play positions will move on and wrap after 0x400 bytes.
1139 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
1140 // increase this pointer on each sample by 2 bytes. If this pointer
1141 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
1144 if (unlikely((spu.spuCtrl & CTRL_IRQ)
1145 && spu.pSpuIrq < spu.spuMemC+0x1000))
1147 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1148 int left = (irq_pos - spu.decode_pos) & 0x1ff;
1149 if (0 < left && left <= ns_to)
1151 //xprintf("decoder irq %x\n", spu.decode_pos);
1155 check_irq_io(spu.spuAddr);
1157 if (unlikely(spu.rvb->dirty))
1160 if (do_direct || worker == NULL || !spu_config.iUseThread) {
1162 do_samples_finish(spu.SSumLR, ns_to, silentch, spu.decode_pos);
1165 queue_channel_work(ns_to, silentch);
1166 //sync_worker_thread(1); // uncomment for debug
1169 // advance "stopped" channels that can cause irqs
1170 // (all chans are always playing on the real thing..)
1171 if (spu.spuCtrl & CTRL_IRQ)
1172 do_silent_chans(ns_to, silentch);
1174 spu.cycles_played += ns_to * 768;
1175 spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
1178 static void do_samples_finish(int *SSumLR, int ns_to,
1179 int silentch, int decode_pos)
1181 int vol_l = ((int)regAreaGet(H_SPUmvolL) << 17) >> 17;
1182 int vol_r = ((int)regAreaGet(H_SPUmvolR) << 17) >> 17;
1186 // must clear silent channel decode buffers
1187 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1)))
1189 memset(&spu.spuMem[0x800/2], 0, 0x400);
1190 spu.decode_dirty_ch &= ~(1<<1);
1192 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
1194 memset(&spu.spuMem[0xc00/2], 0, 0x400);
1195 spu.decode_dirty_ch &= ~(1<<3);
1198 vol_l = vol_l * spu_config.iVolume >> 10;
1199 vol_r = vol_r * spu_config.iVolume >> 10;
1201 if (!(spu.spuCtrl & CTRL_MUTE) || !(vol_l | vol_r))
1204 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
1205 memset(SSumLR, 0, ns_to * 2 * sizeof(SSumLR[0]));
1206 spu.pS += ns_to * 2;
1209 for (ns = 0; ns < ns_to * 2; )
1211 d = SSumLR[ns]; SSumLR[ns] = 0;
1212 d = d * vol_l >> 14;
1217 d = SSumLR[ns]; SSumLR[ns] = 0;
1218 d = d * vol_r >> 14;
1225 void schedule_next_irq(void)
1227 unsigned int upd_samples;
1230 if (spu.scheduleCallback == NULL)
1233 upd_samples = 44100 / 50;
1235 for (ch = 0; ch < MAXCHAN; ch++)
1237 if (spu.dwChannelDead & (1 << ch))
1239 if ((unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
1240 && (unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
1242 if (spu.s_chan[ch].sinc == 0)
1245 scan_for_irq(ch, &upd_samples);
1248 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
1250 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1251 int left = (irq_pos - spu.decode_pos) & 0x1ff;
1252 if (0 < left && left < upd_samples) {
1253 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
1258 if (upd_samples < 44100 / 50)
1259 spu.scheduleCallback(upd_samples * 768);
1262 // SPU ASYNC... even newer epsxe func
1263 // 1 time every 'cycle' cycles... harhar
1265 // rearmed: called dynamically now
1267 void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
1269 do_samples(cycle, spu_config.iUseFixedUpdates);
1271 if (spu.spuCtrl & CTRL_IRQ)
1272 schedule_next_irq();
1275 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
1276 spu.pS = (short *)spu.pSpuBuffer;
1278 if (spu_config.iTempo) {
1279 if (!out_current->busy())
1280 // cause more samples to be generated
1281 // (and break some games because of bad sync)
1282 spu.cycles_played -= 44100 / 60 / 2 * 768;
1287 // SPU UPDATE... new epsxe func
1288 // 1 time every 32 hsync lines
1289 // (312/32)x50 in pal
1290 // (262/32)x60 in ntsc
1292 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
1293 // leave that func in the linux port, until epsxe linux is using
1294 // the async function as well
1296 void CALLBACK SPUupdate(void)
1302 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap, unsigned int cycle, int is_start)
1305 if(!xap->freq) return; // no xa freq ? bye
1308 do_samples(cycle, 1); // catch up to prevent source underflows later
1310 FeedXA(xap); // call main XA feeder
1314 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes, unsigned int cycle, int is_start)
1316 if (!pcm) return -1;
1317 if (nbytes<=0) return -1;
1320 do_samples(cycle, 1); // catch up to prevent source underflows later
1322 return FeedCDDA((unsigned char *)pcm, nbytes);
1325 // to be called after state load
1326 void ClearWorkingState(void)
1328 memset(iFMod, 0, sizeof(iFMod));
1329 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
1332 // SETUPSTREAMS: init most of the spu buffers
1333 static void SetupStreams(void)
1335 spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer
1336 spu.SSumLR = calloc(NSSIZE * 2, sizeof(spu.SSumLR[0]));
1338 spu.XAStart = malloc(44100 * sizeof(uint32_t)); // alloc xa buffer
1339 spu.XAEnd = spu.XAStart + 44100;
1340 spu.XAPlay = spu.XAStart;
1341 spu.XAFeed = spu.XAStart;
1343 spu.CDDAStart = malloc(CDDA_BUFFER_SIZE); // alloc cdda buffer
1344 spu.CDDAEnd = spu.CDDAStart + 16384;
1345 spu.CDDAPlay = spu.CDDAStart;
1346 spu.CDDAFeed = spu.CDDAStart;
1348 ClearWorkingState();
1351 // REMOVESTREAMS: free most buffer
1352 static void RemoveStreams(void)
1354 free(spu.pSpuBuffer); // free mixing buffer
1355 spu.pSpuBuffer = NULL;
1358 free(spu.XAStart); // free XA buffer
1360 free(spu.CDDAStart); // free CDDA buffer
1361 spu.CDDAStart = NULL;
1364 #if defined(C64X_DSP)
1366 /* special code for TI C64x DSP */
1367 #include "spu_c64x.c"
1369 #elif P_HAVE_PTHREAD
1371 #include <pthread.h>
1372 #include <semaphore.h>
1381 /* generic pthread implementation */
1383 static void thread_work_start(void)
1385 sem_post(&t.sem_avail);
1388 static void thread_work_wait_sync(struct work_item *work, int force)
1390 sem_wait(&t.sem_done);
1393 static int thread_get_i_done(void)
1395 return worker->i_done;
1398 static void thread_sync_caches(void)
1402 static void *spu_worker_thread(void *unused)
1404 struct work_item *work;
1407 sem_wait(&t.sem_avail);
1408 if (worker->exit_thread)
1411 work = &worker->i[worker->i_done & WORK_I_MASK];
1412 do_channel_work(work);
1415 sem_post(&t.sem_done);
1421 static void init_spu_thread(void)
1425 if (sysconf(_SC_NPROCESSORS_ONLN) <= 1)
1428 worker = calloc(1, sizeof(*worker));
1431 ret = sem_init(&t.sem_avail, 0, 0);
1433 goto fail_sem_avail;
1434 ret = sem_init(&t.sem_done, 0, 0);
1438 ret = pthread_create(&t.thread, NULL, spu_worker_thread, NULL);
1442 spu_config.iThreadAvail = 1;
1446 sem_destroy(&t.sem_done);
1448 sem_destroy(&t.sem_avail);
1452 spu_config.iThreadAvail = 0;
1455 static void exit_spu_thread(void)
1459 worker->exit_thread = 1;
1460 sem_post(&t.sem_avail);
1461 pthread_join(t.thread, NULL);
1462 sem_destroy(&t.sem_done);
1463 sem_destroy(&t.sem_avail);
1468 #else // if !P_HAVE_PTHREAD
1470 static void init_spu_thread(void)
1474 static void exit_spu_thread(void)
1480 // SPUINIT: this func will be called first by the main emu
1481 long CALLBACK SPUinit(void)
1485 spu.spuMemC = calloc(1, 512 * 1024);
1488 spu.s_chan = calloc(MAXCHAN+1, sizeof(spu.s_chan[0])); // channel + 1 infos (1 is security for fmod handling)
1489 spu.rvb = calloc(1, sizeof(REVERBInfo));
1490 spu.SB = calloc(MAXCHAN, sizeof(spu.SB[0]) * SB_SIZE);
1494 spu.pSpuIrq = spu.spuMemC;
1496 SetupStreams(); // prepare streaming
1498 if (spu_config.iVolume == 0)
1499 spu_config.iVolume = 768; // 1024 is 1.0
1503 for (i = 0; i < MAXCHAN; i++) // loop sound channels
1505 spu.s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1506 spu.s_chan[i].ADSRX.SustainIncrease = 1;
1507 spu.s_chan[i].pLoop = spu.spuMemC;
1508 spu.s_chan[i].pCurr = spu.spuMemC;
1509 spu.s_chan[i].bIgnoreLoop = 0;
1512 spu.bSpuInit=1; // flag: we are inited
1517 // SPUOPEN: called by main emu after init
1518 long CALLBACK SPUopen(void)
1520 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
1522 SetupSound(); // setup sound (before init!)
1526 return PSE_SPU_ERR_SUCCESS;
1529 // SPUCLOSE: called before shutdown
1530 long CALLBACK SPUclose(void)
1532 if (!spu.bSPUIsOpen) return 0; // some security
1534 spu.bSPUIsOpen = 0; // no more open
1536 out_current->finish(); // no more sound handling
1541 // SPUSHUTDOWN: called by main emu on final exit
1542 long CALLBACK SPUshutdown(void)
1557 RemoveStreams(); // no more streaming
1563 // SPUTEST: we don't test, we are always fine ;)
1564 long CALLBACK SPUtest(void)
1569 // SPUCONFIGURE: call config dialog
1570 long CALLBACK SPUconfigure(void)
1575 // StartCfgTool("CFG");
1580 // SPUABOUT: show about window
1581 void CALLBACK SPUabout(void)
1586 // StartCfgTool("ABOUT");
1591 // this functions will be called once,
1592 // passes a callback that should be called on SPU-IRQ/cdda volume change
1593 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1595 spu.irqCallback = callback;
1598 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(short, short))
1600 spu.cddavCallback = CDDAVcallback;
1603 void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1605 spu.scheduleCallback = callback;
1608 // COMMON PLUGIN INFO FUNCS
1610 char * CALLBACK PSEgetLibName(void)
1612 return _(libraryName);
1615 unsigned long CALLBACK PSEgetLibType(void)
1620 unsigned long CALLBACK PSEgetLibVersion(void)
1622 return (1 << 16) | (6 << 8);
1625 char * SPUgetLibInfos(void)
1627 return _(libraryInfo);
1632 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1634 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1636 if (spu.s_chan == NULL)
1639 for(;ch<MAXCHAN;ch++)
1641 if (!(spu.dwChannelsAudible & (1<<ch)))
1643 if (spu.s_chan[ch].bFMod == 2)
1644 fmod_chans |= 1 << ch;
1645 if (spu.s_chan[ch].bNoise)
1646 noise_chans |= 1 << ch;
1647 if((spu.spuCtrl&CTRL_IRQ) && spu.s_chan[ch].pCurr <= spu.pSpuIrq && spu.s_chan[ch].pLoop <= spu.pSpuIrq)
1648 irq_chans |= 1 << ch;
1651 *chans_out = spu.dwChannelsAudible;
1652 *run_chans = ~spu.dwChannelsAudible & ~spu.dwChannelDead & irq_chans;
1653 *fmod_chans_out = fmod_chans;
1654 *noise_chans_out = noise_chans;
1657 // vim:shiftwidth=1:expandtab