| 1 | // Emulation routines for the RF5C164 PCM chip. |
| 2 | // Based on Gens code by Stéphane Dallongeville |
| 3 | // (c) Copyright 2007, Grazvydas "notaz" Ignotas |
| 4 | |
| 5 | #include "../PicoInt.h" |
| 6 | #include "pcm.h" |
| 7 | |
| 8 | static unsigned int g_rate = 0; // 18.14 fixed point |
| 9 | |
| 10 | PICO_INTERNAL_ASM void pcm_write(unsigned int a, unsigned int d) |
| 11 | { |
| 12 | //printf("pcm_write(%i, %02x)\n", a, d); |
| 13 | |
| 14 | if (a < 7) |
| 15 | { |
| 16 | Pico_mcd->pcm.ch[Pico_mcd->pcm.cur_ch].regs[a] = d; |
| 17 | } |
| 18 | else if (a == 7) // control register |
| 19 | { |
| 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); |
| 24 | } |
| 25 | else if (a == 8) // sound on/off |
| 26 | { |
| 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); |
| 46 | |
| 47 | Pico_mcd->pcm.enabled = ~d; |
| 48 | //printf("enabled=%02x\n", Pico_mcd->pcm.enabled); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | PICO_INTERNAL void pcm_set_rate(int rate) |
| 54 | { |
| 55 | float step = 31.8 * 1024.0 / (float) rate; // max <4 @ 8000Hz |
| 56 | step *= 256*256/4; |
| 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); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | PICO_INTERNAL void pcm_update(int *buffer, int length, int stereo) |
| 64 | { |
| 65 | struct pcm_chan *ch; |
| 66 | unsigned int step, addr; |
| 67 | int mul_l, mul_r, smp; |
| 68 | int i, j, k; |
| 69 | int *out; |
| 70 | |
| 71 | |
| 72 | // PCM disabled or all channels off (to be checked by caller) |
| 73 | //if (!(Pico_mcd->pcm.control & 0x80) || !Pico_mcd->pcm.enabled) return; |
| 74 | |
| 75 | //printf("-- upd %i\n", length); |
| 76 | |
| 77 | for (i = 0; i < 8; i++) |
| 78 | { |
| 79 | if (!(Pico_mcd->pcm.enabled & (1 << i))) continue; // channel disabled |
| 80 | |
| 81 | out = buffer; |
| 82 | ch = &Pico_mcd->pcm.ch[i]; |
| 83 | |
| 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); |
| 90 | |
| 91 | if (!stereo && mul_l < mul_r) mul_l = mul_r; |
| 92 | |
| 93 | for (j = 0; j < length; j++) |
| 94 | { |
| 95 | // printf("addr=%08x\n", addr); |
| 96 | smp = Pico_mcd->pcm_ram[addr >> PCM_STEP_SHIFT]; |
| 97 | |
| 98 | // test for loop signal |
| 99 | if (smp == 0xff) |
| 100 | { |
| 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; |
| 105 | } |
| 106 | |
| 107 | if (smp & 0x80) smp = -(smp & 0x7f); |
| 108 | |
| 109 | *out++ += smp * mul_l; // max 128 * 119 = 15232 |
| 110 | if(stereo) |
| 111 | *out++ += smp * mul_r; |
| 112 | |
| 113 | // update address register |
| 114 | k = (addr >> PCM_STEP_SHIFT) + 1; |
| 115 | addr = (addr + step) & 0x7FFFFFF; |
| 116 | |
| 117 | for(; k < (addr >> PCM_STEP_SHIFT); k++) |
| 118 | { |
| 119 | if (Pico_mcd->pcm_ram[k] == 0xff) |
| 120 | { |
| 121 | addr = (unsigned int)(*(unsigned short *)&ch->regs[4]) << PCM_STEP_SHIFT; // loop_addr |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 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 |
| 129 | |
| 130 | ch->addr = addr; |
| 131 | } |
| 132 | } |
| 133 | |