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

@ -21,7 +21,7 @@ typedef struct pssj_t
int amplitude;
int irq;
int timer_count;
pc_timer_t timer;
int enable;
int wave_pos;
@ -46,7 +46,13 @@ static void pssj_write(uint16_t port, uint8_t val, void *p)
{
case 0:
pssj->ctrl = val;
if (!pssj->enable && ((val & 4) && (pssj->ctrl & 3)))
timer_set_delay_u64(&pssj->timer, (TIMER_USEC * (1000000.0 / 3579545.0) * (double)(pssj->freq ? pssj->freq : 0x400)));
pssj->enable = (val & 4) && (pssj->ctrl & 3);
if (!pssj->enable)
timer_disable(&pssj->timer);
sn74689_set_extra_divide(&pssj->sn76489, val & 0x40);
if (!(val & 8))
pssj->irq = 0;
@ -166,7 +172,7 @@ static void pssj_callback(void *p)
pssj->wave_pos = (pssj->wave_pos + 1) & 31;
}
pssj->timer_count += (int)(TIMER_USEC * (1000000.0 / 3579545.0) * (double)(pssj->freq ? pssj->freq : 0x400));
timer_advance_u64(&pssj->timer, (TIMER_USEC * (1000000.0 / 3579545.0) * (double)(pssj->freq ? pssj->freq : 0x400)));
}
static void pssj_get_buffer(int32_t *buffer, int len, void *p)
@ -190,7 +196,7 @@ void *pssj_init()
sn76489_init(&pssj->sn76489, 0x00c0, 0x0004, PSSJ, 3579545);
io_sethandler(0x00C4, 0x0004, pssj_read, NULL, NULL, pssj_write, NULL, NULL, pssj);
timer_add(pssj_callback, &pssj->timer_count, &pssj->enable, pssj);
timer_add(&pssj->timer, pssj_callback, pssj, 0);
sound_add_handler(pssj_get_buffer, pssj);
return pssj;