root / hw / adlib.c @ 1673ad51
History | View | Annotate | Download (7.4 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 | 87ecb68b | pbrook | #include "hw.h" |
26 | 87ecb68b | pbrook | #include "audiodev.h" |
27 | e140e05c | ths | #include "audio/audio.h" |
28 | 69b34976 | ths | #include "isa.h" |
29 | 85571bc7 | bellard | |
30 | 9f0683d9 | ths | //#define DEBUG
|
31 | 9f0683d9 | ths | |
32 | 1d14ffa9 | bellard | #define ADLIB_KILL_TIMERS 1 |
33 | 1d14ffa9 | bellard | |
34 | 9f0683d9 | ths | #ifdef DEBUG
|
35 | 9f0683d9 | ths | #include "qemu-timer.h" |
36 | 9f0683d9 | ths | #endif
|
37 | 9f0683d9 | ths | |
38 | fb065187 | bellard | #define dolog(...) AUD_log ("adlib", __VA_ARGS__) |
39 | fb065187 | bellard | #ifdef DEBUG
|
40 | fb065187 | bellard | #define ldebug(...) dolog (__VA_ARGS__)
|
41 | fb065187 | bellard | #else
|
42 | fb065187 | bellard | #define ldebug(...)
|
43 | fb065187 | bellard | #endif
|
44 | 85571bc7 | bellard | |
45 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
46 | 85571bc7 | bellard | #include "ymf262.h" |
47 | 1d14ffa9 | bellard | void YMF262UpdateOneQEMU (int which, INT16 *dst, int length); |
48 | 85571bc7 | bellard | #define SHIFT 2 |
49 | 85571bc7 | bellard | #else
|
50 | 85571bc7 | bellard | #include "fmopl.h" |
51 | 85571bc7 | bellard | #define SHIFT 1 |
52 | 85571bc7 | bellard | #endif
|
53 | 85571bc7 | bellard | |
54 | 85571bc7 | bellard | #define IO_READ_PROTO(name) \
|
55 | 85571bc7 | bellard | uint32_t name (void *opaque, uint32_t nport)
|
56 | 85571bc7 | bellard | #define IO_WRITE_PROTO(name) \
|
57 | 85571bc7 | bellard | void name (void *opaque, uint32_t nport, uint32_t val) |
58 | 85571bc7 | bellard | |
59 | 85571bc7 | bellard | static struct { |
60 | 85571bc7 | bellard | int port;
|
61 | 85571bc7 | bellard | int freq;
|
62 | 85571bc7 | bellard | } conf = {0x220, 44100}; |
63 | 85571bc7 | bellard | |
64 | 85571bc7 | bellard | typedef struct { |
65 | c0fe3827 | bellard | QEMUSoundCard card; |
66 | 1d14ffa9 | bellard | int ticking[2]; |
67 | 85571bc7 | bellard | int enabled;
|
68 | 85571bc7 | bellard | int active;
|
69 | 85571bc7 | bellard | int bufpos;
|
70 | 1d14ffa9 | bellard | #ifdef DEBUG
|
71 | 1d14ffa9 | bellard | int64_t exp[2];
|
72 | 1d14ffa9 | bellard | #endif
|
73 | 85571bc7 | bellard | int16_t *mixbuf; |
74 | 1d14ffa9 | bellard | uint64_t dexp[2];
|
75 | 1d14ffa9 | bellard | SWVoiceOut *voice; |
76 | 1d14ffa9 | bellard | int left, pos, samples;
|
77 | 1d14ffa9 | bellard | QEMUAudioTimeStamp ats; |
78 | 1d14ffa9 | bellard | #ifndef HAS_YMF262
|
79 | 85571bc7 | bellard | FM_OPL *opl; |
80 | 85571bc7 | bellard | #endif
|
81 | 85571bc7 | bellard | } AdlibState; |
82 | 85571bc7 | bellard | |
83 | c0fe3827 | bellard | static AdlibState glob_adlib;
|
84 | 85571bc7 | bellard | |
85 | 1d14ffa9 | bellard | static void adlib_stop_opl_timer (AdlibState *s, size_t n) |
86 | 1d14ffa9 | bellard | { |
87 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
88 | 1d14ffa9 | bellard | YMF262TimerOver (0, n);
|
89 | 1d14ffa9 | bellard | #else
|
90 | 1d14ffa9 | bellard | OPLTimerOver (s->opl, n); |
91 | 1d14ffa9 | bellard | #endif
|
92 | 1d14ffa9 | bellard | s->ticking[n] = 0;
|
93 | 1d14ffa9 | bellard | } |
94 | 1d14ffa9 | bellard | |
95 | 1d14ffa9 | bellard | static void adlib_kill_timers (AdlibState *s) |
96 | 1d14ffa9 | bellard | { |
97 | 1d14ffa9 | bellard | size_t i; |
98 | 1d14ffa9 | bellard | |
99 | 1d14ffa9 | bellard | for (i = 0; i < 2; ++i) { |
100 | 1d14ffa9 | bellard | if (s->ticking[i]) {
|
101 | 1d14ffa9 | bellard | uint64_t delta; |
102 | 1d14ffa9 | bellard | |
103 | c0fe3827 | bellard | delta = AUD_get_elapsed_usec_out (s->voice, &s->ats); |
104 | 1d14ffa9 | bellard | ldebug ( |
105 | 1d14ffa9 | bellard | "delta = %f dexp = %f expired => %d\n",
|
106 | 1d14ffa9 | bellard | delta / 1000000.0, |
107 | 1d14ffa9 | bellard | s->dexp[i] / 1000000.0, |
108 | 1d14ffa9 | bellard | delta >= s->dexp[i] |
109 | 1d14ffa9 | bellard | ); |
110 | 1d14ffa9 | bellard | if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
|
111 | 1d14ffa9 | bellard | adlib_stop_opl_timer (s, i); |
112 | 1d14ffa9 | bellard | AUD_init_time_stamp_out (s->voice, &s->ats); |
113 | 1d14ffa9 | bellard | } |
114 | 1d14ffa9 | bellard | } |
115 | 1d14ffa9 | bellard | } |
116 | 1d14ffa9 | bellard | } |
117 | 1d14ffa9 | bellard | |
118 | d999f7e0 | malc | static IO_WRITE_PROTO (adlib_write)
|
119 | 85571bc7 | bellard | { |
120 | 85571bc7 | bellard | AdlibState *s = opaque; |
121 | 85571bc7 | bellard | int a = nport & 3; |
122 | 85571bc7 | bellard | int status;
|
123 | 85571bc7 | bellard | |
124 | 85571bc7 | bellard | s->active = 1;
|
125 | 1d14ffa9 | bellard | AUD_set_active_out (s->voice, 1);
|
126 | 85571bc7 | bellard | |
127 | 1d14ffa9 | bellard | adlib_kill_timers (s); |
128 | 1d14ffa9 | bellard | |
129 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
130 | 85571bc7 | bellard | status = YMF262Write (0, a, val);
|
131 | 85571bc7 | bellard | #else
|
132 | 85571bc7 | bellard | status = OPLWrite (s->opl, a, val); |
133 | 85571bc7 | bellard | #endif
|
134 | 85571bc7 | bellard | } |
135 | 85571bc7 | bellard | |
136 | d999f7e0 | malc | static IO_READ_PROTO (adlib_read)
|
137 | 85571bc7 | bellard | { |
138 | 85571bc7 | bellard | AdlibState *s = opaque; |
139 | 85571bc7 | bellard | uint8_t data; |
140 | 85571bc7 | bellard | int a = nport & 3; |
141 | 85571bc7 | bellard | |
142 | 1d14ffa9 | bellard | adlib_kill_timers (s); |
143 | 1d14ffa9 | bellard | |
144 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
145 | 85571bc7 | bellard | data = YMF262Read (0, a);
|
146 | 85571bc7 | bellard | #else
|
147 | 85571bc7 | bellard | data = OPLRead (s->opl, a); |
148 | 85571bc7 | bellard | #endif
|
149 | 85571bc7 | bellard | return data;
|
150 | 85571bc7 | bellard | } |
151 | 85571bc7 | bellard | |
152 | 1d14ffa9 | bellard | static void timer_handler (int c, double interval_Sec) |
153 | 85571bc7 | bellard | { |
154 | c0fe3827 | bellard | AdlibState *s = &glob_adlib; |
155 | 1d14ffa9 | bellard | unsigned n = c & 1; |
156 | 1d14ffa9 | bellard | #ifdef DEBUG
|
157 | 1d14ffa9 | bellard | double interval;
|
158 | c0fe3827 | bellard | int64_t exp; |
159 | 85571bc7 | bellard | #endif
|
160 | 85571bc7 | bellard | |
161 | 85571bc7 | bellard | if (interval_Sec == 0.0) { |
162 | 1d14ffa9 | bellard | s->ticking[n] = 0;
|
163 | 85571bc7 | bellard | return;
|
164 | 85571bc7 | bellard | } |
165 | 1d14ffa9 | bellard | |
166 | 1d14ffa9 | bellard | s->ticking[n] = 1;
|
167 | 1d14ffa9 | bellard | #ifdef DEBUG
|
168 | 6ee093c9 | Juan Quintela | interval = get_ticks_per_sec() * interval_Sec; |
169 | 1d14ffa9 | bellard | exp = qemu_get_clock (vm_clock) + interval; |
170 | 1d14ffa9 | bellard | s->exp[n] = exp; |
171 | 1d14ffa9 | bellard | #endif
|
172 | 1d14ffa9 | bellard | |
173 | 1d14ffa9 | bellard | s->dexp[n] = interval_Sec * 1000000.0; |
174 | 1d14ffa9 | bellard | AUD_init_time_stamp_out (s->voice, &s->ats); |
175 | 85571bc7 | bellard | } |
176 | 85571bc7 | bellard | |
177 | 85571bc7 | bellard | static int write_audio (AdlibState *s, int samples) |
178 | 85571bc7 | bellard | { |
179 | 85571bc7 | bellard | int net = 0; |
180 | 1d14ffa9 | bellard | int pos = s->pos;
|
181 | 1d14ffa9 | bellard | |
182 | 85571bc7 | bellard | while (samples) {
|
183 | 1d14ffa9 | bellard | int nbytes, wbytes, wsampl;
|
184 | 1d14ffa9 | bellard | |
185 | 1d14ffa9 | bellard | nbytes = samples << SHIFT; |
186 | 1d14ffa9 | bellard | wbytes = AUD_write ( |
187 | 1d14ffa9 | bellard | s->voice, |
188 | 1d14ffa9 | bellard | s->mixbuf + (pos << (SHIFT - 1)),
|
189 | 1d14ffa9 | bellard | nbytes |
190 | 1d14ffa9 | bellard | ); |
191 | 1d14ffa9 | bellard | |
192 | 1d14ffa9 | bellard | if (wbytes) {
|
193 | 1d14ffa9 | bellard | wsampl = wbytes >> SHIFT; |
194 | 1d14ffa9 | bellard | |
195 | 1d14ffa9 | bellard | samples -= wsampl; |
196 | 1d14ffa9 | bellard | pos = (pos + wsampl) % s->samples; |
197 | 1d14ffa9 | bellard | |
198 | 1d14ffa9 | bellard | net += wsampl; |
199 | 1d14ffa9 | bellard | } |
200 | 1d14ffa9 | bellard | else {
|
201 | 85571bc7 | bellard | break;
|
202 | 1d14ffa9 | bellard | } |
203 | 85571bc7 | bellard | } |
204 | 1d14ffa9 | bellard | |
205 | 85571bc7 | bellard | return net;
|
206 | 85571bc7 | bellard | } |
207 | 85571bc7 | bellard | |
208 | 1d14ffa9 | bellard | static void adlib_callback (void *opaque, int free) |
209 | 85571bc7 | bellard | { |
210 | 85571bc7 | bellard | AdlibState *s = opaque; |
211 | 1d14ffa9 | bellard | int samples, net = 0, to_play, written; |
212 | 85571bc7 | bellard | |
213 | 1d14ffa9 | bellard | samples = free >> SHIFT; |
214 | 1d14ffa9 | bellard | if (!(s->active && s->enabled) || !samples) {
|
215 | 1d14ffa9 | bellard | return;
|
216 | 85571bc7 | bellard | } |
217 | 85571bc7 | bellard | |
218 | 1d14ffa9 | bellard | to_play = audio_MIN (s->left, samples); |
219 | 1d14ffa9 | bellard | while (to_play) {
|
220 | 1d14ffa9 | bellard | written = write_audio (s, to_play); |
221 | 1d14ffa9 | bellard | |
222 | 1d14ffa9 | bellard | if (written) {
|
223 | 1d14ffa9 | bellard | s->left -= written; |
224 | 1d14ffa9 | bellard | samples -= written; |
225 | 1d14ffa9 | bellard | to_play -= written; |
226 | 1d14ffa9 | bellard | s->pos = (s->pos + written) % s->samples; |
227 | 1d14ffa9 | bellard | } |
228 | 1d14ffa9 | bellard | else {
|
229 | 1d14ffa9 | bellard | return;
|
230 | 1d14ffa9 | bellard | } |
231 | 1d14ffa9 | bellard | } |
232 | 85571bc7 | bellard | |
233 | 85571bc7 | bellard | samples = audio_MIN (samples, s->samples - s->pos); |
234 | 1d14ffa9 | bellard | if (!samples) {
|
235 | 1d14ffa9 | bellard | return;
|
236 | 1d14ffa9 | bellard | } |
237 | 85571bc7 | bellard | |
238 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
239 | 85571bc7 | bellard | YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples); |
240 | 85571bc7 | bellard | #else
|
241 | 85571bc7 | bellard | YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples); |
242 | 85571bc7 | bellard | #endif
|
243 | 85571bc7 | bellard | |
244 | 85571bc7 | bellard | while (samples) {
|
245 | 1d14ffa9 | bellard | written = write_audio (s, samples); |
246 | 1d14ffa9 | bellard | |
247 | 1d14ffa9 | bellard | if (written) {
|
248 | 1d14ffa9 | bellard | net += written; |
249 | 1d14ffa9 | bellard | samples -= written; |
250 | 1d14ffa9 | bellard | s->pos = (s->pos + written) % s->samples; |
251 | 1d14ffa9 | bellard | } |
252 | 1d14ffa9 | bellard | else {
|
253 | 1d14ffa9 | bellard | s->left = samples; |
254 | 1d14ffa9 | bellard | return;
|
255 | 1d14ffa9 | bellard | } |
256 | 85571bc7 | bellard | } |
257 | 85571bc7 | bellard | } |
258 | 85571bc7 | bellard | |
259 | 85571bc7 | bellard | static void Adlib_fini (AdlibState *s) |
260 | 85571bc7 | bellard | { |
261 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
262 | 85571bc7 | bellard | YMF262Shutdown (); |
263 | 85571bc7 | bellard | #else
|
264 | 85571bc7 | bellard | if (s->opl) {
|
265 | 85571bc7 | bellard | OPLDestroy (s->opl); |
266 | 85571bc7 | bellard | s->opl = NULL;
|
267 | 85571bc7 | bellard | } |
268 | 85571bc7 | bellard | #endif
|
269 | 85571bc7 | bellard | |
270 | 1d14ffa9 | bellard | if (s->mixbuf) {
|
271 | 1d14ffa9 | bellard | qemu_free (s->mixbuf); |
272 | 1d14ffa9 | bellard | } |
273 | 85571bc7 | bellard | |
274 | 85571bc7 | bellard | s->active = 0;
|
275 | 85571bc7 | bellard | s->enabled = 0;
|
276 | c0fe3827 | bellard | AUD_remove_card (&s->card); |
277 | 85571bc7 | bellard | } |
278 | 85571bc7 | bellard | |
279 | 22d83b14 | Paul Brook | int Adlib_init (qemu_irq *pic)
|
280 | 85571bc7 | bellard | { |
281 | c0fe3827 | bellard | AdlibState *s = &glob_adlib; |
282 | 1ea879e5 | malc | struct audsettings as;
|
283 | c0fe3827 | bellard | |
284 | 1d14ffa9 | bellard | #ifdef HAS_YMF262
|
285 | 85571bc7 | bellard | if (YMF262Init (1, 14318180, conf.freq)) { |
286 | 85571bc7 | bellard | dolog ("YMF262Init %d failed\n", conf.freq);
|
287 | c0fe3827 | bellard | return -1; |
288 | 85571bc7 | bellard | } |
289 | 85571bc7 | bellard | else {
|
290 | 1d14ffa9 | bellard | YMF262SetTimerHandler (0, timer_handler, 0); |
291 | 85571bc7 | bellard | s->enabled = 1;
|
292 | 85571bc7 | bellard | } |
293 | 85571bc7 | bellard | #else
|
294 | 85571bc7 | bellard | s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
|
295 | 85571bc7 | bellard | if (!s->opl) {
|
296 | 85571bc7 | bellard | dolog ("OPLCreate %d failed\n", conf.freq);
|
297 | c0fe3827 | bellard | return -1; |
298 | 85571bc7 | bellard | } |
299 | 85571bc7 | bellard | else {
|
300 | 1d14ffa9 | bellard | OPLSetTimerHandler (s->opl, timer_handler, 0);
|
301 | 85571bc7 | bellard | s->enabled = 1;
|
302 | 85571bc7 | bellard | } |
303 | 85571bc7 | bellard | #endif
|
304 | 85571bc7 | bellard | |
305 | c0fe3827 | bellard | as.freq = conf.freq; |
306 | c0fe3827 | bellard | as.nchannels = SHIFT; |
307 | c0fe3827 | bellard | as.fmt = AUD_FMT_S16; |
308 | d929eba5 | bellard | as.endianness = AUDIO_HOST_ENDIANNESS; |
309 | c0fe3827 | bellard | |
310 | 1a7dafce | malc | AUD_register_card ("adlib", &s->card);
|
311 | c0fe3827 | bellard | |
312 | 1d14ffa9 | bellard | s->voice = AUD_open_out ( |
313 | c0fe3827 | bellard | &s->card, |
314 | 1d14ffa9 | bellard | s->voice, |
315 | 1d14ffa9 | bellard | "adlib",
|
316 | 1d14ffa9 | bellard | s, |
317 | 1d14ffa9 | bellard | adlib_callback, |
318 | d929eba5 | bellard | &as |
319 | 1d14ffa9 | bellard | ); |
320 | 85571bc7 | bellard | if (!s->voice) {
|
321 | 85571bc7 | bellard | Adlib_fini (s); |
322 | c0fe3827 | bellard | return -1; |
323 | 85571bc7 | bellard | } |
324 | 85571bc7 | bellard | |
325 | 1d14ffa9 | bellard | s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT; |
326 | 85571bc7 | bellard | s->mixbuf = qemu_mallocz (s->samples << SHIFT); |
327 | 85571bc7 | bellard | |
328 | 85571bc7 | bellard | register_ioport_read (0x388, 4, 1, adlib_read, s); |
329 | 85571bc7 | bellard | register_ioport_write (0x388, 4, 1, adlib_write, s); |
330 | 85571bc7 | bellard | |
331 | 85571bc7 | bellard | register_ioport_read (conf.port, 4, 1, adlib_read, s); |
332 | 85571bc7 | bellard | register_ioport_write (conf.port, 4, 1, adlib_write, s); |
333 | 85571bc7 | bellard | |
334 | 85571bc7 | bellard | register_ioport_read (conf.port + 8, 2, 1, adlib_read, s); |
335 | 85571bc7 | bellard | register_ioport_write (conf.port + 8, 2, 1, adlib_write, s); |
336 | c0fe3827 | bellard | |
337 | c0fe3827 | bellard | return 0; |
338 | 85571bc7 | bellard | } |