+ SPUCHAN *s_chan = &spu.s_chan[ch];
+ unsigned char *start = s_chan->pCurr;
+ int flags;
+ int ret = 0;
+
+ if (s_chan->prevflags & 1) {
+ if (!(s_chan->prevflags & 2))
+ ret = 1;
+
+ start = s_chan->pLoop;
+ }
+ else
+ check_irq(ch, start);
+
+ flags = start[1];
+ if (flags & 4)
+ s_chan->pLoop = start;
+
+ start += 16;
+
+ if (flags & 1) {
+ start = s_chan->pLoop;
+ check_irq(ch, start);
+ }
+
+ s_chan->pCurr = start;
+ s_chan->prevflags = flags;
+
+ return ret;
+}
+
+// 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->pCurr;
+ pos = s_chan->spos;
+ sinc = s_chan->sinc;
+ end = pos + *upd_samples * sinc;
+
+ pos += (28 - s_chan->iSBPos) << 16;
+ while (pos < end)
+ {
+ if (block == spu.pSpuIrq)
+ break;
+ flags = block[1];
+ block += 16;
+ if (flags & 1) { // 1: stop/loop
+ block = s_chan->pLoop;
+ if (block == spu.pSpuIrq) // hack.. (see decode_block)
+ break;
+ }
+ pos += 28 << 16;
+ }
+
+ if (pos < end)
+ {
+ sinc_inv = s_chan->sinc_inv;
+ if (sinc_inv == 0)
+ sinc_inv = s_chan->sinc_inv = (0x80000000u / (uint32_t)sinc) << 1;
+
+ 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);
+ }
+}
+
+#define make_do_samples(name, fmod_code, interp_start, interp1_code, interp2_code, interp_end) \
+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; \
+ interp_start; \
+ \
+ for (ns = 0; ns < ns_to; ns++) \
+ { \
+ fmod_code; \
+ \
+ *spos += sinc; \
+ while (*spos >= 0x10000) \
+ { \
+ fa = SB[(*sbpos)++]; \
+ if (*sbpos >= 28) \
+ { \
+ *sbpos = 0; \
+ d = decode_f(ctx, ch, SB); \
+ if (d && ns < ret) \
+ ret = ns; \
+ } \
+ \
+ interp1_code; \
+ *spos -= 0x10000; \
+ } \
+ \
+ interp2_code; \
+ } \
+ \
+ interp_end; \
+ \
+ return ret; \
+}
+
+#define fmod_recv_check \
+ 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, 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 \
+ 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? */ \
+ 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, )
+
+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;
+
+ spos += s_chan->iSBPos << 16;
+
+ for (ns = 0; ns < ns_to; ns++)
+ {
+ spos += sinc;
+ while (spos >= 28*0x10000)
+ {
+ d = skip_block(ch);
+ if (d && ns < ret)
+ ret = ns;
+ spos -= 28*0x10000;
+ }
+ }
+
+ s_chan->iSBPos = spos >> 16;
+ s_chan->spos = 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 = (ctrl >> 10) & 0x0f;
+ level = 0x8000 >> level;
+
+ for (ns = 0; ns < ns_to; ns++)
+ {
+ counter += 2;
+ if (counter >= level)
+ {
+ counter -= level;
+ shift = (val >> 10) & 0x1f;
+ bit = (0x69696969 >> shift) & 1;
+ bit ^= (val >> 15) & 1;
+ val = (val << 1) | bit;
+ }
+
+ 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;
+}
+
+#ifdef HAVE_ARMV5
+// asm code; lv and rv must be 0-3fff
+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);