]>
Commit | Line | Data |
---|---|---|
85571bc7 | 1 | /* |
1d14ffa9 FB |
2 | * QEMU Proxy for OPL2/3 emulation by MAME team |
3 | * | |
4 | * Copyright (c) 2004-2005 Vassili Karpov (malc) | |
5 | * | |
85571bc7 FB |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * THE SOFTWARE. | |
23 | */ | |
9f0683d9 | 24 | |
83c9f4ca | 25 | #include "hw/hw.h" |
0d09e41a | 26 | #include "hw/audio/audio.h" |
e140e05c | 27 | #include "audio/audio.h" |
0d09e41a | 28 | #include "hw/isa/isa.h" |
85571bc7 | 29 | |
9f0683d9 TS |
30 | //#define DEBUG |
31 | ||
1d14ffa9 FB |
32 | #define ADLIB_KILL_TIMERS 1 |
33 | ||
8c444a19 PB |
34 | #ifdef HAS_YMF262 |
35 | #define ADLIB_DESC "Yamaha YMF262 (OPL3)" | |
36 | #else | |
37 | #define ADLIB_DESC "Yamaha YM3812 (OPL2)" | |
38 | #endif | |
39 | ||
9f0683d9 | 40 | #ifdef DEBUG |
1de7afc9 | 41 | #include "qemu/timer.h" |
9f0683d9 TS |
42 | #endif |
43 | ||
fb065187 FB |
44 | #define dolog(...) AUD_log ("adlib", __VA_ARGS__) |
45 | #ifdef DEBUG | |
46 | #define ldebug(...) dolog (__VA_ARGS__) | |
47 | #else | |
48 | #define ldebug(...) | |
49 | #endif | |
85571bc7 | 50 | |
1d14ffa9 | 51 | #ifdef HAS_YMF262 |
85571bc7 | 52 | #include "ymf262.h" |
1d14ffa9 | 53 | void YMF262UpdateOneQEMU (int which, INT16 *dst, int length); |
85571bc7 FB |
54 | #define SHIFT 2 |
55 | #else | |
47b43a1f | 56 | #include "fmopl.h" |
85571bc7 FB |
57 | #define SHIFT 1 |
58 | #endif | |
59 | ||
85571bc7 FB |
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) | |
64 | ||
8c444a19 PB |
65 | #define TYPE_ADLIB "adlib" |
66 | #define ADLIB(obj) OBJECT_CHECK(AdlibState, (obj), TYPE_ADLIB) | |
85571bc7 FB |
67 | |
68 | typedef struct { | |
8c444a19 PB |
69 | ISADevice parent_obj; |
70 | ||
c0fe3827 | 71 | QEMUSoundCard card; |
8c444a19 PB |
72 | uint32_t freq; |
73 | uint32_t port; | |
1d14ffa9 | 74 | int ticking[2]; |
85571bc7 FB |
75 | int enabled; |
76 | int active; | |
85571bc7 | 77 | int bufpos; |
1d14ffa9 FB |
78 | #ifdef DEBUG |
79 | int64_t exp[2]; | |
80 | #endif | |
85571bc7 | 81 | int16_t *mixbuf; |
1d14ffa9 FB |
82 | uint64_t dexp[2]; |
83 | SWVoiceOut *voice; | |
84 | int left, pos, samples; | |
85 | QEMUAudioTimeStamp ats; | |
86 | #ifndef HAS_YMF262 | |
85571bc7 FB |
87 | FM_OPL *opl; |
88 | #endif | |
89 | } AdlibState; | |
90 | ||
8c444a19 | 91 | static AdlibState *glob_adlib; |
85571bc7 | 92 | |
1d14ffa9 FB |
93 | static void adlib_stop_opl_timer (AdlibState *s, size_t n) |
94 | { | |
95 | #ifdef HAS_YMF262 | |
96 | YMF262TimerOver (0, n); | |
97 | #else | |
98 | OPLTimerOver (s->opl, n); | |
99 | #endif | |
100 | s->ticking[n] = 0; | |
101 | } | |
102 | ||
103 | static void adlib_kill_timers (AdlibState *s) | |
104 | { | |
105 | size_t i; | |
106 | ||
107 | for (i = 0; i < 2; ++i) { | |
108 | if (s->ticking[i]) { | |
109 | uint64_t delta; | |
110 | ||
c0fe3827 | 111 | delta = AUD_get_elapsed_usec_out (s->voice, &s->ats); |
1d14ffa9 FB |
112 | ldebug ( |
113 | "delta = %f dexp = %f expired => %d\n", | |
114 | delta / 1000000.0, | |
115 | s->dexp[i] / 1000000.0, | |
116 | delta >= s->dexp[i] | |
117 | ); | |
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); | |
121 | } | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
d999f7e0 | 126 | static IO_WRITE_PROTO (adlib_write) |
85571bc7 FB |
127 | { |
128 | AdlibState *s = opaque; | |
129 | int a = nport & 3; | |
85571bc7 | 130 | |
85571bc7 | 131 | s->active = 1; |
1d14ffa9 | 132 | AUD_set_active_out (s->voice, 1); |
85571bc7 | 133 | |
1d14ffa9 FB |
134 | adlib_kill_timers (s); |
135 | ||
136 | #ifdef HAS_YMF262 | |
e8beeae4 | 137 | YMF262Write (0, a, val); |
85571bc7 | 138 | #else |
e8beeae4 | 139 | OPLWrite (s->opl, a, val); |
85571bc7 FB |
140 | #endif |
141 | } | |
142 | ||
d999f7e0 | 143 | static IO_READ_PROTO (adlib_read) |
85571bc7 FB |
144 | { |
145 | AdlibState *s = opaque; | |
146 | uint8_t data; | |
147 | int a = nport & 3; | |
148 | ||
1d14ffa9 FB |
149 | adlib_kill_timers (s); |
150 | ||
151 | #ifdef HAS_YMF262 | |
85571bc7 FB |
152 | data = YMF262Read (0, a); |
153 | #else | |
154 | data = OPLRead (s->opl, a); | |
155 | #endif | |
156 | return data; | |
157 | } | |
158 | ||
1d14ffa9 | 159 | static void timer_handler (int c, double interval_Sec) |
85571bc7 | 160 | { |
8c444a19 | 161 | AdlibState *s = glob_adlib; |
1d14ffa9 FB |
162 | unsigned n = c & 1; |
163 | #ifdef DEBUG | |
164 | double interval; | |
c0fe3827 | 165 | int64_t exp; |
85571bc7 | 166 | #endif |
85571bc7 | 167 | |
85571bc7 | 168 | if (interval_Sec == 0.0) { |
1d14ffa9 | 169 | s->ticking[n] = 0; |
85571bc7 FB |
170 | return; |
171 | } | |
1d14ffa9 FB |
172 | |
173 | s->ticking[n] = 1; | |
174 | #ifdef DEBUG | |
cf4dc461 | 175 | interval = get_ticks_per_sec () * interval_Sec; |
74475455 | 176 | exp = qemu_get_clock_ns (vm_clock) + interval; |
1d14ffa9 FB |
177 | s->exp[n] = exp; |
178 | #endif | |
179 | ||
180 | s->dexp[n] = interval_Sec * 1000000.0; | |
181 | AUD_init_time_stamp_out (s->voice, &s->ats); | |
85571bc7 FB |
182 | } |
183 | ||
184 | static int write_audio (AdlibState *s, int samples) | |
185 | { | |
186 | int net = 0; | |
1d14ffa9 FB |
187 | int pos = s->pos; |
188 | ||
85571bc7 | 189 | while (samples) { |
1d14ffa9 FB |
190 | int nbytes, wbytes, wsampl; |
191 | ||
192 | nbytes = samples << SHIFT; | |
193 | wbytes = AUD_write ( | |
194 | s->voice, | |
195 | s->mixbuf + (pos << (SHIFT - 1)), | |
196 | nbytes | |
197 | ); | |
198 | ||
199 | if (wbytes) { | |
200 | wsampl = wbytes >> SHIFT; | |
201 | ||
202 | samples -= wsampl; | |
203 | pos = (pos + wsampl) % s->samples; | |
204 | ||
205 | net += wsampl; | |
206 | } | |
207 | else { | |
85571bc7 | 208 | break; |
1d14ffa9 | 209 | } |
85571bc7 | 210 | } |
1d14ffa9 | 211 | |
85571bc7 FB |
212 | return net; |
213 | } | |
214 | ||
1d14ffa9 | 215 | static void adlib_callback (void *opaque, int free) |
85571bc7 FB |
216 | { |
217 | AdlibState *s = opaque; | |
1d14ffa9 | 218 | int samples, net = 0, to_play, written; |
85571bc7 | 219 | |
1d14ffa9 FB |
220 | samples = free >> SHIFT; |
221 | if (!(s->active && s->enabled) || !samples) { | |
222 | return; | |
85571bc7 | 223 | } |
85571bc7 | 224 | |
1d14ffa9 FB |
225 | to_play = audio_MIN (s->left, samples); |
226 | while (to_play) { | |
227 | written = write_audio (s, to_play); | |
228 | ||
229 | if (written) { | |
230 | s->left -= written; | |
231 | samples -= written; | |
232 | to_play -= written; | |
233 | s->pos = (s->pos + written) % s->samples; | |
234 | } | |
235 | else { | |
236 | return; | |
237 | } | |
238 | } | |
85571bc7 FB |
239 | |
240 | samples = audio_MIN (samples, s->samples - s->pos); | |
1d14ffa9 FB |
241 | if (!samples) { |
242 | return; | |
243 | } | |
85571bc7 | 244 | |
1d14ffa9 | 245 | #ifdef HAS_YMF262 |
85571bc7 FB |
246 | YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples); |
247 | #else | |
248 | YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples); | |
249 | #endif | |
250 | ||
251 | while (samples) { | |
1d14ffa9 FB |
252 | written = write_audio (s, samples); |
253 | ||
254 | if (written) { | |
255 | net += written; | |
256 | samples -= written; | |
257 | s->pos = (s->pos + written) % s->samples; | |
258 | } | |
259 | else { | |
260 | s->left = samples; | |
261 | return; | |
262 | } | |
85571bc7 | 263 | } |
85571bc7 FB |
264 | } |
265 | ||
266 | static void Adlib_fini (AdlibState *s) | |
267 | { | |
1d14ffa9 | 268 | #ifdef HAS_YMF262 |
85571bc7 FB |
269 | YMF262Shutdown (); |
270 | #else | |
271 | if (s->opl) { | |
272 | OPLDestroy (s->opl); | |
273 | s->opl = NULL; | |
274 | } | |
275 | #endif | |
276 | ||
1d14ffa9 | 277 | if (s->mixbuf) { |
7267c094 | 278 | g_free (s->mixbuf); |
1d14ffa9 | 279 | } |
85571bc7 FB |
280 | |
281 | s->active = 0; | |
282 | s->enabled = 0; | |
c0fe3827 | 283 | AUD_remove_card (&s->card); |
85571bc7 FB |
284 | } |
285 | ||
db895a1e | 286 | static void adlib_realizefn (DeviceState *dev, Error **errp) |
85571bc7 | 287 | { |
8c444a19 | 288 | AdlibState *s = ADLIB(dev); |
1ea879e5 | 289 | struct audsettings as; |
c0fe3827 | 290 | |
8c444a19 | 291 | if (glob_adlib) { |
db895a1e AF |
292 | error_setg (errp, "Cannot create more than 1 adlib device"); |
293 | return; | |
8c444a19 PB |
294 | } |
295 | glob_adlib = s; | |
296 | ||
1d14ffa9 | 297 | #ifdef HAS_YMF262 |
8c444a19 | 298 | if (YMF262Init (1, 14318180, s->freq)) { |
db895a1e AF |
299 | error_setg (errp, "YMF262Init %d failed", s->freq); |
300 | return; | |
85571bc7 FB |
301 | } |
302 | else { | |
1d14ffa9 | 303 | YMF262SetTimerHandler (0, timer_handler, 0); |
85571bc7 FB |
304 | s->enabled = 1; |
305 | } | |
306 | #else | |
8c444a19 | 307 | s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, s->freq); |
85571bc7 | 308 | if (!s->opl) { |
db895a1e AF |
309 | error_setg (errp, "OPLCreate %d failed", s->freq); |
310 | return; | |
85571bc7 FB |
311 | } |
312 | else { | |
1d14ffa9 | 313 | OPLSetTimerHandler (s->opl, timer_handler, 0); |
85571bc7 FB |
314 | s->enabled = 1; |
315 | } | |
316 | #endif | |
317 | ||
8c444a19 | 318 | as.freq = s->freq; |
c0fe3827 FB |
319 | as.nchannels = SHIFT; |
320 | as.fmt = AUD_FMT_S16; | |
d929eba5 | 321 | as.endianness = AUDIO_HOST_ENDIANNESS; |
c0fe3827 | 322 | |
1a7dafce | 323 | AUD_register_card ("adlib", &s->card); |
c0fe3827 | 324 | |
1d14ffa9 | 325 | s->voice = AUD_open_out ( |
c0fe3827 | 326 | &s->card, |
1d14ffa9 FB |
327 | s->voice, |
328 | "adlib", | |
329 | s, | |
330 | adlib_callback, | |
d929eba5 | 331 | &as |
1d14ffa9 | 332 | ); |
85571bc7 FB |
333 | if (!s->voice) { |
334 | Adlib_fini (s); | |
db895a1e AF |
335 | error_setg (errp, "Initializing audio voice failed"); |
336 | return; | |
85571bc7 FB |
337 | } |
338 | ||
1d14ffa9 | 339 | s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT; |
7267c094 | 340 | s->mixbuf = g_malloc0 (s->samples << SHIFT); |
85571bc7 | 341 | |
85571bc7 FB |
342 | register_ioport_read (0x388, 4, 1, adlib_read, s); |
343 | register_ioport_write (0x388, 4, 1, adlib_write, s); | |
344 | ||
8c444a19 PB |
345 | register_ioport_read (s->port, 4, 1, adlib_read, s); |
346 | register_ioport_write (s->port, 4, 1, adlib_write, s); | |
85571bc7 | 347 | |
8c444a19 PB |
348 | register_ioport_read (s->port + 8, 2, 1, adlib_read, s); |
349 | register_ioport_write (s->port + 8, 2, 1, adlib_write, s); | |
85571bc7 | 350 | } |
8c444a19 PB |
351 | |
352 | static Property adlib_properties[] = { | |
353 | DEFINE_PROP_HEX32 ("iobase", AdlibState, port, 0x220), | |
354 | DEFINE_PROP_UINT32 ("freq", AdlibState, freq, 44100), | |
355 | DEFINE_PROP_END_OF_LIST (), | |
356 | }; | |
357 | ||
358 | static void adlib_class_initfn (ObjectClass *klass, void *data) | |
359 | { | |
360 | DeviceClass *dc = DEVICE_CLASS (klass); | |
db895a1e AF |
361 | |
362 | dc->realize = adlib_realizefn; | |
8c444a19 PB |
363 | dc->desc = ADLIB_DESC; |
364 | dc->props = adlib_properties; | |
365 | } | |
366 | ||
367 | static const TypeInfo adlib_info = { | |
368 | .name = TYPE_ADLIB, | |
369 | .parent = TYPE_ISA_DEVICE, | |
370 | .instance_size = sizeof (AdlibState), | |
371 | .class_init = adlib_class_initfn, | |
372 | }; | |
373 | ||
36cd6f6f | 374 | static int Adlib_init (ISABus *bus) |
8c444a19 PB |
375 | { |
376 | isa_create_simple (bus, TYPE_ADLIB); | |
377 | return 0; | |
378 | } | |
379 | ||
380 | static void adlib_register_types (void) | |
381 | { | |
382 | type_register_static (&adlib_info); | |
36cd6f6f | 383 | isa_register_soundhw("adlib", ADLIB_DESC, Adlib_init); |
8c444a19 PB |
384 | } |
385 | ||
386 | type_init (adlib_register_types) |