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