+static void do_silent_chans(int ns_to, int silentch)
+{
+ unsigned int mask;
+ SPUCHAN *s_chan;
+ int ch;
+
+ mask = silentch & 0xffffff;
+ for (ch = 0; mask != 0; ch++, mask >>= 1)
+ {
+ if (!(mask & 1)) continue;
+ if (spu.dwChannelDead & (1<<ch)) continue;
+
+ s_chan = &spu.s_chan[ch];
+ if (s_chan->pCurr > spu.pSpuIrq && s_chan->pLoop > spu.pSpuIrq)
+ continue;
+
+ s_chan->spos += s_chan->iSBPos << 16;
+ s_chan->iSBPos = 0;
+
+ s_chan->spos += s_chan->sinc * ns_to;
+ while (s_chan->spos >= 28 * 0x10000)
+ {
+ unsigned char *start = s_chan->pCurr;
+
+ skip_block(ch);
+ if (start == s_chan->pCurr || start - spu.spuMemC < 0x1000)
+ {
+ // looping on self or stopped(?)
+ spu.dwChannelDead |= 1<<ch;
+ s_chan->spos = 0;
+ break;
+ }
+
+ s_chan->spos -= 28 * 0x10000;
+ }
+ }
+}
+
+static void do_channels(int ns_to)
+{
+ unsigned int mask;
+ int do_rvb, ch, d;
+ SPUCHAN *s_chan;
+ int *SB, sinc;
+
+ 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) {
+ if (mask & 1)
+ StartSound(ch);
+ }
+
+ mask = spu.dwChannelOn & 0xffffff;
+ for (ch = 0; mask != 0; ch++, mask >>= 1) // loop em all...
+ {
+ if (!(mask & 1)) continue; // channel not playing? next
+
+ s_chan = &spu.s_chan[ch];
+ SB = spu.SB + ch * SB_SIZE;
+ sinc = s_chan->sinc;
+
+ if (s_chan->bNoise)
+ 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, 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, NULL, ch, ns_to,
+ SB, sinc, &s_chan->spos, &s_chan->iSBPos);
+ else
+ d = do_samples_default(decode_block, NULL, ch, ns_to,
+ SB, sinc, &s_chan->spos, &s_chan->iSBPos);
+
+ d = MixADSR(&s_chan->ADSRX, d);
+ if (d < ns_to) {
+ spu.dwChannelOn &= ~(1 << ch);
+ s_chan->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->bFMod == 2) // fmod freq channel
+ memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
+ 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)
+ 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 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 last_boot_cnt; // dsp
+ unsigned int ram_dirty;
+ };
+ // 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;
+ int rvb_addr;
+ 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;
+ short vol_l;
+ short vol_r;
+ ADSRInfoEx adsr;
+ // might also want to add fmod flags..
+ } ch[24];
+ 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(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)
+{
+ 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, unsigned int silentch)
+{
+ struct work_item *work;
+ SPUCHAN *s_chan;
+ unsigned int mask;
+ int ch, d;
+
+ 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;
+
+ for (ch = 0; mask != 0; ch++, mask >>= 1)
+ {
+ if (!(mask & 1)) continue;
+
+ s_chan = &spu.s_chan[ch];
+ 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].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)
+ work->ch[ch].start = work->ch[ch].loop;
+
+ 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;
+ }
+ }
+
+ work->rvb_addr = 0;
+ if (spu.rvb->StartAddr) {
+ if (spu_config.iUseReverb)
+ 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();
+}
+
+static void do_channel_work(struct work_item *work)
+{
+ unsigned int mask;
+ unsigned int decode_dirty_ch = 0;
+ const SPUCHAN *s_chan;
+ int *SB, sinc, spos, sbpos;
+ int d, ch, ns_to;
+
+ ns_to = work->ns_to;
+
+ 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) {
+ 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 = 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, 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, work, ch, d, SB, sinc, &spos, &sbpos);
+ else if (s_chan->bFMod == 0 && spu_config.iUseInterpolation == 1)
+ do_samples_simple(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos);
+ else
+ do_samples_default(decode_block_work, work, ch, d, SB, sinc, &spos, &sbpos);
+
+ d = MixADSR(&work->ch[ch].adsr, d);
+ if (d < ns_to) {
+ work->ch[ch].adsr.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, 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 && work->rvb_addr)
+ mix_chan_rvb(work->SSumLR, ns_to,
+ work->ch[ch].vol_l, work->ch[ch].vol_r, RVB);
+ else
+ 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)
+{
+ struct work_item *work;
+ int done, used_space;
+
+ // rvb offsets will change, thread may be using them
+ force |= spu.rvb->dirty && spu.rvb->StartAddr;
+
+ 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);
+
+ 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);
+
+ 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
+
+static void queue_channel_work(int ns_to, int silentch) {}
+static void sync_worker_thread(int force) {}
+
+static const void * const worker = NULL;
+
+#endif // THREAD_ENABLED
+