static bool cpuFinished = false;
static bool cpuSleep = false;
+
// Let's try a thread...
/*
Here's how it works: Execute 1 frame's worth, then sleep.
#endif
SDL_SemWait(mainSem);
+// There are exactly 800 slices of 21.333 cycles per frame, so it works out
+// evenly.
+#if 0
uint32_t cycles = 17066;
#ifdef CPU_THREAD_OVERFLOW_COMPENSATION
// ODD! It's closer *without* this overflow compensation. ??? WHY ???
WriteLog("CPU: AdjustLastToggleCycles(mainCPU.clock);\n");
#endif
AdjustLastToggleCycles(mainCPU.clock);
+#else
+#ifdef THREAD_DEBUGGING
+WriteLog("CPU: Execute65C02(&mainCPU, cycles);\n");
+#endif
+ for(int i=0; i<800; i++)
+ {
+ uint32_t cycles = 21;
+ overflow += 0.333333334;
+
+ if (overflow > 1.0)
+ {
+ cycles++;
+ overflow -= 1.0;
+ }
+
+ Execute65C02(&mainCPU, cycles);
+ WriteSampleToBuffer();
+ }
+#endif
+
#ifdef THREAD_DEBUGGING
WriteLog("CPU: SDL_mutexP(cpuMutex);\n");
}
#endif
+
// Test GUI function
Element * TestWindow(void)
return win;
}
+
Element * QuitEmulator(void)
{
gui->Stop();
return NULL;
}
+
/*
Small Apple II memory map:
ram[addr] = b;
}
+
//
// Load a file into RAM/ROM image space
//
return true;
}
+
static void SaveApple2State(const char * filename)
{
}
+
static bool LoadApple2State(const char * filename)
{
return false;
}
+
#ifdef CPU_CLOCK_CHECKING
uint8_t counter = 0;
uint32_t totalCPU = 0;
#ifdef THREADED_65C02
cpuCond = SDL_CreateCond();
+ mainSem = SDL_CreateSemaphore(1);
cpuThread = SDL_CreateThread(CPUThreadFunc, NULL, NULL);
//Hmm... CPU does POST (+1), wait, then WAIT (-1)
- mainSem = SDL_CreateSemaphore(1);
// SDL_sem * mainMutex = SDL_CreateMutex();
#endif
return 0;
}
+
/*
Apple II keycodes
-----------------
#endif
}
+
static void BlinkTimer(void)
{
flash = !flash;
SetCallbackTime(BlinkTimer, 250000); // Set up blinking at 1/4 sec intervals
}
+
/*
Next problem is this: How to have events occur and synchronize with the rest
of the threads?
// Local variables
static SDL_AudioSpec desired, obtained;
+static SDL_AudioDeviceID device;
static bool soundInitialized = false;
static bool speakerState = false;
static int16_t soundBuffer[SOUND_BUFFER_SIZE];
static SDL_mutex * mutex = NULL;
static SDL_mutex * mutex2 = NULL;
static int16_t sample;
-static uint8_t ampPtr = 14; // Start with -16 - +16
+static uint8_t ampPtr = 12; // Start with -2047 - +2047
static int16_t amplitude[17] = { 0, 1, 2, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047,
4095, 8191, 16383, 32767 };
#ifdef WRITE_OUT_WAVE
static void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
+
//
// Initialize the SDL sound system
//
// To weed out problems for now...
return;
#endif
-
+ SDL_zero(desired);
desired.freq = SAMPLE_RATE; // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
-// desired.format = AUDIO_S8; // This uses the native endian (for portability)...
desired.format = AUDIO_S16SYS; // This uses the native endian (for portability)...
desired.channels = 1;
-// desired.samples = 4096; // Let's try a 4K buffer (can always go lower)
-// desired.samples = 2048; // Let's try a 2K buffer (can always go lower)
-// desired.samples = 1024; // Let's try a 1K buffer (can always go lower)
desired.samples = 512; // Let's try a 1/2K buffer (can always go lower)
desired.callback = SDLSoundCallback;
-// if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
-//When doing it this way, we need to check to see if we got what we asked for...
- if (SDL_OpenAudio(&desired, &obtained) < 0)
+ device = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0);
+
+ if (device == 0)
{
WriteLog("Sound: Failed to initialize SDL sound.\n");
return;
lastToggleCycles = 0;
sample = desired.silence; // ? wilwok ? yes
- SDL_PauseAudio(false); // Start playback!
+ SDL_PauseAudioDevice(device, 0); // Start playback!
soundInitialized = true;
WriteLog("Sound: Successfully initialized.\n");
#endif
}
+
//
// Close down the SDL sound subsystem
//
{
if (soundInitialized)
{
- SDL_PauseAudio(true);
- SDL_CloseAudio();
+// SDL_PauseAudio(true);
+ SDL_PauseAudioDevice(device, 1);
+// SDL_CloseAudio();
+ SDL_CloseAudioDevice(device);
SDL_DestroyCond(conditional);
SDL_DestroyMutex(mutex);
SDL_DestroyMutex(mutex2);
}
}
+
//
// Sound card callback handler
//
-static void SDLSoundCallback(void * userdata, Uint8 * buffer8, int length8)
+static void SDLSoundCallback(void * /*userdata*/, Uint8 * buffer8, int length8)
{
+//WriteLog("SDLSoundCallback(): begin (soundBufferPos=%i)\n", soundBufferPos);
// The sound buffer should only starve when starting which will cause it to
// lag behind the emulation at most by around 1 frame...
// (Actually, this should never happen since we fill the buffer beforehand.)
// Let's try using a mutex for shared resource consumption...
//Actually, I think Lock/UnlockAudio() does this already...
+//WriteLog("SDLSoundCallback(): SDL_mutexP(mutex2)\n");
SDL_mutexP(mutex2);
// Recast this as a 16-bit type...
int16_t * buffer = (int16_t *)buffer8;
uint32_t length = (uint32_t)length8 / 2;
+//WriteLog("SDLSoundCallback(): filling buffer...\n");
if (soundBufferPos < length) // The sound buffer is starved...
{
for(uint32_t i=0; i<soundBufferPos; i++)
// Fill buffer with last value
// memset(buffer + soundBufferPos, (uint8_t)sample, length - soundBufferPos);
for(uint32_t i=soundBufferPos; i<length; i++)
- buffer[i] = (uint16_t)sample;
+ buffer[i] = sample;
+
soundBufferPos = 0; // Reset soundBufferPos to start of buffer...
}
else
// memcpy(buffer, soundBuffer, length);
for(uint32_t i=0; i<length; i++)
buffer[i] = soundBuffer[i];
+
soundBufferPos -= length;
// Move current buffer down to start
}
// Free the mutex...
+//WriteLog("SDLSoundCallback(): SDL_mutexV(mutex2)\n");
SDL_mutexV(mutex2);
// Wake up any threads waiting for the buffer to drain...
SDL_CondSignal(conditional);
+//WriteLog("SDLSoundCallback(): end\n");
}
+
+// This is called by the main CPU thread every ~21.333 cycles.
+void WriteSampleToBuffer(void)
+{
+//WriteLog("WriteSampleToBuffer(): SDL_mutexP(mutex2)\n");
+ SDL_mutexP(mutex2);
+
+ // This should almost never happen, but...
+ while (soundBufferPos >= (SOUND_BUFFER_SIZE - 1))
+ {
+//WriteLog("WriteSampleToBuffer(): Waiting for sound thread. soundBufferPos=%i, SOUNDBUFFERSIZE-1=%i\n", soundBufferPos, SOUND_BUFFER_SIZE-1);
+ SDL_mutexV(mutex2); // Release it so sound thread can get it,
+ SDL_mutexP(mutex); // Must lock the mutex for the cond to work properly...
+ SDL_CondWait(conditional, mutex); // Sleep/wait for the sound thread
+ SDL_mutexV(mutex); // Must unlock the mutex for the cond to work properly...
+ SDL_mutexP(mutex2); // Re-lock it until we're done with it...
+ }
+
+ soundBuffer[soundBufferPos++] = sample;
+//WriteLog("WriteSampleToBuffer(): SDL_mutexV(mutex2)\n");
+ SDL_mutexV(mutex2);
+}
+
+
// Need some interface functions here to take care of flipping the
// waveform at the correct time in the sound stream...
// Step 3: Make sure there's room for it
// We need to lock since we touch both soundBuffer and soundBufferPos
SDL_mutexP(mutex2);
+
while ((soundBufferPos + currentPos) > (SOUND_BUFFER_SIZE - 1))
{
SDL_mutexV(mutex2); // Release it so sound thread can get it,
#endif
// Backfill with current toggle state
while (soundBufferPos < currentPos)
- soundBuffer[soundBufferPos++] = (uint16_t)sample;
+ soundBuffer[soundBufferPos++] = sample;
#ifdef WRITE_OUT_WAVE
fwrite(&soundBuffer[sbpSave], sizeof(int16_t), currentPos - sbpSave, fp);
lastToggleCycles = elapsedCycles;
}
+
void ToggleSpeaker(uint64_t elapsedCycles)
{
if (!soundInitialized)
return;
- HandleBuffer(elapsedCycles);
+// HandleBuffer(elapsedCycles);
speakerState = !speakerState;
sample = (speakerState ? amplitude[ampPtr] : -amplitude[ampPtr]);
}
+
void AdjustLastToggleCycles(uint64_t elapsedCycles)
{
if (!soundInitialized)
HandleBuffer(elapsedCycles);
}
+
void VolumeUp(void)
{
- // Currently set for 8-bit samples
- // Now 16
+ // Currently set for 16-bit samples
if (ampPtr < 16)
ampPtr++;
}
+
void VolumeDown(void)
{
if (ampPtr > 0)
ampPtr--;
}
+
uint8_t GetVolume(void)
{
return ampPtr;