src/audio/mint/SDL_mintaudio_xbios.c
author Sam Lantinga
Sat, 19 Sep 2009 13:29:40 +0000
changeset 3280 00cace2d9080
parent 2942 1e431c2631ee
permissions -rw-r--r--
Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
patmandin@644
     1
/*
patmandin@644
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     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
icculus@2049
    63
static unsigned long cookie_snd = 0;
patmandin@644
    64
slouken@1895
    65
static void
icculus@2049
    66
MINTXBIOS_LockDevice(_THIS)
patmandin@644
    67
{
slouken@1895
    68
    /* Stop replay */
slouken@1895
    69
    Buffoper(0);
patmandin@644
    70
}
patmandin@644
    71
slouken@1895
    72
static void
icculus@2049
    73
MINTXBIOS_UnlockDevice(_THIS)
patmandin@644
    74
{
slouken@1895
    75
    /* Restart replay */
slouken@1895
    76
    Buffoper(SB_PLA_ENA | SB_PLA_RPT);
patmandin@644
    77
}
patmandin@644
    78
slouken@1895
    79
static void
icculus@2049
    80
MINTXBIOS_CloseDevice(_THIS)
patmandin@644
    81
{
icculus@2049
    82
    if (this->hidden != NULL) {
icculus@2049
    83
        /* Stop replay */
icculus@2049
    84
        SDL_MintAudio_WaitThread();
icculus@2049
    85
        Buffoper(0);
patmandin@644
    86
icculus@2049
    87
        if (!SDL_MintAudio_mint_present) {
icculus@2049
    88
            /* Uninstall interrupt */
icculus@2049
    89
            Jdisint(MFP_DMASOUND);
icculus@2049
    90
        }
patmandin@644
    91
icculus@2049
    92
        /* Wait if currently playing sound */
slouken@2060
    93
        while (SDL_MintAudio_mutex != 0) {
slouken@2060
    94
        }
patmandin@644
    95
icculus@2049
    96
        /* Clear buffers */
icculus@2049
    97
        if (SDL_MintAudio_audiobuf[0]) {
icculus@2049
    98
            Mfree(SDL_MintAudio_audiobuf[0]);
icculus@2049
    99
            SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
icculus@2049
   100
        }
icculus@2049
   101
icculus@2049
   102
        /* Unlock sound system */
icculus@2049
   103
        Unlocksnd();
icculus@2049
   104
icculus@2049
   105
        SDL_free(this->hidden);
icculus@2049
   106
        this->hidden = NULL;
slouken@1895
   107
    }
patmandin@644
   108
}
patmandin@644
   109
patmandin@961
   110
/* Falcon XBIOS implementation of Devconnect() is buggy with external clock */
slouken@1895
   111
static void
slouken@1895
   112
Devconnect2(int src, int dst, int sclk, int pre)
slouken@1895
   113
{
slouken@1895
   114
    static const unsigned short MASK1[3] = { 0, 0x6000, 0 };
slouken@1895
   115
    static const unsigned short MASK2[4] = { 0xFFF0, 0xFF8F, 0xF0FF, 0x0FFF };
slouken@1895
   116
    static const unsigned short INDEX1[4] = { 1, 3, 5, 7 };
slouken@1895
   117
    static const unsigned short INDEX2[4] = { 0, 2, 4, 6 };
slouken@1895
   118
    unsigned short sync_div, dev_ctrl, dest_ctrl;
slouken@1895
   119
    void *oldstack;
patmandin@961
   120
slouken@1895
   121
    if (dst == 0) {
slouken@1895
   122
        return;
slouken@1895
   123
    }
patmandin@961
   124
slouken@1895
   125
    oldstack = (void *) Super(0);
slouken@1895
   126
slouken@1895
   127
    dev_ctrl = DMAAUDIO_IO.dev_ctrl;
slouken@1895
   128
    dest_ctrl = DMAAUDIO_IO.dest_ctrl;
slouken@1895
   129
    dev_ctrl &= MASK2[src];
patmandin@961
   130
slouken@1895
   131
    if (src == ADC) {
slouken@1895
   132
        dev_ctrl |= MASK1[sclk];
slouken@1895
   133
    } else {
slouken@1895
   134
        dev_ctrl |= (INDEX1[sclk] << (src << 4));
slouken@1895
   135
    }
patmandin@961
   136
slouken@1895
   137
    if (dst & DMAREC) {
slouken@1895
   138
        dest_ctrl &= 0xFFF0;
slouken@1895
   139
        dest_ctrl |= INDEX1[src];
slouken@1895
   140
    }
patmandin@961
   141
slouken@1895
   142
    if (dst & DSPRECV) {
slouken@1895
   143
        dest_ctrl &= 0xFF8F;
slouken@1895
   144
        dest_ctrl |= (INDEX1[src] << 4);
slouken@1895
   145
    }
patmandin@961
   146
slouken@1895
   147
    if (dst & EXTOUT) {
slouken@1895
   148
        dest_ctrl &= 0xF0FF;
slouken@1895
   149
        dest_ctrl |= (INDEX1[src] << 8);
slouken@1895
   150
    }
patmandin@961
   151
slouken@1895
   152
    if (dst & DAC) {
slouken@1895
   153
        dev_ctrl &= 0x0FFF;
slouken@1895
   154
        dev_ctrl |= MASK1[sclk];
slouken@1895
   155
        dest_ctrl &= 0x0FFF;
slouken@1895
   156
        dest_ctrl |= (INDEX2[src] << 12);
slouken@1895
   157
    }
patmandin@961
   158
slouken@1895
   159
    sync_div = DMAAUDIO_IO.sync_div;
slouken@1895
   160
    if (sclk == CLKEXT) {
slouken@1895
   161
        pre <<= 8;
slouken@1895
   162
        sync_div &= 0xF0FF;
slouken@1895
   163
    } else {
slouken@1895
   164
        sync_div &= 0xFFF0;
slouken@1895
   165
    }
slouken@1895
   166
    sync_div |= pre;
patmandin@961
   167
slouken@1895
   168
    DMAAUDIO_IO.dev_ctrl = dev_ctrl;
slouken@1895
   169
    DMAAUDIO_IO.dest_ctrl = dest_ctrl;
slouken@1895
   170
    DMAAUDIO_IO.sync_div = sync_div;
patmandin@961
   171
slouken@1895
   172
    Super(oldstack);
patmandin@961
   173
}
patmandin@961
   174
slouken@1895
   175
static void
icculus@2049
   176
MINTXBIOS_CheckExternalClock(_THIS)
patmandin@961
   177
{
patmandin@961
   178
#define SIZE_BUF_CLOCK_MEASURE (44100/10)
patmandin@961
   179
slouken@1895
   180
    unsigned long cookie_snd;
slouken@1895
   181
    char *buffer;
slouken@1895
   182
    int i, j;
patmandin@961
   183
slouken@1895
   184
    /* DSP present with its GPIO port ? */
slouken@1895
   185
    if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
slouken@1895
   186
        return;
slouken@1895
   187
    }
slouken@1895
   188
    if ((cookie_snd & SND_DSP) == 0) {
slouken@1895
   189
        return;
slouken@1895
   190
    }
slouken@1895
   191
slouken@1895
   192
    buffer = Atari_SysMalloc(SIZE_BUF_CLOCK_MEASURE, MX_STRAM);
slouken@1895
   193
    if (buffer == NULL) {
slouken@1895
   194
        DEBUG_PRINT((DEBUG_NAME "Not enough memory for the measure\n"));
slouken@1895
   195
        return;
slouken@1895
   196
    }
slouken@1895
   197
    SDL_memset(buffer, 0, SIZE_BUF_CLOCK_MEASURE);
patmandin@961
   198
slouken@1895
   199
    Buffoper(0);
slouken@1895
   200
    Settracks(0, 0);
slouken@1895
   201
    Setmontracks(0);
slouken@1895
   202
    Setmode(MONO8);
slouken@1895
   203
    Jdisint(MFP_TIMERA);
patmandin@961
   204
slouken@1895
   205
    for (i = 0; i < 2; i++) {
slouken@1895
   206
        Gpio(GPIO_SET, 7);      /* DSP port gpio outputs */
slouken@1895
   207
        Gpio(GPIO_WRITE, 2 + i);        /* 22.5792/24.576 MHz for 44.1/48KHz */
slouken@1895
   208
        Devconnect2(DMAPLAY, DAC, CLKEXT, CLK50K);      /* Matrix and clock source */
slouken@1895
   209
        Setbuffer(0, buffer, buffer + SIZE_BUF_CLOCK_MEASURE);  /* Set buffer */
slouken@1895
   210
        Xbtimer(XB_TIMERA, 5, 38, SDL_MintAudio_XbiosInterruptMeasureClock);    /* delay mode timer A, prediv /64, 1KHz */
slouken@1895
   211
        Jenabint(MFP_TIMERA);
slouken@1895
   212
        SDL_MintAudio_clocktics = 0;
slouken@1895
   213
        Buffoper(SB_PLA_ENA);
slouken@1895
   214
        usleep(110000);
slouken@1895
   215
slouken@1895
   216
        if ((Buffoper(-1) & 1) == 0) {
slouken@1895
   217
            if (SDL_MintAudio_clocktics) {
slouken@1895
   218
                unsigned long khz;
patmandin@961
   219
slouken@1895
   220
                khz =
slouken@1895
   221
                    ((SIZE_BUF_CLOCK_MEASURE /
slouken@1895
   222
                      SDL_MintAudio_clocktics) + 1) & 0xFFFFFFFE;
slouken@1895
   223
                DEBUG_PRINT((DEBUG_NAME "measure %d: freq=%lu KHz\n",
slouken@1895
   224
                             i + 1, khz));
patmandin@961
   225
slouken@1895
   226
                if (khz == 44) {
slouken@1895
   227
                    for (j = 1; j < 4; j++) {
slouken@1895
   228
                        SDL_MintAudio_AddFrequency(this,
slouken@1895
   229
                                                   MASTERCLOCK_44K
slouken@1895
   230
                                                   /
slouken@1895
   231
                                                   (MASTERPREDIV_FALCON
slouken@1895
   232
                                                    * (1 << j)),
slouken@1895
   233
                                                   MASTERCLOCK_44K,
slouken@1895
   234
                                                   (1 << j) - 1, 2 + i);
slouken@1895
   235
                    }
slouken@1895
   236
                } else if (khz == 48) {
slouken@1895
   237
                    for (j = 1; j < 4; j++) {
slouken@1895
   238
                        SDL_MintAudio_AddFrequency(this,
slouken@1895
   239
                                                   MASTERCLOCK_48K
slouken@1895
   240
                                                   /
slouken@1895
   241
                                                   (MASTERPREDIV_FALCON
slouken@1895
   242
                                                    * (1 << j)),
slouken@1895
   243
                                                   MASTERCLOCK_48K,
slouken@1895
   244
                                                   (1 << j) - 1, 2 + i);
slouken@1895
   245
                    }
slouken@1895
   246
                }
slouken@1895
   247
            } else {
slouken@1895
   248
                DEBUG_PRINT((DEBUG_NAME "No measure\n"));
slouken@1895
   249
            }
slouken@1895
   250
        } else {
slouken@1895
   251
            DEBUG_PRINT((DEBUG_NAME "No SDMA clock\n"));
slouken@1895
   252
        }
patmandin@961
   253
slouken@1895
   254
        Buffoper(0);            /* stop */
slouken@1895
   255
        Jdisint(MFP_TIMERA);    /* Uninstall interrupt */
slouken@1895
   256
    }
patmandin@961
   257
slouken@1895
   258
    Mfree(buffer);
patmandin@961
   259
}
patmandin@961
   260
slouken@1895
   261
static int
icculus@2049
   262
MINTXBIOS_CheckAudio(_THIS)
patmandin@644
   263
{
slouken@1895
   264
    int i;
slouken@1895
   265
    Uint32 extclock;
patmandin@644
   266
slouken@2043
   267
    DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",
icculus@2049
   268
                 SDL_AUDIO_BITSIZE(this->spec.format)));
icculus@2049
   269
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
icculus@2049
   270
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
slouken@2060
   271
    DEBUG_PRINT(("big endian=%d, ",
slouken@2060
   272
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
icculus@2049
   273
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
icculus@2049
   274
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));
patmandin@644
   275
slouken@2060
   276
    this->spec.format |= SDL_AUDIO_MASK_SIGNED; /* Audio is always signed */
icculus@2005
   277
icculus@2005
   278
    /* clamp out int32/float32 */
icculus@2049
   279
    if (SDL_AUDIO_BITSIZE(this->spec.format) >= 16) {
slouken@2060
   280
        this->spec.format = AUDIO_S16MSB;       /* Audio is always big endian */
slouken@2060
   281
        this->spec.channels = 2;        /* 16 bits always stereo */
icculus@2049
   282
    } else if (this->spec.channels > 2) {
slouken@2060
   283
        this->spec.channels = 2;        /* no more than stereo! */
slouken@1895
   284
    }
patmandin@644
   285
slouken@1895
   286
    MINTAUDIO_freqcount = 0;
slouken@1895
   287
slouken@1895
   288
    /* Add external clocks if present */
icculus@2049
   289
    MINTXBIOS_CheckExternalClock(this);
patmandin@1107
   290
slouken@1895
   291
    /* Standard clocks */
slouken@1895
   292
    for (i = 1; i < 12; i++) {
slouken@1895
   293
        /* Remove unusable Falcon codec predivisors */
slouken@1895
   294
        if ((i == 6) || (i == 8) || (i == 10)) {
slouken@1895
   295
            continue;
slouken@1895
   296
        }
slouken@1895
   297
        SDL_MintAudio_AddFrequency(this,
slouken@1895
   298
                                   MASTERCLOCK_FALCON1 /
slouken@1895
   299
                                   (MASTERPREDIV_FALCON * (i + 1)),
slouken@1895
   300
                                   MASTERCLOCK_FALCON1, i, -1);
slouken@1895
   301
    }
patmandin@961
   302
patmandin@961
   303
#if 1
slouken@1895
   304
    for (i = 0; i < MINTAUDIO_freqcount; i++) {
slouken@1895
   305
        DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
slouken@1895
   306
                     i, MINTAUDIO_frequencies[i].frequency,
slouken@1895
   307
                     MINTAUDIO_frequencies[i].masterclock,
slouken@1895
   308
                     MINTAUDIO_frequencies[i].predivisor));
slouken@1895
   309
    }
patmandin@961
   310
#endif
patmandin@961
   311
icculus@2049
   312
    MINTAUDIO_numfreq = SDL_MintAudio_SearchFrequency(this, this->spec.freq);
icculus@2049
   313
    this->spec.freq = MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
patmandin@644
   314
slouken@2043
   315
    DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",
icculus@2049
   316
                 SDL_AUDIO_BITSIZE(this->spec.format)));
icculus@2049
   317
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
icculus@2049
   318
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
slouken@2060
   319
    DEBUG_PRINT(("big endian=%d, ",
slouken@2060
   320
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
icculus@2049
   321
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
icculus@2049
   322
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));
patmandin@644
   323
slouken@1895
   324
    return 0;
patmandin@644
   325
}
patmandin@644
   326
slouken@1895
   327
static void
icculus@2049
   328
MINTXBIOS_InitAudio(_THIS)
patmandin@644
   329
{
slouken@1895
   330
    int channels_mode, dmaclock, prediv;
slouken@1895
   331
    void *buffer;
patmandin@644
   332
slouken@1895
   333
    /* Stop currently playing sound */
slouken@1895
   334
    SDL_MintAudio_quit_thread = SDL_FALSE;
slouken@1895
   335
    SDL_MintAudio_thread_finished = SDL_TRUE;
slouken@1895
   336
    SDL_MintAudio_WaitThread();
slouken@1895
   337
    Buffoper(0);
patmandin@644
   338
slouken@1895
   339
    /* Set replay tracks */
slouken@1895
   340
    Settracks(0, 0);
slouken@1895
   341
    Setmontracks(0);
slouken@1895
   342
slouken@1895
   343
    /* Select replay format */
slouken@1895
   344
    channels_mode = STEREO16;
icculus@2049
   345
    switch (SDL_AUDIO_BITSIZE(this->spec.format)) {
slouken@1895
   346
    case 8:
icculus@2049
   347
        if (this->spec.channels == 2) {
slouken@1895
   348
            channels_mode = STEREO8;
slouken@1895
   349
        } else {
slouken@1895
   350
            channels_mode = MONO8;
slouken@1895
   351
        }
slouken@1895
   352
        break;
slouken@1895
   353
    }
slouken@1895
   354
    if (Setmode(channels_mode) < 0) {
slouken@1895
   355
        DEBUG_PRINT((DEBUG_NAME "Setmode() failed\n"));
slouken@1895
   356
    }
patmandin@644
   357
slouken@1895
   358
    dmaclock = MINTAUDIO_frequencies[MINTAUDIO_numfreq].masterclock;
slouken@1895
   359
    prediv = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
slouken@1895
   360
    if (MINTAUDIO_frequencies[MINTAUDIO_numfreq].gpio_bits != -1) {
slouken@1895
   361
        Gpio(GPIO_SET, 7);      /* DSP port gpio outputs */
slouken@1895
   362
        Gpio(GPIO_WRITE, MINTAUDIO_frequencies[MINTAUDIO_numfreq].gpio_bits);
slouken@1895
   363
        Devconnect2(DMAPLAY, DAC | EXTOUT, CLKEXT, prediv);
slouken@1895
   364
    } else {
slouken@1895
   365
        Devconnect2(DMAPLAY, DAC, CLK25M, prediv);
slouken@1895
   366
    }
slouken@1895
   367
slouken@1895
   368
    /* Set buffer */
slouken@1895
   369
    buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
icculus@2049
   370
    if (Setbuffer(0, buffer, buffer + this->spec.size) < 0) {
slouken@1895
   371
        DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
slouken@1895
   372
    }
patmandin@644
   373
slouken@1895
   374
    if (SDL_MintAudio_mint_present) {
slouken@1895
   375
        SDL_MintAudio_thread_pid = tfork(SDL_MintAudio_Thread, 0);
slouken@1895
   376
    } else {
slouken@1895
   377
        /* Install interrupt */
slouken@1895
   378
        Jdisint(MFP_DMASOUND);
slouken@2043
   379
        /*Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt); */
patmandin@2022
   380
        Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_Dma8Interrupt);
slouken@1895
   381
        Jenabint(MFP_DMASOUND);
patmandin@644
   382
slouken@1895
   383
        if (Setinterrupt(SI_TIMERA, SI_PLAY) < 0) {
slouken@1895
   384
            DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
slouken@1895
   385
        }
slouken@1895
   386
    }
patmandin@644
   387
slouken@1895
   388
    /* Go */
slouken@1895
   389
    Buffoper(SB_PLA_ENA | SB_PLA_RPT);
slouken@1895
   390
    DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
patmandin@644
   391
}
patmandin@644
   392
slouken@1895
   393
static int
icculus@2049
   394
MINTXBIOS_OpenDevice(_THIS, const char *devname, int iscapture)
patmandin@644
   395
{
slouken@1895
   396
    /* Lock sound system */
slouken@1895
   397
    if (Locksnd() != 1) {
icculus@2049
   398
        SDL_SetError("MINTXBIOS_OpenAudio: Audio system already in use");
icculus@2049
   399
        return 0;
slouken@1895
   400
    }
patmandin@644
   401
slouken@1895
   402
    SDL_MintAudio_device = this;
patmandin@644
   403
slouken@1895
   404
    /* Check audio capabilities */
icculus@2049
   405
    if (MINTXBIOS_CheckAudio(this) == -1) {
icculus@2049
   406
        return 0;
slouken@1895
   407
    }
patmandin@644
   408
icculus@2049
   409
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   410
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   411
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   412
    if (this->hidden == NULL) {
icculus@2049
   413
        SDL_OutOfMemory();
icculus@2049
   414
        return 0;
icculus@2049
   415
    }
icculus@2049
   416
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   417
icculus@2049
   418
    SDL_CalculateAudioSpec(&this->spec);
slouken@1895
   419
slouken@1895
   420
    /* Allocate memory for audio buffers in DMA-able RAM */
icculus@2049
   421
    DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", this->spec.size));
patmandin@644
   422
slouken@2060
   423
    SDL_MintAudio_audiobuf[0] =
slouken@2060
   424
        Atari_SysMalloc(this->spec.size * 2, MX_STRAM);
slouken@1895
   425
    if (SDL_MintAudio_audiobuf[0] == NULL) {
icculus@2049
   426
        SDL_free(this->hidden);
icculus@2049
   427
        this->hidden = NULL;
icculus@2049
   428
        SDL_OutOfMemory();
icculus@2049
   429
        return 0;
slouken@1895
   430
    }
icculus@2049
   431
    SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + this->spec.size;
slouken@1895
   432
    SDL_MintAudio_numbuf = 0;
slouken@2060
   433
    SDL_memset(SDL_MintAudio_audiobuf[0], this->spec.silence,
slouken@2060
   434
               this->spec.size * 2);
icculus@2049
   435
    SDL_MintAudio_audiosize = this->spec.size;
slouken@1895
   436
    SDL_MintAudio_mutex = 0;
patmandin@644
   437
slouken@1895
   438
    DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n",
slouken@1895
   439
                 SDL_MintAudio_audiobuf[0]));
slouken@1895
   440
    DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n",
slouken@1895
   441
                 SDL_MintAudio_audiobuf[1]));
patmandin@644
   442
patmandin@2027
   443
    SDL_MintAudio_CheckFpu();
patmandin@2027
   444
slouken@1895
   445
    /* Setup audio hardware */
icculus@2049
   446
    MINTXBIOS_InitAudio(this);
patmandin@644
   447
slouken@2060
   448
    return 1;                   /* good to go. */
patmandin@644
   449
}
slouken@1895
   450
icculus@2049
   451
static int
slouken@2060
   452
MINTXBIOS_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   453
{
icculus@2049
   454
    unsigned long dummy = 0;
icculus@2049
   455
    /*SDL_MintAudio_mint_present = (Getcookie(C_MiNT, &dummy) == C_FOUND); */
icculus@2049
   456
    SDL_MintAudio_mint_present = SDL_FALSE;
icculus@2049
   457
icculus@2049
   458
    /* We can't use XBIOS in interrupt with Magic, don't know about thread */
icculus@2049
   459
    if (Getcookie(C_MagX, &dummy) == C_FOUND) {
icculus@2049
   460
        return (0);
icculus@2049
   461
    }
icculus@2049
   462
icculus@2049
   463
    /* Cookie _SND present ? if not, assume ST machine */
icculus@2049
   464
    if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
icculus@2049
   465
        cookie_snd = SND_PSG;
icculus@2049
   466
    }
icculus@2049
   467
icculus@2049
   468
    /* Check if we have 16 bits audio */
icculus@2049
   469
    if ((cookie_snd & SND_16BIT) == 0) {
icculus@2049
   470
        SDL_SetError(DEBUG_NAME "no 16-bit sound");
icculus@2049
   471
        return (0);
icculus@2049
   472
    }
icculus@2049
   473
icculus@2049
   474
    /* Check if audio is lockable */
icculus@2049
   475
    if (Locksnd() != 1) {
icculus@2049
   476
        SDL_SetError(DEBUG_NAME "audio locked by other application");
icculus@2049
   477
        return (0);
icculus@2049
   478
    }
icculus@2049
   479
icculus@2049
   480
    Unlocksnd();
icculus@2049
   481
icculus@2049
   482
    DEBUG_PRINT((DEBUG_NAME "XBIOS audio available!\n"));
icculus@2049
   483
icculus@2049
   484
    /* Set the function pointers */
icculus@2049
   485
    impl->OpenDevice = MINTXBIOS_OpenDevice;
icculus@2049
   486
    impl->CloseDevice = MINTXBIOS_CloseDevice;
patmandin@2191
   487
    impl->LockDevice = MINTXBIOS_LockDevice;
patmandin@2191
   488
    impl->UnlockDevice = MINTXBIOS_UnlockDevice;
icculus@2049
   489
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   490
    impl->ProvidesOwnCallbackThread = 1;
icculus@2049
   491
    impl->SkipMixerLock = 1;
icculus@2049
   492
slouken@2942
   493
    return 2;                   /* 2 == definitely has an audio device. */
icculus@2049
   494
}
icculus@2049
   495
icculus@2049
   496
AudioBootStrap MINTAUDIO_XBIOS_bootstrap = {
icculus@2049
   497
    MINT_AUDIO_DRIVER_NAME, "MiNT XBIOS audio driver", MINTXBIOS_Init, 0
icculus@2049
   498
};
icculus@2049
   499
slouken@1895
   500
/* vi: set ts=4 sw=4 expandtab: */