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