merged 15a_aqemu.patch audio patch (malc)
[qemu] / hw / adlib.c
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 #include <assert.h>
25 #include "vl.h"
26
27 #define ADLIB_KILL_TIMERS 1
28
29 #define dolog(...) AUD_log ("adlib", __VA_ARGS__)
30 #ifdef DEBUG
31 #define ldebug(...) dolog (__VA_ARGS__)
32 #else
33 #define ldebug(...)
34 #endif
35
36 #ifdef HAS_YMF262
37 #include "ymf262.h"
38 void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
39 #define SHIFT 2
40 #else
41 #include "fmopl.h"
42 #define SHIFT 1
43 #endif
44
45 #define IO_READ_PROTO(name) \
46     uint32_t name (void *opaque, uint32_t nport)
47 #define IO_WRITE_PROTO(name) \
48     void name (void *opaque, uint32_t nport, uint32_t val)
49
50 static struct {
51     int port;
52     int freq;
53 } conf = {0x220, 44100};
54
55 typedef struct {
56     int ticking[2];
57     int enabled;
58     int active;
59     int bufpos;
60 #ifdef DEBUG
61     int64_t exp[2];
62 #endif
63     int16_t *mixbuf;
64     uint64_t dexp[2];
65     SWVoiceOut *voice;
66     int left, pos, samples;
67     QEMUAudioTimeStamp ats;
68 #ifndef HAS_YMF262
69     FM_OPL *opl;
70 #endif
71 } AdlibState;
72
73 static AdlibState adlib;
74
75 static void adlib_stop_opl_timer (AdlibState *s, size_t n)
76 {
77 #ifdef HAS_YMF262
78     YMF262TimerOver (0, n);
79 #else
80     OPLTimerOver (s->opl, n);
81 #endif
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_time_stamp_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 IO_WRITE_PROTO(adlib_write)
109 {
110     AdlibState *s = opaque;
111     int a = nport & 3;
112     int status;
113
114     s->active = 1;
115     AUD_set_active_out (s->voice, 1);
116
117     adlib_kill_timers (s);
118
119 #ifdef HAS_YMF262
120     status = YMF262Write (0, a, val);
121 #else
122     status = OPLWrite (s->opl, a, val);
123 #endif
124 }
125
126 static IO_READ_PROTO(adlib_read)
127 {
128     AdlibState *s = opaque;
129     uint8_t data;
130     int a = nport & 3;
131
132     adlib_kill_timers (s);
133
134 #ifdef HAS_YMF262
135     data = YMF262Read (0, a);
136 #else
137     data = OPLRead (s->opl, a);
138 #endif
139     return data;
140 }
141
142 static void timer_handler (int c, double interval_Sec)
143 {
144     AdlibState *s = &adlib;
145     unsigned n = c & 1;
146 #ifdef DEBUG
147     double interval;
148 #endif
149
150     if (interval_Sec == 0.0) {
151         s->ticking[n] = 0;
152         return;
153     }
154
155     s->ticking[n] = 1;
156 #ifdef DEBUG
157     interval = ticks_per_sec * interval_Sec;
158     exp = qemu_get_clock (vm_clock) + interval;
159     s->exp[n] = exp;
160 #endif
161
162     s->dexp[n] = interval_Sec * 1000000.0;
163     AUD_init_time_stamp_out (s->voice, &s->ats);
164 }
165
166 static int write_audio (AdlibState *s, int samples)
167 {
168     int net = 0;
169     int pos = s->pos;
170
171     while (samples) {
172         int nbytes, wbytes, wsampl;
173
174         nbytes = samples << SHIFT;
175         wbytes = AUD_write (
176             s->voice,
177             s->mixbuf + (pos << (SHIFT - 1)),
178             nbytes
179             );
180
181         if (wbytes) {
182             wsampl = wbytes >> SHIFT;
183
184             samples -= wsampl;
185             pos = (pos + wsampl) % s->samples;
186
187             net += wsampl;
188         }
189         else {
190             break;
191         }
192     }
193
194     return net;
195 }
196
197 static void adlib_callback (void *opaque, int free)
198 {
199     AdlibState *s = opaque;
200     int samples, net = 0, to_play, written;
201
202     samples = free >> SHIFT;
203     if (!(s->active && s->enabled) || !samples) {
204         return;
205     }
206
207     to_play = audio_MIN (s->left, samples);
208     while (to_play) {
209         written = write_audio (s, to_play);
210
211         if (written) {
212             s->left -= written;
213             samples -= written;
214             to_play -= written;
215             s->pos = (s->pos + written) % s->samples;
216         }
217         else {
218             return;
219         }
220     }
221
222     samples = audio_MIN (samples, s->samples - s->pos);
223     if (!samples) {
224         return;
225     }
226
227 #ifdef HAS_YMF262
228     YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
229 #else
230     YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
231 #endif
232
233     while (samples) {
234         written = write_audio (s, samples);
235
236         if (written) {
237             net += written;
238             samples -= written;
239             s->pos = (s->pos + written) % s->samples;
240         }
241         else {
242             s->left = samples;
243             return;
244         }
245     }
246 }
247
248 static void Adlib_fini (AdlibState *s)
249 {
250 #ifdef HAS_YMF262
251     YMF262Shutdown ();
252 #else
253     if (s->opl) {
254         OPLDestroy (s->opl);
255         s->opl = NULL;
256     }
257 #endif
258
259     if (s->mixbuf) {
260         qemu_free (s->mixbuf);
261     }
262
263     s->active = 0;
264     s->enabled = 0;
265 }
266
267 void Adlib_init (void)
268 {
269     AdlibState *s = &adlib;
270
271 #ifdef HAS_YMF262
272     if (YMF262Init (1, 14318180, conf.freq)) {
273         dolog ("YMF262Init %d failed\n", conf.freq);
274         return;
275     }
276     else {
277         YMF262SetTimerHandler (0, timer_handler, 0);
278         s->enabled = 1;
279     }
280 #else
281     s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
282     if (!s->opl) {
283         dolog ("OPLCreate %d failed\n", conf.freq);
284         return;
285     }
286     else {
287         OPLSetTimerHandler (s->opl, timer_handler, 0);
288         s->enabled = 1;
289     }
290 #endif
291
292     s->voice = AUD_open_out (
293         s->voice,
294         "adlib",
295         s,
296         adlib_callback,
297         conf.freq,
298         SHIFT,
299         AUD_FMT_S16
300         );
301     if (!s->voice) {
302         Adlib_fini (s);
303         return;
304     }
305
306     s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
307     s->mixbuf = qemu_mallocz (s->samples << SHIFT);
308
309     if (!s->mixbuf) {
310         dolog ("not enough memory for adlib mixing buffer (%d)\n",
311                s->samples << SHIFT);
312         Adlib_fini (s);
313         return;
314     }
315
316     register_ioport_read (0x388, 4, 1, adlib_read, s);
317     register_ioport_write (0x388, 4, 1, adlib_write, s);
318
319     register_ioport_read (conf.port, 4, 1, adlib_read, s);
320     register_ioport_write (conf.port, 4, 1, adlib_write, s);
321
322     register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
323     register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
324 }