]> Shamusworld >> Repos - virtualjaguar/blob - src/dac.cpp
Added some #ifdefs to reduce unwanted logging
[virtualjaguar] / src / dac.cpp
1 //
2 // DAC (really, Synchronous Serial Interface) Handler
3 //
4 // 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
13 //#define DEBUG_DAC
14
15 #define BUFFER_SIZE             0x8000                                          // Make the DAC buffers 32K x 16 bits
16
17 // Jaguar memory locations
18
19 #define LTXD                    0xF1A148
20 #define RTXD                    0xF1A14C
21 #define SCLK                    0xF1A150
22 #define SMODE                   0xF1A154
23
24 // Local variables
25
26 uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr;
27 SDL_AudioSpec desired;
28
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.
31
32 uint16 * DACBuffer;
33 uint8 SCLKFrequencyDivider = 19;                                                // Default is roughly 22 KHz (20774 Hz in NTSC mode)
34 uint16 serialMode = 0;
35
36 // Private function prototypes
37
38 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
39 int GetCalculatedFrequency(void);
40
41 //
42 // Initialize the SDL sound system (?) (!)
43 //
44 void DACInit(void)
45 {
46         memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
47
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)...
50         desired.channels = 2;
51         desired.samples = 4096;                                                 // Let's try a 4K buffer (can always go lower)
52         desired.callback = SDLSoundCallback;
53
54         if (SDL_OpenAudio(&desired, NULL) < 0)                  // NULL means SDL guarantees what we want
55         {
56                 WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n");
57                 log_done();
58                 exit(1);
59         }
60
61         DACReset();
62         SDL_PauseAudio(false);                                                  // Start playback!
63         WriteLog("DAC: Successfully initialized.\n");
64 }
65
66 //
67 // Reset the sound buffer FIFOs
68 //
69 void DACReset(void)
70 {
71         LeftFIFOHeadPtr = LeftFIFOTailPtr = 0, RightFIFOHeadPtr = RightFIFOTailPtr = 1;
72 }
73
74 //
75 // Close down the SDL sound subsystem (?) (!)
76 //
77 void DACDone(void)
78 {
79         SDL_PauseAudio(true);
80         SDL_CloseAudio();
81         WriteLog("DAC: Done.\n");
82 }
83
84 //
85 // SDL callback routine to fill audio buffer
86 //
87 // Note: The samples are packed in the buffer in 16 bit left/16 bit right pairs.
88 //
89 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
90 {
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)
95         {
96 //WriteLog("DAC: About to write some data!\n");
97                 int numLeftSamplesReady
98                         = (LeftFIFOTailPtr + (LeftFIFOTailPtr < LeftFIFOHeadPtr ? BUFFER_SIZE : 0))
99                                 - LeftFIFOHeadPtr;
100                 int numRightSamplesReady
101                         = (RightFIFOTailPtr + (RightFIFOTailPtr < RightFIFOHeadPtr ? BUFFER_SIZE : 0))
102                                 - RightFIFOHeadPtr;
103                 int numSamplesReady
104                         = (numLeftSamplesReady < numRightSamplesReady
105                                 ? numLeftSamplesReady : numRightSamplesReady);//Hmm. * 2;
106
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);
110
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;
115 //else
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);
118
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)];
125
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);
132         }
133 //Hmm. Seems that the SDL buffer isn't being starved by the DAC buffer...
134 //      else
135 //              WriteLog("DAC: Silence...!\n");
136 }
137
138 //
139 // Calculate the frequency of SCLK * 32 using the divider
140 //
141 int GetCalculatedFrequency(void)
142 {
143         extern bool hardwareTypeNTSC;
144         int systemClockFrequency = (hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
145
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)));
149 }
150
151 //
152 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
153 //
154 void DACWriteByte(uint32 offset, uint8 data)
155 {
156         WriteLog("DAC: Writing %02X at %08X\n", data, offset);
157         if (offset == SCLK + 3)
158                 DACWriteWord(offset - 3, (uint16)data);
159 }
160
161 void DACWriteWord(uint32 offset, uint16 data)
162 {
163         if (offset == LTXD + 2)
164         {
165                 if (LeftFIFOTailPtr + 2 != LeftFIFOHeadPtr)
166                 {
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;
178                         SDL_UnlockAudio();
179                 }
180 #ifdef DEBUG_DAC
181                 else
182                         WriteLog("DAC: Ran into FIFO's left tail pointer!\n");
183 #endif
184         }
185         else if (offset == RTXD + 2)
186         {
187                 if (RightFIFOTailPtr + 2 != RightFIFOHeadPtr)
188                 {
189                         SDL_LockAudio();
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;
194                         SDL_UnlockAudio();
195                 }
196 #ifdef DEBUG_DAC
197                 else
198                         WriteLog("DAC: Ran into FIFO's right tail pointer!\n");
199 #endif
200         }
201         else if (offset == SCLK + 2)                                    // Sample rate
202         {
203                 WriteLog("DAC: Writing %u to SCLK...\n", data);
204                 if ((uint8)data != SCLKFrequencyDivider)
205                 {
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!
209                         {
210                                 SDL_CloseAudio();
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);
213
214                                 if (SDL_OpenAudio(&desired, NULL) < 0)  // NULL means SDL guarantees what we want
215                                 {
216                                         WriteLog("DAC: Failed to initialize SDL sound: %s.\nDesired freq: %u\nShutting down!\n", SDL_GetError(), desired.freq);
217                                         log_done();
218                                         exit(1);
219                                 }
220
221                                 DACReset();
222                                 SDL_PauseAudio(false);                          // Start playback!
223                         }
224                 }
225         }
226         else if (offset == SMODE + 2)
227         {
228                 serialMode = data;
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" : ""));
233         }
234 }
235
236 //
237 // LRXD/RRXD/SSTAT ($F1A148/4C/50)
238 //
239 uint8 DACReadByte(uint32 offset)
240 {
241 //      WriteLog("DAC: Reading byte from %08X\n", offset);
242         return 0xFF;
243 }
244
245 uint16 DACReadWord(uint32 offset)
246 {
247 //      WriteLog("DAC: Reading word from %08X\n", offset);
248         return 0xFFFF;
249 }