root / hw / adlib.c @ 00dbbb03
History | View | Annotate | Download (7.7 kB)
1 | 85571bc7 | bellard | /*
|
---|---|---|---|
2 | 1d14ffa9 | bellard | * QEMU Proxy for OPL2/3 emulation by MAME team
|
3 | 1d14ffa9 | bellard | *
|
4 | 1d14ffa9 | bellard | * Copyright (c) 2004-2005 Vassili Karpov (malc)
|
5 | 1d14ffa9 | bellard | *
|
6 | 85571bc7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 85571bc7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 85571bc7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 85571bc7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 85571bc7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 85571bc7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 85571bc7 | bellard | *
|
13 | 85571bc7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 85571bc7 | bellard | * all copies or substantial portions of the Software.
|
15 | 85571bc7 | bellard | *
|
16 | 85571bc7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 85571bc7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 85571bc7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 85571bc7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 85571bc7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 85571bc7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 85571bc7 | bellard | * THE SOFTWARE.
|
23 | 85571bc7 | bellard | */
|
24 | 9f0683d9 | ths | |
25 | 1d14ffa9 | bellard | #include <assert.h> |
26 | 87ecb68b | pbrook | #include "hw.h" |
27 | 87ecb68b | pbrook | #include "audiodev.h" |
28 | e140e05c | ths | #include "audio/audio.h" |
29 | 69b34976 | ths | #include "isa.h" |
30 | 85571bc7 | bellard | |
31 | 9f0683d9 | ths | //#define DEBUG
|
32 | 9f0683d9 | ths | |
33 | 1d14ffa9 | bellard | #define ADLIB_KILL_TIMERS 1 |
34 | 1d14ffa9 | bellard | |
35 | 9f0683d9 | ths | #ifdef DEBUG
|
36 | 9f0683d9 | ths | #include "qemu-timer.h" |
37 | 9f0683d9 | ths | #endif
|
38 | 9f0683d9 | ths | |
39 | fb065187 | bellard | #define dolog(...) AUD_log ("adlib", __VA_ARGS__) |
40 | fb065187 | bellard | #ifdef DEBUG
|
41 | fb065187 | bellard | #define ldebug(...) dolog (__VA_ARGS__)
|
42 | fb065187 | bellard | #else
|
43 | fb065187 | bellard | #define ldebug(...)
|
44 | fb065187 | bellard | #endif
|
45 | 85571bc7 | bellard | |
46 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
47 | 85571bc7 | bellard | #include "ymf262.h" |
48 | 1d14ffa9 | bellard | void YMF262UpdateOneQEMU (int which, INT16 *dst, int length); |
49 | 85571bc7 | bellard | #define SHIFT 2 |
50 | 85571bc7 | bellard | #else
|
51 | 85571bc7 | bellard | #include "fmopl.h" |
52 | 85571bc7 | bellard | #define SHIFT 1 |
53 | 85571bc7 | bellard | #endif
|
54 | 85571bc7 | bellard | |
55 | 85571bc7 | bellard | #define IO_READ_PROTO(name) \
|
56 | 85571bc7 | bellard | uint32_t name (void *opaque, uint32_t nport)
|
57 | 85571bc7 | bellard | #define IO_WRITE_PROTO(name) \
|
58 | 85571bc7 | bellard | void name (void *opaque, uint32_t nport, uint32_t val) |
59 | 85571bc7 | bellard | |
60 | 85571bc7 | bellard | static struct { |
61 | 85571bc7 | bellard | int port;
|
62 | 85571bc7 | bellard | int freq;
|
63 | 85571bc7 | bellard | } conf = {0x220, 44100}; |
64 | 85571bc7 | bellard | |
65 | 85571bc7 | bellard | typedef struct { |
66 | c0fe3827 | bellard | QEMUSoundCard card; |
67 | 1d14ffa9 | bellard | int ticking[2]; |
68 | 85571bc7 | bellard | int enabled;
|
69 | 85571bc7 | bellard | int active;
|
70 | 85571bc7 | bellard | int bufpos;
|
71 | 1d14ffa9 | bellard | #ifdef DEBUG
|
72 | 1d14ffa9 | bellard | int64_t exp[2];
|
73 | 1d14ffa9 | bellard | #endif
|
74 | 85571bc7 | bellard | int16_t *mixbuf; |
75 | 1d14ffa9 | bellard | uint64_t dexp[2];
|
76 | 1d14ffa9 | bellard | SWVoiceOut *voice; |
77 | 1d14ffa9 | bellard | int left, pos, samples;
|
78 | 1d14ffa9 | bellard | QEMUAudioTimeStamp ats; |
79 | 1d14ffa9 | bellard | #ifndef HAS_YMF262
|
80 | 85571bc7 | bellard | FM_OPL *opl; |
81 | 85571bc7 | bellard | #endif
|
82 | 85571bc7 | bellard | } AdlibState; |
83 | 85571bc7 | bellard | |
84 | c0fe3827 | bellard | static AdlibState glob_adlib;
|
85 | 85571bc7 | bellard | |
86 | 1d14ffa9 | bellard | static void adlib_stop_opl_timer (AdlibState *s, size_t n) |
87 | 1d14ffa9 | bellard | { |
88 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
89 | 1d14ffa9 | bellard | YMF262TimerOver (0, n);
|
90 | 1d14ffa9 | bellard | #else
|
91 | 1d14ffa9 | bellard | OPLTimerOver (s->opl, n); |
92 | 1d14ffa9 | bellard | #endif
|
93 | 1d14ffa9 | bellard | s->ticking[n] = 0;
|
94 | 1d14ffa9 | bellard | } |
95 | 1d14ffa9 | bellard | |
96 | 1d14ffa9 | bellard | static void adlib_kill_timers (AdlibState *s) |
97 | 1d14ffa9 | bellard | { |
98 | 1d14ffa9 | bellard | size_t i; |
99 | 1d14ffa9 | bellard | |
100 | 1d14ffa9 | bellard | for (i = 0; i < 2; ++i) { |
101 | 1d14ffa9 | bellard | if (s->ticking[i]) {
|
102 | 1d14ffa9 | bellard | uint64_t delta; |
103 | 1d14ffa9 | bellard | |
104 | c0fe3827 | bellard | delta = AUD_get_elapsed_usec_out (s->voice, &s->ats); |
105 | 1d14ffa9 | bellard | ldebug ( |
106 | 1d14ffa9 | bellard | "delta = %f dexp = %f expired => %d\n",
|
107 | 1d14ffa9 | bellard | delta / 1000000.0, |
108 | 1d14ffa9 | bellard | s->dexp[i] / 1000000.0, |
109 | 1d14ffa9 | bellard | delta >= s->dexp[i] |
110 | 1d14ffa9 | bellard | ); |
111 | 1d14ffa9 | bellard | if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
|
112 | 1d14ffa9 | bellard | adlib_stop_opl_timer (s, i); |
113 | 1d14ffa9 | bellard | AUD_init_time_stamp_out (s->voice, &s->ats); |
114 | 1d14ffa9 | bellard | } |
115 | 1d14ffa9 | bellard | } |
116 | 1d14ffa9 | bellard | } |
117 | 1d14ffa9 | bellard | } |
118 | 1d14ffa9 | bellard | |
119 | 85571bc7 | bellard | static IO_WRITE_PROTO(adlib_write)
|
120 | 85571bc7 | bellard | { |
121 | 85571bc7 | bellard | AdlibState *s = opaque; |
122 | 85571bc7 | bellard | int a = nport & 3; |
123 | 85571bc7 | bellard | int status;
|
124 | 85571bc7 | bellard | |
125 | 85571bc7 | bellard | s->active = 1;
|
126 | 1d14ffa9 | bellard | AUD_set_active_out (s->voice, 1);
|
127 | 85571bc7 | bellard | |
128 | 1d14ffa9 | bellard | adlib_kill_timers (s); |
129 | 1d14ffa9 | bellard | |
130 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
131 | 85571bc7 | bellard | status = YMF262Write (0, a, val);
|
132 | 85571bc7 | bellard | #else
|
133 | 85571bc7 | bellard | status = OPLWrite (s->opl, a, val); |
134 | 85571bc7 | bellard | #endif
|
135 | 85571bc7 | bellard | } |
136 | 85571bc7 | bellard | |
137 | 85571bc7 | bellard | static IO_READ_PROTO(adlib_read)
|
138 | 85571bc7 | bellard | { |
139 | 85571bc7 | bellard | AdlibState *s = opaque; |
140 | 85571bc7 | bellard | uint8_t data; |
141 | 85571bc7 | bellard | int a = nport & 3; |
142 | 85571bc7 | bellard | |
143 | 1d14ffa9 | bellard | adlib_kill_timers (s); |
144 | 1d14ffa9 | bellard | |
145 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
146 | 85571bc7 | bellard | data = YMF262Read (0, a);
|
147 | 85571bc7 | bellard | #else
|
148 | 85571bc7 | bellard | data = OPLRead (s->opl, a); |
149 | 85571bc7 | bellard | #endif
|
150 | 85571bc7 | bellard | return data;
|
151 | 85571bc7 | bellard | } |
152 | 85571bc7 | bellard | |
153 | 1d14ffa9 | bellard | static void timer_handler (int c, double interval_Sec) |
154 | 85571bc7 | bellard | { |
155 | c0fe3827 | bellard | AdlibState *s = &glob_adlib; |
156 | 1d14ffa9 | bellard | unsigned n = c & 1; |
157 | 1d14ffa9 | bellard | #ifdef DEBUG
|
158 | 1d14ffa9 | bellard | double interval;
|
159 | c0fe3827 | bellard | int64_t exp; |
160 | 85571bc7 | bellard | #endif
|
161 | 85571bc7 | bellard | |
162 | 85571bc7 | bellard | if (interval_Sec == 0.0) { |
163 | 1d14ffa9 | bellard | s->ticking[n] = 0;
|
164 | 85571bc7 | bellard | return;
|
165 | 85571bc7 | bellard | } |
166 | 1d14ffa9 | bellard | |
167 | 1d14ffa9 | bellard | s->ticking[n] = 1;
|
168 | 1d14ffa9 | bellard | #ifdef DEBUG
|
169 | 1d14ffa9 | bellard | interval = ticks_per_sec * interval_Sec; |
170 | 1d14ffa9 | bellard | exp = qemu_get_clock (vm_clock) + interval; |
171 | 1d14ffa9 | bellard | s->exp[n] = exp; |
172 | 1d14ffa9 | bellard | #endif
|
173 | 1d14ffa9 | bellard | |
174 | 1d14ffa9 | bellard | s->dexp[n] = interval_Sec * 1000000.0; |
175 | 1d14ffa9 | bellard | AUD_init_time_stamp_out (s->voice, &s->ats); |
176 | 85571bc7 | bellard | } |
177 | 85571bc7 | bellard | |
178 | 85571bc7 | bellard | static int write_audio (AdlibState *s, int samples) |
179 | 85571bc7 | bellard | { |
180 | 85571bc7 | bellard | int net = 0; |
181 | 1d14ffa9 | bellard | int pos = s->pos;
|
182 | 1d14ffa9 | bellard | |
183 | 85571bc7 | bellard | while (samples) {
|
184 | 1d14ffa9 | bellard | int nbytes, wbytes, wsampl;
|
185 | 1d14ffa9 | bellard | |
186 | 1d14ffa9 | bellard | nbytes = samples << SHIFT; |
187 | 1d14ffa9 | bellard | wbytes = AUD_write ( |
188 | 1d14ffa9 | bellard | s->voice, |
189 | 1d14ffa9 | bellard | s->mixbuf + (pos << (SHIFT - 1)),
|
190 | 1d14ffa9 | bellard | nbytes |
191 | 1d14ffa9 | bellard | ); |
192 | 1d14ffa9 | bellard | |
193 | 1d14ffa9 | bellard | if (wbytes) {
|
194 | 1d14ffa9 | bellard | wsampl = wbytes >> SHIFT; |
195 | 1d14ffa9 | bellard | |
196 | 1d14ffa9 | bellard | samples -= wsampl; |
197 | 1d14ffa9 | bellard | pos = (pos + wsampl) % s->samples; |
198 | 1d14ffa9 | bellard | |
199 | 1d14ffa9 | bellard | net += wsampl; |
200 | 1d14ffa9 | bellard | } |
201 | 1d14ffa9 | bellard | else {
|
202 | 85571bc7 | bellard | break;
|
203 | 1d14ffa9 | bellard | } |
204 | 85571bc7 | bellard | } |
205 | 1d14ffa9 | bellard | |
206 | 85571bc7 | bellard | return net;
|
207 | 85571bc7 | bellard | } |
208 | 85571bc7 | bellard | |
209 | 1d14ffa9 | bellard | static void adlib_callback (void *opaque, int free) |
210 | 85571bc7 | bellard | { |
211 | 85571bc7 | bellard | AdlibState *s = opaque; |
212 | 1d14ffa9 | bellard | int samples, net = 0, to_play, written; |
213 | 85571bc7 | bellard | |
214 | 1d14ffa9 | bellard | samples = free >> SHIFT; |
215 | 1d14ffa9 | bellard | if (!(s->active && s->enabled) || !samples) {
|
216 | 1d14ffa9 | bellard | return;
|
217 | 85571bc7 | bellard | } |
218 | 85571bc7 | bellard | |
219 | 1d14ffa9 | bellard | to_play = audio_MIN (s->left, samples); |
220 | 1d14ffa9 | bellard | while (to_play) {
|
221 | 1d14ffa9 | bellard | written = write_audio (s, to_play); |
222 | 1d14ffa9 | bellard | |
223 | 1d14ffa9 | bellard | if (written) {
|
224 | 1d14ffa9 | bellard | s->left -= written; |
225 | 1d14ffa9 | bellard | samples -= written; |
226 | 1d14ffa9 | bellard | to_play -= written; |
227 | 1d14ffa9 | bellard | s->pos = (s->pos + written) % s->samples; |
228 | 1d14ffa9 | bellard | } |
229 | 1d14ffa9 | bellard | else {
|
230 | 1d14ffa9 | bellard | return;
|
231 | 1d14ffa9 | bellard | } |
232 | 1d14ffa9 | bellard | } |
233 | 85571bc7 | bellard | |
234 | 85571bc7 | bellard | samples = audio_MIN (samples, s->samples - s->pos); |
235 | 1d14ffa9 | bellard | if (!samples) {
|
236 | 1d14ffa9 | bellard | return;
|
237 | 1d14ffa9 | bellard | } |
238 | 85571bc7 | bellard | |
239 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
240 | 85571bc7 | bellard | YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples); |
241 | 85571bc7 | bellard | #else
|
242 | 85571bc7 | bellard | YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples); |
243 | 85571bc7 | bellard | #endif
|
244 | 85571bc7 | bellard | |
245 | 85571bc7 | bellard | while (samples) {
|
246 | 1d14ffa9 | bellard | written = write_audio (s, samples); |
247 | 1d14ffa9 | bellard | |
248 | 1d14ffa9 | bellard | if (written) {
|
249 | 1d14ffa9 | bellard | net += written; |
250 | 1d14ffa9 | bellard | samples -= written; |
251 | 1d14ffa9 | bellard | s->pos = (s->pos + written) % s->samples; |
252 | 1d14ffa9 | bellard | } |
253 | 1d14ffa9 | bellard | else {
|
254 | 1d14ffa9 | bellard | s->left = samples; |
255 | 1d14ffa9 | bellard | return;
|
256 | 1d14ffa9 | bellard | } |
257 | 85571bc7 | bellard | } |
258 | 85571bc7 | bellard | } |
259 | 85571bc7 | bellard | |
260 | 85571bc7 | bellard | static void Adlib_fini (AdlibState *s) |
261 | 85571bc7 | bellard | { |
262 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
263 | 85571bc7 | bellard | YMF262Shutdown (); |
264 | 85571bc7 | bellard | #else
|
265 | 85571bc7 | bellard | if (s->opl) {
|
266 | 85571bc7 | bellard | OPLDestroy (s->opl); |
267 | 85571bc7 | bellard | s->opl = NULL;
|
268 | 85571bc7 | bellard | } |
269 | 85571bc7 | bellard | #endif
|
270 | 85571bc7 | bellard | |
271 | 1d14ffa9 | bellard | if (s->mixbuf) {
|
272 | 1d14ffa9 | bellard | qemu_free (s->mixbuf); |
273 | 1d14ffa9 | bellard | } |
274 | 85571bc7 | bellard | |
275 | 85571bc7 | bellard | s->active = 0;
|
276 | 85571bc7 | bellard | s->enabled = 0;
|
277 | c0fe3827 | bellard | AUD_remove_card (&s->card); |
278 | 85571bc7 | bellard | } |
279 | 85571bc7 | bellard | |
280 | d537cf6c | pbrook | int Adlib_init (AudioState *audio, qemu_irq *pic)
|
281 | 85571bc7 | bellard | { |
282 | c0fe3827 | bellard | AdlibState *s = &glob_adlib; |
283 | c0fe3827 | bellard | audsettings_t as; |
284 | c0fe3827 | bellard | |
285 | c0fe3827 | bellard | if (!audio) {
|
286 | c0fe3827 | bellard | dolog ("No audio state\n");
|
287 | c0fe3827 | bellard | return -1; |
288 | c0fe3827 | bellard | } |
289 | 85571bc7 | bellard | |
290 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
291 | 85571bc7 | bellard | if (YMF262Init (1, 14318180, conf.freq)) { |
292 | 85571bc7 | bellard | dolog ("YMF262Init %d failed\n", conf.freq);
|
293 | c0fe3827 | bellard | return -1; |
294 | 85571bc7 | bellard | } |
295 | 85571bc7 | bellard | else {
|
296 | 1d14ffa9 | bellard | YMF262SetTimerHandler (0, timer_handler, 0); |
297 | 85571bc7 | bellard | s->enabled = 1;
|
298 | 85571bc7 | bellard | } |
299 | 85571bc7 | bellard | #else
|
300 | 85571bc7 | bellard | s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
|
301 | 85571bc7 | bellard | if (!s->opl) {
|
302 | 85571bc7 | bellard | dolog ("OPLCreate %d failed\n", conf.freq);
|
303 | c0fe3827 | bellard | return -1; |
304 | 85571bc7 | bellard | } |
305 | 85571bc7 | bellard | else {
|
306 | 1d14ffa9 | bellard | OPLSetTimerHandler (s->opl, timer_handler, 0);
|
307 | 85571bc7 | bellard | s->enabled = 1;
|
308 | 85571bc7 | bellard | } |
309 | 85571bc7 | bellard | #endif
|
310 | 85571bc7 | bellard | |
311 | c0fe3827 | bellard | as.freq = conf.freq; |
312 | c0fe3827 | bellard | as.nchannels = SHIFT; |
313 | c0fe3827 | bellard | as.fmt = AUD_FMT_S16; |
314 | d929eba5 | bellard | as.endianness = AUDIO_HOST_ENDIANNESS; |
315 | c0fe3827 | bellard | |
316 | c0fe3827 | bellard | AUD_register_card (audio, "adlib", &s->card);
|
317 | c0fe3827 | bellard | |
318 | 1d14ffa9 | bellard | s->voice = AUD_open_out ( |
319 | c0fe3827 | bellard | &s->card, |
320 | 1d14ffa9 | bellard | s->voice, |
321 | 1d14ffa9 | bellard | "adlib",
|
322 | 1d14ffa9 | bellard | s, |
323 | 1d14ffa9 | bellard | adlib_callback, |
324 | d929eba5 | bellard | &as |
325 | 1d14ffa9 | bellard | ); |
326 | 85571bc7 | bellard | if (!s->voice) {
|
327 | 85571bc7 | bellard | Adlib_fini (s); |
328 | c0fe3827 | bellard | return -1; |
329 | 85571bc7 | bellard | } |
330 | 85571bc7 | bellard | |
331 | 1d14ffa9 | bellard | s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT; |
332 | 85571bc7 | bellard | s->mixbuf = qemu_mallocz (s->samples << SHIFT); |
333 | 85571bc7 | bellard | |
334 | 85571bc7 | bellard | if (!s->mixbuf) {
|
335 | 546754dc | bellard | dolog ("Could not allocate mixing buffer, %d samples (each %d bytes)\n",
|
336 | 546754dc | bellard | s->samples, 1 << SHIFT);
|
337 | 85571bc7 | bellard | Adlib_fini (s); |
338 | c0fe3827 | bellard | return -1; |
339 | 85571bc7 | bellard | } |
340 | 1d14ffa9 | bellard | |
341 | 85571bc7 | bellard | register_ioport_read (0x388, 4, 1, adlib_read, s); |
342 | 85571bc7 | bellard | register_ioport_write (0x388, 4, 1, adlib_write, s); |
343 | 85571bc7 | bellard | |
344 | 85571bc7 | bellard | register_ioport_read (conf.port, 4, 1, adlib_read, s); |
345 | 85571bc7 | bellard | register_ioport_write (conf.port, 4, 1, adlib_write, s); |
346 | 85571bc7 | bellard | |
347 | 85571bc7 | bellard | register_ioport_read (conf.port + 8, 2, 1, adlib_read, s); |
348 | 85571bc7 | bellard | register_ioport_write (conf.port + 8, 2, 1, adlib_write, s); |
349 | c0fe3827 | bellard | |
350 | c0fe3827 | bellard | return 0; |
351 | 85571bc7 | bellard | } |