2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "SDL_config.h"
26 using XBIOS functions (GSXB compatible driver)
32 #include <mint/osbind.h>
33 #include <mint/falcon.h>
34 #include <mint/cookie.h>
36 #include "SDL_audio.h"
37 #include "../SDL_audio_c.h"
38 #include "../SDL_sysaudio.h"
40 #include "../../video/ataricommon/SDL_atarimxalloc_c.h"
42 #include "SDL_mintaudio.h"
43 #include "SDL_mintaudio_gsxb.h"
47 #define MINT_AUDIO_DRIVER_NAME "mint_gsxb"
49 /* Debug print info */
50 #define DEBUG_NAME "audio:gsxb: "
52 #define DEBUG_PRINT(what) \
57 #define DEBUG_PRINT(what)
60 /*--- Static variables ---*/
62 static unsigned long cookie_snd, cookie_gsxb;
64 /*--- Audio driver functions ---*/
66 static void Mint_CloseAudio(_THIS);
67 static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec);
68 static void Mint_LockAudio(_THIS);
69 static void Mint_UnlockAudio(_THIS);
71 /* To check/init hardware audio */
72 static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
73 static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
76 static void Mint_GsxbInterrupt(void);
77 static void Mint_GsxbNullInterrupt(void);
79 /*--- Audio driver bootstrap functions ---*/
81 static int Audio_Available(void)
83 const char *envr = SDL_getenv("SDL_AUDIODRIVER");
85 /* Check if user asked a different audio driver */
86 if ((envr) && (SDL_strcmp(envr, MINT_AUDIO_DRIVER_NAME)!=0)) {
87 DEBUG_PRINT((DEBUG_NAME "user asked a different audio driver\n"));
91 /* Cookie _SND present ? if not, assume ST machine */
92 if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
96 /* Check if we have 16 bits audio */
97 if ((cookie_snd & SND_16BIT)==0) {
98 DEBUG_PRINT((DEBUG_NAME "no 16 bits sound\n"));
102 /* Cookie GSXB present ? */
103 cookie_gsxb = (Getcookie(C_GSXB, &cookie_gsxb) == C_FOUND);
106 if (((cookie_snd & SND_GSXB)==0) || (cookie_gsxb==0)) {
107 DEBUG_PRINT((DEBUG_NAME "no GSXB audio\n"));
111 /* Check if audio is lockable */
113 DEBUG_PRINT((DEBUG_NAME "audio locked by other application\n"));
119 DEBUG_PRINT((DEBUG_NAME "GSXB audio available!\n"));
123 static void Audio_DeleteDevice(SDL_AudioDevice *device)
125 SDL_free(device->hidden);
129 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
131 SDL_AudioDevice *this;
133 /* Initialize all variables that we clean on shutdown */
134 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
136 SDL_memset(this, 0, (sizeof *this));
137 this->hidden = (struct SDL_PrivateAudioData *)
138 SDL_malloc((sizeof *this->hidden));
140 if ( (this == NULL) || (this->hidden == NULL) ) {
147 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
149 /* Set the function pointers */
150 this->OpenAudio = Mint_OpenAudio;
151 this->CloseAudio = Mint_CloseAudio;
152 this->LockAudio = Mint_LockAudio;
153 this->UnlockAudio = Mint_UnlockAudio;
154 this->free = Audio_DeleteDevice;
159 AudioBootStrap MINTAUDIO_GSXB_bootstrap = {
160 MINT_AUDIO_DRIVER_NAME, "MiNT GSXB audio driver",
161 Audio_Available, Audio_CreateDevice
164 static void Mint_LockAudio(_THIS)
170 static void Mint_UnlockAudio(_THIS)
173 Buffoper(SB_PLA_ENA|SB_PLA_RPT);
176 static void Mint_CloseAudio(_THIS)
181 /* Uninstall interrupt */
182 if (NSetinterrupt(2, SI_NONE, Mint_GsxbNullInterrupt)<0) {
183 DEBUG_PRINT((DEBUG_NAME "NSetinterrupt() failed in close\n"));
186 /* Wait if currently playing sound */
187 while (SDL_MintAudio_mutex != 0) {
191 if (SDL_MintAudio_audiobuf[0]) {
192 Mfree(SDL_MintAudio_audiobuf[0]);
193 SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
196 /* Unlock sound system */
200 static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
203 int i, resolution, format_signed, format_bigendian;
204 Uint16 test_format = SDL_FirstAudioFormat(spec->format);
205 int valid_datatype = 0;
207 resolution = spec->format & 0x00ff;
208 format_signed = ((spec->format & 0x8000)!=0);
209 format_bigendian = ((spec->format & 0x1000)!=0);
211 DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",spec->format & 0x00ff));
212 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
213 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
214 DEBUG_PRINT(("channels=%d, ", spec->channels));
215 DEBUG_PRINT(("freq=%d\n", spec->freq));
217 if (spec->channels > 2) {
218 spec->channels = 2; /* no more than stereo! */
221 while ((!valid_datatype) && (test_format)) {
222 /* Check formats available */
223 snd_format = Sndstatus(SND_QUERYFORMATS);
224 spec->format = test_format;
225 resolution = spec->format & 0xff;
226 format_signed = (spec->format & (1<<15));
227 format_bigendian = (spec->format & (1<<12));
228 switch (test_format) {
231 if (snd_format & SND_FORMAT8) {
233 snd_format = Sndstatus(SND_QUERY8BIT);
241 if (snd_format & SND_FORMAT16) {
243 snd_format = Sndstatus(SND_QUERY16BIT);
248 test_format = SDL_NextAudioFormat();
253 if (!valid_datatype) {
254 SDL_SetError("Unsupported audio format");
258 /* Check signed/unsigned format */
260 if (snd_format & SND_FORMATSIGNED) {
262 } else if (snd_format & SND_FORMATUNSIGNED) {
263 /* Give unsigned format */
264 spec->format = spec->format & (~0x8000);
267 if (snd_format & SND_FORMATUNSIGNED) {
269 } else if (snd_format & SND_FORMATSIGNED) {
270 /* Give signed format */
271 spec->format |= 0x8000;
275 if (format_bigendian) {
276 if (snd_format & SND_FORMATBIGENDIAN) {
278 } else if (snd_format & SND_FORMATLITTLEENDIAN) {
279 /* Give little endian format */
280 spec->format = spec->format & (~0x1000);
283 if (snd_format & SND_FORMATLITTLEENDIAN) {
285 } else if (snd_format & SND_FORMATBIGENDIAN) {
286 /* Give big endian format */
287 spec->format |= 0x1000;
291 /* Calculate and select the closest frequency */
292 MINTAUDIO_freqcount=0;
294 SDL_MintAudio_AddFrequency(this,
295 MASTERCLOCK_44K/(MASTERPREDIV_MILAN*(1<<i)), MASTERCLOCK_44K,
300 for (i=0; i<MINTAUDIO_freqcount; i++) {
301 DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
302 i, MINTAUDIO_frequencies[i].frequency, MINTAUDIO_frequencies[i].masterclock,
303 MINTAUDIO_frequencies[i].predivisor
308 MINTAUDIO_numfreq=SDL_MintAudio_SearchFrequency(this, spec->freq);
309 spec->freq=MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
311 DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",spec->format & 0x00ff));
312 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
313 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
314 DEBUG_PRINT(("channels=%d, ", spec->channels));
315 DEBUG_PRINT(("freq=%d\n", spec->freq));
320 static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
322 int channels_mode, prediv;
325 /* Stop currently playing sound */
328 /* Set replay tracks */
332 /* Select replay format */
333 switch (spec->format & 0xff) {
335 if (spec->channels==2) {
336 channels_mode=STEREO8;
342 if (spec->channels==2) {
343 channels_mode=STEREO16;
345 channels_mode=MONO16;
349 channels_mode=STEREO16;
352 if (Setmode(channels_mode)<0) {
353 DEBUG_PRINT((DEBUG_NAME "Setmode() failed\n"));
356 prediv = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
357 Devconnect(DMAPLAY, DAC, CLKEXT, prediv, 1);
360 buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
361 if (Setbuffer(0, buffer, buffer + spec->size)<0) {
362 DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
365 /* Install interrupt */
366 if (NSetinterrupt(2, SI_PLAY, Mint_GsxbInterrupt)<0) {
367 DEBUG_PRINT((DEBUG_NAME "NSetinterrupt() failed\n"));
371 Buffoper(SB_PLA_ENA|SB_PLA_RPT);
372 DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
375 static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec)
377 /* Lock sound system */
379 SDL_SetError("Mint_OpenAudio: Audio system already in use");
383 SDL_MintAudio_device = this;
385 /* Check audio capabilities */
386 if (Mint_CheckAudio(this, spec)==-1) {
390 SDL_CalculateAudioSpec(spec);
392 /* Allocate memory for audio buffers in DMA-able RAM */
393 DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
395 SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
396 if (SDL_MintAudio_audiobuf[0]==NULL) {
397 SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
400 SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
401 SDL_MintAudio_numbuf=0;
402 SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
403 SDL_MintAudio_audiosize = spec->size;
404 SDL_MintAudio_mutex = 0;
406 DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
407 DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
409 SDL_MintAudio_CheckFpu();
411 /* Setup audio hardware */
412 Mint_InitAudio(this, spec);
414 return(1); /* We don't use threaded audio */
417 static void Mint_GsxbInterrupt(void)
421 if (SDL_MintAudio_mutex)
424 SDL_MintAudio_mutex=1;
426 SDL_MintAudio_numbuf ^= 1;
427 SDL_MintAudio_Callback();
428 newbuf = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
429 Setbuffer(0, newbuf, newbuf + SDL_MintAudio_audiosize);
431 SDL_MintAudio_mutex=0;
434 static void Mint_GsxbNullInterrupt(void)