Statistics
| Branch: | Revision:

root / hw / adlib.c @ 3871481c

History | View | Annotate | Download (7.4 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 9f0683d9 ths
25 87ecb68b pbrook
#include "hw.h"
26 87ecb68b pbrook
#include "audiodev.h"
27 e140e05c ths
#include "audio/audio.h"
28 69b34976 ths
#include "isa.h"
29 85571bc7 bellard
30 9f0683d9 ths
//#define DEBUG
31 9f0683d9 ths
32 1d14ffa9 bellard
#define ADLIB_KILL_TIMERS 1
33 1d14ffa9 bellard
34 9f0683d9 ths
#ifdef DEBUG
35 9f0683d9 ths
#include "qemu-timer.h"
36 9f0683d9 ths
#endif
37 9f0683d9 ths
38 fb065187 bellard
#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
39 fb065187 bellard
#ifdef DEBUG
40 fb065187 bellard
#define ldebug(...) dolog (__VA_ARGS__)
41 fb065187 bellard
#else
42 fb065187 bellard
#define ldebug(...)
43 fb065187 bellard
#endif
44 85571bc7 bellard
45 1d14ffa9 bellard
#ifdef HAS_YMF262
46 85571bc7 bellard
#include "ymf262.h"
47 1d14ffa9 bellard
void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
48 85571bc7 bellard
#define SHIFT 2
49 85571bc7 bellard
#else
50 85571bc7 bellard
#include "fmopl.h"
51 85571bc7 bellard
#define SHIFT 1
52 85571bc7 bellard
#endif
53 85571bc7 bellard
54 85571bc7 bellard
#define IO_READ_PROTO(name) \
55 85571bc7 bellard
    uint32_t name (void *opaque, uint32_t nport)
56 85571bc7 bellard
#define IO_WRITE_PROTO(name) \
57 85571bc7 bellard
    void name (void *opaque, uint32_t nport, uint32_t val)
58 85571bc7 bellard
59 85571bc7 bellard
static struct {
60 85571bc7 bellard
    int port;
61 85571bc7 bellard
    int freq;
62 85571bc7 bellard
} conf = {0x220, 44100};
63 85571bc7 bellard
64 85571bc7 bellard
typedef struct {
65 c0fe3827 bellard
    QEMUSoundCard card;
66 1d14ffa9 bellard
    int ticking[2];
67 85571bc7 bellard
    int enabled;
68 85571bc7 bellard
    int active;
69 85571bc7 bellard
    int bufpos;
70 1d14ffa9 bellard
#ifdef DEBUG
71 1d14ffa9 bellard
    int64_t exp[2];
72 1d14ffa9 bellard
#endif
73 85571bc7 bellard
    int16_t *mixbuf;
74 1d14ffa9 bellard
    uint64_t dexp[2];
75 1d14ffa9 bellard
    SWVoiceOut *voice;
76 1d14ffa9 bellard
    int left, pos, samples;
77 1d14ffa9 bellard
    QEMUAudioTimeStamp ats;
78 1d14ffa9 bellard
#ifndef HAS_YMF262
79 85571bc7 bellard
    FM_OPL *opl;
80 85571bc7 bellard
#endif
81 85571bc7 bellard
} AdlibState;
82 85571bc7 bellard
83 c0fe3827 bellard
static AdlibState glob_adlib;
84 85571bc7 bellard
85 1d14ffa9 bellard
static void adlib_stop_opl_timer (AdlibState *s, size_t n)
86 1d14ffa9 bellard
{
87 1d14ffa9 bellard
#ifdef HAS_YMF262
88 1d14ffa9 bellard
    YMF262TimerOver (0, n);
89 1d14ffa9 bellard
#else
90 1d14ffa9 bellard
    OPLTimerOver (s->opl, n);
91 1d14ffa9 bellard
#endif
92 1d14ffa9 bellard
    s->ticking[n] = 0;
93 1d14ffa9 bellard
}
94 1d14ffa9 bellard
95 1d14ffa9 bellard
static void adlib_kill_timers (AdlibState *s)
96 1d14ffa9 bellard
{
97 1d14ffa9 bellard
    size_t i;
98 1d14ffa9 bellard
99 1d14ffa9 bellard
    for (i = 0; i < 2; ++i) {
100 1d14ffa9 bellard
        if (s->ticking[i]) {
101 1d14ffa9 bellard
            uint64_t delta;
102 1d14ffa9 bellard
103 c0fe3827 bellard
            delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
104 1d14ffa9 bellard
            ldebug (
105 1d14ffa9 bellard
                "delta = %f dexp = %f expired => %d\n",
106 1d14ffa9 bellard
                delta / 1000000.0,
107 1d14ffa9 bellard
                s->dexp[i] / 1000000.0,
108 1d14ffa9 bellard
                delta >= s->dexp[i]
109 1d14ffa9 bellard
                );
110 1d14ffa9 bellard
            if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
111 1d14ffa9 bellard
                adlib_stop_opl_timer (s, i);
112 1d14ffa9 bellard
                AUD_init_time_stamp_out (s->voice, &s->ats);
113 1d14ffa9 bellard
            }
114 1d14ffa9 bellard
        }
115 1d14ffa9 bellard
    }
116 1d14ffa9 bellard
}
117 1d14ffa9 bellard
118 d999f7e0 malc
static IO_WRITE_PROTO (adlib_write)
119 85571bc7 bellard
{
120 85571bc7 bellard
    AdlibState *s = opaque;
121 85571bc7 bellard
    int a = nport & 3;
122 85571bc7 bellard
123 85571bc7 bellard
    s->active = 1;
124 1d14ffa9 bellard
    AUD_set_active_out (s->voice, 1);
125 85571bc7 bellard
126 1d14ffa9 bellard
    adlib_kill_timers (s);
127 1d14ffa9 bellard
128 1d14ffa9 bellard
#ifdef HAS_YMF262
129 e8beeae4 Hervé Poussineau
    YMF262Write (0, a, val);
130 85571bc7 bellard
#else
131 e8beeae4 Hervé Poussineau
    OPLWrite (s->opl, a, val);
132 85571bc7 bellard
#endif
133 85571bc7 bellard
}
134 85571bc7 bellard
135 d999f7e0 malc
static IO_READ_PROTO (adlib_read)
136 85571bc7 bellard
{
137 85571bc7 bellard
    AdlibState *s = opaque;
138 85571bc7 bellard
    uint8_t data;
139 85571bc7 bellard
    int a = nport & 3;
140 85571bc7 bellard
141 1d14ffa9 bellard
    adlib_kill_timers (s);
142 1d14ffa9 bellard
143 1d14ffa9 bellard
#ifdef HAS_YMF262
144 85571bc7 bellard
    data = YMF262Read (0, a);
145 85571bc7 bellard
#else
146 85571bc7 bellard
    data = OPLRead (s->opl, a);
147 85571bc7 bellard
#endif
148 85571bc7 bellard
    return data;
149 85571bc7 bellard
}
150 85571bc7 bellard
151 1d14ffa9 bellard
static void timer_handler (int c, double interval_Sec)
152 85571bc7 bellard
{
153 c0fe3827 bellard
    AdlibState *s = &glob_adlib;
154 1d14ffa9 bellard
    unsigned n = c & 1;
155 1d14ffa9 bellard
#ifdef DEBUG
156 1d14ffa9 bellard
    double interval;
157 c0fe3827 bellard
    int64_t exp;
158 85571bc7 bellard
#endif
159 85571bc7 bellard
160 85571bc7 bellard
    if (interval_Sec == 0.0) {
161 1d14ffa9 bellard
        s->ticking[n] = 0;
162 85571bc7 bellard
        return;
163 85571bc7 bellard
    }
164 1d14ffa9 bellard
165 1d14ffa9 bellard
    s->ticking[n] = 1;
166 1d14ffa9 bellard
#ifdef DEBUG
167 cf4dc461 malc
    interval = get_ticks_per_sec () * interval_Sec;
168 74475455 Paolo Bonzini
    exp = qemu_get_clock_ns (vm_clock) + interval;
169 1d14ffa9 bellard
    s->exp[n] = exp;
170 1d14ffa9 bellard
#endif
171 1d14ffa9 bellard
172 1d14ffa9 bellard
    s->dexp[n] = interval_Sec * 1000000.0;
173 1d14ffa9 bellard
    AUD_init_time_stamp_out (s->voice, &s->ats);
174 85571bc7 bellard
}
175 85571bc7 bellard
176 85571bc7 bellard
static int write_audio (AdlibState *s, int samples)
177 85571bc7 bellard
{
178 85571bc7 bellard
    int net = 0;
179 1d14ffa9 bellard
    int pos = s->pos;
180 1d14ffa9 bellard
181 85571bc7 bellard
    while (samples) {
182 1d14ffa9 bellard
        int nbytes, wbytes, wsampl;
183 1d14ffa9 bellard
184 1d14ffa9 bellard
        nbytes = samples << SHIFT;
185 1d14ffa9 bellard
        wbytes = AUD_write (
186 1d14ffa9 bellard
            s->voice,
187 1d14ffa9 bellard
            s->mixbuf + (pos << (SHIFT - 1)),
188 1d14ffa9 bellard
            nbytes
189 1d14ffa9 bellard
            );
190 1d14ffa9 bellard
191 1d14ffa9 bellard
        if (wbytes) {
192 1d14ffa9 bellard
            wsampl = wbytes >> SHIFT;
193 1d14ffa9 bellard
194 1d14ffa9 bellard
            samples -= wsampl;
195 1d14ffa9 bellard
            pos = (pos + wsampl) % s->samples;
196 1d14ffa9 bellard
197 1d14ffa9 bellard
            net += wsampl;
198 1d14ffa9 bellard
        }
199 1d14ffa9 bellard
        else {
200 85571bc7 bellard
            break;
201 1d14ffa9 bellard
        }
202 85571bc7 bellard
    }
203 1d14ffa9 bellard
204 85571bc7 bellard
    return net;
205 85571bc7 bellard
}
206 85571bc7 bellard
207 1d14ffa9 bellard
static void adlib_callback (void *opaque, int free)
208 85571bc7 bellard
{
209 85571bc7 bellard
    AdlibState *s = opaque;
210 1d14ffa9 bellard
    int samples, net = 0, to_play, written;
211 85571bc7 bellard
212 1d14ffa9 bellard
    samples = free >> SHIFT;
213 1d14ffa9 bellard
    if (!(s->active && s->enabled) || !samples) {
214 1d14ffa9 bellard
        return;
215 85571bc7 bellard
    }
216 85571bc7 bellard
217 1d14ffa9 bellard
    to_play = audio_MIN (s->left, samples);
218 1d14ffa9 bellard
    while (to_play) {
219 1d14ffa9 bellard
        written = write_audio (s, to_play);
220 1d14ffa9 bellard
221 1d14ffa9 bellard
        if (written) {
222 1d14ffa9 bellard
            s->left -= written;
223 1d14ffa9 bellard
            samples -= written;
224 1d14ffa9 bellard
            to_play -= written;
225 1d14ffa9 bellard
            s->pos = (s->pos + written) % s->samples;
226 1d14ffa9 bellard
        }
227 1d14ffa9 bellard
        else {
228 1d14ffa9 bellard
            return;
229 1d14ffa9 bellard
        }
230 1d14ffa9 bellard
    }
231 85571bc7 bellard
232 85571bc7 bellard
    samples = audio_MIN (samples, s->samples - s->pos);
233 1d14ffa9 bellard
    if (!samples) {
234 1d14ffa9 bellard
        return;
235 1d14ffa9 bellard
    }
236 85571bc7 bellard
237 1d14ffa9 bellard
#ifdef HAS_YMF262
238 85571bc7 bellard
    YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
239 85571bc7 bellard
#else
240 85571bc7 bellard
    YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
241 85571bc7 bellard
#endif
242 85571bc7 bellard
243 85571bc7 bellard
    while (samples) {
244 1d14ffa9 bellard
        written = write_audio (s, samples);
245 1d14ffa9 bellard
246 1d14ffa9 bellard
        if (written) {
247 1d14ffa9 bellard
            net += written;
248 1d14ffa9 bellard
            samples -= written;
249 1d14ffa9 bellard
            s->pos = (s->pos + written) % s->samples;
250 1d14ffa9 bellard
        }
251 1d14ffa9 bellard
        else {
252 1d14ffa9 bellard
            s->left = samples;
253 1d14ffa9 bellard
            return;
254 1d14ffa9 bellard
        }
255 85571bc7 bellard
    }
256 85571bc7 bellard
}
257 85571bc7 bellard
258 85571bc7 bellard
static void Adlib_fini (AdlibState *s)
259 85571bc7 bellard
{
260 1d14ffa9 bellard
#ifdef HAS_YMF262
261 85571bc7 bellard
    YMF262Shutdown ();
262 85571bc7 bellard
#else
263 85571bc7 bellard
    if (s->opl) {
264 85571bc7 bellard
        OPLDestroy (s->opl);
265 85571bc7 bellard
        s->opl = NULL;
266 85571bc7 bellard
    }
267 85571bc7 bellard
#endif
268 85571bc7 bellard
269 1d14ffa9 bellard
    if (s->mixbuf) {
270 7267c094 Anthony Liguori
        g_free (s->mixbuf);
271 1d14ffa9 bellard
    }
272 85571bc7 bellard
273 85571bc7 bellard
    s->active = 0;
274 85571bc7 bellard
    s->enabled = 0;
275 c0fe3827 bellard
    AUD_remove_card (&s->card);
276 85571bc7 bellard
}
277 85571bc7 bellard
278 4a0f031d Hervé Poussineau
int Adlib_init (ISABus *bus)
279 85571bc7 bellard
{
280 c0fe3827 bellard
    AdlibState *s = &glob_adlib;
281 1ea879e5 malc
    struct audsettings as;
282 c0fe3827 bellard
283 1d14ffa9 bellard
#ifdef HAS_YMF262
284 85571bc7 bellard
    if (YMF262Init (1, 14318180, conf.freq)) {
285 85571bc7 bellard
        dolog ("YMF262Init %d failed\n", conf.freq);
286 c0fe3827 bellard
        return -1;
287 85571bc7 bellard
    }
288 85571bc7 bellard
    else {
289 1d14ffa9 bellard
        YMF262SetTimerHandler (0, timer_handler, 0);
290 85571bc7 bellard
        s->enabled = 1;
291 85571bc7 bellard
    }
292 85571bc7 bellard
#else
293 85571bc7 bellard
    s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
294 85571bc7 bellard
    if (!s->opl) {
295 85571bc7 bellard
        dolog ("OPLCreate %d failed\n", conf.freq);
296 c0fe3827 bellard
        return -1;
297 85571bc7 bellard
    }
298 85571bc7 bellard
    else {
299 1d14ffa9 bellard
        OPLSetTimerHandler (s->opl, timer_handler, 0);
300 85571bc7 bellard
        s->enabled = 1;
301 85571bc7 bellard
    }
302 85571bc7 bellard
#endif
303 85571bc7 bellard
304 c0fe3827 bellard
    as.freq = conf.freq;
305 c0fe3827 bellard
    as.nchannels = SHIFT;
306 c0fe3827 bellard
    as.fmt = AUD_FMT_S16;
307 d929eba5 bellard
    as.endianness = AUDIO_HOST_ENDIANNESS;
308 c0fe3827 bellard
309 1a7dafce malc
    AUD_register_card ("adlib", &s->card);
310 c0fe3827 bellard
311 1d14ffa9 bellard
    s->voice = AUD_open_out (
312 c0fe3827 bellard
        &s->card,
313 1d14ffa9 bellard
        s->voice,
314 1d14ffa9 bellard
        "adlib",
315 1d14ffa9 bellard
        s,
316 1d14ffa9 bellard
        adlib_callback,
317 d929eba5 bellard
        &as
318 1d14ffa9 bellard
        );
319 85571bc7 bellard
    if (!s->voice) {
320 85571bc7 bellard
        Adlib_fini (s);
321 c0fe3827 bellard
        return -1;
322 85571bc7 bellard
    }
323 85571bc7 bellard
324 1d14ffa9 bellard
    s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
325 7267c094 Anthony Liguori
    s->mixbuf = g_malloc0 (s->samples << SHIFT);
326 85571bc7 bellard
327 85571bc7 bellard
    register_ioport_read (0x388, 4, 1, adlib_read, s);
328 85571bc7 bellard
    register_ioport_write (0x388, 4, 1, adlib_write, s);
329 85571bc7 bellard
330 85571bc7 bellard
    register_ioport_read (conf.port, 4, 1, adlib_read, s);
331 85571bc7 bellard
    register_ioport_write (conf.port, 4, 1, adlib_write, s);
332 85571bc7 bellard
333 85571bc7 bellard
    register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
334 85571bc7 bellard
    register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
335 c0fe3827 bellard
336 c0fe3827 bellard
    return 0;
337 85571bc7 bellard
}