X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdac.cpp;h=1fdadf6ae18410cae7060ff5bc76b07e5200cec2;hb=43107ad33b400f193f81c51305f12206e2228331;hp=be2f2c522a2ca62ce5545d6ad2a0da9271d5637e;hpb=2d57a03aa1aa1f1ee55bfaed762fabe6d6154992;p=virtualjaguar diff --git a/src/dac.cpp b/src/dac.cpp index be2f2c5..1fdadf6 100644 --- a/src/dac.cpp +++ b/src/dac.cpp @@ -1,35 +1,46 @@ // // DAC (really, Synchronous Serial Interface) Handler // -// by cal2 +// Originally by David Raingeard // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS) // Rewritten by James L. Hammons // -#include +#include "SDL.h" +#include "m68k.h" #include "jaguar.h" +#include "settings.h" #include "dac.h" -#define BUFFER_SIZE 0x8000 // Make the DAC buffers 32K x 16 bits +//#define DEBUG_DAC + +#define BUFFER_SIZE 0x10000 // Make the DAC buffers 64K x 16 bits // Jaguar memory locations #define LTXD 0xF1A148 #define RTXD 0xF1A14C +#define LRXD 0xF1A148 +#define RRXD 0xF1A14C #define SCLK 0xF1A150 #define SMODE 0xF1A154 +// Global variables + +uint16 lrxd, rrxd; // I2S ports (into Jaguar) + // Local variables -uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr; -SDL_AudioSpec desired; +static uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr; +static SDL_AudioSpec desired; +static bool SDLSoundInitialized = false; // We can get away with using native endian here because we can tell SDL to use the native -// when looking at the sample buffer, i.e., no need to worry about it. +// endian when looking at the sample buffer, i.e., no need to worry about it. -uint16 * DACBuffer; -uint8 SCLKFrequencyDivider = 9; // Start out roughly 44.1K (46164 Hz in NTSC mode) -uint16 serialMode = 0; +static uint16 * DACBuffer; +static uint8 SCLKFrequencyDivider = 19; // Default is roughly 22 KHz (20774 Hz in NTSC mode) +/*static*/ uint16 serialMode = 0; // Private function prototypes @@ -37,7 +48,7 @@ void SDLSoundCallback(void * userdata, Uint8 * buffer, int length); int GetCalculatedFrequency(void); // -// Initialize the SDL sound system (?) (!) +// Initialize the SDL sound system // void DACInit(void) { @@ -46,19 +57,24 @@ void DACInit(void) 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)... desired.channels = 2; - desired.samples = 4096; // Let's try a 4K buffer (can always go lower) +// 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.callback = SDLSoundCallback; if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want { - WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n"); - log_done(); - exit(1); +// WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n"); +// log_done(); +// exit(1); + WriteLog("DAC: Failed to initialize SDL sound...\n"); + } + else + { + SDLSoundInitialized = true; + DACReset(); + SDL_PauseAudio(false); // Start playback! + WriteLog("DAC: Successfully initialized.\n"); } - - DACReset(); - SDL_PauseAudio(false); // Start playback! - WriteLog("DAC: Successfully initialized.\n"); } // @@ -70,12 +86,17 @@ void DACReset(void) } // -// Close down the SDL sound subsystem (?) (!) +// Close down the SDL sound subsystem // void DACDone(void) { - SDL_PauseAudio(true); - SDL_CloseAudio(); + if (SDLSoundInitialized) + { + SDL_PauseAudio(true); + SDL_CloseAudio(); + } + + memory_free(DACBuffer); WriteLog("DAC: Done.\n"); } @@ -86,6 +107,9 @@ void DACDone(void) // void SDLSoundCallback(void * userdata, Uint8 * buffer, int length) { + // Clear the buffer to silence, in case the DAC buffer is empty (or short) +//This causes choppy sound... Ick. + memset(buffer, desired.silence, length); //WriteLog("DAC: Inside callback...\n"); if (LeftFIFOHeadPtr != LeftFIFOTailPtr) { @@ -98,24 +122,37 @@ void SDLSoundCallback(void * userdata, Uint8 * buffer, int length) - RightFIFOHeadPtr; int numSamplesReady = (numLeftSamplesReady < numRightSamplesReady - ? numLeftSamplesReady : numRightSamplesReady) * 2; + ? numLeftSamplesReady : numRightSamplesReady);//Hmm. * 2; + +//The numbers look good--it's just that the DSP can't get enough samples in the DAC buffer! +//WriteLog("DAC: Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr); +//WriteLog(" numLeft/RightSamplesReady: %i/%i, numSamplesReady: %i, length of buffer: %i\n", numLeftSamplesReady, numRightSamplesReady, numSamplesReady, length); - if (numSamplesReady > length) - numSamplesReady = length; +/* if (numSamplesReady > length) + numSamplesReady = length;//*/ + if (numSamplesReady > length / 2) // length / 2 because we're comparing 16-bit lengths + numSamplesReady = length / 2; +//else +// WriteLog(" Not enough samples to fill the buffer (short by %u L/R samples)...\n", (length / 2) - numSamplesReady); +//WriteLog("DAC: %u samples ready.\n", numSamplesReady); // Actually, it's a bit more involved than this, but this is the general idea: // memcpy(buffer, DACBuffer, length); for(int i=0; i Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr); } +//Hmm. Seems that the SDL buffer isn't being starved by the DAC buffer... +// else +// WriteLog("DAC: Silence...!\n"); } // @@ -123,8 +160,7 @@ void SDLSoundCallback(void * userdata, Uint8 * buffer, int length) // int GetCalculatedFrequency(void) { - extern bool hardwareTypeNTSC; - int systemClockFrequency = (hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL); + int systemClockFrequency = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL); // We divide by 32 here in order to find the frequency of 32 SCLKs in a row (transferring // 16 bits of left data + 16 bits of right data = 32 bits, 1 SCLK = 1 bit transferred). @@ -134,86 +170,136 @@ int GetCalculatedFrequency(void) // // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54) // -void DACWriteByte(uint32 offset, uint8 data) +void DACWriteByte(uint32 offset, uint8 data, uint32 who/*= UNKNOWN*/) { -// WriteLog("DAC: Writing %02X at %08X\n", data, offset); + WriteLog("DAC: %s writing BYTE %02X at %08X\n", whoName[who], data, offset); + if (offset == SCLK + 3) + DACWriteWord(offset - 3, (uint16)data); } -void DACWriteWord(uint32 offset, uint16 data) +void DACWriteWord(uint32 offset, uint16 data, uint32 who/*= UNKNOWN*/) { if (offset == LTXD + 2) { - if (LeftFIFOTailPtr + 2 != LeftFIFOHeadPtr) - { - SDL_LockAudio(); // Is it necessary to do this? Mebbe. - // We use a circular buffer 'cause it's easy. Note that the callback function - // takes care of dumping audio to the soundcard...! - LeftFIFOTailPtr = (LeftFIFOTailPtr + 2) % BUFFER_SIZE; - DACBuffer[LeftFIFOTailPtr] = data; -// Aaron's code does this, but I don't know why... -//Flipping this bit makes the audio MUCH louder. Need to look at the amplitude of the -//waveform to see if any massaging is needed here... -// DACBuffer[LeftFIFOTailPtr] = data ^ 0x8000; - SDL_UnlockAudio(); - } - else - WriteLog("DAC: Ran into FIFO's left tail pointer!\n"); + // 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] + // Also, we're taking advantage of the fact that the buffer is a multiple of two + // in this check... + while (((LeftFIFOTailPtr + 2) & (BUFFER_SIZE - 1)) == LeftFIFOHeadPtr); + + SDL_LockAudio(); // Is it necessary to do this? Mebbe. + // We use a circular buffer 'cause it's easy. Note that the callback function + // takes care of dumping audio to the soundcard...! Also note that we're writing + // the samples in the buffer in an interleaved L/R format. + LeftFIFOTailPtr = (LeftFIFOTailPtr + 2) % BUFFER_SIZE; + DACBuffer[LeftFIFOTailPtr] = data; + SDL_UnlockAudio(); } else if (offset == RTXD + 2) { - if (RightFIFOTailPtr + 2 != RightFIFOHeadPtr) - { - SDL_LockAudio(); - RightFIFOTailPtr = (RightFIFOTailPtr + 2) % BUFFER_SIZE; - DACBuffer[RightFIFOTailPtr] = data; -// Aaron's code does this, but I don't know why... -// DACBuffer[RightFIFOTailPtr] = data ^ 0x8000; - SDL_UnlockAudio(); - } + // Spin until buffer has been drained (for too fast processors!)... +//uint32 spin = 0; + while (((RightFIFOTailPtr + 2) & (BUFFER_SIZE - 1)) == RightFIFOHeadPtr); +/* { +spin++; +//if ((spin & 0x0FFFFFFF) == 0) +// WriteLog("Tail=%X, Head=%X, BUFFER_SIZE-1=%X\n", RightFIFOTailPtr, RightFIFOHeadPtr, BUFFER_SIZE - 1); + +if (spin == 0xFFFF0000) +{ +uint32 rtail = RightFIFOTailPtr, rhead = RightFIFOHeadPtr; +WriteLog("Tail=%X, Head=%X\n", rtail, rhead); + + WriteLog("\nStuck in right DAC spinlock!\nAborting!\n\n"); + WriteLog("Tail=%X, Head=%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); +} + }//*/ + +//This is wrong if (RightFIFOTailPtr + 2 != RightFIFOHeadPtr) +// { + SDL_LockAudio(); + RightFIFOTailPtr = (RightFIFOTailPtr + 2) % BUFFER_SIZE; + DACBuffer[RightFIFOTailPtr] = data; + SDL_UnlockAudio(); +// } +/*#ifdef DEBUG_DAC else WriteLog("DAC: Ran into FIFO's right tail pointer!\n"); +#endif*/ } else if (offset == SCLK + 2) // Sample rate { + WriteLog("DAC: Writing %u to SCLK...\n", data); if ((uint8)data != SCLKFrequencyDivider) { -WriteLog("DAC: Changing sample rate!\n"); - SDL_CloseAudio(); SCLKFrequencyDivider = (uint8)data; - desired.freq = GetCalculatedFrequency();// SDL will do conversion on the fly, if it can't get the exact rate. Nice! - - if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want +//Of course a better way would be to query the hardware to find the upper limit... + if (data > 7) // Anything less than 8 is too high! { - WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n"); - log_done(); - exit(1); - } + if (SDLSoundInitialized) + SDL_CloseAudio(); + + desired.freq = GetCalculatedFrequency();// 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 + { + WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq); + log_done(); + exit(1); + } + } - DACReset(); - SDL_PauseAudio(false); // Start playback! + DACReset(); + + if (SDLSoundInitialized) + SDL_PauseAudio(false); // Start playback! + } } } else if (offset == SMODE + 2) { serialMode = data; - WriteLog("DAC: Writing to SMODE. Bits: %s%s%s%s%s%s\n", + WriteLog("DAC: %s writing to SMODE. Bits: %s%s%s%s%s%s [68K PC=%08X]\n", whoName[who], (data & 0x01 ? "INTERNAL " : ""), (data & 0x02 ? "MODE " : ""), (data & 0x04 ? "WSEN " : ""), (data & 0x08 ? "RISING " : ""), - (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : "")); + (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : ""), + m68k_get_reg(NULL, M68K_REG_PC)); } } // // LRXD/RRXD/SSTAT ($F1A148/4C/50) // -uint8 DACReadByte(uint32 offset) +uint8 DACReadByte(uint32 offset, uint32 who/*= UNKNOWN*/) { -// WriteLog("DAC: Reading byte from %08X\n", offset); +// WriteLog("DAC: %s reading byte from %08X\n", whoName[who], offset); return 0xFF; } -uint16 DACReadWord(uint32 offset) +//static uint16 fakeWord = 0; +uint16 DACReadWord(uint32 offset, uint32 who/*= UNKNOWN*/) { -// WriteLog("DAC: Reading word from %08X\n", offset); - return 0xFFFF; +// WriteLog("DAC: %s reading word from %08X\n", whoName[who], offset); +// return 0xFFFF; +// WriteLog("DAC: %s reading WORD %04X from %08X\n", whoName[who], fakeWord, offset); +// return fakeWord++; +//NOTE: This only works if a bunch of things are set in BUTCH which we currently don't +// check for. !!! FIX !!! +// Partially fixed: We check for I2SCNTRL in the JERRY I2S routine... +// return GetWordFromButchSSI(offset, who); + if (offset == LRXD || offset == RRXD) + return 0x0000; + else if (offset == LRXD + 2) + return lrxd; + else if (offset == RRXD + 2) + return rrxd; + + return 0xFFFF; // May need SSTAT as well... (but may be a Jaguar II only feature) }