src/audio/mint/SDL_mintaudio_xbios.c
author Patrice Mandin <patmandin@gmail.com>
Sat, 03 Sep 2011 09:52:45 +0200
branchSDL-1.2
changeset 5867 94e0116059f4
parent 5653 9a7d629b403a
child 6137 4720145f848b
permissions -rw-r--r--
Remove unused variables
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     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.
     9 
    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.
    14 
    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
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /*
    25 	MiNT audio driver
    26 	using XBIOS functions (Falcon)
    27 
    28 	Patrice Mandin, Didier Méquignon
    29 */
    30 
    31 #include <unistd.h>
    32 #include <support.h>
    33 
    34 /* Mint includes */
    35 #include <mint/osbind.h>
    36 #include <mint/falcon.h>
    37 #include <mint/cookie.h>
    38 
    39 #include "SDL_audio.h"
    40 #include "../SDL_audio_c.h"
    41 #include "../SDL_sysaudio.h"
    42 
    43 #include "../../video/ataricommon/SDL_atarimxalloc_c.h"
    44 
    45 #include "SDL_mintaudio.h"
    46 #include "SDL_mintaudio_dma8.h"
    47 
    48 /*--- Defines ---*/
    49 
    50 #define MINT_AUDIO_DRIVER_NAME "mint_xbios"
    51 
    52 /* Debug print info */
    53 #define DEBUG_NAME "audio:xbios: "
    54 #if 0
    55 #define DEBUG_PRINT(what) \
    56 	{ \
    57 		printf what; \
    58 	}
    59 #else
    60 #define DEBUG_PRINT(what)
    61 #endif
    62 
    63 /*--- Static variables ---*/
    64 
    65 static long cookie_snd;
    66 
    67 /*--- Audio driver functions ---*/
    68 
    69 static void Mint_CloseAudio(_THIS);
    70 static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec);
    71 static void Mint_LockAudio(_THIS);
    72 static void Mint_UnlockAudio(_THIS);
    73 
    74 /* To check/init hardware audio */
    75 static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
    76 static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
    77 
    78 /*--- Audio driver bootstrap functions ---*/
    79 
    80 static int Audio_Available(void)
    81 {
    82 /*	unsigned long dummy;*/
    83 	const char *envr = SDL_getenv("SDL_AUDIODRIVER");
    84 
    85 	/*SDL_MintAudio_mint_present = (Getcookie(C_MiNT, &dummy) == C_FOUND);*/
    86 	SDL_MintAudio_mint_present = SDL_FALSE;
    87 
    88 	/* We can't use XBIOS in interrupt with Magic, don't know about thread */
    89 	/*if (Getcookie(C_MagX, &dummy) == C_FOUND) {
    90 		return(0);
    91 	}*/
    92 
    93 	/* Check if user asked a different audio driver */
    94 	if ((envr) && (SDL_strcmp(envr, MINT_AUDIO_DRIVER_NAME)!=0)) {
    95 		DEBUG_PRINT((DEBUG_NAME "user asked a different audio driver\n"));
    96 		return(0);
    97 	}
    98 
    99 	/* Cookie _SND present ? if not, assume ST machine */
   100 	if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
   101 		cookie_snd = SND_PSG;
   102 	}
   103 
   104 	/* Check if we have 16 bits audio */
   105 	if ((cookie_snd & SND_16BIT)==0) {
   106 		DEBUG_PRINT((DEBUG_NAME "no 16 bits sound\n"));
   107 		return(0);
   108 	}
   109 
   110 	/* Check if audio is lockable */
   111 	if (Locksnd()!=1) {
   112 		DEBUG_PRINT((DEBUG_NAME "audio locked by other application\n"));
   113 		return(0);
   114 	}
   115 
   116 	Unlocksnd();
   117 
   118 	DEBUG_PRINT((DEBUG_NAME "XBIOS audio available!\n"));
   119 	return(1);
   120 }
   121 
   122 static void Audio_DeleteDevice(SDL_AudioDevice *device)
   123 {
   124     SDL_free(device->hidden);
   125     SDL_free(device);
   126 }
   127 
   128 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
   129 {
   130 	SDL_AudioDevice *this;
   131 
   132 	/* Initialize all variables that we clean on shutdown */
   133 	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
   134     if ( this ) {
   135         SDL_memset(this, 0, (sizeof *this));
   136         this->hidden = (struct SDL_PrivateAudioData *)
   137                 SDL_malloc((sizeof *this->hidden));
   138     }
   139     if ( (this == NULL) || (this->hidden == NULL) ) {
   140         SDL_OutOfMemory();
   141         if ( this ) {
   142             SDL_free(this);
   143         }
   144         return(0);
   145     }
   146     SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   147 
   148     /* Set the function pointers */
   149     this->OpenAudio   = Mint_OpenAudio;
   150     this->CloseAudio  = Mint_CloseAudio;
   151     this->LockAudio   = Mint_LockAudio;
   152     this->UnlockAudio = Mint_UnlockAudio;
   153     this->free        = Audio_DeleteDevice;
   154 
   155     return this;
   156 }
   157 
   158 AudioBootStrap MINTAUDIO_XBIOS_bootstrap = {
   159 	MINT_AUDIO_DRIVER_NAME, "MiNT XBIOS audio driver",
   160 	Audio_Available, Audio_CreateDevice
   161 };
   162 
   163 static void Mint_LockAudio(_THIS)
   164 {
   165 	/* Stop replay */
   166 	Buffoper(0);
   167 }
   168 
   169 static void Mint_UnlockAudio(_THIS)
   170 {
   171 	/* Restart replay */
   172 	Buffoper(SB_PLA_ENA|SB_PLA_RPT);
   173 }
   174 
   175 static void Mint_CloseAudio(_THIS)
   176 {
   177 	/* Stop replay */
   178 	SDL_MintAudio_WaitThread();
   179 	Buffoper(0);
   180 
   181 	if (!SDL_MintAudio_mint_present) {
   182 		/* Uninstall interrupt */
   183 		Jdisint(MFP_DMASOUND);
   184 	}
   185 
   186 	/* Wait if currently playing sound */
   187 	while (SDL_MintAudio_mutex != 0) {
   188 	}
   189 
   190 	/* Clear buffers */
   191 	if (SDL_MintAudio_audiobuf[0]) {
   192 		Mfree(SDL_MintAudio_audiobuf[0]);
   193 		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
   194 	}
   195 
   196 	/* Unlock sound system */
   197 	Unlocksnd();
   198 }
   199 
   200 /* Falcon XBIOS implementation of Devconnect() is buggy with external clock */
   201 static void Devconnect2(int src, int dst, int sclk, int pre)
   202 {		
   203 	static const unsigned short MASK1[3] = { 0, 0x6000, 0 };
   204 	static const unsigned short MASK2[4] = { 0xFFF0, 0xFF8F, 0xF0FF, 0x0FFF };
   205 	static const unsigned short INDEX1[4] = {  1, 3, 5, 7 };
   206 	static const unsigned short INDEX2[4] = {  0, 2, 4, 6 };
   207 	unsigned short sync_div,dev_ctrl,dest_ctrl;
   208 	void *oldstack;
   209 
   210 	if (dst==0) {
   211 		return;
   212 	}
   213 
   214 	oldstack=(void *)Super(0);
   215 
   216 	dev_ctrl = DMAAUDIO_IO.dev_ctrl;
   217 	dest_ctrl = DMAAUDIO_IO.dest_ctrl;
   218 	dev_ctrl &= MASK2[src];
   219 
   220 	if (src==ADC) {
   221 		dev_ctrl |= MASK1[sclk];
   222 	} else {
   223 		dev_ctrl |= (INDEX1[sclk] << (src<<4));
   224 	}
   225 
   226 	if (dst & DMAREC) {		
   227 		dest_ctrl &= 0xFFF0;
   228 		dest_ctrl |= INDEX1[src];
   229 	}
   230 
   231 	if (dst & DSPRECV) {		
   232 		dest_ctrl &= 0xFF8F;
   233 		dest_ctrl |= (INDEX1[src]<<4); 
   234 	}
   235 
   236 	if (dst & EXTOUT) {		
   237 		dest_ctrl &= 0xF0FF;
   238 		dest_ctrl |= (INDEX1[src]<<8); 
   239 	}
   240 
   241 	if (dst & DAC) {		
   242 		dev_ctrl &= 0x0FFF;
   243 		dev_ctrl |= MASK1[sclk]; 
   244 		dest_ctrl &=  0x0FFF;
   245 		dest_ctrl |= (INDEX2[src]<<12); 
   246 	}
   247 
   248 	sync_div = DMAAUDIO_IO.sync_div;
   249 	if (sclk==CLKEXT) {
   250 		pre<<=8;
   251 		sync_div &= 0xF0FF;
   252 	} else {
   253 		sync_div &= 0xFFF0;
   254 	}
   255 	sync_div |= pre;
   256 
   257 	DMAAUDIO_IO.dev_ctrl = dev_ctrl;
   258 	DMAAUDIO_IO.dest_ctrl = dest_ctrl;
   259 	DMAAUDIO_IO.sync_div = sync_div;
   260 
   261 	Super(oldstack);
   262 }
   263 
   264 static void Mint_CheckExternalClock(_THIS)
   265 {
   266 #define SIZE_BUF_CLOCK_MEASURE (44100/10)
   267 
   268 	char *buffer;
   269 	int i, j;
   270 
   271 	/* DSP present with its GPIO port ? */
   272 	if ((cookie_snd & SND_DSP)==0) {
   273 		return;
   274 	}
   275 
   276 	buffer = Atari_SysMalloc(SIZE_BUF_CLOCK_MEASURE, MX_STRAM);
   277 	if (buffer==NULL) {
   278 		DEBUG_PRINT((DEBUG_NAME "Not enough memory for the measure\n"));
   279 		return;
   280 	}
   281 	SDL_memset(buffer, 0, SIZE_BUF_CLOCK_MEASURE);
   282 
   283 	Buffoper(0);
   284 	Settracks(0,0);
   285 	Setmontracks(0);
   286 	Setmode(MONO8);
   287 	Jdisint(MFP_TIMERA);
   288 
   289 	for (i=0; i<2; i++) {
   290 		Gpio(GPIO_SET,7);      /* DSP port gpio outputs */
   291 		Gpio(GPIO_WRITE,2+i);  /* 22.5792/24.576 MHz for 44.1/48KHz */
   292 		Devconnect2(DMAPLAY, DAC, CLKEXT, CLK50K);  /* Matrix and clock source */
   293 		Setbuffer(0, buffer, buffer + SIZE_BUF_CLOCK_MEASURE);		           /* Set buffer */
   294 		Xbtimer(XB_TIMERA, 5, 38, SDL_MintAudio_XbiosInterruptMeasureClock); /* delay mode timer A, prediv /64, 1KHz */
   295 		Jenabint(MFP_TIMERA);
   296 		SDL_MintAudio_clocktics = 0;
   297 		Buffoper(SB_PLA_ENA);
   298 		usleep(110000);
   299 
   300 		if((Buffoper(-1) & 1)==0) {
   301 			if (SDL_MintAudio_clocktics) {
   302 				unsigned long khz;
   303 
   304 				khz = ((SIZE_BUF_CLOCK_MEASURE/SDL_MintAudio_clocktics) +1) & 0xFFFFFFFE;
   305 				DEBUG_PRINT((DEBUG_NAME "measure %d: freq=%lu KHz\n", i+1, khz));
   306 
   307 				if(khz==44) {
   308 					for (j=1; j<4; j++) {
   309 						SDL_MintAudio_AddFrequency(this, MASTERCLOCK_44K/(MASTERPREDIV_FALCON*(1<<j)), MASTERCLOCK_44K, (1<<j)-1, 2+i);
   310 					}
   311 				} else if (khz==48) {
   312 					for (j=1; j<4; j++) {
   313 						SDL_MintAudio_AddFrequency(this, MASTERCLOCK_48K/(MASTERPREDIV_FALCON*(1<<j)), MASTERCLOCK_48K, (1<<j)-1, 2+i);
   314 					}
   315 				}
   316 			} else {
   317 				DEBUG_PRINT((DEBUG_NAME "No measure\n"));
   318 			}
   319 		} else {
   320 			DEBUG_PRINT((DEBUG_NAME "No SDMA clock\n"));
   321 		}
   322 
   323 		Buffoper(0);             /* stop */
   324 		Jdisint(MFP_TIMERA);     /* Uninstall interrupt */
   325 	}
   326 
   327 	Mfree(buffer);
   328 }
   329 
   330 static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
   331 {
   332 	int i;
   333 
   334 	DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",spec->format & 0x00ff));
   335 	DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
   336 	DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
   337 	DEBUG_PRINT(("channels=%d, ", spec->channels));
   338 	DEBUG_PRINT(("freq=%d\n", spec->freq));
   339 
   340     if (spec->channels > 2) {
   341         spec->channels = 2;  /* no more than stereo! */
   342     }
   343 
   344 	spec->format |= 0x8000;	/* Audio is always signed */
   345 	if ((spec->format & 0x00ff)==16) {
   346 		spec->format |= 0x1000;	/* Audio is always big endian */
   347 		spec->channels=2;	/* 16 bits always stereo */
   348 	}
   349 
   350 	MINTAUDIO_freqcount=0;
   351 
   352 	/* Add external clocks if present */
   353 	Mint_CheckExternalClock(this);
   354 
   355 	/* Standard clocks */
   356 	for (i=1;i<12;i++) {
   357 		/* Remove unusable Falcon codec predivisors */
   358 		if ((i==6) || (i==8) || (i==10)) {
   359 			continue;
   360 		}
   361 		SDL_MintAudio_AddFrequency(this, MASTERCLOCK_FALCON1/(MASTERPREDIV_FALCON*(i+1)), MASTERCLOCK_FALCON1, i, -1);
   362 	}
   363 
   364 #if 1
   365 	for (i=0; i<MINTAUDIO_freqcount; i++) {
   366 		DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
   367 			i, MINTAUDIO_frequencies[i].frequency, MINTAUDIO_frequencies[i].masterclock,
   368 			MINTAUDIO_frequencies[i].predivisor
   369 		));
   370 	}
   371 #endif
   372 
   373 	MINTAUDIO_numfreq=SDL_MintAudio_SearchFrequency(this, spec->freq);
   374 	spec->freq=MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
   375 
   376 	DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",spec->format & 0x00ff));
   377 	DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
   378 	DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x1000)!=0)));
   379 	DEBUG_PRINT(("channels=%d, ", spec->channels));
   380 	DEBUG_PRINT(("freq=%d\n", spec->freq));
   381 
   382 	return 0;
   383 }
   384 
   385 static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
   386 {
   387 	int channels_mode, prediv;
   388 	void *buffer;
   389 
   390 	/* Stop currently playing sound */
   391 	SDL_MintAudio_quit_thread = SDL_FALSE;
   392 	SDL_MintAudio_thread_finished = SDL_TRUE;
   393 	SDL_MintAudio_WaitThread();
   394 	Buffoper(0);
   395 
   396 	/* Set replay tracks */
   397 	Settracks(0,0);
   398 	Setmontracks(0);
   399 
   400 	/* Select replay format */
   401 	channels_mode=STEREO16;
   402 	switch (spec->format & 0xff) {
   403 		case 8:
   404 			if (spec->channels==2) {
   405 				channels_mode=STEREO8;
   406 			} else {
   407 				channels_mode=MONO8;
   408 			}
   409 			break;
   410 	}
   411 	if (Setmode(channels_mode)<0) {
   412 		DEBUG_PRINT((DEBUG_NAME "Setmode() failed\n"));
   413 	}
   414 
   415 	prediv = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
   416 	if (MINTAUDIO_frequencies[MINTAUDIO_numfreq].gpio_bits != -1) {
   417 		Gpio(GPIO_SET,7);		/* DSP port gpio outputs */
   418 		Gpio(GPIO_WRITE, MINTAUDIO_frequencies[MINTAUDIO_numfreq].gpio_bits);
   419 		Devconnect2(DMAPLAY, DAC|EXTOUT, CLKEXT, prediv);
   420 	} else {
   421 		Devconnect2(DMAPLAY, DAC, CLK25M, prediv);
   422 	}
   423 
   424 	/* Set buffer */
   425 	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
   426 	if (Setbuffer(0, buffer, buffer + spec->size)<0) {
   427 		DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
   428 	}
   429 	
   430 	if (SDL_MintAudio_mint_present) {
   431 		SDL_MintAudio_thread_pid = tfork(SDL_MintAudio_Thread, 0);
   432 	} else {
   433 		/* Install interrupt */
   434 		Jdisint(MFP_DMASOUND);
   435 		/*Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt);*/
   436 		Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_Dma8Interrupt);
   437 		Jenabint(MFP_DMASOUND);
   438 
   439 		if (Setinterrupt(SI_TIMERA, SI_PLAY)<0) {
   440 			DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
   441 		}
   442 	}
   443 
   444 	/* Go */
   445 	Buffoper(SB_PLA_ENA|SB_PLA_RPT);
   446 	DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
   447 }
   448 
   449 static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec)
   450 {
   451 	/* Lock sound system */
   452 	if (Locksnd()!=1) {
   453    	    SDL_SetError("Mint_OpenAudio: Audio system already in use");
   454         return(-1);
   455 	}
   456 
   457 	SDL_MintAudio_device = this;
   458 
   459 	/* Check audio capabilities */
   460 	if (Mint_CheckAudio(this, spec)==-1) {
   461 		return -1;
   462 	}
   463 
   464 	SDL_CalculateAudioSpec(spec);
   465 
   466 	/* Allocate memory for audio buffers in DMA-able RAM */
   467 	DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
   468 
   469 	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
   470 	if (SDL_MintAudio_audiobuf[0]==NULL) {
   471 		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
   472 		return (-1);
   473 	}
   474 	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
   475 	SDL_MintAudio_numbuf=0;
   476 	SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
   477 	SDL_MintAudio_audiosize = spec->size;
   478 	SDL_MintAudio_mutex = 0;
   479 
   480 	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
   481 	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
   482 
   483 	SDL_MintAudio_CheckFpu();
   484 
   485 	/* Setup audio hardware */
   486 	Mint_InitAudio(this, spec);
   487 
   488     return(1);	/* We don't use SDL threaded audio */
   489 }