src/audio/mint/SDL_mintaudio_dma8.c
changeset 644 594422ab8f9f
child 704 c4803992e09c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/mint/SDL_mintaudio_dma8.c	Mon Jul 07 19:16:03 2003 +0000
     1.3 @@ -0,0 +1,354 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +/*
    1.27 +	MiNT audio driver
    1.28 +	using DMA 8bits (hardware access)
    1.29 +
    1.30 +	Patrice Mandin
    1.31 +*/
    1.32 +
    1.33 +#include <stdlib.h>
    1.34 +#include <stdio.h>
    1.35 +#include <string.h>
    1.36 +
    1.37 +/* Mint includes */
    1.38 +#include <mint/osbind.h>
    1.39 +#include <mint/falcon.h>
    1.40 +#include <mint/cookie.h>
    1.41 +
    1.42 +#include "SDL_endian.h"
    1.43 +#include "SDL_audio.h"
    1.44 +#include "SDL_audio_c.h"
    1.45 +#include "SDL_audiomem.h"
    1.46 +#include "SDL_sysaudio.h"
    1.47 +
    1.48 +#include "SDL_atarimxalloc_c.h"
    1.49 +
    1.50 +#include "SDL_mintaudio.h"
    1.51 +#include "SDL_mintaudio_dma8.h"
    1.52 +
    1.53 +/*--- Defines ---*/
    1.54 +
    1.55 +#define MINT_AUDIO_DRIVER_NAME "mint_dma8"
    1.56 +
    1.57 +/* Debug print info */
    1.58 +#define DEBUG_NAME "audio:dma8: "
    1.59 +#if 0
    1.60 +#define DEBUG_PRINT(what) \
    1.61 +	{ \
    1.62 +		printf what; \
    1.63 +	}
    1.64 +#else
    1.65 +#define DEBUG_PRINT(what)
    1.66 +#endif
    1.67 +
    1.68 +/*--- Static variables ---*/
    1.69 +
    1.70 +static unsigned long cookie_snd, cookie_mch;
    1.71 +
    1.72 +/*--- Audio driver functions ---*/
    1.73 +
    1.74 +static void Mint_CloseAudio(_THIS);
    1.75 +static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec);
    1.76 +static void Mint_LockAudio(_THIS);
    1.77 +static void Mint_UnlockAudio(_THIS);
    1.78 +
    1.79 +/* To check/init hardware audio */
    1.80 +static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec);
    1.81 +static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec);
    1.82 +
    1.83 +/*--- Audio driver bootstrap functions ---*/
    1.84 +
    1.85 +static int Audio_Available(void)
    1.86 +{
    1.87 +	const char *envr = getenv("SDL_AUDIODRIVER");
    1.88 +
    1.89 +	/* Check if user asked a different audio driver */
    1.90 +	if ((envr) && (strcmp(envr, MINT_AUDIO_DRIVER_NAME)!=0)) {
    1.91 +		DEBUG_PRINT((DEBUG_NAME "user asked a different audio driver\n"));
    1.92 +		return 0;
    1.93 +	}
    1.94 +
    1.95 +	/* Cookie _MCH present ? if not, assume ST machine */
    1.96 +	if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
    1.97 +		cookie_mch = MCH_ST;
    1.98 +	}
    1.99 +
   1.100 +	/* Cookie _SND present ? if not, assume ST machine */
   1.101 +	if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
   1.102 +		cookie_snd = SND_PSG;
   1.103 +	}
   1.104 +
   1.105 +	/* Check if we have 8 bits audio */
   1.106 +	if ((cookie_snd & SND_8BIT)==0) {
   1.107 +		DEBUG_PRINT((DEBUG_NAME "no 8 bits sound\n"));
   1.108 +	    return(0);
   1.109 +	}
   1.110 +
   1.111 +	if ((cookie_mch>>16)>MCH_F30) {
   1.112 +		DEBUG_PRINT((DEBUG_NAME "unknown 8 bits audio chip\n"));
   1.113 +		return 0;
   1.114 +	}
   1.115 +
   1.116 +	/* Check if audio is lockable */
   1.117 +	if ((cookie_mch>>16) == MCH_F30) {
   1.118 +		if (Locksnd()!=1) {
   1.119 +			DEBUG_PRINT((DEBUG_NAME "audio locked by other application\n"));
   1.120 +			return(0);
   1.121 +		}
   1.122 +
   1.123 +		Unlocksnd();
   1.124 +	}
   1.125 +
   1.126 +	DEBUG_PRINT((DEBUG_NAME "8 bits audio available!\n"));
   1.127 +	return(1);
   1.128 +}
   1.129 +
   1.130 +static void Audio_DeleteDevice(SDL_AudioDevice *device)
   1.131 +{
   1.132 +    free(device->hidden);
   1.133 +    free(device);
   1.134 +}
   1.135 +
   1.136 +static SDL_AudioDevice *Audio_CreateDevice(int devindex)
   1.137 +{
   1.138 +	SDL_AudioDevice *this;
   1.139 +
   1.140 +	/* Initialize all variables that we clean on shutdown */
   1.141 +	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
   1.142 +    if ( this ) {
   1.143 +        memset(this, 0, (sizeof *this));
   1.144 +        this->hidden = (struct SDL_PrivateAudioData *)
   1.145 +                malloc((sizeof *this->hidden));
   1.146 +    }
   1.147 +    if ( (this == NULL) || (this->hidden == NULL) ) {
   1.148 +        SDL_OutOfMemory();
   1.149 +        if ( this ) {
   1.150 +            free(this);
   1.151 +        }
   1.152 +        return(0);
   1.153 +    }
   1.154 +    memset(this->hidden, 0, (sizeof *this->hidden));
   1.155 +
   1.156 +    /* Set the function pointers */
   1.157 +    this->OpenAudio   = Mint_OpenAudio;
   1.158 +    this->CloseAudio  = Mint_CloseAudio;
   1.159 +    this->LockAudio   = Mint_LockAudio;
   1.160 +    this->UnlockAudio = Mint_UnlockAudio;
   1.161 +    this->free        = Audio_DeleteDevice;
   1.162 +
   1.163 +    return this;
   1.164 +}
   1.165 +
   1.166 +AudioBootStrap MINTAUDIO_DMA8_bootstrap = {
   1.167 +	MINT_AUDIO_DRIVER_NAME, "MiNT DMA 8 bits audio driver",
   1.168 +	Audio_Available, Audio_CreateDevice
   1.169 +};
   1.170 +
   1.171 +static void Mint_LockAudio(_THIS)
   1.172 +{
   1.173 +	void *oldpile;
   1.174 +
   1.175 +	/* Stop replay */
   1.176 +	oldpile=(void *)Super(0);
   1.177 +	DMAAUDIO_IO.control=0;
   1.178 +	Super(oldpile);
   1.179 +}
   1.180 +
   1.181 +static void Mint_UnlockAudio(_THIS)
   1.182 +{
   1.183 +	void *oldpile;
   1.184 +
   1.185 +	/* Restart replay */
   1.186 +	oldpile=(void *)Super(0);
   1.187 +	DMAAUDIO_IO.control=3;
   1.188 +	Super(oldpile);
   1.189 +}
   1.190 +
   1.191 +static void Mint_CloseAudio(_THIS)
   1.192 +{
   1.193 +	void *oldpile;
   1.194 +
   1.195 +	/* Stop replay */
   1.196 +	oldpile=(void *)Super(0);
   1.197 +	DMAAUDIO_IO.control=0;
   1.198 +	Super(oldpile);
   1.199 +
   1.200 +	DEBUG_PRINT((DEBUG_NAME "closeaudio: replay stopped\n"));
   1.201 +
   1.202 +	/* Disable interrupt */
   1.203 +	Jdisint(MFP_DMASOUND);
   1.204 +
   1.205 +	DEBUG_PRINT((DEBUG_NAME "closeaudio: interrupt disabled\n"));
   1.206 +
   1.207 +	/* Wait if currently playing sound */
   1.208 +	while (SDL_MintAudio_mutex != 0) {
   1.209 +	}
   1.210 +
   1.211 +	DEBUG_PRINT((DEBUG_NAME "closeaudio: no more interrupt running\n"));
   1.212 +
   1.213 +	/* Clear buffers */
   1.214 +	if (SDL_MintAudio_audiobuf[0]) {
   1.215 +		Mfree(SDL_MintAudio_audiobuf[0]);
   1.216 +		SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
   1.217 +	}
   1.218 +
   1.219 +	DEBUG_PRINT((DEBUG_NAME "closeaudio: buffers freed\n"));
   1.220 +}
   1.221 +
   1.222 +static int Mint_CheckAudio(_THIS, SDL_AudioSpec *spec)
   1.223 +{
   1.224 +	int i, masterprediv;
   1.225 +	unsigned long masterclock;
   1.226 +
   1.227 +	DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",spec->format & 0x00ff));
   1.228 +	DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
   1.229 +	DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x8000)!=0)));
   1.230 +	DEBUG_PRINT(("channels=%d, ", spec->channels));
   1.231 +	DEBUG_PRINT(("freq=%d\n", spec->freq));
   1.232 +
   1.233 +	/* Check formats available */
   1.234 +	spec->format = AUDIO_S8;
   1.235 +	
   1.236 +	/* Calculate and select the closest frequency */
   1.237 +	MINTAUDIO_nfreq=4;
   1.238 +	MINTAUDIO_sfreq=0;
   1.239 +	masterclock=MASTERCLOCK_STE;
   1.240 +	masterprediv=MASTERPREDIV_STE;
   1.241 +	switch(cookie_mch>>16) {
   1.242 +/*
   1.243 +		case MCH_STE:
   1.244 +			masterclock=MASTERCLOCK_STE;
   1.245 +			masterprediv=MASTERPREDIV_STE;
   1.246 +			break;
   1.247 +*/
   1.248 +		case MCH_TT:
   1.249 +			masterclock=MASTERCLOCK_TT;
   1.250 +			masterprediv=MASTERPREDIV_TT;
   1.251 +			break;
   1.252 +		case MCH_F30:
   1.253 +			masterclock=MASTERCLOCK_FALCON1;
   1.254 +			masterprediv=MASTERPREDIV_FALCON<<1;
   1.255 +			MINTAUDIO_nfreq=3;
   1.256 +			MINTAUDIO_sfreq=1;
   1.257 +			break;
   1.258 +	}
   1.259 +	for (i=MINTAUDIO_sfreq;i<MINTAUDIO_nfreq;i++) {
   1.260 +		MINTAUDIO_hardfreq[i]=masterclock/(masterprediv*(1<<i));
   1.261 +		DEBUG_PRINT((DEBUG_NAME "calc:freq(%d)=%lu\n", i, MINTAUDIO_hardfreq[i]));
   1.262 +	}
   1.263 +
   1.264 +	MINTAUDIO_numfreq=SDL_MintAudio_SearchFrequency(this, 0, spec->freq);
   1.265 +	spec->freq=MINTAUDIO_hardfreq[MINTAUDIO_numfreq];
   1.266 +
   1.267 +	DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",spec->format & 0x00ff));
   1.268 +	DEBUG_PRINT(("signed=%d, ", ((spec->format & 0x8000)!=0)));
   1.269 +	DEBUG_PRINT(("big endian=%d, ", ((spec->format & 0x8000)!=0)));
   1.270 +	DEBUG_PRINT(("channels=%d, ", spec->channels));
   1.271 +	DEBUG_PRINT(("freq=%d\n", spec->freq));
   1.272 +
   1.273 +	return 0;
   1.274 +}
   1.275 +
   1.276 +static void Mint_InitAudio(_THIS, SDL_AudioSpec *spec)
   1.277 +{
   1.278 +	void *oldpile;
   1.279 +	unsigned long buffer;
   1.280 +	unsigned char mode;
   1.281 +	
   1.282 +	/* Set replay tracks */
   1.283 +	if (cookie_snd & SND_16BIT) {
   1.284 +		Settracks(0,0);
   1.285 +		Setmontracks(0);
   1.286 +	}
   1.287 +
   1.288 +	oldpile=(void *)Super(0);
   1.289 +
   1.290 +	/* Stop currently playing sound */
   1.291 +	DMAAUDIO_IO.control=0;
   1.292 +
   1.293 +	/* Set buffer */
   1.294 +	buffer = (unsigned long) SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
   1.295 +	DMAAUDIO_IO.start_high = (buffer>>16) & 255;
   1.296 +	DMAAUDIO_IO.start_mid = (buffer>>8) & 255;
   1.297 +	DMAAUDIO_IO.start_low = buffer & 255;
   1.298 +
   1.299 +	buffer += SDL_MintAudio_audiosize;
   1.300 +	DMAAUDIO_IO.end_high = (buffer>>16) & 255;
   1.301 +	DMAAUDIO_IO.end_mid = (buffer>>8) & 255;
   1.302 +	DMAAUDIO_IO.end_low = buffer & 255;
   1.303 +
   1.304 +	mode = 3-MINTAUDIO_numfreq;
   1.305 +	if (spec->channels==1) {
   1.306 +		mode |= 1<<7;
   1.307 +	}
   1.308 +	DMAAUDIO_IO.mode = mode;	
   1.309 +
   1.310 +	/* Set interrupt */
   1.311 +	Jdisint(MFP_DMASOUND);
   1.312 +	Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_Dma8Interrupt);
   1.313 +	Jenabint(MFP_DMASOUND);
   1.314 +
   1.315 +	/* Go */
   1.316 +	DMAAUDIO_IO.control = 3;	/* playback + repeat */
   1.317 +
   1.318 +	Super(oldpile);
   1.319 +}
   1.320 +
   1.321 +static int Mint_OpenAudio(_THIS, SDL_AudioSpec *spec)
   1.322 +{
   1.323 +	SDL_MintAudio_device = this;
   1.324 +
   1.325 +	/* Check audio capabilities */
   1.326 +	if (Mint_CheckAudio(this, spec)==-1) {
   1.327 +		return -1;
   1.328 +	}
   1.329 +
   1.330 +	SDL_CalculateAudioSpec(spec);
   1.331 +
   1.332 +	/* Allocate memory for audio buffers in DMA-able RAM */
   1.333 +	spec->size = spec->samples;
   1.334 +	spec->size *= spec->channels;
   1.335 +	spec->size *= (spec->format & 0xFF)/8;
   1.336 +
   1.337 +	DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", spec->size));
   1.338 +
   1.339 +	SDL_MintAudio_audiobuf[0] = Atari_SysMalloc(spec->size *2, MX_STRAM);
   1.340 +	if (SDL_MintAudio_audiobuf[0]==NULL) {
   1.341 +		SDL_SetError("MINT_OpenAudio: Not enough memory for audio buffer");
   1.342 +		return (-1);
   1.343 +	}
   1.344 +	SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + spec->size ;
   1.345 +	SDL_MintAudio_numbuf=0;
   1.346 +	memset(SDL_MintAudio_audiobuf[0], spec->silence, spec->size *2);
   1.347 +	SDL_MintAudio_audiosize = spec->size;
   1.348 +	SDL_MintAudio_mutex = 0;
   1.349 +
   1.350 +	DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n", SDL_MintAudio_audiobuf[0]));
   1.351 +	DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n", SDL_MintAudio_audiobuf[1]));
   1.352 +
   1.353 +	/* Setup audio hardware */
   1.354 +	Mint_InitAudio(this, spec);
   1.355 +
   1.356 +    return(1);	/* We don't use threaded audio */
   1.357 +}