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