//
// Originally by David Raingeard
// GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
-// Rewritten by James L. Hammons
+// Rewritten by James Hammons
+// (C) 2010 Underground Software
+//
+// JLH = James Hammons <jlhamm@acm.org>
+//
+// Who When What
+// --- ---------- -------------------------------------------------------------
+// JLH 01/16/2010 Created this log ;-)
//
// Need to set up defaults that the BIOS sets for the SSI here in DACInit()... !!! FIX !!!
// to prevent things like the DSP filling only one side and such. Do such
// mono modes exist on the Jag? Seems to according to Super Burnout.
+// After testing on a real Jaguar, it seems clear that the I2S interrupt drives
+// the audio subsystem. So while you can drive the audio at a *slower* rate than
+// set by SCLK, you can't drive it any *faster*. Also note, that if the I2S
+// interrupt is not enabled/running on the DSP, then there is no audio. Also,
+// audio can be muted by clearing bit 8 of JOYSTICK (JOY1).
+//
+// Approach: We can run the DSP in the host system's audio IRQ, by running the
+// DSP for the alloted time (depending on the host buffer size & sample rate)
+// by simply reading the L/R_I2S (L/RTXD) registers at regular intervals. We
+// would also have to time the I2S/TIMER0/TIMER1 interrupts in the DSP as well.
+// This way, we can run the host audio IRQ at, say, 48 KHz and not have to care
+// so much about SCLK and running a separate buffer and all the attendant
+// garbage that comes with that awful approach.
+//
+// There would still be potential gotchas, as the SCLK can theoretically drive
+// the I2S at 26590906 / 2 (for SCLK == 0) = 13.3 MHz which corresponds to an
+// audio rate 416 KHz (dividing the I2S rate by 32, for 16-bit stereo). It
+// seems doubtful that anything useful could come of such a high rate, and we
+// can probably safely ignore any such ridiculously high audio rates. It won't
+// sound the same as on a real Jaguar, but who cares? :-)
+
#include "dac.h"
#include "SDL.h"
-#include "gui.h"
+//#include "gui.h"
#include "jaguar.h"
#include "log.h"
#include "m68k.h"
+//#include "memory.h"
#include "settings.h"
//#define DEBUG_DAC
// Global variables
-uint16 lrxd, rrxd; // I2S ports (into Jaguar)
+//uint16 lrxd, rrxd; // I2S ports (into Jaguar)
// Local variables
static uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr;
static SDL_AudioSpec desired;
-static bool SDLSoundInitialized = false;
+static bool SDLSoundInitialized;
// We can get away with using native endian here because we can tell SDL to use the native
// endian when looking at the sample buffer, i.e., no need to worry about it.
// Private function prototypes
void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
-int GetCalculatedFrequency(void);
//
// Initialize the SDL sound system
//
void DACInit(void)
{
+ SDLSoundInitialized = false;
+
+ if (!vjs.audioEnabled)
+ {
+ WriteLog("DAC: Host audio playback disabled.\n");
+ return;
+ }
+
// memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
// DACBuffer = (uint16 *)memory_malloc(BUFFER_SIZE * sizeof(uint16), "DAC buffer");
WriteLog("DAC: Done.\n");
}
+
+// Approach: Run the DSP for however many cycles needed to correspond to whatever sample rate
+// we've set the audio to run at. So, e.g., if we run it at 48 KHz, then we would run the DSP
+// for however much time it takes to fill the buffer. So with a 2K buffer, this would correspond
+// to running the DSP for 0.042666... seconds. At 26590906 Hz, this would correspond to
+// running the DSP for 1134545 cycles. You would then sample the L/RTXD registers every
+// 1134545 / 2048 = 554 cycles to fill the buffer. You would also have to manage interrupt
+// timing as well (generating them at the proper times), but that shouldn't be too difficult...
+// If the DSP isn't running, then fill the buffer with L/RTXD and exit.
+
//
// SDL callback routine to fill audio buffer
//
return systemClockFrequency / (32 * (2 * (SCLKFrequencyDivider + 1)));
}
+static int oldFreq = 0;
+
+void DACSetNewFrequency(int freq)
+{
+ if (freq == oldFreq)
+ return;
+
+ oldFreq = freq;
+
+ // Should do some sanity checking on the frequency...
+
+ if (SDLSoundInitialized)
+ SDL_CloseAudio();
+
+ desired.freq = freq;// SDL will do conversion on the fly, if it can't get the exact rate. Nice!
+ WriteLog("DAC: Changing sample rate to %u Hz!\n", desired.freq);
+
+ if (SDLSoundInitialized)
+ {
+ if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
+ {
+// This is bad, Bad, BAD !!! DON'T ABORT BECAUSE WE DIDN'T GET OUR FREQ! !!! FIX !!!
+#warning !!! FIX !!! Aborting because of SDL audio problem is bad!
+ WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq);
+// LogDone();
+// exit(1);
+#warning "Reimplement GUICrashGracefully!"
+// GUICrashGracefully("Failed to initialize SDL sound!");
+ return;
+ }
+ }
+
+ DACReset();
+
+ if (SDLSoundInitialized)
+ SDL_PauseAudio(false); // Start playback!
+}
+
//
// LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
//
{
if (offset == LTXD + 2)
{
+ if (!SDLSoundInitialized)
+ return;
// Spin until buffer has been drained (for too fast processors!)...
//Small problem--if Head == 0 and Tail == buffer end, then this will fail... !!! FIX !!!
//[DONE]
WriteLog("From while: Tail=%X, Head=%X", (LeftFIFOTailPtr + 2) & (BUFFER_SIZE - 1), LeftFIFOHeadPtr);
// LogDone();
// exit(0);
- GUICrashGracefully("Stuck in left DAC spinlock!");
+#warning "Reimplement GUICrashGracefully!"
+// GUICrashGracefully("Stuck in left DAC spinlock!");
return;
}
}//*/
}
else if (offset == RTXD + 2)
{
+ if (!SDLSoundInitialized)
+ return;
/*
Here's what's happening now:
WriteLog("From while: Tail=%X, Head=%X", (RightFIFOTailPtr + 2) & (BUFFER_SIZE - 1), RightFIFOHeadPtr);
// LogDone();
// exit(0);
- GUICrashGracefully("Stuck in right DAC spinlock!");
+#warning "Reimplement GUICrashGracefully!"
+// GUICrashGracefully("Stuck in right DAC spinlock!");
return;
}
}//*/
WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq);
// LogDone();
// exit(1);
- GUICrashGracefully("Failed to initialize SDL sound!");
+#warning "Reimplement GUICrashGracefully!"
+// GUICrashGracefully("Failed to initialize SDL sound!");
return;
}
}