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