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 AUDIO_CAP "adlib"
27
#include "audio/audio.h"
32
void YMF262UpdateOneQEMU(int which, INT16 *dst, int length);
41
#define small_delay() Sleep (1)
43
#define small_delay() usleep (1)
46
#define IO_READ_PROTO(name) \
47
uint32_t name (void *opaque, uint32_t nport)
48
#define IO_WRITE_PROTO(name) \
49
void name (void *opaque, uint32_t nport, uint32_t val)
54
} conf = {0x220, 44100};
64
QEMUTimer *ts, *opl_ts;
66
int left, pos, samples, bytes_per_second, old_free;
73
static AdlibState adlib;
75
static IO_WRITE_PROTO(adlib_write)
77
AdlibState *s = opaque;
81
s->ticks = qemu_get_clock (vm_clock);
83
AUD_enable (s->voice, 1);
86
status = YMF262Write (0, a, val);
88
status = OPLWrite (s->opl, a, val);
92
static IO_READ_PROTO(adlib_read)
94
AdlibState *s = opaque;
100
data = YMF262Read (0, a);
102
data = OPLRead (s->opl, a);
107
static void OPL_timer (void *opaque)
109
AdlibState *s = opaque;
111
YMF262TimerOver (s->cparam >> 1, s->cparam & 1);
113
OPLTimerOver (s->opl, s->cparam);
115
qemu_mod_timer (s->opl_ts, qemu_get_clock (vm_clock) + s->interval);
118
static void YMF262TimerHandler (int c, double interval_Sec)
120
AdlibState *s = &adlib;
121
if (interval_Sec == 0.0) {
122
qemu_del_timer (s->opl_ts);
126
s->interval = ticks_per_sec * interval_Sec;
127
qemu_mod_timer (s->opl_ts, qemu_get_clock (vm_clock) + s->interval);
131
static int write_audio (AdlibState *s, int samples)
136
int nbytes = samples << SHIFT;
137
int wbytes = AUD_write (s->voice,
138
s->mixbuf + (s->pos << (SHIFT - 1)),
140
int wsampl = wbytes >> SHIFT;
142
s->pos = (s->pos + wsampl) % s->samples;
148
dolog ("WARNING: net > ss\n");
153
static void timer (void *opaque)
155
AdlibState *s = opaque;
156
int elapsed, samples, net = 0;
159
dolog ("refcount=%d\n", s->refcount);
162
if (!(s->active && s->enabled))
168
int written = write_audio (s, s->left);
176
elapsed = AUD_calc_elapsed (s->voice);
180
/* elapsed = AUD_get_free (s->voice); */
181
samples = elapsed >> SHIFT;
185
samples = audio_MIN (samples, s->samples - s->pos);
187
dolog ("left=%d samples=%d elapsed=%d free=%d\n",
188
s->left, samples, elapsed, AUD_get_free (s->voice));
194
YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
196
YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
200
int written = write_audio (s, samples);
211
AUD_adjust (s->voice, net << SHIFT);
213
qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + ticks_per_sec / 1024);
217
static void Adlib_fini (AdlibState *s)
229
qemu_free_timer (s->opl_ts);
232
qemu_free_timer (s->ts);
234
#define maybe_free(p) if (p) qemu_free (p)
235
maybe_free (s->mixbuf);
242
void Adlib_init (void)
244
AdlibState *s = &adlib;
246
memset (s, 0, sizeof (*s));
249
if (YMF262Init (1, 14318180, conf.freq)) {
250
dolog ("YMF262Init %d failed\n", conf.freq);
254
YMF262SetTimerHandler (0, YMF262TimerHandler, 0);
258
s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
260
dolog ("OPLCreate %d failed\n", conf.freq);
264
OPLSetTimerHandler (s->opl, YMF262TimerHandler, 0);
269
s->opl_ts = qemu_new_timer (vm_clock, OPL_timer, s);
271
dolog ("Can not get timer for adlib emulation\n");
276
s->ts = qemu_new_timer (vm_clock, timer, s);
278
dolog ("Can not get timer for adlib emulation\n");
283
s->voice = AUD_open (s->voice, "adlib", conf.freq, SHIFT, AUD_FMT_S16);
289
s->bytes_per_second = conf.freq << SHIFT;
290
s->samples = AUD_get_buffer_size (s->voice) >> SHIFT;
291
s->mixbuf = qemu_mallocz (s->samples << SHIFT);
294
dolog ("not enough memory for adlib mixing buffer (%d)\n",
295
s->samples << SHIFT);
299
register_ioport_read (0x388, 4, 1, adlib_read, s);
300
register_ioport_write (0x388, 4, 1, adlib_write, s);
302
register_ioport_read (conf.port, 4, 1, adlib_read, s);
303
register_ioport_write (conf.port, 4, 1, adlib_write, s);
305
register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
306
register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
308
qemu_mod_timer (s->ts, qemu_get_clock (vm_clock) + 1);