Use MiNT thread to update DMA pointers instead of unusable MFP interrupt
authorPatrice Mandin
Wed, 10 Aug 2005 13:29:49 +0000
changeset 111112c49532be00
parent 1110 b6fdf299a2f3
child 1112 6ded3dd929f5
Use MiNT thread to update DMA pointers instead of unusable MFP interrupt
src/audio/mint/SDL_mintaudio.c
src/audio/mint/SDL_mintaudio.h
src/audio/mint/SDL_mintaudio_mcsn.c
src/audio/mint/SDL_mintaudio_xbios.c
     1.1 --- a/src/audio/mint/SDL_mintaudio.c	Tue Aug 09 07:28:35 2005 +0000
     1.2 +++ b/src/audio/mint/SDL_mintaudio.c	Wed Aug 10 13:29:49 2005 +0000
     1.3 @@ -27,6 +27,11 @@
     1.4  */
     1.5  
     1.6  #include <string.h>
     1.7 +#include <unistd.h>
     1.8 +
     1.9 +#include <mint/osbind.h>
    1.10 +#include <mint/falcon.h>
    1.11 +#include <mint/mintbind.h>
    1.12  
    1.13  #include "SDL_types.h"
    1.14  #include "SDL_audio.h"
    1.15 @@ -44,6 +49,12 @@
    1.16  unsigned long SDL_MintAudio_clocktics;
    1.17  cookie_stfa_t	*SDL_MintAudio_stfa;
    1.18  
    1.19 +/* MiNT thread variables */
    1.20 +SDL_bool SDL_MintAudio_mint_present;
    1.21 +SDL_bool SDL_MintAudio_quit_thread;
    1.22 +SDL_bool SDL_MintAudio_thread_finished;
    1.23 +long SDL_MintAudio_thread_pid;
    1.24 +
    1.25  /* The callback function, called by each driver whenever needed */
    1.26  
    1.27  void SDL_MintAudio_Callback(void)
    1.28 @@ -129,3 +140,56 @@
    1.29  	/* Not in the array, give the latest */
    1.30  	return MINTAUDIO_freqcount-1;
    1.31  }
    1.32 +
    1.33 +/* The thread function, used under MiNT with xbios */
    1.34 +int SDL_MintAudio_Thread(long param)
    1.35 +{
    1.36 +	SndBufPtr	pointers;
    1.37 +	SDL_bool	buffers_filled[2] = {SDL_FALSE, SDL_FALSE};
    1.38 +
    1.39 +	SDL_MintAudio_thread_finished = SDL_FALSE;
    1.40 +	while (!SDL_MintAudio_quit_thread) {
    1.41 +		if (Buffptr(&pointers)!=0)
    1.42 +			continue;
    1.43 +
    1.44 +		if (( (unsigned long)pointers.play>=(unsigned long)SDL_MintAudio_audiobuf[0])
    1.45 +			&& ( (unsigned long)pointers.play<=(unsigned long)SDL_MintAudio_audiobuf[1])) 
    1.46 +		{
    1.47 +			/* DMA is reading buffer #0, setup buffer #1 if not already done */
    1.48 +			if (!buffers_filled[1]) {
    1.49 +				SDL_MintAudio_numbuf = 1;
    1.50 +				SDL_MintAudio_Callback();
    1.51 +				Setbuffer(0, SDL_MintAudio_audiobuf[1], SDL_MintAudio_audiobuf[1] + SDL_MintAudio_audiosize);
    1.52 +				buffers_filled[1]=SDL_TRUE;
    1.53 +				buffers_filled[0]=SDL_FALSE;
    1.54 +			}
    1.55 +		} else {
    1.56 +			/* DMA is reading buffer #1, setup buffer #0 if not already done */
    1.57 +			if (!buffers_filled[0]) {
    1.58 +				SDL_MintAudio_numbuf = 0;
    1.59 +				SDL_MintAudio_Callback();
    1.60 +				Setbuffer(0, SDL_MintAudio_audiobuf[0], SDL_MintAudio_audiobuf[0] + SDL_MintAudio_audiosize);
    1.61 +				buffers_filled[0]=SDL_TRUE;
    1.62 +				buffers_filled[1]=SDL_FALSE;
    1.63 +			}
    1.64 +		}
    1.65 +
    1.66 +		usleep(1000);
    1.67 +	}
    1.68 +	SDL_MintAudio_thread_finished = SDL_TRUE;
    1.69 +	return 0;
    1.70 +}
    1.71 +
    1.72 +void SDL_MintAudio_WaitThread(void)
    1.73 +{
    1.74 +	if (!SDL_MintAudio_mint_present)
    1.75 +		return;
    1.76 +
    1.77 +	if (SDL_MintAudio_thread_finished)
    1.78 +		return;
    1.79 +
    1.80 +	SDL_MintAudio_quit_thread = SDL_TRUE;
    1.81 +	while (!SDL_MintAudio_thread_finished) {
    1.82 +		Syield();
    1.83 +	}
    1.84 +}
     2.1 --- a/src/audio/mint/SDL_mintaudio.h	Tue Aug 09 07:28:35 2005 +0000
     2.2 +++ b/src/audio/mint/SDL_mintaudio.h	Wed Aug 10 13:29:49 2005 +0000
     2.3 @@ -123,12 +123,22 @@
     2.4  extern cookie_stfa_t *SDL_MintAudio_stfa;
     2.5  extern unsigned long SDL_MintAudio_clocktics;
     2.6  
     2.7 +/* MiNT thread variables */
     2.8 +extern SDL_bool	SDL_MintAudio_mint_present;
     2.9 +extern SDL_bool SDL_MintAudio_quit_thread;
    2.10 +extern SDL_bool SDL_MintAudio_thread_finished;
    2.11 +extern long SDL_MintAudio_thread_pid;
    2.12 +
    2.13  /* Functions */
    2.14  void SDL_MintAudio_Callback(void);
    2.15  void SDL_MintAudio_AddFrequency(_THIS, Uint32 frequency, Uint32 clock,
    2.16  	Uint32 prediv, int gpio_bits);
    2.17  int SDL_MintAudio_SearchFrequency(_THIS, int desired_freq);
    2.18  
    2.19 +/* MiNT thread functions */
    2.20 +int SDL_MintAudio_Thread(long param);
    2.21 +void SDL_MintAudio_WaitThread(void);
    2.22 +
    2.23  /* ASM interrupt functions */
    2.24  void SDL_MintAudio_GsxbInterrupt(void);
    2.25  void SDL_MintAudio_EmptyGsxbInterrupt(void);
     3.1 --- a/src/audio/mint/SDL_mintaudio_mcsn.c	Tue Aug 09 07:28:35 2005 +0000
     3.2 +++ b/src/audio/mint/SDL_mintaudio_mcsn.c	Wed Aug 10 13:29:49 2005 +0000
     3.3 @@ -30,6 +30,7 @@
     3.4  #include <stdlib.h>
     3.5  #include <stdio.h>
     3.6  #include <string.h>
     3.7 +#include <support.h>
     3.8  
     3.9  /* Mint includes */
    3.10  #include <mint/osbind.h>
    3.11 @@ -85,12 +86,9 @@
    3.12  	unsigned long dummy;
    3.13  	const char *envr = getenv("SDL_AUDIODRIVER");
    3.14  
    3.15 -	/* We can't use XBIOS in interrupt under MiNT */
    3.16 -	if (Getcookie(C_MiNT, &dummy) == C_FOUND) {
    3.17 -		return(0);
    3.18 -	}
    3.19 +	SDL_MintAudio_mint_present = (Getcookie(C_MiNT, &dummy) == C_FOUND);
    3.20  
    3.21 -	/* nor with Magic */
    3.22 +	/* We can't use XBIOS in interrupt with Magic, don't know about thread */
    3.23  	if (Getcookie(C_MagX, &dummy) == C_FOUND) {
    3.24  		return(0);
    3.25  	}
    3.26 @@ -197,10 +195,13 @@
    3.27  static void Mint_CloseAudio(_THIS)
    3.28  {
    3.29  	/* Stop replay */
    3.30 +	SDL_MintAudio_WaitThread();
    3.31  	Buffoper(0);
    3.32  
    3.33 -	/* Uninstall interrupt */
    3.34 -	Jdisint(MFP_DMASOUND);
    3.35 +	if (!SDL_MintAudio_mint_present) {
    3.36 +		/* Uninstall interrupt */
    3.37 +		Jdisint(MFP_DMASOUND);
    3.38 +	}
    3.39  
    3.40  	/* Wait if currently playing sound */
    3.41  	while (SDL_MintAudio_mutex != 0) {
    3.42 @@ -298,6 +299,9 @@
    3.43  	void *buffer;
    3.44  
    3.45  	/* Stop currently playing sound */
    3.46 +	SDL_MintAudio_quit_thread = SDL_FALSE;
    3.47 +	SDL_MintAudio_thread_finished = SDL_TRUE;
    3.48 +	SDL_MintAudio_WaitThread();
    3.49  	Buffoper(0);
    3.50  
    3.51  	/* Set replay tracks */
    3.52 @@ -339,13 +343,17 @@
    3.53  		DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
    3.54  	}
    3.55  	
    3.56 -	/* Install interrupt */
    3.57 -	Jdisint(MFP_DMASOUND);
    3.58 -	Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt);
    3.59 -	Jenabint(MFP_DMASOUND);
    3.60 +	if (SDL_MintAudio_mint_present) {
    3.61 +		SDL_MintAudio_thread_pid = tfork(SDL_MintAudio_Thread, 0);
    3.62 +	} else {
    3.63 +		/* Install interrupt */
    3.64 +		Jdisint(MFP_DMASOUND);
    3.65 +		Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt);
    3.66 +		Jenabint(MFP_DMASOUND);
    3.67  
    3.68 -	if (Setinterrupt(SI_TIMERA, SI_PLAY)<0) {
    3.69 -		DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
    3.70 +		if (Setinterrupt(SI_TIMERA, SI_PLAY)<0) {
    3.71 +			DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
    3.72 +		}
    3.73  	}
    3.74  
    3.75  	/* Go */
    3.76 @@ -390,5 +398,5 @@
    3.77  	/* Setup audio hardware */
    3.78  	Mint_InitAudio(this, spec);
    3.79  
    3.80 -    return(1);	/* We don't use threaded audio */
    3.81 +    return(1);	/* We don't use SDL threaded audio */
    3.82  }
     4.1 --- a/src/audio/mint/SDL_mintaudio_xbios.c	Tue Aug 09 07:28:35 2005 +0000
     4.2 +++ b/src/audio/mint/SDL_mintaudio_xbios.c	Wed Aug 10 13:29:49 2005 +0000
     4.3 @@ -31,6 +31,7 @@
     4.4  #include <stdio.h>
     4.5  #include <string.h>
     4.6  #include <unistd.h>
     4.7 +#include <support.h>
     4.8  
     4.9  /* Mint includes */
    4.10  #include <mint/osbind.h>
    4.11 @@ -85,12 +86,9 @@
    4.12  	unsigned long dummy;
    4.13  	const char *envr = getenv("SDL_AUDIODRIVER");
    4.14  
    4.15 -	/* We can't use XBIOS in interrupt under MiNT */
    4.16 -	if (Getcookie(C_MiNT, &dummy) == C_FOUND) {
    4.17 -		return(0);
    4.18 -	}
    4.19 +	SDL_MintAudio_mint_present = (Getcookie(C_MiNT, &dummy) == C_FOUND);
    4.20  
    4.21 -	/* nor with Magic */
    4.22 +	/* We can't use XBIOS in interrupt with Magic, don't know about thread */
    4.23  	if (Getcookie(C_MagX, &dummy) == C_FOUND) {
    4.24  		return(0);
    4.25  	}
    4.26 @@ -180,10 +178,13 @@
    4.27  static void Mint_CloseAudio(_THIS)
    4.28  {
    4.29  	/* Stop replay */
    4.30 +	SDL_MintAudio_WaitThread();
    4.31  	Buffoper(0);
    4.32  
    4.33 -	/* Uninstall interrupt */
    4.34 -	Jdisint(MFP_DMASOUND);
    4.35 +	if (!SDL_MintAudio_mint_present) {
    4.36 +		/* Uninstall interrupt */
    4.37 +		Jdisint(MFP_DMASOUND);
    4.38 +	}
    4.39  
    4.40  	/* Wait if currently playing sound */
    4.41  	while (SDL_MintAudio_mutex != 0) {
    4.42 @@ -391,6 +392,9 @@
    4.43  	void *buffer;
    4.44  
    4.45  	/* Stop currently playing sound */
    4.46 +	SDL_MintAudio_quit_thread = SDL_FALSE;
    4.47 +	SDL_MintAudio_thread_finished = SDL_TRUE;
    4.48 +	SDL_MintAudio_WaitThread();
    4.49  	Buffoper(0);
    4.50  
    4.51  	/* Set replay tracks */
    4.52 @@ -428,13 +432,17 @@
    4.53  		DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
    4.54  	}
    4.55  	
    4.56 -	/* Install interrupt */
    4.57 -	Jdisint(MFP_DMASOUND);
    4.58 -	Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt);
    4.59 -	Jenabint(MFP_DMASOUND);
    4.60 +	if (SDL_MintAudio_mint_present) {
    4.61 +		SDL_MintAudio_thread_pid = tfork(SDL_MintAudio_Thread, 0);
    4.62 +	} else {
    4.63 +		/* Install interrupt */
    4.64 +		Jdisint(MFP_DMASOUND);
    4.65 +		Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt);
    4.66 +		Jenabint(MFP_DMASOUND);
    4.67  
    4.68 -	if (Setinterrupt(SI_TIMERA, SI_PLAY)<0) {
    4.69 -		DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
    4.70 +		if (Setinterrupt(SI_TIMERA, SI_PLAY)<0) {
    4.71 +			DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
    4.72 +		}
    4.73  	}
    4.74  
    4.75  	/* Go */
    4.76 @@ -479,5 +487,5 @@
    4.77  	/* Setup audio hardware */
    4.78  	Mint_InitAudio(this, spec);
    4.79  
    4.80 -    return(1);	/* We don't use threaded audio */
    4.81 +    return(1);	/* We don't use SDL threaded audio */
    4.82  }