]> Shamusworld >> Repos - virtualjaguar/blob - src/dac.cpp
Brand new sound code uses SDL!
[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 BUFFER_SIZE             0x8000                                          // Make the DAC buffers 32K x 16 bits
14
15 // Jaguar memory locations
16
17 #define LTXD                    0xF1A148
18 #define RTXD                    0xF1A14C
19 #define SCLK                    0xF1A150
20 #define SMODE                   0xF1A154
21
22 // Local variables
23
24 uint32 LeftFIFOHeadPtr, LeftFIFOTailPtr, RightFIFOHeadPtr, RightFIFOTailPtr;
25 SDL_AudioSpec desired;
26
27 // We can get away with using native endian here because we can tell SDL to use the native
28 // when looking at the sample buffer, i.e., no need to worry about it.
29
30 uint16 * DACBuffer;
31 uint8 SCLKFrequencyDivider = 9;                                         // Start out roughly 44.1K (46164 Hz in NTSC mode)
32 uint16 serialMode = 0;
33
34 // Private function prototypes
35
36 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
37 int GetCalculatedFrequency(void);
38
39 //
40 // Initialize the SDL sound system (?) (!)
41 //
42 void DACInit(void)
43 {
44         memory_malloc_secure((void **)&DACBuffer, BUFFER_SIZE * sizeof(uint16), "DAC buffer");
45
46         desired.freq = GetCalculatedFrequency();                // SDL will do conversion on the fly, if it can't get the exact rate. Nice!
47         desired.format = AUDIO_S16SYS;                                  // This uses the native endian (for portability)...
48         desired.channels = 2;
49         desired.samples = 4096;                                                 // Let's try a 4K buffer (can always go lower)
50         desired.callback = SDLSoundCallback;
51
52         if (SDL_OpenAudio(&desired, NULL) < 0)                  // NULL means SDL guarantees what we want
53         {
54                 WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n");
55                 log_done();
56                 exit(1);
57         }
58
59         DACReset();
60         SDL_PauseAudio(false);                                                  // Start playback!
61         WriteLog("DAC: Successfully initialized.\n");
62 }
63
64 //
65 // Reset the sound buffer FIFOs
66 //
67 void DACReset(void)
68 {
69         LeftFIFOHeadPtr = LeftFIFOTailPtr = 0, RightFIFOHeadPtr = RightFIFOTailPtr = 1;
70 }
71
72 //
73 // Close down the SDL sound subsystem (?) (!)
74 //
75 void DACDone(void)
76 {
77         SDL_PauseAudio(true);
78         SDL_CloseAudio();
79         WriteLog("DAC: Done.\n");
80 }
81
82 //
83 // SDL callback routine to fill audio buffer
84 //
85 // Note: The samples are packed in the buffer in 16 bit left/16 bit right pairs.
86 //
87 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
88 {
89 //WriteLog("DAC: Inside callback...\n");
90         if (LeftFIFOHeadPtr != LeftFIFOTailPtr)
91         {
92 //WriteLog("DAC: About to write some data!\n");
93                 int numLeftSamplesReady
94                         = (LeftFIFOTailPtr + (LeftFIFOTailPtr < LeftFIFOHeadPtr ? BUFFER_SIZE : 0))
95                                 - LeftFIFOHeadPtr;
96                 int numRightSamplesReady
97                         = (RightFIFOTailPtr + (RightFIFOTailPtr < RightFIFOHeadPtr ? BUFFER_SIZE : 0))
98                                 - RightFIFOHeadPtr;
99                 int numSamplesReady
100                         = (numLeftSamplesReady < numRightSamplesReady
101                                 ? numLeftSamplesReady : numRightSamplesReady) * 2;
102
103                 if (numSamplesReady > length)
104                         numSamplesReady = length;
105
106                 // Actually, it's a bit more involved than this, but this is the general idea:
107 //              memcpy(buffer, DACBuffer, length);
108                 for(int i=0; i<numSamplesReady; i++)
109                         // Could also use (as long as BUFFER_SIZE is a multiple of 2):
110                         ((uint16 *)buffer)[i] = DACBuffer[(LeftFIFOHeadPtr + i) % BUFFER_SIZE];
111 //                      buffer[i] = DACBuffer[(LeftFIFOHeadPtr + i) & (BUFFER_SIZE - 1)];
112
113                 LeftFIFOHeadPtr = (LeftFIFOHeadPtr + (numSamplesReady / 2)) % BUFFER_SIZE;
114                 RightFIFOHeadPtr = (RightFIFOHeadPtr + (numSamplesReady / 2)) % BUFFER_SIZE;
115                 // Could also use (as long as BUFFER_SIZE is a multiple of 2):
116 //              LeftFIFOHeadPtr = (LeftFIFOHeadPtr + (numSamplesReady / 2)) & (BUFFER_SIZE - 1);
117 //              RightFIFOHeadPtr = (RightFIFOHeadPtr + (numSamplesReady / 2)) & (BUFFER_SIZE - 1);
118         }
119 }
120
121 //
122 // Calculate the frequency of SCLK * 32 using the divider
123 //
124 int GetCalculatedFrequency(void)
125 {
126         extern bool hardwareTypeNTSC;
127         int systemClockFrequency = (hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
128
129         // We divide by 32 here in order to find the frequency of 32 SCLKs in a row (transferring
130         // 16 bits of left data + 16 bits of right data = 32 bits, 1 SCLK = 1 bit transferred).
131         return systemClockFrequency / (32 * (2 * (SCLKFrequencyDivider + 1)));
132 }
133
134 //
135 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
136 //
137 void DACWriteByte(uint32 offset, uint8 data)
138 {
139 //      WriteLog("DAC: Writing %02X at %08X\n", data, offset);
140 }
141
142 void DACWriteWord(uint32 offset, uint16 data)
143 {
144         if (offset == LTXD + 2)
145         {
146                 if (LeftFIFOTailPtr + 2 != LeftFIFOHeadPtr)
147                 {
148                         SDL_LockAudio();                                                // Is it necessary to do this? Mebbe.
149                         // We use a circular buffer 'cause it's easy. Note that the callback function
150                         // takes care of dumping audio to the soundcard...!
151                         LeftFIFOTailPtr = (LeftFIFOTailPtr + 2) % BUFFER_SIZE;
152                         DACBuffer[LeftFIFOTailPtr] = data;
153 // Aaron's code does this, but I don't know why...
154 //Flipping this bit makes the audio MUCH louder. Need to look at the amplitude of the
155 //waveform to see if any massaging is needed here...
156 //                      DACBuffer[LeftFIFOTailPtr] = data ^ 0x8000;
157                         SDL_UnlockAudio();
158                 }
159                 else
160                         WriteLog("DAC: Ran into FIFO's left tail pointer!\n");
161         }
162         else if (offset == RTXD + 2)
163         {
164                 if (RightFIFOTailPtr + 2 != RightFIFOHeadPtr)
165                 {
166                         SDL_LockAudio();
167                         RightFIFOTailPtr = (RightFIFOTailPtr + 2) % BUFFER_SIZE;
168                         DACBuffer[RightFIFOTailPtr] = data;
169 // Aaron's code does this, but I don't know why...
170 //                      DACBuffer[RightFIFOTailPtr] = data ^ 0x8000;
171                         SDL_UnlockAudio();
172                 }
173                 else
174                         WriteLog("DAC: Ran into FIFO's right tail pointer!\n");
175         }
176         else if (offset == SCLK + 2)                                    // Sample rate
177         {
178                 if ((uint8)data != SCLKFrequencyDivider)
179                 {
180 WriteLog("DAC: Changing sample rate!\n");
181                         SDL_CloseAudio();
182                         SCLKFrequencyDivider = (uint8)data;
183                         desired.freq = GetCalculatedFrequency();// SDL will do conversion on the fly, if it can't get the exact rate. Nice!
184
185                         if (SDL_OpenAudio(&desired, NULL) < 0)  // NULL means SDL guarantees what we want
186                         {
187                                 WriteLog("DAC: Failed to initialize SDL sound. Shutting down!\n");
188                                 log_done();
189                                 exit(1);
190                         }
191
192                         DACReset();
193                         SDL_PauseAudio(false);                                  // Start playback!
194                 }
195         }
196         else if (offset == SMODE + 2)
197         {
198                 serialMode = data;
199                 WriteLog("DAC: Writing to SMODE. Bits: %s%s%s%s%s%s\n",
200                         (data & 0x01 ? "INTERNAL " : ""), (data & 0x02 ? "MODE " : ""),
201                         (data & 0x04 ? "WSEN " : ""), (data & 0x08 ? "RISING " : ""),
202                         (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : ""));
203         }
204 }
205
206 //
207 // LRXD/RRXD/SSTAT ($F1A148/4C/50)
208 //
209 uint8 DACReadByte(uint32 offset)
210 {
211 //      WriteLog("DAC: Reading byte from %08X\n", offset);
212         return 0xFF;
213 }
214
215 uint16 DACReadWord(uint32 offset)
216 {
217 //      WriteLog("DAC: Reading word from %08X\n", offset);
218         return 0xFFFF;
219 }