]>
Commit | Line | Data |
---|---|---|
85571bc7 | 1 | /* |
1d14ffa9 FB |
2 | * QEMU Proxy for OPL2/3 emulation by MAME team |
3 | * | |
4 | * Copyright (c) 2004-2005 Vassili Karpov (malc) | |
5 | * | |
85571bc7 FB |
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 | */ | |
1d14ffa9 | 24 | #include <assert.h> |
85571bc7 FB |
25 | #include "vl.h" |
26 | ||
1d14ffa9 FB |
27 | #define ADLIB_KILL_TIMERS 1 |
28 | ||
fb065187 FB |
29 | #define dolog(...) AUD_log ("adlib", __VA_ARGS__) |
30 | #ifdef DEBUG | |
31 | #define ldebug(...) dolog (__VA_ARGS__) | |
32 | #else | |
33 | #define ldebug(...) | |
34 | #endif | |
85571bc7 | 35 | |
1d14ffa9 | 36 | #ifdef HAS_YMF262 |
85571bc7 | 37 | #include "ymf262.h" |
1d14ffa9 | 38 | void YMF262UpdateOneQEMU (int which, INT16 *dst, int length); |
85571bc7 FB |
39 | #define SHIFT 2 |
40 | #else | |
41 | #include "fmopl.h" | |
42 | #define SHIFT 1 | |
43 | #endif | |
44 | ||
85571bc7 FB |
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 { | |
1d14ffa9 | 56 | int ticking[2]; |
85571bc7 FB |
57 | int enabled; |
58 | int active; | |
85571bc7 | 59 | int bufpos; |
1d14ffa9 FB |
60 | #ifdef DEBUG |
61 | int64_t exp[2]; | |
62 | #endif | |
85571bc7 | 63 | int16_t *mixbuf; |
1d14ffa9 FB |
64 | uint64_t dexp[2]; |
65 | SWVoiceOut *voice; | |
66 | int left, pos, samples; | |
67 | QEMUAudioTimeStamp ats; | |
68 | #ifndef HAS_YMF262 | |
85571bc7 FB |
69 | FM_OPL *opl; |
70 | #endif | |
71 | } AdlibState; | |
72 | ||
73 | static AdlibState adlib; | |
74 | ||
1d14ffa9 FB |
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 | ||
85571bc7 FB |
108 | static IO_WRITE_PROTO(adlib_write) |
109 | { | |
110 | AdlibState *s = opaque; | |
111 | int a = nport & 3; | |
112 | int status; | |
113 | ||
85571bc7 | 114 | s->active = 1; |
1d14ffa9 | 115 | AUD_set_active_out (s->voice, 1); |
85571bc7 | 116 | |
1d14ffa9 FB |
117 | adlib_kill_timers (s); |
118 | ||
119 | #ifdef HAS_YMF262 | |
85571bc7 FB |
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 | ||
1d14ffa9 FB |
132 | adlib_kill_timers (s); |
133 | ||
134 | #ifdef HAS_YMF262 | |
85571bc7 FB |
135 | data = YMF262Read (0, a); |
136 | #else | |
137 | data = OPLRead (s->opl, a); | |
138 | #endif | |
139 | return data; | |
140 | } | |
141 | ||
1d14ffa9 | 142 | static void timer_handler (int c, double interval_Sec) |
85571bc7 | 143 | { |
1d14ffa9 FB |
144 | AdlibState *s = &adlib; |
145 | unsigned n = c & 1; | |
146 | #ifdef DEBUG | |
147 | double interval; | |
85571bc7 | 148 | #endif |
85571bc7 | 149 | |
85571bc7 | 150 | if (interval_Sec == 0.0) { |
1d14ffa9 | 151 | s->ticking[n] = 0; |
85571bc7 FB |
152 | return; |
153 | } | |
1d14ffa9 FB |
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); | |
85571bc7 FB |
164 | } |
165 | ||
166 | static int write_audio (AdlibState *s, int samples) | |
167 | { | |
168 | int net = 0; | |
1d14ffa9 FB |
169 | int pos = s->pos; |
170 | ||
85571bc7 | 171 | while (samples) { |
1d14ffa9 FB |
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 { | |
85571bc7 | 190 | break; |
1d14ffa9 | 191 | } |
85571bc7 | 192 | } |
1d14ffa9 | 193 | |
85571bc7 FB |
194 | return net; |
195 | } | |
196 | ||
1d14ffa9 | 197 | static void adlib_callback (void *opaque, int free) |
85571bc7 FB |
198 | { |
199 | AdlibState *s = opaque; | |
1d14ffa9 | 200 | int samples, net = 0, to_play, written; |
85571bc7 | 201 | |
1d14ffa9 FB |
202 | samples = free >> SHIFT; |
203 | if (!(s->active && s->enabled) || !samples) { | |
204 | return; | |
85571bc7 | 205 | } |
85571bc7 | 206 | |
1d14ffa9 FB |
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 | } | |
85571bc7 FB |
221 | |
222 | samples = audio_MIN (samples, s->samples - s->pos); | |
1d14ffa9 FB |
223 | if (!samples) { |
224 | return; | |
225 | } | |
85571bc7 | 226 | |
1d14ffa9 | 227 | #ifdef HAS_YMF262 |
85571bc7 FB |
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) { | |
1d14ffa9 FB |
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 | } | |
85571bc7 | 245 | } |
85571bc7 FB |
246 | } |
247 | ||
248 | static void Adlib_fini (AdlibState *s) | |
249 | { | |
1d14ffa9 | 250 | #ifdef HAS_YMF262 |
85571bc7 FB |
251 | YMF262Shutdown (); |
252 | #else | |
253 | if (s->opl) { | |
254 | OPLDestroy (s->opl); | |
255 | s->opl = NULL; | |
256 | } | |
257 | #endif | |
258 | ||
1d14ffa9 FB |
259 | if (s->mixbuf) { |
260 | qemu_free (s->mixbuf); | |
261 | } | |
85571bc7 FB |
262 | |
263 | s->active = 0; | |
264 | s->enabled = 0; | |
265 | } | |
266 | ||
267 | void Adlib_init (void) | |
268 | { | |
269 | AdlibState *s = &adlib; | |
270 | ||
1d14ffa9 | 271 | #ifdef HAS_YMF262 |
85571bc7 FB |
272 | if (YMF262Init (1, 14318180, conf.freq)) { |
273 | dolog ("YMF262Init %d failed\n", conf.freq); | |
274 | return; | |
275 | } | |
276 | else { | |
1d14ffa9 | 277 | YMF262SetTimerHandler (0, timer_handler, 0); |
85571bc7 FB |
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 { | |
1d14ffa9 | 287 | OPLSetTimerHandler (s->opl, timer_handler, 0); |
85571bc7 FB |
288 | s->enabled = 1; |
289 | } | |
290 | #endif | |
291 | ||
1d14ffa9 FB |
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 | ); | |
85571bc7 FB |
301 | if (!s->voice) { |
302 | Adlib_fini (s); | |
303 | return; | |
304 | } | |
305 | ||
1d14ffa9 | 306 | s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT; |
85571bc7 FB |
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 | } | |
1d14ffa9 | 315 | |
85571bc7 FB |
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); | |
85571bc7 | 324 | } |