2 * Emulation routines for the RF5C164 PCM chip
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
9 #include "../pico_int.h"
12 static unsigned int g_rate = 0; // 18.14 fixed point
14 PICO_INTERNAL_ASM void pcm_write(unsigned int a, unsigned int d)
16 //printf("pcm_write(%i, %02x)\n", a, d);
20 Pico_mcd->pcm.ch[Pico_mcd->pcm.cur_ch].regs[a] = d;
22 else if (a == 7) // control register
24 if (d & 0x40) Pico_mcd->pcm.cur_ch = d & 7;
25 else Pico_mcd->pcm.bank = d & 0xf;
26 Pico_mcd->pcm.control = d;
27 // dprintf("pcm control=%02x", Pico_mcd->pcm.control);
29 else if (a == 8) // sound on/off
31 if (!(Pico_mcd->pcm.enabled & 0x01)) Pico_mcd->pcm.ch[0].addr =
32 Pico_mcd->pcm.ch[0].regs[6] << (PCM_STEP_SHIFT + 8);
33 if (!(Pico_mcd->pcm.enabled & 0x02)) Pico_mcd->pcm.ch[1].addr =
34 Pico_mcd->pcm.ch[1].regs[6] << (PCM_STEP_SHIFT + 8);
35 if (!(Pico_mcd->pcm.enabled & 0x04)) Pico_mcd->pcm.ch[2].addr =
36 Pico_mcd->pcm.ch[2].regs[6] << (PCM_STEP_SHIFT + 8);
37 if (!(Pico_mcd->pcm.enabled & 0x08)) Pico_mcd->pcm.ch[3].addr =
38 Pico_mcd->pcm.ch[3].regs[6] << (PCM_STEP_SHIFT + 8);
39 if (!(Pico_mcd->pcm.enabled & 0x10)) Pico_mcd->pcm.ch[4].addr =
40 Pico_mcd->pcm.ch[4].regs[6] << (PCM_STEP_SHIFT + 8);
41 if (!(Pico_mcd->pcm.enabled & 0x20)) Pico_mcd->pcm.ch[5].addr =
42 Pico_mcd->pcm.ch[5].regs[6] << (PCM_STEP_SHIFT + 8);
43 if (!(Pico_mcd->pcm.enabled & 0x40)) Pico_mcd->pcm.ch[6].addr =
44 Pico_mcd->pcm.ch[6].regs[6] << (PCM_STEP_SHIFT + 8);
45 if (!(Pico_mcd->pcm.enabled & 0x80)) Pico_mcd->pcm.ch[7].addr =
46 Pico_mcd->pcm.ch[7].regs[6] << (PCM_STEP_SHIFT + 8);
47 // printf("addr %x %x %x %x %x %x %x %x\n", Pico_mcd->pcm.ch[0].addr, Pico_mcd->pcm.ch[1].addr
48 // , Pico_mcd->pcm.ch[2].addr, Pico_mcd->pcm.ch[3].addr, Pico_mcd->pcm.ch[4].addr, Pico_mcd->pcm.ch[5].addr
49 // , Pico_mcd->pcm.ch[6].addr, Pico_mcd->pcm.ch[7].addr);
51 Pico_mcd->pcm.enabled = ~d;
52 //printf("enabled=%02x\n", Pico_mcd->pcm.enabled);
57 PICO_INTERNAL void pcm_set_rate(int rate)
59 float step = 31.8 * 1024.0 / (float) rate; // max <4 @ 8000Hz
61 g_rate = (unsigned int) step;
62 if (step - (float) g_rate >= 0.5) g_rate++;
63 elprintf(EL_STATUS, "g_rate: %f %08x\n", (double)step, g_rate);
67 PICO_INTERNAL void pcm_update(int *buffer, int length, int stereo)
70 unsigned int step, addr;
71 int mul_l, mul_r, smp;
76 // PCM disabled or all channels off (to be checked by caller)
77 //if (!(Pico_mcd->pcm.control & 0x80) || !Pico_mcd->pcm.enabled) return;
79 //printf("-- upd %i\n", length);
81 for (i = 0; i < 8; i++)
83 if (!(Pico_mcd->pcm.enabled & (1 << i))) continue; // channel disabled
86 ch = &Pico_mcd->pcm.ch[i];
88 addr = ch->addr; // >> PCM_STEP_SHIFT;
89 mul_l = ((int)ch->regs[0] * (ch->regs[1] & 0xf)) >> (5+1); // (env * pan) >> 5
90 mul_r = ((int)ch->regs[0] * (ch->regs[1] >> 4)) >> (5+1);
91 step = ((unsigned int)(*(unsigned short *)&ch->regs[2]) * g_rate) >> 14; // freq step
92 // fprintf(stderr, "step=%i, cstep=%i, mul_l=%i, mul_r=%i, ch=%i, addr=%x, en=%02x\n",
93 // *(unsigned short *)&ch->regs[2], step, mul_l, mul_r, i, addr, Pico_mcd->pcm.enabled);
95 if (!stereo && mul_l < mul_r) mul_l = mul_r;
97 for (j = 0; j < length; j++)
99 // printf("addr=%08x\n", addr);
100 smp = Pico_mcd->pcm_ram[addr >> PCM_STEP_SHIFT];
102 // test for loop signal
105 addr = *(unsigned short *)&ch->regs[4]; // loop_addr
106 smp = Pico_mcd->pcm_ram[addr];
107 addr <<= PCM_STEP_SHIFT;
108 if (smp == 0xff) break;
111 if (smp & 0x80) smp = -(smp & 0x7f);
113 *out++ += smp * mul_l; // max 128 * 119 = 15232
115 *out++ += smp * mul_r;
117 // update address register
118 k = (addr >> PCM_STEP_SHIFT) + 1;
119 addr = (addr + step) & 0x7FFFFFF;
121 for(; k < (addr >> PCM_STEP_SHIFT); k++)
123 if (Pico_mcd->pcm_ram[k] == 0xff)
125 addr = (unsigned int)(*(unsigned short *)&ch->regs[4]) << PCM_STEP_SHIFT; // loop_addr
131 if (Pico_mcd->pcm_ram[addr >> PCM_STEP_SHIFT] == 0xff)
132 addr = (unsigned int)(*(unsigned short *)&ch->regs[4]) << PCM_STEP_SHIFT; // loop_addr