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 TYPE_ADLIB "adlib"
61 #define ADLIB(obj) OBJECT_CHECK(AdlibState, (obj), TYPE_ADLIB)
79 int left
, pos
, samples
;
80 QEMUAudioTimeStamp ats
;
87 static AdlibState
*glob_adlib
;
89 static void adlib_stop_opl_timer (AdlibState
*s
, size_t n
)
92 YMF262TimerOver (0, n
);
94 OPLTimerOver (s
->opl
, n
);
99 static void adlib_kill_timers (AdlibState
*s
)
103 for (i
= 0; i
< 2; ++i
) {
107 delta
= AUD_get_elapsed_usec_out (s
->voice
, &s
->ats
);
109 "delta = %f dexp = %f expired => %d\n",
111 s
->dexp
[i
] / 1000000.0,
114 if (ADLIB_KILL_TIMERS
|| delta
>= s
->dexp
[i
]) {
115 adlib_stop_opl_timer (s
, i
);
116 AUD_init_time_stamp_out (s
->voice
, &s
->ats
);
122 static void adlib_write(void *opaque
, uint32_t nport
, uint32_t val
)
124 AdlibState
*s
= opaque
;
128 AUD_set_active_out (s
->voice
, 1);
130 adlib_kill_timers (s
);
133 YMF262Write (0, a
, val
);
135 OPLWrite (s
->opl
, a
, val
);
139 static uint32_t adlib_read(void *opaque
, uint32_t nport
)
141 AdlibState
*s
= opaque
;
145 adlib_kill_timers (s
);
148 data
= YMF262Read (0, a
);
150 data
= OPLRead (s
->opl
, a
);
155 static void timer_handler (int c
, double interval_Sec
)
157 AdlibState
*s
= glob_adlib
;
164 if (interval_Sec
== 0.0) {
171 interval
= get_ticks_per_sec () * interval_Sec
;
172 exp
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + interval
;
176 s
->dexp
[n
] = interval_Sec
* 1000000.0;
177 AUD_init_time_stamp_out (s
->voice
, &s
->ats
);
180 static int write_audio (AdlibState
*s
, int samples
)
186 int nbytes
, wbytes
, wsampl
;
188 nbytes
= samples
<< SHIFT
;
191 s
->mixbuf
+ (pos
<< (SHIFT
- 1)),
196 wsampl
= wbytes
>> SHIFT
;
199 pos
= (pos
+ wsampl
) % s
->samples
;
211 static void adlib_callback (void *opaque
, int free
)
213 AdlibState
*s
= opaque
;
214 int samples
, net
= 0, to_play
, written
;
216 samples
= free
>> SHIFT
;
217 if (!(s
->active
&& s
->enabled
) || !samples
) {
221 to_play
= audio_MIN (s
->left
, samples
);
223 written
= write_audio (s
, to_play
);
229 s
->pos
= (s
->pos
+ written
) % s
->samples
;
236 samples
= audio_MIN (samples
, s
->samples
- s
->pos
);
242 YMF262UpdateOneQEMU (0, s
->mixbuf
+ s
->pos
* 2, samples
);
244 YM3812UpdateOne (s
->opl
, s
->mixbuf
+ s
->pos
, samples
);
248 written
= write_audio (s
, samples
);
253 s
->pos
= (s
->pos
+ written
) % s
->samples
;
262 static void Adlib_fini (AdlibState
*s
)
277 AUD_remove_card (&s
->card
);
280 static MemoryRegionPortio adlib_portio_list
[] = {
281 { 0, 4, 1, .read
= adlib_read
, .write
= adlib_write
, },
282 { 0, 2, 1, .read
= adlib_read
, .write
= adlib_write
, },
283 { 0x388, 4, 1, .read
= adlib_read
, .write
= adlib_write
, },
284 PORTIO_END_OF_LIST(),
287 static void adlib_realizefn (DeviceState
*dev
, Error
**errp
)
289 AdlibState
*s
= ADLIB(dev
);
290 struct audsettings as
;
293 error_setg (errp
, "Cannot create more than 1 adlib device");
299 if (YMF262Init (1, 14318180, s
->freq
)) {
300 error_setg (errp
, "YMF262Init %d failed", s
->freq
);
304 YMF262SetTimerHandler (0, timer_handler
, 0);
308 s
->opl
= OPLCreate (OPL_TYPE_YM3812
, 3579545, s
->freq
);
310 error_setg (errp
, "OPLCreate %d failed", s
->freq
);
314 OPLSetTimerHandler (s
->opl
, timer_handler
, 0);
320 as
.nchannels
= SHIFT
;
321 as
.fmt
= AUD_FMT_S16
;
322 as
.endianness
= AUDIO_HOST_ENDIANNESS
;
324 AUD_register_card ("adlib", &s
->card
);
326 s
->voice
= AUD_open_out (
336 error_setg (errp
, "Initializing audio voice failed");
340 s
->samples
= AUD_get_buffer_size_out (s
->voice
) >> SHIFT
;
341 s
->mixbuf
= g_malloc0 (s
->samples
<< SHIFT
);
343 adlib_portio_list
[0].offset
= s
->port
;
344 adlib_portio_list
[1].offset
= s
->port
+ 8;
345 portio_list_init (&s
->port_list
, OBJECT(s
), adlib_portio_list
, s
, "adlib");
346 portio_list_add (&s
->port_list
, isa_address_space_io(&s
->parent_obj
), 0);
349 static Property adlib_properties
[] = {
350 DEFINE_PROP_UINT32 ("iobase", AdlibState
, port
, 0x220),
351 DEFINE_PROP_UINT32 ("freq", AdlibState
, freq
, 44100),
352 DEFINE_PROP_END_OF_LIST (),
355 static void adlib_class_initfn (ObjectClass
*klass
, void *data
)
357 DeviceClass
*dc
= DEVICE_CLASS (klass
);
359 dc
->realize
= adlib_realizefn
;
360 set_bit(DEVICE_CATEGORY_SOUND
, dc
->categories
);
361 dc
->desc
= ADLIB_DESC
;
362 dc
->props
= adlib_properties
;
365 static const TypeInfo adlib_info
= {
367 .parent
= TYPE_ISA_DEVICE
,
368 .instance_size
= sizeof (AdlibState
),
369 .class_init
= adlib_class_initfn
,
372 static int Adlib_init (ISABus
*bus
)
374 isa_create_simple (bus
, TYPE_ADLIB
);
378 static void adlib_register_types (void)
380 type_register_static (&adlib_info
);
381 isa_register_soundhw("adlib", ADLIB_DESC
, Adlib_init
);
384 type_init (adlib_register_types
)