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