4 * Copyright (c) 2004 Vassili Karpov (malc)
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #define dolog(...) AUD_log ("adlib", __VA_ARGS__)
28 #define ldebug(...) dolog (__VA_ARGS__)
36 void YMF262UpdateOneQEMU(int which, INT16 *dst, int length);
45 #define small_delay() Sleep (1)
47 #define small_delay() usleep (1)
50 #define IO_READ_PROTO(name) \
51 uint32_t name (void *opaque, uint32_t nport)
52 #define IO_WRITE_PROTO(name) \
53 void name (void *opaque, uint32_t nport, uint32_t val)
58 } conf = {0x220, 44100};
68 QEMUTimer *ts, *opl_ts;
70 int left, pos, samples, bytes_per_second, old_free;
77 static AdlibState adlib;
79 static IO_WRITE_PROTO(adlib_write)
81 AdlibState *s = opaque;
85 s->ticks = qemu_get_clock (vm_clock);
87 AUD_enable (s->voice, 1);
90 status = YMF262Write (0, a, val);
92 status = OPLWrite (s->opl, a, val);
96 static IO_READ_PROTO(adlib_read)
98 AdlibState *s = opaque;
104 data = YMF262Read (0, a);
106 data = OPLRead (s->opl, a);
111 static void OPL_timer (void *opaque)
113 AdlibState *s = opaque;
115 YMF262TimerOver (s->cparam >> 1, s->cparam & 1);
117 OPLTimerOver (s->opl, s->cparam);
119 qemu_mod_timer (s->opl_ts, qemu_get_clock (vm_clock) + s->interval);
122 static void YMF262TimerHandler (int c, double interval_Sec)
124 AdlibState *s = &adlib;
125 if (interval_Sec == 0.0) {
126 qemu_del_timer (s->opl_ts);
130 s->interval = ticks_per_sec * interval_Sec;
131 qemu_mod_timer (s->opl_ts, qemu_get_clock (vm_clock) + s->interval);
135 static int write_audio (AdlibState *s, int samples)
140 int nbytes = samples << SHIFT;
141 int wbytes = AUD_write (s->voice,
142 s->mixbuf + (s->pos << (SHIFT - 1)),
144 int wsampl = wbytes >> SHIFT;
146 s->pos = (s->pos + wsampl) % s->samples;
152 dolog ("WARNING: net > ss\n");
157 static void timer (void *opaque)
159 AdlibState *s = opaque;
160 int elapsed, samples, net = 0;
163 dolog ("refcount=%d\n", s->refcount);
166 if (!(s->active && s->enabled))
172 int written = write_audio (s, s->left);
180 elapsed = AUD_calc_elapsed (s->voice);
184 /* elapsed = AUD_get_free (s->voice); */
185 samples = elapsed >> SHIFT;
189 samples = audio_MIN (samples, s->samples - s->pos);
191 dolog ("left=%d samples=%d elapsed=%d free=%d\n",
192 s->left, samples, elapsed, AUD_get_free (s->voice));
198 YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
200 YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
204 int written = write_audio (s, samples);
215 AUD_adjust (s->voice, net << SHIFT);
217 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + ticks_per_sec / 1024);
221 static void Adlib_fini (AdlibState *s)
233 qemu_free_timer (s->opl_ts);
236 qemu_free_timer (s->ts);
238 #define maybe_free(p) if (p) qemu_free (p)
239 maybe_free (s->mixbuf);
246 void Adlib_init (void)
248 AdlibState *s = &adlib;
250 memset (s, 0, sizeof (*s));
253 if (YMF262Init (1, 14318180, conf.freq)) {
254 dolog ("YMF262Init %d failed\n", conf.freq);
258 YMF262SetTimerHandler (0, YMF262TimerHandler, 0);
262 s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
264 dolog ("OPLCreate %d failed\n", conf.freq);
268 OPLSetTimerHandler (s->opl, YMF262TimerHandler, 0);
273 s->opl_ts = qemu_new_timer (vm_clock, OPL_timer, s);
275 dolog ("Can not get timer for adlib emulation\n");
280 s->ts = qemu_new_timer (vm_clock, timer, s);
282 dolog ("Can not get timer for adlib emulation\n");
287 s->voice = AUD_open (s->voice, "adlib", conf.freq, SHIFT, AUD_FMT_S16);
293 s->bytes_per_second = conf.freq << SHIFT;
294 s->samples = AUD_get_buffer_size (s->voice) >> SHIFT;
295 s->mixbuf = qemu_mallocz (s->samples << SHIFT);
298 dolog ("not enough memory for adlib mixing buffer (%d)\n",
299 s->samples << SHIFT);
303 register_ioport_read (0x388, 4, 1, adlib_read, s);
304 register_ioport_write (0x388, 4, 1, adlib_write, s);
306 register_ioport_read (conf.port, 4, 1, adlib_read, s);
307 register_ioport_write (conf.port, 4, 1, adlib_write, s);
309 register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
310 register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
312 qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + 1);