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