]> Shamusworld >> Repos - virtualjaguar/blob - src/dac.cpp
Added missing parens to LHS of FIFO spinlocks--should work properly now.
[virtualjaguar] / src / dac.cpp
1 //
2 // DAC (really, Synchronous Serial Interface) Handler
3 //
4 // Originally by David Raingeard
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Rewritten by James L. Hammons
7 //
8
9 #include "SDL.h"
10 #include "m68k.h"
11 #include "jaguar.h"
12 #include "settings.h"
13 #include "dac.h"
14
15 //#define DEBUG_DAC
16
17 #define BUFFER_SIZE             0x10000                                         // Make the DAC buffers 64K x 16 bits
18
19 // Jaguar memory locations
20
21 #define LTXD                    0xF1A148
22 #define RTXD                    0xF1A14C
23 #define LRXD                    0xF1A148
24 #define RRXD                    0xF1A14C
25 #define SCLK                    0xF1A150
26 #define SMODE                   0xF1A154
27
28 // Global variables
29
30 uint16 lrxd, rrxd;                                                                      // I2S ports (into Jaguar)
31
32 // Local variables
33
34 static uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr;
35 static SDL_AudioSpec desired;
36 static bool SDLSoundInitialized = false;
37
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.
40
41 static uint16 * DACBuffer;
42 static uint8 SCLKFrequencyDivider = 19;                         // Default is roughly 22 KHz (20774 Hz in NTSC mode)
43 /*static*/ uint16 serialMode = 0;
44
45 // Private function prototypes
46
47 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
48 int GetCalculatedFrequency(void);
49
50 //
51 // Initialize the SDL sound system
52 //
53 void DACInit(void)
54 {
55         memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
56
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)...
59         desired.channels = 2;
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;
63
64         if (SDL_OpenAudio(&desired, NULL) < 0)                  // NULL means SDL guarantees what we want
65         {
66 //              WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n");
67 //              log_done();
68 //              exit(1);
69                 WriteLog("DAC: Failed to initialize SDL sound...\n");
70         }
71         else
72         {
73                 SDLSoundInitialized = true;
74                 DACReset();
75                 SDL_PauseAudio(false);                                                  // Start playback!
76                 WriteLog("DAC: Successfully initialized.\n");
77         }
78 }
79
80 //
81 // Reset the sound buffer FIFOs
82 //
83 void DACReset(void)
84 {
85         LeftFIFOHeadPtr = LeftFIFOTailPtr = 0, RightFIFOHeadPtr = RightFIFOTailPtr = 1;
86 }
87
88 //
89 // Close down the SDL sound subsystem
90 //
91 void DACDone(void)
92 {
93         if (SDLSoundInitialized)
94         {
95                 SDL_PauseAudio(true);
96                 SDL_CloseAudio();
97         }
98
99         memory_free(DACBuffer);
100         WriteLog("DAC: Done.\n");
101 }
102
103 //
104 // SDL callback routine to fill audio buffer
105 //
106 // Note: The samples are packed in the buffer in 16 bit left/16 bit right pairs.
107 //
108 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
109 {
110         // Clear the buffer to silence, in case the DAC buffer is empty (or short)
111 //This causes choppy sound... Ick.
112         memset(buffer, desired.silence, length);
113 //WriteLog("DAC: Inside callback...\n");
114         if (LeftFIFOHeadPtr != LeftFIFOTailPtr)
115         {
116 //WriteLog("DAC: About to write some data!\n");
117                 int numLeftSamplesReady
118                         = (LeftFIFOTailPtr + (LeftFIFOTailPtr < LeftFIFOHeadPtr ? BUFFER_SIZE : 0))
119                                 - LeftFIFOHeadPtr;
120                 int numRightSamplesReady
121                         = (RightFIFOTailPtr + (RightFIFOTailPtr < RightFIFOHeadPtr ? BUFFER_SIZE : 0))
122                                 - RightFIFOHeadPtr;
123                 int numSamplesReady
124                         = (numLeftSamplesReady < numRightSamplesReady
125                                 ? numLeftSamplesReady : numRightSamplesReady);//Hmm. * 2;
126
127 //The numbers look good--it's just that the DSP can't get enough samples in the DAC buffer!
128 //WriteLog("DAC: Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
129 //WriteLog("     numLeft/RightSamplesReady: %i/%i, numSamplesReady: %i, length of buffer: %i\n", numLeftSamplesReady, numRightSamplesReady, numSamplesReady, length);
130
131 /*              if (numSamplesReady > length)
132                         numSamplesReady = length;//*/
133                 if (numSamplesReady > length / 2)       // length / 2 because we're comparing 16-bit lengths
134                         numSamplesReady = length / 2;
135 //else
136 //      WriteLog("     Not enough samples to fill the buffer (short by %u L/R samples)...\n", (length / 2) - numSamplesReady);
137 //WriteLog("DAC: %u samples ready.\n", numSamplesReady);
138
139                 // Actually, it's a bit more involved than this, but this is the general idea:
140 //              memcpy(buffer, DACBuffer, length);
141                 for(int i=0; i<numSamplesReady; i++)
142                         ((uint16 *)buffer)[i] = DACBuffer[(LeftFIFOHeadPtr + i) % BUFFER_SIZE];
143                         // Could also use (as long as BUFFER_SIZE is a multiple of 2):
144 //                      buffer[i] = DACBuffer[(LeftFIFOHeadPtr + i) & (BUFFER_SIZE - 1)];
145
146                 LeftFIFOHeadPtr = (LeftFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
147                 RightFIFOHeadPtr = (RightFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
148                 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
149 //              LeftFIFOHeadPtr = (LeftFIFOHeadPtr + numSamplesReady) & (BUFFER_SIZE - 1);
150 //              RightFIFOHeadPtr = (RightFIFOHeadPtr + numSamplesReady) & (BUFFER_SIZE - 1);
151 //WriteLog("  -> Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
152         }
153 //Hmm. Seems that the SDL buffer isn't being starved by the DAC buffer...
154 //      else
155 //              WriteLog("DAC: Silence...!\n");
156 }
157
158 //
159 // Calculate the frequency of SCLK * 32 using the divider
160 //
161 int GetCalculatedFrequency(void)
162 {
163         int systemClockFrequency = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
164
165         // We divide by 32 here in order to find the frequency of 32 SCLKs in a row (transferring
166         // 16 bits of left data + 16 bits of right data = 32 bits, 1 SCLK = 1 bit transferred).
167         return systemClockFrequency / (32 * (2 * (SCLKFrequencyDivider + 1)));
168 }
169
170 //
171 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
172 //
173 void DACWriteByte(uint32 offset, uint8 data, uint32 who/*= UNKNOWN*/)
174 {
175         WriteLog("DAC: %s writing BYTE %02X at %08X\n", whoName[who], data, offset);
176         if (offset == SCLK + 3)
177                 DACWriteWord(offset - 3, (uint16)data);
178 }
179
180 void DACWriteWord(uint32 offset, uint16 data, uint32 who/*= UNKNOWN*/)
181 {
182         if (offset == LTXD + 2)
183         {
184                 // Spin until buffer has been drained (for too fast processors!)...
185 //Small problem--if Head == 0 and Tail == buffer end, then this will fail... !!! FIX !!!
186 //[DONE]
187                 // Also, we're taking advantage of the fact that the buffer is a multiple of two
188                 // in this check...
189                 while (((LeftFIFOTailPtr + 2) & (BUFFER_SIZE - 1)) == LeftFIFOHeadPtr);
190
191                 SDL_LockAudio();                                                        // Is it necessary to do this? Mebbe.
192                 // We use a circular buffer 'cause it's easy. Note that the callback function
193                 // takes care of dumping audio to the soundcard...! Also note that we're writing
194                 // the samples in the buffer in an interleaved L/R format.
195                 LeftFIFOTailPtr = (LeftFIFOTailPtr + 2) % BUFFER_SIZE;
196                 DACBuffer[LeftFIFOTailPtr] = data;
197                 SDL_UnlockAudio();
198         }
199         else if (offset == RTXD + 2)
200         {
201                 // Spin until buffer has been drained (for too fast processors!)...
202 //uint32 spin = 0;
203                 while (((RightFIFOTailPtr + 2) & (BUFFER_SIZE - 1)) == RightFIFOHeadPtr);
204 /*              {
205 spin++;
206 //if ((spin & 0x0FFFFFFF) == 0)
207 //      WriteLog("Tail=%X, Head=%X, BUFFER_SIZE-1=%X\n", RightFIFOTailPtr, RightFIFOHeadPtr, BUFFER_SIZE - 1);
208
209 if (spin == 0xFFFF0000)
210 {
211 uint32 rtail = RightFIFOTailPtr, rhead = RightFIFOHeadPtr;
212 WriteLog("Tail=%X, Head=%X\n", rtail, rhead);
213
214         WriteLog("\nStuck in right DAC spinlock!\nAborting!\n\n");
215         WriteLog("Tail=%X, Head=%X, BUFFER_SIZE-1=%X\n", RightFIFOTailPtr, RightFIFOHeadPtr, BUFFER_SIZE - 1);
216         WriteLog("From while: Tail=%X, Head=%X", (RightFIFOTailPtr + 2) & (BUFFER_SIZE - 1), RightFIFOHeadPtr);
217         log_done();
218         exit(0);
219 }
220                 }//*/
221
222 //This is wrong         if (RightFIFOTailPtr + 2 != RightFIFOHeadPtr)
223 //              {
224                 SDL_LockAudio();
225                 RightFIFOTailPtr = (RightFIFOTailPtr + 2) % BUFFER_SIZE;
226                 DACBuffer[RightFIFOTailPtr] = data;
227                 SDL_UnlockAudio();
228 //              }
229 /*#ifdef DEBUG_DAC
230                 else
231                         WriteLog("DAC: Ran into FIFO's right tail pointer!\n");
232 #endif*/
233         }
234         else if (offset == SCLK + 2)                                    // Sample rate
235         {
236                 WriteLog("DAC: Writing %u to SCLK...\n", data);
237                 if ((uint8)data != SCLKFrequencyDivider)
238                 {
239                         SCLKFrequencyDivider = (uint8)data;
240 //Of course a better way would be to query the hardware to find the upper limit...
241                         if (data > 7)   // Anything less than 8 is too high!
242                         {
243                                 if (SDLSoundInitialized)
244                                         SDL_CloseAudio();
245
246                                 desired.freq = GetCalculatedFrequency();// SDL will do conversion on the fly, if it can't get the exact rate. Nice!
247                                 WriteLog("DAC: Changing sample rate to %u Hz!\n", desired.freq);
248
249                                 if (SDLSoundInitialized)
250                                 {
251                                         if (SDL_OpenAudio(&desired, NULL) < 0)  // NULL means SDL guarantees what we want
252                                         {
253                                                 WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq);
254                                                 log_done();
255                                                 exit(1);
256                                         }
257                                 }
258
259                                 DACReset();
260
261                                 if (SDLSoundInitialized)
262                                         SDL_PauseAudio(false);                  // Start playback!
263                         }
264                 }
265         }
266         else if (offset == SMODE + 2)
267         {
268                 serialMode = data;
269                 WriteLog("DAC: %s writing to SMODE. Bits: %s%s%s%s%s%s [68K PC=%08X]\n", whoName[who],
270                         (data & 0x01 ? "INTERNAL " : ""), (data & 0x02 ? "MODE " : ""),
271                         (data & 0x04 ? "WSEN " : ""), (data & 0x08 ? "RISING " : ""),
272                         (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : ""),
273                         m68k_get_reg(NULL, M68K_REG_PC));
274         }
275 }
276
277 //
278 // LRXD/RRXD/SSTAT ($F1A148/4C/50)
279 //
280 uint8 DACReadByte(uint32 offset, uint32 who/*= UNKNOWN*/)
281 {
282 //      WriteLog("DAC: %s reading byte from %08X\n", whoName[who], offset);
283         return 0xFF;
284 }
285
286 //static uint16 fakeWord = 0;
287 uint16 DACReadWord(uint32 offset, uint32 who/*= UNKNOWN*/)
288 {
289 //      WriteLog("DAC: %s reading word from %08X\n", whoName[who], offset);
290 //      return 0xFFFF;
291 //      WriteLog("DAC: %s reading WORD %04X from %08X\n", whoName[who], fakeWord, offset);
292 //      return fakeWord++;
293 //NOTE: This only works if a bunch of things are set in BUTCH which we currently don't
294 //      check for. !!! FIX !!!
295 // Partially fixed: We check for I2SCNTRL in the JERRY I2S routine...
296 //      return GetWordFromButchSSI(offset, who);
297         if (offset == LRXD || offset == RRXD)
298                 return 0x0000;
299         else if (offset == LRXD + 2)
300                 return lrxd;
301         else if (offset == RRXD + 2)
302                 return rrxd;
303
304         return 0xFFFF;  // May need SSTAT as well... (but may be a Jaguar II only feature)              
305 }