diff options
author | notaz | 2015-01-02 04:13:50 +0200 |
---|---|---|
committer | notaz | 2015-01-04 22:40:35 +0200 |
commit | 63a4f6b6a3b0315590cd3009df2c92480ed2d98b (patch) | |
tree | 8103d9fa32988012c108f982ecd68db2c05a9d1b | |
parent | 9ad8abfa940cd5c13eb0653639ea86736b65a2c4 (diff) | |
download | pcsx_rearmed-63a4f6b6a3b0315590cd3009df2c92480ed2d98b.tar.gz pcsx_rearmed-63a4f6b6a3b0315590cd3009df2c92480ed2d98b.tar.bz2 pcsx_rearmed-63a4f6b6a3b0315590cd3009df2c92480ed2d98b.zip |
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
-rw-r--r-- | frontend/main.c | 1 | ||||
-rw-r--r-- | frontend/menu.c | 3 | ||||
-rw-r--r-- | plugins/dfsound/dma.c | 4 | ||||
-rw-r--r-- | plugins/dfsound/externals.h | 8 | ||||
-rw-r--r-- | plugins/dfsound/freeze.c | 2 | ||||
-rw-r--r-- | plugins/dfsound/registers.c | 2 | ||||
-rw-r--r-- | plugins/dfsound/spu.c | 349 | ||||
-rw-r--r-- | plugins/dfsound/spu_config.h | 1 |
8 files changed, 330 insertions, 40 deletions
diff --git a/frontend/main.c b/frontend/main.c index bdea1b5..4c84803 100644 --- a/frontend/main.c +++ b/frontend/main.c @@ -141,6 +141,7 @@ void emu_set_default_config(void) spu_config.iXAPitch = 0; spu_config.iVolume = 768; spu_config.iTempo = 0; + spu_config.iUseThread = 1; // no effect if only 1 core is detected #if defined(__arm__) && !defined(__ARM_ARCH_7A__) /* XXX GPH hack */ spu_config.iUseReverb = 0; spu_config.iUseInterpolation = 0; diff --git a/frontend/menu.c b/frontend/menu.c index a7012e6..a1edac2 100644 --- a/frontend/menu.c +++ b/frontend/menu.c @@ -75,6 +75,7 @@ typedef enum MA_OPT_SAVECFG, MA_OPT_SAVECFG_GAME, MA_OPT_CPU_CLOCKS, + MA_OPT_SPU_THREAD, MA_OPT_DISP_OPTS, MA_OPT_VARSCALER, MA_OPT_VARSCALER_C, @@ -444,6 +445,7 @@ static const struct { CE_INTVAL(spu_config.iXAPitch), CE_INTVAL(spu_config.iUseInterpolation), CE_INTVAL(spu_config.iTempo), + CE_INTVAL(spu_config.iUseThread), CE_INTVAL(config_save_counter), CE_INTVAL(in_evdev_allow_abs_only), CE_INTVAL(volume_boost), @@ -1604,6 +1606,7 @@ static menu_entry e_menu_options[] = mee_onoff ("Show FPS", 0, g_opts, OPT_SHOWFPS), mee_enum ("Region", 0, region, men_region), mee_range ("CPU clock", MA_OPT_CPU_CLOCKS, cpu_clock, 20, 5000), + mee_onoff ("Threaded SPU", MA_OPT_SPU_THREAD, spu_config.iUseThread, 1), mee_handler_id("[Display]", MA_OPT_DISP_OPTS, menu_loop_gfx_options), mee_handler ("[BIOS/Plugins]", menu_loop_plugin_options), mee_handler ("[Advanced]", menu_loop_adv_options), 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<iSize;i++)
{
@@ -82,7 +82,7 @@ void CALLBACK SPUwriteDMAMem(unsigned short *pusPSXMem, int iSize, {
int i;
- do_samples_if_needed(cycles);
+ do_samples_if_needed(cycles, 1);
if(spu.spuAddr + iSize*2 < 0x80000)
{
diff --git a/plugins/dfsound/externals.h b/plugins/dfsound/externals.h index d1d081a..3a3ff68 100644 --- a/plugins/dfsound/externals.h +++ b/plugins/dfsound/externals.h @@ -237,16 +237,16 @@ extern SPUInfo spu; extern SPUCHAN s_chan[];
extern REVERBInfo rvb;
-void do_samples(unsigned int cycles_to);
+void do_samples(unsigned int cycles_to, int do_sync);
void schedule_next_irq(void);
#define regAreaGet(ch,offset) \
spu.regArea[((ch<<4)|(offset))>>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 <sys/time.h> // 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 <pthread.h> +#include <semaphore.h> +#include <unistd.h> + +// 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; |