Statistics
| Branch: | Revision:

root / hw / adlib.c @ 1d14ffa9

History | View | Annotate | Download (7.2 kB)

1 85571bc7 bellard
/*
2 1d14ffa9 bellard
 * QEMU Proxy for OPL2/3 emulation by MAME team
3 1d14ffa9 bellard
 *
4 1d14ffa9 bellard
 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 1d14ffa9 bellard
 *
6 85571bc7 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 85571bc7 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 85571bc7 bellard
 * in the Software without restriction, including without limitation the rights
9 85571bc7 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 85571bc7 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 85571bc7 bellard
 * furnished to do so, subject to the following conditions:
12 85571bc7 bellard
 *
13 85571bc7 bellard
 * The above copyright notice and this permission notice shall be included in
14 85571bc7 bellard
 * all copies or substantial portions of the Software.
15 85571bc7 bellard
 *
16 85571bc7 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 85571bc7 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 85571bc7 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 85571bc7 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 85571bc7 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 85571bc7 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 85571bc7 bellard
 * THE SOFTWARE.
23 85571bc7 bellard
 */
24 1d14ffa9 bellard
#include <assert.h>
25 85571bc7 bellard
#include "vl.h"
26 85571bc7 bellard
27 1d14ffa9 bellard
#define ADLIB_KILL_TIMERS 1
28 1d14ffa9 bellard
29 fb065187 bellard
#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
30 fb065187 bellard
#ifdef DEBUG
31 fb065187 bellard
#define ldebug(...) dolog (__VA_ARGS__)
32 fb065187 bellard
#else
33 fb065187 bellard
#define ldebug(...)
34 fb065187 bellard
#endif
35 85571bc7 bellard
36 1d14ffa9 bellard
#ifdef HAS_YMF262
37 85571bc7 bellard
#include "ymf262.h"
38 1d14ffa9 bellard
void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
39 85571bc7 bellard
#define SHIFT 2
40 85571bc7 bellard
#else
41 85571bc7 bellard
#include "fmopl.h"
42 85571bc7 bellard
#define SHIFT 1
43 85571bc7 bellard
#endif
44 85571bc7 bellard
45 85571bc7 bellard
#define IO_READ_PROTO(name) \
46 85571bc7 bellard
    uint32_t name (void *opaque, uint32_t nport)
47 85571bc7 bellard
#define IO_WRITE_PROTO(name) \
48 85571bc7 bellard
    void name (void *opaque, uint32_t nport, uint32_t val)
49 85571bc7 bellard
50 85571bc7 bellard
static struct {
51 85571bc7 bellard
    int port;
52 85571bc7 bellard
    int freq;
53 85571bc7 bellard
} conf = {0x220, 44100};
54 85571bc7 bellard
55 85571bc7 bellard
typedef struct {
56 1d14ffa9 bellard
    int ticking[2];
57 85571bc7 bellard
    int enabled;
58 85571bc7 bellard
    int active;
59 85571bc7 bellard
    int bufpos;
60 1d14ffa9 bellard
#ifdef DEBUG
61 1d14ffa9 bellard
    int64_t exp[2];
62 1d14ffa9 bellard
#endif
63 85571bc7 bellard
    int16_t *mixbuf;
64 1d14ffa9 bellard
    uint64_t dexp[2];
65 1d14ffa9 bellard
    SWVoiceOut *voice;
66 1d14ffa9 bellard
    int left, pos, samples;
67 1d14ffa9 bellard
    QEMUAudioTimeStamp ats;
68 1d14ffa9 bellard
#ifndef HAS_YMF262
69 85571bc7 bellard
    FM_OPL *opl;
70 85571bc7 bellard
#endif
71 85571bc7 bellard
} AdlibState;
72 85571bc7 bellard
73 85571bc7 bellard
static AdlibState adlib;
74 85571bc7 bellard
75 1d14ffa9 bellard
static void adlib_stop_opl_timer (AdlibState *s, size_t n)
76 1d14ffa9 bellard
{
77 1d14ffa9 bellard
#ifdef HAS_YMF262
78 1d14ffa9 bellard
    YMF262TimerOver (0, n);
79 1d14ffa9 bellard
#else
80 1d14ffa9 bellard
    OPLTimerOver (s->opl, n);
81 1d14ffa9 bellard
#endif
82 1d14ffa9 bellard
    s->ticking[n] = 0;
83 1d14ffa9 bellard
}
84 1d14ffa9 bellard
85 1d14ffa9 bellard
static void adlib_kill_timers (AdlibState *s)
86 1d14ffa9 bellard
{
87 1d14ffa9 bellard
    size_t i;
88 1d14ffa9 bellard
89 1d14ffa9 bellard
    for (i = 0; i < 2; ++i) {
90 1d14ffa9 bellard
        if (s->ticking[i]) {
91 1d14ffa9 bellard
            uint64_t delta;
92 1d14ffa9 bellard
93 1d14ffa9 bellard
            delta = AUD_time_stamp_get_elapsed_usec_out (s->voice, &s->ats);
94 1d14ffa9 bellard
            ldebug (
95 1d14ffa9 bellard
                "delta = %f dexp = %f expired => %d\n",
96 1d14ffa9 bellard
                delta / 1000000.0,
97 1d14ffa9 bellard
                s->dexp[i] / 1000000.0,
98 1d14ffa9 bellard
                delta >= s->dexp[i]
99 1d14ffa9 bellard
                );
100 1d14ffa9 bellard
            if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
101 1d14ffa9 bellard
                adlib_stop_opl_timer (s, i);
102 1d14ffa9 bellard
                AUD_init_time_stamp_out (s->voice, &s->ats);
103 1d14ffa9 bellard
            }
104 1d14ffa9 bellard
        }
105 1d14ffa9 bellard
    }
106 1d14ffa9 bellard
}
107 1d14ffa9 bellard
108 85571bc7 bellard
static IO_WRITE_PROTO(adlib_write)
109 85571bc7 bellard
{
110 85571bc7 bellard
    AdlibState *s = opaque;
111 85571bc7 bellard
    int a = nport & 3;
112 85571bc7 bellard
    int status;
113 85571bc7 bellard
114 85571bc7 bellard
    s->active = 1;
115 1d14ffa9 bellard
    AUD_set_active_out (s->voice, 1);
116 85571bc7 bellard
117 1d14ffa9 bellard
    adlib_kill_timers (s);
118 1d14ffa9 bellard
119 1d14ffa9 bellard
#ifdef HAS_YMF262
120 85571bc7 bellard
    status = YMF262Write (0, a, val);
121 85571bc7 bellard
#else
122 85571bc7 bellard
    status = OPLWrite (s->opl, a, val);
123 85571bc7 bellard
#endif
124 85571bc7 bellard
}
125 85571bc7 bellard
126 85571bc7 bellard
static IO_READ_PROTO(adlib_read)
127 85571bc7 bellard
{
128 85571bc7 bellard
    AdlibState *s = opaque;
129 85571bc7 bellard
    uint8_t data;
130 85571bc7 bellard
    int a = nport & 3;
131 85571bc7 bellard
132 1d14ffa9 bellard
    adlib_kill_timers (s);
133 1d14ffa9 bellard
134 1d14ffa9 bellard
#ifdef HAS_YMF262
135 85571bc7 bellard
    data = YMF262Read (0, a);
136 85571bc7 bellard
#else
137 85571bc7 bellard
    data = OPLRead (s->opl, a);
138 85571bc7 bellard
#endif
139 85571bc7 bellard
    return data;
140 85571bc7 bellard
}
141 85571bc7 bellard
142 1d14ffa9 bellard
static void timer_handler (int c, double interval_Sec)
143 85571bc7 bellard
{
144 1d14ffa9 bellard
    AdlibState *s = &adlib;
145 1d14ffa9 bellard
    unsigned n = c & 1;
146 1d14ffa9 bellard
#ifdef DEBUG
147 1d14ffa9 bellard
    double interval;
148 85571bc7 bellard
#endif
149 85571bc7 bellard
150 85571bc7 bellard
    if (interval_Sec == 0.0) {
151 1d14ffa9 bellard
        s->ticking[n] = 0;
152 85571bc7 bellard
        return;
153 85571bc7 bellard
    }
154 1d14ffa9 bellard
155 1d14ffa9 bellard
    s->ticking[n] = 1;
156 1d14ffa9 bellard
#ifdef DEBUG
157 1d14ffa9 bellard
    interval = ticks_per_sec * interval_Sec;
158 1d14ffa9 bellard
    exp = qemu_get_clock (vm_clock) + interval;
159 1d14ffa9 bellard
    s->exp[n] = exp;
160 1d14ffa9 bellard
#endif
161 1d14ffa9 bellard
162 1d14ffa9 bellard
    s->dexp[n] = interval_Sec * 1000000.0;
163 1d14ffa9 bellard
    AUD_init_time_stamp_out (s->voice, &s->ats);
164 85571bc7 bellard
}
165 85571bc7 bellard
166 85571bc7 bellard
static int write_audio (AdlibState *s, int samples)
167 85571bc7 bellard
{
168 85571bc7 bellard
    int net = 0;
169 1d14ffa9 bellard
    int pos = s->pos;
170 1d14ffa9 bellard
171 85571bc7 bellard
    while (samples) {
172 1d14ffa9 bellard
        int nbytes, wbytes, wsampl;
173 1d14ffa9 bellard
174 1d14ffa9 bellard
        nbytes = samples << SHIFT;
175 1d14ffa9 bellard
        wbytes = AUD_write (
176 1d14ffa9 bellard
            s->voice,
177 1d14ffa9 bellard
            s->mixbuf + (pos << (SHIFT - 1)),
178 1d14ffa9 bellard
            nbytes
179 1d14ffa9 bellard
            );
180 1d14ffa9 bellard
181 1d14ffa9 bellard
        if (wbytes) {
182 1d14ffa9 bellard
            wsampl = wbytes >> SHIFT;
183 1d14ffa9 bellard
184 1d14ffa9 bellard
            samples -= wsampl;
185 1d14ffa9 bellard
            pos = (pos + wsampl) % s->samples;
186 1d14ffa9 bellard
187 1d14ffa9 bellard
            net += wsampl;
188 1d14ffa9 bellard
        }
189 1d14ffa9 bellard
        else {
190 85571bc7 bellard
            break;
191 1d14ffa9 bellard
        }
192 85571bc7 bellard
    }
193 1d14ffa9 bellard
194 85571bc7 bellard
    return net;
195 85571bc7 bellard
}
196 85571bc7 bellard
197 1d14ffa9 bellard
static void adlib_callback (void *opaque, int free)
198 85571bc7 bellard
{
199 85571bc7 bellard
    AdlibState *s = opaque;
200 1d14ffa9 bellard
    int samples, net = 0, to_play, written;
201 85571bc7 bellard
202 1d14ffa9 bellard
    samples = free >> SHIFT;
203 1d14ffa9 bellard
    if (!(s->active && s->enabled) || !samples) {
204 1d14ffa9 bellard
        return;
205 85571bc7 bellard
    }
206 85571bc7 bellard
207 1d14ffa9 bellard
    to_play = audio_MIN (s->left, samples);
208 1d14ffa9 bellard
    while (to_play) {
209 1d14ffa9 bellard
        written = write_audio (s, to_play);
210 1d14ffa9 bellard
211 1d14ffa9 bellard
        if (written) {
212 1d14ffa9 bellard
            s->left -= written;
213 1d14ffa9 bellard
            samples -= written;
214 1d14ffa9 bellard
            to_play -= written;
215 1d14ffa9 bellard
            s->pos = (s->pos + written) % s->samples;
216 1d14ffa9 bellard
        }
217 1d14ffa9 bellard
        else {
218 1d14ffa9 bellard
            return;
219 1d14ffa9 bellard
        }
220 1d14ffa9 bellard
    }
221 85571bc7 bellard
222 85571bc7 bellard
    samples = audio_MIN (samples, s->samples - s->pos);
223 1d14ffa9 bellard
    if (!samples) {
224 1d14ffa9 bellard
        return;
225 1d14ffa9 bellard
    }
226 85571bc7 bellard
227 1d14ffa9 bellard
#ifdef HAS_YMF262
228 85571bc7 bellard
    YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
229 85571bc7 bellard
#else
230 85571bc7 bellard
    YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
231 85571bc7 bellard
#endif
232 85571bc7 bellard
233 85571bc7 bellard
    while (samples) {
234 1d14ffa9 bellard
        written = write_audio (s, samples);
235 1d14ffa9 bellard
236 1d14ffa9 bellard
        if (written) {
237 1d14ffa9 bellard
            net += written;
238 1d14ffa9 bellard
            samples -= written;
239 1d14ffa9 bellard
            s->pos = (s->pos + written) % s->samples;
240 1d14ffa9 bellard
        }
241 1d14ffa9 bellard
        else {
242 1d14ffa9 bellard
            s->left = samples;
243 1d14ffa9 bellard
            return;
244 1d14ffa9 bellard
        }
245 85571bc7 bellard
    }
246 85571bc7 bellard
}
247 85571bc7 bellard
248 85571bc7 bellard
static void Adlib_fini (AdlibState *s)
249 85571bc7 bellard
{
250 1d14ffa9 bellard
#ifdef HAS_YMF262
251 85571bc7 bellard
    YMF262Shutdown ();
252 85571bc7 bellard
#else
253 85571bc7 bellard
    if (s->opl) {
254 85571bc7 bellard
        OPLDestroy (s->opl);
255 85571bc7 bellard
        s->opl = NULL;
256 85571bc7 bellard
    }
257 85571bc7 bellard
#endif
258 85571bc7 bellard
259 1d14ffa9 bellard
    if (s->mixbuf) {
260 1d14ffa9 bellard
        qemu_free (s->mixbuf);
261 1d14ffa9 bellard
    }
262 85571bc7 bellard
263 85571bc7 bellard
    s->active = 0;
264 85571bc7 bellard
    s->enabled = 0;
265 85571bc7 bellard
}
266 85571bc7 bellard
267 85571bc7 bellard
void Adlib_init (void)
268 85571bc7 bellard
{
269 85571bc7 bellard
    AdlibState *s = &adlib;
270 85571bc7 bellard
271 1d14ffa9 bellard
#ifdef HAS_YMF262
272 85571bc7 bellard
    if (YMF262Init (1, 14318180, conf.freq)) {
273 85571bc7 bellard
        dolog ("YMF262Init %d failed\n", conf.freq);
274 85571bc7 bellard
        return;
275 85571bc7 bellard
    }
276 85571bc7 bellard
    else {
277 1d14ffa9 bellard
        YMF262SetTimerHandler (0, timer_handler, 0);
278 85571bc7 bellard
        s->enabled = 1;
279 85571bc7 bellard
    }
280 85571bc7 bellard
#else
281 85571bc7 bellard
    s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
282 85571bc7 bellard
    if (!s->opl) {
283 85571bc7 bellard
        dolog ("OPLCreate %d failed\n", conf.freq);
284 85571bc7 bellard
        return;
285 85571bc7 bellard
    }
286 85571bc7 bellard
    else {
287 1d14ffa9 bellard
        OPLSetTimerHandler (s->opl, timer_handler, 0);
288 85571bc7 bellard
        s->enabled = 1;
289 85571bc7 bellard
    }
290 85571bc7 bellard
#endif
291 85571bc7 bellard
292 1d14ffa9 bellard
    s->voice = AUD_open_out (
293 1d14ffa9 bellard
        s->voice,
294 1d14ffa9 bellard
        "adlib",
295 1d14ffa9 bellard
        s,
296 1d14ffa9 bellard
        adlib_callback,
297 1d14ffa9 bellard
        conf.freq,
298 1d14ffa9 bellard
        SHIFT,
299 1d14ffa9 bellard
        AUD_FMT_S16
300 1d14ffa9 bellard
        );
301 85571bc7 bellard
    if (!s->voice) {
302 85571bc7 bellard
        Adlib_fini (s);
303 85571bc7 bellard
        return;
304 85571bc7 bellard
    }
305 85571bc7 bellard
306 1d14ffa9 bellard
    s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
307 85571bc7 bellard
    s->mixbuf = qemu_mallocz (s->samples << SHIFT);
308 85571bc7 bellard
309 85571bc7 bellard
    if (!s->mixbuf) {
310 85571bc7 bellard
        dolog ("not enough memory for adlib mixing buffer (%d)\n",
311 85571bc7 bellard
               s->samples << SHIFT);
312 85571bc7 bellard
        Adlib_fini (s);
313 85571bc7 bellard
        return;
314 85571bc7 bellard
    }
315 1d14ffa9 bellard
316 85571bc7 bellard
    register_ioport_read (0x388, 4, 1, adlib_read, s);
317 85571bc7 bellard
    register_ioport_write (0x388, 4, 1, adlib_write, s);
318 85571bc7 bellard
319 85571bc7 bellard
    register_ioport_read (conf.port, 4, 1, adlib_read, s);
320 85571bc7 bellard
    register_ioport_write (conf.port, 4, 1, adlib_write, s);
321 85571bc7 bellard
322 85571bc7 bellard
    register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
323 85571bc7 bellard
    register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
324 85571bc7 bellard
}