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
75 SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling)
81 #include <semaphore.h>
84 // worker thread state
86 unsigned int pending:1;
87 unsigned int exit_thread:1;
95 unsigned int r_chan_end;
96 unsigned int r_decode_dirty;
108 // might want to add vol and fmod flags..
113 static const void * const worker = NULL;
116 // certain globals (were local before, but with the new timeproc I need em global)
118 static const int f[8][2] = { { 0, 0 },
124 int SSumLR[NSSIZE*2];
127 #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2
129 ////////////////////////////////////////////////////////////////////////
131 ////////////////////////////////////////////////////////////////////////
133 // dirty inline func includes
138 ////////////////////////////////////////////////////////////////////////
139 // helpers for simple interpolation
142 // easy interpolation on upsampling, no special filter, just "Pete's common sense" tm
144 // instead of having n equal sample values in a row like:
148 // we compare the current delta change with the next delta change.
150 // if curr_delta is positive,
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 // if curr_delta is negative,
167 // - and next delta is smaller (or changing direction):
171 // - and next delta significant (at least twice) bigger:
175 // - and next delta is nearly same:
180 static void InterpolateUp(int *SB, int sinc)
182 if(SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass
184 const int id1=SB[30]-SB[29]; // curr delta to next val
185 const int id2=SB[31]-SB[30]; // and next delta to next-next val :)
189 if(id1>0) // curr delta positive
192 {SB[28]=id1;SB[32]=2;}
195 SB[28]=(id1*sinc)>>16;
197 SB[28]=(id1*sinc)>>17;
199 else // curr delta negative
202 {SB[28]=id1;SB[32]=2;}
205 SB[28]=(id1*sinc)>>16;
207 SB[28]=(id1*sinc)>>17;
211 if(SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass
215 SB[28]=(SB[28]*sinc)>>17;
217 // SB[29]=SB[30]-(SB[28]*((0x10000/sinc)-1));
221 else // no flags? add bigger val (if possible), calc smaller step, set flag1
226 // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm
229 static void InterpolateDown(int *SB, int sinc)
231 if(sinc>=0x20000L) // we would skip at least one val?
233 SB[29]+=(SB[30]-SB[29])/2; // add easy weight
234 if(sinc>=0x30000L) // we would skip even more vals?
235 SB[29]+=(SB[31]-SB[30])/2; // add additional next weight
239 ////////////////////////////////////////////////////////////////////////
240 // helpers for gauss interpolation
242 #define gval0 (((short*)(&SB[29]))[gpos&3])
243 #define gval(x) ((int)((short*)(&SB[29]))[(gpos+x)&3])
247 ////////////////////////////////////////////////////////////////////////
251 static void do_irq(void)
253 //if(!(spu.spuStat & STAT_IRQ))
255 spu.spuStat |= STAT_IRQ; // asserted status?
256 if(spu.irqCallback) spu.irqCallback();
260 static int check_irq(int ch, unsigned char *pos)
262 if((spu.spuCtrl & CTRL_IRQ) && pos == spu.pSpuIrq)
264 //printf("ch%d irq %04x\n", ch, pos - spu.spuMemC);
271 ////////////////////////////////////////////////////////////////////////
272 // START SOUND... called by main thread to setup a new sound on a channel
273 ////////////////////////////////////////////////////////////////////////
275 INLINE void StartSound(int ch)
280 s_chan[ch].prevflags=2;
282 s_chan[ch].SB[26]=0; // init mixing vars
284 s_chan[ch].iSBPos=27;
287 s_chan[ch].SB[29]=0; // init our interpolation helpers
292 spu.dwNewChannel&=~(1<<ch); // clear new channel bit
293 spu.dwChannelOn|=1<<ch;
294 spu.dwChannelDead&=~(1<<ch);
297 ////////////////////////////////////////////////////////////////////////
298 // ALL KIND OF HELPERS
299 ////////////////////////////////////////////////////////////////////////
301 INLINE int FModChangeFrequency(int *SB, int pitch, int ns)
303 unsigned int NP=pitch;
306 NP=((32768L+iFMod[ns])*NP)>>15;
308 if(NP>0x3fff) NP=0x3fff;
311 sinc=NP<<4; // calc frequency
312 if(spu_config.iUseInterpolation==1) // freq change in simple interpolation mode
319 ////////////////////////////////////////////////////////////////////////
321 INLINE void StoreInterpolationVal(int *SB, int sinc, int fa, int fmod_freq)
323 if(fmod_freq) // fmod freq channel
329 if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation
337 if(spu_config.iUseInterpolation==1) // simple interpolation
340 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'
343 SB[32] = 1; // -> flag: calc new interolation
345 else SB[29]=fa; // no interpolation
349 ////////////////////////////////////////////////////////////////////////
351 INLINE int iGetInterpolationVal(int *SB, int sinc, int spos, int fmod_freq)
355 if(fmod_freq) return SB[29];
357 switch(spu_config.iUseInterpolation)
359 //--------------------------------------------------//
360 case 3: // cubic interpolation
366 fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0;
367 fa *= (xd - (2<<15)) / 6;
369 fa += gval(2) - gval(1) - gval(1) + gval0;
370 fa *= (xd - (1<<15)) >> 1;
372 fa += gval(1) - gval0;
378 //--------------------------------------------------//
379 case 2: // gauss interpolation
382 vl = (spos >> 6) & ~3;
384 vr=(gauss[vl]*(int)gval0)&~2047;
385 vr+=(gauss[vl+1]*gval(1))&~2047;
386 vr+=(gauss[vl+2]*gval(2))&~2047;
387 vr+=(gauss[vl+3]*gval(3))&~2047;
390 //--------------------------------------------------//
391 case 1: // simple interpolation
393 if(sinc<0x10000L) // -> upsampling?
394 InterpolateUp(SB, sinc); // --> interpolate up
395 else InterpolateDown(SB, sinc); // --> else down
398 //--------------------------------------------------//
399 default: // no interpolation
403 //--------------------------------------------------//
409 static void decode_block_data(int *dest, const unsigned char *src, int predict_nr, int shift_factor)
412 int fa, s_1, s_2, d, s;
417 for (nSample = 0; nSample < 28; src++)
420 s = (int)(signed short)((d & 0x0f) << 12);
422 fa = s >> shift_factor;
423 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
426 dest[nSample++] = fa;
428 s = (int)(signed short)((d & 0xf0) << 8);
429 fa = s >> shift_factor;
430 fa += ((s_1 * f[predict_nr][0])>>6) + ((s_2 * f[predict_nr][1])>>6);
433 dest[nSample++] = fa;
437 static int decode_block(int ch, int *SB)
439 unsigned char *start;
440 int predict_nr, shift_factor, flags;
443 start = s_chan[ch].pCurr; // set up the current pos
444 if (start == spu.spuMemC) // ?
447 if (s_chan[ch].prevflags & 1) // 1: stop/loop
449 if (!(s_chan[ch].prevflags & 2))
452 start = s_chan[ch].pLoop;
455 check_irq(ch, start); // hack, see check_irq below..
457 predict_nr = (int)start[0];
458 shift_factor = predict_nr & 0xf;
461 decode_block_data(SB, start + 2, predict_nr, shift_factor);
465 s_chan[ch].pLoop = start; // loop adress
469 if (flags & 1) { // 1: stop/loop
470 start = s_chan[ch].pLoop;
471 check_irq(ch, start); // hack.. :(
474 if (start - spu.spuMemC >= 0x80000)
477 s_chan[ch].pCurr = start; // store values for next cycle
478 s_chan[ch].prevflags = flags;
483 // do block, but ignore sample data
484 static int skip_block(int ch)
486 unsigned char *start = s_chan[ch].pCurr;
490 if (s_chan[ch].prevflags & 1) {
491 if (!(s_chan[ch].prevflags & 2))
494 start = s_chan[ch].pLoop;
497 check_irq(ch, start);
501 s_chan[ch].pLoop = start;
506 start = s_chan[ch].pLoop;
507 check_irq(ch, start);
510 s_chan[ch].pCurr = start;
511 s_chan[ch].prevflags = flags;
516 #ifdef THREAD_ENABLED
518 static int decode_block_work(int ch, int *SB)
520 int predict_nr, shift_factor, flags;
521 const unsigned char *ram = worker->ram;
522 int start = worker->ch[ch].start;
523 int loop = worker->ch[ch].loop;
525 predict_nr = ram[start];
526 shift_factor = predict_nr & 0xf;
529 decode_block_data(SB, ram + start + 2, predict_nr, shift_factor);
531 flags = ram[start + 1];
533 loop = start; // loop adress
537 if (flags & 1) // 1: stop/loop
540 worker->ch[ch].start = start & 0x7ffff;
541 worker->ch[ch].loop = loop;
548 // if irq is going to trigger sooner than in upd_samples, set upd_samples
549 static void scan_for_irq(int ch, unsigned int *upd_samples)
551 int pos, sinc, sinc_inv, end;
552 unsigned char *block;
555 block = s_chan[ch].pCurr;
556 pos = s_chan[ch].spos;
557 sinc = s_chan[ch].sinc;
558 end = pos + *upd_samples * sinc;
560 pos += (28 - s_chan[ch].iSBPos) << 16;
563 if (block == spu.pSpuIrq)
567 if (flags & 1) { // 1: stop/loop
568 block = s_chan[ch].pLoop;
569 if (block == spu.pSpuIrq) // hack.. (see decode_block)
577 sinc_inv = s_chan[ch].sinc_inv;
579 sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
581 pos -= s_chan[ch].spos;
582 *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1;
583 //xprintf("ch%02d: irq sched: %3d %03d\n",
584 // ch, *upd_samples, *upd_samples * 60 * 263 / 44100);
588 #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
589 static noinline int do_samples_##name(int (*decode_f)(int ch, int *SB), int ch, \
590 int ns_to, int *SB, int sinc, int *spos, int *sbpos) \
596 for (ns = 0; ns < ns_to; ns++) \
601 while (*spos >= 0x10000) \
603 fa = SB[(*sbpos)++]; \
607 d = decode_f(ch, SB); \
624 #define fmod_recv_check \
625 if(s_chan[ch].bFMod==1 && iFMod[ns]) \
626 sinc = FModChangeFrequency(SB, s_chan[ch].iRawPitch, ns)
628 make_do_samples(default, fmod_recv_check, ,
629 StoreInterpolationVal(SB, sinc, fa, s_chan[ch].bFMod==2),
630 ChanBuf[ns] = iGetInterpolationVal(SB, sinc, *spos, s_chan[ch].bFMod==2), )
631 make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa)
633 #define simple_interp_store \
640 #define simple_interp_get \
641 if(sinc<0x10000) /* -> upsampling? */ \
642 InterpolateUp(SB, sinc); /* --> interpolate up */ \
643 else InterpolateDown(SB, sinc); /* --> else down */ \
646 make_do_samples(simple, , ,
647 simple_interp_store, simple_interp_get, )
649 static int do_samples_skip(int ch, int ns_to)
651 int ret = ns_to, ns, d;
653 s_chan[ch].spos += s_chan[ch].iSBPos << 16;
655 for (ns = 0; ns < ns_to; ns++)
657 s_chan[ch].spos += s_chan[ch].sinc;
658 while (s_chan[ch].spos >= 28*0x10000)
663 s_chan[ch].spos -= 28*0x10000;
667 s_chan[ch].iSBPos = s_chan[ch].spos >> 16;
668 s_chan[ch].spos &= 0xffff;
673 static void do_lsfr_samples(int ns_to, int ctrl,
674 unsigned int *dwNoiseCount, unsigned int *dwNoiseVal)
676 unsigned int counter = *dwNoiseCount;
677 unsigned int val = *dwNoiseVal;
678 unsigned int level, shift, bit;
681 // modified from DrHell/shalma, no fraction
682 level = (ctrl >> 10) & 0x0f;
683 level = 0x8000 >> level;
685 for (ns = 0; ns < ns_to; ns++)
688 if (counter >= level)
691 shift = (val >> 10) & 0x1f;
692 bit = (0x69696969 >> shift) & 1;
693 bit ^= (val >> 15) & 1;
694 val = (val << 1) | bit;
697 ChanBuf[ns] = (signed short)val;
700 *dwNoiseCount = counter;
704 static int do_samples_noise(int ch, int ns_to)
708 ret = do_samples_skip(ch, ns_to);
710 do_lsfr_samples(ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
716 // asm code; lv and rv must be 0-3fff
717 extern void mix_chan(int start, int count, int lv, int rv);
718 extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb);
720 static void mix_chan(int start, int count, int lv, int rv)
722 int *dst = SSumLR + start * 2;
723 const int *src = ChanBuf + start;
730 l = (sval * lv) >> 14;
731 r = (sval * rv) >> 14;
737 static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb)
739 int *dst = SSumLR + start * 2;
740 int *drvb = rvb + start * 2;
741 const int *src = ChanBuf + start;
748 l = (sval * lv) >> 14;
749 r = (sval * rv) >> 14;
758 // 0x0800-0x0bff Voice 1
759 // 0x0c00-0x0fff Voice 3
760 static noinline void do_decode_bufs(unsigned short *mem, int which,
761 int count, int decode_pos)
763 unsigned short *dst = &mem[0x800/2 + which*0x400/2];
764 const int *src = ChanBuf;
765 int cursor = decode_pos;
770 dst[cursor] = *src++;
774 // decode_pos is updated and irqs are checked later, after voice loop
777 static void do_silent_chans(int ns_to, int silentch)
781 for (ch = 0; ch < MAXCHAN; ch++)
783 if (!(silentch & (1<<ch))) continue; // already handled
784 if (spu.dwChannelDead & (1<<ch)) continue;
785 if (s_chan[ch].pCurr > spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq)
788 s_chan[ch].spos += s_chan[ch].iSBPos << 16;
789 s_chan[ch].iSBPos = 0;
791 s_chan[ch].spos += s_chan[ch].sinc * ns_to;
792 while (s_chan[ch].spos >= 28 * 0x10000)
794 unsigned char *start = s_chan[ch].pCurr;
797 if (start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000)
799 // looping on self or stopped(?)
800 spu.dwChannelDead |= 1<<ch;
805 s_chan[ch].spos -= 28 * 0x10000;
810 static void do_channels(int ns_to)
818 mask = spu.dwChannelOn & 0xffffff;
819 for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all...
821 if (!(mask & 1)) continue; // channel not playing? next
824 sinc = s_chan[ch].sinc;
826 if (s_chan[ch].bNoise)
827 d = do_samples_noise(ch, ns_to);
828 else if (s_chan[ch].bFMod == 2
829 || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0))
830 d = do_samples_noint(decode_block, ch, ns_to,
831 SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos);
832 else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1)
833 d = do_samples_simple(decode_block, ch, ns_to,
834 SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos);
836 d = do_samples_default(decode_block, ch, ns_to,
837 SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos);
839 d = MixADSR(&s_chan[ch].ADSRX, d);
841 spu.dwChannelOn &= ~(1 << ch);
842 s_chan[ch].ADSRX.EnvelopeVol = 0;
843 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
846 if (ch == 1 || ch == 3)
848 do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos);
849 spu.decode_dirty_ch |= 1 << ch;
852 if (s_chan[ch].bFMod == 2) // fmod freq channel
853 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
854 if (s_chan[ch].bRVBActive)
855 mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, spu.sRVBStart);
857 mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
861 static void do_samples_finish(int ns_to, int silentch, int decode_pos);
863 // optional worker thread handling
865 #ifdef THREAD_ENABLED
867 static void queue_channel_work(int ns_to, int silentch)
872 worker->ns_to = ns_to;
873 worker->ctrl = spu.spuCtrl;
874 worker->decode_pos = spu.decode_pos;
875 worker->silentch = silentch;
876 worker->sRVBStart = spu.sRVBStart;
877 worker->ram = spu.spuMemC;
879 mask = worker->chmask = spu.dwChannelOn & 0xffffff;
880 for (ch = 0; mask != 0; ch++, mask >>= 1)
882 if (!(mask & 1)) continue;
884 worker->ch[ch].spos = s_chan[ch].spos;
885 worker->ch[ch].sbpos = s_chan[ch].iSBPos;
886 worker->ch[ch].sinc = s_chan[ch].sinc;
887 worker->ch[ch].adsr = s_chan[ch].ADSRX;
888 worker->ch[ch].start = s_chan[ch].pCurr - spu.spuMemC;
889 worker->ch[ch].loop = s_chan[ch].pLoop - spu.spuMemC;
890 if (s_chan[ch].prevflags & 1)
891 worker->ch[ch].start = worker->ch[ch].loop;
893 worker->ch[ch].ns_to = do_samples_skip(ch, ns_to);
897 sem_post(&worker->sem_avail);
900 static void do_channel_work(void)
902 unsigned int mask, endmask = 0;
903 unsigned int decode_dirty_ch = 0;
904 int *SB, sinc, spos, sbpos;
907 ns_to = worker->ns_to;
908 memset(worker->sRVBStart, 0, ns_to * sizeof(worker->sRVBStart[0]) * 2);
910 mask = worker->chmask;
911 for (ch = 0; mask != 0; ch++, mask >>= 1)
913 if (!(mask & 1)) continue;
915 d = worker->ch[ch].ns_to;
916 spos = worker->ch[ch].spos;
917 sbpos = worker->ch[ch].sbpos;
918 sinc = worker->ch[ch].sinc;
921 if (s_chan[ch].bNoise)
922 do_lsfr_samples(d, worker->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal);
923 else if (s_chan[ch].bFMod == 2
924 || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0))
925 do_samples_noint(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
926 else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1)
927 do_samples_simple(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
929 do_samples_default(decode_block_work, ch, d, SB, sinc, &spos, &sbpos);
931 d = MixADSR(&worker->ch[ch].adsr, d);
934 worker->ch[ch].adsr.EnvelopeVol = 0;
935 memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0]));
938 if (ch == 1 || ch == 3)
940 do_decode_bufs((void *)worker->ram, ch/2, ns_to, worker->decode_pos);
941 decode_dirty_ch |= 1 << ch;
944 if (s_chan[ch].bFMod == 2) // fmod freq channel
945 memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
946 if (s_chan[ch].bRVBActive)
947 mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, worker->sRVBStart);
949 mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume);
952 worker->r_chan_end = endmask;
953 worker->r_decode_dirty = decode_dirty_ch;
956 static void sync_worker_thread(void)
961 if (!worker->pending)
964 sem_wait(&worker->sem_done);
967 mask = worker->chmask;
968 for (ch = 0; mask != 0; ch++, mask >>= 1) {
969 if (!(mask & 1)) continue;
971 // be sure there was no keyoff while thread was working
972 if (s_chan[ch].ADSRX.State != ADSR_RELEASE)
973 s_chan[ch].ADSRX.State = worker->ch[ch].adsr.State;
974 s_chan[ch].ADSRX.EnvelopeVol = worker->ch[ch].adsr.EnvelopeVol;
977 spu.dwChannelOn &= ~worker->r_chan_end;
978 spu.decode_dirty_ch |= worker->r_decode_dirty;
980 do_samples_finish(worker->ns_to, worker->silentch,
986 static void queue_channel_work(int ns_to, int silentch) {}
987 static void sync_worker_thread(void) {}
989 #endif // THREAD_ENABLED
991 ////////////////////////////////////////////////////////////////////////
993 // here is the main job handler...
994 ////////////////////////////////////////////////////////////////////////
996 void do_samples(unsigned int cycles_to, int do_sync)
1003 cycle_diff = cycles_to - spu.cycles_played;
1004 if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576)
1006 //xprintf("desync %u %d\n", cycles_to, cycle_diff);
1007 spu.cycles_played = cycles_to;
1011 if (cycle_diff < 2 * 768)
1014 ns_to = (cycle_diff / 768 + 1) & ~1;
1015 if (ns_to > NSSIZE) {
1016 // should never happen
1017 //xprintf("ns_to oflow %d %d\n", ns_to, NSSIZE);
1021 //////////////////////////////////////////////////////
1022 // special irq handling in the decode buffers (0x0000-0x1000)
1024 // the decode buffers are located in spu memory in the following way:
1025 // 0x0000-0x03ff CD audio left
1026 // 0x0400-0x07ff CD audio right
1027 // 0x0800-0x0bff Voice 1
1028 // 0x0c00-0x0fff Voice 3
1029 // and decoded data is 16 bit for one sample
1031 // even if voices 1/3 are off or no cd audio is playing, the internal
1032 // play positions will move on and wrap after 0x400 bytes.
1033 // Therefore: we just need a pointer from spumem+0 to spumem+3ff, and
1034 // increase this pointer on each sample by 2 bytes. If this pointer
1035 // (or 0x400 offsets of this pointer) hits the spuirq address, we generate
1038 if (unlikely((spu.spuCtrl & CTRL_IRQ)
1039 && spu.pSpuIrq < spu.spuMemC+0x1000))
1041 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1042 int left = (irq_pos - spu.decode_pos) & 0x1ff;
1043 if (0 < left && left <= ns_to)
1045 //xprintf("decoder irq %x\n", spu.decode_pos);
1051 sync_worker_thread();
1053 mask = spu.dwNewChannel & 0xffffff;
1054 for (ch = 0; mask != 0; ch++, mask >>= 1) {
1059 silentch = ~spu.dwChannelOn & 0xffffff;
1061 if (spu.dwChannelOn == 0) {
1063 do_samples_finish(ns_to, silentch, spu.decode_pos);
1066 if (do_sync || worker == NULL || !spu_config.iUseThread) {
1068 do_samples_finish(ns_to, silentch, spu.decode_pos);
1071 queue_channel_work(ns_to, silentch);
1075 // advance "stopped" channels that can cause irqs
1076 // (all chans are always playing on the real thing..)
1077 if (spu.spuCtrl & CTRL_IRQ)
1078 do_silent_chans(ns_to, silentch);
1080 spu.cycles_played += ns_to * 768;
1081 spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff;
1084 static void do_samples_finish(int ns_to, int silentch, int decode_pos)
1086 int volmult = spu_config.iVolume;
1090 if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers
1092 memset(&spu.spuMem[0x800/2], 0, 0x400);
1093 spu.decode_dirty_ch &= ~(1<<1);
1095 if(unlikely(silentch & spu.decode_dirty_ch & (1<<3)))
1097 memset(&spu.spuMem[0xc00/2], 0, 0x400);
1098 spu.decode_dirty_ch &= ~(1<<3);
1101 //---------------------------------------------------//
1102 // mix XA infos (if any)
1104 MixXA(ns_to, decode_pos);
1106 ///////////////////////////////////////////////////////
1107 // mix all channels (including reverb) into one buffer
1109 if(spu_config.iUseReverb)
1112 if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this)
1114 memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0]));
1115 spu.pS += ns_to * 2;
1118 for (ns = 0; ns < ns_to * 2; )
1120 d = SSumLR[ns]; SSumLR[ns] = 0;
1121 d = d * volmult >> 10;
1126 d = SSumLR[ns]; SSumLR[ns] = 0;
1127 d = d * volmult >> 10;
1134 void schedule_next_irq(void)
1136 unsigned int upd_samples;
1139 if (spu.scheduleCallback == NULL)
1142 upd_samples = 44100 / 50;
1144 for (ch = 0; ch < MAXCHAN; ch++)
1146 if (spu.dwChannelDead & (1 << ch))
1148 if ((unsigned long)(spu.pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16
1149 && (unsigned long)(spu.pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16)
1152 scan_for_irq(ch, &upd_samples);
1155 if (unlikely(spu.pSpuIrq < spu.spuMemC + 0x1000))
1157 int irq_pos = (spu.pSpuIrq - spu.spuMemC) / 2 & 0x1ff;
1158 int left = (irq_pos - spu.decode_pos) & 0x1ff;
1159 if (0 < left && left < upd_samples) {
1160 //xprintf("decode: %3d (%3d/%3d)\n", left, spu.decode_pos, irq_pos);
1165 if (upd_samples < 44100 / 50)
1166 spu.scheduleCallback(upd_samples * 768);
1169 // SPU ASYNC... even newer epsxe func
1170 // 1 time every 'cycle' cycles... harhar
1172 // rearmed: called dynamically now
1174 void CALLBACK SPUasync(unsigned int cycle, unsigned int flags)
1176 do_samples(cycle, 0);
1178 if (spu.spuCtrl & CTRL_IRQ)
1179 schedule_next_irq();
1182 out_current->feed(spu.pSpuBuffer, (unsigned char *)spu.pS - spu.pSpuBuffer);
1183 spu.pS = (short *)spu.pSpuBuffer;
1185 if (spu_config.iTempo) {
1186 if (!out_current->busy())
1187 // cause more samples to be generated
1188 // (and break some games because of bad sync)
1189 spu.cycles_played -= 44100 / 60 / 2 * 768;
1194 // SPU UPDATE... new epsxe func
1195 // 1 time every 32 hsync lines
1196 // (312/32)x50 in pal
1197 // (262/32)x60 in ntsc
1199 // since epsxe 1.5.2 (linux) uses SPUupdate, not SPUasync, I will
1200 // leave that func in the linux port, until epsxe linux is using
1201 // the async function as well
1203 void CALLBACK SPUupdate(void)
1209 void CALLBACK SPUplayADPCMchannel(xa_decode_t *xap)
1212 if(!xap->freq) return; // no xa freq ? bye
1214 FeedXA(xap); // call main XA feeder
1218 int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes)
1220 if (!pcm) return -1;
1221 if (nbytes<=0) return -1;
1223 return FeedCDDA((unsigned char *)pcm, nbytes);
1226 // to be called after state load
1227 void ClearWorkingState(void)
1229 memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers
1230 memset(iFMod,0,sizeof(iFMod));
1231 spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer
1234 // SETUPSTREAMS: init most of the spu buffers
1235 void SetupStreams(void)
1239 spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer
1240 spu.sRVBStart = (int *)malloc(NSSIZE*2*4); // alloc reverb buffer
1241 memset(spu.sRVBStart,0,NSSIZE*2*4);
1243 spu.XAStart = // alloc xa buffer
1244 (uint32_t *)malloc(44100 * sizeof(uint32_t));
1245 spu.XAEnd = spu.XAStart + 44100;
1246 spu.XAPlay = spu.XAStart;
1247 spu.XAFeed = spu.XAStart;
1249 spu.CDDAStart = // alloc cdda buffer
1250 (uint32_t *)malloc(CDDA_BUFFER_SIZE);
1251 spu.CDDAEnd = spu.CDDAStart + 16384;
1252 spu.CDDAPlay = spu.CDDAStart;
1253 spu.CDDAFeed = spu.CDDAStart;
1255 for(i=0;i<MAXCHAN;i++) // loop sound channels
1257 s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain
1258 s_chan[i].ADSRX.SustainIncrease = 1;
1259 s_chan[i].pLoop=spu.spuMemC;
1260 s_chan[i].pCurr=spu.spuMemC;
1263 ClearWorkingState();
1265 spu.bSpuInit=1; // flag: we are inited
1268 // REMOVESTREAMS: free most buffer
1269 void RemoveStreams(void)
1271 free(spu.pSpuBuffer); // free mixing buffer
1272 spu.pSpuBuffer = NULL;
1273 free(spu.sRVBStart); // free reverb buffer
1274 spu.sRVBStart = NULL;
1275 free(spu.XAStart); // free XA buffer
1277 free(spu.CDDAStart); // free CDDA buffer
1278 spu.CDDAStart = NULL;
1281 #ifdef THREAD_ENABLED
1283 static void *spu_worker_thread(void *unused)
1286 sem_wait(&worker->sem_avail);
1287 if (worker->exit_thread)
1292 sem_post(&worker->sem_done);
1298 static void init_spu_thread(void)
1302 if (sysconf(_SC_NPROCESSORS_ONLN) <= 1)
1305 worker = calloc(1, sizeof(*worker));
1308 ret = sem_init(&worker->sem_avail, 0, 0);
1310 goto fail_sem_avail;
1311 ret = sem_init(&worker->sem_done, 0, 0);
1315 ret = pthread_create(&worker->thread, NULL, spu_worker_thread, NULL);
1322 sem_destroy(&worker->sem_done);
1324 sem_destroy(&worker->sem_avail);
1330 static void exit_spu_thread(void)
1334 worker->exit_thread = 1;
1335 sem_post(&worker->sem_avail);
1336 pthread_join(worker->thread, NULL);
1337 sem_destroy(&worker->sem_done);
1338 sem_destroy(&worker->sem_avail);
1343 #else // if !THREAD_ENABLED
1345 static void init_spu_thread(void)
1349 static void exit_spu_thread(void)
1355 // SPUINIT: this func will be called first by the main emu
1356 long CALLBACK SPUinit(void)
1358 spu.spuMemC = (unsigned char *)spu.spuMem; // just small setup
1359 memset((void *)&rvb, 0, sizeof(REVERBInfo));
1362 spu.spuAddr = 0xffffffff;
1364 memset((void *)s_chan, 0, sizeof(s_chan));
1365 spu.pSpuIrq = spu.spuMemC;
1367 SetupStreams(); // prepare streaming
1369 if (spu_config.iVolume == 0)
1370 spu_config.iVolume = 768; // 1024 is 1.0
1377 // SPUOPEN: called by main emu after init
1378 long CALLBACK SPUopen(void)
1380 if (spu.bSPUIsOpen) return 0; // security for some stupid main emus
1382 SetupSound(); // setup sound (before init!)
1386 return PSE_SPU_ERR_SUCCESS;
1389 // SPUCLOSE: called before shutdown
1390 long CALLBACK SPUclose(void)
1392 if (!spu.bSPUIsOpen) return 0; // some security
1394 spu.bSPUIsOpen = 0; // no more open
1396 out_current->finish(); // no more sound handling
1401 // SPUSHUTDOWN: called by main emu on final exit
1402 long CALLBACK SPUshutdown(void)
1405 RemoveStreams(); // no more streaming
1413 // SPUTEST: we don't test, we are always fine ;)
1414 long CALLBACK SPUtest(void)
1419 // SPUCONFIGURE: call config dialog
1420 long CALLBACK SPUconfigure(void)
1425 // StartCfgTool("CFG");
1430 // SPUABOUT: show about window
1431 void CALLBACK SPUabout(void)
1436 // StartCfgTool("ABOUT");
1441 // this functions will be called once,
1442 // passes a callback that should be called on SPU-IRQ/cdda volume change
1443 void CALLBACK SPUregisterCallback(void (CALLBACK *callback)(void))
1445 spu.irqCallback = callback;
1448 void CALLBACK SPUregisterCDDAVolume(void (CALLBACK *CDDAVcallback)(unsigned short,unsigned short))
1450 spu.cddavCallback = CDDAVcallback;
1453 void CALLBACK SPUregisterScheduleCb(void (CALLBACK *callback)(unsigned int))
1455 spu.scheduleCallback = callback;
1458 // COMMON PLUGIN INFO FUNCS
1460 char * CALLBACK PSEgetLibName(void)
1462 return _(libraryName);
1465 unsigned long CALLBACK PSEgetLibType(void)
1470 unsigned long CALLBACK PSEgetLibVersion(void)
1472 return (1 << 16) | (6 << 8);
1475 char * SPUgetLibInfos(void)
1477 return _(libraryInfo);
1482 void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int *noise_chans_out)
1484 int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0;
1486 for(;ch<MAXCHAN;ch++)
1488 if (!(spu.dwChannelOn & (1<<ch)))
1490 if (s_chan[ch].bFMod == 2)
1491 fmod_chans |= 1 << ch;
1492 if (s_chan[ch].bNoise)
1493 noise_chans |= 1 << ch;
1494 if((spu.spuCtrl&CTRL_IRQ) && s_chan[ch].pCurr <= spu.pSpuIrq && s_chan[ch].pLoop <= spu.pSpuIrq)
1495 irq_chans |= 1 << ch;
1498 *chans_out = spu.dwChannelOn;
1499 *run_chans = ~spu.dwChannelOn & ~spu.dwChannelDead & irq_chans;
1500 *fmod_chans_out = fmod_chans;
1501 *noise_chans_out = noise_chans;
1504 // vim:shiftwidth=1:expandtab