adlib.c 8.9 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.
 */
T
ths 已提交
24

25
#include "hw/hw.h"
P
Paolo Bonzini 已提交
26
#include "hw/audio/audio.h"
T
ths 已提交
27
#include "audio/audio.h"
P
Paolo Bonzini 已提交
28
#include "hw/isa/isa.h"
B
bellard 已提交
29

T
ths 已提交
30 31
//#define DEBUG

32 33
#define ADLIB_KILL_TIMERS 1

P
Paolo Bonzini 已提交
34 35 36 37 38 39
#ifdef HAS_YMF262
#define ADLIB_DESC "Yamaha YMF262 (OPL3)"
#else
#define ADLIB_DESC "Yamaha YM3812 (OPL2)"
#endif

T
ths 已提交
40
#ifdef DEBUG
41
#include "qemu/timer.h"
T
ths 已提交
42 43
#endif

B
bellard 已提交
44 45 46 47 48 49
#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif
B
bellard 已提交
50

51
#ifdef HAS_YMF262
B
bellard 已提交
52
#include "ymf262.h"
53
void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
B
bellard 已提交
54 55
#define SHIFT 2
#else
56
#include "fmopl.h"
B
bellard 已提交
57 58 59 60 61 62 63 64
#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)

P
Paolo Bonzini 已提交
65 66
#define TYPE_ADLIB "adlib"
#define ADLIB(obj) OBJECT_CHECK(AdlibState, (obj), TYPE_ADLIB)
B
bellard 已提交
67 68

typedef struct {
P
Paolo Bonzini 已提交
69 70
    ISADevice parent_obj;

B
bellard 已提交
71
    QEMUSoundCard card;
P
Paolo Bonzini 已提交
72 73
    uint32_t freq;
    uint32_t port;
74
    int ticking[2];
B
bellard 已提交
75 76 77
    int enabled;
    int active;
    int bufpos;
78 79 80
#ifdef DEBUG
    int64_t exp[2];
#endif
B
bellard 已提交
81
    int16_t *mixbuf;
82 83 84 85 86
    uint64_t dexp[2];
    SWVoiceOut *voice;
    int left, pos, samples;
    QEMUAudioTimeStamp ats;
#ifndef HAS_YMF262
B
bellard 已提交
87 88 89 90
    FM_OPL *opl;
#endif
} AdlibState;

P
Paolo Bonzini 已提交
91
static AdlibState *glob_adlib;
B
bellard 已提交
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
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 已提交
111
            delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
112 113 114 115 116 117 118 119 120 121 122 123 124 125
            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);
            }
        }
    }
}

126
static IO_WRITE_PROTO (adlib_write)
B
bellard 已提交
127 128 129 130 131
{
    AdlibState *s = opaque;
    int a = nport & 3;

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

134 135 136
    adlib_kill_timers (s);

#ifdef HAS_YMF262
137
    YMF262Write (0, a, val);
B
bellard 已提交
138
#else
139
    OPLWrite (s->opl, a, val);
B
bellard 已提交
140 141 142
#endif
}

143
static IO_READ_PROTO (adlib_read)
B
bellard 已提交
144 145 146 147 148
{
    AdlibState *s = opaque;
    uint8_t data;
    int a = nport & 3;

149 150 151
    adlib_kill_timers (s);

#ifdef HAS_YMF262
B
bellard 已提交
152 153 154 155 156 157 158
    data = YMF262Read (0, a);
#else
    data = OPLRead (s->opl, a);
#endif
    return data;
}

159
static void timer_handler (int c, double interval_Sec)
B
bellard 已提交
160
{
P
Paolo Bonzini 已提交
161
    AdlibState *s = glob_adlib;
162 163 164
    unsigned n = c & 1;
#ifdef DEBUG
    double interval;
B
bellard 已提交
165
    int64_t exp;
B
bellard 已提交
166 167 168
#endif

    if (interval_Sec == 0.0) {
169
        s->ticking[n] = 0;
B
bellard 已提交
170 171
        return;
    }
172 173 174

    s->ticking[n] = 1;
#ifdef DEBUG
M
malc 已提交
175
    interval = get_ticks_per_sec () * interval_Sec;
176
    exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval;
177 178 179 180 181
    s->exp[n] = exp;
#endif

    s->dexp[n] = interval_Sec * 1000000.0;
    AUD_init_time_stamp_out (s->voice, &s->ats);
B
bellard 已提交
182 183 184 185 186
}

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

B
bellard 已提交
189
    while (samples) {
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        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 已提交
208
            break;
209
        }
B
bellard 已提交
210
    }
211

B
bellard 已提交
212 213 214
    return net;
}

215
static void adlib_callback (void *opaque, int free)
B
bellard 已提交
216 217
{
    AdlibState *s = opaque;
218
    int samples, net = 0, to_play, written;
B
bellard 已提交
219

220 221 222
    samples = free >> SHIFT;
    if (!(s->active && s->enabled) || !samples) {
        return;
B
bellard 已提交
223 224
    }

225 226 227 228 229 230 231 232 233 234 235 236 237 238
    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 已提交
239 240

    samples = audio_MIN (samples, s->samples - s->pos);
241 242 243
    if (!samples) {
        return;
    }
B
bellard 已提交
244

245
#ifdef HAS_YMF262
B
bellard 已提交
246 247 248 249 250 251
    YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
#else
    YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
#endif

    while (samples) {
252 253 254 255 256 257 258 259 260 261 262
        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 已提交
263 264 265 266 267
    }
}

static void Adlib_fini (AdlibState *s)
{
268
#ifdef HAS_YMF262
B
bellard 已提交
269 270 271 272 273 274 275 276
    YMF262Shutdown ();
#else
    if (s->opl) {
        OPLDestroy (s->opl);
        s->opl = NULL;
    }
#endif

277
    if (s->mixbuf) {
278
        g_free (s->mixbuf);
279
    }
B
bellard 已提交
280 281 282

    s->active = 0;
    s->enabled = 0;
B
bellard 已提交
283
    AUD_remove_card (&s->card);
B
bellard 已提交
284 285
}

J
Jan Kiszka 已提交
286 287 288
static MemoryRegionPortio adlib_portio_list[] = {
    { 0, 4, 1, .read = adlib_read, .write = adlib_write, },
    { 0, 2, 1, .read = adlib_read, .write = adlib_write, },
289
    { 0x388, 4, 1, .read = adlib_read, .write = adlib_write, },
J
Jan Kiszka 已提交
290 291 292
    PORTIO_END_OF_LIST(),
};

293
static void adlib_realizefn (DeviceState *dev, Error **errp)
B
bellard 已提交
294
{
P
Paolo Bonzini 已提交
295
    AdlibState *s = ADLIB(dev);
J
Jan Kiszka 已提交
296
    PortioList *port_list = g_new(PortioList, 1);
M
malc 已提交
297
    struct audsettings as;
B
bellard 已提交
298

P
Paolo Bonzini 已提交
299
    if (glob_adlib) {
300 301
        error_setg (errp, "Cannot create more than 1 adlib device");
        return;
P
Paolo Bonzini 已提交
302 303 304
    }
    glob_adlib = s;

305
#ifdef HAS_YMF262
P
Paolo Bonzini 已提交
306
    if (YMF262Init (1, 14318180, s->freq)) {
307 308
        error_setg (errp, "YMF262Init %d failed", s->freq);
        return;
B
bellard 已提交
309 310
    }
    else {
311
        YMF262SetTimerHandler (0, timer_handler, 0);
B
bellard 已提交
312 313 314
        s->enabled = 1;
    }
#else
P
Paolo Bonzini 已提交
315
    s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, s->freq);
B
bellard 已提交
316
    if (!s->opl) {
317 318
        error_setg (errp, "OPLCreate %d failed", s->freq);
        return;
B
bellard 已提交
319 320
    }
    else {
321
        OPLSetTimerHandler (s->opl, timer_handler, 0);
B
bellard 已提交
322 323 324 325
        s->enabled = 1;
    }
#endif

P
Paolo Bonzini 已提交
326
    as.freq = s->freq;
B
bellard 已提交
327 328
    as.nchannels = SHIFT;
    as.fmt = AUD_FMT_S16;
B
bellard 已提交
329
    as.endianness = AUDIO_HOST_ENDIANNESS;
B
bellard 已提交
330

331
    AUD_register_card ("adlib", &s->card);
B
bellard 已提交
332

333
    s->voice = AUD_open_out (
B
bellard 已提交
334
        &s->card,
335 336 337 338
        s->voice,
        "adlib",
        s,
        adlib_callback,
B
bellard 已提交
339
        &as
340
        );
B
bellard 已提交
341 342
    if (!s->voice) {
        Adlib_fini (s);
343 344
        error_setg (errp, "Initializing audio voice failed");
        return;
B
bellard 已提交
345 346
    }

347
    s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
348
    s->mixbuf = g_malloc0 (s->samples << SHIFT);
B
bellard 已提交
349

J
Jan Kiszka 已提交
350 351
    adlib_portio_list[1].offset = s->port;
    adlib_portio_list[2].offset = s->port + 8;
352
    portio_list_init (port_list, OBJECT(s), adlib_portio_list, s, "adlib");
J
Jan Kiszka 已提交
353
    portio_list_add (port_list, isa_address_space_io(&s->parent_obj), 0);
B
bellard 已提交
354
}
P
Paolo Bonzini 已提交
355 356 357 358 359 360 361 362 363 364

static Property adlib_properties[] = {
    DEFINE_PROP_HEX32  ("iobase",  AdlibState, port, 0x220),
    DEFINE_PROP_UINT32 ("freq",    AdlibState, freq,  44100),
    DEFINE_PROP_END_OF_LIST (),
};

static void adlib_class_initfn (ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS (klass);
365 366

    dc->realize = adlib_realizefn;
367
    set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
P
Paolo Bonzini 已提交
368 369 370 371 372 373 374 375 376 377 378
    dc->desc = ADLIB_DESC;
    dc->props = adlib_properties;
}

static const TypeInfo adlib_info = {
    .name          = TYPE_ADLIB,
    .parent        = TYPE_ISA_DEVICE,
    .instance_size = sizeof (AdlibState),
    .class_init    = adlib_class_initfn,
};

379
static int Adlib_init (ISABus *bus)
P
Paolo Bonzini 已提交
380 381 382 383 384 385 386 387
{
    isa_create_simple (bus, TYPE_ADLIB);
    return 0;
}

static void adlib_register_types (void)
{
    type_register_static (&adlib_info);
388
    isa_register_soundhw("adlib", ADLIB_DESC, Adlib_init);
P
Paolo Bonzini 已提交
389 390 391
}

type_init (adlib_register_types)