X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdac.cpp;h=990a7248b5a7c3fb4ec7152d94d0466d89e288a5;hb=b74298f2a4540168f8e4a2bbc9fc6fdc7f4afa57;hp=0ad2b9f7114da3d59120bebe04edb06ea50cf477;hpb=191b0f477fee869ee36744763acb2d3e4d25aa28;p=virtualjaguar diff --git a/src/dac.cpp b/src/dac.cpp index 0ad2b9f..990a724 100644 --- a/src/dac.cpp +++ b/src/dac.cpp @@ -4,6 +4,13 @@ // Originally by David Raingeard // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS) // Rewritten by James L. Hammons +// (C) 2010 Underground Software +// +// JLH = James L. Hammons +// +// 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 !!! @@ -11,11 +18,23 @@ // work correctly...! Perhaps just need to set up SSI stuff so BUTCH doesn't get // confused... +// ALSO: Need to implement some form of proper locking to replace the clusterfuck +// that is the current spinlock implementation. Since the DSP is a separate +// entity, could we get away with running it in the sound IRQ? + +// ALSO: It may be a good idea to physically separate the left and right buffers +// 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. + +#include "dac.h" + #include "SDL.h" -#include "m68k.h" +//#include "gui.h" #include "jaguar.h" +#include "log.h" +#include "m68k.h" +//#include "memory.h" #include "settings.h" -#include "dac.h" //#define DEBUG_DAC @@ -32,18 +51,18 @@ // 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. -static uint16 * DACBuffer; +static uint16 DACBuffer[BUFFER_SIZE]; static uint8 SCLKFrequencyDivider = 19; // Default is roughly 22 KHz (20774 Hz in NTSC mode) /*static*/ uint16 serialMode = 0; @@ -57,7 +76,16 @@ int GetCalculatedFrequency(void); // void DACInit(void) { - memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer"); + 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"); desired.freq = GetCalculatedFrequency(); // SDL will do conversion on the fly, if it can't get the exact rate. Nice! desired.format = AUDIO_S16SYS; // This uses the native endian (for portability)... @@ -96,7 +124,7 @@ void DACDone(void) SDL_CloseAudio(); } - memory_free(DACBuffer); +// memory_free(DACBuffer); WriteLog("DAC: Done.\n"); } @@ -162,7 +190,7 @@ if (numLeftSamplesReady == 0 || numRightSamplesReady == 0) } // -// Calculate the frequency of SCLK * 32 using the divider +// Calculate the freq9uency of SCLK * 32 using the divider // int GetCalculatedFrequency(void) { @@ -187,6 +215,8 @@ void DACWriteWord(uint32 offset, uint16 data, uint32 who/*= UNKNOWN*/) { 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] @@ -208,8 +238,11 @@ WriteLog("Tail=%X, Head=%X", ltail, lhead); WriteLog("LTail=%X, LHead=%X, BUFFER_SIZE-1=%X\n", LeftFIFOTailPtr, LeftFIFOHeadPtr, BUFFER_SIZE - 1); WriteLog("RTail=%X, RHead=%X, BUFFER_SIZE-1=%X\n", RightFIFOTailPtr, RightFIFOHeadPtr, BUFFER_SIZE - 1); WriteLog("From while: Tail=%X, Head=%X", (LeftFIFOTailPtr + 2) & (BUFFER_SIZE - 1), LeftFIFOHeadPtr); - log_done(); - exit(0); +// LogDone(); +// exit(0); +#warning "Reimplement GUICrashGracefully!" +// GUICrashGracefully("Stuck in left DAC spinlock!"); + return; } }//*/ @@ -223,6 +256,8 @@ WriteLog("Tail=%X, Head=%X", ltail, lhead); } else if (offset == RTXD + 2) { + if (!SDLSoundInitialized) + return; /* Here's what's happening now: @@ -278,6 +313,8 @@ LTail=60D8, LHead=60D8, BUFFER_SIZE-1=FFFF RTail=DB, RHead=60D9, BUFFER_SIZE-1=FFFF From while: Tail=60DA, Head=60D8 */ +#warning Spinlock problem--!!! FIX !!! +#warning Odd: The right FIFO is empty, but the left FIFO is full! // Spin until buffer has been drained (for too fast processors!)... uint32 spin = 0; while (((RightFIFOTailPtr + 2) & (BUFFER_SIZE - 1)) == RightFIFOHeadPtr)//; @@ -295,8 +332,11 @@ WriteLog("Tail=%X, Head=%X", rtail, rhead); WriteLog("LTail=%X, LHead=%X, BUFFER_SIZE-1=%X\n", LeftFIFOTailPtr, LeftFIFOHeadPtr, BUFFER_SIZE - 1); WriteLog("RTail=%X, RHead=%X, BUFFER_SIZE-1=%X\n", RightFIFOTailPtr, RightFIFOHeadPtr, BUFFER_SIZE - 1); WriteLog("From while: Tail=%X, Head=%X", (RightFIFOTailPtr + 2) & (BUFFER_SIZE - 1), RightFIFOHeadPtr); - log_done(); - exit(0); +// LogDone(); +// exit(0); +#warning "Reimplement GUICrashGracefully!" +// GUICrashGracefully("Stuck in right DAC spinlock!"); + return; } }//*/ @@ -328,9 +368,14 @@ WriteLog("Tail=%X, Head=%X", rtail, rhead); { 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); - log_done(); - exit(1); +// LogDone(); +// exit(1); +#warning "Reimplement GUICrashGracefully!" +// GUICrashGracefully("Failed to initialize SDL sound!"); + return; } } @@ -379,5 +424,5 @@ uint16 DACReadWord(uint32 offset, uint32 who/*= UNKNOWN*/) else if (offset == RRXD + 2) return rrxd; - return 0xFFFF; // May need SSTAT as well... (but may be a Jaguar II only feature) + return 0xFFFF; // May need SSTAT as well... (but may be a Jaguar II only feature) }