2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 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 DMA 8bits (hardware access)
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_dma8.h"
47 #define MINT_AUDIO_DRIVER_NAME "mint_dma8"
49 /* Debug print info */
50 #define DEBUG_NAME "audio:dma8: "
52 #define DEBUG_PRINT(what) \
57 #define DEBUG_PRINT(what)
60 /*--- Static variables ---*/
62 static long cookie_snd, cookie_mch;
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);
74 /* Functions called in supervisor mode */
75 static void Mint_InitDma(void);
76 static void Mint_StopReplay(void);
77 static void Mint_StartReplay(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 _MCH present ? if not, assume ST machine */
92 if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
96 /* Cookie _SND present ? if not, assume ST machine */
97 if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
101 /* Check if we have 8 bits audio */
102 if ((cookie_snd & SND_8BIT)==0) {
103 DEBUG_PRINT((DEBUG_NAME "no 8 bits sound\n"));
107 /* Check if audio is lockable */
108 if (cookie_snd & SND_16BIT) {
110 DEBUG_PRINT((DEBUG_NAME "audio locked by other application\n"));
117 DEBUG_PRINT((DEBUG_NAME "8 bits audio available!\n"));
121 static void Audio_DeleteDevice(SDL_AudioDevice *device)
123 SDL_free(device->hidden);
127 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
129 SDL_AudioDevice *this;
131 /* Initialize all variables that we clean on shutdown */
132 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
134 SDL_memset(this, 0, (sizeof *this));
135 this->hidden = (struct SDL_PrivateAudioData *)
136 SDL_malloc((sizeof *this->hidden));
138 if ( (this == NULL) || (this->hidden == NULL) ) {
145 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
147 /* Set the function pointers */
148 this->OpenAudio = Mint_OpenAudio;
149 this->CloseAudio = Mint_CloseAudio;
150 this->LockAudio = Mint_LockAudio;
151 this->UnlockAudio = Mint_UnlockAudio;
152 this->free = Audio_DeleteDevice;
157 AudioBootStrap MINTAUDIO_DMA8_bootstrap = {
158 MINT_AUDIO_DRIVER_NAME, "MiNT DMA 8 bits audio driver",
159 Audio_Available, Audio_CreateDevice
162 static void Mint_LockAudio(_THIS)
164 Supexec(Mint_StopReplay);
167 static void Mint_UnlockAudio(_THIS)
169 Supexec(Mint_StartReplay);
172 static void Mint_CloseAudio(_THIS)
174 Supexec(Mint_StopReplay);
176 DEBUG_PRINT((DEBUG_NAME "closeaudio: replay stopped\n"));
178 /* Disable interrupt */
179 Jdisint(MFP_DMASOUND);
181 DEBUG_PRINT((DEBUG_NAME "closeaudio: interrupt disabled\n"));
183 /* Wait if currently playing sound */
184 while (SDL_MintAudio_mutex != 0) {
187 DEBUG_PRINT((DEBUG_NAME "closeaudio: no more interrupt running\n"));
190 if (SDL_MintAudio_audiobuf[0]) {
191 Mfree(SDL_MintAudio_audiobuf[0]);
192 SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
195 DEBUG_PRINT((DEBUG_NAME "closeaudio: buffers freed\n"));
198 static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
200 int i, masterprediv, sfreq;
201 unsigned long masterclock;
203 DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",spec->format & 0x00ff));
204 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
205 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
206 DEBUG_PRINT(("channels=%d, ", spec->channels));
207 DEBUG_PRINT(("freq=%d\n", spec->freq));
209 if (spec->channels > 2)
212 /* Check formats available */
213 spec->format = AUDIO_S8;
215 /* Calculate and select the closest frequency */
217 masterclock=MASTERCLOCK_STE;
218 masterprediv=MASTERPREDIV_STE;
219 switch(cookie_mch>>16) {
222 masterclock=MASTERCLOCK_STE;
223 masterprediv=MASTERPREDIV_STE;
227 masterclock=MASTERCLOCK_TT;
228 masterprediv=MASTERPREDIV_TT;
232 masterclock=MASTERCLOCK_FALCON1;
233 masterprediv=MASTERPREDIV_FALCON;
238 MINTAUDIO_freqcount=0;
239 for (i=sfreq;i<4;i++) {
240 SDL_MintAudio_AddFrequency(this, masterclock/(masterprediv*(1<<i)),
241 masterclock, i-sfreq, -1);
245 for (i=0; i<MINTAUDIO_freqcount; i++) {
246 DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
247 i, MINTAUDIO_frequencies[i].frequency, MINTAUDIO_frequencies[i].masterclock,
248 MINTAUDIO_frequencies[i].predivisor
253 MINTAUDIO_numfreq=SDL_MintAudio_SearchFrequency(this, spec->freq);
254 spec->freq=MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
256 DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",spec->format & 0x00ff));
257 DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
258 DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
259 DEBUG_PRINT(("channels=%d, ", spec->channels));
260 DEBUG_PRINT(("freq=%d\n", spec->freq));
265 static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec)
267 SDL_MintAudio_device = this;
269 /* Check audio capabilities */
270 if (Mint_CheckAudio(this, spec)==-1) {
274 SDL_CalculateAudioSpec(spec);
276 /* Allocate memory for audio buffers in DMA-able RAM */
277 DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
279 SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
280 if (SDL_MintAudio_audiobuf[0]==NULL) {
281 SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
284 SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
285 SDL_MintAudio_numbuf=0;
286 SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
287 SDL_MintAudio_audiosize = spec->size;
288 SDL_MintAudio_mutex = 0;
290 DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
291 DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
293 SDL_MintAudio_CheckFpu();
295 /* Set replay tracks */
296 if (cookie_snd & SND_16BIT) {
301 Supexec(Mint_InitDma);
304 Jdisint(MFP_DMASOUND);
305 Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_Dma8Interrupt);
306 Jenabint(MFP_DMASOUND);
308 if (cookie_snd & SND_16BIT) {
309 if (Setinterrupt(SI_TIMERA, SI_PLAY)<0) {
310 DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
314 Supexec(Mint_StartReplay);
316 return(1); /* We don't use threaded audio */
319 /* Functions called in supervisor mode */
321 static void Mint_InitDma(void)
323 unsigned long buffer;
325 SDL_AudioDevice *this = SDL_MintAudio_device;
330 buffer = (unsigned long) SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
331 DMAAUDIO_IO.start_high = (buffer>>16) & 255;
332 DMAAUDIO_IO.start_mid = (buffer>>8) & 255;
333 DMAAUDIO_IO.start_low = buffer & 255;
335 buffer += SDL_MintAudio_audiosize;
336 DMAAUDIO_IO.end_high = (buffer>>16) & 255;
337 DMAAUDIO_IO.end_mid = (buffer>>8) & 255;
338 DMAAUDIO_IO.end_low = buffer & 255;
340 mode = 3-MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
341 if (this->spec.channels==1) {
344 DMAAUDIO_IO.sound_ctrl = mode;
347 static void Mint_StopReplay(void)
350 DMAAUDIO_IO.control=0;
353 static void Mint_StartReplay(void)
356 DMAAUDIO_IO.control=3;