Refactored sound code - slight speedup.

This commit is contained in:
TomW 2016-02-10 20:49:28 +00:00
commit 3a57341837
33 changed files with 706 additions and 535 deletions

View file

@ -3,6 +3,7 @@
AD1848 CODEC emulation (Windows Sound System compatible)*/
#include "ibm.h"
#include "sound.h"
#include "sound_ad1848.h"
static int ad1848_vols[64];
@ -66,14 +67,14 @@ void ad1848_write(uint16_t addr, uint8_t val, void *p)
case 6: freq /= 512; break;
case 7: freq /= 2560; break;
}
ad1848->inc = (int)((freq * 65536) / 48000);
ad1848->freq = freq;
ad1848->timer_latch = (int)((double)TIMER_USEC * (1000000.0 / (double)ad1848->freq));
break;
case 9:
if (!ad1848->enable)
ad1848->interp_count = 0;
ad1848->enable = ((val & 0x41) == 0x01);
if (!ad1848->enable)
ad1848->out_l = ad1848->out_r = 0;
break;
case 12:
@ -91,71 +92,84 @@ void ad1848_write(uint16_t addr, uint8_t val, void *p)
}
}
void ad1848_poll(void *p, int16_t *l, int16_t *r)
void ad1848_speed_changed(ad1848_t *ad1848)
{
ad1848->timer_latch = (int)((double)TIMER_USEC * (1000000.0 / (double)ad1848->freq));
}
void ad1848_update(ad1848_t *ad1848)
{
for (; ad1848->pos < sound_pos_global; ad1848->pos++)
{
ad1848->buffer[ad1848->pos*2] = ad1848->out_l;
ad1848->buffer[ad1848->pos*2 + 1] = ad1848->out_r;
}
}
static void ad1848_poll(void *p)
{
ad1848_t *ad1848 = (ad1848_t *)p;
if (ad1848->timer_latch)
ad1848->timer_count += ad1848->timer_latch;
else
ad1848->timer_count = TIMER_USEC;
// opl3_poll(&ad1848->opl, &ad1848->opl_buffer[ad1848->pos * 2], &ad1848->opl_buffer[(ad1848->pos * 2) + 1]);
ad1848_update(ad1848);
if (ad1848->enable)
{
int32_t temp;
switch (ad1848->regs[8] & 0x70)
{
case 0x00: /*Mono, 8-bit PCM*/
ad1848->out_l = ad1848->out_r = (dma_channel_read(ad1848->dma) ^ 0x80) * 256;
break;
case 0x10: /*Stereo, 8-bit PCM*/
ad1848->out_l = (dma_channel_read(ad1848->dma) ^ 0x80) * 256;
ad1848->out_r = (dma_channel_read(ad1848->dma) ^ 0x80) * 256;
break;
case 0x40: /*Mono, 16-bit PCM*/
temp = dma_channel_read(ad1848->dma);
ad1848->out_l = ad1848->out_r = (dma_channel_read(ad1848->dma) << 8) | temp;
break;
case 0x50: /*Stereo, 16-bit PCM*/
temp = dma_channel_read(ad1848->dma);
ad1848->out_l = (dma_channel_read(ad1848->dma) << 8) | temp;
temp = dma_channel_read(ad1848->dma);
ad1848->out_r = (dma_channel_read(ad1848->dma) << 8) | temp;
break;
}
if (ad1848->regs[6] & 0x80)
*l = 0;
ad1848->out_l = 0;
else
*l = (ad1848->out_l * ad1848_vols[ad1848->regs[6] & 0x3f]) >> 16;
ad1848->out_l = (ad1848->out_l * ad1848_vols[ad1848->regs[6] & 0x3f]) >> 16;
if (ad1848->regs[7] & 0x80)
*r = 0;
ad1848->out_r = 0;
else
*r = (ad1848->out_r * ad1848_vols[ad1848->regs[7] & 0x3f]) >> 16;
ad1848->out_r = (ad1848->out_r * ad1848_vols[ad1848->regs[7] & 0x3f]) >> 16;
ad1848->interp_count += ad1848->inc;
if (ad1848->interp_count >= 0x10000)
if (ad1848->count < 0)
{
ad1848->interp_count -= 0x10000;
if (ad1848->count < 0)
{
ad1848->count = ad1848->regs[15] | (ad1848->regs[14] << 8);
if (!(ad1848->status & 0x01))
{
ad1848->status |= 0x01;
if (ad1848->regs[0xa] & 2)
picint(1 << ad1848->irq);
}
}
switch (ad1848->regs[8] & 0x70)
ad1848->count = ad1848->regs[15] | (ad1848->regs[14] << 8);
if (!(ad1848->status & 0x01))
{
case 0x00: /*Mono, 8-bit PCM*/
ad1848->out_l = ad1848->out_r = (dma_channel_read(ad1848->dma) ^ 0x80) * 256;
break;
case 0x10: /*Stereo, 8-bit PCM*/
ad1848->out_l = (dma_channel_read(ad1848->dma) ^ 0x80) * 256;
ad1848->out_r = (dma_channel_read(ad1848->dma) ^ 0x80) * 256;
break;
case 0x40: /*Mono, 16-bit PCM*/
temp = dma_channel_read(ad1848->dma);
ad1848->out_l = ad1848->out_r = dma_channel_read(ad1848->dma) | (temp << 8);
break;
case 0x50: /*Stereo, 16-bit PCM*/
temp = dma_channel_read(ad1848->dma);
ad1848->out_l = dma_channel_read(ad1848->dma) | (temp << 8);
temp = dma_channel_read(ad1848->dma);
ad1848->out_r = dma_channel_read(ad1848->dma) | (temp << 8);
break;
}
ad1848->count--;
ad1848->status |= 0x01;
if (ad1848->regs[0xa] & 2)
picint(1 << ad1848->irq);
}
}
ad1848->count--;
// pclog("ad1848_poll : enable %X %X %X %X %X %X\n", ad1848->pcm_buffer[0][ad1848->pos], ad1848->pcm_buffer[1][ad1848->pos], ad1848->out_l[0], ad1848->out_r[0], ad1848->out_l[1], ad1848->out_r[1]);
}
else
{
*l = *r = 0;
ad1848->out_l = ad1848->out_r = 0;
// pclog("ad1848_poll : not enable\n");
}
}
@ -184,7 +198,6 @@ void ad1848_init(ad1848_t *ad1848)
ad1848->out_l = 0;
ad1848->out_r = 0;
ad1848->interp_count = 0;
for (c = 0; c < 64; c++)
{
@ -201,4 +214,6 @@ void ad1848_init(ad1848_t *ad1848)
ad1848_vols[c] = (int)(attenuation * 65536);
// pclog("ad1848_vols %i = %f %i\n", c, attenuation, ad1848_vols[c]);
}
timer_add(ad1848_poll, &ad1848->timer_count, &ad1848->enable, ad1848);
}