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