From 003cfc63c69b3e900de768317354dfb80bbf6a55 Mon Sep 17 00:00:00 2001 From: notaz Date: Sat, 3 Jan 2015 02:22:14 +0200 Subject: mingw build fixes --- plugins/cdrcimg/cdrcimg.c | 10 +++++++++- plugins/dfinput/main.c | 15 +++++++++++---- plugins/dfsound/spu.c | 13 +++---------- plugins/dfsound/stdafx.h | 20 +++++++------------- plugins/dfsound/xa.c | 4 ++++ 5 files changed, 34 insertions(+), 28 deletions(-) (limited to 'plugins') diff --git a/plugins/cdrcimg/cdrcimg.c b/plugins/cdrcimg/cdrcimg.c index 0ec14db..76cdfba 100644 --- a/plugins/cdrcimg/cdrcimg.c +++ b/plugins/cdrcimg/cdrcimg.c @@ -12,7 +12,13 @@ #include #include #include +#ifndef _WIN32 +#define CALLBACK #include +#else +#define WIN32_LEAN_AND_MEAN +#include +#endif #include "cdrcimg.h" @@ -44,7 +50,7 @@ static struct { static int current_block, current_sect_in_blk; struct CdrStat; -extern long CDR__getStatus(struct CdrStat *stat); +extern long CALLBACK CDR__getStatus(struct CdrStat *stat); struct CdrStat { @@ -279,6 +285,7 @@ static long CDRinit(void) return -1; } } +#ifndef _WIN32 if (pBZ2_bzBuffToBuffDecompress == NULL) { void *h = dlopen("/usr/lib/libbz2.so.1", RTLD_LAZY); if (h == NULL) @@ -291,6 +298,7 @@ static long CDRinit(void) } } } +#endif return 0; } diff --git a/plugins/dfinput/main.c b/plugins/dfinput/main.c index 73b2bda..475ea07 100644 --- a/plugins/dfinput/main.c +++ b/plugins/dfinput/main.c @@ -8,6 +8,13 @@ * See the COPYING file in the top-level directory. */ +#ifndef _WIN32 +#define CALLBACK +#else +#define WIN32_LEAN_AND_MEAN +#include +#endif + #include "main.h" unsigned char CurPad, CurByte, CurCmd, CmdLen; @@ -15,10 +22,10 @@ unsigned char CurPad, CurByte, CurCmd, CmdLen; /* since this is not a proper plugin, so we'll hook emu internals in a hackish way like this */ extern void *PAD1_startPoll, *PAD1_poll; extern void *PAD2_startPoll, *PAD2_poll; -extern unsigned char PAD1__startPoll(int pad); -extern unsigned char PAD2__startPoll(int pad); -extern unsigned char PAD1__poll(unsigned char value); -extern unsigned char PAD2__poll(unsigned char value); +extern unsigned char CALLBACK PAD1__startPoll(int pad); +extern unsigned char CALLBACK PAD2__startPoll(int pad); +extern unsigned char CALLBACK PAD1__poll(unsigned char value); +extern unsigned char CALLBACK PAD2__poll(unsigned char value); static int old_controller_type1 = -1, old_controller_type2 = -1; diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c index 835555d..d39bbc1 100644 --- a/plugins/dfsound/spu.c +++ b/plugins/dfsound/spu.c @@ -18,6 +18,9 @@ * * ***************************************************************************/ +#ifndef _WIN32 +#include // gettimeofday in xa.c +#endif #include "stdafx.h" #define _IN_SPU @@ -28,16 +31,6 @@ #include "arm_features.h" #include "spu_config.h" -#ifdef ENABLE_NLS -#include -#include -#define _(x) gettext(x) -#define N_(x) (x) -#else -#define _(x) (x) -#define N_(x) (x) -#endif - #ifdef __ARM_ARCH_7A__ #define ssat32_to_16(v) \ asm("ssat %0,#16,%1" : "=r" (v) : "r" (v)) diff --git a/plugins/dfsound/stdafx.h b/plugins/dfsound/stdafx.h index 8993bb3..7e22029 100644 --- a/plugins/dfsound/stdafx.h +++ b/plugins/dfsound/stdafx.h @@ -15,26 +15,20 @@ * * ***************************************************************************/ -#ifndef _MACOSX -#include "config.h" -#endif #include #include -#include -#include -#include -#include -#include -#define RRand(range) (random()%range) #include -#include -#include +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#else #undef CALLBACK #define CALLBACK #define DWORD unsigned int -#define LOWORD(l) ((unsigned short)(l)) -#define HIWORD(l) ((unsigned short)(((unsigned int)(l) >> 16) & 0xFFFF)) +#define LOWORD(l) ((unsigned short)(l)) +#define HIWORD(l) ((unsigned short)(((unsigned int)(l) >> 16) & 0xFFFF)) +#endif #ifndef INLINE #define INLINE static inline diff --git a/plugins/dfsound/xa.c b/plugins/dfsound/xa.c index 704847b..9b7ed5a 100644 --- a/plugins/dfsound/xa.c +++ b/plugins/dfsound/xa.c @@ -90,9 +90,13 @@ INLINE void MixXA(int ns_to) static unsigned long timeGetTime_spu() { +#ifdef _WIN32 + return GetTickCount(); +#else struct timeval tv; gettimeofday(&tv, 0); // well, maybe there are better ways return tv.tv_sec * 1000 + tv.tv_usec/1000; // to do that, but at least it works +#endif } //////////////////////////////////////////////////////////////////////// -- cgit v1.2.3 From 215ff9e69c0b845f24e7a3aa9faeef06d9276145 Mon Sep 17 00:00:00 2001 From: notaz Date: Sat, 27 Dec 2014 23:24:15 +0200 Subject: spu: handle stop better, split main func more --- plugins/dfsound/adsr.c | 22 ++-- plugins/dfsound/spu.c | 345 +++++++++++++++++++++++++++++-------------------- plugins/dfsound/xa.c | 4 +- 3 files changed, 216 insertions(+), 155 deletions(-) (limited to 'plugins') diff --git a/plugins/dfsound/adsr.c b/plugins/dfsound/adsr.c index c1e98b3..976315d 100644 --- a/plugins/dfsound/adsr.c +++ b/plugins/dfsound/adsr.c @@ -63,10 +63,10 @@ INLINE void StartADSR(int ch) // MIX ADSR //////////////////////////////////////////////////////////////////////// -static void MixADSR(int ch, int ns, int ns_to) // MIX ADSR +static int MixADSR(int ch, int ns_to) // MIX ADSR { int EnvelopeVol = s_chan[ch].ADSRX.EnvelopeVol; - int val, rto, level; + int ns = 0, val, rto, level; if (s_chan[ch].bStop) // should be stopped: { // do release @@ -97,9 +97,6 @@ static void MixADSR(int ch, int ns, int ns_to) // MIX ADSR } } - if (EnvelopeVol <= 0) - goto stop; - goto done; } @@ -160,7 +157,10 @@ static void MixADSR(int ch, int ns, int ns_to) // MIX ADSR if (s_chan[ch].ADSRX.SustainIncrease) { if (EnvelopeVol >= 0x7fff0000) + { + ns = ns_to; break; + } rto = 0; if (s_chan[ch].ADSRX.SustainModeExp && EnvelopeVol >= 0x60000000) @@ -173,6 +173,7 @@ static void MixADSR(int ch, int ns, int ns_to) // MIX ADSR if ((unsigned int)EnvelopeVol >= 0x7fe00000) { EnvelopeVol = 0x7fffffff; + ns = ns_to; break; } @@ -189,7 +190,7 @@ static void MixADSR(int ch, int ns, int ns_to) // MIX ADSR { EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); if (EnvelopeVol < 0) - goto stop; + break; ChanBuf[ns] *= EnvelopeVol >> 21; ChanBuf[ns] >>= 10; @@ -201,7 +202,7 @@ static void MixADSR(int ch, int ns, int ns_to) // MIX ADSR { EnvelopeVol += val; if (EnvelopeVol < 0) - goto stop; + break; ChanBuf[ns] *= EnvelopeVol >> 21; ChanBuf[ns] >>= 10; @@ -213,12 +214,7 @@ static void MixADSR(int ch, int ns, int ns_to) // MIX ADSR done: s_chan[ch].ADSRX.EnvelopeVol = EnvelopeVol; - return; - -stop: - memset(&ChanBuf[ns], 0, (ns_to - ns) * sizeof(ChanBuf[0])); - s_chan[ch].ADSRX.EnvelopeVol = 0; - spu.dwChannelOn &= ~(1< turn everything off - s_chan[ch].bStop = 1; - s_chan[ch].ADSRX.EnvelopeVol = 0; - } - - predict_nr=(int)start[0]; - shift_factor=predict_nr&0xf; + predict_nr = (int)start[0]; + shift_factor = predict_nr & 0xf; predict_nr >>= 4; decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor); - flags=(int)start[1]; - if(flags&4) - s_chan[ch].pLoop=start; // loop adress + flags = start[1]; + if (flags & 4) + s_chan[ch].pLoop = start; // loop adress - start+=16; + start += 16; - if(flags&1) { // 1: stop/loop + if (flags & 1) { // 1: stop/loop start = s_chan[ch].pLoop; - ret |= check_irq(ch, start); // hack.. :( + check_irq(ch, start); // hack.. :( } if (start - spu.spuMemC >= 0x80000) @@ -455,19 +447,28 @@ static int decode_block(int ch) static int skip_block(int ch) { unsigned char *start = s_chan[ch].pCurr; - int flags = start[1]; - int ret = check_irq(ch, start); + int flags; + int ret = 0; + + if (s_chan[ch].prevflags & 1) { + if (!(s_chan[ch].prevflags & 2)) + ret = 1; - if(s_chan[ch].prevflags & 1) start = s_chan[ch].pLoop; + } + else + check_irq(ch, start); - if(flags & 4) + flags = start[1]; + if (flags & 4) s_chan[ch].pLoop = start; start += 16; - if(flags & 1) + if (flags & 1) { start = s_chan[ch].pLoop; + check_irq(ch, start); + } s_chan[ch].pCurr = start; s_chan[ch].prevflags = flags; @@ -516,17 +517,17 @@ static void scan_for_irq(int ch, unsigned int *upd_samples) } #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \ -static noinline int do_samples_##name(int ch, int ns, int ns_to) \ +static noinline int do_samples_##name(int ch, int ns_to) \ { \ int sinc = s_chan[ch].sinc; \ int spos = s_chan[ch].spos; \ int sbpos = s_chan[ch].iSBPos; \ int *SB = s_chan[ch].SB; \ - int ret = -1; \ - int d, fa; \ + int ns, d, fa; \ + int ret = ns_to; \ interp_start; \ \ - for (; ns < ns_to; ns++) \ + for (ns = 0; ns < ns_to; ns++) \ { \ fmod_code; \ \ @@ -534,12 +535,12 @@ static noinline int do_samples_##name(int ch, int ns, int ns_to) \ while (spos >= 0x10000) \ { \ fa = SB[sbpos++]; \ - if(sbpos >= 28) \ + if (sbpos >= 28) \ { \ sbpos = 0; \ d = decode_block(ch); \ - if(d) \ - ret = /*ns_to =*/ ns + 1; \ + if (d && ns < ret) \ + ret = ns; \ } \ \ interp1_code; \ @@ -567,11 +568,11 @@ make_do_samples(default, fmod_recv_check, , make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa) #define simple_interp_store \ - s_chan[ch].SB[28] = 0; \ - s_chan[ch].SB[29] = s_chan[ch].SB[30]; \ - s_chan[ch].SB[30] = s_chan[ch].SB[31]; \ - s_chan[ch].SB[31] = fa; \ - s_chan[ch].SB[32] = 1 + SB[28] = 0; \ + SB[29] = SB[30]; \ + SB[30] = SB[31]; \ + SB[31] = fa; \ + SB[32] = 1 #define simple_interp_get \ if(sinc<0x10000) /* -> upsampling? */ \ @@ -582,40 +583,69 @@ make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch]. make_do_samples(simple, , , simple_interp_store, simple_interp_get, ) -static noinline int do_samples_noise(int ch, int ns, int ns_to) +static int do_samples_skip(int ch, int ns_to) { - int level, shift, bit; - int ret = -1, d; + int ret = ns_to, ns, d; - s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns); - while (s_chan[ch].spos >= 28*0x10000) + s_chan[ch].spos += s_chan[ch].iSBPos << 16; + + for (ns = 0; ns < ns_to; ns++) { - d = skip_block(ch); - if (d) - ret = ns_to; - s_chan[ch].spos -= 28*0x10000; + s_chan[ch].spos += s_chan[ch].sinc; + while (s_chan[ch].spos >= 28*0x10000) + { + d = skip_block(ch); + if (d && ns < ret) + ret = ns; + s_chan[ch].spos -= 28*0x10000; + } } + s_chan[ch].iSBPos = s_chan[ch].spos >> 16; + s_chan[ch].spos &= 0xffff; + + return ret; +} + +static void do_lsfr_samples(int ns_to, int ctrl, + unsigned int *dwNoiseCount, unsigned int *dwNoiseVal) +{ + unsigned int counter = *dwNoiseCount; + unsigned int val = *dwNoiseVal; + unsigned int level, shift, bit; + int ns; + // modified from DrHell/shalma, no fraction - level = (spu.spuCtrl >> 10) & 0x0f; + level = (ctrl >> 10) & 0x0f; level = 0x8000 >> level; - for (; ns < ns_to; ns++) + for (ns = 0; ns < ns_to; ns++) { - spu.dwNoiseCount += 2; - if (spu.dwNoiseCount >= level) + counter += 2; + if (counter >= level) { - spu.dwNoiseCount -= level; - shift = (spu.dwNoiseVal >> 10) & 0x1f; + counter -= level; + shift = (val >> 10) & 0x1f; bit = (0x69696969 >> shift) & 1; - if (spu.dwNoiseVal & 0x8000) - bit ^= 1; - spu.dwNoiseVal = (spu.dwNoiseVal << 1) | bit; + bit ^= (val >> 15) & 1; + val = (val << 1) | bit; } - ChanBuf[ns] = (signed short)spu.dwNoiseVal; + ChanBuf[ns] = (signed short)val; } + *dwNoiseCount = counter; + *dwNoiseVal = val; +} + +static int do_samples_noise(int ch, int ns_to) +{ + int ret; + + ret = do_samples_skip(ch, ns_to); + + do_lsfr_samples(ns_to, spu.spuCtrl, &spu.dwNoiseCount, &spu.dwNoiseVal); + return ret; } @@ -664,11 +694,12 @@ static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb) // 0x0800-0x0bff Voice 1 // 0x0c00-0x0fff Voice 3 -static noinline void do_decode_bufs(int which, int start, int count) +static noinline void do_decode_bufs(unsigned short *mem, int which, + int count, int decode_pos) { - const int *src = ChanBuf + start; - unsigned short *dst = &spu.spuMem[0x800/2 + which*0x400/2]; - int cursor = spu.decode_pos + start; + unsigned short *dst = &mem[0x800/2 + which*0x400/2]; + const int *src = ChanBuf; + int cursor = decode_pos; while (count-- > 0) { @@ -680,18 +711,96 @@ static noinline void do_decode_bufs(int which, int start, int count) // decode_pos is updated and irqs are checked later, after voice loop } +static void do_silent_chans(int ns_to, int silentch) +{ + int ch; + + for (ch = 0; ch < MAXCHAN; ch++) + { + if (!(silentch & (1< spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq) + continue; + + s_chan[ch].spos += s_chan[ch].iSBPos << 16; + s_chan[ch].iSBPos = 0; + + s_chan[ch].spos += s_chan[ch].sinc * ns_to; + while (s_chan[ch].spos >= 28 * 0x10000) + { + unsigned char *start = s_chan[ch].pCurr; + + skip_block(ch); + if (start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000) + { + // looping on self or stopped(?) + spu.dwChannelDead |= 1<>= 1) // loop em all... + { + if (!(mask & 1)) continue; // channel not playing? next + + if (s_chan[ch].bNoise) + d = do_samples_noise(ch, ns_to); + else if (s_chan[ch].bFMod == 2 + || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0)) + d = do_samples_noint(ch, ns_to); + else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1) + d = do_samples_simple(ch, ns_to); + else + d = do_samples_default(ch, ns_to); + + d = MixADSR(ch, d); + if (d < ns_to) { + spu.dwChannelOn &= ~(1 << ch); + s_chan[ch].bStop = 1; + s_chan[ch].ADSRX.EnvelopeVol = 0; + memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0])); + } + + if (ch == 1 || ch == 3) + { + do_decode_bufs(spu.spuMem, ch/2, ns_to, spu.decode_pos); + spu.decode_dirty_ch |= 1 << ch; + } + + if (s_chan[ch].bFMod == 2) // fmod freq channel + memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); + if (s_chan[ch].bRVBActive) + mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, spu.sRVBStart); + else + mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume); + } +} + //////////////////////////////////////////////////////////////////////// // MAIN SPU FUNCTION // here is the main job handler... -// basically the whole sound processing is done in this fat func! //////////////////////////////////////////////////////////////////////// +void do_samples_finish(int ns_to, int silentch); + void do_samples(unsigned int cycles_to) { - const int ns_from = 0; - int ns,ns_to,ns_len; - int volmult = spu_config.iVolume; - int ch,d,silentch; + unsigned int mask; + int ch, ns_to; + int silentch; int cycle_diff; cycle_diff = cycles_to - spu.cycles_played; @@ -741,75 +850,36 @@ void do_samples(unsigned int cycles_to) } } - InitREVERB(ns_to); + silentch = ~(spu.dwChannelOn|spu.dwNewChannel); - { - silentch=~(spu.dwChannelOn|spu.dwNewChannel); + mask = spu.dwNewChannel & 0xffffff; + for (ch = 0; mask != 0; ch++, mask >>= 1) { + if (mask & 1) + StartSound(ch); + } - //--------------------------------------------------// - //- main channel loop -// - //--------------------------------------------------// - { - for(ch=0;ch spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq) - continue; - - s_chan[ch].spos += s_chan[ch].sinc * (ns_to - ns_from); - while(s_chan[ch].spos >= 28 * 0x10000) - { - unsigned char *start = s_chan[ch].pCurr; - - skip_block(ch); - if(start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000) - { - // looping on self or stopped(?) - spu.dwChannelDead |= 1< 0) { -- cgit v1.2.3 From 011755d7a88f7128d04a14136e9fd61870706dcf Mon Sep 17 00:00:00 2001 From: notaz Date: Wed, 31 Dec 2014 02:15:08 +0200 Subject: spu: reduce use of globals to be used by upcoming threaded code.. --- plugins/dfsound/spu.c | 127 +++++++++++++++++++++++++------------------------- 1 file changed, 63 insertions(+), 64 deletions(-) (limited to 'plugins') diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c index 07bc4b3..1d5e28f 100644 --- a/plugins/dfsound/spu.c +++ b/plugins/dfsound/spu.c @@ -138,71 +138,70 @@ int iFMod[NSSIZE]; // / // - -INLINE void InterpolateUp(int ch) +static void InterpolateUp(int *SB, int sinc) { - if(s_chan[ch].SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass + if(SB[32]==1) // flag == 1? calc step and set flag... and don't change the value in this pass { - const int id1=s_chan[ch].SB[30]-s_chan[ch].SB[29]; // curr delta to next val - const int id2=s_chan[ch].SB[31]-s_chan[ch].SB[30]; // and next delta to next-next val :) + const int id1=SB[30]-SB[29]; // curr delta to next val + const int id2=SB[31]-SB[30]; // and next delta to next-next val :) - s_chan[ch].SB[32]=0; + SB[32]=0; if(id1>0) // curr delta positive { if(id2>16; else - s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L; + SB[28]=(id1*sinc)>>17; } else // curr delta negative { if(id2>id1) - {s_chan[ch].SB[28]=id1;s_chan[ch].SB[32]=2;} + {SB[28]=id1;SB[32]=2;} else if(id2>(id1<<1)) - s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x10000L; + SB[28]=(id1*sinc)>>16; else - s_chan[ch].SB[28]=(id1*s_chan[ch].sinc)/0x20000L; + SB[28]=(id1*sinc)>>17; } } else - if(s_chan[ch].SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass + if(SB[32]==2) // flag 1: calc step and set flag... and don't change the value in this pass { - s_chan[ch].SB[32]=0; + SB[32]=0; - s_chan[ch].SB[28]=(s_chan[ch].SB[28]*s_chan[ch].sinc)/0x20000L; - //if(s_chan[ch].sinc<=0x8000) - // s_chan[ch].SB[29]=s_chan[ch].SB[30]-(s_chan[ch].SB[28]*((0x10000/s_chan[ch].sinc)-1)); + SB[28]=(SB[28]*sinc)>>17; + //if(sinc<=0x8000) + // SB[29]=SB[30]-(SB[28]*((0x10000/sinc)-1)); //else - s_chan[ch].SB[29]+=s_chan[ch].SB[28]; + SB[29]+=SB[28]; } else // no flags? add bigger val (if possible), calc smaller step, set flag1 - s_chan[ch].SB[29]+=s_chan[ch].SB[28]; + SB[29]+=SB[28]; } // // even easier interpolation on downsampling, also no special filter, again just "Pete's common sense" tm // -INLINE void InterpolateDown(int ch) +static void InterpolateDown(int *SB, int sinc) { - if(s_chan[ch].sinc>=0x20000L) // we would skip at least one val? + if(sinc>=0x20000L) // we would skip at least one val? { - s_chan[ch].SB[29]+=(s_chan[ch].SB[30]-s_chan[ch].SB[29])/2; // add easy weight - if(s_chan[ch].sinc>=0x30000L) // we would skip even more vals? - s_chan[ch].SB[29]+=(s_chan[ch].SB[31]-s_chan[ch].SB[30])/2;// add additional next weight + SB[29]+=(SB[30]-SB[29])/2; // add easy weight + if(sinc>=0x30000L) // we would skip even more vals? + SB[29]+=(SB[31]-SB[30])/2; // add additional next weight } } //////////////////////////////////////////////////////////////////////// // helpers for gauss interpolation -#define gval0 (((short*)(&s_chan[ch].SB[29]))[gpos&3]) -#define gval(x) ((int)((short*)(&s_chan[ch].SB[29]))[(gpos+x)&3]) +#define gval0 (((short*)(&SB[29]))[gpos&3]) +#define gval(x) ((int)((short*)(&SB[29]))[(gpos+x)&3]) #include "gauss_i.h" @@ -261,19 +260,19 @@ INLINE void StartSound(int ch) // ALL KIND OF HELPERS //////////////////////////////////////////////////////////////////////// -INLINE int FModChangeFrequency(int ch,int ns) +INLINE int FModChangeFrequency(int *SB, int pitch, int ns) { - unsigned int NP=s_chan[ch].iRawPitch; + unsigned int NP=pitch; int sinc; - NP=((32768L+iFMod[ns])*NP)/32768L; + NP=((32768L+iFMod[ns])*NP)>>15; if(NP>0x3fff) NP=0x3fff; if(NP<0x1) NP=0x1; sinc=NP<<4; // calc frequency if(spu_config.iUseInterpolation==1) // freq change in simple interpolation mode - s_chan[ch].SB[32]=1; + SB[32]=1; iFMod[ns]=0; return sinc; @@ -281,50 +280,50 @@ INLINE int FModChangeFrequency(int ch,int ns) //////////////////////////////////////////////////////////////////////// -INLINE void StoreInterpolationVal(int ch,int fa) +INLINE void StoreInterpolationVal(int *SB, int sinc, int fa, int fmod_freq) { - if(s_chan[ch].bFMod==2) // fmod freq channel - s_chan[ch].SB[29]=fa; + if(fmod_freq) // fmod freq channel + SB[29]=fa; else { ssat32_to_16(fa); if(spu_config.iUseInterpolation>=2) // gauss/cubic interpolation - { - int gpos = s_chan[ch].SB[28]; - gval0 = fa; + { + int gpos = SB[28]; + gval0 = fa; gpos = (gpos+1) & 3; - s_chan[ch].SB[28] = gpos; + SB[28] = gpos; } else if(spu_config.iUseInterpolation==1) // simple interpolation { - s_chan[ch].SB[28] = 0; - s_chan[ch].SB[29] = s_chan[ch].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' - s_chan[ch].SB[30] = s_chan[ch].SB[31]; - s_chan[ch].SB[31] = fa; - s_chan[ch].SB[32] = 1; // -> flag: calc new interolation + SB[28] = 0; + 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' + SB[30] = SB[31]; + SB[31] = fa; + SB[32] = 1; // -> flag: calc new interolation } - else s_chan[ch].SB[29]=fa; // no interpolation + else SB[29]=fa; // no interpolation } } //////////////////////////////////////////////////////////////////////// -INLINE int iGetInterpolationVal(int ch, int spos) +INLINE int iGetInterpolationVal(int *SB, int sinc, int spos, int fmod_freq) { int fa; - if(s_chan[ch].bFMod==2) return s_chan[ch].SB[29]; + if(fmod_freq) return SB[29]; switch(spu_config.iUseInterpolation) - { + { //--------------------------------------------------// case 3: // cubic interpolation { long xd;int gpos; xd = (spos >> 1)+1; - gpos = s_chan[ch].SB[28]; + gpos = SB[28]; fa = gval(3) - 3*gval(2) + 3*gval(1) - gval0; fa *= (xd - (2<<15)) / 6; @@ -343,7 +342,7 @@ INLINE int iGetInterpolationVal(int ch, int spos) { int vl, vr;int gpos; vl = (spos >> 6) & ~3; - gpos = s_chan[ch].SB[28]; + gpos = SB[28]; vr=(gauss[vl]*(int)gval0)&~2047; vr+=(gauss[vl+1]*gval(1))&~2047; vr+=(gauss[vl+2]*gval(2))&~2047; @@ -353,15 +352,15 @@ INLINE int iGetInterpolationVal(int ch, int spos) //--------------------------------------------------// case 1: // simple interpolation { - if(s_chan[ch].sinc<0x10000L) // -> upsampling? - InterpolateUp(ch); // --> interpolate up - else InterpolateDown(ch); // --> else down - fa=s_chan[ch].SB[29]; + if(sinc<0x10000L) // -> upsampling? + InterpolateUp(SB, sinc); // --> interpolate up + else InterpolateDown(SB, sinc); // --> else down + fa=SB[29]; } break; //--------------------------------------------------// default: // no interpolation { - fa=s_chan[ch].SB[29]; + fa=SB[29]; } break; //--------------------------------------------------// } @@ -397,7 +396,7 @@ static void decode_block_data(int *dest, const unsigned char *src, int predict_n } } -static int decode_block(int ch) +static int decode_block(int ch, int *SB) { unsigned char *start; int predict_nr, shift_factor, flags; @@ -421,7 +420,7 @@ static int decode_block(int ch) shift_factor = predict_nr & 0xf; predict_nr >>= 4; - decode_block_data(s_chan[ch].SB, start + 2, predict_nr, shift_factor); + decode_block_data(SB, start + 2, predict_nr, shift_factor); flags = start[1]; if (flags & 4) @@ -538,7 +537,7 @@ static noinline int do_samples_##name(int ch, int ns_to) \ if (sbpos >= 28) \ { \ sbpos = 0; \ - d = decode_block(ch); \ + d = decode_block(ch, SB); \ if (d && ns < ret) \ ret = ns; \ } \ @@ -560,12 +559,12 @@ static noinline int do_samples_##name(int ch, int ns_to) \ #define fmod_recv_check \ if(s_chan[ch].bFMod==1 && iFMod[ns]) \ - sinc = FModChangeFrequency(ch,ns) + sinc = FModChangeFrequency(SB, s_chan[ch].iRawPitch, ns) make_do_samples(default, fmod_recv_check, , - StoreInterpolationVal(ch, fa), - ChanBuf[ns] = iGetInterpolationVal(ch, spos), ) -make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch].SB[29] = fa) + StoreInterpolationVal(SB, sinc, fa, s_chan[ch].bFMod==2), + ChanBuf[ns] = iGetInterpolationVal(SB, sinc, spos, s_chan[ch].bFMod==2), ) +make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa) #define simple_interp_store \ SB[28] = 0; \ @@ -575,10 +574,10 @@ make_do_samples(noint, , fa = s_chan[ch].SB[29], , ChanBuf[ns] = fa, s_chan[ch]. SB[32] = 1 #define simple_interp_get \ - if(sinc<0x10000) /* -> upsampling? */ \ - InterpolateUp(ch); /* --> interpolate up */ \ - else InterpolateDown(ch); /* --> else down */ \ - ChanBuf[ns] = s_chan[ch].SB[29] + if(sinc<0x10000) /* -> upsampling? */ \ + InterpolateUp(SB, sinc); /* --> interpolate up */ \ + else InterpolateDown(SB, sinc); /* --> else down */ \ + ChanBuf[ns] = SB[29] make_do_samples(simple, , , simple_interp_store, simple_interp_get, ) -- cgit v1.2.3 From 4ccd0fb2874dab39c9db8ab973d4db9b8d54fa0d Mon Sep 17 00:00:00 2001 From: notaz Date: Thu, 1 Jan 2015 19:35:16 +0200 Subject: spu: move more things to StartSound --- plugins/dfsound/registers.c | 7 ------- plugins/dfsound/spu.c | 8 ++++---- 2 files changed, 4 insertions(+), 11 deletions(-) (limited to 'plugins') diff --git a/plugins/dfsound/registers.c b/plugins/dfsound/registers.c index 210adb2..8e4956e 100644 --- a/plugins/dfsound/registers.c +++ b/plugins/dfsound/registers.c @@ -362,16 +362,9 @@ static void SoundOn(int start,int end,unsigned short val) { if((val&1) && regAreaGet(ch,6)) // mmm... start has to be set before key on !?! { - // do this here, not in StartSound - // - fixes fussy timing issues - s_chan[ch].bStop=0; s_chan[ch].pCurr=spu.spuMemC+((regAreaGet(ch,6)&~1)<<3); // must be block aligned s_chan[ch].pLoop=spu.spuMemC+((regAreaGet(ch,14)&~1)<<3); - s_chan[ch].prevflags=2; - spu.dwNewChannel|=(1<EnvelopeVol; int ns = 0, val, rto, level; - if (s_chan[ch].bStop) // should be stopped: - { // do release - val = RateTableSub[s_chan[ch].ADSRX.ReleaseRate * 4]; + if (adsr->State == ADSR_RELEASE) + { + val = RateTableSub[adsr->ReleaseRate * 4]; - if (s_chan[ch].ADSRX.ReleaseModeExp) + if (adsr->ReleaseModeExp) { for (; ns < ns_to; ns++) { @@ -100,13 +100,13 @@ static int MixADSR(int ch, int ns_to) // MIX ADSR goto done; } - switch (s_chan[ch].ADSRX.State) + switch (adsr->State) { - case 0: // -> attack + case ADSR_ATTACK: // -> attack rto = 0; - if (s_chan[ch].ADSRX.AttackModeExp && EnvelopeVol >= 0x60000000) + if (adsr->AttackModeExp && EnvelopeVol >= 0x60000000) rto = 8; - val = RateTableAdd[s_chan[ch].ADSRX.AttackRate + rto]; + val = RateTableAdd[adsr->AttackRate + rto]; for (; ns < ns_to; ns++) { @@ -121,7 +121,7 @@ static int MixADSR(int ch, int ns_to) // MIX ADSR if (EnvelopeVol < 0) // overflow { EnvelopeVol = 0x7fffffff; - s_chan[ch].ADSRX.State = 1; + adsr->State = ADSR_DECAY; ns++; // sample is good already goto decay; } @@ -129,9 +129,9 @@ static int MixADSR(int ch, int ns_to) // MIX ADSR //--------------------------------------------------// decay: - case 1: // -> decay - val = RateTableSub[s_chan[ch].ADSRX.DecayRate * 4]; - level = s_chan[ch].ADSRX.SustainLevel; + case ADSR_DECAY: // -> decay + val = RateTableSub[adsr->DecayRate * 4]; + level = adsr->SustainLevel; for (; ns < ns_to; ) { @@ -145,7 +145,7 @@ static int MixADSR(int ch, int ns_to) // MIX ADSR if (((EnvelopeVol >> 27) & 0xf) <= level) { - s_chan[ch].ADSRX.State = 2; + adsr->State = ADSR_SUSTAIN; goto sustain; } } @@ -153,8 +153,8 @@ static int MixADSR(int ch, int ns_to) // MIX ADSR //--------------------------------------------------// sustain: - case 2: // -> sustain - if (s_chan[ch].ADSRX.SustainIncrease) + case ADSR_SUSTAIN: // -> sustain + if (adsr->SustainIncrease) { if (EnvelopeVol >= 0x7fff0000) { @@ -163,9 +163,9 @@ static int MixADSR(int ch, int ns_to) // MIX ADSR } rto = 0; - if (s_chan[ch].ADSRX.SustainModeExp && EnvelopeVol >= 0x60000000) + if (adsr->SustainModeExp && EnvelopeVol >= 0x60000000) rto = 8; - val = RateTableAdd[s_chan[ch].ADSRX.SustainRate + rto]; + val = RateTableAdd[adsr->SustainRate + rto]; for (; ns < ns_to; ns++) { @@ -183,8 +183,8 @@ static int MixADSR(int ch, int ns_to) // MIX ADSR } else { - val = RateTableSub[s_chan[ch].ADSRX.SustainRate]; - if (s_chan[ch].ADSRX.SustainModeExp) + val = RateTableSub[adsr->SustainRate]; + if (adsr->SustainModeExp) { for (; ns < ns_to; ns++) { @@ -213,7 +213,7 @@ static int MixADSR(int ch, int ns_to) // MIX ADSR } done: - s_chan[ch].ADSRX.EnvelopeVol = EnvelopeVol; + adsr->EnvelopeVol = EnvelopeVol; return ns; } diff --git a/plugins/dfsound/externals.h b/plugins/dfsound/externals.h index 7935cb9..d1d081a 100644 --- a/plugins/dfsound/externals.h +++ b/plugins/dfsound/externals.h @@ -53,10 +53,17 @@ // struct defines /////////////////////////////////////////////////////////// +enum ADSR_State { + ADSR_ATTACK = 0, + ADSR_DECAY = 1, + ADSR_SUSTAIN = 2, + ADSR_RELEASE = 3, +}; + // ADSR INFOS PER CHANNEL typedef struct { - unsigned char State:2; + unsigned char State:2; // ADSR_State unsigned char AttackModeExp:1; unsigned char SustainModeExp:1; unsigned char SustainIncrease:1; @@ -93,7 +100,6 @@ typedef struct unsigned char * pCurr; // current pos in sound mem unsigned char * pLoop; // loop ptr in sound mem - unsigned int bStop:1; // is channel stopped (sample _can_ still be playing, ADSR Release phase) unsigned int bReverb:1; // can we do reverb on this channel? must have ctrl register bit, to get active unsigned int bRVBActive:1; // reverb active flag unsigned int bNoise:1; // noise active flag diff --git a/plugins/dfsound/freeze.c b/plugins/dfsound/freeze.c index d38faa6..be344ce 100644 --- a/plugins/dfsound/freeze.c +++ b/plugins/dfsound/freeze.c @@ -148,7 +148,7 @@ static void save_channel(SPUCHAN_orig *d, const SPUCHAN *s, int ch) d->iCurr = 0; // set by the caller d->iLoop = 0; // set by the caller d->bOn = !!(spu.dwChannelOn & (1<bStop = s->bStop; + d->bStop = s->ADSRX.State == ADSR_RELEASE; d->bReverb = s->bReverb; d->iActFreq = 1; d->iUsedFreq = 2; @@ -188,7 +188,6 @@ static void load_channel(SPUCHAN *d, const SPUCHAN_orig *s, int ch) memcpy(d->SB, s->SB, sizeof(d->SB)); d->pCurr = (void *)((long)s->iCurr & 0x7fff0); d->pLoop = (void *)((long)s->iLoop & 0x7fff0); - d->bStop = s->bStop; d->bReverb = s->bReverb; d->iLeftVolume = s->iLeftVolume; d->iRightVolume = s->iRightVolume; @@ -198,6 +197,7 @@ static void load_channel(SPUCHAN *d, const SPUCHAN_orig *s, int ch) d->bFMod = s->bFMod; d->prevflags = (s->bIgnoreLoop >> 1) ^ 2; d->ADSRX.State = s->ADSRX.State; + if (s->bStop) d->ADSRX.State = ADSR_RELEASE; d->ADSRX.AttackModeExp = s->ADSRX.AttackModeExp; d->ADSRX.AttackRate = s->ADSRX.AttackRate; d->ADSRX.DecayRate = s->ADSRX.DecayRate; @@ -349,7 +349,6 @@ void LoadStateUnknown(SPUFreeze_t * pF, uint32_t cycles) for(i=0;i>=1) // loop channels { - if(val&1) // && s_chan[i].bOn) mmm... + if(val&1) { - s_chan[ch].bStop=1; + s_chan[ch].ADSRX.State = ADSR_RELEASE; // Jungle Book - Rhythm 'n Groove // - turns off buzzing sound (loop hangs) diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c index 147db94..66ab150 100644 --- a/plugins/dfsound/spu.c +++ b/plugins/dfsound/spu.c @@ -238,7 +238,6 @@ INLINE void StartSound(int ch) StartADSR(ch); StartREVERB(ch); - s_chan[ch].bStop=0; s_chan[ch].prevflags=2; s_chan[ch].SB[26]=0; // init mixing vars @@ -765,10 +764,9 @@ static void do_channels(int ns_to) else d = do_samples_default(ch, ns_to); - d = MixADSR(ch, d); + d = MixADSR(&s_chan[ch].ADSRX, d); if (d < ns_to) { spu.dwChannelOn &= ~(1 << ch); - s_chan[ch].bStop = 1; s_chan[ch].ADSRX.EnvelopeVol = 0; memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0])); } -- cgit v1.2.3 From 63a4f6b6a3b0315590cd3009df2c92480ed2d98b Mon Sep 17 00:00:00 2001 From: notaz Date: Fri, 2 Jan 2015 04:13:50 +0200 Subject: spu: threaded implementation unlike original P.E.Op.S. implementation, the goal here is to carefully avoid any side effects caused by having the thread enabled --- plugins/dfsound/dma.c | 4 +- plugins/dfsound/externals.h | 8 +- plugins/dfsound/freeze.c | 2 +- plugins/dfsound/registers.c | 2 +- plugins/dfsound/spu.c | 349 +++++++++++++++++++++++++++++++++++++++---- plugins/dfsound/spu_config.h | 1 + 6 files changed, 326 insertions(+), 40 deletions(-) (limited to 'plugins') diff --git a/plugins/dfsound/dma.c b/plugins/dfsound/dma.c index d3706b9..8d647ff 100644 --- a/plugins/dfsound/dma.c +++ b/plugins/dfsound/dma.c @@ -43,7 +43,7 @@ void CALLBACK SPUreadDMAMem(unsigned short *pusPSXMem, int iSize, { int i; - do_samples_if_needed(cycles); + do_samples_if_needed(cycles, 1); for(i=0;i>1] -#define do_samples_if_needed(c) \ +#define do_samples_if_needed(c, sync) \ do { \ - if ((int)((c) - spu.cycles_played) >= 16 * 768) \ - do_samples(c); \ + if (sync || (int)((c) - spu.cycles_played) >= 16 * 768) \ + do_samples(c, sync); \ } while (0) #endif diff --git a/plugins/dfsound/freeze.c b/plugins/dfsound/freeze.c index be344ce..0b11c10 100644 --- a/plugins/dfsound/freeze.c +++ b/plugins/dfsound/freeze.c @@ -242,7 +242,7 @@ long CALLBACK SPUfreeze(uint32_t ulFreezeMode, SPUFreeze_t * pF, if(ulFreezeMode==2) return 1; // info mode? ok, bye // save mode: - do_samples(cycles); + do_samples(cycles, 1); memcpy(pF->cSPURam,spu.spuMem,0x80000); // copy common infos memcpy(pF->cSPUPort,spu.regArea,0x200); diff --git a/plugins/dfsound/registers.c b/plugins/dfsound/registers.c index bc99d9a..e65d112 100644 --- a/plugins/dfsound/registers.c +++ b/plugins/dfsound/registers.c @@ -57,7 +57,7 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val, if (val == 0 && (r & 0xff8) == 0xd88) return; - do_samples_if_needed(cycles); + do_samples_if_needed(cycles, 0); if(r>=0x0c00 && r<0x0d80) // some channel info? { diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c index 66ab150..b091efc 100644 --- a/plugins/dfsound/spu.c +++ b/plugins/dfsound/spu.c @@ -5,7 +5,7 @@ copyright : (C) 2002 by Pete Bernert email : BlackDove@addcom.de - Portions (C) Gražvydas "notaz" Ignotas, 2010-2012,2014 + Portions (C) Gražvydas "notaz" Ignotas, 2010-2012,2014,2015 ***************************************************************************/ /*************************************************************************** @@ -20,6 +20,7 @@ #ifndef _WIN32 #include // gettimeofday in xa.c +#define THREAD_ENABLED 1 #endif #include "stdafx.h" @@ -74,6 +75,44 @@ SPUConfig spu_config; SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling) REVERBInfo rvb; +#ifdef THREAD_ENABLED + +#include +#include +#include + +// worker thread state +static struct { + unsigned int pending:1; + unsigned int exit_thread:1; + int ns_to; + int ctrl; + int decode_pos; + int silentch; + int *sRVBStart; + unsigned char *ram; + unsigned int chmask; + unsigned int r_chan_end; + unsigned int r_decode_dirty; + pthread_t thread; + sem_t sem_avail; + sem_t sem_done; + struct { + int spos; + int sbpos; + int sinc; + int start; + int loop; + int ns_to; + ADSRInfoEx adsr; + // might want to add vol and fmod flags.. + } ch[24]; +} *worker; + +#else +static const void * const worker = NULL; +#endif + // certain globals (were local before, but with the new timeproc I need em global) static const int f[8][2] = { { 0, 0 }, @@ -474,6 +513,38 @@ static int skip_block(int ch) return ret; } +#ifdef THREAD_ENABLED + +static int decode_block_work(int ch, int *SB) +{ + int predict_nr, shift_factor, flags; + const unsigned char *ram = worker->ram; + int start = worker->ch[ch].start; + int loop = worker->ch[ch].loop; + + predict_nr = ram[start]; + shift_factor = predict_nr & 0xf; + predict_nr >>= 4; + + decode_block_data(SB, ram + start + 2, predict_nr, shift_factor); + + flags = ram[start + 1]; + if (flags & 4) + loop = start; // loop adress + + start += 16; + + if (flags & 1) // 1: stop/loop + start = loop; + + worker->ch[ch].start = start & 0x7ffff; + worker->ch[ch].loop = loop; + + return 0; +} + +#endif + // if irq is going to trigger sooner than in upd_samples, set upd_samples static void scan_for_irq(int ch, unsigned int *upd_samples) { @@ -515,12 +586,9 @@ static void scan_for_irq(int ch, unsigned int *upd_samples) } #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \ -static noinline int do_samples_##name(int ch, int ns_to) \ +static noinline int do_samples_##name(int (*decode_f)(int ch, int *SB), int ch, \ + int ns_to, int *SB, int sinc, int *spos, int *sbpos) \ { \ - int sinc = s_chan[ch].sinc; \ - int spos = s_chan[ch].spos; \ - int sbpos = s_chan[ch].iSBPos; \ - int *SB = s_chan[ch].SB; \ int ns, d, fa; \ int ret = ns_to; \ interp_start; \ @@ -529,28 +597,25 @@ static noinline int do_samples_##name(int ch, int ns_to) \ { \ fmod_code; \ \ - spos += sinc; \ - while (spos >= 0x10000) \ + *spos += sinc; \ + while (*spos >= 0x10000) \ { \ - fa = SB[sbpos++]; \ - if (sbpos >= 28) \ + fa = SB[(*sbpos)++]; \ + if (*sbpos >= 28) \ { \ - sbpos = 0; \ - d = decode_block(ch, SB); \ + *sbpos = 0; \ + d = decode_f(ch, SB); \ if (d && ns < ret) \ ret = ns; \ } \ \ interp1_code; \ - spos -= 0x10000; \ + *spos -= 0x10000; \ } \ \ interp2_code; \ } \ \ - s_chan[ch].sinc = sinc; \ - s_chan[ch].spos = spos; \ - s_chan[ch].iSBPos = sbpos; \ interp_end; \ \ return ret; \ @@ -562,7 +627,7 @@ static noinline int do_samples_##name(int ch, int ns_to) \ make_do_samples(default, fmod_recv_check, , StoreInterpolationVal(SB, sinc, fa, s_chan[ch].bFMod==2), - ChanBuf[ns] = iGetInterpolationVal(SB, sinc, spos, s_chan[ch].bFMod==2), ) + ChanBuf[ns] = iGetInterpolationVal(SB, sinc, *spos, s_chan[ch].bFMod==2), ) make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa) #define simple_interp_store \ @@ -745,6 +810,7 @@ static void do_silent_chans(int ns_to, int silentch) static void do_channels(int ns_to) { unsigned int mask; + int *SB, sinc; int ch, d; InitREVERB(ns_to); @@ -754,15 +820,21 @@ static void do_channels(int ns_to) { if (!(mask & 1)) continue; // channel not playing? next + SB = s_chan[ch].SB; + sinc = s_chan[ch].sinc; + if (s_chan[ch].bNoise) d = do_samples_noise(ch, ns_to); else if (s_chan[ch].bFMod == 2 || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0)) - d = do_samples_noint(ch, ns_to); + d = do_samples_noint(decode_block, ch, ns_to, + SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos); else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1) - d = do_samples_simple(ch, ns_to); + d = do_samples_simple(decode_block, ch, ns_to, + SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos); else - d = do_samples_default(ch, ns_to); + d = do_samples_default(decode_block, ch, ns_to, + SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos); d = MixADSR(&s_chan[ch].ADSRX, d); if (d < ns_to) { @@ -786,14 +858,142 @@ static void do_channels(int ns_to) } } +static void do_samples_finish(int ns_to, int silentch, int decode_pos); + +// optional worker thread handling + +#ifdef THREAD_ENABLED + +static void queue_channel_work(int ns_to, int silentch) +{ + unsigned int mask; + int ch; + + worker->ns_to = ns_to; + worker->ctrl = spu.spuCtrl; + worker->decode_pos = spu.decode_pos; + worker->silentch = silentch; + worker->sRVBStart = spu.sRVBStart; + worker->ram = spu.spuMemC; + + mask = worker->chmask = spu.dwChannelOn & 0xffffff; + for (ch = 0; mask != 0; ch++, mask >>= 1) + { + if (!(mask & 1)) continue; + + worker->ch[ch].spos = s_chan[ch].spos; + worker->ch[ch].sbpos = s_chan[ch].iSBPos; + worker->ch[ch].sinc = s_chan[ch].sinc; + worker->ch[ch].adsr = s_chan[ch].ADSRX; + worker->ch[ch].start = s_chan[ch].pCurr - spu.spuMemC; + worker->ch[ch].loop = s_chan[ch].pLoop - spu.spuMemC; + if (s_chan[ch].prevflags & 1) + worker->ch[ch].start = worker->ch[ch].loop; + + worker->ch[ch].ns_to = do_samples_skip(ch, ns_to); + } + + worker->pending = 1; + sem_post(&worker->sem_avail); +} + +static void do_channel_work(void) +{ + unsigned int mask, endmask = 0; + unsigned int decode_dirty_ch = 0; + int *SB, sinc, spos, sbpos; + int d, ch, ns_to; + + ns_to = worker->ns_to; + memset(worker->sRVBStart, 0, ns_to * sizeof(worker->sRVBStart[0]) * 2); + + mask = worker->chmask; + for (ch = 0; mask != 0; ch++, mask >>= 1) + { + if (!(mask & 1)) continue; + + d = worker->ch[ch].ns_to; + spos = worker->ch[ch].spos; + sbpos = worker->ch[ch].sbpos; + sinc = worker->ch[ch].sinc; + SB = s_chan[ch].SB; + + if (s_chan[ch].bNoise) + do_lsfr_samples(d, worker->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal); + else if (s_chan[ch].bFMod == 2 + || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0)) + do_samples_noint(decode_block_work, ch, d, SB, sinc, &spos, &sbpos); + else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1) + do_samples_simple(decode_block_work, ch, d, SB, sinc, &spos, &sbpos); + else + do_samples_default(decode_block_work, ch, d, SB, sinc, &spos, &sbpos); + + d = MixADSR(&worker->ch[ch].adsr, d); + if (d < ns_to) { + endmask |= 1 << ch; + worker->ch[ch].adsr.EnvelopeVol = 0; + memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0])); + } + + if (ch == 1 || ch == 3) + { + do_decode_bufs((void *)worker->ram, ch/2, ns_to, worker->decode_pos); + decode_dirty_ch |= 1 << ch; + } + + if (s_chan[ch].bFMod == 2) // fmod freq channel + memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); + if (s_chan[ch].bRVBActive) + mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, worker->sRVBStart); + else + mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume); + } + + worker->r_chan_end = endmask; + worker->r_decode_dirty = decode_dirty_ch; +} + +static void sync_worker_thread(void) +{ + unsigned int mask; + int ch; + + if (!worker->pending) + return; + + sem_wait(&worker->sem_done); + worker->pending = 0; + + mask = worker->chmask; + for (ch = 0; mask != 0; ch++, mask >>= 1) { + if (!(mask & 1)) continue; + + // be sure there was no keyoff while thread was working + if (s_chan[ch].ADSRX.State != ADSR_RELEASE) + s_chan[ch].ADSRX.State = worker->ch[ch].adsr.State; + s_chan[ch].ADSRX.EnvelopeVol = worker->ch[ch].adsr.EnvelopeVol; + } + + spu.dwChannelOn &= ~worker->r_chan_end; + spu.decode_dirty_ch |= worker->r_decode_dirty; + + do_samples_finish(worker->ns_to, worker->silentch, + worker->decode_pos); +} + +#else + +static void queue_channel_work(int ns_to, int silentch) {} +static void sync_worker_thread(void) {} + +#endif // THREAD_ENABLED + //////////////////////////////////////////////////////////////////////// // MAIN SPU FUNCTION // here is the main job handler... //////////////////////////////////////////////////////////////////////// -void do_samples_finish(int ns_to, int silentch); - -void do_samples(unsigned int cycles_to) +void do_samples(unsigned int cycles_to, int do_sync) { unsigned int mask; int ch, ns_to; @@ -847,7 +1047,8 @@ void do_samples(unsigned int cycles_to) } } - silentch = ~(spu.dwChannelOn|spu.dwNewChannel); + if (worker != NULL) + sync_worker_thread(); mask = spu.dwNewChannel & 0xffffff; for (ch = 0; mask != 0; ch++, mask >>= 1) { @@ -855,14 +1056,22 @@ void do_samples(unsigned int cycles_to) StartSound(ch); } - if (spu.dwChannelOn == 0) + silentch = ~spu.dwChannelOn & 0xffffff; + + if (spu.dwChannelOn == 0) { InitREVERB(ns_to); + do_samples_finish(ns_to, silentch, spu.decode_pos); + } else { - do_channels(ns_to); + if (do_sync || worker == NULL || !spu_config.iUseThread) { + do_channels(ns_to); + do_samples_finish(ns_to, silentch, spu.decode_pos); + } + else { + queue_channel_work(ns_to, silentch); + } } - do_samples_finish(ns_to, silentch); - // advance "stopped" channels that can cause irqs // (all chans are always playing on the real thing..) if (spu.spuCtrl & CTRL_IRQ) @@ -872,7 +1081,7 @@ void do_samples(unsigned int cycles_to) spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff; } -void do_samples_finish(int ns_to, int silentch) +static void do_samples_finish(int ns_to, int silentch, int decode_pos) { int volmult = spu_config.iVolume; int ns; @@ -892,7 +1101,7 @@ void do_samples_finish(int ns_to, int silentch) //---------------------------------------------------// // mix XA infos (if any) - MixXA(ns_to, spu.decode_pos); + MixXA(ns_to, decode_pos); /////////////////////////////////////////////////////// // mix all channels (including reverb) into one buffer @@ -964,7 +1173,7 @@ void schedule_next_irq(void) void CALLBACK SPUasync(unsigned int cycle, unsigned int flags) { - do_samples(cycle); + do_samples(cycle, 0); if (spu.spuCtrl & CTRL_IRQ) schedule_next_irq(); @@ -1069,7 +1278,79 @@ void RemoveStreams(void) spu.CDDAStart = NULL; } -// INIT/EXIT STUFF +#ifdef THREAD_ENABLED + +static void *spu_worker_thread(void *unused) +{ + while (1) { + sem_wait(&worker->sem_avail); + if (worker->exit_thread) + break; + + do_channel_work(); + + sem_post(&worker->sem_done); + } + + return NULL; +} + +static void init_spu_thread(void) +{ + int ret; + + if (sysconf(_SC_NPROCESSORS_ONLN) <= 1) + return; + + worker = calloc(1, sizeof(*worker)); + if (worker == NULL) + return; + ret = sem_init(&worker->sem_avail, 0, 0); + if (ret != 0) + goto fail_sem_avail; + ret = sem_init(&worker->sem_done, 0, 0); + if (ret != 0) + goto fail_sem_done; + + ret = pthread_create(&worker->thread, NULL, spu_worker_thread, NULL); + if (ret != 0) + goto fail_thread; + + return; + +fail_thread: + sem_destroy(&worker->sem_done); +fail_sem_done: + sem_destroy(&worker->sem_avail); +fail_sem_avail: + free(worker); + worker = NULL; +} + +static void exit_spu_thread(void) +{ + if (worker == NULL) + return; + worker->exit_thread = 1; + sem_post(&worker->sem_avail); + pthread_join(worker->thread, NULL); + sem_destroy(&worker->sem_done); + sem_destroy(&worker->sem_avail); + free(worker); + worker = NULL; +} + +#else // if !THREAD_ENABLED + +static void init_spu_thread(void) +{ +} + +static void exit_spu_thread(void) +{ +} + +#endif // SPUINIT: this func will be called first by the main emu long CALLBACK SPUinit(void) @@ -1088,6 +1369,8 @@ long CALLBACK SPUinit(void) if (spu_config.iVolume == 0) spu_config.iVolume = 768; // 1024 is 1.0 + init_spu_thread(); + return 0; } @@ -1122,6 +1405,8 @@ long CALLBACK SPUshutdown(void) RemoveStreams(); // no more streaming spu.bSpuInit=0; + exit_spu_thread(); + return 0; } diff --git a/plugins/dfsound/spu_config.h b/plugins/dfsound/spu_config.h index c6641be..9a139e1 100644 --- a/plugins/dfsound/spu_config.h +++ b/plugins/dfsound/spu_config.h @@ -7,6 +7,7 @@ typedef struct int iUseReverb; int iUseInterpolation; int iTempo; + int iUseThread; } SPUConfig; extern SPUConfig spu_config; -- cgit v1.2.3 From 5514a050f4e936f5c333fa1271b11bf5a6a9ea39 Mon Sep 17 00:00:00 2001 From: notaz Date: Sun, 4 Jan 2015 01:43:23 +0200 Subject: spu: start offload code to TI C64x DSP --- plugins/dfsound/adsr.c | 4 +- plugins/dfsound/arm_utils.S | 12 +- plugins/dfsound/dma.c | 26 ++-- plugins/dfsound/externals.h | 11 +- plugins/dfsound/freeze.c | 24 ++-- plugins/dfsound/registers.c | 70 ++++----- plugins/dfsound/reverb.c | 6 +- plugins/dfsound/spu.c | 338 +++++++++++++++++++++++++------------------- plugins/dfsound/spu_c64x.c | 135 ++++++++++++++++++ plugins/dfsound/spu_c64x.h | 9 ++ 10 files changed, 418 insertions(+), 217 deletions(-) create mode 100644 plugins/dfsound/spu_c64x.c create mode 100644 plugins/dfsound/spu_c64x.h (limited to 'plugins') diff --git a/plugins/dfsound/adsr.c b/plugins/dfsound/adsr.c index a86f461..836fdbb 100644 --- a/plugins/dfsound/adsr.c +++ b/plugins/dfsound/adsr.c @@ -57,8 +57,8 @@ void InitADSR(void) // INIT ADSR INLINE void StartADSR(int ch) // MIX ADSR { - s_chan[ch].ADSRX.State=ADSR_ATTACK; // and init some adsr vars - s_chan[ch].ADSRX.EnvelopeVol=0; + spu.s_chan[ch].ADSRX.State = ADSR_ATTACK; // and init some adsr vars + spu.s_chan[ch].ADSRX.EnvelopeVol = 0; } //////////////////////////////////////////////////////////////////////// diff --git a/plugins/dfsound/arm_utils.S b/plugins/dfsound/arm_utils.S index 1726389..2511bb3 100644 --- a/plugins/dfsound/arm_utils.S +++ b/plugins/dfsound/arm_utils.S @@ -39,9 +39,10 @@ ptr_SSumLR: .word ESYM(SSumLR) FUNCTION(mix_chan): @ (int start, int count, int lv, int rv) vmov.32 d14[0], r2 vmov.32 d14[1], r3 @ multipliers + load_varadr r2, SSumLR mov r12, r0 load_varadr r0, ChanBuf - load_varadr r2, SSumLR + ldr r2, [r2] add r0, r12, lsl #2 add r2, r12, lsl #3 0: @@ -72,10 +73,11 @@ mc_finish: FUNCTION(mix_chan_rvb): @ (int start, int count, int lv, int rv, int *rvb) vmov.32 d14[0], r2 vmov.32 d14[1], r3 @ multipliers + load_varadr r2, SSumLR mov r12, r0 load_varadr r0, ChanBuf ldr r3, [sp] @ rvb - load_varadr r2, SSumLR + ldr r2, [r2] add r0, r12, lsl #2 add r2, r12, lsl #3 add r3, r12, lsl #3 @@ -116,9 +118,10 @@ FUNCTION(mix_chan): @ (int start, int count, int lv, int rv) stmfd sp!, {r4-r8,lr} orr r3, r2, r3, lsl #16 lsl r3, #1 @ packed multipliers << 1 + load_varadr r2, SSumLR mov r12, r0 load_varadr r0, ChanBuf - load_varadr r2, SSumLR + ldr r2, [r2] add r0, r12, lsl #2 add r2, r12, lsl #3 0: @@ -145,8 +148,9 @@ FUNCTION(mix_chan_rvb): @ (int start, int count, int lv, int rv, int *rvb) stmfd sp!, {r4-r8,lr} orr lr, r2, r3, lsl #16 lsl lr, #1 - ldr r3, [sp] @ rvb load_varadr r2, SSumLR + ldr r3, [sp] @ rvb + ldr r2, [r2] load_varadr r4, ChanBuf add r2, r2, r0, lsl #3 add r3, r3, r0, lsl #3 diff --git a/plugins/dfsound/dma.c b/plugins/dfsound/dma.c index 8d647ff..4798a19 100644 --- a/plugins/dfsound/dma.c +++ b/plugins/dfsound/dma.c @@ -27,9 +27,9 @@ unsigned short CALLBACK SPUreadDMA(void) { - unsigned short s=spu.spuMem[spu.spuAddr>>1]; - spu.spuAddr+=2; - if(spu.spuAddr>0x7ffff) spu.spuAddr=0; + unsigned short s = *(unsigned short *)(spu.spuMemC + spu.spuAddr); + spu.spuAddr += 2; + spu.spuAddr &= 0x7fffe; return s; } @@ -47,9 +47,9 @@ void CALLBACK SPUreadDMAMem(unsigned short *pusPSXMem, int iSize, for(i=0;i>1]; // spu addr got by writeregister - spu.spuAddr+=2; // inc spu addr - if(spu.spuAddr>0x7ffff) spu.spuAddr=0; // wrap + *pusPSXMem++ = *(unsigned short *)(spu.spuMemC + spu.spuAddr); + spu.spuAddr += 2; + spu.spuAddr &= 0x7fffe; } } @@ -67,10 +67,10 @@ void CALLBACK SPUreadDMAMem(unsigned short *pusPSXMem, int iSize, void CALLBACK SPUwriteDMA(unsigned short val) { - spu.spuMem[spu.spuAddr>>1] = val; // spu addr got by writeregister + *(unsigned short *)(spu.spuMemC + spu.spuAddr) = val; - spu.spuAddr+=2; // inc spu addr - if(spu.spuAddr>0x7ffff) spu.spuAddr=0; // wrap + spu.spuAddr += 2; + spu.spuAddr &= 0x7fffe; } //////////////////////////////////////////////////////////////////////// @@ -86,16 +86,16 @@ void CALLBACK SPUwriteDMAMem(unsigned short *pusPSXMem, int iSize, if(spu.spuAddr + iSize*2 < 0x80000) { - memcpy(&spu.spuMem[spu.spuAddr>>1], pusPSXMem, iSize*2); + memcpy(spu.spuMemC + spu.spuAddr, pusPSXMem, iSize*2); spu.spuAddr += iSize*2; return; } for(i=0;i>1] = *pusPSXMem++; // spu addr got by writeregister - spu.spuAddr+=2; // inc spu addr - spu.spuAddr&=0x7ffff; // wrap + *(unsigned short *)(spu.spuMemC + spu.spuAddr) = *pusPSXMem++; + spu.spuAddr += 2; + spu.spuAddr &= 0x7fffe; } } diff --git a/plugins/dfsound/externals.h b/plugins/dfsound/externals.h index 3a3ff68..3047afc 100644 --- a/plugins/dfsound/externals.h +++ b/plugins/dfsound/externals.h @@ -181,7 +181,10 @@ typedef struct unsigned short spuStat; unsigned int spuAddr; - unsigned char * spuMemC; + union { + unsigned char *spuMemC; + unsigned short *spuMem; + }; unsigned char * pSpuIrq; unsigned int cycles_played; @@ -222,9 +225,10 @@ typedef struct int iLeftXAVol; int iRightXAVol; - int pad[32]; + SPUCHAN * s_chan; + + int pad[31]; unsigned short regArea[0x400]; - unsigned short spuMem[256*1024]; } SPUInfo; /////////////////////////////////////////////////////////// @@ -234,7 +238,6 @@ typedef struct #ifndef _IN_SPU extern SPUInfo spu; -extern SPUCHAN s_chan[]; extern REVERBInfo rvb; void do_samples(unsigned int cycles_to, int do_sync); diff --git a/plugins/dfsound/freeze.c b/plugins/dfsound/freeze.c index 0b11c10..83a7d52 100644 --- a/plugins/dfsound/freeze.c +++ b/plugins/dfsound/freeze.c @@ -264,11 +264,11 @@ long CALLBACK SPUfreeze(uint32_t ulFreezeMode, SPUFreeze_t * pF, for(i=0;is_chan[i],&s_chan[i],i); - if(s_chan[i].pCurr) - pFO->s_chan[i].iCurr=s_chan[i].pCurr-spu.spuMemC; - if(s_chan[i].pLoop) - pFO->s_chan[i].iLoop=s_chan[i].pLoop-spu.spuMemC; + save_channel(&pFO->s_chan[i],&spu.s_chan[i],i); + if(spu.s_chan[i].pCurr) + pFO->s_chan[i].iCurr=spu.s_chan[i].pCurr-spu.spuMemC; + if(spu.s_chan[i].pLoop) + pFO->s_chan[i].iLoop=spu.s_chan[i].pLoop-spu.spuMemC; } return 1; @@ -302,7 +302,7 @@ long CALLBACK SPUfreeze(uint32_t ulFreezeMode, SPUFreeze_t * pF, load_register(H_CDRight, cycles); // fix to prevent new interpolations from crashing - for(i=0;ispuAddr) { - spu.spuAddr = pFO->spuAddr; - if (spu.spuAddr == 0xbaadf00d) spu.spuAddr = 0; + if (pFO->spuAddr == 0xbaadf00d) spu.spuAddr = 0; + else spu.spuAddr = pFO->spuAddr & 0x7fffe; } spu.dwNewChannel=0; @@ -334,10 +334,10 @@ void LoadStateV5(SPUFreeze_t * pF) spu.dwChannelDead=0; for(i=0;is_chan[i],i); + load_channel(&spu.s_chan[i],&pFO->s_chan[i],i); - s_chan[i].pCurr+=(unsigned long)spu.spuMemC; - s_chan[i].pLoop+=(unsigned long)spu.spuMemC; + spu.s_chan[i].pCurr+=(unsigned long)spu.spuMemC; + spu.s_chan[i].pLoop+=(unsigned long)spu.spuMemC; } } @@ -349,7 +349,7 @@ void LoadStateUnknown(SPUFreeze_t * pF, uint32_t cycles) for(i=0;i>8) & 0x007f; - s_chan[ch].ADSRX.DecayRate=(lval>>4) & 0x000f; - s_chan[ch].ADSRX.SustainLevel=lval & 0x000f; + spu.s_chan[ch].ADSRX.AttackModeExp=(lval&0x8000)?1:0; + spu.s_chan[ch].ADSRX.AttackRate=(lval>>8) & 0x007f; + spu.s_chan[ch].ADSRX.DecayRate=(lval>>4) & 0x000f; + spu.s_chan[ch].ADSRX.SustainLevel=lval & 0x000f; //---------------------------------------------// } break; @@ -98,11 +98,11 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val, const unsigned long lval=val; //----------------------------------------------// - s_chan[ch].ADSRX.SustainModeExp = (lval&0x8000)?1:0; - s_chan[ch].ADSRX.SustainIncrease= (lval&0x4000)?0:1; - s_chan[ch].ADSRX.SustainRate = (lval>>6) & 0x007f; - s_chan[ch].ADSRX.ReleaseModeExp = (lval&0x0020)?1:0; - s_chan[ch].ADSRX.ReleaseRate = lval & 0x001f; + spu.s_chan[ch].ADSRX.SustainModeExp = (lval&0x8000)?1:0; + spu.s_chan[ch].ADSRX.SustainIncrease= (lval&0x4000)?0:1; + spu.s_chan[ch].ADSRX.SustainRate = (lval>>6) & 0x007f; + spu.s_chan[ch].ADSRX.ReleaseModeExp = (lval&0x0020)?1:0; + spu.s_chan[ch].ADSRX.ReleaseRate = lval & 0x001f; //----------------------------------------------// } break; @@ -111,7 +111,7 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val, break; //------------------------------------------------// case 14: // loop? - s_chan[ch].pLoop=spu.spuMemC+((val&~1)<<3); + spu.s_chan[ch].pLoop=spu.spuMemC+((val&~1)<<3); goto upd_irq; //------------------------------------------------// } @@ -126,9 +126,9 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val, break; //-------------------------------------------------// case H_SPUdata: - spu.spuMem[spu.spuAddr>>1] = val; - spu.spuAddr+=2; - if(spu.spuAddr>0x7ffff) spu.spuAddr=0; + *(unsigned short *)(spu.spuMemC + spu.spuAddr) = val; + spu.spuAddr += 2; + spu.spuAddr &= 0x7fffe; break; //-------------------------------------------------// case H_SPUctrl: @@ -307,15 +307,15 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg) const int ch=(r>>4)-0xc0; if(spu.dwNewChannel&(1<>16); + return (unsigned short)(spu.s_chan[ch].ADSRX.EnvelopeVol>>16); } case 14: // get loop address { const int ch=(r>>4)-0xc0; - return (unsigned short)((s_chan[ch].pLoop-spu.spuMemC)>>3); + return (unsigned short)((spu.s_chan[ch].pLoop-spu.spuMemC)>>3); } } } @@ -333,9 +333,9 @@ unsigned short CALLBACK SPUreadRegister(unsigned long reg) case H_SPUdata: { - unsigned short s=spu.spuMem[spu.spuAddr>>1]; - spu.spuAddr+=2; - if(spu.spuAddr>0x7ffff) spu.spuAddr=0; + unsigned short s = *(unsigned short *)(spu.spuMemC + spu.spuAddr); + spu.spuAddr += 2; + spu.spuAddr &= 0x7fffe; return s; } @@ -362,8 +362,8 @@ static void SoundOn(int start,int end,unsigned short val) { if((val&1) && regAreaGet(ch,6)) // mmm... start has to be set before key on !?! { - s_chan[ch].pCurr=spu.spuMemC+((regAreaGet(ch,6)&~1)<<3); // must be block aligned - s_chan[ch].pLoop=spu.spuMemC+((regAreaGet(ch,14)&~1)<<3); + spu.s_chan[ch].pCurr=spu.spuMemC+((regAreaGet(ch,6)&~1)<<3); // must be block aligned + spu.s_chan[ch].pLoop=spu.spuMemC+((regAreaGet(ch,14)&~1)<<3); spu.dwNewChannel|=(1<0) { - s_chan[ch].bFMod=1; // --> sound channel - s_chan[ch-1].bFMod=2; // --> freq channel + spu.s_chan[ch].bFMod=1; // --> sound channel + spu.s_chan[ch-1].bFMod=2; // --> freq channel } } else { - s_chan[ch].bFMod=0; // --> turn off fmod - if(ch>0&&s_chan[ch-1].bFMod==2) - s_chan[ch-1].bFMod=0; + spu.s_chan[ch].bFMod=0; // --> turn off fmod + if(ch>0&&spu.s_chan[ch-1].bFMod==2) + spu.s_chan[ch-1].bFMod=0; } } } @@ -426,7 +426,7 @@ static void NoiseOn(int start,int end,unsigned short val) for(ch=start;ch>=1) // loop channels { - s_chan[ch].bNoise=val&1; // -> noise on/off + spu.s_chan[ch].bNoise=val&1; // -> noise on/off } } @@ -456,7 +456,7 @@ static void SetVolumeL(unsigned char ch,short vol) // LEFT VOLUME } vol&=0x3fff; - s_chan[ch].iLeftVolume=vol; // store volume + spu.s_chan[ch].iLeftVolume=vol; // store volume } //////////////////////////////////////////////////////////////////////// @@ -482,7 +482,7 @@ static void SetVolumeR(unsigned char ch,short vol) // RIGHT VOLUME vol&=0x3fff; - s_chan[ch].iRightVolume=vol; + spu.s_chan[ch].iRightVolume=vol; } //////////////////////////////////////////////////////////////////////// @@ -495,10 +495,10 @@ static void SetPitch(int ch,unsigned short val) // SET PITCH if(val>0x3fff) NP=0x3fff; // get pitch val else NP=val; - s_chan[ch].iRawPitch=NP; - s_chan[ch].sinc=(NP<<4)|8; - s_chan[ch].sinc_inv=0; - if(spu_config.iUseInterpolation==1) s_chan[ch].SB[32]=1; // -> freq change in simple interpolation mode: set flag + spu.s_chan[ch].iRawPitch=NP; + spu.s_chan[ch].sinc=(NP<<4)|8; + spu.s_chan[ch].sinc_inv=0; + if(spu_config.iUseInterpolation==1) spu.s_chan[ch].SB[32]=1; // -> freq change in simple interpolation mode: set flag } //////////////////////////////////////////////////////////////////////// @@ -511,6 +511,6 @@ static void ReverbOn(int start,int end,unsigned short val) for(ch=start;ch>=1) // loop channels { - s_chan[ch].bReverb=val&1; // -> reverb on/off + spu.s_chan[ch].bReverb=val&1; // -> reverb on/off } } diff --git a/plugins/dfsound/reverb.c b/plugins/dfsound/reverb.c index bb5ee8e..b7bcf12 100644 --- a/plugins/dfsound/reverb.c +++ b/plugins/dfsound/reverb.c @@ -32,11 +32,11 @@ INLINE void StartREVERB(int ch) { - if(s_chan[ch].bReverb && (spu.spuCtrl&0x80)) // reverb possible? + if(spu.s_chan[ch].bReverb && (spu.spuCtrl&0x80)) // reverb possible? { - s_chan[ch].bRVBActive=!!spu_config.iUseReverb; + spu.s_chan[ch].bRVBActive=!!spu_config.iUseReverb; } - else s_chan[ch].bRVBActive=0; // else -> no reverb + else spu.s_chan[ch].bRVBActive=0; // else -> no reverb } //////////////////////////////////////////////////////////////////////// diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c index b091efc..96ef69c 100644 --- a/plugins/dfsound/spu.c +++ b/plugins/dfsound/spu.c @@ -72,31 +72,21 @@ SPUConfig spu_config; // MAIN infos struct for each channel -SPUCHAN s_chan[MAXCHAN+1]; // channel + 1 infos (1 is security for fmod handling) REVERBInfo rvb; #ifdef THREAD_ENABLED -#include -#include -#include - // worker thread state -static struct { +static struct spu_worker { unsigned int pending:1; unsigned int exit_thread:1; int ns_to; int ctrl; int decode_pos; int silentch; - int *sRVBStart; - unsigned char *ram; unsigned int chmask; unsigned int r_chan_end; unsigned int r_decode_dirty; - pthread_t thread; - sem_t sem_avail; - sem_t sem_done; struct { int spos; int sbpos; @@ -115,14 +105,9 @@ static const void * const worker = NULL; // certain globals (were local before, but with the new timeproc I need em global) -static const int f[8][2] = { { 0, 0 }, - { 60, 0 }, - { 115, -52 }, - { 98, -55 }, - { 122, -60 } }; +static int iFMod[NSSIZE]; int ChanBuf[NSSIZE]; -int SSumLR[NSSIZE*2]; -int iFMod[NSSIZE]; +int *SSumLR; #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2 @@ -274,20 +259,22 @@ static int check_irq(int ch, unsigned char *pos) INLINE void StartSound(int ch) { + SPUCHAN *s_chan = &spu.s_chan[ch]; + StartADSR(ch); StartREVERB(ch); - s_chan[ch].prevflags=2; + s_chan->prevflags=2; - s_chan[ch].SB[26]=0; // init mixing vars - s_chan[ch].SB[27]=0; - s_chan[ch].iSBPos=27; + s_chan->SB[26]=0; // init mixing vars + s_chan->SB[27]=0; + s_chan->iSBPos=27; - s_chan[ch].SB[28]=0; - s_chan[ch].SB[29]=0; // init our interpolation helpers - s_chan[ch].SB[30]=0; - s_chan[ch].SB[31]=0; - s_chan[ch].spos=0; + s_chan->SB[28]=0; + s_chan->SB[29]=0; // init our interpolation helpers + s_chan->SB[30]=0; + s_chan->SB[31]=0; + s_chan->spos=0; spu.dwNewChannel&=~(1<pCurr; // set up the current pos if (start == spu.spuMemC) // ? ret = 1; - if (s_chan[ch].prevflags & 1) // 1: stop/loop + if (s_chan->prevflags & 1) // 1: stop/loop { - if (!(s_chan[ch].prevflags & 2)) + if (!(s_chan->prevflags & 2)) ret = 1; - start = s_chan[ch].pLoop; + start = s_chan->pLoop; } else check_irq(ch, start); // hack, see check_irq below.. - predict_nr = (int)start[0]; + predict_nr = start[0]; shift_factor = predict_nr & 0xf; predict_nr >>= 4; @@ -462,20 +457,20 @@ static int decode_block(int ch, int *SB) flags = start[1]; if (flags & 4) - s_chan[ch].pLoop = start; // loop adress + s_chan->pLoop = start; // loop adress start += 16; if (flags & 1) { // 1: stop/loop - start = s_chan[ch].pLoop; + start = s_chan->pLoop; check_irq(ch, start); // hack.. :( } if (start - spu.spuMemC >= 0x80000) start = spu.spuMemC; - s_chan[ch].pCurr = start; // store values for next cycle - s_chan[ch].prevflags = flags; + s_chan->pCurr = start; // store values for next cycle + s_chan->prevflags = flags; return ret; } @@ -483,32 +478,33 @@ static int decode_block(int ch, int *SB) // do block, but ignore sample data static int skip_block(int ch) { - unsigned char *start = s_chan[ch].pCurr; + SPUCHAN *s_chan = &spu.s_chan[ch]; + unsigned char *start = s_chan->pCurr; int flags; int ret = 0; - if (s_chan[ch].prevflags & 1) { - if (!(s_chan[ch].prevflags & 2)) + if (s_chan->prevflags & 1) { + if (!(s_chan->prevflags & 2)) ret = 1; - start = s_chan[ch].pLoop; + start = s_chan->pLoop; } else check_irq(ch, start); flags = start[1]; if (flags & 4) - s_chan[ch].pLoop = start; + s_chan->pLoop = start; start += 16; if (flags & 1) { - start = s_chan[ch].pLoop; + start = s_chan->pLoop; check_irq(ch, start); } - s_chan[ch].pCurr = start; - s_chan[ch].prevflags = flags; + s_chan->pCurr = start; + s_chan->prevflags = flags; return ret; } @@ -517,8 +513,8 @@ static int skip_block(int ch) static int decode_block_work(int ch, int *SB) { + const unsigned char *ram = spu.spuMemC; int predict_nr, shift_factor, flags; - const unsigned char *ram = worker->ram; int start = worker->ch[ch].start; int loop = worker->ch[ch].loop; @@ -548,16 +544,17 @@ static int decode_block_work(int ch, int *SB) // if irq is going to trigger sooner than in upd_samples, set upd_samples static void scan_for_irq(int ch, unsigned int *upd_samples) { + SPUCHAN *s_chan = &spu.s_chan[ch]; int pos, sinc, sinc_inv, end; unsigned char *block; int flags; - block = s_chan[ch].pCurr; - pos = s_chan[ch].spos; - sinc = s_chan[ch].sinc; + block = s_chan->pCurr; + pos = s_chan->spos; + sinc = s_chan->sinc; end = pos + *upd_samples * sinc; - pos += (28 - s_chan[ch].iSBPos) << 16; + pos += (28 - s_chan->iSBPos) << 16; while (pos < end) { if (block == spu.pSpuIrq) @@ -565,7 +562,7 @@ static void scan_for_irq(int ch, unsigned int *upd_samples) flags = block[1]; block += 16; if (flags & 1) { // 1: stop/loop - block = s_chan[ch].pLoop; + block = s_chan->pLoop; if (block == spu.pSpuIrq) // hack.. (see decode_block) break; } @@ -574,11 +571,11 @@ static void scan_for_irq(int ch, unsigned int *upd_samples) if (pos < end) { - sinc_inv = s_chan[ch].sinc_inv; + sinc_inv = s_chan->sinc_inv; if (sinc_inv == 0) - sinc_inv = s_chan[ch].sinc_inv = (0x80000000u / (uint32_t)sinc) << 1; + sinc_inv = s_chan->sinc_inv = (0x80000000u / (uint32_t)sinc) << 1; - pos -= s_chan[ch].spos; + pos -= s_chan->spos; *upd_samples = (((uint64_t)pos * sinc_inv) >> 32) + 1; //xprintf("ch%02d: irq sched: %3d %03d\n", // ch, *upd_samples, *upd_samples * 60 * 263 / 44100); @@ -622,12 +619,12 @@ static noinline int do_samples_##name(int (*decode_f)(int ch, int *SB), int ch, } #define fmod_recv_check \ - if(s_chan[ch].bFMod==1 && iFMod[ns]) \ - sinc = FModChangeFrequency(SB, s_chan[ch].iRawPitch, ns) + if(spu.s_chan[ch].bFMod==1 && iFMod[ns]) \ + sinc = FModChangeFrequency(SB, spu.s_chan[ch].iRawPitch, ns) make_do_samples(default, fmod_recv_check, , - StoreInterpolationVal(SB, sinc, fa, s_chan[ch].bFMod==2), - ChanBuf[ns] = iGetInterpolationVal(SB, sinc, *spos, s_chan[ch].bFMod==2), ) + StoreInterpolationVal(SB, sinc, fa, spu.s_chan[ch].bFMod==2), + ChanBuf[ns] = iGetInterpolationVal(SB, sinc, *spos, spu.s_chan[ch].bFMod==2), ) make_do_samples(noint, , fa = SB[29], , ChanBuf[ns] = fa, SB[29] = fa) #define simple_interp_store \ @@ -648,24 +645,25 @@ make_do_samples(simple, , , static int do_samples_skip(int ch, int ns_to) { + SPUCHAN *s_chan = &spu.s_chan[ch]; int ret = ns_to, ns, d; - s_chan[ch].spos += s_chan[ch].iSBPos << 16; + s_chan->spos += s_chan->iSBPos << 16; for (ns = 0; ns < ns_to; ns++) { - s_chan[ch].spos += s_chan[ch].sinc; - while (s_chan[ch].spos >= 28*0x10000) + s_chan->spos += s_chan->sinc; + while (s_chan->spos >= 28*0x10000) { d = skip_block(ch); if (d && ns < ret) ret = ns; - s_chan[ch].spos -= 28*0x10000; + s_chan->spos -= 28*0x10000; } } - s_chan[ch].iSBPos = s_chan[ch].spos >> 16; - s_chan[ch].spos &= 0xffff; + s_chan->iSBPos = s_chan->spos >> 16; + s_chan->spos &= 0xffff; return ret; } @@ -776,33 +774,38 @@ static noinline void do_decode_bufs(unsigned short *mem, int which, static void do_silent_chans(int ns_to, int silentch) { + unsigned int mask; + SPUCHAN *s_chan; int ch; - for (ch = 0; ch < MAXCHAN; ch++) + mask = silentch & 0xffffff; + for (ch = 0; mask != 0; ch++, mask >>= 1) { - if (!(silentch & (1< spu.pSpuIrq && s_chan[ch].pLoop > spu.pSpuIrq) + + s_chan = &spu.s_chan[ch]; + if (s_chan->pCurr > spu.pSpuIrq && s_chan->pLoop > spu.pSpuIrq) continue; - s_chan[ch].spos += s_chan[ch].iSBPos << 16; - s_chan[ch].iSBPos = 0; + s_chan->spos += s_chan->iSBPos << 16; + s_chan->iSBPos = 0; - s_chan[ch].spos += s_chan[ch].sinc * ns_to; - while (s_chan[ch].spos >= 28 * 0x10000) + s_chan->spos += s_chan->sinc * ns_to; + while (s_chan->spos >= 28 * 0x10000) { - unsigned char *start = s_chan[ch].pCurr; + unsigned char *start = s_chan->pCurr; skip_block(ch); - if (start == s_chan[ch].pCurr || start - spu.spuMemC < 0x1000) + if (start == s_chan->pCurr || start - spu.spuMemC < 0x1000) { // looping on self or stopped(?) spu.dwChannelDead |= 1<spos = 0; break; } - s_chan[ch].spos -= 28 * 0x10000; + s_chan->spos -= 28 * 0x10000; } } } @@ -810,6 +813,7 @@ static void do_silent_chans(int ns_to, int silentch) static void do_channels(int ns_to) { unsigned int mask; + SPUCHAN *s_chan; int *SB, sinc; int ch, d; @@ -820,26 +824,27 @@ static void do_channels(int ns_to) { if (!(mask & 1)) continue; // channel not playing? next - SB = s_chan[ch].SB; - sinc = s_chan[ch].sinc; + s_chan = &spu.s_chan[ch]; + SB = s_chan->SB; + sinc = s_chan->sinc; - if (s_chan[ch].bNoise) + if (s_chan->bNoise) d = do_samples_noise(ch, ns_to); - else if (s_chan[ch].bFMod == 2 - || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0)) + else if (s_chan->bFMod == 2 + || (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 0)) d = do_samples_noint(decode_block, ch, ns_to, - SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos); - else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1) + SB, sinc, &s_chan->spos, &s_chan->iSBPos); + else if (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 1) d = do_samples_simple(decode_block, ch, ns_to, - SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos); + SB, sinc, &s_chan->spos, &s_chan->iSBPos); else d = do_samples_default(decode_block, ch, ns_to, - SB, sinc, &s_chan[ch].spos, &s_chan[ch].iSBPos); + SB, sinc, &s_chan->spos, &s_chan->iSBPos); - d = MixADSR(&s_chan[ch].ADSRX, d); + d = MixADSR(&s_chan->ADSRX, d); if (d < ns_to) { spu.dwChannelOn &= ~(1 << ch); - s_chan[ch].ADSRX.EnvelopeVol = 0; + s_chan->ADSRX.EnvelopeVol = 0; memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0])); } @@ -849,12 +854,12 @@ static void do_channels(int ns_to) spu.decode_dirty_ch |= 1 << ch; } - if (s_chan[ch].bFMod == 2) // fmod freq channel + if (s_chan->bFMod == 2) // fmod freq channel memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); - if (s_chan[ch].bRVBActive) - mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, spu.sRVBStart); + if (s_chan->bRVBActive) + mix_chan_rvb(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, spu.sRVBStart); else - mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume); + mix_chan(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); } } @@ -864,8 +869,12 @@ static void do_samples_finish(int ns_to, int silentch, int decode_pos); #ifdef THREAD_ENABLED +static void thread_work_start(void); +static void thread_work_wait_sync(void); + static void queue_channel_work(int ns_to, int silentch) { + const SPUCHAN *s_chan; unsigned int mask; int ch; @@ -873,28 +882,27 @@ static void queue_channel_work(int ns_to, int silentch) worker->ctrl = spu.spuCtrl; worker->decode_pos = spu.decode_pos; worker->silentch = silentch; - worker->sRVBStart = spu.sRVBStart; - worker->ram = spu.spuMemC; mask = worker->chmask = spu.dwChannelOn & 0xffffff; for (ch = 0; mask != 0; ch++, mask >>= 1) { if (!(mask & 1)) continue; - worker->ch[ch].spos = s_chan[ch].spos; - worker->ch[ch].sbpos = s_chan[ch].iSBPos; - worker->ch[ch].sinc = s_chan[ch].sinc; - worker->ch[ch].adsr = s_chan[ch].ADSRX; - worker->ch[ch].start = s_chan[ch].pCurr - spu.spuMemC; - worker->ch[ch].loop = s_chan[ch].pLoop - spu.spuMemC; - if (s_chan[ch].prevflags & 1) + s_chan = &spu.s_chan[ch]; + worker->ch[ch].spos = s_chan->spos; + worker->ch[ch].sbpos = s_chan->iSBPos; + worker->ch[ch].sinc = s_chan->sinc; + worker->ch[ch].adsr = s_chan->ADSRX; + worker->ch[ch].start = s_chan->pCurr - spu.spuMemC; + worker->ch[ch].loop = s_chan->pLoop - spu.spuMemC; + if (s_chan->prevflags & 1) worker->ch[ch].start = worker->ch[ch].loop; worker->ch[ch].ns_to = do_samples_skip(ch, ns_to); } worker->pending = 1; - sem_post(&worker->sem_avail); + thread_work_start(); } static void do_channel_work(void) @@ -903,9 +911,10 @@ static void do_channel_work(void) unsigned int decode_dirty_ch = 0; int *SB, sinc, spos, sbpos; int d, ch, ns_to; + SPUCHAN *s_chan; ns_to = worker->ns_to; - memset(worker->sRVBStart, 0, ns_to * sizeof(worker->sRVBStart[0]) * 2); + memset(spu.sRVBStart, 0, ns_to * sizeof(spu.sRVBStart[0]) * 2); mask = worker->chmask; for (ch = 0; mask != 0; ch++, mask >>= 1) @@ -916,14 +925,16 @@ static void do_channel_work(void) spos = worker->ch[ch].spos; sbpos = worker->ch[ch].sbpos; sinc = worker->ch[ch].sinc; - SB = s_chan[ch].SB; - if (s_chan[ch].bNoise) + s_chan = &spu.s_chan[ch]; + SB = s_chan->SB; + + if (s_chan->bNoise) do_lsfr_samples(d, worker->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal); - else if (s_chan[ch].bFMod == 2 - || (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 0)) + else if (s_chan->bFMod == 2 + || (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 0)) do_samples_noint(decode_block_work, ch, d, SB, sinc, &spos, &sbpos); - else if (s_chan[ch].bFMod == 0 && spu_config.iUseInterpolation == 1) + else if (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 1) do_samples_simple(decode_block_work, ch, d, SB, sinc, &spos, &sbpos); else do_samples_default(decode_block_work, ch, d, SB, sinc, &spos, &sbpos); @@ -937,16 +948,16 @@ static void do_channel_work(void) if (ch == 1 || ch == 3) { - do_decode_bufs((void *)worker->ram, ch/2, ns_to, worker->decode_pos); + do_decode_bufs(spu.spuMem, ch/2, ns_to, worker->decode_pos); decode_dirty_ch |= 1 << ch; } - if (s_chan[ch].bFMod == 2) // fmod freq channel + if (s_chan->bFMod == 2) // fmod freq channel memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); - if (s_chan[ch].bRVBActive) - mix_chan_rvb(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume, worker->sRVBStart); + if (s_chan->bRVBActive) + mix_chan_rvb(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, spu.sRVBStart); else - mix_chan(0, ns_to, s_chan[ch].iLeftVolume, s_chan[ch].iRightVolume); + mix_chan(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); } worker->r_chan_end = endmask; @@ -961,7 +972,7 @@ static void sync_worker_thread(void) if (!worker->pending) return; - sem_wait(&worker->sem_done); + thread_work_wait_sync(); worker->pending = 0; mask = worker->chmask; @@ -969,9 +980,9 @@ static void sync_worker_thread(void) if (!(mask & 1)) continue; // be sure there was no keyoff while thread was working - if (s_chan[ch].ADSRX.State != ADSR_RELEASE) - s_chan[ch].ADSRX.State = worker->ch[ch].adsr.State; - s_chan[ch].ADSRX.EnvelopeVol = worker->ch[ch].adsr.EnvelopeVol; + if (spu.s_chan[ch].ADSRX.State != ADSR_RELEASE) + spu.s_chan[ch].ADSRX.State = worker->ch[ch].adsr.State; + spu.s_chan[ch].ADSRX.EnvelopeVol = worker->ch[ch].adsr.EnvelopeVol; } spu.dwChannelOn &= ~worker->r_chan_end; @@ -1145,8 +1156,8 @@ void schedule_next_irq(void) { if (spu.dwChannelDead & (1 << ch)) continue; - if ((unsigned long)(spu.pSpuIrq - s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16 - && (unsigned long)(spu.pSpuIrq - s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16) + if ((unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pCurr) > IRQ_NEAR_BLOCKS * 16 + && (unsigned long)(spu.pSpuIrq - spu.s_chan[ch].pLoop) > IRQ_NEAR_BLOCKS * 16) continue; scan_for_irq(ch, &upd_samples); @@ -1226,8 +1237,8 @@ int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes) // to be called after state load void ClearWorkingState(void) { - memset(SSumLR,0,sizeof(SSumLR)); // init some mixing buffers - memset(iFMod,0,sizeof(iFMod)); + memset(SSumLR, 0, NSSIZE * 2 * 4); // init some mixing buffers + memset(iFMod, 0, sizeof(iFMod)); spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer } @@ -1237,8 +1248,8 @@ void SetupStreams(void) int i; spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer - spu.sRVBStart = (int *)malloc(NSSIZE*2*4); // alloc reverb buffer - memset(spu.sRVBStart,0,NSSIZE*2*4); + spu.sRVBStart = calloc(NSSIZE * 2, sizeof(spu.sRVBStart[0])); + SSumLR = calloc(NSSIZE * 2, sizeof(SSumLR[0])); spu.XAStart = // alloc xa buffer (uint32_t *)malloc(44100 * sizeof(uint32_t)); @@ -1254,10 +1265,10 @@ void SetupStreams(void) for(i=0;i init sustain - s_chan[i].ADSRX.SustainIncrease = 1; - s_chan[i].pLoop=spu.spuMemC; - s_chan[i].pCurr=spu.spuMemC; + spu.s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain + spu.s_chan[i].ADSRX.SustainIncrease = 1; + spu.s_chan[i].pLoop=spu.spuMemC; + spu.s_chan[i].pCurr=spu.spuMemC; } ClearWorkingState(); @@ -1272,24 +1283,53 @@ void RemoveStreams(void) spu.pSpuBuffer = NULL; free(spu.sRVBStart); // free reverb buffer spu.sRVBStart = NULL; + free(SSumLR); + SSumLR = NULL; free(spu.XAStart); // free XA buffer spu.XAStart = NULL; free(spu.CDDAStart); // free CDDA buffer spu.CDDAStart = NULL; } -#ifdef THREAD_ENABLED +#if defined(C64X_DSP) + +/* special code for TI C64x DSP */ +#include "spu_c64x.c" + +#elif defined(THREAD_ENABLED) + +#include +#include +#include + +static struct { + pthread_t thread; + sem_t sem_avail; + sem_t sem_done; +} t; + +/* generic pthread implementation */ + +static void thread_work_start(void) +{ + sem_post(&t.sem_avail); +} + +static void thread_work_wait_sync(void) +{ + sem_wait(&t.sem_done); +} static void *spu_worker_thread(void *unused) { while (1) { - sem_wait(&worker->sem_avail); + sem_wait(&t.sem_avail); if (worker->exit_thread) break; do_channel_work(); - sem_post(&worker->sem_done); + sem_post(&t.sem_done); } return NULL; @@ -1305,23 +1345,23 @@ static void init_spu_thread(void) worker = calloc(1, sizeof(*worker)); if (worker == NULL) return; - ret = sem_init(&worker->sem_avail, 0, 0); + ret = sem_init(&t.sem_avail, 0, 0); if (ret != 0) goto fail_sem_avail; - ret = sem_init(&worker->sem_done, 0, 0); + ret = sem_init(&t.sem_done, 0, 0); if (ret != 0) goto fail_sem_done; - ret = pthread_create(&worker->thread, NULL, spu_worker_thread, NULL); + ret = pthread_create(&t.thread, NULL, spu_worker_thread, NULL); if (ret != 0) goto fail_thread; return; fail_thread: - sem_destroy(&worker->sem_done); + sem_destroy(&t.sem_done); fail_sem_done: - sem_destroy(&worker->sem_avail); + sem_destroy(&t.sem_avail); fail_sem_avail: free(worker); worker = NULL; @@ -1332,10 +1372,10 @@ static void exit_spu_thread(void) if (worker == NULL) return; worker->exit_thread = 1; - sem_post(&worker->sem_avail); - pthread_join(worker->thread, NULL); - sem_destroy(&worker->sem_done); - sem_destroy(&worker->sem_avail); + sem_post(&t.sem_avail); + pthread_join(t.thread, NULL); + sem_destroy(&t.sem_done); + sem_destroy(&t.sem_avail); free(worker); worker = NULL; } @@ -1355,13 +1395,14 @@ static void exit_spu_thread(void) // SPUINIT: this func will be called first by the main emu long CALLBACK SPUinit(void) { - spu.spuMemC = (unsigned char *)spu.spuMem; // just small setup + spu.spuMemC = calloc(1, 512 * 1024); memset((void *)&rvb, 0, sizeof(REVERBInfo)); InitADSR(); - spu.spuAddr = 0xffffffff; + spu.s_chan = calloc(MAXCHAN+1, sizeof(spu.s_chan[0])); // channel + 1 infos (1 is security for fmod handling) + + spu.spuAddr = 0; spu.decode_pos = 0; - memset((void *)s_chan, 0, sizeof(s_chan)); spu.pSpuIrq = spu.spuMemC; SetupStreams(); // prepare streaming @@ -1402,11 +1443,17 @@ long CALLBACK SPUclose(void) long CALLBACK SPUshutdown(void) { SPUclose(); - RemoveStreams(); // no more streaming - spu.bSpuInit=0; exit_spu_thread(); + free(spu.spuMemC); + spu.spuMemC = NULL; + free(spu.s_chan); + spu.s_chan = NULL; + + RemoveStreams(); // no more streaming + spu.bSpuInit=0; + return 0; } @@ -1483,15 +1530,18 @@ void spu_get_debug_info(int *chans_out, int *run_chans, int *fmod_chans_out, int { int ch = 0, fmod_chans = 0, noise_chans = 0, irq_chans = 0; + if (spu.s_chan == NULL) + return; + for(;ch +#include +#include "spu_c64x.h" + +static dsp_mem_region_t region; + +static struct { + void *handle; + int (*dsp_open)(void); + dsp_mem_region_t (*dsp_shm_alloc)(dsp_cache_t _type, sU32 _numBytes); + int (*dsp_shm_free)(dsp_mem_region_t _mem); + void (*dsp_close)(void); + int (*dsp_component_load)(const char *_path, const char *_name, dsp_component_id_t *_id); + int (*dsp_cache_inv_virt)(void *_virtAddr, sU32 _size); + int (*dsp_rpc_send)(const dsp_msg_t *_msgTo); + int (*dsp_rpc_recv)(dsp_msg_t *_msgFrom); + void (*dsp_logbuf_print)(void); +} f; + +static void thread_work_start(void) +{ + do_channel_work(); +} + +static void thread_work_wait_sync(void) +{ +} + +static void init_spu_thread(void) +{ + struct region_mem *mem; + int ret; + + if (f.handle == NULL) { + const char lib[] = "libc64.so.1"; + int failed = 0; + + f.handle = dlopen(lib, RTLD_NOW); + if (f.handle == NULL) { + fprintf(stderr, "can't load %s: %s\n", lib, dlerror()); + return; + } + #define LDS(name) \ + failed |= (f.name = dlsym(f.handle, #name)) == NULL + LDS(dsp_open); + LDS(dsp_close); + LDS(dsp_shm_alloc); + LDS(dsp_shm_free); + LDS(dsp_cache_inv_virt); + LDS(dsp_component_load); + LDS(dsp_rpc_send); + LDS(dsp_rpc_recv); + LDS(dsp_logbuf_print); + #undef LDS + if (failed) { + fprintf(stderr, "missing symbol(s) in %s\n", lib); + dlclose(f.handle); + f.handle = NULL; + return; + } + } + + ret = f.dsp_open(); + if (ret != 0) { + fprintf(stderr, "dsp_open failed: %d\n", ret); + return; + } + + region = f.dsp_shm_alloc(DSP_CACHE_R, sizeof(*mem)); // writethrough + if (region.size < sizeof(*mem) || region.virt_addr == 0) { + fprintf(stderr, "dsp_shm_alloc failed\n"); + goto fail_mem; + } + mem = (void *)region.virt_addr; + + // override default allocations + free(spu.spuMemC); + spu.spuMemC = mem->spu_ram; + free(spu.sRVBStart); + spu.sRVBStart = mem->RVB; + free(SSumLR); + SSumLR = mem->SSumLR; + free(spu.s_chan); + spu.s_chan = mem->s_chan; + worker = &mem->worker; + + printf("C64x DSP ready.\n"); + return; + +fail_mem: + f.dsp_close(); + worker = NULL; +} + +static void exit_spu_thread(void) +{ + if (worker == NULL) + return; + + if (worker->pending) + thread_work_wait_sync(); + f.dsp_shm_free(region); + f.dsp_close(); + + spu.spuMemC = NULL; + spu.sRVBStart = NULL; + SSumLR = NULL; + spu.s_chan = NULL; + worker = NULL; +} + +// vim:shiftwidth=1:expandtab diff --git a/plugins/dfsound/spu_c64x.h b/plugins/dfsound/spu_c64x.h new file mode 100644 index 0000000..ba2a4c3 --- /dev/null +++ b/plugins/dfsound/spu_c64x.h @@ -0,0 +1,9 @@ + +struct region_mem { + unsigned char spu_ram[512 * 1024]; + int RVB[NSSIZE * 2]; + int SSumLR[NSSIZE * 2]; + SPUCHAN s_chan[24 + 1]; + struct spu_worker worker; +}; + -- cgit v1.2.3 From de4a0279efefdd2e4595c8fc27f1564f4bff9341 Mon Sep 17 00:00:00 2001 From: notaz Date: Mon, 5 Jan 2015 04:27:03 +0200 Subject: spu: finish offload code to TI C64x DSP rather disappointing result so far, though :( --- plugins/dfsound/Makefile.c64p | 15 ++++++ plugins/dfsound/externals.h | 7 +-- plugins/dfsound/freeze.c | 10 ++-- plugins/dfsound/registers.c | 3 +- plugins/dfsound/spu.c | 80 +++++++++++++++++---------- plugins/dfsound/spu_c64x.c | 89 +++++++++++++++++++++++++++++- plugins/dfsound/spu_c64x.h | 16 ++++++ plugins/dfsound/spu_c64x_dspcode.c | 108 +++++++++++++++++++++++++++++++++++++ plugins/dfsound/xa.c | 4 +- 9 files changed, 292 insertions(+), 40 deletions(-) create mode 100644 plugins/dfsound/Makefile.c64p create mode 100644 plugins/dfsound/spu_c64x_dspcode.c (limited to 'plugins') diff --git a/plugins/dfsound/Makefile.c64p b/plugins/dfsound/Makefile.c64p new file mode 100644 index 0000000..45fe76a --- /dev/null +++ b/plugins/dfsound/Makefile.c64p @@ -0,0 +1,15 @@ +ifndef C64_TOOLS_DSP_ROOT +$(error need C64_TOOLS_DSP_ROOT) +endif + +include $(C64_TOOLS_DSP_ROOT)/install.mk + +TARGET_BASENAME = pcsxr_spu +OPTFLAGS += -O2 +CFLAGS += -DNO_OS -DWANT_THREAD_CODE + +OBJ = \ + spu_c64x_dspcode.o64 + +include $(C64_TOOLS_DSP_ROOT)/build_area3.mk +include $(C64_TOOLS_DSP_ROOT)/build.mk diff --git a/plugins/dfsound/externals.h b/plugins/dfsound/externals.h index 3047afc..d3bcbc6 100644 --- a/plugins/dfsound/externals.h +++ b/plugins/dfsound/externals.h @@ -110,8 +110,6 @@ typedef struct int iRightVolume; // right volume ADSRInfoEx ADSRX; int iRawPitch; // raw pitch (0...3fff) - - int SB[32+4]; } SPUCHAN; /////////////////////////////////////////////////////////// @@ -175,6 +173,8 @@ typedef struct // psx buffers / addresses +#define SB_SIZE (32 + 4) + typedef struct { unsigned short spuCtrl; @@ -226,8 +226,9 @@ typedef struct int iRightXAVol; SPUCHAN * s_chan; + int * SB; - int pad[31]; + int pad[30]; unsigned short regArea[0x400]; } SPUInfo; diff --git a/plugins/dfsound/freeze.c b/plugins/dfsound/freeze.c index 83a7d52..5b4267b 100644 --- a/plugins/dfsound/freeze.c +++ b/plugins/dfsound/freeze.c @@ -143,7 +143,7 @@ static void save_channel(SPUCHAN_orig *d, const SPUCHAN *s, int ch) d->iSBPos = s->iSBPos; d->spos = s->spos; d->sinc = s->sinc; - memcpy(d->SB, s->SB, sizeof(d->SB)); + memcpy(d->SB, spu.SB + ch * SB_SIZE, sizeof(d->SB[0]) * SB_SIZE); d->iStart = (regAreaGet(ch,6)&~1)<<3; d->iCurr = 0; // set by the caller d->iLoop = 0; // set by the caller @@ -157,8 +157,8 @@ static void save_channel(SPUCHAN_orig *d, const SPUCHAN *s, int ch) d->bIgnoreLoop = (s->prevflags ^ 2) << 1; d->iRightVolume = s->iRightVolume; d->iRawPitch = s->iRawPitch; - d->s_1 = s->SB[27]; // yes it's reversed - d->s_2 = s->SB[26]; + d->s_1 = spu.SB[ch * SB_SIZE + 27]; // yes it's reversed + d->s_2 = spu.SB[ch * SB_SIZE + 26]; d->bRVBActive = s->bRVBActive; d->bNoise = s->bNoise; d->bFMod = s->bFMod; @@ -185,7 +185,7 @@ static void load_channel(SPUCHAN *d, const SPUCHAN_orig *s, int ch) d->spos = s->spos; d->sinc = s->sinc; d->sinc_inv = 0; - memcpy(d->SB, s->SB, sizeof(d->SB)); + memcpy(spu.SB + ch * SB_SIZE, s->SB, sizeof(spu.SB[0]) * SB_SIZE); d->pCurr = (void *)((long)s->iCurr & 0x7fff0); d->pLoop = (void *)((long)s->iLoop & 0x7fff0); d->bReverb = s->bReverb; @@ -302,7 +302,7 @@ long CALLBACK SPUfreeze(uint32_t ulFreezeMode, SPUFreeze_t * pF, load_register(H_CDRight, cycles); // fix to prevent new interpolations from crashing - for(i=0;i freq change in simple interpolation mode: set flag + if (spu_config.iUseInterpolation == 1) + spu.SB[ch * SB_SIZE + 32] = 1; // -> freq change in simple interpolation mode: set flag } //////////////////////////////////////////////////////////////////////// diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c index 96ef69c..c6a06fd 100644 --- a/plugins/dfsound/spu.c +++ b/plugins/dfsound/spu.c @@ -18,7 +18,7 @@ * * ***************************************************************************/ -#ifndef _WIN32 +#if !defined(_WIN32) && !defined(NO_OS) #include // gettimeofday in xa.c #define THREAD_ENABLED 1 #endif @@ -29,9 +29,12 @@ #include "externals.h" #include "registers.h" #include "out.h" -#include "arm_features.h" #include "spu_config.h" +#ifdef __arm__ +#include "arm_features.h" +#endif + #ifdef __ARM_ARCH_7A__ #define ssat32_to_16(v) \ asm("ssat %0,#16,%1" : "=r" (v) : "r" (v)) @@ -74,19 +77,18 @@ SPUConfig spu_config; REVERBInfo rvb; -#ifdef THREAD_ENABLED +#if defined(THREAD_ENABLED) || defined(WANT_THREAD_CODE) // worker thread state static struct spu_worker { unsigned int pending:1; unsigned int exit_thread:1; + unsigned int stale_cache:1; int ns_to; int ctrl; int decode_pos; int silentch; unsigned int chmask; - unsigned int r_chan_end; - unsigned int r_decode_dirty; struct { int spos; int sbpos; @@ -97,6 +99,14 @@ static struct spu_worker { ADSRInfoEx adsr; // might want to add vol and fmod flags.. } ch[24]; + struct { + struct { + int adsrState; + int adsrEnvelopeVol; + } ch[24]; + unsigned int chan_end; + unsigned int decode_dirty; + } r; } *worker; #else @@ -260,20 +270,21 @@ static int check_irq(int ch, unsigned char *pos) INLINE void StartSound(int ch) { SPUCHAN *s_chan = &spu.s_chan[ch]; + int *SB = spu.SB + ch * SB_SIZE; StartADSR(ch); StartREVERB(ch); s_chan->prevflags=2; - s_chan->SB[26]=0; // init mixing vars - s_chan->SB[27]=0; s_chan->iSBPos=27; + SB[26]=0; // init mixing vars + SB[27]=0; - s_chan->SB[28]=0; - s_chan->SB[29]=0; // init our interpolation helpers - s_chan->SB[30]=0; - s_chan->SB[31]=0; + SB[28]=0; + SB[29]=0; // init our interpolation helpers + SB[30]=0; + SB[31]=0; s_chan->spos=0; spu.dwNewChannel&=~(1<SB; + SB = spu.SB + ch * SB_SIZE; sinc = s_chan->sinc; if (s_chan->bNoise) @@ -867,10 +878,11 @@ static void do_samples_finish(int ns_to, int silentch, int decode_pos); // optional worker thread handling -#ifdef THREAD_ENABLED +#if defined(THREAD_ENABLED) || defined(WANT_THREAD_CODE) static void thread_work_start(void); static void thread_work_wait_sync(void); +static void thread_sync_caches(void); static void queue_channel_work(int ns_to, int silentch) { @@ -927,7 +939,7 @@ static void do_channel_work(void) sinc = worker->ch[ch].sinc; s_chan = &spu.s_chan[ch]; - SB = s_chan->SB; + SB = spu.SB + ch * SB_SIZE; if (s_chan->bNoise) do_lsfr_samples(d, worker->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal); @@ -945,6 +957,8 @@ static void do_channel_work(void) worker->ch[ch].adsr.EnvelopeVol = 0; memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0])); } + worker->r.ch[ch].adsrState = worker->ch[ch].adsr.State; + worker->r.ch[ch].adsrEnvelopeVol = worker->ch[ch].adsr.EnvelopeVol; if (ch == 1 || ch == 3) { @@ -960,15 +974,17 @@ static void do_channel_work(void) mix_chan(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); } - worker->r_chan_end = endmask; - worker->r_decode_dirty = decode_dirty_ch; + worker->r.chan_end = endmask; + worker->r.decode_dirty = decode_dirty_ch; } -static void sync_worker_thread(void) +static void sync_worker_thread(int do_direct) { unsigned int mask; int ch; + if (do_direct) + thread_sync_caches(); if (!worker->pending) return; @@ -981,12 +997,12 @@ static void sync_worker_thread(void) // be sure there was no keyoff while thread was working if (spu.s_chan[ch].ADSRX.State != ADSR_RELEASE) - spu.s_chan[ch].ADSRX.State = worker->ch[ch].adsr.State; - spu.s_chan[ch].ADSRX.EnvelopeVol = worker->ch[ch].adsr.EnvelopeVol; + spu.s_chan[ch].ADSRX.State = worker->r.ch[ch].adsrState; + spu.s_chan[ch].ADSRX.EnvelopeVol = worker->r.ch[ch].adsrEnvelopeVol; } - spu.dwChannelOn &= ~worker->r_chan_end; - spu.decode_dirty_ch |= worker->r_decode_dirty; + spu.dwChannelOn &= ~worker->r.chan_end; + spu.decode_dirty_ch |= worker->r.decode_dirty; do_samples_finish(worker->ns_to, worker->silentch, worker->decode_pos); @@ -995,7 +1011,7 @@ static void sync_worker_thread(void) #else static void queue_channel_work(int ns_to, int silentch) {} -static void sync_worker_thread(void) {} +static void sync_worker_thread(int do_direct) {} #endif // THREAD_ENABLED @@ -1004,7 +1020,7 @@ static void sync_worker_thread(void) {} // here is the main job handler... //////////////////////////////////////////////////////////////////////// -void do_samples(unsigned int cycles_to, int do_sync) +void do_samples(unsigned int cycles_to, int do_direct) { unsigned int mask; int ch, ns_to; @@ -1019,6 +1035,10 @@ void do_samples(unsigned int cycles_to, int do_sync) return; } + do_direct |= (cycle_diff < 64 * 768); + if (worker != NULL) + sync_worker_thread(do_direct); + if (cycle_diff < 2 * 768) return; @@ -1058,9 +1078,6 @@ void do_samples(unsigned int cycles_to, int do_sync) } } - if (worker != NULL) - sync_worker_thread(); - mask = spu.dwNewChannel & 0xffffff; for (ch = 0; mask != 0; ch++, mask >>= 1) { if (mask & 1) @@ -1074,7 +1091,7 @@ void do_samples(unsigned int cycles_to, int do_sync) do_samples_finish(ns_to, silentch, spu.decode_pos); } else { - if (do_sync || worker == NULL || !spu_config.iUseThread) { + if (do_direct || worker == NULL || !spu_config.iUseThread) { do_channels(ns_to); do_samples_finish(ns_to, silentch, spu.decode_pos); } @@ -1320,6 +1337,10 @@ static void thread_work_wait_sync(void) sem_wait(&t.sem_done); } +static void thread_sync_caches(void) +{ +} + static void *spu_worker_thread(void *unused) { while (1) { @@ -1400,6 +1421,7 @@ long CALLBACK SPUinit(void) InitADSR(); spu.s_chan = calloc(MAXCHAN+1, sizeof(spu.s_chan[0])); // channel + 1 infos (1 is security for fmod handling) + spu.SB = calloc(MAXCHAN, sizeof(spu.SB[0]) * SB_SIZE); spu.spuAddr = 0; spu.decode_pos = 0; @@ -1448,6 +1470,8 @@ long CALLBACK SPUshutdown(void) free(spu.spuMemC); spu.spuMemC = NULL; + free(spu.SB); + spu.SB = NULL; free(spu.s_chan); spu.s_chan = NULL; diff --git a/plugins/dfsound/spu_c64x.c b/plugins/dfsound/spu_c64x.c index 86b2149..d829d29 100644 --- a/plugins/dfsound/spu_c64x.c +++ b/plugins/dfsound/spu_c64x.c @@ -22,10 +22,13 @@ */ #include +#include + #include #include "spu_c64x.h" static dsp_mem_region_t region; +static dsp_component_id_t compid; static struct { void *handle; @@ -37,20 +40,57 @@ static struct { int (*dsp_cache_inv_virt)(void *_virtAddr, sU32 _size); int (*dsp_rpc_send)(const dsp_msg_t *_msgTo); int (*dsp_rpc_recv)(dsp_msg_t *_msgFrom); + int (*dsp_rpc)(const dsp_msg_t *_msgTo, dsp_msg_t *_msgFrom); void (*dsp_logbuf_print)(void); } f; static void thread_work_start(void) { - do_channel_work(); + dsp_msg_t msg; + int ret; + + DSP_MSG_INIT(&msg, compid, CCMD_DOIT, 0, 0); + ret = f.dsp_rpc_send(&msg); + if (ret != 0) { + fprintf(stderr, "dsp_rpc_send failed: %d\n", ret); + f.dsp_logbuf_print(); + // maybe stop using the DSP? + } } static void thread_work_wait_sync(void) { + dsp_msg_t msg; + int ns_to; + int ret; + + ns_to = worker->ns_to; + f.dsp_cache_inv_virt(spu.sRVBStart, sizeof(spu.sRVBStart[0]) * 2 * ns_to); + f.dsp_cache_inv_virt(SSumLR, sizeof(SSumLR[0]) * 2 * ns_to); + f.dsp_cache_inv_virt(&worker->r, sizeof(worker->r)); + worker->stale_cache = 1; // SB, ram + + ret = f.dsp_rpc_recv(&msg); + if (ret != 0) { + fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret); + f.dsp_logbuf_print(); + } + //f.dsp_logbuf_print(); +} + +// called before ARM decides to do SPU mixing itself +static void thread_sync_caches(void) +{ + if (worker->stale_cache) { + f.dsp_cache_inv_virt(spu.SB, sizeof(spu.SB[0]) * SB_SIZE * 24); + f.dsp_cache_inv_virt(spu.spuMemC + 0x800, 0x800); + worker->stale_cache = 0; + } } static void init_spu_thread(void) { + dsp_msg_t init_msg, msg_in; struct region_mem *mem; int ret; @@ -73,6 +113,7 @@ static void init_spu_thread(void) LDS(dsp_component_load); LDS(dsp_rpc_send); LDS(dsp_rpc_recv); + LDS(dsp_rpc); LDS(dsp_logbuf_print); #undef LDS if (failed) { @@ -89,6 +130,12 @@ static void init_spu_thread(void) return; } + ret = f.dsp_component_load(NULL, COMPONENT_NAME, &compid); + if (ret != 0) { + fprintf(stderr, "dsp_component_load failed: %d\n", ret); + goto fail_cload; + } + region = f.dsp_shm_alloc(DSP_CACHE_R, sizeof(*mem)); // writethrough if (region.size < sizeof(*mem) || region.virt_addr == 0) { fprintf(stderr, "dsp_shm_alloc failed\n"); @@ -96,6 +143,31 @@ static void init_spu_thread(void) } mem = (void *)region.virt_addr; + memcpy(&mem->spu_config, &spu_config, sizeof(mem->spu_config)); + + DSP_MSG_INIT(&init_msg, compid, CCMD_INIT, region.phys_addr, 0); + ret = f.dsp_rpc(&init_msg, &msg_in); + if (ret != 0) { + fprintf(stderr, "dsp_rpc failed: %d\n", ret); + goto fail_init; + } + + if (mem->sizeof_region_mem != sizeof(*mem)) { + fprintf(stderr, "error: size mismatch 1: %d vs %zd\n", + mem->sizeof_region_mem, sizeof(*mem)); + goto fail_init; + } + if (mem->offsetof_s_chan1 != offsetof(typeof(*mem), s_chan[1])) { + fprintf(stderr, "error: size mismatch 2: %d vs %zd\n", + mem->offsetof_s_chan1, offsetof(typeof(*mem), s_chan[1])); + goto fail_init; + } + if (mem->offsetof_worker_ram != offsetof(typeof(*mem), worker.ch[1])) { + fprintf(stderr, "error: size mismatch 3: %d vs %zd\n", + mem->offsetof_worker_ram, offsetof(typeof(*mem), worker.ch[1])); + goto fail_init; + } + // override default allocations free(spu.spuMemC); spu.spuMemC = mem->spu_ram; @@ -103,14 +175,26 @@ static void init_spu_thread(void) spu.sRVBStart = mem->RVB; free(SSumLR); SSumLR = mem->SSumLR; + free(spu.SB); + spu.SB = mem->SB; free(spu.s_chan); spu.s_chan = mem->s_chan; worker = &mem->worker; - printf("C64x DSP ready.\n"); + printf("spu: C64x DSP ready (id=%d).\n", (int)compid); + f.dsp_logbuf_print(); + +pcnt_init(); + (void)do_channel_work; // used by DSP instead return; +fail_init: + f.dsp_shm_free(region); fail_mem: + // no component unload func? +fail_cload: + printf("spu: C64x DSP init failed.\n"); + f.dsp_logbuf_print(); f.dsp_close(); worker = NULL; } @@ -128,6 +212,7 @@ static void exit_spu_thread(void) spu.spuMemC = NULL; spu.sRVBStart = NULL; SSumLR = NULL; + spu.SB = NULL; spu.s_chan = NULL; worker = NULL; } diff --git a/plugins/dfsound/spu_c64x.h b/plugins/dfsound/spu_c64x.h index ba2a4c3..d4e73e9 100644 --- a/plugins/dfsound/spu_c64x.h +++ b/plugins/dfsound/spu_c64x.h @@ -1,9 +1,25 @@ +#define COMPONENT_NAME "pcsxr_spu" + +enum { + CCMD_INIT = 0x101, + CCMD_DOIT = 0x102, +}; struct region_mem { unsigned char spu_ram[512 * 1024]; int RVB[NSSIZE * 2]; int SSumLR[NSSIZE * 2]; + int SB[SB_SIZE * 24]; + // careful not to lose ARM writes by DSP overwriting + // with old data when it's writing out neighbor cachelines + int _pad1[128/4 - ((NSSIZE * 4 + SB_SIZE * 24) & (128/4 - 1))]; SPUCHAN s_chan[24 + 1]; + int _pad2[128/4 - ((sizeof(SPUCHAN) * 25 / 4) & (128/4 - 1))]; struct spu_worker worker; + SPUConfig spu_config; + // init/debug + int sizeof_region_mem; + int offsetof_s_chan1; + int offsetof_worker_ram; }; diff --git a/plugins/dfsound/spu_c64x_dspcode.c b/plugins/dfsound/spu_c64x_dspcode.c new file mode 100644 index 0000000..117a296 --- /dev/null +++ b/plugins/dfsound/spu_c64x_dspcode.c @@ -0,0 +1,108 @@ +/* + * SPU processing offload to TI C64x DSP using bsp's c64_tools + * (C) Gražvydas "notaz" Ignotas, 2015 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#define SYSCALLS_C +#include +#include + +#include "spu.c" +#include "spu_c64x.h" + +/* dummy deps, some bloat but avoids ifdef hell in SPU code.. */ +static void thread_work_start(void) {} +static void thread_work_wait_sync(void) {} +static void thread_sync_caches(void) {} +struct out_driver *out_current; +void SetupSound(void) {} + +#if 0 +// no use, c64_tools does BCACHE_wbInvAll.. +static void sync_caches(void) +{ + int ns_to = worker->ns_to; + + syscalls.cache_wb(spu.sRVBStart, sizeof(spu.sRVBStart[0]) * 2 * ns_to, 1); + syscalls.cache_wb(SSumLR, sizeof(SSumLR[0]) * 2 * ns_to, 1); + + syscalls.cache_wbInv(worker, sizeof(*worker), 1); +} +#endif + +static unsigned int exec(dsp_component_cmd_t cmd, + unsigned int arg1, unsigned int arg2, + unsigned int *ret1, unsigned int *ret2) +{ + struct region_mem *mem = (void *)arg1; + int i; + + switch (cmd) { + case CCMD_INIT: + InitADSR(); + + spu.spuMemC = mem->spu_ram; + spu.sRVBStart = mem->RVB; + SSumLR = mem->SSumLR; + spu.SB = mem->SB; + spu.s_chan = mem->s_chan; + worker = &mem->worker; + memcpy(&spu_config, &mem->spu_config, sizeof(spu_config)); + + mem->sizeof_region_mem = sizeof(*mem); + mem->offsetof_s_chan1 = offsetof(typeof(*mem), s_chan[1]); + mem->offsetof_worker_ram = offsetof(typeof(*mem), worker.ch[1]); + // seems to be unneeded, no write-alloc? but just in case.. + syscalls.cache_wb(&mem->sizeof_region_mem, 3 * 4, 1); + break; + + case CCMD_DOIT: + do_channel_work(); + // c64_tools lib does BCACHE_wbInvAll() when it receives mailbox irq, + // so there is no benefit of syncing only what's needed. + // But call wbInvAll() anyway in case c64_tools is ever fixed.. + //sync_caches(); + syscalls.cache_wbInvAll(); + break; + + default: + syscalls.printf("bad cmd: %x\n", cmd); + break; + } + + return 0; +} + +#pragma DATA_SECTION(component_test_dsp, ".sec_com"); +dsp_component_t component_test_dsp = { + { + NULL, /* init */ + exec, + NULL, /* exec fastcall RPC */ + NULL, /* exit */ + }, + + COMPONENT_NAME, +}; + +DSP_COMPONENT_MAIN + +// vim:shiftwidth=1:expandtab diff --git a/plugins/dfsound/xa.c b/plugins/dfsound/xa.c index 1241380..6c0ce4b 100644 --- a/plugins/dfsound/xa.c +++ b/plugins/dfsound/xa.c @@ -90,7 +90,9 @@ INLINE void MixXA(int ns_to, int decode_pos) static unsigned long timeGetTime_spu() { -#ifdef _WIN32 +#if defined(NO_OS) + return 0; +#elif defined(_WIN32) return GetTickCount(); #else struct timeval tv; -- cgit v1.2.3 From 3bd31caf9e9f5ddab2bf4fbdb5a129f4972c45f3 Mon Sep 17 00:00:00 2001 From: notaz Date: Sat, 10 Jan 2015 01:55:24 +0200 Subject: spu: support multiple pending buffers for threaded implementation --- plugins/dfsound/adsr.c | 125 +++++++++++ plugins/dfsound/arm_utils.S | 38 +--- plugins/dfsound/externals.h | 7 +- plugins/dfsound/reverb.c | 23 +- plugins/dfsound/spu.c | 438 ++++++++++++++++++++----------------- plugins/dfsound/spu_c64x.c | 157 +++++++++---- plugins/dfsound/spu_c64x.h | 7 +- plugins/dfsound/spu_c64x_dspcode.c | 84 +++++-- plugins/dfsound/spu_config.h | 3 + plugins/dfsound/xa.c | 2 +- 10 files changed, 561 insertions(+), 323 deletions(-) (limited to 'plugins') diff --git a/plugins/dfsound/adsr.c b/plugins/dfsound/adsr.c index 836fdbb..69e55bd 100644 --- a/plugins/dfsound/adsr.c +++ b/plugins/dfsound/adsr.c @@ -217,6 +217,131 @@ done: return ns; } +static int SkipADSR(ADSRInfoEx *adsr, int ns_to) +{ + int EnvelopeVol = adsr->EnvelopeVol; + int ns = 0, val, rto, level; + + if (adsr->State == ADSR_RELEASE) + { + val = RateTableSub[adsr->ReleaseRate * 4]; + if (adsr->ReleaseModeExp) + { + for (; ns < ns_to; ns++) + { + EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); + if (EnvelopeVol <= 0) + break; + } + } + else + { + EnvelopeVol += val * ns_to; + if (EnvelopeVol > 0) + ns = ns_to; + } + goto done; + } + + switch (adsr->State) + { + case ADSR_ATTACK: // -> attack + rto = 0; + if (adsr->AttackModeExp && EnvelopeVol >= 0x60000000) + rto = 8; + val = RateTableAdd[adsr->AttackRate + rto]; + + for (; ns < ns_to; ns++) + { + EnvelopeVol += val; + if (EnvelopeVol < 0) + break; + } + if (EnvelopeVol < 0) // overflow + { + EnvelopeVol = 0x7fffffff; + adsr->State = ADSR_DECAY; + ns++; + goto decay; + } + break; + + //--------------------------------------------------// + decay: + case ADSR_DECAY: // -> decay + val = RateTableSub[adsr->DecayRate * 4]; + level = adsr->SustainLevel; + + for (; ns < ns_to; ) + { + EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); + if (EnvelopeVol < 0) + EnvelopeVol = 0; + + ns++; + + if (((EnvelopeVol >> 27) & 0xf) <= level) + { + adsr->State = ADSR_SUSTAIN; + goto sustain; + } + } + break; + + //--------------------------------------------------// + sustain: + case ADSR_SUSTAIN: // -> sustain + if (adsr->SustainIncrease) + { + if (EnvelopeVol >= 0x7fff0000) + { + ns = ns_to; + break; + } + + rto = 0; + if (adsr->SustainModeExp && EnvelopeVol >= 0x60000000) + rto = 8; + val = RateTableAdd[adsr->SustainRate + rto]; + + EnvelopeVol += val * (ns_to - ns); + if ((unsigned int)EnvelopeVol >= 0x7fe00000) + { + EnvelopeVol = 0x7fffffff; + ns = ns_to; + break; + } + } + else + { + val = RateTableSub[adsr->SustainRate]; + if (adsr->SustainModeExp) + { + for (; ns < ns_to; ns++) + { + EnvelopeVol += ((long long)val * EnvelopeVol) >> (15+16); + if (EnvelopeVol < 0) + break; + } + } + else + { + EnvelopeVol += val * (ns_to - ns); + if (EnvelopeVol > 0) + { + ns = ns_to; + break; + } + } + } + break; + } + +done: + adsr->EnvelopeVol = EnvelopeVol; + return ns; +} + #endif /* diff --git a/plugins/dfsound/arm_utils.S b/plugins/dfsound/arm_utils.S index 2511bb3..9652313 100644 --- a/plugins/dfsound/arm_utils.S +++ b/plugins/dfsound/arm_utils.S @@ -14,7 +14,6 @@ .data .align 2 ptr_ChanBuf: .word ESYM(ChanBuf) -ptr_SSumLR: .word ESYM(SSumLR) #endif .text @@ -36,15 +35,11 @@ ptr_SSumLR: .word ESYM(SSumLR) #ifdef __ARM_NEON__ -FUNCTION(mix_chan): @ (int start, int count, int lv, int rv) +FUNCTION(mix_chan): @ (int *SSumLR, int count, int lv, int rv) vmov.32 d14[0], r2 vmov.32 d14[1], r3 @ multipliers - load_varadr r2, SSumLR - mov r12, r0 + mov r2, r0 load_varadr r0, ChanBuf - ldr r2, [r2] - add r0, r12, lsl #2 - add r2, r12, lsl #3 0: vldmia r0!, {d0-d1} vldmia r2, {d2-d5} @@ -70,17 +65,12 @@ mc_finish: bx lr -FUNCTION(mix_chan_rvb): @ (int start, int count, int lv, int rv, int *rvb) +FUNCTION(mix_chan_rvb): @ (int *SSumLR, int count, int lv, int rv, int *rvb) vmov.32 d14[0], r2 vmov.32 d14[1], r3 @ multipliers - load_varadr r2, SSumLR - mov r12, r0 + mov r2, r0 load_varadr r0, ChanBuf ldr r3, [sp] @ rvb - ldr r2, [r2] - add r0, r12, lsl #2 - add r2, r12, lsl #3 - add r3, r12, lsl #3 0: vldmia r0!, {d0-d1} vldmia r2, {d2-d5} @@ -114,16 +104,12 @@ mcr_finish: #elif defined(HAVE_ARMV5) -FUNCTION(mix_chan): @ (int start, int count, int lv, int rv) +FUNCTION(mix_chan): @ (int *SSumLR, int count, int lv, int rv) stmfd sp!, {r4-r8,lr} orr r3, r2, r3, lsl #16 lsl r3, #1 @ packed multipliers << 1 - load_varadr r2, SSumLR - mov r12, r0 + mov r2, r0 load_varadr r0, ChanBuf - ldr r2, [r2] - add r0, r12, lsl #2 - add r2, r12, lsl #3 0: ldmia r0!, {r4,r5} ldmia r2, {r6-r8,lr} @@ -144,17 +130,13 @@ mc_finish: ldmfd sp!, {r4-r8,pc} -FUNCTION(mix_chan_rvb): @ (int start, int count, int lv, int rv, int *rvb) +FUNCTION(mix_chan_rvb): @ (int *SSumLR, int count, int lv, int rv, int *rvb) stmfd sp!, {r4-r8,lr} orr lr, r2, r3, lsl #16 lsl lr, #1 - load_varadr r2, SSumLR - ldr r3, [sp] @ rvb - ldr r2, [r2] - load_varadr r4, ChanBuf - add r2, r2, r0, lsl #3 - add r3, r3, r0, lsl #3 - add r0, r4, r0, lsl #2 + mov r2, r0 + load_varadr r0, ChanBuf + ldr r3, [sp, #6*4] @ rvb 0: ldr r4, [r0], #4 ldmia r2, {r6,r7} diff --git a/plugins/dfsound/externals.h b/plugins/dfsound/externals.h index d3bcbc6..4832fac 100644 --- a/plugins/dfsound/externals.h +++ b/plugins/dfsound/externals.h @@ -206,8 +206,6 @@ typedef struct void (CALLBACK *cddavCallback)(unsigned short,unsigned short); void (CALLBACK *scheduleCallback)(unsigned int); - int * sRVBStart; - xa_decode_t * xapGlobal; unsigned int * XAFeed; unsigned int * XAPlay; @@ -228,7 +226,10 @@ typedef struct SPUCHAN * s_chan; int * SB; - int pad[30]; + int * RVB; + int * SSumLR; + + int pad[29]; unsigned short regArea[0x400]; } SPUInfo; diff --git a/plugins/dfsound/reverb.c b/plugins/dfsound/reverb.c index b7bcf12..7e32b8e 100644 --- a/plugins/dfsound/reverb.c +++ b/plugins/dfsound/reverb.c @@ -39,15 +39,6 @@ INLINE void StartREVERB(int ch) else spu.s_chan[ch].bRVBActive=0; // else -> no reverb } -//////////////////////////////////////////////////////////////////////// -// HELPER FOR NEILL'S REVERB: re-inits our reverb mixing buf -//////////////////////////////////////////////////////////////////////// - -INLINE void InitREVERB(int ns_to) -{ - memset(spu.sRVBStart,0,ns_to*sizeof(spu.sRVBStart[0])*2); -} - //////////////////////////////////////////////////////////////////////// INLINE int rvb2ram_offs(int curr, int space, int iOff) @@ -73,7 +64,7 @@ INLINE int rvb2ram_offs(int curr, int space, int iOff) //////////////////////////////////////////////////////////////////////// // portions based on spu2-x from PCSX2 -static void MixREVERB(int ns_to) +static void MixREVERB(int *SSumLR, int *RVB, int ns_to) { int l_old = rvb.iRVBLeft; int r_old = rvb.iRVBRight; @@ -87,8 +78,8 @@ static void MixREVERB(int ns_to) int ACC0, ACC1, FB_A0, FB_A1, FB_B0, FB_B1; int mix_dest_a0, mix_dest_a1, mix_dest_b0, mix_dest_b1; - int input_L = spu.sRVBStart[ns] * rvb.IN_COEF_L; - int input_R = spu.sRVBStart[ns+1] * rvb.IN_COEF_R; + int input_L = RVB[ns] * rvb.IN_COEF_L; + int input_R = RVB[ns+1] * rvb.IN_COEF_R; int IIR_INPUT_A0 = ((g_buffer(IIR_SRC_A0) * rvb.IIR_COEF) + input_L) >> 15; int IIR_INPUT_A1 = ((g_buffer(IIR_SRC_A1) * rvb.IIR_COEF) + input_R) >> 15; @@ -158,7 +149,7 @@ static void MixREVERB(int ns_to) rvb.CurrAddr = curr_addr; } -static void MixREVERB_off(int ns_to) +static void MixREVERB_off(int *SSumLR, int ns_to) { int l_old = rvb.iRVBLeft; int r_old = rvb.iRVBRight; @@ -236,7 +227,7 @@ static void prepare_offsets(void) rvb.dirty = 0; } -INLINE void REVERBDo(int ns_to) +INLINE void REVERBDo(int *SSumLR, int *RVB, int ns_to) { if (!rvb.StartAddr) // reverb is off { @@ -249,14 +240,14 @@ INLINE void REVERBDo(int ns_to) if (unlikely(rvb.dirty)) prepare_offsets(); - MixREVERB(ns_to); + MixREVERB(SSumLR, RVB, ns_to); } else if (rvb.VolLeft || rvb.VolRight) { if (unlikely(rvb.dirty)) prepare_offsets(); - MixREVERB_off(ns_to); + MixREVERB_off(SSumLR, ns_to); } else // -> reverb off { diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c index c6a06fd..f5edd3a 100644 --- a/plugins/dfsound/spu.c +++ b/plugins/dfsound/spu.c @@ -77,47 +77,8 @@ SPUConfig spu_config; REVERBInfo rvb; -#if defined(THREAD_ENABLED) || defined(WANT_THREAD_CODE) - -// worker thread state -static struct spu_worker { - unsigned int pending:1; - unsigned int exit_thread:1; - unsigned int stale_cache:1; - int ns_to; - int ctrl; - int decode_pos; - int silentch; - unsigned int chmask; - struct { - int spos; - int sbpos; - int sinc; - int start; - int loop; - int ns_to; - ADSRInfoEx adsr; - // might want to add vol and fmod flags.. - } ch[24]; - struct { - struct { - int adsrState; - int adsrEnvelopeVol; - } ch[24]; - unsigned int chan_end; - unsigned int decode_dirty; - } r; -} *worker; - -#else -static const void * const worker = NULL; -#endif - -// certain globals (were local before, but with the new timeproc I need em global) - static int iFMod[NSSIZE]; int ChanBuf[NSSIZE]; -int *SSumLR; #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2 @@ -267,17 +228,8 @@ static int check_irq(int ch, unsigned char *pos) // START SOUND... called by main thread to setup a new sound on a channel //////////////////////////////////////////////////////////////////////// -INLINE void StartSound(int ch) +static void StartSoundSB(int *SB) { - SPUCHAN *s_chan = &spu.s_chan[ch]; - int *SB = spu.SB + ch * SB_SIZE; - - StartADSR(ch); - StartREVERB(ch); - - s_chan->prevflags=2; - - s_chan->iSBPos=27; SB[26]=0; // init mixing vars SB[27]=0; @@ -285,6 +237,17 @@ INLINE void StartSound(int ch) SB[29]=0; // init our interpolation helpers SB[30]=0; SB[31]=0; +} + +static void StartSoundMain(int ch) +{ + SPUCHAN *s_chan = &spu.s_chan[ch]; + + StartADSR(ch); + StartREVERB(ch); + + s_chan->prevflags=2; + s_chan->iSBPos=27; s_chan->spos=0; spu.dwNewChannel&=~(1<ch[ch].start; - int loop = worker->ch[ch].loop; - - predict_nr = ram[start]; - shift_factor = predict_nr & 0xf; - predict_nr >>= 4; - - decode_block_data(SB, ram + start + 2, predict_nr, shift_factor); - - flags = ram[start + 1]; - if (flags & 4) - loop = start; // loop adress - - start += 16; - - if (flags & 1) // 1: stop/loop - start = loop; - - worker->ch[ch].start = start & 0x7ffff; - worker->ch[ch].loop = loop; - - return 0; -} - -#endif - // if irq is going to trigger sooner than in upd_samples, set upd_samples static void scan_for_irq(int ch, unsigned int *upd_samples) { @@ -594,8 +531,9 @@ static void scan_for_irq(int ch, unsigned int *upd_samples) } #define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \ -static noinline int do_samples_##name(int (*decode_f)(int ch, int *SB), int ch, \ - int ns_to, int *SB, int sinc, int *spos, int *sbpos) \ +static noinline int do_samples_##name( \ + int (*decode_f)(void *context, int ch, int *SB), void *ctx, \ + int ch, int ns_to, int *SB, int sinc, int *spos, int *sbpos) \ { \ int ns, d, fa; \ int ret = ns_to; \ @@ -612,7 +550,7 @@ static noinline int do_samples_##name(int (*decode_f)(int ch, int *SB), int ch, if (*sbpos >= 28) \ { \ *sbpos = 0; \ - d = decode_f(ch, SB); \ + d = decode_f(ctx, ch, SB); \ if (d && ns < ret) \ ret = ns; \ } \ @@ -723,13 +661,12 @@ static int do_samples_noise(int ch, int ns_to) #ifdef HAVE_ARMV5 // asm code; lv and rv must be 0-3fff -extern void mix_chan(int start, int count, int lv, int rv); -extern void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb); +extern void mix_chan(int *SSumLR, int count, int lv, int rv); +extern void mix_chan_rvb(int *SSumLR, int count, int lv, int rv, int *rvb); #else -static void mix_chan(int start, int count, int lv, int rv) +static void mix_chan(int *SSumLR, int count, int lv, int rv) { - int *dst = SSumLR + start * 2; - const int *src = ChanBuf + start; + const int *src = ChanBuf; int l, r; while (count--) @@ -738,16 +675,16 @@ static void mix_chan(int start, int count, int lv, int rv) l = (sval * lv) >> 14; r = (sval * rv) >> 14; - *dst++ += l; - *dst++ += r; + *SSumLR++ += l; + *SSumLR++ += r; } } -static void mix_chan_rvb(int start, int count, int lv, int rv, int *rvb) +static void mix_chan_rvb(int *SSumLR, int count, int lv, int rv, int *rvb) { - int *dst = SSumLR + start * 2; - int *drvb = rvb + start * 2; - const int *src = ChanBuf + start; + const int *src = ChanBuf; + int *dst = SSumLR; + int *drvb = rvb; int l, r; while (count--) @@ -828,7 +765,13 @@ static void do_channels(int ns_to) int *SB, sinc; int ch, d; - InitREVERB(ns_to); + memset(spu.RVB, 0, ns_to * sizeof(spu.RVB[0]) * 2); + + mask = spu.dwNewChannel & 0xffffff; + for (ch = 0; mask != 0; ch++, mask >>= 1) { + if (mask & 1) + StartSound(ch); + } mask = spu.dwChannelOn & 0xffffff; for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all... @@ -843,13 +786,13 @@ static void do_channels(int ns_to) d = do_samples_noise(ch, ns_to); else if (s_chan->bFMod == 2 || (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 0)) - d = do_samples_noint(decode_block, ch, ns_to, + d = do_samples_noint(decode_block, NULL, ch, ns_to, SB, sinc, &s_chan->spos, &s_chan->iSBPos); else if (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 1) - d = do_samples_simple(decode_block, ch, ns_to, + d = do_samples_simple(decode_block, NULL, ch, ns_to, SB, sinc, &s_chan->spos, &s_chan->iSBPos); else - d = do_samples_default(decode_block, ch, ns_to, + d = do_samples_default(decode_block, NULL, ch, ns_to, SB, sinc, &s_chan->spos, &s_chan->iSBPos); d = MixADSR(&s_chan->ADSRX, d); @@ -868,150 +811,240 @@ static void do_channels(int ns_to) if (s_chan->bFMod == 2) // fmod freq channel memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); if (s_chan->bRVBActive) - mix_chan_rvb(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, spu.sRVBStart); + mix_chan_rvb(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, spu.RVB); else - mix_chan(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); + mix_chan(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); } } -static void do_samples_finish(int ns_to, int silentch, int decode_pos); +static void do_samples_finish(int *SSumLR, int *RVB, int ns_to, + int silentch, int decode_pos); // optional worker thread handling #if defined(THREAD_ENABLED) || defined(WANT_THREAD_CODE) +// worker thread state +static struct spu_worker { + union { + struct { + unsigned int exit_thread; + unsigned int i_ready; + unsigned int i_reaped; + unsigned int req_sent; // dsp + unsigned int last_boot_cnt; + }; + // aligning for C64X_DSP + unsigned int _pad0[128/4]; + }; + union { + struct { + unsigned int i_done; + unsigned int active; // dsp + unsigned int boot_cnt; + }; + unsigned int _pad1[128/4]; + }; + struct work_item { + int ns_to; + int ctrl; + int decode_pos; + unsigned int channels_new; + unsigned int channels_on; + unsigned int channels_silent; + struct { + int spos; + int sbpos; + int sinc; + int start; + int loop; + int ns_to; + ADSRInfoEx adsr; + // might want to add vol and fmod flags.. + } ch[24]; + int RVB[NSSIZE * 2]; + int SSumLR[NSSIZE * 2]; + } i[4]; +} *worker; + +#define WORK_MAXCNT (sizeof(worker->i) / sizeof(worker->i[0])) +#define WORK_I_MASK (WORK_MAXCNT - 1) + static void thread_work_start(void); -static void thread_work_wait_sync(void); -static void thread_sync_caches(void); +static void thread_work_wait_sync(struct work_item *work, int force); +static int thread_get_i_done(void); + +static int decode_block_work(void *context, int ch, int *SB) +{ + const unsigned char *ram = spu.spuMemC; + int predict_nr, shift_factor, flags; + struct work_item *work = context; + int start = work->ch[ch].start; + int loop = work->ch[ch].loop; + + predict_nr = ram[start]; + shift_factor = predict_nr & 0xf; + predict_nr >>= 4; + + decode_block_data(SB, ram + start + 2, predict_nr, shift_factor); + + flags = ram[start + 1]; + if (flags & 4) + loop = start; // loop adress + + start += 16; + + if (flags & 1) // 1: stop/loop + start = loop; + + work->ch[ch].start = start & 0x7ffff; + work->ch[ch].loop = loop; + + return 0; +} -static void queue_channel_work(int ns_to, int silentch) +static void queue_channel_work(int ns_to, unsigned int silentch) { - const SPUCHAN *s_chan; + struct work_item *work; + SPUCHAN *s_chan; unsigned int mask; - int ch; + int ch, d; - worker->ns_to = ns_to; - worker->ctrl = spu.spuCtrl; - worker->decode_pos = spu.decode_pos; - worker->silentch = silentch; + work = &worker->i[worker->i_ready & WORK_I_MASK]; + work->ns_to = ns_to; + work->ctrl = spu.spuCtrl; + work->decode_pos = spu.decode_pos; + work->channels_silent = silentch; + + mask = work->channels_new = spu.dwNewChannel & 0xffffff; + for (ch = 0; mask != 0; ch++, mask >>= 1) { + if (mask & 1) + StartSoundMain(ch); + } + + mask = work->channels_on = spu.dwChannelOn & 0xffffff; + spu.decode_dirty_ch |= mask & 0x0a; - mask = worker->chmask = spu.dwChannelOn & 0xffffff; for (ch = 0; mask != 0; ch++, mask >>= 1) { if (!(mask & 1)) continue; s_chan = &spu.s_chan[ch]; - worker->ch[ch].spos = s_chan->spos; - worker->ch[ch].sbpos = s_chan->iSBPos; - worker->ch[ch].sinc = s_chan->sinc; - worker->ch[ch].adsr = s_chan->ADSRX; - worker->ch[ch].start = s_chan->pCurr - spu.spuMemC; - worker->ch[ch].loop = s_chan->pLoop - spu.spuMemC; + work->ch[ch].spos = s_chan->spos; + work->ch[ch].sbpos = s_chan->iSBPos; + work->ch[ch].sinc = s_chan->sinc; + work->ch[ch].adsr = s_chan->ADSRX; + work->ch[ch].start = s_chan->pCurr - spu.spuMemC; + work->ch[ch].loop = s_chan->pLoop - spu.spuMemC; if (s_chan->prevflags & 1) - worker->ch[ch].start = worker->ch[ch].loop; + work->ch[ch].start = work->ch[ch].loop; - worker->ch[ch].ns_to = do_samples_skip(ch, ns_to); + d = do_samples_skip(ch, ns_to); + work->ch[ch].ns_to = d; + + // note: d is not accurate on skip + d = SkipADSR(&s_chan->ADSRX, d); + if (d < ns_to) { + spu.dwChannelOn &= ~(1 << ch); + s_chan->ADSRX.EnvelopeVol = 0; + } } - worker->pending = 1; + worker->i_ready++; thread_work_start(); } -static void do_channel_work(void) +static void do_channel_work(struct work_item *work) { - unsigned int mask, endmask = 0; + unsigned int mask; unsigned int decode_dirty_ch = 0; int *SB, sinc, spos, sbpos; int d, ch, ns_to; SPUCHAN *s_chan; - ns_to = worker->ns_to; - memset(spu.sRVBStart, 0, ns_to * sizeof(spu.sRVBStart[0]) * 2); + ns_to = work->ns_to; + memset(work->RVB, 0, ns_to * sizeof(work->RVB[0]) * 2); - mask = worker->chmask; + mask = work->channels_new; + for (ch = 0; mask != 0; ch++, mask >>= 1) { + if (mask & 1) + StartSoundSB(spu.SB + ch * SB_SIZE); + } + + mask = work->channels_on; for (ch = 0; mask != 0; ch++, mask >>= 1) { if (!(mask & 1)) continue; - d = worker->ch[ch].ns_to; - spos = worker->ch[ch].spos; - sbpos = worker->ch[ch].sbpos; - sinc = worker->ch[ch].sinc; + d = work->ch[ch].ns_to; + spos = work->ch[ch].spos; + sbpos = work->ch[ch].sbpos; + sinc = work->ch[ch].sinc; s_chan = &spu.s_chan[ch]; SB = spu.SB + ch * SB_SIZE; if (s_chan->bNoise) - do_lsfr_samples(d, worker->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal); + do_lsfr_samples(d, work->ctrl, &spu.dwNoiseCount, &spu.dwNoiseVal); else if (s_chan->bFMod == 2 || (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 0)) - do_samples_noint(decode_block_work, ch, d, SB, sinc, &spos, &sbpos); + do_samples_noint(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos); else if (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 1) - do_samples_simple(decode_block_work, ch, d, SB, sinc, &spos, &sbpos); + do_samples_simple(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos); else - do_samples_default(decode_block_work, ch, d, SB, sinc, &spos, &sbpos); + do_samples_default(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos); - d = MixADSR(&worker->ch[ch].adsr, d); + d = MixADSR(&work->ch[ch].adsr, d); if (d < ns_to) { - endmask |= 1 << ch; - worker->ch[ch].adsr.EnvelopeVol = 0; + work->ch[ch].adsr.EnvelopeVol = 0; memset(&ChanBuf[d], 0, (ns_to - d) * sizeof(ChanBuf[0])); } - worker->r.ch[ch].adsrState = worker->ch[ch].adsr.State; - worker->r.ch[ch].adsrEnvelopeVol = worker->ch[ch].adsr.EnvelopeVol; if (ch == 1 || ch == 3) { - do_decode_bufs(spu.spuMem, ch/2, ns_to, worker->decode_pos); + do_decode_bufs(spu.spuMem, ch/2, ns_to, work->decode_pos); decode_dirty_ch |= 1 << ch; } if (s_chan->bFMod == 2) // fmod freq channel memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); if (s_chan->bRVBActive) - mix_chan_rvb(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, spu.sRVBStart); + mix_chan_rvb(work->SSumLR, ns_to, + s_chan->iLeftVolume, s_chan->iRightVolume, work->RVB); else - mix_chan(0, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); + mix_chan(work->SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); } - - worker->r.chan_end = endmask; - worker->r.decode_dirty = decode_dirty_ch; } -static void sync_worker_thread(int do_direct) +static void sync_worker_thread(int force) { - unsigned int mask; - int ch; + struct work_item *work; + int done, used_space; - if (do_direct) - thread_sync_caches(); - if (!worker->pending) - return; + done = thread_get_i_done() - worker->i_reaped; + used_space = worker->i_ready - worker->i_reaped; + //printf("done: %d use: %d dsp: %u/%u\n", done, used_space, + // worker->boot_cnt, worker->i_done); - thread_work_wait_sync(); - worker->pending = 0; + while ((force && used_space > 0) || used_space >= WORK_MAXCNT || done > 0) { + work = &worker->i[worker->i_reaped & WORK_I_MASK]; + thread_work_wait_sync(work, force); - mask = worker->chmask; - for (ch = 0; mask != 0; ch++, mask >>= 1) { - if (!(mask & 1)) continue; + do_samples_finish(work->SSumLR, work->RVB, work->ns_to, + work->channels_silent, work->decode_pos); - // be sure there was no keyoff while thread was working - if (spu.s_chan[ch].ADSRX.State != ADSR_RELEASE) - spu.s_chan[ch].ADSRX.State = worker->r.ch[ch].adsrState; - spu.s_chan[ch].ADSRX.EnvelopeVol = worker->r.ch[ch].adsrEnvelopeVol; + worker->i_reaped++; + done = thread_get_i_done() - worker->i_reaped; + used_space = worker->i_ready - worker->i_reaped; } - - spu.dwChannelOn &= ~worker->r.chan_end; - spu.decode_dirty_ch |= worker->r.decode_dirty; - - do_samples_finish(worker->ns_to, worker->silentch, - worker->decode_pos); } #else static void queue_channel_work(int ns_to, int silentch) {} -static void sync_worker_thread(int do_direct) {} +static void sync_worker_thread(int force) {} + +static const void * const worker = NULL; #endif // THREAD_ENABLED @@ -1022,10 +1055,9 @@ static void sync_worker_thread(int do_direct) {} void do_samples(unsigned int cycles_to, int do_direct) { - unsigned int mask; - int ch, ns_to; - int silentch; + unsigned int silentch; int cycle_diff; + int ns_to; cycle_diff = cycles_to - spu.cycles_played; if (cycle_diff < -2*1048576 || cycle_diff > 2*1048576) @@ -1035,7 +1067,9 @@ void do_samples(unsigned int cycles_to, int do_direct) return; } - do_direct |= (cycle_diff < 64 * 768); + silentch = ~(spu.dwChannelOn | spu.dwNewChannel) & 0xffffff; + + do_direct |= (silentch == 0xffffff); if (worker != NULL) sync_worker_thread(do_direct); @@ -1078,26 +1112,12 @@ void do_samples(unsigned int cycles_to, int do_direct) } } - mask = spu.dwNewChannel & 0xffffff; - for (ch = 0; mask != 0; ch++, mask >>= 1) { - if (mask & 1) - StartSound(ch); - } - - silentch = ~spu.dwChannelOn & 0xffffff; - - if (spu.dwChannelOn == 0) { - InitREVERB(ns_to); - do_samples_finish(ns_to, silentch, spu.decode_pos); + if (do_direct || worker == NULL || !spu_config.iUseThread) { + do_channels(ns_to); + do_samples_finish(spu.SSumLR, spu.RVB, ns_to, silentch, spu.decode_pos); } else { - if (do_direct || worker == NULL || !spu_config.iUseThread) { - do_channels(ns_to); - do_samples_finish(ns_to, silentch, spu.decode_pos); - } - else { - queue_channel_work(ns_to, silentch); - } + queue_channel_work(ns_to, silentch); } // advance "stopped" channels that can cause irqs @@ -1109,13 +1129,15 @@ void do_samples(unsigned int cycles_to, int do_direct) spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff; } -static void do_samples_finish(int ns_to, int silentch, int decode_pos) +static void do_samples_finish(int *SSumLR, int *RVB, int ns_to, + int silentch, int decode_pos) { int volmult = spu_config.iVolume; int ns; int d; - if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) // must clear silent channel decode buffers + // must clear silent channel decode buffers + if(unlikely(silentch & spu.decode_dirty_ch & (1<<1))) { memset(&spu.spuMem[0x800/2], 0, 0x400); spu.decode_dirty_ch &= ~(1<<1); @@ -1129,13 +1151,13 @@ static void do_samples_finish(int ns_to, int silentch, int decode_pos) //---------------------------------------------------// // mix XA infos (if any) - MixXA(ns_to, decode_pos); + MixXA(SSumLR, ns_to, decode_pos); /////////////////////////////////////////////////////// // mix all channels (including reverb) into one buffer if(spu_config.iUseReverb) - REVERBDo(ns_to); + REVERBDo(SSumLR, RVB, ns_to); if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this) { @@ -1254,7 +1276,6 @@ int CALLBACK SPUplayCDDAchannel(short *pcm, int nbytes) // to be called after state load void ClearWorkingState(void) { - memset(SSumLR, 0, NSSIZE * 2 * 4); // init some mixing buffers memset(iFMod, 0, sizeof(iFMod)); spu.pS=(short *)spu.pSpuBuffer; // setup soundbuffer pointer } @@ -1265,8 +1286,8 @@ void SetupStreams(void) int i; spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer - spu.sRVBStart = calloc(NSSIZE * 2, sizeof(spu.sRVBStart[0])); - SSumLR = calloc(NSSIZE * 2, sizeof(SSumLR[0])); + spu.RVB = calloc(NSSIZE * 2, sizeof(spu.RVB[0])); + spu.SSumLR = calloc(NSSIZE * 2, sizeof(spu.SSumLR[0])); spu.XAStart = // alloc xa buffer (uint32_t *)malloc(44100 * sizeof(uint32_t)); @@ -1298,10 +1319,10 @@ void RemoveStreams(void) { free(spu.pSpuBuffer); // free mixing buffer spu.pSpuBuffer = NULL; - free(spu.sRVBStart); // free reverb buffer - spu.sRVBStart = NULL; - free(SSumLR); - SSumLR = NULL; + free(spu.RVB); // free reverb buffer + spu.RVB = NULL; + free(spu.SSumLR); + spu.SSumLR = NULL; free(spu.XAStart); // free XA buffer spu.XAStart = NULL; free(spu.CDDAStart); // free CDDA buffer @@ -1332,23 +1353,28 @@ static void thread_work_start(void) sem_post(&t.sem_avail); } -static void thread_work_wait_sync(void) +static void thread_work_wait_sync(struct work_item *work, int force) { sem_wait(&t.sem_done); } -static void thread_sync_caches(void) +static int thread_get_i_done(void) { + return worker->i_done; } static void *spu_worker_thread(void *unused) { + struct work_item *work; + while (1) { sem_wait(&t.sem_avail); if (worker->exit_thread) break; - do_channel_work(); + work = &worker->i[worker->i_done & WORK_I_MASK]; + do_channel_work(work); + worker->i_done++; sem_post(&t.sem_done); } @@ -1377,6 +1403,7 @@ static void init_spu_thread(void) if (ret != 0) goto fail_thread; + spu_config.iThreadAvail = 1; return; fail_thread: @@ -1386,6 +1413,7 @@ fail_sem_done: fail_sem_avail: free(worker); worker = NULL; + spu_config.iThreadAvail = 0; } static void exit_spu_thread(void) diff --git a/plugins/dfsound/spu_c64x.c b/plugins/dfsound/spu_c64x.c index d829d29..200ab38 100644 --- a/plugins/dfsound/spu_c64x.c +++ b/plugins/dfsound/spu_c64x.c @@ -23,13 +23,11 @@ #include #include +#include #include #include "spu_c64x.h" -static dsp_mem_region_t region; -static dsp_component_id_t compid; - static struct { void *handle; int (*dsp_open)(void); @@ -42,49 +40,114 @@ static struct { int (*dsp_rpc_recv)(dsp_msg_t *_msgFrom); int (*dsp_rpc)(const dsp_msg_t *_msgTo, dsp_msg_t *_msgFrom); void (*dsp_logbuf_print)(void); + + dsp_mem_region_t region; + dsp_component_id_t compid; } f; static void thread_work_start(void) { + struct region_mem *mem; dsp_msg_t msg; int ret; - DSP_MSG_INIT(&msg, compid, CCMD_DOIT, 0, 0); + // make sure new work is written out + __sync_synchronize(); + + // this should be safe, as dsp checks for new work even + // after it decrements ->active + // cacheline: i_done, active + f.dsp_cache_inv_virt(&worker->i_done, 64); + if (worker->active == ACTIVE_CNT) + return; + + // to start the DSP, dsp_rpc_send() must be used, + // but before that, previous request must be finished + if (worker->req_sent) { + if (worker->boot_cnt == worker->last_boot_cnt) { + // hopefully still booting + //printf("booting?\n"); + return; + } + + ret = f.dsp_rpc_recv(&msg); + if (ret != 0) { + fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret); + f.dsp_logbuf_print(); + worker->req_sent = 0; + spu_config.iUseThread = 0; + return; + } + } + + f.dsp_cache_inv_virt(&worker->i_done, 64); + worker->last_boot_cnt = worker->boot_cnt; + + mem = (void *)f.region.virt_addr; + memcpy(&mem->spu_config, &spu_config, sizeof(mem->spu_config)); + + DSP_MSG_INIT(&msg, f.compid, CCMD_DOIT, f.region.phys_addr, 0); ret = f.dsp_rpc_send(&msg); if (ret != 0) { fprintf(stderr, "dsp_rpc_send failed: %d\n", ret); f.dsp_logbuf_print(); - // maybe stop using the DSP? + spu_config.iUseThread = 0; + return; } + worker->req_sent = 1; } -static void thread_work_wait_sync(void) +static int thread_get_i_done(void) { - dsp_msg_t msg; + f.dsp_cache_inv_virt(&worker->i_done, sizeof(worker->i_done)); + return worker->i_done; +} + +static void thread_work_wait_sync(struct work_item *work, int force) +{ + int limit = 1000; int ns_to; - int ret; - ns_to = worker->ns_to; - f.dsp_cache_inv_virt(spu.sRVBStart, sizeof(spu.sRVBStart[0]) * 2 * ns_to); - f.dsp_cache_inv_virt(SSumLR, sizeof(SSumLR[0]) * 2 * ns_to); - f.dsp_cache_inv_virt(&worker->r, sizeof(worker->r)); - worker->stale_cache = 1; // SB, ram + ns_to = work->ns_to; + f.dsp_cache_inv_virt(work->RVB, sizeof(work->RVB[0]) * 2 * ns_to); + f.dsp_cache_inv_virt(work->SSumLR, sizeof(work->SSumLR[0]) * 2 * ns_to); + __builtin_prefetch(work->RVB); + __builtin_prefetch(work->SSumLR); - ret = f.dsp_rpc_recv(&msg); - if (ret != 0) { - fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret); - f.dsp_logbuf_print(); + while (worker->i_done == worker->i_reaped && limit-- > 0) { + if (!worker->active) { + printf("dsp: broken sync\n"); + worker->last_boot_cnt = ~0; + break; + } + + usleep(500); + f.dsp_cache_inv_virt(&worker->i_done, 64); } - //f.dsp_logbuf_print(); -} -// called before ARM decides to do SPU mixing itself -static void thread_sync_caches(void) -{ - if (worker->stale_cache) { + if (limit == 0) + printf("dsp: wait timeout\n"); + + // still in results loop? + if (worker->i_reaped != worker->i_done - 1) + return; + + if (worker->req_sent && (force || worker->i_done == worker->i_ready)) { + dsp_msg_t msg; + int ret; + + ret = f.dsp_rpc_recv(&msg); + if (ret != 0) { + fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret); + f.dsp_logbuf_print(); + spu_config.iUseThread = 0; + } + worker->req_sent = 0; + } + + if (force) { f.dsp_cache_inv_virt(spu.SB, sizeof(spu.SB[0]) * SB_SIZE * 24); f.dsp_cache_inv_virt(spu.spuMemC + 0x800, 0x800); - worker->stale_cache = 0; } } @@ -101,7 +164,7 @@ static void init_spu_thread(void) f.handle = dlopen(lib, RTLD_NOW); if (f.handle == NULL) { fprintf(stderr, "can't load %s: %s\n", lib, dlerror()); - return; + goto fail_open; } #define LDS(name) \ failed |= (f.name = dlsym(f.handle, #name)) == NULL @@ -120,32 +183,32 @@ static void init_spu_thread(void) fprintf(stderr, "missing symbol(s) in %s\n", lib); dlclose(f.handle); f.handle = NULL; - return; + goto fail_open; } } ret = f.dsp_open(); if (ret != 0) { fprintf(stderr, "dsp_open failed: %d\n", ret); - return; + goto fail_open; } - ret = f.dsp_component_load(NULL, COMPONENT_NAME, &compid); + ret = f.dsp_component_load(NULL, COMPONENT_NAME, &f.compid); if (ret != 0) { fprintf(stderr, "dsp_component_load failed: %d\n", ret); goto fail_cload; } - region = f.dsp_shm_alloc(DSP_CACHE_R, sizeof(*mem)); // writethrough - if (region.size < sizeof(*mem) || region.virt_addr == 0) { + f.region = f.dsp_shm_alloc(DSP_CACHE_R, sizeof(*mem)); // writethrough + if (f.region.size < sizeof(*mem) || f.region.virt_addr == 0) { fprintf(stderr, "dsp_shm_alloc failed\n"); goto fail_mem; } - mem = (void *)region.virt_addr; + mem = (void *)f.region.virt_addr; memcpy(&mem->spu_config, &spu_config, sizeof(mem->spu_config)); - DSP_MSG_INIT(&init_msg, compid, CCMD_INIT, region.phys_addr, 0); + DSP_MSG_INIT(&init_msg, f.compid, CCMD_INIT, f.region.phys_addr, 0); ret = f.dsp_rpc(&init_msg, &msg_in); if (ret != 0) { fprintf(stderr, "dsp_rpc failed: %d\n", ret); @@ -162,56 +225,56 @@ static void init_spu_thread(void) mem->offsetof_s_chan1, offsetof(typeof(*mem), s_chan[1])); goto fail_init; } - if (mem->offsetof_worker_ram != offsetof(typeof(*mem), worker.ch[1])) { + if (mem->offsetof_spos_3_20 != offsetof(typeof(*mem), worker.i[3].ch[20])) { fprintf(stderr, "error: size mismatch 3: %d vs %zd\n", - mem->offsetof_worker_ram, offsetof(typeof(*mem), worker.ch[1])); + mem->offsetof_spos_3_20, offsetof(typeof(*mem), worker.i[3].ch[20])); goto fail_init; } // override default allocations free(spu.spuMemC); spu.spuMemC = mem->spu_ram; - free(spu.sRVBStart); - spu.sRVBStart = mem->RVB; - free(SSumLR); - SSumLR = mem->SSumLR; free(spu.SB); spu.SB = mem->SB; free(spu.s_chan); spu.s_chan = mem->s_chan; worker = &mem->worker; - printf("spu: C64x DSP ready (id=%d).\n", (int)compid); + printf("spu: C64x DSP ready (id=%d).\n", (int)f.compid); f.dsp_logbuf_print(); -pcnt_init(); + spu_config.iThreadAvail = 1; (void)do_channel_work; // used by DSP instead return; fail_init: - f.dsp_shm_free(region); + f.dsp_shm_free(f.region); fail_mem: // no component unload func? fail_cload: - printf("spu: C64x DSP init failed.\n"); f.dsp_logbuf_print(); f.dsp_close(); +fail_open: + printf("spu: C64x DSP init failed.\n"); + spu_config.iUseThread = spu_config.iThreadAvail = 0; worker = NULL; } static void exit_spu_thread(void) { + dsp_msg_t msg; + if (worker == NULL) return; - if (worker->pending) - thread_work_wait_sync(); - f.dsp_shm_free(region); + if (worker->req_sent) + f.dsp_rpc_recv(&msg); + + f.dsp_logbuf_print(); + f.dsp_shm_free(f.region); f.dsp_close(); spu.spuMemC = NULL; - spu.sRVBStart = NULL; - SSumLR = NULL; spu.SB = NULL; spu.s_chan = NULL; worker = NULL; diff --git a/plugins/dfsound/spu_c64x.h b/plugins/dfsound/spu_c64x.h index d4e73e9..bb20cc3 100644 --- a/plugins/dfsound/spu_c64x.h +++ b/plugins/dfsound/spu_c64x.h @@ -7,12 +7,10 @@ enum { struct region_mem { unsigned char spu_ram[512 * 1024]; - int RVB[NSSIZE * 2]; - int SSumLR[NSSIZE * 2]; int SB[SB_SIZE * 24]; // careful not to lose ARM writes by DSP overwriting // with old data when it's writing out neighbor cachelines - int _pad1[128/4 - ((NSSIZE * 4 + SB_SIZE * 24) & (128/4 - 1))]; + int _pad1[128/4 - ((SB_SIZE * 24) & (128/4 - 1))]; SPUCHAN s_chan[24 + 1]; int _pad2[128/4 - ((sizeof(SPUCHAN) * 25 / 4) & (128/4 - 1))]; struct spu_worker worker; @@ -20,6 +18,7 @@ struct region_mem { // init/debug int sizeof_region_mem; int offsetof_s_chan1; - int offsetof_worker_ram; + int offsetof_spos_3_20; }; +#define ACTIVE_CNT 3 diff --git a/plugins/dfsound/spu_c64x_dspcode.c b/plugins/dfsound/spu_c64x_dspcode.c index 117a296..97d3028 100644 --- a/plugins/dfsound/spu_c64x_dspcode.c +++ b/plugins/dfsound/spu_c64x_dspcode.c @@ -30,38 +30,80 @@ /* dummy deps, some bloat but avoids ifdef hell in SPU code.. */ static void thread_work_start(void) {} -static void thread_work_wait_sync(void) {} -static void thread_sync_caches(void) {} +static void thread_work_wait_sync(struct work_item *work, int force) {} +static int thread_get_i_done(void) { return 0; } struct out_driver *out_current; void SetupSound(void) {} -#if 0 -// no use, c64_tools does BCACHE_wbInvAll.. -static void sync_caches(void) + +static void invalidate_cache(struct work_item *work) +{ + syscalls.cache_inv(work, offsetof(typeof(*work), RVB), 1); + syscalls.cache_inv(spu.s_chan, sizeof(spu.s_chan[0]) * 24, 0); + syscalls.cache_inv(work->SSumLR, + sizeof(work->SSumLR[0]) * 2 * work->ns_to, 0); +} + +static void writeout_cache(struct work_item *work) { - int ns_to = worker->ns_to; + int ns_to = work->ns_to; + + syscalls.cache_wb(work->RVB, sizeof(work->RVB[0]) * 2 * ns_to, 1); + syscalls.cache_wb(work->SSumLR, sizeof(work->SSumLR[0]) * 2 * ns_to, 1); +} - syscalls.cache_wb(spu.sRVBStart, sizeof(spu.sRVBStart[0]) * 2 * ns_to, 1); - syscalls.cache_wb(SSumLR, sizeof(SSumLR[0]) * 2 * ns_to, 1); +static void do_processing(void) +{ + struct work_item *work; + int left, dirty = 0; - syscalls.cache_wbInv(worker, sizeof(*worker), 1); + while (worker->active) + { + // i_ready is in first cacheline + syscalls.cache_inv(worker, 64, 1); + + left = worker->i_ready - worker->i_done; + if (left > 0) { + dirty = 1; + worker->active = ACTIVE_CNT; + syscalls.cache_wb(&worker->active, 4, 1); + + work = &worker->i[worker->i_done & WORK_I_MASK]; + invalidate_cache(work); + do_channel_work(work); + writeout_cache(work); + + worker->i_done++; + syscalls.cache_wb(&worker->i_done, 4, 1); + continue; + } + + // nothing to do? Write out non-critical caches + if (dirty) { + syscalls.cache_wb(spu.spuMemC + 0x800, 0x800, 1); + syscalls.cache_wb(spu.SB, sizeof(spu.SB[0]) * SB_SIZE * 24, 1); + dirty = 0; + continue; + } + + // this ->active loop thing is to avoid a race where we miss + // new work and clear ->active just after ARM checks it + worker->active--; + syscalls.cache_wb(&worker->active, 4, 1); + } } -#endif static unsigned int exec(dsp_component_cmd_t cmd, unsigned int arg1, unsigned int arg2, unsigned int *ret1, unsigned int *ret2) { struct region_mem *mem = (void *)arg1; - int i; switch (cmd) { case CCMD_INIT: InitADSR(); spu.spuMemC = mem->spu_ram; - spu.sRVBStart = mem->RVB; - SSumLR = mem->SSumLR; spu.SB = mem->SB; spu.s_chan = mem->s_chan; worker = &mem->worker; @@ -69,18 +111,22 @@ static unsigned int exec(dsp_component_cmd_t cmd, mem->sizeof_region_mem = sizeof(*mem); mem->offsetof_s_chan1 = offsetof(typeof(*mem), s_chan[1]); - mem->offsetof_worker_ram = offsetof(typeof(*mem), worker.ch[1]); + mem->offsetof_spos_3_20 = offsetof(typeof(*mem), worker.i[3].ch[20]); // seems to be unneeded, no write-alloc? but just in case.. syscalls.cache_wb(&mem->sizeof_region_mem, 3 * 4, 1); break; case CCMD_DOIT: - do_channel_work(); + worker->active = ACTIVE_CNT; + worker->boot_cnt++; + syscalls.cache_wb(&worker->i_done, 64, 1); + memcpy(&spu_config, &mem->spu_config, sizeof(spu_config)); + + do_processing(); + // c64_tools lib does BCACHE_wbInvAll() when it receives mailbox irq, - // so there is no benefit of syncing only what's needed. - // But call wbInvAll() anyway in case c64_tools is ever fixed.. - //sync_caches(); - syscalls.cache_wbInvAll(); + // but invalidate anyway in case c64_tools is ever fixed.. + syscalls.cache_inv(mem, sizeof(mem->spu_ram) + sizeof(mem->SB), 0); break; default: diff --git a/plugins/dfsound/spu_config.h b/plugins/dfsound/spu_config.h index 9a139e1..fce1cda 100644 --- a/plugins/dfsound/spu_config.h +++ b/plugins/dfsound/spu_config.h @@ -8,6 +8,9 @@ typedef struct int iUseInterpolation; int iTempo; int iUseThread; + + // status + int iThreadAvail; } SPUConfig; extern SPUConfig spu_config; diff --git a/plugins/dfsound/xa.c b/plugins/dfsound/xa.c index 6c0ce4b..ad7e824 100644 --- a/plugins/dfsound/xa.c +++ b/plugins/dfsound/xa.c @@ -38,7 +38,7 @@ static int gauss_window[8] = {0, 0, 0, 0, 0, 0, 0, 0}; // MIX XA & CDDA //////////////////////////////////////////////////////////////////////// -INLINE void MixXA(int ns_to, int decode_pos) +INLINE void MixXA(int *SSumLR, int ns_to, int decode_pos) { int cursor = decode_pos; int ns; -- cgit v1.2.3 From e541b8e06ff03a7fe7bd75b1bc0ad53f60b2d0de Mon Sep 17 00:00:00 2001 From: notaz Date: Mon, 5 Jan 2015 23:50:33 +0200 Subject: spu: don't block on audio --- plugins/dfsound/alsa.c | 15 ++++++++++++++- plugins/dfsound/oss.c | 14 +++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) (limited to 'plugins') diff --git a/plugins/dfsound/alsa.c b/plugins/dfsound/alsa.c index b5655a9..6b9f83e 100644 --- a/plugins/dfsound/alsa.c +++ b/plugins/dfsound/alsa.c @@ -195,8 +195,21 @@ static void alsa_feed(void *pSound, int lBytes) snd_pcm_prepare(handle); snd_pcm_writei(handle, sbuf, sizeof(sbuf) / 4); snd_pcm_writei(handle, sbuf, sizeof(sbuf) / 4); + snd_pcm_writei(handle, sbuf, sizeof(sbuf) / 4); + } + else + { + int l = snd_pcm_avail(handle); + if (l < lBytes / 4) + { + if (l == 0) + return; + + lBytes = l * 4; + } } - snd_pcm_writei(handle,pSound, lBytes / 4); + + snd_pcm_writei(handle, pSound, lBytes / 4); } void out_register_alsa(struct out_driver *drv) diff --git a/plugins/dfsound/oss.c b/plugins/dfsound/oss.c index 8093ae4..6b1cb4a 100644 --- a/plugins/dfsound/oss.c +++ b/plugins/dfsound/oss.c @@ -34,6 +34,9 @@ #define OSS_SPEED_44100 44100 +#define FRAGMENT_SHIFT 12 +#define FRAGMENT_SIZE (1 << FRAGMENT_SHIFT) + static int oss_audio_fd = -1; extern int errno; @@ -46,7 +49,6 @@ static int oss_init(void) int pspeed=44100; int pstereo; int format; - int fragsize = 0; int myfrag; int oss_speed, oss_stereo; @@ -69,8 +71,7 @@ static int oss_init(void) // we use 64 fragments with 1024 bytes each // rearmed: now using 10*4096 for better latency - fragsize=12; - myfrag=(10<<16)|fragsize; + myfrag = (10<<16) | FRAGMENT_SHIFT; if(ioctl(oss_audio_fd,SNDCTL_DSP_SETFRAGMENT,&myfrag)==-1) { @@ -160,11 +161,18 @@ static void oss_feed(void *buf, int bytes) if(oss_audio_fd == -1) return; if(ioctl(oss_audio_fd,SNDCTL_DSP_GETOSPACE,&info)==0) { + // for fast forward + if(bytes > info.fragments * FRAGMENT_SIZE) + bytes = info.fragments * FRAGMENT_SIZE; + if(bytes == 0) + return; + if(info.fragments==info.fragstotal) { memset(sbuf, 0, sizeof(sbuf)); write(oss_audio_fd, sbuf, sizeof(sbuf)); write(oss_audio_fd, sbuf, sizeof(sbuf)); + write(oss_audio_fd, sbuf, sizeof(sbuf)); } } -- cgit v1.2.3 From c632283d5c48d7731ec5704c3d5eef54951fec20 Mon Sep 17 00:00:00 2001 From: notaz Date: Mon, 12 Jan 2015 03:00:12 +0200 Subject: spu: fix some skip accuracy issues --- plugins/dfsound/adsr.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'plugins') diff --git a/plugins/dfsound/adsr.c b/plugins/dfsound/adsr.c index 69e55bd..9e32862 100644 --- a/plugins/dfsound/adsr.c +++ b/plugins/dfsound/adsr.c @@ -221,6 +221,7 @@ static int SkipADSR(ADSRInfoEx *adsr, int ns_to) { int EnvelopeVol = adsr->EnvelopeVol; int ns = 0, val, rto, level; + int64_t v64; if (adsr->State == ADSR_RELEASE) { @@ -236,8 +237,10 @@ static int SkipADSR(ADSRInfoEx *adsr, int ns_to) } else { - EnvelopeVol += val * ns_to; - if (EnvelopeVol > 0) + v64 = EnvelopeVol; + v64 += (int64_t)val * ns_to; + EnvelopeVol = (int)v64; + if (v64 > 0) ns = ns_to; } goto done; @@ -293,24 +296,21 @@ static int SkipADSR(ADSRInfoEx *adsr, int ns_to) case ADSR_SUSTAIN: // -> sustain if (adsr->SustainIncrease) { + ns = ns_to; + if (EnvelopeVol >= 0x7fff0000) - { - ns = ns_to; break; - } rto = 0; if (adsr->SustainModeExp && EnvelopeVol >= 0x60000000) rto = 8; val = RateTableAdd[adsr->SustainRate + rto]; - EnvelopeVol += val * (ns_to - ns); - if ((unsigned int)EnvelopeVol >= 0x7fe00000) - { + v64 = EnvelopeVol; + v64 += (int64_t)val * (ns_to - ns); + EnvelopeVol = (int)v64; + if (v64 >= 0x7fe00000ll) EnvelopeVol = 0x7fffffff; - ns = ns_to; - break; - } } else { @@ -326,8 +326,10 @@ static int SkipADSR(ADSRInfoEx *adsr, int ns_to) } else { - EnvelopeVol += val * (ns_to - ns); - if (EnvelopeVol > 0) + v64 = EnvelopeVol; + v64 += (int64_t)val * (ns_to - ns); + EnvelopeVol = (int)v64; + if (v64 > 0) { ns = ns_to; break; -- cgit v1.2.3 From 8f5f2dd5a70f47322614eda6f97304808447199c Mon Sep 17 00:00:00 2001 From: notaz Date: Sat, 10 Jan 2015 03:51:44 +0200 Subject: some random improvements --- plugins/dfsound/spu.c | 14 ++++++++------ plugins/gpulib/gpu.c | 12 ++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) (limited to 'plugins') diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c index f5edd3a..ec31b0c 100644 --- a/plugins/dfsound/spu.c +++ b/plugins/dfsound/spu.c @@ -595,24 +595,26 @@ make_do_samples(simple, , , static int do_samples_skip(int ch, int ns_to) { SPUCHAN *s_chan = &spu.s_chan[ch]; + int spos = s_chan->spos; + int sinc = s_chan->sinc; int ret = ns_to, ns, d; - s_chan->spos += s_chan->iSBPos << 16; + spos += s_chan->iSBPos << 16; for (ns = 0; ns < ns_to; ns++) { - s_chan->spos += s_chan->sinc; - while (s_chan->spos >= 28*0x10000) + spos += sinc; + while (spos >= 28*0x10000) { d = skip_block(ch); if (d && ns < ret) ret = ns; - s_chan->spos -= 28*0x10000; + spos -= 28*0x10000; } } - s_chan->iSBPos = s_chan->spos >> 16; - s_chan->spos &= 0xffff; + s_chan->iSBPos = spos >> 16; + s_chan->spos = spos & 0xffff; return ret; } diff --git a/plugins/gpulib/gpu.c b/plugins/gpulib/gpu.c index 337e27a..c9b05d4 100644 --- a/plugins/gpulib/gpu.c +++ b/plugins/gpulib/gpu.c @@ -13,8 +13,16 @@ #include "gpu.h" #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) +#ifdef __GNUC__ #define unlikely(x) __builtin_expect((x), 0) +#define preload __builtin_prefetch #define noinline __attribute__((noinline)) +#else +#define unlikely(x) +#define preload(...) +#define noinline +#error huh +#endif #define gpu_log(fmt, ...) \ printf("%d:%03d: " fmt, *gpu.state.frame_count, *gpu.state.hcnt, ##__VA_ARGS__) @@ -518,6 +526,8 @@ long GPUdmaChain(uint32_t *rambase, uint32_t start_addr) int len, left, count; long cpu_cycles = 0; + preload(rambase + (start_addr & 0x1fffff) / 4); + if (unlikely(gpu.cmd_len > 0)) flush_cmd_buffer(); @@ -537,6 +547,8 @@ long GPUdmaChain(uint32_t *rambase, uint32_t start_addr) list = rambase + (addr & 0x1fffff) / 4; len = list[0] >> 24; addr = list[0] & 0xffffff; + preload(rambase + (addr & 0x1fffff) / 4); + cpu_cycles += 10; if (len > 0) cpu_cycles += 5 + len; -- cgit v1.2.3 From 09159d99cd07a084d8643f87c6c23e86c79060ff Mon Sep 17 00:00:00 2001 From: notaz Date: Sat, 10 Jan 2015 21:54:34 +0200 Subject: gpulib: use more conservative loop detection the old one was causing too many cache misses --- plugins/gpulib/gpu.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'plugins') diff --git a/plugins/gpulib/gpu.c b/plugins/gpulib/gpu.c index c9b05d4..0e6fe63 100644 --- a/plugins/gpulib/gpu.c +++ b/plugins/gpulib/gpu.c @@ -521,7 +521,7 @@ void GPUwriteData(uint32_t data) long GPUdmaChain(uint32_t *rambase, uint32_t start_addr) { - uint32_t addr, *list; + uint32_t addr, *list, ld_addr = 0; uint32_t *llist_entry = NULL; int len, left, count; long cpu_cycles = 0; @@ -542,7 +542,7 @@ long GPUdmaChain(uint32_t *rambase, uint32_t start_addr) log_io("gpu_dma_chain\n"); addr = start_addr & 0xffffff; - for (count = 0; addr != 0xffffff; count++) + for (count = 0; (addr & 0x800000) == 0; count++) { list = rambase + (addr & 0x1fffff) / 4; len = list[0] >> 24; @@ -555,28 +555,37 @@ long GPUdmaChain(uint32_t *rambase, uint32_t start_addr) log_io(".chain %08x #%d\n", (list - rambase) * 4, len); - // loop detection marker - // (bit23 set causes DMA error on real machine, so - // unlikely to be ever set by the game) - list[0] |= 0x800000; - if (len) { left = do_cmd_buffer(list + 1, len); if (left) log_anomaly("GPUdmaChain: discarded %d/%d words\n", left, len); } - if (addr & 0x800000) - break; + #define LD_THRESHOLD (8*1024) + if (count >= LD_THRESHOLD) { + if (count == LD_THRESHOLD) { + ld_addr = addr; + continue; + } + + // loop detection marker + // (bit23 set causes DMA error on real machine, so + // unlikely to be ever set by the game) + list[0] |= 0x800000; + } } - // remove loop detection markers - addr = start_addr & 0x1fffff; - while (count-- > 0) { - list = rambase + addr / 4; - addr = list[0] & 0x1fffff; - list[0] &= ~0x800000; + if (ld_addr != 0) { + // remove loop detection markers + count -= LD_THRESHOLD + 2; + addr = ld_addr & 0x1fffff; + while (count-- > 0) { + list = rambase + addr / 4; + addr = list[0] & 0x1fffff; + list[0] &= ~0x800000; + } } + if (llist_entry) *llist_entry &= ~0x800000; -- cgit v1.2.3 From 05c7cec77522f04857f655474574469a5e66661d Mon Sep 17 00:00:00 2001 From: notaz Date: Sun, 11 Jan 2015 03:35:32 +0200 Subject: spu: put reverb on the thread too and more stuff to work struct --- plugins/dfsound/externals.h | 13 ++-- plugins/dfsound/registers.c | 78 ++++++++++---------- plugins/dfsound/reverb.c | 147 +++++++++++++++---------------------- plugins/dfsound/spu.c | 124 +++++++++++++++++++------------ plugins/dfsound/spu_c64x.c | 55 +++++++++----- plugins/dfsound/spu_c64x.h | 8 +- plugins/dfsound/spu_c64x_dspcode.c | 26 +++++-- 7 files changed, 247 insertions(+), 204 deletions(-) (limited to 'plugins') diff --git a/plugins/dfsound/externals.h b/plugins/dfsound/externals.h index 4832fac..f6fc440 100644 --- a/plugins/dfsound/externals.h +++ b/plugins/dfsound/externals.h @@ -28,6 +28,11 @@ #define noinline #define unlikely(x) x #endif +#if defined(__GNUC__) && !defined(_TMS320C6X) +#define preload __builtin_prefetch +#else +#define preload(...) +#endif #define PSE_LT_SPU 4 #define PSE_SPU_ERR_SUCCESS 0 @@ -121,8 +126,6 @@ typedef struct int VolLeft; int VolRight; - int iRVBLeft; - int iRVBRight; int FB_SRC_A; // (offset) int FB_SRC_B; // (offset) @@ -224,9 +227,10 @@ typedef struct int iRightXAVol; SPUCHAN * s_chan; - int * SB; + REVERBInfo * rvb; - int * RVB; + // buffers + int * SB; int * SSumLR; int pad[29]; @@ -240,7 +244,6 @@ typedef struct #ifndef _IN_SPU extern SPUInfo spu; -extern REVERBInfo rvb; void do_samples(unsigned int cycles_to, int do_sync); void schedule_next_irq(void); diff --git a/plugins/dfsound/registers.c b/plugins/dfsound/registers.c index 7a49b1c..4588fa7 100644 --- a/plugins/dfsound/registers.c +++ b/plugins/dfsound/registers.c @@ -146,14 +146,14 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val, //-------------------------------------------------// case H_SPUReverbAddr: if(val==0xFFFF || val<=0x200) - {rvb.StartAddr=rvb.CurrAddr=0;} + {spu.rvb->StartAddr=spu.rvb->CurrAddr=0;} else { const long iv=(unsigned long)val<<2; - if(rvb.StartAddr!=iv) + if(spu.rvb->StartAddr!=iv) { - rvb.StartAddr=(unsigned long)val<<2; - rvb.CurrAddr=rvb.StartAddr; + spu.rvb->StartAddr=(unsigned long)val<<2; + spu.rvb->CurrAddr=spu.rvb->StartAddr; } } goto rvbd; @@ -163,11 +163,11 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val, goto upd_irq; //-------------------------------------------------// case H_SPUrvolL: - rvb.VolLeft=val; + spu.rvb->VolLeft=val; break; //-------------------------------------------------// case H_SPUrvolR: - rvb.VolRight=val; + spu.rvb->VolRight=val; break; //-------------------------------------------------// @@ -246,38 +246,38 @@ void CALLBACK SPUwriteRegister(unsigned long reg, unsigned short val, ReverbOn(16,24,val); break; //-------------------------------------------------// - case H_Reverb+0 : rvb.FB_SRC_A=val*4; goto rvbd; - case H_Reverb+2 : rvb.FB_SRC_B=val*4; goto rvbd; - case H_Reverb+4 : rvb.IIR_ALPHA=(short)val; goto rvbd; - case H_Reverb+6 : rvb.ACC_COEF_A=(short)val; goto rvbd; - case H_Reverb+8 : rvb.ACC_COEF_B=(short)val; goto rvbd; - case H_Reverb+10 : rvb.ACC_COEF_C=(short)val; goto rvbd; - case H_Reverb+12 : rvb.ACC_COEF_D=(short)val; goto rvbd; - case H_Reverb+14 : rvb.IIR_COEF=(short)val; goto rvbd; - case H_Reverb+16 : rvb.FB_ALPHA=(short)val; goto rvbd; - case H_Reverb+18 : rvb.FB_X=(short)val; goto rvbd; - case H_Reverb+20 : rvb.IIR_DEST_A0=val*4; goto rvbd; - case H_Reverb+22 : rvb.IIR_DEST_A1=val*4; goto rvbd; - case H_Reverb+24 : rvb.ACC_SRC_A0=val*4; goto rvbd; - case H_Reverb+26 : rvb.ACC_SRC_A1=val*4; goto rvbd; - case H_Reverb+28 : rvb.ACC_SRC_B0=val*4; goto rvbd; - case H_Reverb+30 : rvb.ACC_SRC_B1=val*4; goto rvbd; - case H_Reverb+32 : rvb.IIR_SRC_A0=val*4; goto rvbd; - case H_Reverb+34 : rvb.IIR_SRC_A1=val*4; goto rvbd; - case H_Reverb+36 : rvb.IIR_DEST_B0=val*4; goto rvbd; - case H_Reverb+38 : rvb.IIR_DEST_B1=val*4; goto rvbd; - case H_Reverb+40 : rvb.ACC_SRC_C0=val*4; goto rvbd; - case H_Reverb+42 : rvb.ACC_SRC_C1=val*4; goto rvbd; - case H_Reverb+44 : rvb.ACC_SRC_D0=val*4; goto rvbd; - case H_Reverb+46 : rvb.ACC_SRC_D1=val*4; goto rvbd; - case H_Reverb+48 : rvb.IIR_SRC_B1=val*4; goto rvbd; - case H_Reverb+50 : rvb.IIR_SRC_B0=val*4; goto rvbd; - case H_Reverb+52 : rvb.MIX_DEST_A0=val*4; goto rvbd; - case H_Reverb+54 : rvb.MIX_DEST_A1=val*4; goto rvbd; - case H_Reverb+56 : rvb.MIX_DEST_B0=val*4; goto rvbd; - case H_Reverb+58 : rvb.MIX_DEST_B1=val*4; goto rvbd; - case H_Reverb+60 : rvb.IN_COEF_L=(short)val; goto rvbd; - case H_Reverb+62 : rvb.IN_COEF_R=(short)val; goto rvbd; + case H_Reverb+0 : spu.rvb->FB_SRC_A=val*4; goto rvbd; + case H_Reverb+2 : spu.rvb->FB_SRC_B=val*4; goto rvbd; + case H_Reverb+4 : spu.rvb->IIR_ALPHA=(short)val; goto rvbd; + case H_Reverb+6 : spu.rvb->ACC_COEF_A=(short)val; goto rvbd; + case H_Reverb+8 : spu.rvb->ACC_COEF_B=(short)val; goto rvbd; + case H_Reverb+10 : spu.rvb->ACC_COEF_C=(short)val; goto rvbd; + case H_Reverb+12 : spu.rvb->ACC_COEF_D=(short)val; goto rvbd; + case H_Reverb+14 : spu.rvb->IIR_COEF=(short)val; goto rvbd; + case H_Reverb+16 : spu.rvb->FB_ALPHA=(short)val; goto rvbd; + case H_Reverb+18 : spu.rvb->FB_X=(short)val; goto rvbd; + case H_Reverb+20 : spu.rvb->IIR_DEST_A0=val*4; goto rvbd; + case H_Reverb+22 : spu.rvb->IIR_DEST_A1=val*4; goto rvbd; + case H_Reverb+24 : spu.rvb->ACC_SRC_A0=val*4; goto rvbd; + case H_Reverb+26 : spu.rvb->ACC_SRC_A1=val*4; goto rvbd; + case H_Reverb+28 : spu.rvb->ACC_SRC_B0=val*4; goto rvbd; + case H_Reverb+30 : spu.rvb->ACC_SRC_B1=val*4; goto rvbd; + case H_Reverb+32 : spu.rvb->IIR_SRC_A0=val*4; goto rvbd; + case H_Reverb+34 : spu.rvb->IIR_SRC_A1=val*4; goto rvbd; + case H_Reverb+36 : spu.rvb->IIR_DEST_B0=val*4; goto rvbd; + case H_Reverb+38 : spu.rvb->IIR_DEST_B1=val*4; goto rvbd; + case H_Reverb+40 : spu.rvb->ACC_SRC_C0=val*4; goto rvbd; + case H_Reverb+42 : spu.rvb->ACC_SRC_C1=val*4; goto rvbd; + case H_Reverb+44 : spu.rvb->ACC_SRC_D0=val*4; goto rvbd; + case H_Reverb+46 : spu.rvb->ACC_SRC_D1=val*4; goto rvbd; + case H_Reverb+48 : spu.rvb->IIR_SRC_B1=val*4; goto rvbd; + case H_Reverb+50 : spu.rvb->IIR_SRC_B0=val*4; goto rvbd; + case H_Reverb+52 : spu.rvb->MIX_DEST_A0=val*4; goto rvbd; + case H_Reverb+54 : spu.rvb->MIX_DEST_A1=val*4; goto rvbd; + case H_Reverb+56 : spu.rvb->MIX_DEST_B0=val*4; goto rvbd; + case H_Reverb+58 : spu.rvb->MIX_DEST_B1=val*4; goto rvbd; + case H_Reverb+60 : spu.rvb->IN_COEF_L=(short)val; goto rvbd; + case H_Reverb+62 : spu.rvb->IN_COEF_R=(short)val; goto rvbd; } return; @@ -287,7 +287,7 @@ upd_irq: return; rvbd: - rvb.dirty = 1; // recalculate on next update + spu.rvb->dirty = 1; // recalculate on next update } //////////////////////////////////////////////////////////////////////// diff --git a/plugins/dfsound/reverb.c b/plugins/dfsound/reverb.c index 7e32b8e..2ff6edc 100644 --- a/plugins/dfsound/reverb.c +++ b/plugins/dfsound/reverb.c @@ -50,41 +50,40 @@ INLINE int rvb2ram_offs(int curr, int space, int iOff) // get_buffer content helper: takes care about wraps #define g_buffer(var) \ - ((int)(signed short)spu.spuMem[rvb2ram_offs(curr_addr, space, rvb.n##var)]) + ((int)(signed short)spu.spuMem[rvb2ram_offs(curr_addr, space, rvb->n##var)]) // saturate iVal and store it as var #define s_buffer(var, iVal) \ ssat32_to_16(iVal); \ - spu.spuMem[rvb2ram_offs(curr_addr, space, rvb.n##var)] = iVal + spu.spuMem[rvb2ram_offs(curr_addr, space, rvb->n##var)] = iVal #define s_buffer1(var, iVal) \ ssat32_to_16(iVal); \ - spu.spuMem[rvb2ram_offs(curr_addr, space, rvb.n##var + 1)] = iVal + spu.spuMem[rvb2ram_offs(curr_addr, space, rvb->n##var + 1)] = iVal //////////////////////////////////////////////////////////////////////// // portions based on spu2-x from PCSX2 -static void MixREVERB(int *SSumLR, int *RVB, int ns_to) +static void MixREVERB(int *SSumLR, int *RVB, int ns_to, int curr_addr) { - int l_old = rvb.iRVBLeft; - int r_old = rvb.iRVBRight; - int curr_addr = rvb.CurrAddr; - int space = 0x40000 - rvb.StartAddr; - int l = 0, r = 0, ns; + const REVERBInfo *rvb = spu.rvb; + int IIR_ALPHA = rvb->IIR_ALPHA; + int IIR_COEF = rvb->IIR_COEF; + int space = 0x40000 - rvb->StartAddr; + int l, r, ns; for (ns = 0; ns < ns_to * 2; ) { - int IIR_ALPHA = rvb.IIR_ALPHA; int ACC0, ACC1, FB_A0, FB_A1, FB_B0, FB_B1; int mix_dest_a0, mix_dest_a1, mix_dest_b0, mix_dest_b1; - int input_L = RVB[ns] * rvb.IN_COEF_L; - int input_R = RVB[ns+1] * rvb.IN_COEF_R; + int input_L = RVB[ns] * rvb->IN_COEF_L; + int input_R = RVB[ns+1] * rvb->IN_COEF_R; - int IIR_INPUT_A0 = ((g_buffer(IIR_SRC_A0) * rvb.IIR_COEF) + input_L) >> 15; - int IIR_INPUT_A1 = ((g_buffer(IIR_SRC_A1) * rvb.IIR_COEF) + input_R) >> 15; - int IIR_INPUT_B0 = ((g_buffer(IIR_SRC_B0) * rvb.IIR_COEF) + input_L) >> 15; - int IIR_INPUT_B1 = ((g_buffer(IIR_SRC_B1) * rvb.IIR_COEF) + input_R) >> 15; + int IIR_INPUT_A0 = ((g_buffer(IIR_SRC_A0) * IIR_COEF) + input_L) >> 15; + int IIR_INPUT_A1 = ((g_buffer(IIR_SRC_A1) * IIR_COEF) + input_R) >> 15; + int IIR_INPUT_B0 = ((g_buffer(IIR_SRC_B0) * IIR_COEF) + input_L) >> 15; + int IIR_INPUT_B1 = ((g_buffer(IIR_SRC_B1) * IIR_COEF) + input_R) >> 15; int iir_dest_a0 = g_buffer(IIR_DEST_A0); int iir_dest_a1 = g_buffer(IIR_DEST_A1); @@ -96,30 +95,34 @@ static void MixREVERB(int *SSumLR, int *RVB, int ns_to) int IIR_B0 = iir_dest_b0 + ((IIR_INPUT_B0 - iir_dest_b0) * IIR_ALPHA >> 15); int IIR_B1 = iir_dest_b1 + ((IIR_INPUT_B1 - iir_dest_b1) * IIR_ALPHA >> 15); + preload(SSumLR + ns + 64*2/4 - 4); + s_buffer1(IIR_DEST_A0, IIR_A0); s_buffer1(IIR_DEST_A1, IIR_A1); s_buffer1(IIR_DEST_B0, IIR_B0); s_buffer1(IIR_DEST_B1, IIR_B1); - ACC0 = (g_buffer(ACC_SRC_A0) * rvb.ACC_COEF_A + - g_buffer(ACC_SRC_B0) * rvb.ACC_COEF_B + - g_buffer(ACC_SRC_C0) * rvb.ACC_COEF_C + - g_buffer(ACC_SRC_D0) * rvb.ACC_COEF_D) >> 15; - ACC1 = (g_buffer(ACC_SRC_A1) * rvb.ACC_COEF_A + - g_buffer(ACC_SRC_B1) * rvb.ACC_COEF_B + - g_buffer(ACC_SRC_C1) * rvb.ACC_COEF_C + - g_buffer(ACC_SRC_D1) * rvb.ACC_COEF_D) >> 15; + preload(RVB + ns + 64*2/4 - 4); + + ACC0 = (g_buffer(ACC_SRC_A0) * rvb->ACC_COEF_A + + g_buffer(ACC_SRC_B0) * rvb->ACC_COEF_B + + g_buffer(ACC_SRC_C0) * rvb->ACC_COEF_C + + g_buffer(ACC_SRC_D0) * rvb->ACC_COEF_D) >> 15; + ACC1 = (g_buffer(ACC_SRC_A1) * rvb->ACC_COEF_A + + g_buffer(ACC_SRC_B1) * rvb->ACC_COEF_B + + g_buffer(ACC_SRC_C1) * rvb->ACC_COEF_C + + g_buffer(ACC_SRC_D1) * rvb->ACC_COEF_D) >> 15; FB_A0 = g_buffer(FB_SRC_A0); FB_A1 = g_buffer(FB_SRC_A1); FB_B0 = g_buffer(FB_SRC_B0); FB_B1 = g_buffer(FB_SRC_B1); - mix_dest_a0 = ACC0 - ((FB_A0 * rvb.FB_ALPHA) >> 15); - mix_dest_a1 = ACC1 - ((FB_A1 * rvb.FB_ALPHA) >> 15); + mix_dest_a0 = ACC0 - ((FB_A0 * rvb->FB_ALPHA) >> 15); + mix_dest_a1 = ACC1 - ((FB_A1 * rvb->FB_ALPHA) >> 15); - mix_dest_b0 = FB_A0 + (((ACC0 - FB_A0) * rvb.FB_ALPHA - FB_B0 * rvb.FB_X) >> 15); - mix_dest_b1 = FB_A1 + (((ACC1 - FB_A1) * rvb.FB_ALPHA - FB_B1 * rvb.FB_X) >> 15); + mix_dest_b0 = FB_A0 + (((ACC0 - FB_A0) * rvb->FB_ALPHA - FB_B0 * rvb->FB_X) >> 15); + mix_dest_b1 = FB_A1 + (((ACC1 - FB_A1) * rvb->FB_ALPHA - FB_B1 * rvb->FB_X) >> 15); s_buffer(MIX_DEST_A0, mix_dest_a0); s_buffer(MIX_DEST_A1, mix_dest_a1); @@ -129,73 +132,60 @@ static void MixREVERB(int *SSumLR, int *RVB, int ns_to) l = (mix_dest_a0 + mix_dest_b0) / 2; r = (mix_dest_a1 + mix_dest_b1) / 2; - l = (l * rvb.VolLeft) >> 15; // 15? - r = (r * rvb.VolRight) >> 15; + l = (l * rvb->VolLeft) >> 15; // 15? + r = (r * rvb->VolRight) >> 15; - SSumLR[ns++] += (l + l_old) / 2; - SSumLR[ns++] += (r + r_old) / 2; SSumLR[ns++] += l; SSumLR[ns++] += r; - - l_old = l; - r_old = r; + SSumLR[ns++] += l; + SSumLR[ns++] += r; curr_addr++; - if (curr_addr >= 0x40000) curr_addr = rvb.StartAddr; + if (curr_addr >= 0x40000) curr_addr = rvb->StartAddr; } - - rvb.iRVBLeft = l; - rvb.iRVBRight = r; - rvb.CurrAddr = curr_addr; } -static void MixREVERB_off(int *SSumLR, int ns_to) +static void MixREVERB_off(int *SSumLR, int ns_to, int curr_addr) { - int l_old = rvb.iRVBLeft; - int r_old = rvb.iRVBRight; - int curr_addr = rvb.CurrAddr; - int space = 0x40000 - rvb.StartAddr; - int l = 0, r = 0, ns; + const REVERBInfo *rvb = spu.rvb; + int space = 0x40000 - rvb->StartAddr; + int l, r, ns; for (ns = 0; ns < ns_to * 2; ) { + preload(SSumLR + ns + 64*2/4 - 4); + l = (g_buffer(MIX_DEST_A0) + g_buffer(MIX_DEST_B0)) / 2; r = (g_buffer(MIX_DEST_A1) + g_buffer(MIX_DEST_B1)) / 2; - l = (l * rvb.VolLeft) >> 15; - r = (r * rvb.VolRight) >> 15; + l = (l * rvb->VolLeft) >> 15; + r = (r * rvb->VolRight) >> 15; - SSumLR[ns++] += (l + l_old) / 2; - SSumLR[ns++] += (r + r_old) / 2; SSumLR[ns++] += l; SSumLR[ns++] += r; - - l_old = l; - r_old = r; + SSumLR[ns++] += l; + SSumLR[ns++] += r; curr_addr++; - if (curr_addr >= 0x40000) curr_addr = rvb.StartAddr; + if (curr_addr >= 0x40000) curr_addr = rvb->StartAddr; } - - rvb.iRVBLeft = l; - rvb.iRVBRight = r; - rvb.CurrAddr = curr_addr; } -static void prepare_offsets(void) +static void REVERBPrep(void) { - int space = 0x40000 - rvb.StartAddr; + REVERBInfo *rvb = spu.rvb; + int space = 0x40000 - rvb->StartAddr; int t; #define prep_offs(v) \ - t = rvb.v; \ + t = rvb->v; \ while (t >= space) \ t -= space; \ - rvb.n##v = t + rvb->n##v = t #define prep_offs2(d, v1, v2) \ - t = rvb.v1 - rvb.v2; \ + t = rvb->v1 - rvb->v2; \ while (t >= space) \ t -= space; \ - rvb.n##d = t + rvb->n##d = t prep_offs(IIR_SRC_A0); prep_offs(IIR_SRC_A1); @@ -224,37 +214,18 @@ static void prepare_offsets(void) #undef prep_offs #undef prep_offs2 - rvb.dirty = 0; + rvb->dirty = 0; } -INLINE void REVERBDo(int *SSumLR, int *RVB, int ns_to) +INLINE void REVERBDo(int *SSumLR, int *RVB, int ns_to, int curr_addr) { - if (!rvb.StartAddr) // reverb is off - { - rvb.iRVBLeft = rvb.iRVBRight = 0; - return; - } - if (spu.spuCtrl & 0x80) // -> reverb on? oki { - if (unlikely(rvb.dirty)) - prepare_offsets(); - - MixREVERB(SSumLR, RVB, ns_to); - } - else if (rvb.VolLeft || rvb.VolRight) - { - if (unlikely(rvb.dirty)) - prepare_offsets(); - - MixREVERB_off(SSumLR, ns_to); + MixREVERB(SSumLR, RVB, ns_to, curr_addr); } - else // -> reverb off + else if (spu.rvb->VolLeft || spu.rvb->VolRight) { - // reverb runs anyway - rvb.CurrAddr += ns_to / 2; - while (rvb.CurrAddr >= 0x40000) - rvb.CurrAddr -= 0x40000 - rvb.StartAddr; + MixREVERB_off(SSumLR, ns_to, curr_addr); } } diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c index ec31b0c..8681d35 100644 --- a/plugins/dfsound/spu.c +++ b/plugins/dfsound/spu.c @@ -73,11 +73,8 @@ static char * libraryInfo = N_("P.E.Op.S. Sound Driver V1.7\nCoded by Pete B SPUInfo spu; SPUConfig spu_config; -// MAIN infos struct for each channel - -REVERBInfo rvb; - static int iFMod[NSSIZE]; +static int RVB[NSSIZE * 2]; int ChanBuf[NSSIZE]; #define CDDA_BUFFER_SIZE (16384 * sizeof(uint32_t)) // must be power of 2 @@ -763,11 +760,13 @@ static void do_silent_chans(int ns_to, int silentch) static void do_channels(int ns_to) { unsigned int mask; + int do_rvb, ch, d; SPUCHAN *s_chan; int *SB, sinc; - int ch, d; - memset(spu.RVB, 0, ns_to * sizeof(spu.RVB[0]) * 2); + do_rvb = spu.rvb->StartAddr && spu_config.iUseReverb; + if (do_rvb) + memset(RVB, 0, ns_to * sizeof(RVB[0]) * 2); mask = spu.dwNewChannel & 0xffffff; for (ch = 0; mask != 0; ch++, mask >>= 1) { @@ -812,14 +811,27 @@ static void do_channels(int ns_to) if (s_chan->bFMod == 2) // fmod freq channel memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); - if (s_chan->bRVBActive) - mix_chan_rvb(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, spu.RVB); + if (s_chan->bRVBActive && do_rvb) + mix_chan_rvb(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume, RVB); else mix_chan(spu.SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); } + + if (spu.rvb->StartAddr) { + if (do_rvb) { + if (unlikely(spu.rvb->dirty)) + REVERBPrep(); + + REVERBDo(spu.SSumLR, RVB, ns_to, spu.rvb->CurrAddr); + } + + spu.rvb->CurrAddr += ns_to / 2; + while (spu.rvb->CurrAddr >= 0x40000) + spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr; + } } -static void do_samples_finish(int *SSumLR, int *RVB, int ns_to, +static void do_samples_finish(int *SSumLR, int ns_to, int silentch, int decode_pos); // optional worker thread handling @@ -833,8 +845,7 @@ static struct spu_worker { unsigned int exit_thread; unsigned int i_ready; unsigned int i_reaped; - unsigned int req_sent; // dsp - unsigned int last_boot_cnt; + unsigned int last_boot_cnt; // dsp }; // aligning for C64X_DSP unsigned int _pad0[128/4]; @@ -851,6 +862,7 @@ static struct spu_worker { int ns_to; int ctrl; int decode_pos; + int rvb_addr; unsigned int channels_new; unsigned int channels_on; unsigned int channels_silent; @@ -861,10 +873,11 @@ static struct spu_worker { int start; int loop; int ns_to; + short vol_l; + short vol_r; ADSRInfoEx adsr; - // might want to add vol and fmod flags.. + // might also want to add fmod flags.. } ch[24]; - int RVB[NSSIZE * 2]; int SSumLR[NSSIZE * 2]; } i[4]; } *worker; @@ -874,6 +887,7 @@ static struct spu_worker { static void thread_work_start(void); static void thread_work_wait_sync(struct work_item *work, int force); +static void thread_sync_caches(void); static int thread_get_i_done(void); static int decode_block_work(void *context, int ch, int *SB) @@ -936,6 +950,8 @@ static void queue_channel_work(int ns_to, unsigned int silentch) work->ch[ch].sbpos = s_chan->iSBPos; work->ch[ch].sinc = s_chan->sinc; work->ch[ch].adsr = s_chan->ADSRX; + work->ch[ch].vol_l = s_chan->iLeftVolume; + work->ch[ch].vol_r = s_chan->iRightVolume; work->ch[ch].start = s_chan->pCurr - spu.spuMemC; work->ch[ch].loop = s_chan->pLoop - spu.spuMemC; if (s_chan->prevflags & 1) @@ -952,6 +968,19 @@ static void queue_channel_work(int ns_to, unsigned int silentch) } } + work->rvb_addr = 0; + if (spu.rvb->StartAddr) { + if (spu_config.iUseReverb) { + if (unlikely(spu.rvb->dirty)) + REVERBPrep(); + work->rvb_addr = spu.rvb->CurrAddr; + } + + spu.rvb->CurrAddr += ns_to / 2; + while (spu.rvb->CurrAddr >= 0x40000) + spu.rvb->CurrAddr -= 0x40000 - spu.rvb->StartAddr; + } + worker->i_ready++; thread_work_start(); } @@ -965,7 +994,9 @@ static void do_channel_work(struct work_item *work) SPUCHAN *s_chan; ns_to = work->ns_to; - memset(work->RVB, 0, ns_to * sizeof(work->RVB[0]) * 2); + + if (work->rvb_addr) + memset(RVB, 0, ns_to * sizeof(RVB[0]) * 2); mask = work->channels_new; for (ch = 0; mask != 0; ch++, mask >>= 1) { @@ -1010,12 +1041,15 @@ static void do_channel_work(struct work_item *work) if (s_chan->bFMod == 2) // fmod freq channel memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0])); - if (s_chan->bRVBActive) + if (s_chan->bRVBActive && work->rvb_addr) mix_chan_rvb(work->SSumLR, ns_to, - s_chan->iLeftVolume, s_chan->iRightVolume, work->RVB); + work->ch[ch].vol_l, work->ch[ch].vol_r, RVB); else - mix_chan(work->SSumLR, ns_to, s_chan->iLeftVolume, s_chan->iRightVolume); + mix_chan(work->SSumLR, ns_to, work->ch[ch].vol_l, work->ch[ch].vol_r); } + + if (work->rvb_addr) + REVERBDo(work->SSumLR, RVB, ns_to, work->rvb_addr); } static void sync_worker_thread(int force) @@ -1032,13 +1066,15 @@ static void sync_worker_thread(int force) work = &worker->i[worker->i_reaped & WORK_I_MASK]; thread_work_wait_sync(work, force); - do_samples_finish(work->SSumLR, work->RVB, work->ns_to, + do_samples_finish(work->SSumLR, work->ns_to, work->channels_silent, work->decode_pos); worker->i_reaped++; done = thread_get_i_done() - worker->i_reaped; used_space = worker->i_ready - worker->i_reaped; } + if (force) + thread_sync_caches(); } #else @@ -1116,7 +1152,7 @@ void do_samples(unsigned int cycles_to, int do_direct) if (do_direct || worker == NULL || !spu_config.iUseThread) { do_channels(ns_to); - do_samples_finish(spu.SSumLR, spu.RVB, ns_to, silentch, spu.decode_pos); + do_samples_finish(spu.SSumLR, ns_to, silentch, spu.decode_pos); } else { queue_channel_work(ns_to, silentch); @@ -1131,7 +1167,7 @@ void do_samples(unsigned int cycles_to, int do_direct) spu.decode_pos = (spu.decode_pos + ns_to) & 0x1ff; } -static void do_samples_finish(int *SSumLR, int *RVB, int ns_to, +static void do_samples_finish(int *SSumLR, int ns_to, int silentch, int decode_pos) { int volmult = spu_config.iVolume; @@ -1150,17 +1186,8 @@ static void do_samples_finish(int *SSumLR, int *RVB, int ns_to, spu.decode_dirty_ch &= ~(1<<3); } - //---------------------------------------------------// - // mix XA infos (if any) - MixXA(SSumLR, ns_to, decode_pos); - /////////////////////////////////////////////////////// - // mix all channels (including reverb) into one buffer - - if(spu_config.iUseReverb) - REVERBDo(SSumLR, RVB, ns_to); - if((spu.spuCtrl&0x4000)==0) // muted? (rare, don't optimize for this) { memset(spu.pS, 0, ns_to * 2 * sizeof(spu.pS[0])); @@ -1283,12 +1310,9 @@ void ClearWorkingState(void) } // SETUPSTREAMS: init most of the spu buffers -void SetupStreams(void) +static void SetupStreams(void) { - int i; - spu.pSpuBuffer = (unsigned char *)malloc(32768); // alloc mixing buffer - spu.RVB = calloc(NSSIZE * 2, sizeof(spu.RVB[0])); spu.SSumLR = calloc(NSSIZE * 2, sizeof(spu.SSumLR[0])); spu.XAStart = // alloc xa buffer @@ -1303,26 +1327,14 @@ void SetupStreams(void) spu.CDDAPlay = spu.CDDAStart; spu.CDDAFeed = spu.CDDAStart; - for(i=0;i init sustain - spu.s_chan[i].ADSRX.SustainIncrease = 1; - spu.s_chan[i].pLoop=spu.spuMemC; - spu.s_chan[i].pCurr=spu.spuMemC; - } - ClearWorkingState(); - - spu.bSpuInit=1; // flag: we are inited } // REMOVESTREAMS: free most buffer -void RemoveStreams(void) +static void RemoveStreams(void) { free(spu.pSpuBuffer); // free mixing buffer spu.pSpuBuffer = NULL; - free(spu.RVB); // free reverb buffer - spu.RVB = NULL; free(spu.SSumLR); spu.SSumLR = NULL; free(spu.XAStart); // free XA buffer @@ -1365,6 +1377,10 @@ static int thread_get_i_done(void) return worker->i_done; } +static void thread_sync_caches(void) +{ +} + static void *spu_worker_thread(void *unused) { struct work_item *work; @@ -1446,11 +1462,13 @@ static void exit_spu_thread(void) // SPUINIT: this func will be called first by the main emu long CALLBACK SPUinit(void) { + int i; + spu.spuMemC = calloc(1, 512 * 1024); - memset((void *)&rvb, 0, sizeof(REVERBInfo)); InitADSR(); spu.s_chan = calloc(MAXCHAN+1, sizeof(spu.s_chan[0])); // channel + 1 infos (1 is security for fmod handling) + spu.rvb = calloc(1, sizeof(REVERBInfo)); spu.SB = calloc(MAXCHAN, sizeof(spu.SB[0]) * SB_SIZE); spu.spuAddr = 0; @@ -1464,6 +1482,16 @@ long CALLBACK SPUinit(void) init_spu_thread(); + for (i = 0; i < MAXCHAN; i++) // loop sound channels + { + spu.s_chan[i].ADSRX.SustainLevel = 0xf; // -> init sustain + spu.s_chan[i].ADSRX.SustainIncrease = 1; + spu.s_chan[i].pLoop = spu.spuMemC; + spu.s_chan[i].pCurr = spu.spuMemC; + } + + spu.bSpuInit=1; // flag: we are inited + return 0; } @@ -1504,6 +1532,8 @@ long CALLBACK SPUshutdown(void) spu.SB = NULL; free(spu.s_chan); spu.s_chan = NULL; + free(spu.rvb); + spu.rvb = NULL; RemoveStreams(); // no more streaming spu.bSpuInit=0; diff --git a/plugins/dfsound/spu_c64x.c b/plugins/dfsound/spu_c64x.c index 200ab38..be10a6b 100644 --- a/plugins/dfsound/spu_c64x.c +++ b/plugins/dfsound/spu_c64x.c @@ -43,6 +43,8 @@ static struct { dsp_mem_region_t region; dsp_component_id_t compid; + unsigned int stale_caches:1; + unsigned int req_sent:1; } f; static void thread_work_start(void) @@ -63,7 +65,7 @@ static void thread_work_start(void) // to start the DSP, dsp_rpc_send() must be used, // but before that, previous request must be finished - if (worker->req_sent) { + if (f.req_sent) { if (worker->boot_cnt == worker->last_boot_cnt) { // hopefully still booting //printf("booting?\n"); @@ -74,7 +76,7 @@ static void thread_work_start(void) if (ret != 0) { fprintf(stderr, "dsp_rpc_recv failed: %d\n", ret); f.dsp_logbuf_print(); - worker->req_sent = 0; + f.req_sent = 0; spu_config.iUseThread = 0; return; } @@ -94,7 +96,7 @@ static void thread_work_start(void) spu_config.iUseThread = 0; return; } - worker->req_sent = 1; + f.req_sent = 1; } static int thread_get_i_done(void) @@ -108,14 +110,13 @@ static void thread_work_wait_sync(struct work_item *work, int force) int limit = 1000; int ns_to; - ns_to = work->ns_to; - f.dsp_cache_inv_virt(work->RVB, sizeof(work->RVB[0]) * 2 * ns_to); - f.dsp_cache_inv_virt(work->SSumLR, sizeof(work->SSumLR[0]) * 2 * ns_to); - __builtin_prefetch(work->RVB); - __builtin_prefetch(work->SSumLR); - while (worker->i_done == worker->i_reaped && limit-- > 0) { - if (!worker->active) { + if (!f.req_sent) { + printf("dsp: req not sent?\n"); + break; + } + + if (worker->boot_cnt != worker->last_boot_cnt && !worker->active) { printf("dsp: broken sync\n"); worker->last_boot_cnt = ~0; break; @@ -125,6 +126,13 @@ static void thread_work_wait_sync(struct work_item *work, int force) f.dsp_cache_inv_virt(&worker->i_done, 64); } + ns_to = work->ns_to; + f.dsp_cache_inv_virt(work->SSumLR, sizeof(work->SSumLR[0]) * 2 * ns_to); + preload(work->SSumLR); + preload(work->SSumLR + 64/4); + + f.stale_caches = 1; // SB, spuMem + if (limit == 0) printf("dsp: wait timeout\n"); @@ -132,7 +140,7 @@ static void thread_work_wait_sync(struct work_item *work, int force) if (worker->i_reaped != worker->i_done - 1) return; - if (worker->req_sent && (force || worker->i_done == worker->i_ready)) { + if (f.req_sent && (force || worker->i_done == worker->i_ready)) { dsp_msg_t msg; int ret; @@ -142,12 +150,20 @@ static void thread_work_wait_sync(struct work_item *work, int force) f.dsp_logbuf_print(); spu_config.iUseThread = 0; } - worker->req_sent = 0; + f.req_sent = 0; } +} - if (force) { +static void thread_sync_caches(void) +{ + if (f.stale_caches) { f.dsp_cache_inv_virt(spu.SB, sizeof(spu.SB[0]) * SB_SIZE * 24); f.dsp_cache_inv_virt(spu.spuMemC + 0x800, 0x800); + if (spu.rvb->StartAddr) { + int left = 0x40000 - spu.rvb->StartAddr; + f.dsp_cache_inv_virt(spu.spuMem + spu.rvb->StartAddr, left * 2); + } + f.stale_caches = 0; } } @@ -220,9 +236,9 @@ static void init_spu_thread(void) mem->sizeof_region_mem, sizeof(*mem)); goto fail_init; } - if (mem->offsetof_s_chan1 != offsetof(typeof(*mem), s_chan[1])) { + if (mem->offsetof_s_chan1 != offsetof(typeof(*mem), in.s_chan[1])) { fprintf(stderr, "error: size mismatch 2: %d vs %zd\n", - mem->offsetof_s_chan1, offsetof(typeof(*mem), s_chan[1])); + mem->offsetof_s_chan1, offsetof(typeof(*mem), in.s_chan[1])); goto fail_init; } if (mem->offsetof_spos_3_20 != offsetof(typeof(*mem), worker.i[3].ch[20])) { @@ -237,7 +253,9 @@ static void init_spu_thread(void) free(spu.SB); spu.SB = mem->SB; free(spu.s_chan); - spu.s_chan = mem->s_chan; + spu.s_chan = mem->in.s_chan; + free(spu.rvb); + spu.rvb = &mem->in.rvb; worker = &mem->worker; printf("spu: C64x DSP ready (id=%d).\n", (int)f.compid); @@ -267,8 +285,10 @@ static void exit_spu_thread(void) if (worker == NULL) return; - if (worker->req_sent) + if (f.req_sent) { f.dsp_rpc_recv(&msg); + f.req_sent = 0; + } f.dsp_logbuf_print(); f.dsp_shm_free(f.region); @@ -277,6 +297,7 @@ static void exit_spu_thread(void) spu.spuMemC = NULL; spu.SB = NULL; spu.s_chan = NULL; + spu.rvb = NULL; worker = NULL; } diff --git a/plugins/dfsound/spu_c64x.h b/plugins/dfsound/spu_c64x.h index bb20cc3..8210e63 100644 --- a/plugins/dfsound/spu_c64x.h +++ b/plugins/dfsound/spu_c64x.h @@ -11,8 +11,12 @@ struct region_mem { // careful not to lose ARM writes by DSP overwriting // with old data when it's writing out neighbor cachelines int _pad1[128/4 - ((SB_SIZE * 24) & (128/4 - 1))]; - SPUCHAN s_chan[24 + 1]; - int _pad2[128/4 - ((sizeof(SPUCHAN) * 25 / 4) & (128/4 - 1))]; + struct spu_in { + // these are not to be modified by DSP + SPUCHAN s_chan[24 + 1]; + REVERBInfo rvb; + } in; + int _pad2[128/4 - ((sizeof(struct spu_in) / 4) & (128/4 - 1))]; struct spu_worker worker; SPUConfig spu_config; // init/debug diff --git a/plugins/dfsound/spu_c64x_dspcode.c b/plugins/dfsound/spu_c64x_dspcode.c index 97d3028..b0352a9 100644 --- a/plugins/dfsound/spu_c64x_dspcode.c +++ b/plugins/dfsound/spu_c64x_dspcode.c @@ -31,6 +31,7 @@ /* dummy deps, some bloat but avoids ifdef hell in SPU code.. */ static void thread_work_start(void) {} static void thread_work_wait_sync(struct work_item *work, int force) {} +static void thread_sync_caches(void) {} static int thread_get_i_done(void) { return 0; } struct out_driver *out_current; void SetupSound(void) {} @@ -38,7 +39,8 @@ void SetupSound(void) {} static void invalidate_cache(struct work_item *work) { - syscalls.cache_inv(work, offsetof(typeof(*work), RVB), 1); + // see comment in writeout_cache() + //syscalls.cache_inv(work, offsetof(typeof(*work), SSumLR), 1); syscalls.cache_inv(spu.s_chan, sizeof(spu.s_chan[0]) * 24, 0); syscalls.cache_inv(work->SSumLR, sizeof(work->SSumLR[0]) * 2 * work->ns_to, 0); @@ -48,14 +50,16 @@ static void writeout_cache(struct work_item *work) { int ns_to = work->ns_to; - syscalls.cache_wb(work->RVB, sizeof(work->RVB[0]) * 2 * ns_to, 1); syscalls.cache_wb(work->SSumLR, sizeof(work->SSumLR[0]) * 2 * ns_to, 1); + // have to invalidate now, otherwise there is a race between + // DSP evicting dirty lines and ARM writing new data to this area + syscalls.cache_inv(work, offsetof(typeof(*work), SSumLR), 0); } static void do_processing(void) { + int left, dirty = 0, had_rvb = 0; struct work_item *work; - int left, dirty = 0; while (worker->active) { @@ -70,6 +74,8 @@ static void do_processing(void) work = &worker->i[worker->i_done & WORK_I_MASK]; invalidate_cache(work); + had_rvb |= work->rvb_addr; + spu.spuCtrl = work->ctrl; do_channel_work(work); writeout_cache(work); @@ -82,6 +88,11 @@ static void do_processing(void) if (dirty) { syscalls.cache_wb(spu.spuMemC + 0x800, 0x800, 1); syscalls.cache_wb(spu.SB, sizeof(spu.SB[0]) * SB_SIZE * 24, 1); + if (had_rvb) { + left = 0x40000 - spu.rvb->StartAddr; + syscalls.cache_wb(spu.spuMem + spu.rvb->StartAddr, left * 2, 1); + had_rvb = 0; + } dirty = 0; continue; } @@ -105,12 +116,13 @@ static unsigned int exec(dsp_component_cmd_t cmd, spu.spuMemC = mem->spu_ram; spu.SB = mem->SB; - spu.s_chan = mem->s_chan; + spu.s_chan = mem->in.s_chan; + spu.rvb = &mem->in.rvb; worker = &mem->worker; memcpy(&spu_config, &mem->spu_config, sizeof(spu_config)); mem->sizeof_region_mem = sizeof(*mem); - mem->offsetof_s_chan1 = offsetof(typeof(*mem), s_chan[1]); + mem->offsetof_s_chan1 = offsetof(typeof(*mem), in.s_chan[1]); mem->offsetof_spos_3_20 = offsetof(typeof(*mem), worker.i[3].ch[20]); // seems to be unneeded, no write-alloc? but just in case.. syscalls.cache_wb(&mem->sizeof_region_mem, 3 * 4, 1); @@ -126,7 +138,9 @@ static unsigned int exec(dsp_component_cmd_t cmd, // c64_tools lib does BCACHE_wbInvAll() when it receives mailbox irq, // but invalidate anyway in case c64_tools is ever fixed.. - syscalls.cache_inv(mem, sizeof(mem->spu_ram) + sizeof(mem->SB), 0); + // XXX edit: don't bother as reverb is not handled, will fix if needed + //syscalls.cache_inv(mem, sizeof(mem->spu_ram) + sizeof(mem->SB), 0); + //syscalls.cache_inv(&mem->in, sizeof(mem->in), 0); break; default: -- cgit v1.2.3