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