Blame view

hw/adlib.c 7.41 KB
bellard authored
1
/*
2
3
4
5
 * QEMU Proxy for OPL2/3 emulation by MAME team
 *
 * Copyright (c) 2004-2005 Vassili Karpov (malc)
 *
bellard authored
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
pbrook authored
25
26
#include "hw.h"
#include "audiodev.h"
27
#include "audio/audio.h"
28
#include "isa.h"
bellard authored
29
30
31
//#define DEBUG
32
33
#define ADLIB_KILL_TIMERS 1
34
35
36
37
#ifdef DEBUG
#include "qemu-timer.h"
#endif
38
39
40
41
42
43
#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif
bellard authored
44
45
#ifdef HAS_YMF262
bellard authored
46
#include "ymf262.h"
47
void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
bellard authored
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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 {
bellard authored
65
    QEMUSoundCard card;
66
    int ticking[2];
bellard authored
67
68
69
    int enabled;
    int active;
    int bufpos;
70
71
72
#ifdef DEBUG
    int64_t exp[2];
#endif
bellard authored
73
    int16_t *mixbuf;
74
75
76
77
78
    uint64_t dexp[2];
    SWVoiceOut *voice;
    int left, pos, samples;
    QEMUAudioTimeStamp ats;
#ifndef HAS_YMF262
bellard authored
79
80
81
82
    FM_OPL *opl;
#endif
} AdlibState;
bellard authored
83
static AdlibState glob_adlib;
bellard authored
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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;
bellard authored
103
            delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
            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);
            }
        }
    }
}
118
static IO_WRITE_PROTO (adlib_write)
bellard authored
119
120
121
122
123
124
{
    AdlibState *s = opaque;
    int a = nport & 3;
    int status;

    s->active = 1;
125
    AUD_set_active_out (s->voice, 1);
bellard authored
126
127
128
129
    adlib_kill_timers (s);

#ifdef HAS_YMF262
bellard authored
130
131
132
133
134
135
    status = YMF262Write (0, a, val);
#else
    status = OPLWrite (s->opl, a, val);
#endif
}
136
static IO_READ_PROTO (adlib_read)
bellard authored
137
138
139
140
141
{
    AdlibState *s = opaque;
    uint8_t data;
    int a = nport & 3;
142
143
144
    adlib_kill_timers (s);

#ifdef HAS_YMF262
bellard authored
145
146
147
148
149
150
151
    data = YMF262Read (0, a);
#else
    data = OPLRead (s->opl, a);
#endif
    return data;
}
152
static void timer_handler (int c, double interval_Sec)
bellard authored
153
{
bellard authored
154
    AdlibState *s = &glob_adlib;
155
156
157
    unsigned n = c & 1;
#ifdef DEBUG
    double interval;
bellard authored
158
    int64_t exp;
bellard authored
159
160
161
#endif

    if (interval_Sec == 0.0) {
162
        s->ticking[n] = 0;
bellard authored
163
164
        return;
    }
165
166
167
168
169
170
171
172
173
174

    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);
bellard authored
175
176
177
178
179
}

static int write_audio (AdlibState *s, int samples)
{
    int net = 0;
180
181
    int pos = s->pos;
bellard authored
182
    while (samples) {
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
        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 {
bellard authored
201
            break;
202
        }
bellard authored
203
    }
204
bellard authored
205
206
207
    return net;
}
208
static void adlib_callback (void *opaque, int free)
bellard authored
209
210
{
    AdlibState *s = opaque;
211
    int samples, net = 0, to_play, written;
bellard authored
212
213
214
215
    samples = free >> SHIFT;
    if (!(s->active && s->enabled) || !samples) {
        return;
bellard authored
216
217
    }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    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;
        }
    }
bellard authored
232
233

    samples = audio_MIN (samples, s->samples - s->pos);
234
235
236
    if (!samples) {
        return;
    }
bellard authored
237
238
#ifdef HAS_YMF262
bellard authored
239
240
241
242
243
244
    YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
#else
    YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
#endif

    while (samples) {
245
246
247
248
249
250
251
252
253
254
255
        written = write_audio (s, samples);

        if (written) {
            net += written;
            samples -= written;
            s->pos = (s->pos + written) % s->samples;
        }
        else {
            s->left = samples;
            return;
        }
bellard authored
256
257
258
259
260
    }
}

static void Adlib_fini (AdlibState *s)
{
261
#ifdef HAS_YMF262
bellard authored
262
263
264
265
266
267
268
269
    YMF262Shutdown ();
#else
    if (s->opl) {
        OPLDestroy (s->opl);
        s->opl = NULL;
    }
#endif
270
271
272
    if (s->mixbuf) {
        qemu_free (s->mixbuf);
    }
bellard authored
273
274
275

    s->active = 0;
    s->enabled = 0;
bellard authored
276
    AUD_remove_card (&s->card);
bellard authored
277
278
}
279
int Adlib_init (qemu_irq *pic)
bellard authored
280
{
bellard authored
281
    AdlibState *s = &glob_adlib;
malc authored
282
    struct audsettings as;
bellard authored
283
284
#ifdef HAS_YMF262
bellard authored
285
286
    if (YMF262Init (1, 14318180, conf.freq)) {
        dolog ("YMF262Init %d failed\n", conf.freq);
bellard authored
287
        return -1;
bellard authored
288
289
    }
    else {
290
        YMF262SetTimerHandler (0, timer_handler, 0);
bellard authored
291
292
293
294
295
296
        s->enabled = 1;
    }
#else
    s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
    if (!s->opl) {
        dolog ("OPLCreate %d failed\n", conf.freq);
bellard authored
297
        return -1;
bellard authored
298
299
    }
    else {
300
        OPLSetTimerHandler (s->opl, timer_handler, 0);
bellard authored
301
302
303
304
        s->enabled = 1;
    }
#endif
bellard authored
305
306
307
    as.freq = conf.freq;
    as.nchannels = SHIFT;
    as.fmt = AUD_FMT_S16;
308
    as.endianness = AUDIO_HOST_ENDIANNESS;
bellard authored
309
310
    AUD_register_card ("adlib", &s->card);
bellard authored
311
312
    s->voice = AUD_open_out (
bellard authored
313
        &s->card,
314
315
316
317
        s->voice,
        "adlib",
        s,
        adlib_callback,
318
        &as
319
        );
bellard authored
320
321
    if (!s->voice) {
        Adlib_fini (s);
bellard authored
322
        return -1;
bellard authored
323
324
    }
325
    s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
bellard authored
326
327
328
329
330
331
332
333
334
335
    s->mixbuf = qemu_mallocz (s->samples << SHIFT);

    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);
bellard authored
336
337

    return 0;
bellard authored
338
}