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