Rewrote timer code.

The new code is based on timestamps, rather than delays. This eliminates the
need to update every timer in timer_process(), which cost a non-trivial amount
of CPU time on low-end machines.

This involved changes to every user of the timer code, so this change almost
certainly introduces bugs.
This commit is contained in:
SarahW 2018-08-02 22:06:10 +01:00
commit a7d006b637
79 changed files with 1050 additions and 1033 deletions

View file

@ -42,13 +42,14 @@ typedef struct gus_t
int16_t buffer[2][MAXSOUNDBUFLEN];
int pos;
int samp_timer, samp_latch;
pc_timer_t samp_timer;
uint64_t samp_latch;
uint8_t *ram;
int irqnext;
int timer_1, timer_2;
pc_timer_t timer_1, timer_2;
int irq, dma, irq_midi;
int latch_enable;
@ -365,9 +366,9 @@ gus->curx[gus->voice]=(gus->curx[gus->voice]&0xFFF8000)|((val&0x7F)<<8);
gus->global=val;
// printf("GUS voices %i\n",val&31);
if (gus->voices < 14)
gus->samp_latch = (int)(TIMER_USEC * (1000000.0 / 44100.0));
gus->samp_latch = (uint64_t)(TIMER_USEC * (1000000.0 / 44100.0));
else
gus->samp_latch = (int)(TIMER_USEC * (1000000.0 / gusfreqs[gus->voices - 14]));
gus->samp_latch = (uint64_t)(TIMER_USEC * (1000000.0 / gusfreqs[gus->voices - 14]));
break;
case 0x41: /*DMA*/
@ -824,7 +825,7 @@ void gus_poll_timer_1(void *p)
{
gus_t *gus = (gus_t *)p;
gus->timer_1 += (TIMER_USEC * 80);
timer_advance_u64(&gus->timer_1, (uint64_t)(TIMER_USEC * 80));
// pclog("gus_poll_timer_1 %i %i %i %i %02X\n", gustime, gus->t1on, gus->t1, gus->t1l, gus->tctrl);
if (gus->t1on)
{
@ -859,7 +860,7 @@ void gus_poll_timer_2(void *p)
{
gus_t *gus = (gus_t *)p;
gus->timer_2 += (TIMER_USEC * 320);
timer_advance_u64(&gus->timer_2, (uint64_t)(TIMER_USEC * 320));
// pclog("pollgus2 %i %i %i %i %02X\n", gustime, gus->t2on, gus->t2, gus->t2l, gus->tctrl);
if (gus->t2on)
{
@ -919,7 +920,7 @@ void gus_poll_wave(void *p)
gus_update(gus);
gus->samp_timer += gus->samp_latch;
timer_advance_u64(&gus->samp_timer, gus->samp_latch);
gus->out_l = gus->out_r = 0;
@ -1120,7 +1121,7 @@ void *gus_init()
printf("Top volume %f %f %f %f\n",vol16bit[4095],vol16bit[3800],vol16bit[3000],vol16bit[2048]);
gus->voices=14;
gus->samp_timer = gus->samp_latch = (int)(TIMER_USEC * (1000000.0 / 44100.0));
gus->samp_latch = (uint64_t)(TIMER_USEC * (1000000.0 / 44100.0));
gus->t1l = gus->t2l = 0xff;
@ -1128,9 +1129,9 @@ void *gus_init()
io_sethandler(0x0340, 0x0010, readgus, NULL, NULL, writegus, NULL, NULL, gus);
io_sethandler(0x0746, 0x0001, readgus, NULL, NULL, writegus, NULL, NULL, gus);
io_sethandler(0x0388, 0x0002, readgus, NULL, NULL, writegus, NULL, NULL, gus);
timer_add(gus_poll_wave, &gus->samp_timer, TIMER_ALWAYS_ENABLED, gus);
timer_add(gus_poll_timer_1, &gus->timer_1, TIMER_ALWAYS_ENABLED, gus);
timer_add(gus_poll_timer_2, &gus->timer_2, TIMER_ALWAYS_ENABLED, gus);
timer_add(&gus->samp_timer, gus_poll_wave, gus, 1);
timer_add(&gus->timer_1, gus_poll_timer_1, gus, 1);
timer_add(&gus->timer_2, gus_poll_timer_2, gus, 1);
sound_add_handler(gus_get_buffer, gus);
@ -1150,9 +1151,9 @@ void gus_speed_changed(void *p)
gus_t *gus = (gus_t *)p;
if (gus->voices < 14)
gus->samp_latch = (int)(TIMER_USEC * (1000000.0 / 44100.0));
gus->samp_latch = (uint64_t)(TIMER_USEC * (1000000.0 / 44100.0));
else
gus->samp_latch = (int)(TIMER_USEC * (1000000.0 / gusfreqs[gus->voices - 14]));
gus->samp_latch = (uint64_t)(TIMER_USEC * (1000000.0 / gusfreqs[gus->voices - 14]));
}
device_t gus_device =