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