summaryrefslogtreecommitdiff
path: root/hw/adlib.c
blob: 939a7ed0360fea7e92102300a717d9de7a334e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * QEMU Adlib emulation
 * 
 * Copyright (c) 2004 Vassili Karpov (malc)
 * 
 * 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.
 */
#include "vl.h"

#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
#ifdef DEBUG
#define ldebug(...) dolog (__VA_ARGS__)
#else
#define ldebug(...)
#endif

#ifdef USE_YMF262
#define HAS_YMF262 1
#include "ymf262.h"
void YMF262UpdateOneQEMU(int which, INT16 *dst, int length);
#define SHIFT 2
#else
#include "fmopl.h"
#define SHIFT 1
#endif

#ifdef _WIN32
#include <windows.h>
#define small_delay() Sleep (1)
#else
#define small_delay() usleep (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 {
    int enabled;
    int active;
    int cparam;
    int64_t ticks;
    int bufpos;
    int16_t *mixbuf;
    double interval;
    QEMUTimer *ts, *opl_ts;
    SWVoice *voice;
    int left, pos, samples, bytes_per_second, old_free;
    int refcount;
#ifndef USE_YMF262
    FM_OPL *opl;
#endif
} AdlibState;

static AdlibState adlib;

static IO_WRITE_PROTO(adlib_write)
{
    AdlibState *s = opaque;
    int a = nport & 3;
    int status;

    s->ticks = qemu_get_clock (vm_clock);
    s->active = 1;
    AUD_enable (s->voice, 1);

#ifdef USE_YMF262
    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;

#ifdef USE_YMF262
    (void) s;
    data = YMF262Read (0, a);
#else
    data = OPLRead (s->opl, a);
#endif
    return data;
}

static void OPL_timer (void *opaque)
{
    AdlibState *s = opaque;
#ifdef USE_YMF262
    YMF262TimerOver (s->cparam >> 1, s->cparam & 1);
#else
    OPLTimerOver (s->opl, s->cparam);
#endif
    qemu_mod_timer (s->opl_ts, qemu_get_clock (vm_clock) + s->interval);
}

static void YMF262TimerHandler (int c, double interval_Sec)
{
    AdlibState *s = &adlib;
    if (interval_Sec == 0.0) {
        qemu_del_timer (s->opl_ts);
        return;
    }
    s->cparam = c;
    s->interval = ticks_per_sec * interval_Sec;
    qemu_mod_timer (s->opl_ts, qemu_get_clock (vm_clock) + s->interval);
    small_delay ();
}

static int write_audio (AdlibState *s, int samples)
{
    int net = 0;
    int ss = samples;
    while (samples) {
        int nbytes = samples << SHIFT;
        int wbytes = AUD_write (s->voice,
                                s->mixbuf + (s->pos << (SHIFT - 1)),
                                nbytes);
        int wsampl = wbytes >> SHIFT;
        samples -= wsampl;
        s->pos = (s->pos + wsampl) % s->samples;
        net += wsampl;
        if (!wbytes)
            break;
    }
    if (net > ss) {
        dolog ("WARNING: net > ss\n");
    }
    return net;
}

static void timer (void *opaque)
{
    AdlibState *s = opaque;
    int elapsed, samples, net = 0;

    if (s->refcount)
        dolog ("refcount=%d\n", s->refcount);

    s->refcount += 1;
    if (!(s->active && s->enabled))
        goto reset;

    AUD_run ();

    while (s->left) {
        int written = write_audio (s, s->left);
        net += written;
        if (!written)
            goto reset2;
        s->left -= written;
    }
    s->pos = 0;

    elapsed = AUD_calc_elapsed (s->voice);
    if (!elapsed)
        goto reset2;

    /* elapsed = AUD_get_free (s->voice); */
    samples = elapsed >> SHIFT;
    if (!samples)
        goto reset2;

    samples = audio_MIN (samples, s->samples - s->pos);
    if (s->left)
        dolog ("left=%d samples=%d elapsed=%d free=%d\n",
               s->left, samples, elapsed, AUD_get_free (s->voice));

    if (!samples)
        goto reset2;

#ifdef USE_YMF262
    YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
#else
    YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
#endif

    while (samples) {
        int written = write_audio (s, samples);
        net += written;
        if (!written)
            break;
        samples -= written;
    }
    if (!samples)
        s->pos = 0;
    s->left = samples;

reset2:
    AUD_adjust (s->voice, net << SHIFT);
reset:
    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + ticks_per_sec / 1024);
    s->refcount -= 1;
}

static void Adlib_fini (AdlibState *s)
{
#ifdef USE_YMF262
    YMF262Shutdown ();
#else
    if (s->opl) {
        OPLDestroy (s->opl);
        s->opl = NULL;
    }
#endif

    if (s->opl_ts)
        qemu_free_timer (s->opl_ts);

    if (s->ts)
        qemu_free_timer (s->ts);

#define maybe_free(p) if (p) qemu_free (p)
    maybe_free (s->mixbuf);
#undef maybe_free

    s->active = 0;
    s->enabled = 0;
}

void Adlib_init (void)
{
    AdlibState *s = &adlib;

    memset (s, 0, sizeof (*s));

#ifdef USE_YMF262
    if (YMF262Init (1, 14318180, conf.freq)) {
        dolog ("YMF262Init %d failed\n", conf.freq);
        return;
    }
    else {
        YMF262SetTimerHandler (0, YMF262TimerHandler, 0);
        s->enabled = 1;
    }
#else
    s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
    if (!s->opl) {
        dolog ("OPLCreate %d failed\n", conf.freq);
        return;
    }
    else {
        OPLSetTimerHandler (s->opl, YMF262TimerHandler, 0);
        s->enabled = 1;
    }
#endif

    s->opl_ts = qemu_new_timer (vm_clock, OPL_timer, s);
    if (!s->opl_ts) {
        dolog ("Can not get timer for adlib emulation\n");
        Adlib_fini (s);
        return;
    }

    s->ts = qemu_new_timer (vm_clock, timer, s);
    if (!s->opl_ts) {
        dolog ("Can not get timer for adlib emulation\n");
        Adlib_fini (s);
        return;
    }

    s->voice = AUD_open (s->voice, "adlib", conf.freq, SHIFT, AUD_FMT_S16);
    if (!s->voice) {
        Adlib_fini (s);
        return;
    }

    s->bytes_per_second = conf.freq << SHIFT;
    s->samples = AUD_get_buffer_size (s->voice) >> SHIFT;
    s->mixbuf = qemu_mallocz (s->samples << SHIFT);

    if (!s->mixbuf) {
        dolog ("not enough memory for adlib mixing buffer (%d)\n",
               s->samples << SHIFT);
        Adlib_fini (s);
        return;
    }
    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);

    qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + 1);
}