- struct pcm_chan *ch;
- unsigned int step, addr;
- int mul_l, mul_r, smp;
- int i, j, k;
- int *out;
-
-
- // PCM disabled or all channels off (to be checked by caller)
- //if (!(Pico_mcd->pcm.control & 0x80) || !Pico_mcd->pcm.enabled) return;
-
-//printf("-- upd %i\n", length);
-
- for (i = 0; i < 8; i++)
- {
- if (!(Pico_mcd->pcm.enabled & (1 << i))) continue; // channel disabled
-
- out = buffer;
- ch = &Pico_mcd->pcm.ch[i];
-
- addr = ch->addr; // >> PCM_STEP_SHIFT;
- mul_l = ((int)ch->regs[0] * (ch->regs[1] & 0xf)) >> (5+1); // (env * pan) >> 5
- mul_r = ((int)ch->regs[0] * (ch->regs[1] >> 4)) >> (5+1);
- step = ((unsigned int)(*(unsigned short *)&ch->regs[2]) * g_rate) >> 14; // freq step
-// fprintf(stderr, "step=%i, cstep=%i, mul_l=%i, mul_r=%i, ch=%i, addr=%x, en=%02x\n",
-// *(unsigned short *)&ch->regs[2], step, mul_l, mul_r, i, addr, Pico_mcd->pcm.enabled);
-
- if (!stereo && mul_l < mul_r) mul_l = mul_r;
-
- for (j = 0; j < length; j++)
- {
-// printf("addr=%08x\n", addr);
- smp = Pico_mcd->pcm_ram[addr >> PCM_STEP_SHIFT];
-
- // test for loop signal
- if (smp == 0xff)
- {
- addr = *(unsigned short *)&ch->regs[4]; // loop_addr
- smp = Pico_mcd->pcm_ram[addr];
- addr <<= PCM_STEP_SHIFT;
- if (smp == 0xff) break;
- }
-
- if (smp & 0x80) smp = -(smp & 0x7f);
-
- *out++ += smp * mul_l; // max 128 * 119 = 15232
- if(stereo)
- *out++ += smp * mul_r;
-
- // update address register
- k = (addr >> PCM_STEP_SHIFT) + 1;
- addr = (addr + step) & 0x7FFFFFF;
-
- for(; k < (addr >> PCM_STEP_SHIFT); k++)
- {
- if (Pico_mcd->pcm_ram[k] == 0xff)
- {
- addr = (unsigned int)(*(unsigned short *)&ch->regs[4]) << PCM_STEP_SHIFT; // loop_addr
- break;
- }
- }
- }
-
- if (Pico_mcd->pcm_ram[addr >> PCM_STEP_SHIFT] == 0xff)
- addr = (unsigned int)(*(unsigned short *)&ch->regs[4]) << PCM_STEP_SHIFT; // loop_addr
-
- ch->addr = addr;
- }
+ int step, *pcm;
+ int p = 0;
+
+ pcd_pcm_sync(SekCyclesDoneS68k());
+
+ if (!Pico_mcd->pcm_mixbuf_dirty || !(PicoOpt & POPT_EN_MCD_PCM))
+ goto out;
+
+ step = (Pico_mcd->pcm_mixpos << 16) / length;
+ pcm = Pico_mcd->pcm_mixbuf;
+
+ if (stereo) {
+ while (length-- > 0) {
+ *buf32++ += pcm[0];
+ *buf32++ += pcm[1];
+
+ p += step;
+ pcm += (p >> 16) * 2;
+ p &= 0xffff;
+ }
+ }
+ else {
+ while (length-- > 0) {
+ // mostly unused
+ *buf32++ += pcm[0];
+
+ p += step;
+ pcm += (p >> 16) * 2;
+ p &= 0xffff;
+ }
+ }
+
+ memset(Pico_mcd->pcm_mixbuf, 0,
+ Pico_mcd->pcm_mixpos * 2 * sizeof(Pico_mcd->pcm_mixbuf[0]));
+
+out:
+ Pico_mcd->pcm_mixbuf_dirty = 0;
+ Pico_mcd->pcm_mixpos = 0;