2 // DAC (really, Synchronous Serial Interface) Handler
4 // Originally by David Raingeard
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Rewritten by James L. Hammons
17 #define BUFFER_SIZE 0x10000 // Make the DAC buffers 64K x 16 bits
19 // Jaguar memory locations
26 #define SMODE 0xF1A154
30 uint16 lrxd, rrxd; // I2S ports (into Jaguar)
34 static uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr;
35 static SDL_AudioSpec desired;
36 static bool SDLSoundInitialized = false;
38 // We can get away with using native endian here because we can tell SDL to use the native
39 // endian when looking at the sample buffer, i.e., no need to worry about it.
41 static uint16 * DACBuffer;
42 static uint8 SCLKFrequencyDivider = 19; // Default is roughly 22 KHz (20774 Hz in NTSC mode)
43 /*static*/ uint16 serialMode = 0;
45 // Private function prototypes
47 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
48 int GetCalculatedFrequency(void);
51 // Initialize the SDL sound system
55 memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
57 desired.freq = GetCalculatedFrequency(); // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
58 desired.format = AUDIO_S16SYS; // This uses the native endian (for portability)...
60 // desired.samples = 4096; // Let's try a 4K buffer (can always go lower)
61 desired.samples = 2048; // Let's try a 2K buffer (can always go lower)
62 desired.callback = SDLSoundCallback;
64 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
66 // WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n");
69 WriteLog("DAC: Failed to initialize SDL sound...\n");
73 SDLSoundInitialized = true;
75 SDL_PauseAudio(false); // Start playback!
76 WriteLog("DAC: Successfully initialized.\n");
81 // Reset the sound buffer FIFOs
85 LeftFIFOHeadPtr = LeftFIFOTailPtr = 0, RightFIFOHeadPtr = RightFIFOTailPtr = 1;
89 // Close down the SDL sound subsystem
93 if (SDLSoundInitialized)
99 memory_free(DACBuffer);
100 WriteLog("DAC: Done.\n");
104 // SDL callback routine to fill audio buffer
106 // Note: The samples are packed in the buffer in 16 bit left/16 bit right pairs.
108 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
110 // Clear the buffer to silence, in case the DAC buffer is empty (or short)
111 memset(buffer, desired.silence, length);
112 //WriteLog("DAC: Inside callback...\n");
113 if (LeftFIFOHeadPtr != LeftFIFOTailPtr)
115 //WriteLog("DAC: About to write some data!\n");
116 int numLeftSamplesReady
117 = (LeftFIFOTailPtr + (LeftFIFOTailPtr < LeftFIFOHeadPtr ? BUFFER_SIZE : 0))
119 int numRightSamplesReady
120 = (RightFIFOTailPtr + (RightFIFOTailPtr < RightFIFOHeadPtr ? BUFFER_SIZE : 0))
123 = (numLeftSamplesReady < numRightSamplesReady
124 ? numLeftSamplesReady : numRightSamplesReady);//Hmm. * 2;
126 //The numbers look good--it's just that the DSP can't get enough samples in the DAC buffer!
127 //WriteLog("DAC: Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
128 //WriteLog(" numLeft/RightSamplesReady: %i/%i, numSamplesReady: %i, length of buffer: %i\n", numLeftSamplesReady, numRightSamplesReady, numSamplesReady, length);
130 /* if (numSamplesReady > length)
131 numSamplesReady = length;//*/
132 if (numSamplesReady > length / 2) // length / 2 because we're comparing 16-bit lengths
133 numSamplesReady = length / 2;
135 // WriteLog(" Not enough samples to fill the buffer (short by %u L/R samples)...\n", (length / 2) - numSamplesReady);
136 //WriteLog("DAC: %u samples ready.\n", numSamplesReady);
138 // Actually, it's a bit more involved than this, but this is the general idea:
139 // memcpy(buffer, DACBuffer, length);
140 for(int i=0; i<numSamplesReady; i++)
141 ((uint16 *)buffer)[i] = DACBuffer[(LeftFIFOHeadPtr + i) % BUFFER_SIZE];
142 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
143 // buffer[i] = DACBuffer[(LeftFIFOHeadPtr + i) & (BUFFER_SIZE - 1)];
145 LeftFIFOHeadPtr = (LeftFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
146 RightFIFOHeadPtr = (RightFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
147 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
148 // LeftFIFOHeadPtr = (LeftFIFOHeadPtr + numSamplesReady) & (BUFFER_SIZE - 1);
149 // RightFIFOHeadPtr = (RightFIFOHeadPtr + numSamplesReady) & (BUFFER_SIZE - 1);
150 //WriteLog(" -> Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
152 //Hmm. Seems that the SDL buffer isn't being starved by the DAC buffer...
154 // WriteLog("DAC: Silence...!\n");
158 // Calculate the frequency of SCLK * 32 using the divider
160 int GetCalculatedFrequency(void)
162 int systemClockFrequency = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
164 // We divide by 32 here in order to find the frequency of 32 SCLKs in a row (transferring
165 // 16 bits of left data + 16 bits of right data = 32 bits, 1 SCLK = 1 bit transferred).
166 return systemClockFrequency / (32 * (2 * (SCLKFrequencyDivider + 1)));
170 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
172 void DACWriteByte(uint32 offset, uint8 data, uint32 who/*= UNKNOWN*/)
174 WriteLog("DAC: %s writing BYTE %02X at %08X\n", whoName[who], data, offset);
175 if (offset == SCLK + 3)
176 DACWriteWord(offset - 3, (uint16)data);
179 void DACWriteWord(uint32 offset, uint16 data, uint32 who/*= UNKNOWN*/)
181 if (offset == LTXD + 2)
183 // Spin until buffer has been drained (for too fast processors!)...
184 //Small problem--if Head == 0 and Tail == buffer end, then this will fail... !!! FIX !!!
186 // Also, we're taking advantage of the fact that the buffer is a multiple of two
188 while ((LeftFIFOTailPtr + 2) & (BUFFER_SIZE - 1) == LeftFIFOHeadPtr);
190 SDL_LockAudio(); // Is it necessary to do this? Mebbe.
191 // We use a circular buffer 'cause it's easy. Note that the callback function
192 // takes care of dumping audio to the soundcard...! Also note that we're writing
193 // the samples in the buffer in an interleaved L/R format.
194 LeftFIFOTailPtr = (LeftFIFOTailPtr + 2) % BUFFER_SIZE;
195 DACBuffer[LeftFIFOTailPtr] = data;
198 else if (offset == RTXD + 2)
200 // Spin until buffer has been drained (for too fast processors!)...
202 while ((RightFIFOTailPtr + 2) & (BUFFER_SIZE - 1) == RightFIFOHeadPtr);
205 if (spin == 0x10000000)
207 WriteLog("\nStuck in right DAC spinlock! Tail=%u, Head=%u\nAborting!\n", RightFIFOTailPtr, RightFIFOHeadPtr);
213 //This is wrong if (RightFIFOTailPtr + 2 != RightFIFOHeadPtr)
216 RightFIFOTailPtr = (RightFIFOTailPtr + 2) % BUFFER_SIZE;
217 DACBuffer[RightFIFOTailPtr] = data;
222 WriteLog("DAC: Ran into FIFO's right tail pointer!\n");
225 else if (offset == SCLK + 2) // Sample rate
227 WriteLog("DAC: Writing %u to SCLK...\n", data);
228 if ((uint8)data != SCLKFrequencyDivider)
230 SCLKFrequencyDivider = (uint8)data;
231 //Of course a better way would be to query the hardware to find the upper limit...
232 if (data > 7) // Anything less than 8 is too high!
234 if (SDLSoundInitialized)
237 desired.freq = GetCalculatedFrequency();// SDL will do conversion on the fly, if it can't get the exact rate. Nice!
238 WriteLog("DAC: Changing sample rate to %u Hz!\n", desired.freq);
240 if (SDLSoundInitialized)
242 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
244 WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq);
252 if (SDLSoundInitialized)
253 SDL_PauseAudio(false); // Start playback!
257 else if (offset == SMODE + 2)
260 WriteLog("DAC: %s writing to SMODE. Bits: %s%s%s%s%s%s [68K PC=%08X]\n", whoName[who],
261 (data & 0x01 ? "INTERNAL " : ""), (data & 0x02 ? "MODE " : ""),
262 (data & 0x04 ? "WSEN " : ""), (data & 0x08 ? "RISING " : ""),
263 (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : ""),
264 m68k_get_reg(NULL, M68K_REG_PC));
269 // LRXD/RRXD/SSTAT ($F1A148/4C/50)
271 uint8 DACReadByte(uint32 offset, uint32 who/*= UNKNOWN*/)
273 // WriteLog("DAC: %s reading byte from %08X\n", whoName[who], offset);
277 //static uint16 fakeWord = 0;
278 uint16 DACReadWord(uint32 offset, uint32 who/*= UNKNOWN*/)
280 // WriteLog("DAC: %s reading word from %08X\n", whoName[who], offset);
282 // WriteLog("DAC: %s reading WORD %04X from %08X\n", whoName[who], fakeWord, offset);
283 // return fakeWord++;
284 //NOTE: This only works if a bunch of things are set in BUTCH which we currently don't
285 // check for. !!! FIX !!!
286 // Partially fixed: We check for I2SCNTRL in the JERRY I2S routine...
287 // return GetWordFromButchSSI(offset, who);
288 if (offset == LRXD || offset == RRXD)
290 else if (offset == LRXD + 2)
292 else if (offset == RRXD + 2)
295 return 0xFFFF; // May need SSTAT as well... (but may be a Jaguar II only feature)