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