From 020e517dc5cf78b1f20e17f0e3f3811d8e78ab62 Mon Sep 17 00:00:00 2001 From: Shamus Hammons Date: Wed, 11 Feb 2009 05:03:29 +0000 Subject: [PATCH] Added cycle exact sound handling --- src/sound.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/sound.cpp b/src/sound.cpp index b67659b..6f06dcd 100755 --- a/src/sound.cpp +++ b/src/sound.cpp @@ -85,13 +85,53 @@ void SDLSoundCallback(void * userdata, Uint8 * buffer, int length) extern uint8 sram[]; int cnt = 0; +#define CYCLE_EXACT_SOUND +#ifdef CYCLE_EXACT_SOUND + float overflow = 0.0; + uint32 time = 20; +#endif + while (cnt != length) { // This is close, but not cycle exact (exact would be 20.289115646...) -//Need to figure out how to get that fraction to execute... !!! FIX !!! +//Need to figure out how to get that fraction to execute... !!! FIX !!! +/* +One way would be to use a fractional accumulator, then subtract 1 every +time it overflows. Like so: + +double overflow = 0; +uint32 time = 20; +while (!done) +{ + Execute6808(&soundCPU, time); + overflow += 0.289115646; + if (overflow > 1.0) + { + overflow -= 1.0; + time = 21; + } + else + time = 20; +} +*/ +#ifdef CYCLE_EXACT_SOUND + Execute6808(&soundCPU, time); +#else Execute6808(&soundCPU, 20); +#endif soundCPU.clock -= 20; buffer[cnt++] = sram[0x0400]; // Fill the buffer with the PIA output value +#ifdef CYCLE_EXACT_SOUND + overflow += 0.289115646; + + if (overflow > 1.0) + { + overflow -= 1.0; + time = 21; + } + else + time = 20; +#endif } } -- 2.37.2