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
16 #define BUFFER_SIZE 0x8000 // Make the DAC buffers 32K x 16 bits
18 // Jaguar memory locations
23 #define SMODE 0xF1A154
27 uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr;
28 SDL_AudioSpec desired;
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.
34 uint8 SCLKFrequencyDivider = 19; // Default is roughly 22 KHz (20774 Hz in NTSC mode)
35 uint16 serialMode = 0;
37 // Private function prototypes
39 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
40 int GetCalculatedFrequency(void);
43 // Initialize the SDL sound system (?) (!)
47 memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
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)...
52 desired.samples = 4096; // Let's try a 4K buffer (can always go lower)
53 desired.callback = SDLSoundCallback;
55 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
57 WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n");
63 SDL_PauseAudio(false); // Start playback!
64 WriteLog("DAC: Successfully initialized.\n");
68 // Reset the sound buffer FIFOs
72 LeftFIFOHeadPtr = LeftFIFOTailPtr = 0, RightFIFOHeadPtr = RightFIFOTailPtr = 1;
76 // Close down the SDL sound subsystem (?) (!)
82 WriteLog("DAC: Done.\n");
86 // SDL callback routine to fill audio buffer
88 // Note: The samples are packed in the buffer in 16 bit left/16 bit right pairs.
90 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
92 // Clear the buffer to silence, in case the DAC buffer is empty (or short)
93 memset(buffer, desired.silence, length);
94 //WriteLog("DAC: Inside callback...\n");
95 if (LeftFIFOHeadPtr != LeftFIFOTailPtr)
97 //WriteLog("DAC: About to write some data!\n");
98 int numLeftSamplesReady
99 = (LeftFIFOTailPtr + (LeftFIFOTailPtr < LeftFIFOHeadPtr ? BUFFER_SIZE : 0))
101 int numRightSamplesReady
102 = (RightFIFOTailPtr + (RightFIFOTailPtr < RightFIFOHeadPtr ? BUFFER_SIZE : 0))
105 = (numLeftSamplesReady < numRightSamplesReady
106 ? numLeftSamplesReady : numRightSamplesReady);//Hmm. * 2;
108 //The numbers look good--it's just that the DSP can't get enough samples in the DAC buffer!
109 //WriteLog("DAC: Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
110 //WriteLog(" numLeft/RightSamplesReady: %i/%i, numSamplesReady: %i, length of buffer: %i\n", numLeftSamplesReady, numRightSamplesReady, numSamplesReady, length);
112 /* if (numSamplesReady > length)
113 numSamplesReady = length;//*/
114 if (numSamplesReady > length / 2) // length / 2 because we're comparing 16-bit lengths
115 numSamplesReady = length / 2;
117 // WriteLog(" Not enough samples to fill the buffer (short by %u L/R samples)...\n", (length / 2) - numSamplesReady);
118 //WriteLog("DAC: %u samples ready.\n", numSamplesReady);
120 // Actually, it's a bit more involved than this, but this is the general idea:
121 // memcpy(buffer, DACBuffer, length);
122 for(int i=0; i<numSamplesReady; i++)
123 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
124 ((uint16 *)buffer)[i] = DACBuffer[(LeftFIFOHeadPtr + i) % BUFFER_SIZE];
125 // buffer[i] = DACBuffer[(LeftFIFOHeadPtr + i) & (BUFFER_SIZE - 1)];
127 LeftFIFOHeadPtr = (LeftFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
128 RightFIFOHeadPtr = (RightFIFOHeadPtr + numSamplesReady) % BUFFER_SIZE;
129 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
130 // LeftFIFOHeadPtr = (LeftFIFOHeadPtr + (numSamplesReady)) & (BUFFER_SIZE - 1);
131 // RightFIFOHeadPtr = (RightFIFOHeadPtr + (numSamplesReady)) & (BUFFER_SIZE - 1);
132 //WriteLog(" -> Left/RightFIFOHeadPtr: %u/%u, Left/RightFIFOTailPtr: %u/%u\n", LeftFIFOHeadPtr, RightFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOTailPtr);
134 //Hmm. Seems that the SDL buffer isn't being starved by the DAC buffer...
136 // WriteLog("DAC: Silence...!\n");
140 // Calculate the frequency of SCLK * 32 using the divider
142 int GetCalculatedFrequency(void)
144 // extern bool hardwareTypeNTSC;
145 int systemClockFrequency = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
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)));
153 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
155 void DACWriteByte(uint32 offset, uint8 data)
157 WriteLog("DAC: Writing %02X at %08X\n", data, offset);
158 if (offset == SCLK + 3)
159 DACWriteWord(offset - 3, (uint16)data);
162 void DACWriteWord(uint32 offset, uint16 data)
164 if (offset == LTXD + 2)
166 if (LeftFIFOTailPtr + 2 != LeftFIFOHeadPtr)
168 SDL_LockAudio(); // Is it necessary to do this? Mebbe.
169 // We use a circular buffer 'cause it's easy. Note that the callback function
170 // takes care of dumping audio to the soundcard...! Also note that we're writing
171 // the samples in the buffer in an interleaved L/R format.
172 LeftFIFOTailPtr = (LeftFIFOTailPtr + 2) % BUFFER_SIZE;
173 DACBuffer[LeftFIFOTailPtr] = data;
174 // Aaron's code does this, but I don't know why...
175 //Flipping this bit makes the audio MUCH louder. Need to look at the amplitude of the
176 //waveform to see if any massaging is needed here...
177 //Looks like a cheap & dirty way to convert signed samples to unsigned...
178 // DACBuffer[LeftFIFOTailPtr] = data ^ 0x8000;
183 WriteLog("DAC: Ran into FIFO's left tail pointer!\n");
186 else if (offset == RTXD + 2)
188 if (RightFIFOTailPtr + 2 != RightFIFOHeadPtr)
191 RightFIFOTailPtr = (RightFIFOTailPtr + 2) % BUFFER_SIZE;
192 DACBuffer[RightFIFOTailPtr] = data;
193 // Aaron's code does this, but I don't know why...
194 // DACBuffer[RightFIFOTailPtr] = data ^ 0x8000;
199 WriteLog("DAC: Ran into FIFO's right tail pointer!\n");
202 else if (offset == SCLK + 2) // Sample rate
204 WriteLog("DAC: Writing %u to SCLK...\n", data);
205 if ((uint8)data != SCLKFrequencyDivider)
207 SCLKFrequencyDivider = (uint8)data;
208 //Of course a better way would be to query the hardware to find the upper limit...
209 if (data > 7) // Anything less than 8 is too high!
212 desired.freq = GetCalculatedFrequency();// SDL will do conversion on the fly, if it can't get the exact rate. Nice!
213 WriteLog("DAC: Changing sample rate to %u Hz!\n", desired.freq);
215 if (SDL_OpenAudio(&desired, NULL) < 0) // NULL means SDL guarantees what we want
217 WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq);
223 SDL_PauseAudio(false); // Start playback!
227 else if (offset == SMODE + 2)
230 WriteLog("DAC: Writing to SMODE. Bits: %s%s%s%s%s%s\n",
231 (data & 0x01 ? "INTERNAL " : ""), (data & 0x02 ? "MODE " : ""),
232 (data & 0x04 ? "WSEN " : ""), (data & 0x08 ? "RISING " : ""),
233 (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : ""));
238 // LRXD/RRXD/SSTAT ($F1A148/4C/50)
240 uint8 DACReadByte(uint32 offset)
242 // WriteLog("DAC: Reading byte from %08X\n", offset);
246 uint16 DACReadWord(uint32 offset)
248 // WriteLog("DAC: Reading word from %08X\n", offset);