adlib.c 7.6 KB
Newer Older
B
bellard 已提交
1
/*
2 3 4 5
 * QEMU Proxy for OPL2/3 emulation by MAME team
 *
 * Copyright (c) 2004-2005 Vassili Karpov (malc)
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24
#include <assert.h>
P
pbrook 已提交
25 26
#include "hw.h"
#include "audiodev.h"
B
bellard 已提交
27

28 29
#define ADLIB_KILL_TIMERS 1

B
bellard 已提交
30 31 32 33 34 35
#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif
B
bellard 已提交
36

37
#ifdef HAS_YMF262
B
bellard 已提交
38
#include "ymf262.h"
39
void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
B
bellard 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#define SHIFT 2
#else
#include "fmopl.h"
#define SHIFT 1
#endif

#define IO_READ_PROTO(name) \
    uint32_t name (void *opaque, uint32_t nport)
#define IO_WRITE_PROTO(name) \
    void name (void *opaque, uint32_t nport, uint32_t val)

static struct {
    int port;
    int freq;
} conf = {0x220, 44100};

typedef struct {
B
bellard 已提交
57
    QEMUSoundCard card;
58
    int ticking[2];
B
bellard 已提交
59 60 61
    int enabled;
    int active;
    int bufpos;
62 63 64
#ifdef DEBUG
    int64_t exp[2];
#endif
B
bellard 已提交
65
    int16_t *mixbuf;
66 67 68 69 70
    uint64_t dexp[2];
    SWVoiceOut *voice;
    int left, pos, samples;
    QEMUAudioTimeStamp ats;
#ifndef HAS_YMF262
B
bellard 已提交
71 72 73 74
    FM_OPL *opl;
#endif
} AdlibState;

B
bellard 已提交
75
static AdlibState glob_adlib;
B
bellard 已提交
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static void adlib_stop_opl_timer (AdlibState *s, size_t n)
{
#ifdef HAS_YMF262
    YMF262TimerOver (0, n);
#else
    OPLTimerOver (s->opl, n);
#endif
    s->ticking[n] = 0;
}

static void adlib_kill_timers (AdlibState *s)
{
    size_t i;

    for (i = 0; i < 2; ++i) {
        if (s->ticking[i]) {
            uint64_t delta;

B
bellard 已提交
95
            delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
96 97 98 99 100 101 102 103 104 105 106 107 108 109
            ldebug (
                "delta = %f dexp = %f expired => %d\n",
                delta / 1000000.0,
                s->dexp[i] / 1000000.0,
                delta >= s->dexp[i]
                );
            if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
                adlib_stop_opl_timer (s, i);
                AUD_init_time_stamp_out (s->voice, &s->ats);
            }
        }
    }
}

B
bellard 已提交
110 111 112 113 114 115 116
static IO_WRITE_PROTO(adlib_write)
{
    AdlibState *s = opaque;
    int a = nport & 3;
    int status;

    s->active = 1;
117
    AUD_set_active_out (s->voice, 1);
B
bellard 已提交
118

119 120 121
    adlib_kill_timers (s);

#ifdef HAS_YMF262
B
bellard 已提交
122 123 124 125 126 127 128 129 130 131 132 133
    status = YMF262Write (0, a, val);
#else
    status = OPLWrite (s->opl, a, val);
#endif
}

static IO_READ_PROTO(adlib_read)
{
    AdlibState *s = opaque;
    uint8_t data;
    int a = nport & 3;

134 135 136
    adlib_kill_timers (s);

#ifdef HAS_YMF262
B
bellard 已提交
137 138 139 140 141 142 143
    data = YMF262Read (0, a);
#else
    data = OPLRead (s->opl, a);
#endif
    return data;
}

144
static void timer_handler (int c, double interval_Sec)
B
bellard 已提交
145
{
B
bellard 已提交
146
    AdlibState *s = &glob_adlib;
147 148 149
    unsigned n = c & 1;
#ifdef DEBUG
    double interval;
B
bellard 已提交
150
    int64_t exp;
B
bellard 已提交
151 152 153
#endif

    if (interval_Sec == 0.0) {
154
        s->ticking[n] = 0;
B
bellard 已提交
155 156
        return;
    }
157 158 159 160 161 162 163 164 165 166

    s->ticking[n] = 1;
#ifdef DEBUG
    interval = ticks_per_sec * interval_Sec;
    exp = qemu_get_clock (vm_clock) + interval;
    s->exp[n] = exp;
#endif

    s->dexp[n] = interval_Sec * 1000000.0;
    AUD_init_time_stamp_out (s->voice, &s->ats);
B
bellard 已提交
167 168 169 170 171
}

static int write_audio (AdlibState *s, int samples)
{
    int net = 0;
172 173
    int pos = s->pos;

B
bellard 已提交
174
    while (samples) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        int nbytes, wbytes, wsampl;

        nbytes = samples << SHIFT;
        wbytes = AUD_write (
            s->voice,
            s->mixbuf + (pos << (SHIFT - 1)),
            nbytes
            );

        if (wbytes) {
            wsampl = wbytes >> SHIFT;

            samples -= wsampl;
            pos = (pos + wsampl) % s->samples;

            net += wsampl;
        }
        else {
B
bellard 已提交
193
            break;
194
        }
B
bellard 已提交
195
    }
196

B
bellard 已提交
197 198 199
    return net;
}

200
static void adlib_callback (void *opaque, int free)
B
bellard 已提交
201 202
{
    AdlibState *s = opaque;
203
    int samples, net = 0, to_play, written;
B
bellard 已提交
204

205 206 207
    samples = free >> SHIFT;
    if (!(s->active && s->enabled) || !samples) {
        return;
B
bellard 已提交
208 209
    }

210 211 212 213 214 215 216 217 218 219 220 221 222 223
    to_play = audio_MIN (s->left, samples);
    while (to_play) {
        written = write_audio (s, to_play);

        if (written) {
            s->left -= written;
            samples -= written;
            to_play -= written;
            s->pos = (s->pos + written) % s->samples;
        }
        else {
            return;
        }
    }
B
bellard 已提交
224 225

    samples = audio_MIN (samples, s->samples - s->pos);
226 227 228
    if (!samples) {
        return;
    }
B
bellard 已提交
229

230
#ifdef HAS_YMF262
B
bellard 已提交
231 232 233 234 235 236
    YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
#else
    YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
#endif

    while (samples) {
237 238 239 240 241 242 243 244 245 246 247
        written = write_audio (s, samples);

        if (written) {
            net += written;
            samples -= written;
            s->pos = (s->pos + written) % s->samples;
        }
        else {
            s->left = samples;
            return;
        }
B
bellard 已提交
248 249 250 251 252
    }
}

static void Adlib_fini (AdlibState *s)
{
253
#ifdef HAS_YMF262
B
bellard 已提交
254 255 256 257 258 259 260 261
    YMF262Shutdown ();
#else
    if (s->opl) {
        OPLDestroy (s->opl);
        s->opl = NULL;
    }
#endif

262 263 264
    if (s->mixbuf) {
        qemu_free (s->mixbuf);
    }
B
bellard 已提交
265 266 267

    s->active = 0;
    s->enabled = 0;
B
bellard 已提交
268
    AUD_remove_card (&s->card);
B
bellard 已提交
269 270
}

P
pbrook 已提交
271
int Adlib_init (AudioState *audio, qemu_irq *pic)
B
bellard 已提交
272
{
B
bellard 已提交
273 274 275 276 277 278 279
    AdlibState *s = &glob_adlib;
    audsettings_t as;

    if (!audio) {
        dolog ("No audio state\n");
        return -1;
    }
B
bellard 已提交
280

281
#ifdef HAS_YMF262
B
bellard 已提交
282 283
    if (YMF262Init (1, 14318180, conf.freq)) {
        dolog ("YMF262Init %d failed\n", conf.freq);
B
bellard 已提交
284
        return -1;
B
bellard 已提交
285 286
    }
    else {
287
        YMF262SetTimerHandler (0, timer_handler, 0);
B
bellard 已提交
288 289 290 291 292 293
        s->enabled = 1;
    }
#else
    s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
    if (!s->opl) {
        dolog ("OPLCreate %d failed\n", conf.freq);
B
bellard 已提交
294
        return -1;
B
bellard 已提交
295 296
    }
    else {
297
        OPLSetTimerHandler (s->opl, timer_handler, 0);
B
bellard 已提交
298 299 300 301
        s->enabled = 1;
    }
#endif

B
bellard 已提交
302 303 304
    as.freq = conf.freq;
    as.nchannels = SHIFT;
    as.fmt = AUD_FMT_S16;
B
bellard 已提交
305
    as.endianness = AUDIO_HOST_ENDIANNESS;
B
bellard 已提交
306 307 308

    AUD_register_card (audio, "adlib", &s->card);

309
    s->voice = AUD_open_out (
B
bellard 已提交
310
        &s->card,
311 312 313 314
        s->voice,
        "adlib",
        s,
        adlib_callback,
B
bellard 已提交
315
        &as
316
        );
B
bellard 已提交
317 318
    if (!s->voice) {
        Adlib_fini (s);
B
bellard 已提交
319
        return -1;
B
bellard 已提交
320 321
    }

322
    s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
B
bellard 已提交
323 324 325
    s->mixbuf = qemu_mallocz (s->samples << SHIFT);

    if (!s->mixbuf) {
B
bellard 已提交
326 327
        dolog ("Could not allocate mixing buffer, %d samples (each %d bytes)\n",
               s->samples, 1 << SHIFT);
B
bellard 已提交
328
        Adlib_fini (s);
B
bellard 已提交
329
        return -1;
B
bellard 已提交
330
    }
331

B
bellard 已提交
332 333 334 335 336 337 338 339
    register_ioport_read (0x388, 4, 1, adlib_read, s);
    register_ioport_write (0x388, 4, 1, adlib_write, s);

    register_ioport_read (conf.port, 4, 1, adlib_read, s);
    register_ioport_write (conf.port, 4, 1, adlib_write, s);

    register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
    register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
B
bellard 已提交
340 341

    return 0;
B
bellard 已提交
342
}