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
27 #include "audio/audio.h"
32 #define ADLIB_KILL_TIMERS 1
35 #include "qemu-timer.h"
38 #define dolog(...) AUD_log ("adlib", __VA_ARGS__)
40 #define ldebug(...) dolog (__VA_ARGS__)
47 void YMF262UpdateOneQEMU (int which
, INT16
*dst
, int length
);
54 #define IO_READ_PROTO(name) \
55 uint32_t name (void *opaque, uint32_t nport)
56 #define IO_WRITE_PROTO(name) \
57 void name (void *opaque, uint32_t nport, uint32_t val)
62 } conf
= {0x220, 44100};
76 int left
, pos
, samples
;
77 QEMUAudioTimeStamp ats
;
83 static AdlibState glob_adlib
;
85 static void adlib_stop_opl_timer (AdlibState
*s
, size_t n
)
88 YMF262TimerOver (0, n
);
90 OPLTimerOver (s
->opl
, n
);
95 static void adlib_kill_timers (AdlibState
*s
)
99 for (i
= 0; i
< 2; ++i
) {
103 delta
= AUD_get_elapsed_usec_out (s
->voice
, &s
->ats
);
105 "delta = %f dexp = %f expired => %d\n",
107 s
->dexp
[i
] / 1000000.0,
110 if (ADLIB_KILL_TIMERS
|| delta
>= s
->dexp
[i
]) {
111 adlib_stop_opl_timer (s
, i
);
112 AUD_init_time_stamp_out (s
->voice
, &s
->ats
);
118 static IO_WRITE_PROTO (adlib_write
)
120 AdlibState
*s
= opaque
;
125 AUD_set_active_out (s
->voice
, 1);
127 adlib_kill_timers (s
);
130 status
= YMF262Write (0, a
, val
);
132 status
= OPLWrite (s
->opl
, a
, val
);
136 static IO_READ_PROTO (adlib_read
)
138 AdlibState
*s
= opaque
;
142 adlib_kill_timers (s
);
145 data
= YMF262Read (0, a
);
147 data
= OPLRead (s
->opl
, a
);
152 static void timer_handler (int c
, double interval_Sec
)
154 AdlibState
*s
= &glob_adlib
;
161 if (interval_Sec
== 0.0) {
168 interval
= get_ticks_per_sec() * interval_Sec
;
169 exp
= qemu_get_clock (vm_clock
) + interval
;
173 s
->dexp
[n
] = interval_Sec
* 1000000.0;
174 AUD_init_time_stamp_out (s
->voice
, &s
->ats
);
177 static int write_audio (AdlibState
*s
, int samples
)
183 int nbytes
, wbytes
, wsampl
;
185 nbytes
= samples
<< SHIFT
;
188 s
->mixbuf
+ (pos
<< (SHIFT
- 1)),
193 wsampl
= wbytes
>> SHIFT
;
196 pos
= (pos
+ wsampl
) % s
->samples
;
208 static void adlib_callback (void *opaque
, int free
)
210 AdlibState
*s
= opaque
;
211 int samples
, net
= 0, to_play
, written
;
213 samples
= free
>> SHIFT
;
214 if (!(s
->active
&& s
->enabled
) || !samples
) {
218 to_play
= audio_MIN (s
->left
, samples
);
220 written
= write_audio (s
, to_play
);
226 s
->pos
= (s
->pos
+ written
) % s
->samples
;
233 samples
= audio_MIN (samples
, s
->samples
- s
->pos
);
239 YMF262UpdateOneQEMU (0, s
->mixbuf
+ s
->pos
* 2, samples
);
241 YM3812UpdateOne (s
->opl
, s
->mixbuf
+ s
->pos
, samples
);
245 written
= write_audio (s
, samples
);
250 s
->pos
= (s
->pos
+ written
) % s
->samples
;
259 static void Adlib_fini (AdlibState
*s
)
271 qemu_free (s
->mixbuf
);
276 AUD_remove_card (&s
->card
);
279 int Adlib_init (qemu_irq
*pic
)
281 AdlibState
*s
= &glob_adlib
;
282 struct audsettings as
;
285 if (YMF262Init (1, 14318180, conf
.freq
)) {
286 dolog ("YMF262Init %d failed\n", conf
.freq
);
290 YMF262SetTimerHandler (0, timer_handler
, 0);
294 s
->opl
= OPLCreate (OPL_TYPE_YM3812
, 3579545, conf
.freq
);
296 dolog ("OPLCreate %d failed\n", conf
.freq
);
300 OPLSetTimerHandler (s
->opl
, timer_handler
, 0);
306 as
.nchannels
= SHIFT
;
307 as
.fmt
= AUD_FMT_S16
;
308 as
.endianness
= AUDIO_HOST_ENDIANNESS
;
310 AUD_register_card ("adlib", &s
->card
);
312 s
->voice
= AUD_open_out (
325 s
->samples
= AUD_get_buffer_size_out (s
->voice
) >> SHIFT
;
326 s
->mixbuf
= qemu_mallocz (s
->samples
<< SHIFT
);
328 register_ioport_read (0x388, 4, 1, adlib_read
, s
);
329 register_ioport_write (0x388, 4, 1, adlib_write
, s
);
331 register_ioport_read (conf
.port
, 4, 1, adlib_read
, s
);
332 register_ioport_write (conf
.port
, 4, 1, adlib_write
, s
);
334 register_ioport_read (conf
.port
+ 8, 2, 1, adlib_read
, s
);
335 register_ioport_write (conf
.port
+ 8, 2, 1, adlib_write
, s
);