+ if (ch == 1 || ch == 3)
+ do_decode_bufs(spu.spuMem, ch/2, ns_to, work->decode_pos);
+
+ if (work->ch[ch].bFMod == 2) // fmod freq channel
+ memcpy(iFMod, &ChanBuf, ns_to * sizeof(iFMod[0]));
+ if (work->ch[ch].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_no_thread)
+{
+ int force = force_no_thread;
+ 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);
+
+ MixCD(work->SSumLR, RVB, work->ns_to, work->decode_pos);
+ 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_no_thread && worker->prev_work_in_thread) {
+ unsigned int ch, mask = worker->channels_last;
+ worker->prev_work_in_thread = 0;
+ thread_sync_caches();
+ for (ch = 0; mask != 0; ch++, mask >>= 1) {
+ if (mask & 1)
+ memcpy(spu.sb[ch].SB, spu.sb_thread[ch].SB, sizeof(spu.sb_thread[ch].SB));
+ }
+ }
+}
+
+#else
+
+static void queue_channel_work(int ns_to, int silentch) {}
+static void sync_worker_thread(int force_no_thread) {}
+
+static const void * const worker = NULL;
+
+#endif // P_HAVE_PTHREAD || defined(WANT_THREAD_CODE)
+
+////////////////////////////////////////////////////////////////////////
+// MAIN SPU FUNCTION
+// here is the main job handler...
+////////////////////////////////////////////////////////////////////////
+
+void do_samples(unsigned int cycles_to, int force_no_thread)
+{
+ 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)
+ {
+ log_unhandled("desync %u %d\n", cycles_to, cycle_diff);
+ spu.cycles_played = cycles_to;
+ return;
+ }
+
+ silentch = ~(spu.dwChannelsAudible | spu.dwNewChannel) & 0xffffff;
+
+ force_no_thread |= (silentch == 0xffffff);
+ if (worker != NULL)
+ sync_worker_thread(force_no_thread);
+
+ if (cycle_diff < 2 * 768)
+ return;
+
+ ns_to = (cycle_diff / 768 + 1) & ~1;
+ if (ns_to > NSSIZE) {
+ // should never happen
+ log_unhandled("ns_to oflow %d %d\n", ns_to, NSSIZE);
+ ns_to = NSSIZE;
+ }
+
+ //////////////////////////////////////////////////////