1 // Emulation routines for the RF5C164 PCM chip.
2 // Based on Gens code by Stéphane Dallongeville
3 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
5 #include "../pico_int.h"
8 static unsigned int g_rate = 0; // 18.14 fixed point
10 PICO_INTERNAL_ASM void pcm_write(unsigned int a, unsigned int d)
12 //printf("pcm_write(%i, %02x)\n", a, d);
16 Pico_mcd->pcm.ch[Pico_mcd->pcm.cur_ch].regs[a] = d;
18 else if (a == 7) // control register
20 if (d & 0x40) Pico_mcd->pcm.cur_ch = d & 7;
21 else Pico_mcd->pcm.bank = d & 0xf;
22 Pico_mcd->pcm.control = d;
23 // dprintf("pcm control=%02x", Pico_mcd->pcm.control);
25 else if (a == 8) // sound on/off
27 if (!(Pico_mcd->pcm.enabled & 0x01)) Pico_mcd->pcm.ch[0].addr =
28 Pico_mcd->pcm.ch[0].regs[6] << (PCM_STEP_SHIFT + 8);
29 if (!(Pico_mcd->pcm.enabled & 0x02)) Pico_mcd->pcm.ch[1].addr =
30 Pico_mcd->pcm.ch[1].regs[6] << (PCM_STEP_SHIFT + 8);
31 if (!(Pico_mcd->pcm.enabled & 0x04)) Pico_mcd->pcm.ch[2].addr =
32 Pico_mcd->pcm.ch[2].regs[6] << (PCM_STEP_SHIFT + 8);
33 if (!(Pico_mcd->pcm.enabled & 0x08)) Pico_mcd->pcm.ch[3].addr =
34 Pico_mcd->pcm.ch[3].regs[6] << (PCM_STEP_SHIFT + 8);
35 if (!(Pico_mcd->pcm.enabled & 0x10)) Pico_mcd->pcm.ch[4].addr =
36 Pico_mcd->pcm.ch[4].regs[6] << (PCM_STEP_SHIFT + 8);
37 if (!(Pico_mcd->pcm.enabled & 0x20)) Pico_mcd->pcm.ch[5].addr =
38 Pico_mcd->pcm.ch[5].regs[6] << (PCM_STEP_SHIFT + 8);
39 if (!(Pico_mcd->pcm.enabled & 0x40)) Pico_mcd->pcm.ch[6].addr =
40 Pico_mcd->pcm.ch[6].regs[6] << (PCM_STEP_SHIFT + 8);
41 if (!(Pico_mcd->pcm.enabled & 0x80)) Pico_mcd->pcm.ch[7].addr =
42 Pico_mcd->pcm.ch[7].regs[6] << (PCM_STEP_SHIFT + 8);
43 // printf("addr %x %x %x %x %x %x %x %x\n", Pico_mcd->pcm.ch[0].addr, Pico_mcd->pcm.ch[1].addr
44 // , Pico_mcd->pcm.ch[2].addr, Pico_mcd->pcm.ch[3].addr, Pico_mcd->pcm.ch[4].addr, Pico_mcd->pcm.ch[5].addr
45 // , Pico_mcd->pcm.ch[6].addr, Pico_mcd->pcm.ch[7].addr);
47 Pico_mcd->pcm.enabled = ~d;
48 //printf("enabled=%02x\n", Pico_mcd->pcm.enabled);
53 PICO_INTERNAL void pcm_set_rate(int rate)
55 float step = 31.8 * 1024.0 / (float) rate; // max <4 @ 8000Hz
57 g_rate = (unsigned int) step;
58 if (step - (float) g_rate >= 0.5) g_rate++;
59 elprintf(EL_STATUS, "g_rate: %f %08x\n", (double)step, g_rate);
63 PICO_INTERNAL void pcm_update(int *buffer, int length, int stereo)
66 unsigned int step, addr;
67 int mul_l, mul_r, smp;
72 // PCM disabled or all channels off (to be checked by caller)
73 //if (!(Pico_mcd->pcm.control & 0x80) || !Pico_mcd->pcm.enabled) return;
75 //printf("-- upd %i\n", length);
77 for (i = 0; i < 8; i++)
79 if (!(Pico_mcd->pcm.enabled & (1 << i))) continue; // channel disabled
82 ch = &Pico_mcd->pcm.ch[i];
84 addr = ch->addr; // >> PCM_STEP_SHIFT;
85 mul_l = ((int)ch->regs[0] * (ch->regs[1] & 0xf)) >> (5+1); // (env * pan) >> 5
86 mul_r = ((int)ch->regs[0] * (ch->regs[1] >> 4)) >> (5+1);
87 step = ((unsigned int)(*(unsigned short *)&ch->regs[2]) * g_rate) >> 14; // freq step
88 // fprintf(stderr, "step=%i, cstep=%i, mul_l=%i, mul_r=%i, ch=%i, addr=%x, en=%02x\n",
89 // *(unsigned short *)&ch->regs[2], step, mul_l, mul_r, i, addr, Pico_mcd->pcm.enabled);
91 if (!stereo && mul_l < mul_r) mul_l = mul_r;
93 for (j = 0; j < length; j++)
95 // printf("addr=%08x\n", addr);
96 smp = Pico_mcd->pcm_ram[addr >> PCM_STEP_SHIFT];
98 // test for loop signal
101 addr = *(unsigned short *)&ch->regs[4]; // loop_addr
102 smp = Pico_mcd->pcm_ram[addr];
103 addr <<= PCM_STEP_SHIFT;
104 if (smp == 0xff) break;
107 if (smp & 0x80) smp = -(smp & 0x7f);
109 *out++ += smp * mul_l; // max 128 * 119 = 15232
111 *out++ += smp * mul_r;
113 // update address register
114 k = (addr >> PCM_STEP_SHIFT) + 1;
115 addr = (addr + step) & 0x7FFFFFF;
117 for(; k < (addr >> PCM_STEP_SHIFT); k++)
119 if (Pico_mcd->pcm_ram[k] == 0xff)
121 addr = (unsigned int)(*(unsigned short *)&ch->regs[4]) << PCM_STEP_SHIFT; // loop_addr
127 if (Pico_mcd->pcm_ram[addr >> PCM_STEP_SHIFT] == 0xff)
128 addr = (unsigned int)(*(unsigned short *)&ch->regs[4]) << PCM_STEP_SHIFT; // loop_addr