src/audio/mint/SDL_mintaudio_mcsn.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 (MacSound compatible driver)
patmandin@644
    27
patmandin@644
    28
	Patrice Mandin
patmandin@644
    29
*/
patmandin@644
    30
patmandin@1111
    31
#include <support.h>
patmandin@644
    32
patmandin@644
    33
/* Mint includes */
patmandin@644
    34
#include <mint/osbind.h>
patmandin@644
    35
#include <mint/falcon.h>
patmandin@644
    36
#include <mint/cookie.h>
patmandin@644
    37
patmandin@644
    38
#include "SDL_audio.h"
slouken@1361
    39
#include "../SDL_audio_c.h"
slouken@1361
    40
#include "../SDL_sysaudio.h"
patmandin@644
    41
patmandin@1412
    42
#include "../../video/ataricommon/SDL_atarimxalloc_c.h"
patmandin@644
    43
patmandin@644
    44
#include "SDL_mintaudio.h"
patmandin@644
    45
#include "SDL_mintaudio_mcsn.h"
patmandin@644
    46
patmandin@644
    47
/*--- Defines ---*/
patmandin@644
    48
patmandin@644
    49
#define MINT_AUDIO_DRIVER_NAME "mint_mcsn"
patmandin@644
    50
patmandin@644
    51
/* Debug print info */
patmandin@644
    52
#define DEBUG_NAME "audio:mcsn: "
patmandin@962
    53
#if 0
patmandin@644
    54
#define DEBUG_PRINT(what) \
patmandin@644
    55
	{ \
patmandin@644
    56
		printf what; \
patmandin@644
    57
	}
patmandin@644
    58
#else
patmandin@644
    59
#define DEBUG_PRINT(what)
patmandin@644
    60
#endif
patmandin@644
    61
patmandin@644
    62
/*--- Static variables ---*/
patmandin@644
    63
icculus@2049
    64
static unsigned long cookie_snd = 0;
icculus@2049
    65
static unsigned long cookie_mch = 0;
icculus@2049
    66
static cookie_mcsn_t *cookie_mcsn = NULL;
patmandin@644
    67
slouken@1895
    68
static void
icculus@2049
    69
MINTMCSN_LockDevice(_THIS)
patmandin@644
    70
{
slouken@1895
    71
    /* Stop replay */
slouken@1895
    72
    Buffoper(0);
patmandin@644
    73
}
patmandin@644
    74
slouken@1895
    75
static void
icculus@2049
    76
MINTMCSN_UnlockDevice(_THIS)
patmandin@644
    77
{
slouken@1895
    78
    /* Restart replay */
slouken@1895
    79
    Buffoper(SB_PLA_ENA | SB_PLA_RPT);
patmandin@644
    80
}
patmandin@644
    81
slouken@1895
    82
static void
icculus@2049
    83
MINTMCSN_CloseDevice(_THIS)
patmandin@644
    84
{
icculus@2049
    85
    if (this->hidden != NULL) {
icculus@2049
    86
        /* Stop replay */
icculus@2049
    87
        SDL_MintAudio_WaitThread();
icculus@2049
    88
        Buffoper(0);
patmandin@644
    89
icculus@2049
    90
        if (!SDL_MintAudio_mint_present) {
icculus@2049
    91
            /* Uninstall interrupt */
icculus@2049
    92
            Jdisint(MFP_DMASOUND);
icculus@2049
    93
        }
patmandin@644
    94
icculus@2049
    95
        /* Wait if currently playing sound */
slouken@2060
    96
        while (SDL_MintAudio_mutex != 0) {
slouken@2060
    97
        }
patmandin@644
    98
icculus@2049
    99
        /* Clear buffers */
icculus@2049
   100
        if (SDL_MintAudio_audiobuf[0]) {
icculus@2049
   101
            Mfree(SDL_MintAudio_audiobuf[0]);
icculus@2049
   102
            SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
icculus@2049
   103
        }
icculus@2049
   104
icculus@2049
   105
        /* Unlock sound system */
icculus@2049
   106
        Unlocksnd();
icculus@2049
   107
icculus@2049
   108
        SDL_free(this->hidden);
icculus@2049
   109
        this->hidden = NULL;
slouken@1895
   110
    }
patmandin@644
   111
}
patmandin@644
   112
slouken@1895
   113
static int
icculus@2049
   114
MINTMCSN_CheckAudio(_THIS)
patmandin@644
   115
{
slouken@1895
   116
    int i;
slouken@1895
   117
    unsigned long masterclock, masterprediv;
patmandin@644
   118
slouken@2043
   119
    DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",
icculus@2049
   120
                 SDL_AUDIO_BITSIZE(this->spec.format)));
icculus@2049
   121
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
icculus@2049
   122
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
slouken@2060
   123
    DEBUG_PRINT(("big endian=%d, ",
slouken@2060
   124
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
icculus@2049
   125
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
icculus@2049
   126
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));
patmandin@644
   127
icculus@2049
   128
    if (this->spec.channels > 2) {
slouken@2060
   129
        this->spec.channels = 2;        /* no more than stereo! */
icculus@2005
   130
    }
icculus@2005
   131
slouken@1895
   132
    /* Check formats available */
slouken@1895
   133
    MINTAUDIO_freqcount = 0;
slouken@1895
   134
    switch (cookie_mcsn->play) {
slouken@1895
   135
    case MCSN_ST:
icculus@2049
   136
        this->spec.channels = 1;
slouken@2060
   137
        this->spec.format = AUDIO_S8;   /* FIXME: is it signed or unsigned ? */
slouken@1895
   138
        SDL_MintAudio_AddFrequency(this, 12500, 0, 0, -1);
slouken@1895
   139
        break;
slouken@1895
   140
    case MCSN_TT:              /* Also STE, Mega STE */
icculus@2049
   141
        this->spec.format = AUDIO_S8;
slouken@1895
   142
        masterclock = MASTERCLOCK_STE;
slouken@1895
   143
        masterprediv = MASTERPREDIV_STE;
slouken@1895
   144
        if ((cookie_mch >> 16) == MCH_TT) {
slouken@1895
   145
            masterclock = MASTERCLOCK_TT;
slouken@1895
   146
            masterprediv = MASTERPREDIV_TT;
slouken@1895
   147
        }
slouken@1895
   148
        for (i = 0; i < 4; i++) {
slouken@1895
   149
            SDL_MintAudio_AddFrequency(this,
slouken@1895
   150
                                       masterclock / (masterprediv *
slouken@1895
   151
                                                      (1 << i)),
slouken@1895
   152
                                       masterclock, 3 - i, -1);
slouken@1895
   153
        }
slouken@1895
   154
        break;
slouken@1895
   155
    case MCSN_FALCON:          /* Also Mac */
slouken@1895
   156
        for (i = 1; i < 12; i++) {
slouken@1895
   157
            /* Remove unusable Falcon codec predivisors */
slouken@1895
   158
            if ((i == 6) || (i == 8) || (i == 10)) {
slouken@1895
   159
                continue;
slouken@1895
   160
            }
slouken@1895
   161
            SDL_MintAudio_AddFrequency(this,
slouken@1895
   162
                                       MASTERCLOCK_FALCON1 /
slouken@1895
   163
                                       (MASTERPREDIV_FALCON * (i + 1)),
slouken@1895
   164
                                       CLK25M, i + 1, -1);
slouken@1895
   165
        }
slouken@1895
   166
        if (cookie_mcsn->res1 != 0) {
slouken@1895
   167
            for (i = 1; i < 4; i++) {
slouken@1895
   168
                SDL_MintAudio_AddFrequency(this,
slouken@1895
   169
                                           (cookie_mcsn->res1) /
slouken@1895
   170
                                           (MASTERPREDIV_FALCON *
slouken@1895
   171
                                            (1 << i)), CLKEXT,
slouken@1895
   172
                                           (1 << i) - 1, -1);
slouken@1895
   173
            }
slouken@1895
   174
        }
slouken@2060
   175
        this->spec.format |= SDL_AUDIO_MASK_SIGNED;     /* Audio is always signed */
icculus@2049
   176
        if ((SDL_AUDIO_BITSIZE(this->spec.format)) == 16) {
slouken@2060
   177
            this->spec.format |= SDL_AUDIO_MASK_ENDIAN; /* Audio is always big endian */
slouken@2060
   178
            this->spec.channels = 2;    /* 16 bits always stereo */
slouken@1895
   179
        }
slouken@1895
   180
        break;
slouken@1895
   181
    }
patmandin@644
   182
patmandin@961
   183
#if 1
slouken@1895
   184
    for (i = 0; i < MINTAUDIO_freqcount; i++) {
slouken@1895
   185
        DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
slouken@1895
   186
                     i, MINTAUDIO_frequencies[i].frequency,
slouken@1895
   187
                     MINTAUDIO_frequencies[i].masterclock,
slouken@1895
   188
                     MINTAUDIO_frequencies[i].predivisor));
slouken@1895
   189
    }
patmandin@961
   190
#endif
patmandin@961
   191
icculus@2049
   192
    MINTAUDIO_numfreq = SDL_MintAudio_SearchFrequency(this, this->spec.freq);
icculus@2049
   193
    this->spec.freq = MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
patmandin@644
   194
slouken@2043
   195
    DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",
icculus@2049
   196
                 SDL_AUDIO_BITSIZE(this->spec.format)));
icculus@2049
   197
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
icculus@2049
   198
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
slouken@2060
   199
    DEBUG_PRINT(("big endian=%d, ",
slouken@2060
   200
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
icculus@2049
   201
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
icculus@2049
   202
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));
patmandin@644
   203
slouken@1895
   204
    return 0;
patmandin@644
   205
}
patmandin@644
   206
slouken@1895
   207
static void
icculus@2049
   208
MINTMCSN_InitAudio(_THIS)
patmandin@644
   209
{
slouken@1895
   210
    int channels_mode, prediv, dmaclock;
slouken@1895
   211
    void *buffer;
patmandin@644
   212
slouken@1895
   213
    /* Stop currently playing sound */
slouken@1895
   214
    SDL_MintAudio_quit_thread = SDL_FALSE;
slouken@1895
   215
    SDL_MintAudio_thread_finished = SDL_TRUE;
slouken@1895
   216
    SDL_MintAudio_WaitThread();
slouken@1895
   217
    Buffoper(0);
patmandin@644
   218
slouken@1895
   219
    /* Set replay tracks */
slouken@1895
   220
    Settracks(0, 0);
slouken@1895
   221
    Setmontracks(0);
slouken@1895
   222
slouken@1895
   223
    /* Select replay format */
slouken@1895
   224
    channels_mode = STEREO16;
icculus@2049
   225
    switch (SDL_AUDIO_BITSIZE(this->spec.format)) {
slouken@1895
   226
    case 8:
icculus@2049
   227
        if (this->spec.channels == 2) {
slouken@1895
   228
            channels_mode = STEREO8;
slouken@1895
   229
        } else {
slouken@1895
   230
            channels_mode = MONO8;
slouken@1895
   231
        }
slouken@1895
   232
        break;
slouken@1895
   233
    }
slouken@1895
   234
    if (Setmode(channels_mode) < 0) {
slouken@1895
   235
        DEBUG_PRINT((DEBUG_NAME "Setmode() failed\n"));
slouken@1895
   236
    }
patmandin@644
   237
slouken@1895
   238
    dmaclock = MINTAUDIO_frequencies[MINTAUDIO_numfreq].masterclock;
slouken@1895
   239
    prediv = MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
slouken@1895
   240
    switch (cookie_mcsn->play) {
slouken@1895
   241
    case MCSN_TT:
slouken@1895
   242
        Devconnect(DMAPLAY, DAC, CLK25M, CLKOLD, 1);
slouken@1895
   243
        Soundcmd(SETPRESCALE, prediv);
slouken@1895
   244
        DEBUG_PRINT((DEBUG_NAME "STE/TT prescaler selected\n"));
slouken@1895
   245
        break;
slouken@1895
   246
    case MCSN_FALCON:
slouken@1895
   247
        Devconnect(DMAPLAY, DAC, dmaclock, prediv, 1);
slouken@1895
   248
        DEBUG_PRINT((DEBUG_NAME "Falcon prescaler selected\n"));
slouken@1895
   249
        break;
slouken@1895
   250
    }
patmandin@644
   251
slouken@1895
   252
    /* Set buffer */
slouken@1895
   253
    buffer = SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
icculus@2049
   254
    if (Setbuffer(0, buffer, buffer + this->spec.size) < 0) {
slouken@1895
   255
        DEBUG_PRINT((DEBUG_NAME "Setbuffer() failed\n"));
slouken@1895
   256
    }
patmandin@644
   257
slouken@1895
   258
    if (SDL_MintAudio_mint_present) {
slouken@1895
   259
        SDL_MintAudio_thread_pid = tfork(SDL_MintAudio_Thread, 0);
slouken@1895
   260
    } else {
slouken@1895
   261
        /* Install interrupt */
slouken@1895
   262
        Jdisint(MFP_DMASOUND);
slouken@1895
   263
        Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_XbiosInterrupt);
slouken@1895
   264
        Jenabint(MFP_DMASOUND);
patmandin@644
   265
slouken@1895
   266
        if (Setinterrupt(SI_TIMERA, SI_PLAY) < 0) {
slouken@1895
   267
            DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
slouken@1895
   268
        }
slouken@1895
   269
    }
slouken@1895
   270
slouken@1895
   271
    /* Go */
slouken@1895
   272
    Buffoper(SB_PLA_ENA | SB_PLA_RPT);
slouken@1895
   273
    DEBUG_PRINT((DEBUG_NAME "hardware initialized\n"));
patmandin@644
   274
}
patmandin@644
   275
slouken@1895
   276
static int
icculus@2049
   277
MINTMCSN_OpenDevice(_THIS, const char *devname, int iscapture)
patmandin@644
   278
{
slouken@1895
   279
    /* Lock sound system */
slouken@1895
   280
    if (Locksnd() != 1) {
icculus@2049
   281
        SDL_SetError("MINTMCSN_OpenDevice: Audio system already in use");
icculus@2049
   282
        return 0;
slouken@1895
   283
    }
patmandin@644
   284
slouken@1895
   285
    SDL_MintAudio_device = this;
patmandin@644
   286
slouken@1895
   287
    /* Check audio capabilities */
icculus@2049
   288
    if (MINTMCSN_CheckAudio(this) == -1) {
icculus@2049
   289
        return 0;
slouken@1895
   290
    }
patmandin@644
   291
icculus@2049
   292
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   293
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   294
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   295
    if (this->hidden == NULL) {
icculus@2049
   296
        SDL_OutOfMemory();
icculus@2049
   297
        return 0;
icculus@2049
   298
    }
icculus@2049
   299
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   300
icculus@2049
   301
    SDL_CalculateAudioSpec(&this->spec);
slouken@1895
   302
slouken@1895
   303
    /* Allocate memory for audio buffers in DMA-able RAM */
icculus@2049
   304
    DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", this->spec.size));
patmandin@644
   305
slouken@2060
   306
    SDL_MintAudio_audiobuf[0] =
slouken@2060
   307
        Atari_SysMalloc(this->spec.size * 2, MX_STRAM);
slouken@1895
   308
    if (SDL_MintAudio_audiobuf[0] == NULL) {
icculus@2049
   309
        SDL_free(this->hidden);
icculus@2049
   310
        this->hidden = NULL;
icculus@2049
   311
        SDL_OutOfMemory();
icculus@2049
   312
        return 0;
slouken@1895
   313
    }
icculus@2049
   314
    SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + this->spec.size;
slouken@1895
   315
    SDL_MintAudio_numbuf = 0;
slouken@2060
   316
    SDL_memset(SDL_MintAudio_audiobuf[0], this->spec.silence,
slouken@2060
   317
               this->spec.size * 2);
icculus@2049
   318
    SDL_MintAudio_audiosize = this->spec.size;
slouken@1895
   319
    SDL_MintAudio_mutex = 0;
patmandin@644
   320
slouken@1895
   321
    DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n",
slouken@1895
   322
                 SDL_MintAudio_audiobuf[0]));
slouken@1895
   323
    DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n",
slouken@1895
   324
                 SDL_MintAudio_audiobuf[1]));
patmandin@644
   325
patmandin@2027
   326
    SDL_MintAudio_CheckFpu();
patmandin@2027
   327
slouken@1895
   328
    /* Setup audio hardware */
icculus@2049
   329
    MINTMCSN_InitAudio(this);
patmandin@644
   330
slouken@2060
   331
    return 1;                   /* good to go. */
patmandin@644
   332
}
slouken@1895
   333
icculus@2049
   334
static int
slouken@2060
   335
MINTMCSN_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   336
{
icculus@2049
   337
    unsigned long dummy = 0;
icculus@2049
   338
icculus@2049
   339
    SDL_MintAudio_mint_present = (Getcookie(C_MiNT, &dummy) == C_FOUND);
icculus@2049
   340
icculus@2049
   341
    /* We can't use XBIOS in interrupt with Magic, don't know about thread */
icculus@2049
   342
    if (Getcookie(C_MagX, &dummy) == C_FOUND) {
icculus@2049
   343
        return 0;
icculus@2049
   344
    }
icculus@2049
   345
icculus@2049
   346
    /* Cookie _MCH present ? if not, assume ST machine */
icculus@2049
   347
    if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
icculus@2049
   348
        cookie_mch = MCH_ST;
icculus@2049
   349
    }
icculus@2049
   350
icculus@2049
   351
    /* Cookie _SND present ? if not, assume ST machine */
icculus@2049
   352
    if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
icculus@2049
   353
        cookie_snd = SND_PSG;
icculus@2049
   354
    }
icculus@2049
   355
icculus@2049
   356
    /* Check if we have 16 bits audio */
icculus@2049
   357
    if ((cookie_snd & SND_16BIT) == 0) {
icculus@2049
   358
        SDL_SetError(DEBUG_NAME "no 16-bit sound");
icculus@2049
   359
        return 0;
icculus@2049
   360
    }
icculus@2049
   361
icculus@2049
   362
    /* Cookie MCSN present ? */
icculus@2049
   363
    if (Getcookie(C_McSn, (long *) &cookie_mcsn) != C_FOUND) {
icculus@2049
   364
        SDL_SetError(DEBUG_NAME "no MCSN audio");
icculus@2049
   365
        return 0;
icculus@2049
   366
    }
icculus@2049
   367
icculus@2049
   368
    /* Check if interrupt at end of replay */
icculus@2049
   369
    if (cookie_mcsn->pint == 0) {
icculus@2049
   370
        SDL_SetError(DEBUG_NAME "no interrupt at end of replay");
icculus@2049
   371
        return 0;
icculus@2049
   372
    }
icculus@2049
   373
icculus@2049
   374
    /* Check if audio is lockable */
icculus@2049
   375
    if (Locksnd() != 1) {
icculus@2049
   376
        SDL_SetError(DEBUG_NAME "audio locked by other application");
icculus@2049
   377
        return 0;
icculus@2049
   378
    }
icculus@2049
   379
icculus@2049
   380
    Unlocksnd();
icculus@2049
   381
icculus@2049
   382
    DEBUG_PRINT((DEBUG_NAME "MCSN audio available!\n"));
icculus@2049
   383
icculus@2049
   384
    /* Set the function pointers */
icculus@2049
   385
    impl->OpenDevice = MINTMCSN_OpenDevice;
icculus@2049
   386
    impl->CloseDevice = MINTMCSN_CloseDevice;
patmandin@2191
   387
    impl->LockDevice = MINTMCSN_LockDevice;
patmandin@2191
   388
    impl->UnlockDevice = MINTMCSN_UnlockDevice;
icculus@2049
   389
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   390
    impl->ProvidesOwnCallbackThread = 1;
icculus@2049
   391
    impl->SkipMixerLock = 1;
icculus@2049
   392
slouken@2942
   393
    return 2;                   /* 2 == definitely has an audio device. */
icculus@2049
   394
}
icculus@2049
   395
icculus@2049
   396
AudioBootStrap MINTAUDIO_MCSN_bootstrap = {
icculus@2049
   397
    MINT_AUDIO_DRIVER_NAME, "MiNT MCSN audio driver", MINTMCSN_Init, 0
icculus@2049
   398
};
icculus@2049
   399
slouken@1895
   400
/* vi: set ts=4 sw=4 expandtab: */