2 // DAC (really, Synchronous Serial Interface) Handler
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Rewritten by James L. Hammons
15 #define BUFFER_SIZE 0x8000 // Make the DAC buffers 32K x 16 bits
17 // Jaguar memory locations
22 #define SMODE 0xF1A154
26 uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr;
27 SDL_AudioSpec desired;
29 // We can get away with using native endian here because we can tell SDL to use the native
30 // endian when looking at the sample buffer, i.e., no need to worry about it.
33 uint8 SCLKFrequencyDivider = 19; // Default is roughly 22 KHz (20774 Hz in NTSC mode)
34 uint16 serialMode = 0;
36 // Private function prototypes
38 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
39 int GetCalculatedFrequency(void);
42 // Initialize the SDL sound system (?) (!)
46 memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
48 desired.freq = GetCalculatedFrequency(); // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
49 desired.format = AUDIO_S16SYS; // This uses the native endian (for portability)...
51 desired.samples = 4096; // Let's try a 4K buffer (can always go lower)
52 desired.callback = SDLSoundCallback;
54 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
56 WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n");
62 SDL_PauseAudio(false); // Start playback!
63 WriteLog("DAC: Successfully initialized.\n");
67 // Reset the sound buffer FIFOs
71 LeftFIFOHeadPtr = LeftFIFOTailPtr = 0, RightFIFOHeadPtr = RightFIFOTailPtr = 1;
75 // Close down the SDL sound subsystem (?) (!)
81 WriteLog("DAC: Done.\n");
85 // SDL callback routine to fill audio buffer
87 // Note: The samples are packed in the buffer in 16 bit left/16 bit right pairs.
89 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
91 // Clear the buffer to silence, in case the DAC buffer is empty (or short)
92 memset(buffer, desired.silence, length);
93 //WriteLog("DAC: Inside callback...\n");
94 if (LeftFIFOHeadPtr != LeftFIFOTailPtr)
96 //WriteLog("DAC: About to write some data!\n");
97 int numLeftSamplesReady
98 = (LeftFIFOTailPtr + (LeftFIFOTailPtr < LeftFIFOHeadPtr ? BUFFER_SIZE : 0))
100 int numRightSamplesReady
101 = (RightFIFOTailPtr + (RightFIFOTailPtr < RightFIFOHeadPtr ? BUFFER_SIZE : 0))
104 = (numLeftSamplesReady < numRightSamplesReady
105 ? numLeftSamplesReady : numRightSamplesReady);//Hmm. * 2;
107 //The numbers look good--it's just that the DSP can't get enough samples in the DAC buffer!
108 //WriteLog("DAC: Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
109 //WriteLog(" numLeft/RightSamplesReady: %i/%i, numSamplesReady: %i, length of buffer: %i\n", numLeftSamplesReady, numRightSamplesReady, numSamplesReady, length);
111 /* if (numSamplesReady > length)
112 numSamplesReady = length;//*/
113 if (numSamplesReady > length / 2) // length / 2 because we're comparing 16-bit lengths
114 numSamplesReady = length / 2;
116 // WriteLog(" Not enough samples to fill the buffer (short by %u L/R samples)...\n", (length / 2) - numSamplesReady);
117 //WriteLog("DAC: %u samples ready.\n", numSamplesReady);
119 // Actually, it's a bit more involved than this, but this is the general idea:
120 // memcpy(buffer, DACBuffer, length);
121 for(int i=0; i<numSamplesReady; i++)
122 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
123 ((uint16 *)buffer)[i] = DACBuffer[(LeftFIFOHeadPtr + i) % BUFFER_SIZE];
124 // buffer[i] = DACBuffer[(LeftFIFOHeadPtr + i) & (BUFFER_SIZE - 1)];
126 LeftFIFOHeadPtr = (LeftFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
127 RightFIFOHeadPtr = (RightFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
128 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
129 // LeftFIFOHeadPtr = (LeftFIFOHeadPtr + (numSamplesReady)) & (BUFFER_SIZE - 1);
130 // RightFIFOHeadPtr = (RightFIFOHeadPtr + (numSamplesReady)) & (BUFFER_SIZE - 1);
131 //WriteLog(" -> Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
133 //Hmm. Seems that the SDL buffer isn't being starved by the DAC buffer...
135 // WriteLog("DAC: Silence...!\n");
139 // Calculate the frequency of SCLK * 32 using the divider
141 int GetCalculatedFrequency(void)
143 extern bool hardwareTypeNTSC;
144 int systemClockFrequency = (hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
146 // We divide by 32 here in order to find the frequency of 32 SCLKs in a row (transferring
147 // 16 bits of left data + 16 bits of right data = 32 bits, 1 SCLK = 1 bit transferred).
148 return systemClockFrequency / (32 * (2 * (SCLKFrequencyDivider + 1)));
152 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
154 void DACWriteByte(uint32 offset, uint8 data)
156 WriteLog("DAC: Writing %02X at %08X\n", data, offset);
157 if (offset == SCLK + 3)
158 DACWriteWord(offset - 3, (uint16)data);
161 void DACWriteWord(uint32 offset, uint16 data)
163 if (offset == LTXD + 2)
165 if (LeftFIFOTailPtr + 2 != LeftFIFOHeadPtr)
167 SDL_LockAudio(); // Is it necessary to do this? Mebbe.
168 // We use a circular buffer 'cause it's easy. Note that the callback function
169 // takes care of dumping audio to the soundcard...! Also note that we're writing
170 // the samples in the buffer in an interleaved L/R format.
171 LeftFIFOTailPtr = (LeftFIFOTailPtr + 2) % BUFFER_SIZE;
172 DACBuffer[LeftFIFOTailPtr] = data;
173 // Aaron's code does this, but I don't know why...
174 //Flipping this bit makes the audio MUCH louder. Need to look at the amplitude of the
175 //waveform to see if any massaging is needed here...
176 //Looks like a cheap & dirty way to convert signed samples to unsigned...
177 // DACBuffer[LeftFIFOTailPtr] = data ^ 0x8000;
182 WriteLog("DAC: Ran into FIFO's left tail pointer!\n");
185 else if (offset == RTXD + 2)
187 if (RightFIFOTailPtr + 2 != RightFIFOHeadPtr)
190 RightFIFOTailPtr = (RightFIFOTailPtr + 2) % BUFFER_SIZE;
191 DACBuffer[RightFIFOTailPtr] = data;
192 // Aaron's code does this, but I don't know why...
193 // DACBuffer[RightFIFOTailPtr] = data ^ 0x8000;
198 WriteLog("DAC: Ran into FIFO's right tail pointer!\n");
201 else if (offset == SCLK + 2) // Sample rate
203 WriteLog("DAC: Writing %u to SCLK...\n", data);
204 if ((uint8)data != SCLKFrequencyDivider)
206 SCLKFrequencyDivider = (uint8)data;
207 //Of course a better way would be to query the hardware to find the upper limit...
208 if (data > 7) // Anything less than 8 is too high!
211 desired.freq = GetCalculatedFrequency();// SDL will do conversion on the fly, if it can't get the exact rate. Nice!
212 WriteLog("DAC: Changing sample rate to %u Hz!\n", desired.freq);
214 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
216 WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq);
222 SDL_PauseAudio(false); // Start playback!
226 else if (offset == SMODE + 2)
229 WriteLog("DAC: Writing to SMODE. Bits: %s%s%s%s%s%s\n",
230 (data & 0x01 ? "INTERNAL " : ""), (data & 0x02 ? "MODE " : ""),
231 (data & 0x04 ? "WSEN " : ""), (data & 0x08 ? "RISING " : ""),
232 (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : ""));
237 // LRXD/RRXD/SSTAT ($F1A148/4C/50)
239 uint8 DACReadByte(uint32 offset)
241 // WriteLog("DAC: Reading byte from %08X\n", offset);
245 uint16 DACReadWord(uint32 offset)
247 // WriteLog("DAC: Reading word from %08X\n", offset);