// --- ---------- ------------------------------------------------------------
// JLH 06/15/2006 Added changelog ;-)
// JLH 06/15/2006 Scrubbed all BYTE, WORD & DWORD references from the code
+// JLH 02/10/2009 Fixed sound IRQ callback so that CPU samples are taken at
+// cycle exact boundaries
+// JLH 07/21/2009 Moved numeric constants into macros for sanity's sake
//
// Notes:
// The sound CPU (6808) runs at 894,750 (3,579,000 / 4) Hz.
#include "types.h"
#include "log.h"
#include "v6808.h"
+#include "timing.h"
//using namespace std;
+//#define AUDIO_SAMPLE_RATE 44100.0
+#define AUDIO_SAMPLE_RATE 48000.0
+#define CYCLES_TO_EXECUTE (M6808_CLOCK_SPEED_IN_HZ / AUDIO_SAMPLE_RATE)
+
// Local variables
-SDL_AudioSpec desired;
-bool soundInitialized = false;
+static SDL_AudioSpec desired;
+static bool soundInitialized = false;
+static uint32 cyclesToExecuteWholePart;
+static double cyclesToExecuteFractionalPart;
// Private function prototypes
{
// memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
- desired.freq = 44100; // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
+ desired.freq = AUDIO_SAMPLE_RATE; // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
desired.format = AUDIO_U8; // This uses the native endian (for portability)...
desired.channels = 1;
// desired.samples = 4096; // Let's try a 4K buffer (can always go lower)
if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
{
WriteLog("Sound: Failed to initialize SDL sound.\n");
-// exit(1);
return;
}
+ // Setup clock cycles & etc.
+ cyclesToExecuteWholePart = (uint32)CYCLES_TO_EXECUTE;
+ cyclesToExecuteFractionalPart = CYCLES_TO_EXECUTE - (double)cyclesToExecuteWholePart;
+#if 0
+printf("Cycles to execute: %lf; cycles W: %u; cycles F: %lf\n", CYCLES_TO_EXECUTE, cyclesToExecuteWholePart, cyclesToExecuteFractionalPart);
+#endif
+
SDL_PauseAudio(false); // Start playback!
soundInitialized = true;
WriteLog("Sound: Successfully initialized.\n");
void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
{
extern V6808REGS soundCPU;
-// extern uint8 * sram;
extern uint8 sram[];
int cnt = 0;
-#define CYCLE_EXACT_SOUND
-#ifdef CYCLE_EXACT_SOUND
- float overflow = 0.0;
- uint32 time = 20;
-#endif
+ static float overflow = 0.0;
+ static uint32 time = cyclesToExecuteWholePart;
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 !!!
-/*
-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;
+ soundCPU.clock -= time;
buffer[cnt++] = sram[0x0400]; // Fill the buffer with the PIA output value
-#ifdef CYCLE_EXACT_SOUND
- overflow += 0.289115646;
+ time = cyclesToExecuteWholePart;
+ overflow += cyclesToExecuteFractionalPart;
if (overflow > 1.0)
{
overflow -= 1.0;
- time = 21;
+ time++;
}
- else
- time = 20;
-#endif
}
}