]> Shamusworld >> Repos - virtualjaguar/blob - src/dac.cpp
b7a781091ebea15cba7f346f0b263b3e838e5280
[virtualjaguar] / src / dac.cpp
1 //
2 // DAC (really, Synchronous Serial Interface) Handler
3 //
4 // Originally by David Raingeard
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Rewritten by James Hammons
7 // (C) 2010 Underground Software
8 //
9 // JLH = James Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -------------------------------------------------------------
13 // JLH  01/16/2010  Created this log ;-)
14 // JLH  04/30/2012  Changed SDL audio handler to run JERRY
15 //
16
17 // Need to set up defaults that the BIOS sets for the SSI here in DACInit()... !!! FIX !!!
18 // or something like that... Seems like it already does, but it doesn't seem to
19 // work correctly...! Perhaps just need to set up SSI stuff so BUTCH doesn't get
20 // confused...
21
22 // After testing on a real Jaguar, it seems clear that the I2S interrupt drives
23 // the audio subsystem. So while you can drive the audio at a *slower* rate than
24 // set by SCLK, you can't drive it any *faster*. Also note, that if the I2S
25 // interrupt is not enabled/running on the DSP, then there is no audio. Also,
26 // audio can be muted by clearing bit 8 of JOYSTICK (JOY1).
27 //
28 // Approach: We can run the DSP in the host system's audio IRQ, by running the
29 // DSP for the alloted time (depending on the host buffer size & sample rate)
30 // by simply reading the L/R_I2S (L/RTXD) registers at regular intervals. We
31 // would also have to time the I2S/TIMER0/TIMER1 interrupts in the DSP as well.
32 // This way, we can run the host audio IRQ at, say, 48 KHz and not have to care
33 // so much about SCLK and running a separate buffer and all the attendant
34 // garbage that comes with that awful approach.
35 //
36 // There would still be potential gotchas, as the SCLK can theoretically drive
37 // the I2S at 26590906 / 2 (for SCLK == 0) = 13.3 MHz which corresponds to an
38 // audio rate 416 KHz (dividing the I2S rate by 32, for 16-bit stereo). It
39 // seems doubtful that anything useful could come of such a high rate, and we
40 // can probably safely ignore any such ridiculously high audio rates. It won't
41 // sound the same as on a real Jaguar, but who cares? :-)
42
43 #include "dac.h"
44
45 #include "SDL.h"
46 #include "cdrom.h"
47 #include "dsp.h"
48 #include "event.h"
49 #include "jerry.h"
50 #include "jaguar.h"
51 #include "log.h"
52 #include "m68000/m68kinterface.h"
53 //#include "memory.h"
54 #include "settings.h"
55
56
57 //#define DEBUG_DAC
58
59 #define BUFFER_SIZE                     0x10000                         // Make the DAC buffers 64K x 16 bits
60 #define DAC_AUDIO_RATE          48000                           // Set the audio rate to 48 KHz
61
62 // Jaguar memory locations
63
64 #define LTXD                    0xF1A148
65 #define RTXD                    0xF1A14C
66 #define LRXD                    0xF1A148
67 #define RRXD                    0xF1A14C
68 #define SCLK                    0xF1A150
69 #define SMODE                   0xF1A154
70
71 // Global variables
72
73 // These are defined in memory.h/cpp
74 //uint16_t lrxd, rrxd;                                                  // I2S ports (into Jaguar)
75
76 // Local variables
77
78 static SDL_AudioSpec desired;
79 static bool SDLSoundInitialized;
80 static uint8_t SCLKFrequencyDivider = 19;                       // Default is roughly 22 KHz (20774 Hz in NTSC mode)
81 /*static*/ uint16_t serialMode = 0;
82
83 // Private function prototypes
84
85 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length);
86 void DSPSampleCallback(void);
87
88
89 //
90 // Initialize the SDL sound system
91 //
92 void DACInit(void)
93 {
94         SDLSoundInitialized = false;
95
96 //      if (!vjs.audioEnabled)
97         if (!vjs.DSPEnabled)
98         {
99                 WriteLog("DAC: DSP/host audio playback disabled.\n");
100                 return;
101         }
102
103         desired.freq = DAC_AUDIO_RATE;
104         desired.format = AUDIO_S16SYS;
105         desired.channels = 2;
106         desired.samples = 2048;                                         // 2K buffer = audio delay of 42.67 ms (@ 48 KHz)
107         desired.callback = SDLSoundCallback;
108
109         if (SDL_OpenAudio(&desired, NULL) < 0)          // NULL means SDL guarantees what we want
110                 WriteLog("DAC: Failed to initialize SDL sound...\n");
111         else
112         {
113                 SDLSoundInitialized = true;
114                 DACReset();
115                 SDL_PauseAudio(false);                                  // Start playback!
116                 WriteLog("DAC: Successfully initialized. Sample rate: %u\n", desired.freq);
117         }
118
119         ltxd = lrxd = desired.silence;
120
121         uint32_t riscClockRate = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
122         uint32_t cyclesPerSample = riscClockRate / DAC_AUDIO_RATE;
123         WriteLog("DAC: RISC clock = %u, cyclesPerSample = %u\n", riscClockRate, cyclesPerSample);
124 }
125
126
127 //
128 // Reset the sound buffer FIFOs
129 //
130 void DACReset(void)
131 {
132 //      LeftFIFOHeadPtr = LeftFIFOTailPtr = 0, RightFIFOHeadPtr = RightFIFOTailPtr = 1;
133         ltxd = lrxd = desired.silence;
134 }
135
136
137 //
138 // Pause/unpause the SDL audio thread
139 //
140 void DACPauseAudioThread(bool state/*= true*/)
141 {
142                 SDL_PauseAudio(state);
143 }
144
145
146 //
147 // Close down the SDL sound subsystem
148 //
149 void DACDone(void)
150 {
151         if (SDLSoundInitialized)
152         {
153                 SDL_PauseAudio(true);
154                 SDL_CloseAudio();
155         }
156
157         WriteLog("DAC: Done.\n");
158 }
159
160
161 // Approach: Run the DSP for however many cycles needed to correspond to whatever sample rate
162 // we've set the audio to run at. So, e.g., if we run it at 48 KHz, then we would run the DSP
163 // for however much time it takes to fill the buffer. So with a 2K buffer, this would correspond
164 // to running the DSP for 0.042666... seconds. At 26590906 Hz, this would correspond to
165 // running the DSP for 1134545 cycles. You would then sample the L/RTXD registers every
166 // 1134545 / 2048 = 554 cycles to fill the buffer. You would also have to manage interrupt
167 // timing as well (generating them at the proper times), but that shouldn't be too difficult...
168 // If the DSP isn't running, then fill the buffer with L/RTXD and exit.
169
170 //
171 // SDL callback routine to fill audio buffer
172 //
173 // Note: The samples are packed in the buffer in 16 bit left/16 bit right pairs.
174 //       Also, length is the length of the buffer in BYTES
175 //
176 static Uint8 * sampleBuffer;
177 static int bufferIndex = 0;
178 static int numberOfSamples = 0;
179 static bool bufferDone = false;
180 void SDLSoundCallback(void * userdata, Uint8 * buffer, int length)
181 {
182         // 1st, check to see if the DSP is running. If not, fill the buffer with L/RXTD and exit.
183
184         if (!DSPIsRunning())
185         {
186                 for(int i=0; i<(length/2); i+=2)
187                 {
188                         ((uint16_t *)buffer)[i + 0] = ltxd;
189                         ((uint16_t *)buffer)[i + 1] = rtxd;
190                 }
191
192                 return;
193         }
194
195         // The length of time we're dealing with here is 1/48000 s, so we multiply this
196         // by the number of cycles per second to get the number of cycles for one sample.
197         uint32_t riscClockRate = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
198         uint32_t cyclesPerSample = riscClockRate / DAC_AUDIO_RATE;
199         // This is the length of time
200 //      timePerSample = (1000000.0 / (double)riscClockRate) * ();
201
202         // Now, run the DSP for that length of time for each sample we need to make
203
204         bufferIndex = 0;
205         sampleBuffer = buffer;
206         numberOfSamples = length / 2;
207         bufferDone = false;
208
209         SetCallbackTime(DSPSampleCallback, 1000000.0 / (double)DAC_AUDIO_RATE, EVENT_JERRY);
210
211         // These timings are tied to NTSC, need to fix that in event.cpp/h!
212         do
213         {
214                 double timeToNextEvent = GetTimeToNextEvent(EVENT_JERRY);
215
216                 if (vjs.DSPEnabled)
217                 {
218                         if (vjs.usePipelinedDSP)
219                                 DSPExecP2(USEC_TO_RISC_CYCLES(timeToNextEvent));
220                         else
221                                 DSPExec(USEC_TO_RISC_CYCLES(timeToNextEvent));
222                 }
223
224                 HandleNextEvent(EVENT_JERRY);
225         }
226         while (!bufferDone);
227 }
228
229
230 void DSPSampleCallback(void)
231 {
232         ((uint16_t *)sampleBuffer)[bufferIndex + 0] = ltxd;
233         ((uint16_t *)sampleBuffer)[bufferIndex + 1] = rtxd;
234         bufferIndex += 2;
235
236         if (bufferIndex == numberOfSamples)
237         {
238                 bufferDone = true;
239                 return;
240         }
241
242         SetCallbackTime(DSPSampleCallback, 1000000.0 / (double)DAC_AUDIO_RATE, EVENT_JERRY);
243 }
244
245
246 #if 0
247 //
248 // Calculate the frequency of SCLK * 32 using the divider
249 //
250 int GetCalculatedFrequency(void)
251 {
252         int systemClockFrequency = (vjs.hardwareTypeNTSC ? RISC_CLOCK_RATE_NTSC : RISC_CLOCK_RATE_PAL);
253
254         // We divide by 32 here in order to find the frequency of 32 SCLKs in a row (transferring
255         // 16 bits of left data + 16 bits of right data = 32 bits, 1 SCLK = 1 bit transferred).
256         return systemClockFrequency / (32 * (2 * (SCLKFrequencyDivider + 1)));
257 }
258 #endif
259
260
261 //
262 // LTXD/RTXD/SCLK/SMODE ($F1A148/4C/50/54)
263 //
264 void DACWriteByte(uint32_t offset, uint8_t data, uint32_t who/*= UNKNOWN*/)
265 {
266         WriteLog("DAC: %s writing BYTE %02X at %08X\n", whoName[who], data, offset);
267         if (offset == SCLK + 3)
268                 DACWriteWord(offset - 3, (uint16_t)data);
269 }
270
271
272 void DACWriteWord(uint32_t offset, uint16_t data, uint32_t who/*= UNKNOWN*/)
273 {
274         if (offset == LTXD + 2)
275         {
276                 ltxd = data;
277         }
278         else if (offset == RTXD + 2)
279         {
280                 rtxd = data;
281         }
282         else if (offset == SCLK + 2)                                    // Sample rate
283         {
284                 WriteLog("DAC: Writing %u to SCLK...\n", data);
285
286                 if ((uint8_t)data != SCLKFrequencyDivider)
287                         SCLKFrequencyDivider = (uint8_t)data;
288         }
289         else if (offset == SMODE + 2)
290         {
291                 serialMode = data;
292                 WriteLog("DAC: %s writing to SMODE. Bits: %s%s%s%s%s%s [68K PC=%08X]\n", whoName[who],
293                         (data & 0x01 ? "INTERNAL " : ""), (data & 0x02 ? "MODE " : ""),
294                         (data & 0x04 ? "WSEN " : ""), (data & 0x08 ? "RISING " : ""),
295                         (data & 0x10 ? "FALLING " : ""), (data & 0x20 ? "EVERYWORD" : ""),
296                         m68k_get_reg(NULL, M68K_REG_PC));
297         }
298 }
299
300
301 //
302 // LRXD/RRXD/SSTAT ($F1A148/4C/50)
303 //
304 uint8_t DACReadByte(uint32_t offset, uint32_t who/*= UNKNOWN*/)
305 {
306 //      WriteLog("DAC: %s reading byte from %08X\n", whoName[who], offset);
307         return 0xFF;
308 }
309
310
311 //static uint16_t fakeWord = 0;
312 uint16_t DACReadWord(uint32_t offset, uint32_t who/*= UNKNOWN*/)
313 {
314 //      WriteLog("DAC: %s reading word from %08X\n", whoName[who], offset);
315 //      return 0xFFFF;
316 //      WriteLog("DAC: %s reading WORD %04X from %08X\n", whoName[who], fakeWord, offset);
317 //      return fakeWord++;
318 //NOTE: This only works if a bunch of things are set in BUTCH which we currently don't
319 //      check for. !!! FIX !!!
320 // Partially fixed: We check for I2SCNTRL in the JERRY I2S routine...
321 //      return GetWordFromButchSSI(offset, who);
322         if (offset == LRXD || offset == RRXD)
323                 return 0x0000;
324         else if (offset == LRXD + 2)
325                 return lrxd;
326         else if (offset == RRXD + 2)
327                 return rrxd;
328
329         return 0xFFFF;  // May need SSTAT as well... (but may be a Jaguar II only feature)
330 }
331