atari: Do not use system interrupt for handling audio. Do it from userspace, like RISCOS backend. SDL-1.2
authorPatrice Mandin
Mon, 28 Jul 2014 21:15:42 +0200
branchSDL-1.2
changeset 9022de368e497138
parent 9021 1691f05e9531
child 9024 372ca9a5be7c
atari: Do not use system interrupt for handling audio. Do it from userspace, like RISCOS backend.
src/audio/mint/SDL_mintaudio.c
src/audio/mint/SDL_mintaudio.h
src/audio/mint/SDL_mintaudio_dma8.c
src/audio/mint/SDL_mintaudio_gsxb.c
src/audio/mint/SDL_mintaudio_it.S
src/audio/mint/SDL_mintaudio_mcsn.c
src/audio/mint/SDL_mintaudio_stfa.c
src/audio/mint/SDL_mintaudio_xbios.c
src/video/ataricommon/SDL_atarievents.c
     1.1 --- a/src/audio/mint/SDL_mintaudio.c	Mon Jul 28 20:40:41 2014 +0200
     1.2 +++ b/src/audio/mint/SDL_mintaudio.c	Mon Jul 28 21:15:42 2014 +0200
     1.3 @@ -35,19 +35,24 @@
     1.4  #include <mint/cookie.h>
     1.5  
     1.6  #include "SDL_audio.h"
     1.7 +#include "../SDL_audio_c.h"
     1.8 +#include "../SDL_sysaudio.h"
     1.9 +
    1.10 +#include "../../video/ataricommon/SDL_atarimxalloc_c.h"
    1.11 +
    1.12  #include "SDL_mintaudio.h"
    1.13 -#include "SDL_mintaudio_stfa.h"
    1.14  
    1.15  /* The audio device */
    1.16  
    1.17 +#define MAX_DMA_BUF	8
    1.18 +
    1.19  SDL_AudioDevice *SDL_MintAudio_device;
    1.20 -Uint8 *SDL_MintAudio_audiobuf[2];	/* Pointers to buffers */
    1.21 -unsigned long SDL_MintAudio_audiosize;		/* Length of audio buffer=spec->size */
    1.22 -volatile unsigned short SDL_MintAudio_numbuf;		/* Buffer to play */
    1.23 -volatile unsigned short SDL_MintAudio_mutex;
    1.24 -volatile unsigned long SDL_MintAudio_clocktics;
    1.25 -cookie_stfa_t	*SDL_MintAudio_stfa;
    1.26 -unsigned short SDL_MintAudio_hasfpu;
    1.27 +
    1.28 +static int SDL_MintAudio_num_upd;	/* Number of calls to update function */
    1.29 +static int SDL_MintAudio_max_buf;	/* Number of buffers to use */
    1.30 +static int SDL_MintAudio_numbuf;	/* Buffer to play */
    1.31 +
    1.32 +static void SDL_MintAudio_Callback(void);
    1.33  
    1.34  /* MiNT thread variables */
    1.35  SDL_bool SDL_MintAudio_mint_present;
    1.36 @@ -55,34 +60,129 @@
    1.37  SDL_bool SDL_MintAudio_thread_finished;
    1.38  long SDL_MintAudio_thread_pid;
    1.39  
    1.40 +/* Debug print info */
    1.41 +#define DEBUG_NAME "audio:mint: "
    1.42 +#if 0
    1.43 +#define DEBUG_PRINT(what) \
    1.44 +	{ \
    1.45 +		printf what; \
    1.46 +	}
    1.47 +#else
    1.48 +#define DEBUG_PRINT(what)
    1.49 +#endif
    1.50 +
    1.51 +/* Initialize DMA buffers */
    1.52 +
    1.53 +int SDL_MintAudio_InitBuffers(SDL_AudioSpec *spec)
    1.54 +{
    1.55 +	SDL_AudioDevice *this = SDL_MintAudio_device;
    1.56 +
    1.57 +	SDL_CalculateAudioSpec(spec);
    1.58 +	MINTAUDIO_audiosize = spec->size * MAX_DMA_BUF;
    1.59 +
    1.60 +	/* Allocate memory for audio buffers in DMA-able RAM */
    1.61 +	MINTAUDIO_audiobuf[0] = Atari_SysMalloc(2 * MINTAUDIO_audiosize, MX_STRAM);
    1.62 +	if (MINTAUDIO_audiobuf[0]==NULL) {
    1.63 +		SDL_SetError("SDL_MintAudio_OpenAudio: Not enough memory for audio buffer");
    1.64 +		return (0);
    1.65 +	}
    1.66 +	MINTAUDIO_audiobuf[1] = MINTAUDIO_audiobuf[0] + MINTAUDIO_audiosize;
    1.67 +	SDL_memset(MINTAUDIO_audiobuf[0], spec->silence, 2 * MINTAUDIO_audiosize);
    1.68 +
    1.69 +	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%p\n", MINTAUDIO_audiobuf[0]));
    1.70 +	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%p\n", MINTAUDIO_audiobuf[1]));
    1.71 +
    1.72 +	SDL_MintAudio_numbuf = SDL_MintAudio_num_its = SDL_MintAudio_num_upd = 0;
    1.73 +	SDL_MintAudio_max_buf = MAX_DMA_BUF;
    1.74 +
    1.75 +	return (1);
    1.76 +}
    1.77 +
    1.78 +/* Destroy DMA buffers */
    1.79 +
    1.80 +void SDL_MintAudio_FreeBuffers(void)
    1.81 +{
    1.82 +	SDL_AudioDevice *this = SDL_MintAudio_device;
    1.83 +
    1.84 +	if (MINTAUDIO_audiobuf[0]) {
    1.85 +		Mfree(MINTAUDIO_audiobuf[0]);
    1.86 +		MINTAUDIO_audiobuf[0] = MINTAUDIO_audiobuf[1] = NULL;
    1.87 +	}
    1.88 +}
    1.89 +
    1.90 +/* Update buffers */
    1.91 +
    1.92 +void SDL_AtariMint_UpdateAudio(void)
    1.93 +{
    1.94 +	SDL_AudioDevice *this = SDL_MintAudio_device;
    1.95 +
    1.96 +	++SDL_MintAudio_num_upd;
    1.97 +
    1.98 +	/* No interrupt triggered? still playing current buffer */
    1.99 +	if (SDL_MintAudio_num_its==0) {
   1.100 +		return;
   1.101 +	}
   1.102 +
   1.103 +	if (SDL_MintAudio_num_upd < (SDL_MintAudio_num_its<<2)) {
   1.104 +		/* Too many interrupts per update, increase latency */
   1.105 +		if (SDL_MintAudio_max_buf < MAX_DMA_BUF) {
   1.106 +			SDL_MintAudio_max_buf <<= 1;
   1.107 +		}
   1.108 +	} else if (SDL_MintAudio_num_its < (SDL_MintAudio_num_upd<<2)) {
   1.109 +		/* Too many updates per interrupt, decrease latency */
   1.110 +		if (SDL_MintAudio_max_buf > 1) {
   1.111 +			SDL_MintAudio_max_buf >>= 1;
   1.112 +		}
   1.113 +	}
   1.114 +	MINTAUDIO_audiosize = this->spec.size * SDL_MintAudio_max_buf;
   1.115 +
   1.116 +	SDL_MintAudio_num_its = 0;
   1.117 +	SDL_MintAudio_num_upd = 0;
   1.118 +
   1.119 +	SDL_MintAudio_numbuf ^= 1;
   1.120 +
   1.121 +	/* Fill new buffer */
   1.122 +	SDL_MintAudio_Callback();
   1.123 +
   1.124 +	/* And swap to it */
   1.125 +	(*MINTAUDIO_swapbuf)(MINTAUDIO_audiobuf[SDL_MintAudio_numbuf], MINTAUDIO_audiosize);
   1.126 +}
   1.127 +
   1.128  /* The callback function, called by each driver whenever needed */
   1.129  
   1.130 -void SDL_MintAudio_Callback(void)
   1.131 +static void SDL_MintAudio_Callback(void)
   1.132  {
   1.133 +	SDL_AudioDevice *this = SDL_MintAudio_device;
   1.134  	Uint8 *buffer;
   1.135 -	SDL_AudioDevice *audio = SDL_MintAudio_device;
   1.136 +	int i;
   1.137  
   1.138 - 	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
   1.139 -	SDL_memset(buffer, audio->spec.silence, audio->spec.size);
   1.140 + 	buffer = MINTAUDIO_audiobuf[SDL_MintAudio_numbuf];
   1.141 +	SDL_memset(buffer, this->spec.silence, this->spec.size * SDL_MintAudio_max_buf);
   1.142  
   1.143 -	if (audio->paused)
   1.144 +	if (this->paused)
   1.145  		return;
   1.146  
   1.147 -	if (audio->convert.needed) {
   1.148 -		int silence;
   1.149 +	for (i=0; i<SDL_MintAudio_max_buf; i++) {
   1.150 +		if (this->convert.needed) {
   1.151 +			int silence;
   1.152  
   1.153 -		if ( audio->convert.src_format == AUDIO_U8 ) {
   1.154 -			silence = 0x80;
   1.155 +			if ( this->convert.src_format == AUDIO_U8 ) {
   1.156 +				silence = 0x80;
   1.157 +			} else {
   1.158 +				silence = 0;
   1.159 +			}
   1.160 +			SDL_memset(this->convert.buf, silence, this->convert.len);
   1.161 +			this->spec.callback(this->spec.userdata,
   1.162 +				(Uint8 *)this->convert.buf,this->convert.len);
   1.163 +			SDL_ConvertAudio(&this->convert);
   1.164 +			SDL_memcpy(buffer, this->convert.buf, this->convert.len_cvt);
   1.165 +
   1.166 +			buffer += this->convert.len_cvt;
   1.167  		} else {
   1.168 -			silence = 0;
   1.169 +			this->spec.callback(this->spec.userdata, buffer, this->spec.size);
   1.170 +
   1.171 +			buffer += this->spec.size;
   1.172  		}
   1.173 -		SDL_memset(audio->convert.buf, silence, audio->convert.len);
   1.174 -		audio->spec.callback(audio->spec.userdata,
   1.175 -			(Uint8 *)audio->convert.buf,audio->convert.len);
   1.176 -		SDL_ConvertAudio(&audio->convert);
   1.177 -		SDL_memcpy(buffer, audio->convert.buf, audio->convert.len_cvt);
   1.178 -	} else {
   1.179 -		audio->spec.callback(audio->spec.userdata, buffer, audio->spec.size);
   1.180  	}
   1.181  }
   1.182  
   1.183 @@ -141,45 +241,26 @@
   1.184  	return MINTAUDIO_freqcount-1;
   1.185  }
   1.186  
   1.187 -/* Check if FPU is present */
   1.188 -void SDL_MintAudio_CheckFpu(void)
   1.189 -{
   1.190 -	long cookie_fpu;
   1.191 -
   1.192 -	SDL_MintAudio_hasfpu = 0;
   1.193 -	if (Getcookie(C__FPU, &cookie_fpu) != C_FOUND) {
   1.194 -		return;
   1.195 -	}
   1.196 -	switch ((cookie_fpu>>16)&0xfffe) {
   1.197 -		case 2:
   1.198 -		case 4:
   1.199 -		case 6:
   1.200 -		case 8:
   1.201 -		case 16:
   1.202 -			SDL_MintAudio_hasfpu = 1;
   1.203 -			break;
   1.204 -	}
   1.205 -}
   1.206 -
   1.207  /* The thread function, used under MiNT with xbios */
   1.208  int SDL_MintAudio_Thread(long param)
   1.209  {
   1.210  	SndBufPtr	pointers;
   1.211  	SDL_bool	buffers_filled[2] = {SDL_FALSE, SDL_FALSE};
   1.212 +	SDL_AudioDevice *this = SDL_MintAudio_device;
   1.213  
   1.214  	SDL_MintAudio_thread_finished = SDL_FALSE;
   1.215  	while (!SDL_MintAudio_quit_thread) {
   1.216  		if (Buffptr(&pointers)!=0)
   1.217  			continue;
   1.218  
   1.219 -		if (( (unsigned long)pointers.play>=(unsigned long)SDL_MintAudio_audiobuf[0])
   1.220 -			&& ( (unsigned long)pointers.play<=(unsigned long)SDL_MintAudio_audiobuf[1])) 
   1.221 +		if (( (unsigned long)pointers.play>=(unsigned long)MINTAUDIO_audiobuf[0])
   1.222 +			&& ( (unsigned long)pointers.play<=(unsigned long)MINTAUDIO_audiobuf[1])) 
   1.223  		{
   1.224  			/* DMA is reading buffer #0, setup buffer #1 if not already done */
   1.225  			if (!buffers_filled[1]) {
   1.226  				SDL_MintAudio_numbuf = 1;
   1.227  				SDL_MintAudio_Callback();
   1.228 -				Setbuffer(0, SDL_MintAudio_audiobuf[1], SDL_MintAudio_audiobuf[1] + SDL_MintAudio_audiosize);
   1.229 +				Setbuffer(0, MINTAUDIO_audiobuf[1], MINTAUDIO_audiobuf[1] + MINTAUDIO_audiosize);
   1.230  				buffers_filled[1]=SDL_TRUE;
   1.231  				buffers_filled[0]=SDL_FALSE;
   1.232  			}
   1.233 @@ -188,7 +269,7 @@
   1.234  			if (!buffers_filled[0]) {
   1.235  				SDL_MintAudio_numbuf = 0;
   1.236  				SDL_MintAudio_Callback();
   1.237 -				Setbuffer(0, SDL_MintAudio_audiobuf[0], SDL_MintAudio_audiobuf[0] + SDL_MintAudio_audiosize);
   1.238 +				Setbuffer(0, MINTAUDIO_audiobuf[0], MINTAUDIO_audiobuf[0] + MINTAUDIO_audiosize);
   1.239  				buffers_filled[0]=SDL_TRUE;
   1.240  				buffers_filled[1]=SDL_FALSE;
   1.241  			}
     2.1 --- a/src/audio/mint/SDL_mintaudio.h	Mon Jul 28 20:40:41 2014 +0200
     2.2 +++ b/src/audio/mint/SDL_mintaudio.h	Mon Jul 28 21:15:42 2014 +0200
     2.3 @@ -48,15 +48,23 @@
     2.4  
     2.5  struct SDL_PrivateAudioData {
     2.6  	mint_frequency_t	frequencies[MINTAUDIO_maxfreqs];
     2.7 -	int 	freq_count;		/* Number of frequencies in the array */
     2.8 -	int		numfreq;		/* Number of selected frequency */
     2.9 +	int	freq_count;	/* Number of frequencies in the array */
    2.10 +	int	numfreq;	/* Number of selected frequency */
    2.11 +
    2.12 +	Uint8	*audiobuf[2];	/* DMA buffers */
    2.13 +	int	audiosize;	/* and their size, variable depending on latency */
    2.14 +
    2.15 +	void (*swapbuf)(Uint8 *nextbuf, int nextsize);	/* Routine to swap DMA buffers */
    2.16  };
    2.17  
    2.18  /* Old variable names */
    2.19  
    2.20  #define MINTAUDIO_frequencies	(this->hidden->frequencies)
    2.21 -#define MINTAUDIO_freqcount		(this->hidden->freq_count)
    2.22 -#define MINTAUDIO_numfreq		(this->hidden->numfreq)
    2.23 +#define MINTAUDIO_freqcount	(this->hidden->freq_count)
    2.24 +#define MINTAUDIO_numfreq	(this->hidden->numfreq)
    2.25 +#define MINTAUDIO_swapbuf	(this->hidden->swapbuf)
    2.26 +#define MINTAUDIO_audiobuf	(this->hidden->audiobuf)
    2.27 +#define MINTAUDIO_audiosize	(this->hidden->audiosize)
    2.28  
    2.29  /* _MCH cookie (values>>16) */
    2.30  enum {
    2.31 @@ -85,13 +93,6 @@
    2.32  
    2.33  /* Variables */
    2.34  extern SDL_AudioDevice *SDL_MintAudio_device;
    2.35 -extern Uint8 *SDL_MintAudio_audiobuf[2];	/* Pointers to buffers */
    2.36 -extern unsigned long SDL_MintAudio_audiosize;		/* Length of audio buffer=spec->size */
    2.37 -extern volatile unsigned short SDL_MintAudio_numbuf;		/* Buffer to play */
    2.38 -extern volatile unsigned short SDL_MintAudio_mutex;
    2.39 -extern cookie_stfa_t *SDL_MintAudio_stfa;
    2.40 -extern volatile unsigned long SDL_MintAudio_clocktics;
    2.41 -extern unsigned short SDL_MintAudio_hasfpu;	/* To preserve fpu registers if needed */
    2.42  
    2.43  /* MiNT thread variables */
    2.44  extern SDL_bool	SDL_MintAudio_mint_present;
    2.45 @@ -100,19 +101,25 @@
    2.46  extern long SDL_MintAudio_thread_pid;
    2.47  
    2.48  /* Functions */
    2.49 -void SDL_MintAudio_Callback(void);
    2.50 +void SDL_AtariMint_UpdateAudio(void);
    2.51 +
    2.52 +int SDL_MintAudio_InitBuffers(SDL_AudioSpec *spec);
    2.53 +void SDL_MintAudio_FreeBuffers(void);
    2.54  void SDL_MintAudio_AddFrequency(_THIS, Uint32 frequency, Uint32 clock,
    2.55  	Uint32 prediv, int gpio_bits);
    2.56  int SDL_MintAudio_SearchFrequency(_THIS, int desired_freq);
    2.57 -void SDL_MintAudio_CheckFpu(void);
    2.58  
    2.59  /* MiNT thread functions */
    2.60  int SDL_MintAudio_Thread(long param);
    2.61  void SDL_MintAudio_WaitThread(void);
    2.62  
    2.63 -/* ASM interrupt functions */
    2.64 -void SDL_MintAudio_GsxbInterrupt(void);
    2.65 -void SDL_MintAudio_EmptyGsxbInterrupt(void);
    2.66 +/*--- SDL_mintaudio_it.S stuff ---*/
    2.67 +
    2.68 +/* Variables */
    2.69 +extern volatile unsigned long SDL_MintAudio_clocktics;
    2.70 +extern volatile unsigned long SDL_MintAudio_num_its;
    2.71 +
    2.72 +/* Functions */
    2.73  void SDL_MintAudio_XbiosInterruptMeasureClock(void);
    2.74  void SDL_MintAudio_XbiosInterrupt(void);
    2.75  void SDL_MintAudio_Dma8Interrupt(void);
     3.1 --- a/src/audio/mint/SDL_mintaudio_dma8.c	Mon Jul 28 20:40:41 2014 +0200
     3.2 +++ b/src/audio/mint/SDL_mintaudio_dma8.c	Mon Jul 28 21:15:42 2014 +0200
     3.3 @@ -37,7 +37,7 @@
     3.4  #include "../SDL_audio_c.h"
     3.5  #include "../SDL_sysaudio.h"
     3.6  
     3.7 -#include "../../video/ataricommon/SDL_atarimxalloc_c.h"
     3.8 +#include "../../video/ataricommon/SDL_atarisuper.h"
     3.9  
    3.10  #include "SDL_mintaudio.h"
    3.11  #include "SDL_mintaudio_dma8.h"
    3.12 @@ -70,8 +70,10 @@
    3.13  
    3.14  /* To check/init hardware audio */
    3.15  static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
    3.16 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize);
    3.17  
    3.18  /* Functions called in supervisor mode */
    3.19 +static void Mint_SwapBuffersSuper(Uint8 *nextbuf, int nextsize);
    3.20  static void Mint_InitDma(void);
    3.21  static void Mint_StopReplay(void);
    3.22  static void Mint_StartReplay(void);
    3.23 @@ -180,19 +182,7 @@
    3.24  
    3.25  	DEBUG_PRINT((DEBUG_NAME "closeaudio: interrupt disabled\n"));
    3.26  
    3.27 -	/* Wait if currently playing sound */
    3.28 -	while (SDL_MintAudio_mutex != 0) {
    3.29 -	}
    3.30 -
    3.31 -	DEBUG_PRINT((DEBUG_NAME "closeaudio: no more interrupt running\n"));
    3.32 -
    3.33 -	/* Clear buffers */
    3.34 -	if (SDL_MintAudio_audiobuf[0]) {
    3.35 -		Mfree(SDL_MintAudio_audiobuf[0]);
    3.36 -		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
    3.37 -	}
    3.38 -
    3.39 -	DEBUG_PRINT((DEBUG_NAME "closeaudio: buffers freed\n"));
    3.40 +	SDL_MintAudio_FreeBuffers();
    3.41  }
    3.42  
    3.43  static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
    3.44 @@ -271,26 +261,11 @@
    3.45  		return -1;
    3.46  	}
    3.47  
    3.48 -	SDL_CalculateAudioSpec(spec);
    3.49 -
    3.50 -	/* Allocate memory for audio buffers in DMA-able RAM */
    3.51 -	DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
    3.52 -
    3.53 -	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
    3.54 -	if (SDL_MintAudio_audiobuf[0]==NULL) {
    3.55 -		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
    3.56 -		return (-1);
    3.57 +	if (!SDL_MintAudio_InitBuffers(spec)) {
    3.58 +		return -1;
    3.59  	}
    3.60 -	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
    3.61 -	SDL_MintAudio_numbuf=0;
    3.62 -	SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
    3.63 -	SDL_MintAudio_audiosize = spec->size;
    3.64 -	SDL_MintAudio_mutex = 0;
    3.65  
    3.66 -	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
    3.67 -	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
    3.68 -
    3.69 -	SDL_MintAudio_CheckFpu();
    3.70 +	MINTAUDIO_swapbuf = Mint_SwapBuffers;
    3.71  
    3.72  	/* Set replay tracks */
    3.73  	if (cookie_snd & SND_16BIT) {
    3.74 @@ -316,26 +291,44 @@
    3.75      return(1);	/* We don't use threaded audio */
    3.76  }
    3.77  
    3.78 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize)
    3.79 +{
    3.80 +	void *old_stack;
    3.81 +
    3.82 +	/* Set first ticks value */
    3.83 +	old_stack = (void *)Super(0);
    3.84 +
    3.85 +	Mint_SwapBuffersSuper(nextbuf, nextsize);
    3.86 +
    3.87 +	SuperToUser(old_stack);
    3.88 +}
    3.89 +
    3.90  /* Functions called in supervisor mode */
    3.91  
    3.92 +static void Mint_SwapBuffersSuper(Uint8 *nextbuf, int nextsize)
    3.93 +{
    3.94 +	unsigned long buffer;
    3.95 +
    3.96 +	buffer = (unsigned long) nextbuf;
    3.97 +	DMAAUDIO_IO.start_high = (buffer>>16) & 255;
    3.98 +	DMAAUDIO_IO.start_mid = (buffer>>8) & 255;
    3.99 +	DMAAUDIO_IO.start_low = buffer & 255;
   3.100 +
   3.101 +	buffer += nextsize;
   3.102 +	DMAAUDIO_IO.end_high = (buffer>>16) & 255;
   3.103 +	DMAAUDIO_IO.end_mid = (buffer>>8) & 255;
   3.104 +	DMAAUDIO_IO.end_low = buffer & 255;
   3.105 +}
   3.106 +
   3.107  static void Mint_InitDma(void)
   3.108  {
   3.109 -	unsigned long buffer;
   3.110 +	SDL_AudioDevice *this = SDL_MintAudio_device;
   3.111  	unsigned char mode;
   3.112 -	SDL_AudioDevice *this = SDL_MintAudio_device;
   3.113  
   3.114  	Mint_StopReplay();
   3.115  
   3.116  	/* Set buffer */
   3.117 -	buffer = (unsigned long) SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
   3.118 -	DMAAUDIO_IO.start_high = (buffer>>16) & 255;
   3.119 -	DMAAUDIO_IO.start_mid = (buffer>>8) & 255;
   3.120 -	DMAAUDIO_IO.start_low = buffer & 255;
   3.121 -
   3.122 -	buffer += SDL_MintAudio_audiosize;
   3.123 -	DMAAUDIO_IO.end_high = (buffer>>16) & 255;
   3.124 -	DMAAUDIO_IO.end_mid = (buffer>>8) & 255;
   3.125 -	DMAAUDIO_IO.end_low = buffer & 255;
   3.126 +	Mint_SwapBuffersSuper(MINTAUDIO_audiobuf[0], MINTAUDIO_audiosize);
   3.127  
   3.128  	mode = 3-MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
   3.129  	if (this->spec.channels==1) {
     4.1 --- a/src/audio/mint/SDL_mintaudio_gsxb.c	Mon Jul 28 20:40:41 2014 +0200
     4.2 +++ b/src/audio/mint/SDL_mintaudio_gsxb.c	Mon Jul 28 21:15:42 2014 +0200
     4.3 @@ -37,8 +37,6 @@
     4.4  #include "../SDL_audio_c.h"
     4.5  #include "../SDL_sysaudio.h"
     4.6  
     4.7 -#include "../../video/ataricommon/SDL_atarimxalloc_c.h"
     4.8 -
     4.9  #include "SDL_mintaudio.h"
    4.10  #include "SDL_mintaudio_gsxb.h"
    4.11  
    4.12 @@ -71,6 +69,7 @@
    4.13  /* To check/init hardware audio */
    4.14  static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
    4.15  static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
    4.16 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize);
    4.17  
    4.18  /* GSXB callbacks */
    4.19  static void Mint_GsxbInterrupt(void);
    4.20 @@ -183,15 +182,7 @@
    4.21  		DEBUG_PRINT((DEBUG_NAME "NSetinterrupt() failed in close\n"));
    4.22  	}
    4.23  
    4.24 -	/* Wait if currently playing sound */
    4.25 -	while (SDL_MintAudio_mutex != 0) {
    4.26 -	}
    4.27 -
    4.28 -	/* Clear buffers */
    4.29 -	if (SDL_MintAudio_audiobuf[0]) {
    4.30 -		Mfree(SDL_MintAudio_audiobuf[0]);
    4.31 -		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
    4.32 -	}
    4.33 +	SDL_MintAudio_FreeBuffers();
    4.34  
    4.35  	/* Unlock sound system */
    4.36  	Unlocksnd();
    4.37 @@ -200,11 +191,11 @@
    4.38  static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
    4.39  {
    4.40  	long snd_format = 0;
    4.41 -	int i, resolution, format_signed, format_bigendian;
    4.42 +	int i, /*resolution,*/ format_signed, format_bigendian;
    4.43      Uint16 test_format = SDL_FirstAudioFormat(spec->format);
    4.44      int valid_datatype = 0;
    4.45  
    4.46 -	resolution = spec->format & 0x00ff;
    4.47 +	/*resolution = spec->format & 0x00ff;*/
    4.48  	format_signed = ((spec->format & 0x8000)!=0);
    4.49  	format_bigendian = ((spec->format & 0x1000)!=0);
    4.50  
    4.51 @@ -222,7 +213,7 @@
    4.52          /* Check formats available */
    4.53          snd_format = Sndstatus(SND_QUERYFORMATS);
    4.54          spec->format = test_format;
    4.55 -        resolution = spec->format & 0xff;
    4.56 +        /*resolution = spec->format & 0xff;*/
    4.57          format_signed = (spec->format & (1<<15));
    4.58          format_bigendian = (spec->format & (1<<12));
    4.59          switch (test_format) {
    4.60 @@ -320,7 +311,6 @@
    4.61  static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
    4.62  {
    4.63  	int channels_mode, prediv;
    4.64 -	void *buffer;
    4.65  
    4.66  	/* Stop currently playing sound */
    4.67  	Buffoper(0);
    4.68 @@ -357,10 +347,7 @@
    4.69  	Devconnect(DMAPLAY, DAC, CLKEXT, prediv, 1);
    4.70  
    4.71  	/* Set buffer */
    4.72 -	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
    4.73 -	if (Setbuffer(0, buffer, buffer + spec->size)<0) {
    4.74 -		DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
    4.75 -	}
    4.76 +	Mint_SwapBuffers(MINTAUDIO_audiobuf[0], MINTAUDIO_audiosize);
    4.77  	
    4.78  	/* Install interrupt */
    4.79  	if (NSetinterrupt(2, SI_PLAY, Mint_GsxbInterrupt)<0) {
    4.80 @@ -387,48 +374,27 @@
    4.81  		return -1;
    4.82  	}
    4.83  
    4.84 -	SDL_CalculateAudioSpec(spec);
    4.85 -
    4.86 -	/* Allocate memory for audio buffers in DMA-able RAM */
    4.87 -	DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
    4.88 -
    4.89 -	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
    4.90 -	if (SDL_MintAudio_audiobuf[0]==NULL) {
    4.91 -		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
    4.92 -		return (-1);
    4.93 +	if (!SDL_MintAudio_InitBuffers(spec)) {
    4.94 +		return -1;
    4.95  	}
    4.96 -	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
    4.97 -	SDL_MintAudio_numbuf=0;
    4.98 -	SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
    4.99 -	SDL_MintAudio_audiosize = spec->size;
   4.100 -	SDL_MintAudio_mutex = 0;
   4.101 -
   4.102 -	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
   4.103 -	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
   4.104 -
   4.105 -	SDL_MintAudio_CheckFpu();
   4.106  
   4.107  	/* Setup audio hardware */
   4.108 +	MINTAUDIO_swapbuf = Mint_SwapBuffers;
   4.109  	Mint_InitAudio(this, spec);
   4.110  
   4.111      return(1);	/* We don't use threaded audio */
   4.112  }
   4.113  
   4.114 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize)
   4.115 +{
   4.116 +	unsigned long buffer = (unsigned long) nextbuf;
   4.117 +
   4.118 +	Setbuffer(0, buffer, buffer + nextsize);
   4.119 +}
   4.120 +
   4.121  static void Mint_GsxbInterrupt(void)
   4.122  {
   4.123 -	Uint8 *newbuf;
   4.124 -
   4.125 -	if (SDL_MintAudio_mutex)
   4.126 -		return;
   4.127 -
   4.128 -	SDL_MintAudio_mutex=1;
   4.129 -
   4.130 -	SDL_MintAudio_numbuf ^= 1;
   4.131 -	SDL_MintAudio_Callback();
   4.132 -	newbuf = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
   4.133 -	Setbuffer(0, newbuf, newbuf + SDL_MintAudio_audiosize);
   4.134 -
   4.135 -	SDL_MintAudio_mutex=0;
   4.136 +	++SDL_MintAudio_num_its;
   4.137  }
   4.138  
   4.139  static void Mint_GsxbNullInterrupt(void)
     5.1 --- a/src/audio/mint/SDL_mintaudio_it.S	Mon Jul 28 20:40:41 2014 +0200
     5.2 +++ b/src/audio/mint/SDL_mintaudio_it.S	Mon Jul 28 21:15:42 2014 +0200
     5.3 @@ -28,359 +28,69 @@
     5.4  
     5.5  	.text
     5.6  
     5.7 -	.globl	_SDL_MintAudio_Callback
     5.8 +	.globl	_SDL_MintAudio_XbiosInterruptMeasureClock
     5.9 +	.globl	_SDL_MintAudio_clocktics
    5.10  
    5.11  	.globl	_SDL_MintAudio_XbiosInterrupt
    5.12 -	.globl	_SDL_MintAudio_XbiosInterruptMeasureClock
    5.13  	.globl	_SDL_MintAudio_Dma8Interrupt
    5.14  	.globl	_SDL_MintAudio_StfaInterrupt
    5.15  
    5.16 -	.globl	_SDL_MintAudio_mutex
    5.17 -	.globl	_SDL_MintAudio_audiobuf
    5.18 -	.globl	_SDL_MintAudio_numbuf
    5.19 -	.globl	_SDL_MintAudio_audiosize
    5.20 -	.globl	_SDL_MintAudio_clocktics
    5.21 -	.globl	_SDL_MintAudio_hasfpu
    5.22 -
    5.23 -	.globl	_SDL_MintAudio_stfa
    5.24 -
    5.25 -/*
    5.26 -	How it works:
    5.27 -	- Audio is playing buffer #0 (resp. #1)
    5.28 -	- We must calculate a sample in buffer #1 (resp. #0)
    5.29 -	  so we first call the callback to do it
    5.30 -	- Then we swap the buffers
    5.31 -*/
    5.32 -
    5.33 -#define	savptr	0x4a2
    5.34 -#define	savamt	0x46
    5.35 -
    5.36 -/*--- Save/restore FPU context ---*/
    5.37 -
    5.38 -#if defined(__mcoldfire__)
    5.39 -
    5.40 -#define SAVE_FPU_CONTEXT \
    5.41 -	lea		sp@(-216),sp;	\
    5.42 -	fsave		sp@;	\
    5.43 -	fmovel		fpiar,sp@-;	\
    5.44 -	lea		sp@(-64),sp;	\
    5.45 -	fmovemd	fp0-fp7,sp@
    5.46 -
    5.47 -#define RESTORE_FPU_CONTEXT	\
    5.48 -	fmovemd		sp@,fp0-fp7;	\
    5.49 -	lea		sp@(64),sp;	\
    5.50 -	fmovel		sp@+,fpiar;	\
    5.51 -	frestore	sp@;	\
    5.52 -	lea		sp@(216),sp
    5.53 -
    5.54 -#else
    5.55 -
    5.56 -#define SAVE_FPU_CONTEXT	\
    5.57 -	.chip	68k/68881;	\
    5.58 -	fsave	sp@-;	\
    5.59 -	fmoveml fpcr/fpsr/fpiar,sp@-;	\
    5.60 -	fmovemx	fp0-fp7,sp@-;	\
    5.61 -	.chip	68k
    5.62 -
    5.63 -#define RESTORE_FPU_CONTEXT	\
    5.64 -	.chip	68k/68881;	\
    5.65 -	fmovemx	sp@+,fp0-fp7;	\
    5.66 -	fmoveml	sp@+,fpcr/fpsr/fpiar;	\
    5.67 -	frestore	sp@+;	\
    5.68 -	.chip	68k
    5.69 -
    5.70 -#endif
    5.71 +	.globl	_SDL_MintAudio_num_its
    5.72  
    5.73  /*--- Xbios interrupt vector to measure Falcon external clock ---*/
    5.74  
    5.75  _SDL_MintAudio_XbiosInterruptMeasureClock:          /* 1 mS */
    5.76 +
    5.77 +	/* state DMA sound */
    5.78  #if defined(__mcoldfire__)
    5.79  	movel	d0,sp@-
    5.80  	
    5.81  	moveql	#0,d0
    5.82 -	btst	d0,0xFFFF8901:w	/* state DMA sound */
    5.83 +	btst	d0,0xFFFF8901:w
    5.84  #else
    5.85 -	btst	#0,0xFFFF8901:w	/* state DMA sound */
    5.86 +	btst	#0,0xFFFF8901:w
    5.87  #endif
    5.88  	beqs	SDL_MintAudio_EndIntMeasure
    5.89  	addql	#1,_SDL_MintAudio_clocktics
    5.90  SDL_MintAudio_EndIntMeasure:
    5.91 +
    5.92 +	/* Clear service bit */
    5.93  #if defined(__mcoldfire__)
    5.94  	moveql	#5,d0
    5.95 -	bclr	d0,0xFFFFFA0F:w	/* Clear service bit */
    5.96 +	bclr	d0,0xFFFFFA0F:w
    5.97  
    5.98  	movel	sp@+,d0
    5.99  #else
   5.100 -	bclr	#5,0xFFFFFA0F:w	/* Clear service bit */
   5.101 +	bclr	#5,0xFFFFFA0F:w
   5.102  #endif
   5.103  	rte
   5.104  
   5.105 -/*--- Xbios interrupt vector ---*/
   5.106 +/*--- Interrupt vectors ---*/
   5.107 +/*    Xbios, DMA 8 bits: need to set ISR on MFP */
   5.108 +/*    STFA: do not touch MFP */
   5.109  
   5.110  _SDL_MintAudio_XbiosInterrupt:
   5.111 -#if defined(__mcoldfire__)
   5.112 -	lea	sp@(-60),sp
   5.113 -	moveml	d0-d7/a0-a6,sp@
   5.114 -#else
   5.115 -	moveml	d0-d7/a0-a6,sp@-
   5.116 -#endif
   5.117 -
   5.118 -	/* Reenable interrupts, so other interrupts can work */
   5.119 -	movew	#0x2300,sr
   5.120 +_SDL_MintAudio_Dma8Interrupt:
   5.121  
   5.122  	/* Clear service bit, so other MFP interrupts can work */
   5.123  #if defined(__mcoldfire__)
   5.124 +	movel	d0,sp@-
   5.125 +
   5.126  	moveql	#5,d0
   5.127  	bclr	d0,0xfffffa0f:w
   5.128 +
   5.129 +	movel	sp@+,d0
   5.130  #else
   5.131  	bclr	#5,0xfffffa0f:w
   5.132  #endif
   5.133  
   5.134 -	/* Check if we are not already running */
   5.135 -	tstw	_SDL_MintAudio_mutex
   5.136 -	bne	SDL_MintAudio_XbiosEnd
   5.137 -
   5.138 -#if defined(__mcoldfire__)
   5.139 -	movew	_SDL_MintAudio_mutex,d0
   5.140 -	notl	d0
   5.141 -	movew	d0,_SDL_MintAudio_mutex
   5.142 -
   5.143 -	movew	_SDL_MintAudio_numbuf,d1
   5.144 -	eorl	#1,d1
   5.145 -	movew	d1,_SDL_MintAudio_numbuf
   5.146 -#else
   5.147 -	notw	_SDL_MintAudio_mutex
   5.148 -
   5.149 -	/* Swap buffers */
   5.150 -	eorw	#1,_SDL_MintAudio_numbuf
   5.151 -#endif
   5.152 -	
   5.153 -	/* Save FPU if needed */
   5.154 -	tstw	_SDL_MintAudio_hasfpu
   5.155 -	beqs	SDL_MintAudio_Xbios_nofpu1
   5.156 -	SAVE_FPU_CONTEXT
   5.157 -SDL_MintAudio_Xbios_nofpu1:
   5.158 -
   5.159 -	/* Callback */
   5.160 -	jsr	_SDL_MintAudio_Callback
   5.161 -
   5.162 -	/* Restore FPU if needed */
   5.163 -	tstw	_SDL_MintAudio_hasfpu
   5.164 -	beqs	SDL_MintAudio_Xbios_nofpu2
   5.165 -	RESTORE_FPU_CONTEXT
   5.166 -SDL_MintAudio_Xbios_nofpu2:
   5.167 +_SDL_MintAudio_StfaInterrupt:
   5.168 +	addql	#1,_SDL_MintAudio_num_its
   5.169  
   5.170 -	/* Reserve space for registers */
   5.171 -#if defined(__mcoldfire__)
   5.172 -	movel	#savamt,d0
   5.173 -	subl	d0,savptr
   5.174 -#else
   5.175 -	subl	#savamt,savptr
   5.176 -#endif
   5.177 -
   5.178 -	/* Set new buffer */
   5.179 -
   5.180 -	moveq	#0,d0
   5.181 -	movel	_SDL_MintAudio_audiosize,d1
   5.182 -
   5.183 -	movew	_SDL_MintAudio_numbuf,d0
   5.184 -	lsll	#2,d0
   5.185 -	lea	_SDL_MintAudio_audiobuf,a0
   5.186 -	movel	a0@(d0:l),a1
   5.187 -
   5.188 -	lea	a1@(d1:l),a2
   5.189 -
   5.190 -	movel	a2,sp@-
   5.191 -	movel	a1,sp@-
   5.192 -	clrw	sp@-
   5.193 -	movew	#131,sp@-
   5.194 -	trap	#14
   5.195 -	lea	sp@(12),sp
   5.196 -
   5.197 -	/* Restore registers space */
   5.198 -#if defined(__mcoldfire__)
   5.199 -	movel	#savamt,d0
   5.200 -	addl	d0,savptr
   5.201 -#else
   5.202 -	addl	#savamt,savptr
   5.203 -#endif
   5.204 -
   5.205 -	clrw	_SDL_MintAudio_mutex
   5.206 -SDL_MintAudio_XbiosEnd:
   5.207 -#if defined(__mcoldfire__)
   5.208 -	moveml	sp@,d0-d7/a0-a6
   5.209 -	lea	sp@(60),sp
   5.210 -#else
   5.211 -	moveml	sp@+,d0-d7/a0-a6
   5.212 -#endif
   5.213  	rte
   5.214  
   5.215 -/*--- DMA 8 bits interrupt vector ---*/
   5.216 -
   5.217 -_SDL_MintAudio_Dma8Interrupt:
   5.218 -#if defined(__mcoldfire__)
   5.219 -	lea	sp@(-16),sp
   5.220 -	moveml	d0-d1/a0-a1,sp@
   5.221 -#else
   5.222 -	moveml	d0-d1/a0-a1,sp@-
   5.223 -#endif
   5.224 -
   5.225 -	/* Reenable interrupts, so other interrupts can work */
   5.226 -	movew	#0x2300,sr
   5.227 -
   5.228 -	/* Clear service bit, so other MFP interrupts can work */
   5.229 -#if defined(__mcoldfire__)
   5.230 -	moveql	#5,d0
   5.231 -	bclr	d0,0xfffffa0f:w
   5.232 -#else
   5.233 -	bclr	#5,0xfffffa0f:w
   5.234 -#endif
   5.235 -	/* Check if we are not already running */
   5.236 -	tstw	_SDL_MintAudio_mutex
   5.237 -	bne	SDL_MintAudio_Dma8End
   5.238 -
   5.239 -#if defined(__mcoldfire__)
   5.240 -	movew	_SDL_MintAudio_mutex,d0
   5.241 -	notl	d0
   5.242 -	movew	d0,_SDL_MintAudio_mutex
   5.243 -
   5.244 -	movew	_SDL_MintAudio_numbuf,d1
   5.245 -	eorl	#1,d1
   5.246 -	movew	d1,_SDL_MintAudio_numbuf
   5.247 -#else
   5.248 -	notw	_SDL_MintAudio_mutex
   5.249 -
   5.250 -	/* Swap buffers */
   5.251 -	eorw	#1,_SDL_MintAudio_numbuf
   5.252 -#endif
   5.253 -
   5.254 -	/* Save FPU if needed */
   5.255 -	tstw	_SDL_MintAudio_hasfpu
   5.256 -	beqs	SDL_MintAudio_Dma8_nofpu1
   5.257 -	SAVE_FPU_CONTEXT
   5.258 -SDL_MintAudio_Dma8_nofpu1:
   5.259 -
   5.260 -	/* Callback */
   5.261 -	jsr	_SDL_MintAudio_Callback
   5.262 -
   5.263 -	/* Restore FPU if needed */
   5.264 -	tstw	_SDL_MintAudio_hasfpu
   5.265 -	beqs	SDL_MintAudio_Dma8_nofpu2
   5.266 -	RESTORE_FPU_CONTEXT
   5.267 -SDL_MintAudio_Dma8_nofpu2:
   5.268 -
   5.269 -	/* Set new buffer */
   5.270 -
   5.271 -	moveq	#0,d0
   5.272 -
   5.273 -	movew	_SDL_MintAudio_numbuf,d0
   5.274 -	lsll	#2,d0
   5.275 -	lea	_SDL_MintAudio_audiobuf,a0
   5.276 -	movel	a0@(d0:l),d1
   5.277 -
   5.278 -	/* Modify DMA addresses */
   5.279 -	lea	0xffff8900:w,a0
   5.280 -
   5.281 -	movel	d1,d0
   5.282 -
   5.283 -	moveb	d0,a0@(0x07)	/* Start address */
   5.284 -	lsrl	#8,d0
   5.285 -	moveb	d0,a0@(0x05)
   5.286 -	lsrl	#8,d0
   5.287 -	moveb	d0,a0@(0x03)
   5.288 -
   5.289 -	addl	_SDL_MintAudio_audiosize,d1
   5.290 -
   5.291 -	movel	d1,d0
   5.292 -
   5.293 -	moveb	d0,a0@(0x13)	/* End address */
   5.294 -	lsrl	#8,d0
   5.295 -	moveb	d0,a0@(0x11)
   5.296 -	lsrl	#8,d0
   5.297 -	moveb	d0,a0@(0x0f)
   5.298 +	.bss
   5.299  
   5.300 -	clrw	_SDL_MintAudio_mutex
   5.301 -SDL_MintAudio_Dma8End:
   5.302 -#if defined(__mcoldfire__)
   5.303 -	moveml	sp@,d0-d1/a0-a1
   5.304 -	lea	sp@(16),sp
   5.305 -#else
   5.306 -	moveml	sp@+,d0-d1/a0-a1
   5.307 -#endif
   5.308 -	rte
   5.309 -
   5.310 -/*--- STFA interrupt vector ---*/
   5.311 -
   5.312 -STFA_SOUND_START	=	6
   5.313 -STFA_SOUND_END		=	STFA_SOUND_START+8
   5.314 -
   5.315 -_SDL_MintAudio_StfaInterrupt:
   5.316 -	/* Reenable interrupts, so other interrupts can work */
   5.317 -	movew	#0x2300,sr
   5.318 -
   5.319 -	/* Check if we are not already running */
   5.320 -	tstw	_SDL_MintAudio_mutex
   5.321 -
   5.322 -#if defined(__mcoldfire__)
   5.323 -	bne	SDL_MintAudio_StfaEnd
   5.324 -
   5.325 -	lea	sp@(-60),sp
   5.326 -	moveml	d0-d7/a0-a6,sp@
   5.327 -
   5.328 -	movew	_SDL_MintAudio_mutex,d0
   5.329 -	notl	d0
   5.330 -	movew	d0,_SDL_MintAudio_mutex
   5.331 -
   5.332 -	movew	_SDL_MintAudio_numbuf,d1
   5.333 -	eorl	#1,d1
   5.334 -	movew	d1,_SDL_MintAudio_numbuf
   5.335 -#else
   5.336 -	bnes	SDL_MintAudio_StfaEnd
   5.337 -
   5.338 -	moveml	d0-d7/a0-a6,sp@-
   5.339 -
   5.340 -	notw	_SDL_MintAudio_mutex
   5.341 -
   5.342 -	/* Swap buffers */
   5.343 -	eorw	#1,_SDL_MintAudio_numbuf
   5.344 -#endif
   5.345 -	
   5.346 -	/* Save FPU if needed */
   5.347 -	tstw	_SDL_MintAudio_hasfpu
   5.348 -	beqs	SDL_MintAudio_Stfa_nofpu1
   5.349 -	SAVE_FPU_CONTEXT
   5.350 -SDL_MintAudio_Stfa_nofpu1:
   5.351 -
   5.352 -	/* Callback */
   5.353 -	jsr	_SDL_MintAudio_Callback
   5.354 -
   5.355 -	/* Restore FPU if needed */
   5.356 -	tstw	_SDL_MintAudio_hasfpu
   5.357 -	beqs	SDL_MintAudio_Stfa_nofpu2
   5.358 -	RESTORE_FPU_CONTEXT
   5.359 -SDL_MintAudio_Stfa_nofpu2:
   5.360 -
   5.361 -	/* Set new buffer */
   5.362 -
   5.363 -	moveq	#0,d0
   5.364 -	movel	_SDL_MintAudio_stfa,a1
   5.365 -
   5.366 -	movew	_SDL_MintAudio_numbuf,d0
   5.367 -	lsll	#2,d0
   5.368 -	lea	_SDL_MintAudio_audiobuf,a0
   5.369 -	movel	a0@(d0:l),d1
   5.370 -
   5.371 -	/* Modify STFA replay buffers */
   5.372 -	movel	d1,a1@(STFA_SOUND_START)
   5.373 -	addl	_SDL_MintAudio_audiosize,d1
   5.374 -	movel	d1,a1@(STFA_SOUND_END)
   5.375 -
   5.376 -#if defined(__mcoldfire__)
   5.377 -	moveml	sp@,d0-d7/a0-a6
   5.378 -	lea	sp@(60),sp
   5.379 -#else
   5.380 -	moveml	sp@+,d0-d7/a0-a6
   5.381 -#endif
   5.382 -	clrw	_SDL_MintAudio_mutex
   5.383 -SDL_MintAudio_StfaEnd:
   5.384 -	rte
   5.385 +	.even
   5.386 +	.comm	_SDL_MintAudio_clocktics,4
   5.387 +	.comm	_SDL_MintAudio_num_its,4
     6.1 --- a/src/audio/mint/SDL_mintaudio_mcsn.c	Mon Jul 28 20:40:41 2014 +0200
     6.2 +++ b/src/audio/mint/SDL_mintaudio_mcsn.c	Mon Jul 28 21:15:42 2014 +0200
     6.3 @@ -39,8 +39,6 @@
     6.4  #include "../SDL_audio_c.h"
     6.5  #include "../SDL_sysaudio.h"
     6.6  
     6.7 -#include "../../video/ataricommon/SDL_atarimxalloc_c.h"
     6.8 -
     6.9  #include "SDL_mintaudio.h"
    6.10  #include "SDL_mintaudio_mcsn.h"
    6.11  
    6.12 @@ -74,6 +72,7 @@
    6.13  /* To check/init hardware audio */
    6.14  static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
    6.15  static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
    6.16 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize);
    6.17  
    6.18  /*--- Audio driver bootstrap functions ---*/
    6.19  
    6.20 @@ -200,15 +199,7 @@
    6.21  		Jdisint(MFP_DMASOUND);
    6.22  	}
    6.23  
    6.24 -	/* Wait if currently playing sound */
    6.25 -	while (SDL_MintAudio_mutex != 0) {
    6.26 -	}
    6.27 -
    6.28 -	/* Clear buffers */
    6.29 -	if (SDL_MintAudio_audiobuf[0]) {
    6.30 -		Mfree(SDL_MintAudio_audiobuf[0]);
    6.31 -		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
    6.32 -	}
    6.33 +	SDL_MintAudio_FreeBuffers();
    6.34  
    6.35  	/* Unlock sound system */
    6.36  	Unlocksnd();
    6.37 @@ -297,7 +288,6 @@
    6.38  static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
    6.39  {
    6.40  	int channels_mode, prediv, dmaclock;
    6.41 -	void *buffer;
    6.42  
    6.43  	/* Stop currently playing sound */
    6.44  	SDL_MintAudio_quit_thread = SDL_FALSE;
    6.45 @@ -339,10 +329,7 @@
    6.46  	}
    6.47  
    6.48  	/* Set buffer */
    6.49 -	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
    6.50 -	if (Setbuffer(0, buffer, buffer + spec->size)<0) {
    6.51 -		DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
    6.52 -	}
    6.53 +	Mint_SwapBuffers(MINTAUDIO_audiobuf[0], MINTAUDIO_audiosize);
    6.54  	
    6.55  	if (SDL_MintAudio_mint_present) {
    6.56  		SDL_MintAudio_thread_pid = tfork(SDL_MintAudio_Thread, 0);
    6.57 @@ -377,29 +364,20 @@
    6.58  		return -1;
    6.59  	}
    6.60  
    6.61 -	SDL_CalculateAudioSpec(spec);
    6.62 -
    6.63 -	/* Allocate memory for audio buffers in DMA-able RAM */
    6.64 -	DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
    6.65 -
    6.66 -	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
    6.67 -	if (SDL_MintAudio_audiobuf[0]==NULL) {
    6.68 -		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
    6.69 -		return (-1);
    6.70 +	if (!SDL_MintAudio_InitBuffers(spec)) {
    6.71 +		return -1;
    6.72  	}
    6.73 -	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
    6.74 -	SDL_MintAudio_numbuf=0;
    6.75 -	SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
    6.76 -	SDL_MintAudio_audiosize = spec->size;
    6.77 -	SDL_MintAudio_mutex = 0;
    6.78 -
    6.79 -	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
    6.80 -	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
    6.81 -
    6.82 -	SDL_MintAudio_CheckFpu();
    6.83  
    6.84  	/* Setup audio hardware */
    6.85 +	MINTAUDIO_swapbuf = Mint_SwapBuffers;
    6.86  	Mint_InitAudio(this, spec);
    6.87  
    6.88      return(1);	/* We don't use SDL threaded audio */
    6.89  }
    6.90 +
    6.91 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize)
    6.92 +{
    6.93 +	unsigned long buffer = (unsigned long) nextbuf;
    6.94 +
    6.95 +	Setbuffer(0, buffer, buffer + nextsize);
    6.96 +}
     7.1 --- a/src/audio/mint/SDL_mintaudio_stfa.c	Mon Jul 28 20:40:41 2014 +0200
     7.2 +++ b/src/audio/mint/SDL_mintaudio_stfa.c	Mon Jul 28 21:15:42 2014 +0200
     7.3 @@ -37,8 +37,6 @@
     7.4  #include "../SDL_audio_c.h"
     7.5  #include "../SDL_sysaudio.h"
     7.6  
     7.7 -#include "../../video/ataricommon/SDL_atarimxalloc_c.h"
     7.8 -
     7.9  #include "SDL_mintaudio.h"
    7.10  #include "SDL_mintaudio_stfa.h"
    7.11  
    7.12 @@ -79,6 +77,7 @@
    7.13  /* To check/init hardware audio */
    7.14  static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
    7.15  static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
    7.16 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize);
    7.17  
    7.18  /*--- Audio driver bootstrap functions ---*/
    7.19  
    7.20 @@ -110,8 +109,6 @@
    7.21  	}
    7.22  	cookie_stfa = (cookie_stfa_t *) dummy;
    7.23  
    7.24 -	SDL_MintAudio_stfa = cookie_stfa;
    7.25 -
    7.26  	DEBUG_PRINT((DEBUG_NAME "STFA audio available!\n"));
    7.27  	return(1);
    7.28  }
    7.29 @@ -171,15 +168,7 @@
    7.30  {
    7.31  	cookie_stfa->sound_enable=STFA_PLAY_DISABLE;
    7.32  
    7.33 -	/* Wait if currently playing sound */
    7.34 -	while (SDL_MintAudio_mutex != 0) {
    7.35 -	}
    7.36 -
    7.37 -	/* Clear buffers */
    7.38 -	if (SDL_MintAudio_audiobuf[0]) {
    7.39 -		Mfree(SDL_MintAudio_audiobuf[0]);
    7.40 -		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
    7.41 -	}
    7.42 +	SDL_MintAudio_FreeBuffers();
    7.43  }
    7.44  
    7.45  static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
    7.46 @@ -225,10 +214,6 @@
    7.47  
    7.48  static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
    7.49  {
    7.50 -	void *buffer;
    7.51 -
    7.52 -	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
    7.53 -
    7.54  	/* Stop replay */
    7.55  	cookie_stfa->sound_enable=STFA_PLAY_DISABLE;
    7.56  
    7.57 @@ -256,8 +241,7 @@
    7.58  	}
    7.59  
    7.60  	/* Set buffer */
    7.61 -	cookie_stfa->sound_start = (unsigned long) buffer;
    7.62 -	cookie_stfa->sound_end = (unsigned long) (buffer + spec->size);
    7.63 +	Mint_SwapBuffers(MINTAUDIO_audiobuf[0], MINTAUDIO_audiosize);
    7.64  
    7.65  	/* Set interrupt */
    7.66  	cookie_stfa->stfa_it = SDL_MintAudio_StfaInterrupt;
    7.67 @@ -277,29 +261,19 @@
    7.68  		return -1;
    7.69  	}
    7.70  
    7.71 -	SDL_CalculateAudioSpec(spec);
    7.72 -
    7.73 -	/* Allocate memory for audio buffers in DMA-able RAM */
    7.74 -	DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
    7.75 -
    7.76 -	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
    7.77 -	if (SDL_MintAudio_audiobuf[0]==NULL) {
    7.78 -		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
    7.79 -		return (-1);
    7.80 +	if (!SDL_MintAudio_InitBuffers(spec)) {
    7.81 +		return -1;
    7.82  	}
    7.83 -	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
    7.84 -	SDL_MintAudio_numbuf=0;
    7.85 -	SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
    7.86 -	SDL_MintAudio_audiosize = spec->size;
    7.87 -	SDL_MintAudio_mutex = 0;
    7.88 -
    7.89 -	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
    7.90 -	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
    7.91 -
    7.92 -	SDL_MintAudio_CheckFpu();
    7.93  
    7.94  	/* Setup audio hardware */
    7.95 +	MINTAUDIO_swapbuf = Mint_SwapBuffers;
    7.96  	Mint_InitAudio(this, spec);
    7.97  
    7.98      return(1);	/* We don't use threaded audio */
    7.99  }
   7.100 +
   7.101 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize)
   7.102 +{
   7.103 +	cookie_stfa->sound_start = (unsigned long) nextbuf;
   7.104 +	cookie_stfa->sound_end = cookie_stfa->sound_start + nextsize;
   7.105 +}
     8.1 --- a/src/audio/mint/SDL_mintaudio_xbios.c	Mon Jul 28 20:40:41 2014 +0200
     8.2 +++ b/src/audio/mint/SDL_mintaudio_xbios.c	Mon Jul 28 21:15:42 2014 +0200
     8.3 @@ -75,6 +75,7 @@
     8.4  /* To check/init hardware audio */
     8.5  static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
     8.6  static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
     8.7 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize);
     8.8  
     8.9  /*--- Audio driver bootstrap functions ---*/
    8.10  
    8.11 @@ -184,15 +185,7 @@
    8.12  		Jdisint(MFP_DMASOUND);
    8.13  	}
    8.14  
    8.15 -	/* Wait if currently playing sound */
    8.16 -	while (SDL_MintAudio_mutex != 0) {
    8.17 -	}
    8.18 -
    8.19 -	/* Clear buffers */
    8.20 -	if (SDL_MintAudio_audiobuf[0]) {
    8.21 -		Mfree(SDL_MintAudio_audiobuf[0]);
    8.22 -		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
    8.23 -	}
    8.24 +	SDL_MintAudio_FreeBuffers();
    8.25  
    8.26  	/* Unlock sound system */
    8.27  	Unlocksnd();
    8.28 @@ -386,7 +379,6 @@
    8.29  static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
    8.30  {
    8.31  	int channels_mode, prediv;
    8.32 -	void *buffer;
    8.33  
    8.34  	/* Stop currently playing sound */
    8.35  	SDL_MintAudio_quit_thread = SDL_FALSE;
    8.36 @@ -423,18 +415,15 @@
    8.37  	}
    8.38  
    8.39  	/* Set buffer */
    8.40 -	buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
    8.41 -	if (Setbuffer(0, buffer, buffer + spec->size)<0) {
    8.42 -		DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
    8.43 -	}
    8.44 +	Mint_SwapBuffers(MINTAUDIO_audiobuf[0], MINTAUDIO_audiosize);
    8.45  	
    8.46  	if (SDL_MintAudio_mint_present) {
    8.47  		SDL_MintAudio_thread_pid = tfork(SDL_MintAudio_Thread, 0);
    8.48  	} else {
    8.49  		/* Install interrupt */
    8.50  		Jdisint(MFP_DMASOUND);
    8.51 -		/*Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt);*/
    8.52 -		Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_Dma8Interrupt);
    8.53 +		Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt);
    8.54 +		/*Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_Dma8Interrupt);*/
    8.55  		Jenabint(MFP_DMASOUND);
    8.56  
    8.57  		if (Setinterrupt(SI_TIMERA, SI_PLAY)<0) {
    8.58 @@ -462,29 +451,20 @@
    8.59  		return -1;
    8.60  	}
    8.61  
    8.62 -	SDL_CalculateAudioSpec(spec);
    8.63 -
    8.64 -	/* Allocate memory for audio buffers in DMA-able RAM */
    8.65 -	DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
    8.66 -
    8.67 -	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
    8.68 -	if (SDL_MintAudio_audiobuf[0]==NULL) {
    8.69 -		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
    8.70 -		return (-1);
    8.71 +	if (!SDL_MintAudio_InitBuffers(spec)) {
    8.72 +		return -1;
    8.73  	}
    8.74 -	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
    8.75 -	SDL_MintAudio_numbuf=0;
    8.76 -	SDL_memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
    8.77 -	SDL_MintAudio_audiosize = spec->size;
    8.78 -	SDL_MintAudio_mutex = 0;
    8.79 -
    8.80 -	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
    8.81 -	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
    8.82 -
    8.83 -	SDL_MintAudio_CheckFpu();
    8.84  
    8.85  	/* Setup audio hardware */
    8.86 +	MINTAUDIO_swapbuf = Mint_SwapBuffers;
    8.87  	Mint_InitAudio(this, spec);
    8.88  
    8.89      return(1);	/* We don't use SDL threaded audio */
    8.90  }
    8.91 +
    8.92 +static void Mint_SwapBuffers(Uint8 *nextbuf, int nextsize)
    8.93 +{
    8.94 +	unsigned long buffer = (unsigned long) nextbuf;
    8.95 +
    8.96 +	Setbuffer(0, buffer, buffer + nextsize);
    8.97 +}
     9.1 --- a/src/video/ataricommon/SDL_atarievents.c	Mon Jul 28 20:40:41 2014 +0200
     9.2 +++ b/src/video/ataricommon/SDL_atarievents.c	Mon Jul 28 21:15:42 2014 +0200
     9.3 @@ -42,6 +42,8 @@
     9.4  #include "SDL_gemdosevents_c.h"
     9.5  #include "SDL_ikbdevents_c.h"
     9.6  
     9.7 +/* from src/audio/mint/SDL_mintaudio.c */
     9.8 +void SDL_AtariMint_UpdateAudio(void);
     9.9  /* from src/timer/mint/SDL_systimer.c */
    9.10  void SDL_AtariMint_CheckTimer(void);
    9.11  
    9.12 @@ -239,5 +241,6 @@
    9.13  
    9.14  void SDL_AtariMint_BackgroundTasks(void)
    9.15  {
    9.16 +	SDL_AtariMint_UpdateAudio();
    9.17  	if (SDL_timer_running) SDL_AtariMint_CheckTimer();
    9.18  }