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