static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
{
	int channels_mode, prediv;
	void *buffer;

	
	SDL_MintAudio_quit_thread = SDL_FALSE;
	SDL_MintAudio_thread_finished = SDL_TRUE;
	SDL_MintAudio_WaitThread();
	Buffoper(0);

	
	Settracks(0,0);
	Setmontracks(0);

	
	channels_mode=STEREO16;
	switch (spec->format & 0xff) {
		case 8:
			if (spec->channels==2) {
				channels_mode=STEREO8;
			} else {
				channels_mode=MONO8;
			}
			break;
	}
	if (Setmode(channels_mode)<0) {
		DEBUG_PRINT((DEBUG_NAME "Setmode() failed\n"));
	}

	prediv = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
	if (MINTAUDIO_frequencies[MINTAUDIO_numfreq].gpio_bits != -1) {
		Gpio(GPIO_SET,7);		
		Gpio(GPIO_WRITE, MINTAUDIO_frequencies[MINTAUDIO_numfreq].gpio_bits);
		Devconnect2(DMAPLAY, DAC|EXTOUT, CLKEXT, prediv);
	} else {
		Devconnect2(DMAPLAY, DAC, CLK25M, prediv);
	}

	
	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
	if (Setbuffer(0, buffer, buffer + spec->size)<0) {
		DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
	}
	
	if (SDL_MintAudio_mint_present) {
		SDL_MintAudio_thread_pid = tfork(SDL_MintAudio_Thread, 0);
	} else {
		
		Jdisint(MFP_DMASOUND);
		
		Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_Dma8Interrupt);
		Jenabint(MFP_DMASOUND);

		if (Setinterrupt(SI_TIMERA, SI_PLAY)<0) {
			DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
		}
	}

	
	Buffoper(SB_PLA_ENA|SB_PLA_RPT);
	DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
}
static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
{
	int channels_mode, dmaclock, prediv;
	void *buffer;

	/* Stop currently playing sound */
	SDL_MintAudio_quit_thread = SDL_FALSE;
	SDL_MintAudio_thread_finished = SDL_TRUE;
	SDL_MintAudio_WaitThread();
	Buffoper(0);

	/* Set replay tracks */
	Settracks(0,0);
	Setmontracks(0);

	/* Select replay format */
	channels_mode=STEREO16;
	switch (spec->format & 0xff) {
		case 8:
			if (spec->channels==2) {
				channels_mode=STEREO8;
			} else {
				channels_mode=MONO8;
			}
			break;
	}
	if (Setmode(channels_mode)<0) {
		DEBUG_PRINT((DEBUG_NAME "Setmode() failed\n"));
	}

	dmaclock = MINTAUDIO_frequencies[MINTAUDIO_numfreq].masterclock;
	prediv = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
	if (MINTAUDIO_frequencies[MINTAUDIO_numfreq].gpio_bits != -1) {
		Gpio(GPIO_SET,7);		/* DSP port gpio outputs */
		Gpio(GPIO_WRITE, MINTAUDIO_frequencies[MINTAUDIO_numfreq].gpio_bits);
		Devconnect2(DMAPLAY, DAC|EXTOUT, CLKEXT, prediv);
	} else {
		Devconnect2(DMAPLAY, DAC, CLK25M, prediv);
	}

	/* Set buffer */
	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
	if (Setbuffer(0, buffer, buffer + spec->size)<0) {
		DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
	}
	
	if (SDL_MintAudio_mint_present) {
		SDL_MintAudio_thread_pid = tfork(SDL_MintAudio_Thread, 0);
	} else {
		/* Install interrupt */
		Jdisint(MFP_DMASOUND);
		/*Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt);*/
		Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_Dma8Interrupt);
		Jenabint(MFP_DMASOUND);

		if (Setinterrupt(SI_TIMERA, SI_PLAY)<0) {
			DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
		}
	}

	/* Go */
	Buffoper(SB_PLA_ENA|SB_PLA_RPT);
	DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
}
static void Mint_CheckExternalClock(_THIS)
{
#define SIZE_BUF_CLOCK_MEASURE (44100/10)

	char *buffer;
	int i, j;

	
	if ((cookie_snd & SND_DSP)==0) {
		return;
	}

	buffer = Atari_SysMalloc(SIZE_BUF_CLOCK_MEASURE, MX_STRAM);
	if (buffer==NULL) {
		DEBUG_PRINT((DEBUG_NAME "Not enough memory for the measure\n"));
		return;
	}
	SDL_memset(buffer, 0, SIZE_BUF_CLOCK_MEASURE);

	Buffoper(0);
	Settracks(0,0);
	Setmontracks(0);
	Setmode(MONO8);
	Jdisint(MFP_TIMERA);

	for (i=0; i<2; i++) {
		Gpio(GPIO_SET,7);      
		Gpio(GPIO_WRITE,2+i);  
		Devconnect2(DMAPLAY, DAC, CLKEXT, CLK50K);  
		Setbuffer(0, buffer, buffer + SIZE_BUF_CLOCK_MEASURE);		           
		Xbtimer(XB_TIMERA, 5, 38, SDL_MintAudio_XbiosInterruptMeasureClock); 
		Jenabint(MFP_TIMERA);
		SDL_MintAudio_clocktics = 0;
		Buffoper(SB_PLA_ENA);
		usleep(110000);

		if((Buffoper(-1) & 1)==0) {
			if (SDL_MintAudio_clocktics) {
				unsigned long khz;

				khz = ((SIZE_BUF_CLOCK_MEASURE/SDL_MintAudio_clocktics) +1) & 0xFFFFFFFE;
				DEBUG_PRINT((DEBUG_NAME "measure %d: freq=%lu KHz\n", i+1, khz));

				if(khz==44) {
					for (j=1; j<4; j++) {
						SDL_MintAudio_AddFrequency(this, MASTERCLOCK_44K/(MASTERPREDIV_FALCON*(1<<j)), MASTERCLOCK_44K, (1<<j)-1, 2+i);
					}
				} else if (khz==48) {
					for (j=1; j<4; j++) {
						SDL_MintAudio_AddFrequency(this, MASTERCLOCK_48K/(MASTERPREDIV_FALCON*(1<<j)), MASTERCLOCK_48K, (1<<j)-1, 2+i);
					}
				}
			} else {
				DEBUG_PRINT((DEBUG_NAME "No measure\n"));
			}
		} else {
			DEBUG_PRINT((DEBUG_NAME "No SDMA clock\n"));
		}

		Buffoper(0);             
		Jdisint(MFP_TIMERA);     
	}

	Mfree(buffer);
}
static void Mint_CheckExternalClock(_THIS)
{
#define SIZE_BUF_CLOCK_MEASURE (44100/10)

	unsigned long cookie_snd;
	char *buffer;
	int i, j;

	/* DSP present with its GPIO port ? */
	if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
		return;
	}
	if ((cookie_snd & SND_DSP)==0) {
		return;
	}

	buffer = Atari_SysMalloc(SIZE_BUF_CLOCK_MEASURE, MX_STRAM);
	if (buffer==NULL) {
		DEBUG_PRINT((DEBUG_NAME "Not enough memory for the measure\n"));
		return;
	}
	SDL_memset(buffer, 0, SIZE_BUF_CLOCK_MEASURE);

	Buffoper(0);
	Settracks(0,0);
	Setmontracks(0);
	Setmode(MONO8);
	Jdisint(MFP_TIMERA);

	for (i=0; i<2; i++) {
		Gpio(GPIO_SET,7);      /* DSP port gpio outputs */
		Gpio(GPIO_WRITE,2+i);  /* 22.5792/24.576 MHz for 44.1/48KHz */
		Devconnect2(DMAPLAY, DAC, CLKEXT, CLK50K);  /* Matrix and clock source */
		Setbuffer(0, buffer, buffer + SIZE_BUF_CLOCK_MEASURE);		           /* Set buffer */
		Xbtimer(XB_TIMERA, 5, 38, SDL_MintAudio_XbiosInterruptMeasureClock); /* delay mode timer A, prediv /64, 1KHz */
		Jenabint(MFP_TIMERA);
		SDL_MintAudio_clocktics = 0;
		Buffoper(SB_PLA_ENA);
		usleep(110000);

		if((Buffoper(-1) & 1)==0) {
			if (SDL_MintAudio_clocktics) {
				unsigned long khz;

				khz = ((SIZE_BUF_CLOCK_MEASURE/SDL_MintAudio_clocktics) +1) & 0xFFFFFFFE;
				DEBUG_PRINT((DEBUG_NAME "measure %d: freq=%lu KHz\n", i+1, khz));

				if(khz==44) {
					for (j=1; j<4; j++) {
						SDL_MintAudio_AddFrequency(this, MASTERCLOCK_44K/(MASTERPREDIV_FALCON*(1<<j)), MASTERCLOCK_44K, (1<<j)-1, 2+i);
					}
				} else if (khz==48) {
					for (j=1; j<4; j++) {
						SDL_MintAudio_AddFrequency(this, MASTERCLOCK_48K/(MASTERPREDIV_FALCON*(1<<j)), MASTERCLOCK_48K, (1<<j)-1, 2+i);
					}
				}
			} else {
				DEBUG_PRINT((DEBUG_NAME "No measure\n"));
			}
		} else {
			DEBUG_PRINT((DEBUG_NAME "No SDMA clock\n"));
		}

		Buffoper(0);             /* stop */
		Jdisint(MFP_TIMERA);     /* Uninstall interrupt */
	}

	Mfree(buffer);
}