]>
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" |
da34e65c | 26 | #include "qapi/error.h" |
0b8fa32f | 27 | #include "qemu/module.h" |
8a824e4d | 28 | #include "hw/audio/soundhw.h" |
e140e05c | 29 | #include "audio/audio.h" |
0d09e41a | 30 | #include "hw/isa/isa.h" |
a27bd6c7 | 31 | #include "hw/qdev-properties.h" |
db1015e9 | 32 | #include "qom/object.h" |
85571bc7 | 33 | |
9f0683d9 TS |
34 | //#define DEBUG |
35 | ||
1d14ffa9 FB |
36 | #define ADLIB_KILL_TIMERS 1 |
37 | ||
8c444a19 | 38 | #define ADLIB_DESC "Yamaha YM3812 (OPL2)" |
8c444a19 | 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 | |
47b43a1f | 51 | #include "fmopl.h" |
85571bc7 | 52 | #define SHIFT 1 |
85571bc7 | 53 | |
8c444a19 | 54 | #define TYPE_ADLIB "adlib" |
8063396b | 55 | OBJECT_DECLARE_SIMPLE_TYPE(AdlibState, ADLIB) |
85571bc7 | 56 | |
db1015e9 | 57 | struct AdlibState { |
8c444a19 PB |
58 | ISADevice parent_obj; |
59 | ||
c0fe3827 | 60 | QEMUSoundCard card; |
8c444a19 PB |
61 | uint32_t freq; |
62 | uint32_t port; | |
1d14ffa9 | 63 | int ticking[2]; |
85571bc7 FB |
64 | int enabled; |
65 | int active; | |
85571bc7 | 66 | int bufpos; |
1d14ffa9 FB |
67 | #ifdef DEBUG |
68 | int64_t exp[2]; | |
69 | #endif | |
85571bc7 | 70 | int16_t *mixbuf; |
1d14ffa9 FB |
71 | uint64_t dexp[2]; |
72 | SWVoiceOut *voice; | |
73 | int left, pos, samples; | |
74 | QEMUAudioTimeStamp ats; | |
85571bc7 | 75 | FM_OPL *opl; |
848696bf | 76 | PortioList port_list; |
db1015e9 | 77 | }; |
85571bc7 | 78 | |
1d14ffa9 FB |
79 | static void adlib_stop_opl_timer (AdlibState *s, size_t n) |
80 | { | |
1d14ffa9 | 81 | OPLTimerOver (s->opl, n); |
1d14ffa9 FB |
82 | s->ticking[n] = 0; |
83 | } | |
84 | ||
85 | static void adlib_kill_timers (AdlibState *s) | |
86 | { | |
87 | size_t i; | |
88 | ||
89 | for (i = 0; i < 2; ++i) { | |
90 | if (s->ticking[i]) { | |
91 | uint64_t delta; | |
92 | ||
c0fe3827 | 93 | delta = AUD_get_elapsed_usec_out (s->voice, &s->ats); |
1d14ffa9 FB |
94 | ldebug ( |
95 | "delta = %f dexp = %f expired => %d\n", | |
96 | delta / 1000000.0, | |
97 | s->dexp[i] / 1000000.0, | |
98 | delta >= s->dexp[i] | |
99 | ); | |
100 | if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) { | |
101 | adlib_stop_opl_timer (s, i); | |
102 | AUD_init_time_stamp_out (s->voice, &s->ats); | |
103 | } | |
104 | } | |
105 | } | |
106 | } | |
107 | ||
8307c294 | 108 | static void adlib_write(void *opaque, uint32_t nport, uint32_t val) |
85571bc7 FB |
109 | { |
110 | AdlibState *s = opaque; | |
111 | int a = nport & 3; | |
85571bc7 | 112 | |
85571bc7 | 113 | s->active = 1; |
1d14ffa9 | 114 | AUD_set_active_out (s->voice, 1); |
85571bc7 | 115 | |
1d14ffa9 FB |
116 | adlib_kill_timers (s); |
117 | ||
e8beeae4 | 118 | OPLWrite (s->opl, a, val); |
85571bc7 FB |
119 | } |
120 | ||
8307c294 | 121 | static uint32_t adlib_read(void *opaque, uint32_t nport) |
85571bc7 FB |
122 | { |
123 | AdlibState *s = opaque; | |
85571bc7 FB |
124 | int a = nport & 3; |
125 | ||
1d14ffa9 | 126 | adlib_kill_timers (s); |
b3ac2b94 | 127 | return OPLRead (s->opl, a); |
85571bc7 FB |
128 | } |
129 | ||
c57fbf50 | 130 | static void timer_handler (void *opaque, int c, double interval_Sec) |
85571bc7 | 131 | { |
639b49ef | 132 | AdlibState *s = opaque; |
1d14ffa9 FB |
133 | unsigned n = c & 1; |
134 | #ifdef DEBUG | |
135 | double interval; | |
c0fe3827 | 136 | int64_t exp; |
85571bc7 | 137 | #endif |
85571bc7 | 138 | |
85571bc7 | 139 | if (interval_Sec == 0.0) { |
1d14ffa9 | 140 | s->ticking[n] = 0; |
85571bc7 FB |
141 | return; |
142 | } | |
1d14ffa9 FB |
143 | |
144 | s->ticking[n] = 1; | |
145 | #ifdef DEBUG | |
73bcb24d | 146 | interval = NANOSECONDS_PER_SECOND * interval_Sec; |
bc72ad67 | 147 | exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval; |
1d14ffa9 FB |
148 | s->exp[n] = exp; |
149 | #endif | |
150 | ||
151 | s->dexp[n] = interval_Sec * 1000000.0; | |
152 | AUD_init_time_stamp_out (s->voice, &s->ats); | |
85571bc7 FB |
153 | } |
154 | ||
155 | static int write_audio (AdlibState *s, int samples) | |
156 | { | |
157 | int net = 0; | |
1d14ffa9 FB |
158 | int pos = s->pos; |
159 | ||
85571bc7 | 160 | while (samples) { |
1d14ffa9 FB |
161 | int nbytes, wbytes, wsampl; |
162 | ||
163 | nbytes = samples << SHIFT; | |
164 | wbytes = AUD_write ( | |
165 | s->voice, | |
166 | s->mixbuf + (pos << (SHIFT - 1)), | |
167 | nbytes | |
168 | ); | |
169 | ||
170 | if (wbytes) { | |
171 | wsampl = wbytes >> SHIFT; | |
172 | ||
173 | samples -= wsampl; | |
174 | pos = (pos + wsampl) % s->samples; | |
175 | ||
176 | net += wsampl; | |
177 | } | |
178 | else { | |
85571bc7 | 179 | break; |
1d14ffa9 | 180 | } |
85571bc7 | 181 | } |
1d14ffa9 | 182 | |
85571bc7 FB |
183 | return net; |
184 | } | |
185 | ||
1d14ffa9 | 186 | static void adlib_callback (void *opaque, int free) |
85571bc7 FB |
187 | { |
188 | AdlibState *s = opaque; | |
79fe9e43 | 189 | int samples, to_play, written; |
85571bc7 | 190 | |
1d14ffa9 FB |
191 | samples = free >> SHIFT; |
192 | if (!(s->active && s->enabled) || !samples) { | |
193 | return; | |
85571bc7 | 194 | } |
85571bc7 | 195 | |
58935915 | 196 | to_play = MIN (s->left, samples); |
1d14ffa9 FB |
197 | while (to_play) { |
198 | written = write_audio (s, to_play); | |
199 | ||
200 | if (written) { | |
201 | s->left -= written; | |
202 | samples -= written; | |
203 | to_play -= written; | |
204 | s->pos = (s->pos + written) % s->samples; | |
205 | } | |
206 | else { | |
207 | return; | |
208 | } | |
209 | } | |
85571bc7 | 210 | |
58935915 | 211 | samples = MIN (samples, s->samples - s->pos); |
1d14ffa9 FB |
212 | if (!samples) { |
213 | return; | |
214 | } | |
85571bc7 | 215 | |
85571bc7 | 216 | YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples); |
85571bc7 FB |
217 | |
218 | while (samples) { | |
1d14ffa9 FB |
219 | written = write_audio (s, samples); |
220 | ||
221 | if (written) { | |
1d14ffa9 FB |
222 | samples -= written; |
223 | s->pos = (s->pos + written) % s->samples; | |
224 | } | |
225 | else { | |
226 | s->left = samples; | |
227 | return; | |
228 | } | |
85571bc7 | 229 | } |
85571bc7 FB |
230 | } |
231 | ||
232 | static void Adlib_fini (AdlibState *s) | |
233 | { | |
85571bc7 FB |
234 | if (s->opl) { |
235 | OPLDestroy (s->opl); | |
236 | s->opl = NULL; | |
237 | } | |
85571bc7 | 238 | |
fb7da626 | 239 | g_free(s->mixbuf); |
85571bc7 FB |
240 | |
241 | s->active = 0; | |
242 | s->enabled = 0; | |
c0fe3827 | 243 | AUD_remove_card (&s->card); |
85571bc7 FB |
244 | } |
245 | ||
a8aec295 | 246 | static MemoryRegionPortio adlib_portio_list[] = { |
a8aec295 JK |
247 | { 0, 4, 1, .read = adlib_read, .write = adlib_write, }, |
248 | { 0, 2, 1, .read = adlib_read, .write = adlib_write, }, | |
2b21fb57 | 249 | { 0x388, 4, 1, .read = adlib_read, .write = adlib_write, }, |
a8aec295 JK |
250 | PORTIO_END_OF_LIST(), |
251 | }; | |
252 | ||
db895a1e | 253 | static void adlib_realizefn (DeviceState *dev, Error **errp) |
85571bc7 | 254 | { |
8c444a19 | 255 | AdlibState *s = ADLIB(dev); |
1ea879e5 | 256 | struct audsettings as; |
c0fe3827 | 257 | |
8f7e2c2c | 258 | s->opl = OPLCreate (3579545, s->freq); |
85571bc7 | 259 | if (!s->opl) { |
db895a1e AF |
260 | error_setg (errp, "OPLCreate %d failed", s->freq); |
261 | return; | |
85571bc7 FB |
262 | } |
263 | else { | |
639b49ef | 264 | OPLSetTimerHandler(s->opl, timer_handler, s); |
85571bc7 FB |
265 | s->enabled = 1; |
266 | } | |
85571bc7 | 267 | |
8c444a19 | 268 | as.freq = s->freq; |
c0fe3827 | 269 | as.nchannels = SHIFT; |
85bc5852 | 270 | as.fmt = AUDIO_FORMAT_S16; |
d929eba5 | 271 | as.endianness = AUDIO_HOST_ENDIANNESS; |
c0fe3827 | 272 | |
1a7dafce | 273 | AUD_register_card ("adlib", &s->card); |
c0fe3827 | 274 | |
1d14ffa9 | 275 | s->voice = AUD_open_out ( |
c0fe3827 | 276 | &s->card, |
1d14ffa9 FB |
277 | s->voice, |
278 | "adlib", | |
279 | s, | |
280 | adlib_callback, | |
d929eba5 | 281 | &as |
1d14ffa9 | 282 | ); |
85571bc7 FB |
283 | if (!s->voice) { |
284 | Adlib_fini (s); | |
db895a1e AF |
285 | error_setg (errp, "Initializing audio voice failed"); |
286 | return; | |
85571bc7 FB |
287 | } |
288 | ||
1d14ffa9 | 289 | s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT; |
7267c094 | 290 | s->mixbuf = g_malloc0 (s->samples << SHIFT); |
85571bc7 | 291 | |
7f0ba7bb PB |
292 | adlib_portio_list[0].offset = s->port; |
293 | adlib_portio_list[1].offset = s->port + 8; | |
848696bf KB |
294 | portio_list_init (&s->port_list, OBJECT(s), adlib_portio_list, s, "adlib"); |
295 | portio_list_add (&s->port_list, isa_address_space_io(&s->parent_obj), 0); | |
85571bc7 | 296 | } |
8c444a19 PB |
297 | |
298 | static Property adlib_properties[] = { | |
88e47b9a | 299 | DEFINE_AUDIO_PROPERTIES(AdlibState, card), |
c7bcc85d | 300 | DEFINE_PROP_UINT32 ("iobase", AdlibState, port, 0x220), |
8c444a19 PB |
301 | DEFINE_PROP_UINT32 ("freq", AdlibState, freq, 44100), |
302 | DEFINE_PROP_END_OF_LIST (), | |
303 | }; | |
304 | ||
305 | static void adlib_class_initfn (ObjectClass *klass, void *data) | |
306 | { | |
307 | DeviceClass *dc = DEVICE_CLASS (klass); | |
db895a1e AF |
308 | |
309 | dc->realize = adlib_realizefn; | |
125ee0ed | 310 | set_bit(DEVICE_CATEGORY_SOUND, dc->categories); |
8c444a19 | 311 | dc->desc = ADLIB_DESC; |
4f67d30b | 312 | device_class_set_props(dc, adlib_properties); |
8c444a19 PB |
313 | } |
314 | ||
315 | static const TypeInfo adlib_info = { | |
316 | .name = TYPE_ADLIB, | |
317 | .parent = TYPE_ISA_DEVICE, | |
318 | .instance_size = sizeof (AdlibState), | |
319 | .class_init = adlib_class_initfn, | |
320 | }; | |
321 | ||
8c444a19 PB |
322 | static void adlib_register_types (void) |
323 | { | |
324 | type_register_static (&adlib_info); | |
86388a3b | 325 | deprecated_register_soundhw("adlib", ADLIB_DESC, 1, TYPE_ADLIB); |
8c444a19 PB |
326 | } |
327 | ||
328 | type_init (adlib_register_types) |