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