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
7 // (C) 2010 Underground Software
9 // JLH = James L. Hammons <jlhamm@acm.org>
12 // --- ---------- -------------------------------------------------------------
13 // JLH 01/16/2010 Created this log ;-)
16 // Need to set up defaults that the BIOS sets for the SSI here in DACInit()... !!! FIX !!!
17 // or something like that... Seems like it already does, but it doesn't seem to
18 // work correctly...! Perhaps just need to set up SSI stuff so BUTCH doesn't get
21 // ALSO: Need to implement some form of proper locking to replace the clusterfuck
22 // that is the current spinlock implementation. Since the DSP is a separate
23 // entity, could we get away with running it in the sound IRQ?
25 // ALSO: It may be a good idea to physically separate the left and right buffers
26 // to prevent things like the DSP filling only one side and such. Do such
27 // mono modes exist on the Jag? Seems to according to Super Burnout.
41 #define BUFFER_SIZE 0x10000 // Make the DAC buffers 64K x 16 bits
43 // Jaguar memory locations
50 #define SMODE 0xF1A154
54 //uint16 lrxd, rrxd; // I2S ports (into Jaguar)
58 static uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr;
59 static SDL_AudioSpec desired;
60 static bool SDLSoundInitialized;
62 // We can get away with using native endian here because we can tell SDL to use the native
63 // endian when looking at the sample buffer, i.e., no need to worry about it.
65 static uint16 DACBuffer[BUFFER_SIZE];
66 static uint8 SCLKFrequencyDivider = 19; // Default is roughly 22 KHz (20774 Hz in NTSC mode)
67 /*static*/ uint16 serialMode = 0;
69 // Private function prototypes
71 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
72 int GetCalculatedFrequency(void);
75 // Initialize the SDL sound system
79 SDLSoundInitialized = false;
81 if (!vjs.audioEnabled)
83 WriteLog("DAC: Host audio playback disabled.\n");
87 // memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
88 // DACBuffer = (uint16 *)memory_malloc(BUFFER_SIZE * sizeof(uint16), "DAC buffer");
90 desired.freq = GetCalculatedFrequency(); // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
91 desired.format = AUDIO_S16SYS; // This uses the native endian (for portability)...
93 // desired.samples = 4096; // Let's try a 4K buffer (can always go lower)
94 desired.samples = 2048; // Let's try a 2K buffer (can always go lower)
95 desired.callback = SDLSoundCallback;
97 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
98 WriteLog("DAC: Failed to initialize SDL sound...\n");
101 SDLSoundInitialized = true;
103 SDL_PauseAudio(false); // Start playback!
104 WriteLog("DAC: Successfully initialized.\n");
109 // Reset the sound buffer FIFOs
113 LeftFIFOHeadPtr = LeftFIFOTailPtr = 0, RightFIFOHeadPtr = RightFIFOTailPtr = 1;
117 // Close down the SDL sound subsystem
121 if (SDLSoundInitialized)
123 SDL_PauseAudio(true);
127 // memory_free(DACBuffer);
128 WriteLog("DAC: Done.\n");
132 // SDL callback routine to fill audio buffer
134 // Note: The samples are packed in the buffer in 16 bit left/16 bit right pairs.
136 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
138 // Clear the buffer to silence, in case the DAC buffer is empty (or short)
139 //This causes choppy sound... Ick.
140 memset(buffer, desired.silence, length);
141 //WriteLog("DAC: Inside callback...\n");
142 if (LeftFIFOHeadPtr != LeftFIFOTailPtr)
144 //WriteLog("DAC: About to write some data!\n");
145 int numLeftSamplesReady
146 = (LeftFIFOTailPtr + (LeftFIFOTailPtr < LeftFIFOHeadPtr ? BUFFER_SIZE : 0))
148 int numRightSamplesReady
149 = (RightFIFOTailPtr + (RightFIFOTailPtr < RightFIFOHeadPtr ? BUFFER_SIZE : 0))
151 //This waits for the slower side to catch up. If writing only one side, then this
152 //causes the buffer not to drain...
154 = (numLeftSamplesReady < numRightSamplesReady
155 ? numLeftSamplesReady : numRightSamplesReady);//Hmm. * 2;
157 //Kludge, until I can figure out WTF is going on WRT Super Burnout.
158 if (numLeftSamplesReady == 0 || numRightSamplesReady == 0)
159 numSamplesReady = numLeftSamplesReady + numRightSamplesReady;
161 //The numbers look good--it's just that the DSP can't get enough samples in the DAC buffer!
162 //WriteLog("DAC: Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
163 //WriteLog(" numLeft/RightSamplesReady: %i/%i, numSamplesReady: %i, length of buffer: %i\n", numLeftSamplesReady, numRightSamplesReady, numSamplesReady, length);
165 /* if (numSamplesReady > length)
166 numSamplesReady = length;//*/
167 if (numSamplesReady > length / 2) // length / 2 because we're comparing 16-bit lengths
168 numSamplesReady = length / 2;
170 // WriteLog(" Not enough samples to fill the buffer (short by %u L/R samples)...\n", (length / 2) - numSamplesReady);
171 //WriteLog("DAC: %u samples ready.\n", numSamplesReady);
173 // Actually, it's a bit more involved than this, but this is the general idea:
174 // memcpy(buffer, DACBuffer, length);
175 for(int i=0; i<numSamplesReady; i++)
176 ((uint16 *)buffer)[i] = DACBuffer[(LeftFIFOHeadPtr + i) % BUFFER_SIZE];
177 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
178 // buffer[i] = DACBuffer[(LeftFIFOHeadPtr + i) & (BUFFER_SIZE - 1)];
180 LeftFIFOHeadPtr = (LeftFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
181 RightFIFOHeadPtr = (RightFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
182 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
183 // LeftFIFOHeadPtr = (LeftFIFOHeadPtr + numSamplesReady) & (BUFFER_SIZE - 1);
184 // RightFIFOHeadPtr = (RightFIFOHeadPtr + numSamplesReady) & (BUFFER_SIZE - 1);
185 //WriteLog(" -> Left/RightFIFOHeadPtr: %04X/%04X, Left/RightFIFOTailPtr: %04X/%04X\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
187 //Hmm. Seems that the SDL buffer isn't being starved by the DAC buffer...
189 // WriteLog("DAC: Silence...!\n");
193 // Calculate the freq9uency of SCLK * 32 using the divider
195 int GetCalculatedFrequency(void)
197 int systemClockFrequency = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
199 // We divide by 32 here in order to find the frequency of 32 SCLKs in a row (transferring
200 // 16 bits of left data + 16 bits of right data = 32 bits, 1 SCLK = 1 bit transferred).
201 return systemClockFrequency / (32 * (2 * (SCLKFrequencyDivider + 1)));
205 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
207 void DACWriteByte(uint32 offset, uint8 data, uint32 who/*= UNKNOWN*/)
209 WriteLog("DAC: %s writing BYTE %02X at %08X\n", whoName[who], data, offset);
210 if (offset == SCLK + 3)
211 DACWriteWord(offset - 3, (uint16)data);
214 void DACWriteWord(uint32 offset, uint16 data, uint32 who/*= UNKNOWN*/)
216 if (offset == LTXD + 2)
218 if (!SDLSoundInitialized)
220 // Spin until buffer has been drained (for too fast processors!)...
221 //Small problem--if Head == 0 and Tail == buffer end, then this will fail... !!! FIX !!!
223 // Also, we're taking advantage of the fact that the buffer is a multiple of two
226 while (((LeftFIFOTailPtr + 2) & (BUFFER_SIZE - 1)) == LeftFIFOHeadPtr)//;
229 //if ((spin & 0x0FFFFFFF) == 0)
230 // WriteLog("Tail=%X, Head=%X, BUFFER_SIZE-1=%X\n", RightFIFOTailPtr, RightFIFOHeadPtr, BUFFER_SIZE - 1);
232 if (spin == 0xFFFF0000)
234 uint32 ltail = LeftFIFOTailPtr, lhead = LeftFIFOHeadPtr;
235 WriteLog("Tail=%X, Head=%X", ltail, lhead);
237 WriteLog("\nStuck in left DAC spinlock! Aborting!\n");
238 WriteLog("LTail=%X, LHead=%X, BUFFER_SIZE-1=%X\n", LeftFIFOTailPtr, LeftFIFOHeadPtr, BUFFER_SIZE - 1);
239 WriteLog("RTail=%X, RHead=%X, BUFFER_SIZE-1=%X\n", RightFIFOTailPtr, RightFIFOHeadPtr, BUFFER_SIZE - 1);
240 WriteLog("From while: Tail=%X, Head=%X", (LeftFIFOTailPtr + 2) & (BUFFER_SIZE - 1), LeftFIFOHeadPtr);
243 #warning "Reimplement GUICrashGracefully!"
244 // GUICrashGracefully("Stuck in left DAC spinlock!");
249 SDL_LockAudio(); // Is it necessary to do this? Mebbe.
250 // We use a circular buffer 'cause it's easy. Note that the callback function
251 // takes care of dumping audio to the soundcard...! Also note that we're writing
252 // the samples in the buffer in an interleaved L/R format.
253 LeftFIFOTailPtr = (LeftFIFOTailPtr + 2) % BUFFER_SIZE;
254 DACBuffer[LeftFIFOTailPtr] = data;
257 else if (offset == RTXD + 2)
259 if (!SDLSoundInitialized)
262 Here's what's happening now:
264 Stuck in right DAC spinlock!
267 Tail=681, Head=681, BUFFER_SIZE-1=FFFF
268 From while: Tail=683, Head=681
270 ????? What the FUCK ?????
272 & when I uncomment the lines below spin++; it *doesn't* lock here... WTF?????
274 I think it was missing parentheses causing the fuckup... Seems to work now...
276 Except for Super Burnout now...! Aarrrgggghhhhh!
279 Stuck in left DAC spinlock! Aborting!
280 Tail=AC, Head=AE, BUFFER_SIZE-1=FFFF
281 From while: Tail=AE, Head=AE
283 So it's *really* stuck here in the left FIFO. Figure out why!!!
285 Prolly 'cause it doesn't set the sample rate right away--betcha it works with the BIOS...
286 It gets farther, but then locks here (weird!):
289 Stuck in left DAC spinlock! Aborting!
290 Tail=2564, Head=2566, BUFFER_SIZE-1=FFFF
291 From while: Tail=2566, Head=2566
293 Weird--recompile with more WriteLog() entries and it *doesn't* lock...
294 Yeah, because there was no DSP running. Duh!
297 Stuck in left DAC spinlock! Aborting!
298 LTail=AC, LHead=AE, BUFFER_SIZE-1=FFFF
299 RTail=AF, RHead=AF, BUFFER_SIZE-1=FFFF
300 From while: Tail=AE, Head=AE
302 Odd: The right FIFO is empty, but the left FIFO is full!
303 And this is what is causing the lockup--the DAC callback waits for the side with
304 less samples ready and in this case it's the right channel (that never fills up)
305 that it's waiting for...!
307 Okay, with the kludge in place for the right channel not being filled, we select
308 a track and then it locks here:
311 Stuck in left DAC spinlock! Aborting!
312 LTail=60D8, LHead=60D8, BUFFER_SIZE-1=FFFF
313 RTail=DB, RHead=60D9, BUFFER_SIZE-1=FFFF
314 From while: Tail=60DA, Head=60D8
316 #warning Spinlock problem--!!! FIX !!!
317 #warning Odd: The right FIFO is empty, but the left FIFO is full!
318 // Spin until buffer has been drained (for too fast processors!)...
320 while (((RightFIFOTailPtr + 2) & (BUFFER_SIZE - 1)) == RightFIFOHeadPtr)//;
323 //if ((spin & 0x0FFFFFFF) == 0)
324 // WriteLog("Tail=%X, Head=%X, BUFFER_SIZE-1=%X\n", RightFIFOTailPtr, RightFIFOHeadPtr, BUFFER_SIZE - 1);
326 if (spin == 0xFFFF0000)
328 uint32 rtail = RightFIFOTailPtr, rhead = RightFIFOHeadPtr;
329 WriteLog("Tail=%X, Head=%X", rtail, rhead);
331 WriteLog("\nStuck in right DAC spinlock! Aborting!\n");
332 WriteLog("LTail=%X, LHead=%X, BUFFER_SIZE-1=%X\n", LeftFIFOTailPtr, LeftFIFOHeadPtr, BUFFER_SIZE - 1);
333 WriteLog("RTail=%X, RHead=%X, BUFFER_SIZE-1=%X\n", RightFIFOTailPtr, RightFIFOHeadPtr, BUFFER_SIZE - 1);
334 WriteLog("From while: Tail=%X, Head=%X", (RightFIFOTailPtr + 2) & (BUFFER_SIZE - 1), RightFIFOHeadPtr);
337 #warning "Reimplement GUICrashGracefully!"
338 // GUICrashGracefully("Stuck in right DAC spinlock!");
344 RightFIFOTailPtr = (RightFIFOTailPtr + 2) % BUFFER_SIZE;
345 DACBuffer[RightFIFOTailPtr] = data;
349 WriteLog("DAC: Ran into FIFO's right tail pointer!\n");
352 else if (offset == SCLK + 2) // Sample rate
354 WriteLog("DAC: Writing %u to SCLK...\n", data);
355 if ((uint8)data != SCLKFrequencyDivider)
357 SCLKFrequencyDivider = (uint8)data;
358 //Of course a better way would be to query the hardware to find the upper limit...
359 if (data > 7) // Anything less than 8 is too high!
361 if (SDLSoundInitialized)
364 desired.freq = GetCalculatedFrequency();// SDL will do conversion on the fly, if it can't get the exact rate. Nice!
365 WriteLog("DAC: Changing sample rate to %u Hz!\n", desired.freq);
367 if (SDLSoundInitialized)
369 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
371 // This is bad, Bad, BAD !!! DON'T ABORT BECAUSE WE DIDN'T GET OUR FREQ! !!! FIX !!!
372 #warning !!! FIX !!! Aborting because of SDL audio problem is bad!
373 WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq);
376 #warning "Reimplement GUICrashGracefully!"
377 // GUICrashGracefully("Failed to initialize SDL sound!");
384 if (SDLSoundInitialized)
385 SDL_PauseAudio(false); // Start playback!
389 else if (offset == SMODE + 2)
392 WriteLog("DAC: %s writing to SMODE. Bits: %s%s%s%s%s%s [68K PC=%08X]\n", whoName[who],
393 (data & 0x01 ? "INTERNAL " : ""), (data & 0x02 ? "MODE " : ""),
394 (data & 0x04 ? "WSEN " : ""), (data & 0x08 ? "RISING " : ""),
395 (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : ""),
396 m68k_get_reg(NULL, M68K_REG_PC));
401 // LRXD/RRXD/SSTAT ($F1A148/4C/50)
403 uint8 DACReadByte(uint32 offset, uint32 who/*= UNKNOWN*/)
405 // WriteLog("DAC: %s reading byte from %08X\n", whoName[who], offset);
409 //static uint16 fakeWord = 0;
410 uint16 DACReadWord(uint32 offset, uint32 who/*= UNKNOWN*/)
412 // WriteLog("DAC: %s reading word from %08X\n", whoName[who], offset);
414 // WriteLog("DAC: %s reading WORD %04X from %08X\n", whoName[who], fakeWord, offset);
415 // return fakeWord++;
416 //NOTE: This only works if a bunch of things are set in BUTCH which we currently don't
417 // check for. !!! FIX !!!
418 // Partially fixed: We check for I2SCNTRL in the JERRY I2S routine...
419 // return GetWordFromButchSSI(offset, who);
420 if (offset == LRXD || offset == RRXD)
422 else if (offset == LRXD + 2)
424 else if (offset == RRXD + 2)
427 return 0xFFFF; // May need SSTAT as well... (but may be a Jaguar II only feature)