src/audio/mint/SDL_mintaudio_dma8.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 DMA 8bits (hardware access)
patmandin@644
    27
patmandin@644
    28
	Patrice Mandin
patmandin@644
    29
*/
patmandin@644
    30
patmandin@644
    31
/* Mint includes */
patmandin@644
    32
#include <mint/osbind.h>
patmandin@644
    33
#include <mint/falcon.h>
patmandin@644
    34
#include <mint/cookie.h>
patmandin@644
    35
patmandin@644
    36
#include "SDL_audio.h"
slouken@1361
    37
#include "../SDL_audio_c.h"
slouken@1361
    38
#include "../SDL_sysaudio.h"
patmandin@644
    39
patmandin@1412
    40
#include "../../video/ataricommon/SDL_atarimxalloc_c.h"
patmandin@644
    41
patmandin@644
    42
#include "SDL_mintaudio.h"
patmandin@644
    43
#include "SDL_mintaudio_dma8.h"
patmandin@644
    44
patmandin@644
    45
/*--- Defines ---*/
patmandin@644
    46
patmandin@644
    47
#define MINT_AUDIO_DRIVER_NAME "mint_dma8"
patmandin@644
    48
patmandin@644
    49
/* Debug print info */
patmandin@644
    50
#define DEBUG_NAME "audio:dma8: "
patmandin@962
    51
#if 0
patmandin@644
    52
#define DEBUG_PRINT(what) \
patmandin@644
    53
	{ \
patmandin@644
    54
		printf what; \
patmandin@644
    55
	}
patmandin@644
    56
#else
patmandin@644
    57
#define DEBUG_PRINT(what)
patmandin@644
    58
#endif
patmandin@644
    59
patmandin@644
    60
/*--- Static variables ---*/
patmandin@644
    61
patmandin@644
    62
static unsigned long cookie_snd, cookie_mch;
patmandin@644
    63
slouken@1895
    64
static void
icculus@2049
    65
MINTDMA8_LockDevice(_THIS)
patmandin@644
    66
{
slouken@1895
    67
    void *oldpile;
patmandin@644
    68
slouken@1895
    69
    /* Stop replay */
slouken@1895
    70
    oldpile = (void *) Super(0);
slouken@1895
    71
    DMAAUDIO_IO.control = 0;
slouken@1895
    72
    Super(oldpile);
patmandin@644
    73
}
patmandin@644
    74
slouken@1895
    75
static void
icculus@2049
    76
MINTDMA8_UnlockDevice(_THIS)
patmandin@644
    77
{
slouken@1895
    78
    void *oldpile;
patmandin@644
    79
slouken@1895
    80
    /* Restart replay */
slouken@1895
    81
    oldpile = (void *) Super(0);
slouken@1895
    82
    DMAAUDIO_IO.control = 3;
slouken@1895
    83
    Super(oldpile);
patmandin@644
    84
}
patmandin@644
    85
slouken@1895
    86
static void
icculus@2049
    87
MINTDMA8_CloseDevice(_THIS)
patmandin@644
    88
{
icculus@2049
    89
    if (this->hidden != NULL) {
icculus@2049
    90
        /* Stop replay */
icculus@2049
    91
        void *oldpile = (void *) Super(0);
patmandin@644
    92
icculus@2049
    93
        DMAAUDIO_IO.control = 0;
icculus@2049
    94
        Super(oldpile);
icculus@2049
    95
icculus@2049
    96
        DEBUG_PRINT((DEBUG_NAME "closeaudio: replay stopped\n"));
patmandin@644
    97
icculus@2049
    98
        /* Disable interrupt */
icculus@2049
    99
        Jdisint(MFP_DMASOUND);
patmandin@644
   100
icculus@2049
   101
        DEBUG_PRINT((DEBUG_NAME "closeaudio: interrupt disabled\n"));
patmandin@644
   102
icculus@2049
   103
        /* Wait if currently playing sound */
slouken@2060
   104
        while (SDL_MintAudio_mutex != 0) {
slouken@2060
   105
        }
patmandin@644
   106
icculus@2049
   107
        DEBUG_PRINT((DEBUG_NAME "closeaudio: no more interrupt running\n"));
patmandin@644
   108
icculus@2049
   109
        /* Clear buffers */
icculus@2049
   110
        if (SDL_MintAudio_audiobuf[0]) {
icculus@2049
   111
            Mfree(SDL_MintAudio_audiobuf[0]);
icculus@2049
   112
            SDL_MintAudio_audiobuf[0] = SDL_MintAudio_audiobuf[1] = NULL;
icculus@2049
   113
        }
icculus@2049
   114
icculus@2049
   115
        DEBUG_PRINT((DEBUG_NAME "closeaudio: buffers freed\n"));
patmandin@2191
   116
        SDL_free(this->hidden);
patmandin@2191
   117
        this->hidden = NULL;
slouken@1895
   118
    }
patmandin@644
   119
}
patmandin@644
   120
slouken@1895
   121
static int
icculus@2049
   122
MINTDMA8_CheckAudio(_THIS)
patmandin@644
   123
{
slouken@1895
   124
    int i, masterprediv, sfreq;
slouken@1895
   125
    unsigned long masterclock;
patmandin@644
   126
slouken@2043
   127
    DEBUG_PRINT((DEBUG_NAME "asked: %d bits, ",
icculus@2049
   128
                 SDL_AUDIO_BITSIZE(this->spec.format)));
icculus@2049
   129
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
icculus@2049
   130
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
slouken@2060
   131
    DEBUG_PRINT(("big endian=%d, ",
slouken@2060
   132
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
icculus@2049
   133
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
icculus@2049
   134
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));
patmandin@644
   135
icculus@2049
   136
    if (this->spec.channels > 2) {
slouken@2060
   137
        this->spec.channels = 2;        /* no more than stereo! */
icculus@2005
   138
    }
icculus@2005
   139
slouken@1895
   140
    /* Check formats available */
icculus@2049
   141
    this->spec.format = AUDIO_S8;
slouken@1895
   142
slouken@1895
   143
    /* Calculate and select the closest frequency */
slouken@1895
   144
    sfreq = 0;
slouken@1895
   145
    masterclock = MASTERCLOCK_STE;
slouken@1895
   146
    masterprediv = MASTERPREDIV_STE;
slouken@1895
   147
    switch (cookie_mch >> 16) {
patmandin@644
   148
/*
patmandin@644
   149
		case MCH_STE:
patmandin@644
   150
			masterclock=MASTERCLOCK_STE;
patmandin@644
   151
			masterprediv=MASTERPREDIV_STE;
patmandin@644
   152
			break;
patmandin@644
   153
*/
slouken@1895
   154
    case MCH_TT:
slouken@1895
   155
        masterclock = MASTERCLOCK_TT;
slouken@1895
   156
        masterprediv = MASTERPREDIV_TT;
slouken@1895
   157
        break;
slouken@1895
   158
    case MCH_F30:
slouken@1895
   159
    case MCH_ARANYM:
slouken@1895
   160
        masterclock = MASTERCLOCK_FALCON1;
slouken@1895
   161
        masterprediv = MASTERPREDIV_FALCON;
slouken@1895
   162
        sfreq = 1;
slouken@1895
   163
        break;
slouken@1895
   164
    }
slouken@1895
   165
slouken@1895
   166
    MINTAUDIO_freqcount = 0;
slouken@1895
   167
    for (i = sfreq; i < 4; i++) {
slouken@1895
   168
        SDL_MintAudio_AddFrequency(this,
slouken@1895
   169
                                   masterclock / (masterprediv * (1 << i)),
slouken@1895
   170
                                   masterclock, i - sfreq, -1);
slouken@1895
   171
    }
patmandin@644
   172
patmandin@961
   173
#if 1
slouken@1895
   174
    for (i = 0; i < MINTAUDIO_freqcount; i++) {
slouken@1895
   175
        DEBUG_PRINT((DEBUG_NAME "freq %d: %lu Hz, clock %lu, prediv %d\n",
slouken@1895
   176
                     i, MINTAUDIO_frequencies[i].frequency,
slouken@1895
   177
                     MINTAUDIO_frequencies[i].masterclock,
slouken@1895
   178
                     MINTAUDIO_frequencies[i].predivisor));
slouken@1895
   179
    }
patmandin@961
   180
#endif
patmandin@961
   181
icculus@2049
   182
    MINTAUDIO_numfreq = SDL_MintAudio_SearchFrequency(this, this->spec.freq);
icculus@2049
   183
    this->spec.freq = MINTAUDIO_frequencies[MINTAUDIO_numfreq].frequency;
patmandin@644
   184
slouken@2043
   185
    DEBUG_PRINT((DEBUG_NAME "obtained: %d bits, ",
icculus@2049
   186
                 SDL_AUDIO_BITSIZE(this->spec.format)));
icculus@2049
   187
    DEBUG_PRINT(("float=%d, ", SDL_AUDIO_ISFLOAT(this->spec.format)));
icculus@2049
   188
    DEBUG_PRINT(("signed=%d, ", SDL_AUDIO_ISSIGNED(this->spec.format)));
slouken@2060
   189
    DEBUG_PRINT(("big endian=%d, ",
slouken@2060
   190
                 SDL_AUDIO_ISBIGENDIAN(this->spec.format)));
icculus@2049
   191
    DEBUG_PRINT(("channels=%d, ", this->spec.channels));
icculus@2049
   192
    DEBUG_PRINT(("freq=%d\n", this->spec.freq));
patmandin@644
   193
slouken@1895
   194
    return 0;
patmandin@644
   195
}
patmandin@644
   196
slouken@1895
   197
static void
icculus@2049
   198
MINTDMA8_InitAudio(_THIS)
patmandin@644
   199
{
slouken@1895
   200
    void *oldpile;
slouken@1895
   201
    unsigned long buffer;
slouken@1895
   202
    unsigned char mode;
slouken@1895
   203
slouken@1895
   204
    /* Set replay tracks */
slouken@1895
   205
    if (cookie_snd & SND_16BIT) {
slouken@1895
   206
        Settracks(0, 0);
slouken@1895
   207
        Setmontracks(0);
slouken@1895
   208
    }
patmandin@644
   209
slouken@1895
   210
    oldpile = (void *) Super(0);
patmandin@644
   211
slouken@1895
   212
    /* Stop currently playing sound */
slouken@1895
   213
    DMAAUDIO_IO.control = 0;
patmandin@644
   214
slouken@1895
   215
    /* Set buffer */
slouken@1895
   216
    buffer = (unsigned long) SDL_MintAudio_audiobuf[SDL_MintAudio_numbuf];
slouken@1895
   217
    DMAAUDIO_IO.start_high = (buffer >> 16) & 255;
slouken@1895
   218
    DMAAUDIO_IO.start_mid = (buffer >> 8) & 255;
slouken@1895
   219
    DMAAUDIO_IO.start_low = buffer & 255;
patmandin@644
   220
slouken@1895
   221
    buffer += SDL_MintAudio_audiosize;
slouken@1895
   222
    DMAAUDIO_IO.end_high = (buffer >> 16) & 255;
slouken@1895
   223
    DMAAUDIO_IO.end_mid = (buffer >> 8) & 255;
slouken@1895
   224
    DMAAUDIO_IO.end_low = buffer & 255;
patmandin@644
   225
slouken@1895
   226
    mode = 3 - MINTAUDIO_frequencies[MINTAUDIO_numfreq].predivisor;
icculus@2049
   227
    if (this->spec.channels == 1) {
slouken@1895
   228
        mode |= 1 << 7;
slouken@1895
   229
    }
slouken@1895
   230
    DMAAUDIO_IO.sound_ctrl = mode;
patmandin@644
   231
slouken@1895
   232
    /* Set interrupt */
slouken@1895
   233
    Jdisint(MFP_DMASOUND);
slouken@1895
   234
    Xbtimer(XB_TIMERA, 8, 1, SDL_MintAudio_Dma8Interrupt);
slouken@1895
   235
    Jenabint(MFP_DMASOUND);
patmandin@644
   236
slouken@1895
   237
    if (cookie_snd & SND_16BIT) {
slouken@1895
   238
        if (Setinterrupt(SI_TIMERA, SI_PLAY) < 0) {
slouken@1895
   239
            DEBUG_PRINT((DEBUG_NAME "Setinterrupt() failed\n"));
slouken@1895
   240
        }
slouken@1895
   241
    }
patmandin@1098
   242
slouken@1895
   243
    /* Go */
slouken@1895
   244
    DMAAUDIO_IO.control = 3;    /* playback + repeat */
patmandin@644
   245
slouken@1895
   246
    Super(oldpile);
patmandin@644
   247
}
patmandin@644
   248
slouken@1895
   249
static int
icculus@2049
   250
MINTDMA8_OpenDevice(_THIS, const char *devname, int iscapture)
patmandin@644
   251
{
slouken@1895
   252
    SDL_MintAudio_device = this;
patmandin@644
   253
slouken@1895
   254
    /* Check audio capabilities */
icculus@2049
   255
    if (MINTDMA8_CheckAudio(this) == -1) {
icculus@2049
   256
        return 0;
slouken@1895
   257
    }
patmandin@644
   258
icculus@2049
   259
    /* Initialize all variables that we clean on shutdown */
icculus@2049
   260
    this->hidden = (struct SDL_PrivateAudioData *)
slouken@2060
   261
        SDL_malloc((sizeof *this->hidden));
icculus@2049
   262
    if (this->hidden == NULL) {
icculus@2049
   263
        SDL_OutOfMemory();
icculus@2049
   264
        return 0;
icculus@2049
   265
    }
icculus@2049
   266
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@2049
   267
icculus@2049
   268
    SDL_CalculateAudioSpec(&this->spec);
patmandin@644
   269
slouken@1895
   270
    /* Allocate memory for audio buffers in DMA-able RAM */
icculus@2049
   271
    DEBUG_PRINT((DEBUG_NAME "buffer size=%d\n", this->spec.size));
patmandin@644
   272
slouken@2060
   273
    SDL_MintAudio_audiobuf[0] =
slouken@2060
   274
        Atari_SysMalloc(this->spec.size * 2, MX_STRAM);
slouken@1895
   275
    if (SDL_MintAudio_audiobuf[0] == NULL) {
icculus@2049
   276
        SDL_free(this->hidden);
icculus@2049
   277
        this->hidden = NULL;
icculus@2049
   278
        SDL_OutOfMemory();
icculus@2049
   279
        return 0;
slouken@1895
   280
    }
icculus@2049
   281
    SDL_MintAudio_audiobuf[1] = SDL_MintAudio_audiobuf[0] + this->spec.size;
slouken@1895
   282
    SDL_MintAudio_numbuf = 0;
slouken@2060
   283
    SDL_memset(SDL_MintAudio_audiobuf[0], this->spec.silence,
slouken@2060
   284
               this->spec.size * 2);
icculus@2049
   285
    SDL_MintAudio_audiosize = this->spec.size;
slouken@1895
   286
    SDL_MintAudio_mutex = 0;
patmandin@644
   287
slouken@1895
   288
    DEBUG_PRINT((DEBUG_NAME "buffer 0 at 0x%08x\n",
slouken@1895
   289
                 SDL_MintAudio_audiobuf[0]));
slouken@1895
   290
    DEBUG_PRINT((DEBUG_NAME "buffer 1 at 0x%08x\n",
slouken@1895
   291
                 SDL_MintAudio_audiobuf[1]));
patmandin@644
   292
patmandin@2027
   293
    SDL_MintAudio_CheckFpu();
patmandin@2027
   294
slouken@1895
   295
    /* Setup audio hardware */
icculus@2049
   296
    MINTDMA8_InitAudio(this);
patmandin@644
   297
slouken@2060
   298
    return 1;                   /* good to go. */
patmandin@644
   299
}
slouken@1895
   300
icculus@2049
   301
static int
slouken@2060
   302
MINTDMA8_Init(SDL_AudioDriverImpl * impl)
icculus@2049
   303
{
icculus@2049
   304
    /* Cookie _MCH present ? if not, assume ST machine */
icculus@2049
   305
    if (Getcookie(C__MCH, &cookie_mch) == C_NOTFOUND) {
icculus@2049
   306
        cookie_mch = MCH_ST;
icculus@2049
   307
    }
icculus@2049
   308
icculus@2049
   309
    /* Cookie _SND present ? if not, assume ST machine */
icculus@2049
   310
    if (Getcookie(C__SND, &cookie_snd) == C_NOTFOUND) {
icculus@2049
   311
        cookie_snd = SND_PSG;
icculus@2049
   312
    }
icculus@2049
   313
icculus@2049
   314
    /* Check if we have 8 bits audio */
icculus@2049
   315
    if ((cookie_snd & SND_8BIT) == 0) {
icculus@2049
   316
        SDL_SetError(DEBUG_NAME "no 8 bits sound");
icculus@2049
   317
        return 0;
icculus@2049
   318
    }
icculus@2049
   319
icculus@2049
   320
    /* Check if audio is lockable */
icculus@2049
   321
    if (cookie_snd & SND_16BIT) {
icculus@2049
   322
        if (Locksnd() != 1) {
icculus@2049
   323
            SDL_SetError(DEBUG_NAME "audio locked by other application");
icculus@2049
   324
            return 0;
icculus@2049
   325
        }
icculus@2049
   326
icculus@2049
   327
        Unlocksnd();
icculus@2049
   328
    }
icculus@2049
   329
icculus@2049
   330
    DEBUG_PRINT((DEBUG_NAME "8 bits audio available!\n"));
icculus@2049
   331
icculus@2049
   332
    /* Set the function pointers */
icculus@2049
   333
    impl->OpenDevice = MINTDMA8_OpenDevice;
icculus@2049
   334
    impl->CloseDevice = MINTDMA8_CloseDevice;
patmandin@2191
   335
    impl->LockDevice = MINTDMA8_LockDevice;
patmandin@2191
   336
    impl->UnlockDevice = MINTDMA8_UnlockDevice;
icculus@2049
   337
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@2049
   338
    impl->ProvidesOwnCallbackThread = 1;
icculus@2049
   339
    impl->SkipMixerLock = 1;
icculus@2049
   340
slouken@2942
   341
    return 2;                   /* 2 == definitely has an audio device. */
icculus@2049
   342
}
icculus@2049
   343
icculus@2049
   344
AudioBootStrap MINTAUDIO_DMA8_bootstrap = {
icculus@2049
   345
    MINT_AUDIO_DRIVER_NAME, "MiNT DMA 8 bits audio driver", MINTDMA8_Init, 0
icculus@2049
   346
};
icculus@2049
   347
slouken@1895
   348
/* vi: set ts=4 sw=4 expandtab: */