2 * QEMU Proxy for OPL2/3 emulation by MAME team
4 * Copyright (c) 2004-2005 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 #include "hw/audio/audio.h"
27 #include "audio/audio.h"
28 #include "hw/isa/isa.h"
32 #define ADLIB_KILL_TIMERS 1
35 #define ADLIB_DESC "Yamaha YMF262 (OPL3)"
37 #define ADLIB_DESC "Yamaha YM3812 (OPL2)"
41 #include "qemu/timer.h"
44 #define dolog(...) AUD_log ("adlib", __VA_ARGS__)
46 #define ldebug(...) dolog (__VA_ARGS__)
53 void YMF262UpdateOneQEMU (int which
, INT16
*dst
, int length
);
60 #define IO_READ_PROTO(name) \
61 uint32_t name (void *opaque, uint32_t nport)
62 #define IO_WRITE_PROTO(name) \
63 void name (void *opaque, uint32_t nport, uint32_t val)
65 #define TYPE_ADLIB "adlib"
66 #define ADLIB(obj) OBJECT_CHECK(AdlibState, (obj), TYPE_ADLIB)
84 int left
, pos
, samples
;
85 QEMUAudioTimeStamp ats
;
91 static AdlibState
*glob_adlib
;
93 static void adlib_stop_opl_timer (AdlibState
*s
, size_t n
)
96 YMF262TimerOver (0, n
);
98 OPLTimerOver (s
->opl
, n
);
103 static void adlib_kill_timers (AdlibState
*s
)
107 for (i
= 0; i
< 2; ++i
) {
111 delta
= AUD_get_elapsed_usec_out (s
->voice
, &s
->ats
);
113 "delta = %f dexp = %f expired => %d\n",
115 s
->dexp
[i
] / 1000000.0,
118 if (ADLIB_KILL_TIMERS
|| delta
>= s
->dexp
[i
]) {
119 adlib_stop_opl_timer (s
, i
);
120 AUD_init_time_stamp_out (s
->voice
, &s
->ats
);
126 static IO_WRITE_PROTO (adlib_write
)
128 AdlibState
*s
= opaque
;
132 AUD_set_active_out (s
->voice
, 1);
134 adlib_kill_timers (s
);
137 YMF262Write (0, a
, val
);
139 OPLWrite (s
->opl
, a
, val
);
143 static IO_READ_PROTO (adlib_read
)
145 AdlibState
*s
= opaque
;
149 adlib_kill_timers (s
);
152 data
= YMF262Read (0, a
);
154 data
= OPLRead (s
->opl
, a
);
159 static void timer_handler (int c
, double interval_Sec
)
161 AdlibState
*s
= glob_adlib
;
168 if (interval_Sec
== 0.0) {
175 interval
= get_ticks_per_sec () * interval_Sec
;
176 exp
= qemu_get_clock_ns (vm_clock
) + interval
;
180 s
->dexp
[n
] = interval_Sec
* 1000000.0;
181 AUD_init_time_stamp_out (s
->voice
, &s
->ats
);
184 static int write_audio (AdlibState
*s
, int samples
)
190 int nbytes
, wbytes
, wsampl
;
192 nbytes
= samples
<< SHIFT
;
195 s
->mixbuf
+ (pos
<< (SHIFT
- 1)),
200 wsampl
= wbytes
>> SHIFT
;
203 pos
= (pos
+ wsampl
) % s
->samples
;
215 static void adlib_callback (void *opaque
, int free
)
217 AdlibState
*s
= opaque
;
218 int samples
, net
= 0, to_play
, written
;
220 samples
= free
>> SHIFT
;
221 if (!(s
->active
&& s
->enabled
) || !samples
) {
225 to_play
= audio_MIN (s
->left
, samples
);
227 written
= write_audio (s
, to_play
);
233 s
->pos
= (s
->pos
+ written
) % s
->samples
;
240 samples
= audio_MIN (samples
, s
->samples
- s
->pos
);
246 YMF262UpdateOneQEMU (0, s
->mixbuf
+ s
->pos
* 2, samples
);
248 YM3812UpdateOne (s
->opl
, s
->mixbuf
+ s
->pos
, samples
);
252 written
= write_audio (s
, samples
);
257 s
->pos
= (s
->pos
+ written
) % s
->samples
;
266 static void Adlib_fini (AdlibState
*s
)
283 AUD_remove_card (&s
->card
);
286 static int Adlib_initfn (ISADevice
*dev
)
288 AdlibState
*s
= ADLIB(dev
);
289 struct audsettings as
;
292 dolog ("Cannot create more than 1 adlib device\n");
298 if (YMF262Init (1, 14318180, s
->freq
)) {
299 dolog ("YMF262Init %d failed\n", s
->freq
);
303 YMF262SetTimerHandler (0, timer_handler
, 0);
307 s
->opl
= OPLCreate (OPL_TYPE_YM3812
, 3579545, s
->freq
);
309 dolog ("OPLCreate %d failed\n", s
->freq
);
313 OPLSetTimerHandler (s
->opl
, timer_handler
, 0);
319 as
.nchannels
= SHIFT
;
320 as
.fmt
= AUD_FMT_S16
;
321 as
.endianness
= AUDIO_HOST_ENDIANNESS
;
323 AUD_register_card ("adlib", &s
->card
);
325 s
->voice
= AUD_open_out (
338 s
->samples
= AUD_get_buffer_size_out (s
->voice
) >> SHIFT
;
339 s
->mixbuf
= g_malloc0 (s
->samples
<< SHIFT
);
341 register_ioport_read (0x388, 4, 1, adlib_read
, s
);
342 register_ioport_write (0x388, 4, 1, adlib_write
, s
);
344 register_ioport_read (s
->port
, 4, 1, adlib_read
, s
);
345 register_ioport_write (s
->port
, 4, 1, adlib_write
, s
);
347 register_ioport_read (s
->port
+ 8, 2, 1, adlib_read
, s
);
348 register_ioport_write (s
->port
+ 8, 2, 1, adlib_write
, s
);
353 static Property adlib_properties
[] = {
354 DEFINE_PROP_HEX32 ("iobase", AdlibState
, port
, 0x220),
355 DEFINE_PROP_UINT32 ("freq", AdlibState
, freq
, 44100),
356 DEFINE_PROP_END_OF_LIST (),
359 static void adlib_class_initfn (ObjectClass
*klass
, void *data
)
361 DeviceClass
*dc
= DEVICE_CLASS (klass
);
362 ISADeviceClass
*ic
= ISA_DEVICE_CLASS (klass
);
363 ic
->init
= Adlib_initfn
;
364 dc
->desc
= ADLIB_DESC
;
365 dc
->props
= adlib_properties
;
368 static const TypeInfo adlib_info
= {
370 .parent
= TYPE_ISA_DEVICE
,
371 .instance_size
= sizeof (AdlibState
),
372 .class_init
= adlib_class_initfn
,
375 static int Adlib_init (ISABus
*bus
)
377 isa_create_simple (bus
, TYPE_ADLIB
);
381 static void adlib_register_types (void)
383 type_register_static (&adlib_info
);
384 isa_register_soundhw("adlib", ADLIB_DESC
, Adlib_init
);
387 type_init (adlib_register_types
)