blob: d39cd97384d26e1ca752ab3dc65d739f3b3b9b48 [file] [log] [blame]
bellard85571bc2004-11-07 18:04:02 +00001/*
bellard1d14ffa2005-10-30 18:58:22 +00002 * QEMU Proxy for OPL2/3 emulation by MAME team
3 *
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 *
bellard85571bc2004-11-07 18:04:02 +00006 * 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 */
ths9f0683d2007-12-02 17:47:33 +000024
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw.h"
26#include "audiodev.h"
thse140e052007-12-02 17:17:45 +000027#include "audio/audio.h"
ths69b34972007-12-17 03:15:52 +000028#include "isa.h"
bellard85571bc2004-11-07 18:04:02 +000029
ths9f0683d2007-12-02 17:47:33 +000030//#define DEBUG
31
bellard1d14ffa2005-10-30 18:58:22 +000032#define ADLIB_KILL_TIMERS 1
33
ths9f0683d2007-12-02 17:47:33 +000034#ifdef DEBUG
35#include "qemu-timer.h"
36#endif
37
bellardfb065182004-11-09 23:09:44 +000038#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
39#ifdef DEBUG
40#define ldebug(...) dolog (__VA_ARGS__)
41#else
42#define ldebug(...)
43#endif
bellard85571bc2004-11-07 18:04:02 +000044
bellard1d14ffa2005-10-30 18:58:22 +000045#ifdef HAS_YMF262
bellard85571bc2004-11-07 18:04:02 +000046#include "ymf262.h"
bellard1d14ffa2005-10-30 18:58:22 +000047void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
bellard85571bc2004-11-07 18:04:02 +000048#define SHIFT 2
49#else
50#include "fmopl.h"
51#define SHIFT 1
52#endif
53
bellard85571bc2004-11-07 18:04:02 +000054#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
59static struct {
60 int port;
61 int freq;
62} conf = {0x220, 44100};
63
64typedef struct {
bellardc0fe3822005-11-05 18:55:28 +000065 QEMUSoundCard card;
bellard1d14ffa2005-10-30 18:58:22 +000066 int ticking[2];
bellard85571bc2004-11-07 18:04:02 +000067 int enabled;
68 int active;
bellard85571bc2004-11-07 18:04:02 +000069 int bufpos;
bellard1d14ffa2005-10-30 18:58:22 +000070#ifdef DEBUG
71 int64_t exp[2];
72#endif
bellard85571bc2004-11-07 18:04:02 +000073 int16_t *mixbuf;
bellard1d14ffa2005-10-30 18:58:22 +000074 uint64_t dexp[2];
75 SWVoiceOut *voice;
76 int left, pos, samples;
77 QEMUAudioTimeStamp ats;
78#ifndef HAS_YMF262
bellard85571bc2004-11-07 18:04:02 +000079 FM_OPL *opl;
80#endif
81} AdlibState;
82
bellardc0fe3822005-11-05 18:55:28 +000083static AdlibState glob_adlib;
bellard85571bc2004-11-07 18:04:02 +000084
bellard1d14ffa2005-10-30 18:58:22 +000085static 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
95static 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
bellardc0fe3822005-11-05 18:55:28 +0000103 delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
bellard1d14ffa2005-10-30 18:58:22 +0000104 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
malcd999f7e2009-06-20 05:13:29 +0400118static IO_WRITE_PROTO (adlib_write)
bellard85571bc2004-11-07 18:04:02 +0000119{
120 AdlibState *s = opaque;
121 int a = nport & 3;
bellard85571bc2004-11-07 18:04:02 +0000122
bellard85571bc2004-11-07 18:04:02 +0000123 s->active = 1;
bellard1d14ffa2005-10-30 18:58:22 +0000124 AUD_set_active_out (s->voice, 1);
bellard85571bc2004-11-07 18:04:02 +0000125
bellard1d14ffa2005-10-30 18:58:22 +0000126 adlib_kill_timers (s);
127
128#ifdef HAS_YMF262
Hervé Poussineaue8beeae2011-09-18 16:27:23 +0200129 YMF262Write (0, a, val);
bellard85571bc2004-11-07 18:04:02 +0000130#else
Hervé Poussineaue8beeae2011-09-18 16:27:23 +0200131 OPLWrite (s->opl, a, val);
bellard85571bc2004-11-07 18:04:02 +0000132#endif
133}
134
malcd999f7e2009-06-20 05:13:29 +0400135static IO_READ_PROTO (adlib_read)
bellard85571bc2004-11-07 18:04:02 +0000136{
137 AdlibState *s = opaque;
138 uint8_t data;
139 int a = nport & 3;
140
bellard1d14ffa2005-10-30 18:58:22 +0000141 adlib_kill_timers (s);
142
143#ifdef HAS_YMF262
bellard85571bc2004-11-07 18:04:02 +0000144 data = YMF262Read (0, a);
145#else
146 data = OPLRead (s->opl, a);
147#endif
148 return data;
149}
150
bellard1d14ffa2005-10-30 18:58:22 +0000151static void timer_handler (int c, double interval_Sec)
bellard85571bc2004-11-07 18:04:02 +0000152{
bellardc0fe3822005-11-05 18:55:28 +0000153 AdlibState *s = &glob_adlib;
bellard1d14ffa2005-10-30 18:58:22 +0000154 unsigned n = c & 1;
155#ifdef DEBUG
156 double interval;
bellardc0fe3822005-11-05 18:55:28 +0000157 int64_t exp;
bellard1d14ffa2005-10-30 18:58:22 +0000158#endif
159
bellard85571bc2004-11-07 18:04:02 +0000160 if (interval_Sec == 0.0) {
bellard1d14ffa2005-10-30 18:58:22 +0000161 s->ticking[n] = 0;
bellard85571bc2004-11-07 18:04:02 +0000162 return;
163 }
bellard1d14ffa2005-10-30 18:58:22 +0000164
165 s->ticking[n] = 1;
166#ifdef DEBUG
malccf4dc462012-02-07 22:11:04 +0400167 interval = get_ticks_per_sec () * interval_Sec;
Paolo Bonzini74475452011-03-11 16:47:48 +0100168 exp = qemu_get_clock_ns (vm_clock) + interval;
bellard1d14ffa2005-10-30 18:58:22 +0000169 s->exp[n] = exp;
170#endif
171
172 s->dexp[n] = interval_Sec * 1000000.0;
173 AUD_init_time_stamp_out (s->voice, &s->ats);
bellard85571bc2004-11-07 18:04:02 +0000174}
175
176static int write_audio (AdlibState *s, int samples)
177{
178 int net = 0;
bellard1d14ffa2005-10-30 18:58:22 +0000179 int pos = s->pos;
180
bellard85571bc2004-11-07 18:04:02 +0000181 while (samples) {
bellard1d14ffa2005-10-30 18:58:22 +0000182 int nbytes, wbytes, wsampl;
183
184 nbytes = samples << SHIFT;
185 wbytes = AUD_write (
186 s->voice,
187 s->mixbuf + (pos << (SHIFT - 1)),
188 nbytes
189 );
190
191 if (wbytes) {
192 wsampl = wbytes >> SHIFT;
193
194 samples -= wsampl;
195 pos = (pos + wsampl) % s->samples;
196
197 net += wsampl;
198 }
199 else {
bellard85571bc2004-11-07 18:04:02 +0000200 break;
bellard1d14ffa2005-10-30 18:58:22 +0000201 }
bellard85571bc2004-11-07 18:04:02 +0000202 }
bellard1d14ffa2005-10-30 18:58:22 +0000203
bellard85571bc2004-11-07 18:04:02 +0000204 return net;
205}
206
bellard1d14ffa2005-10-30 18:58:22 +0000207static void adlib_callback (void *opaque, int free)
bellard85571bc2004-11-07 18:04:02 +0000208{
209 AdlibState *s = opaque;
bellard1d14ffa2005-10-30 18:58:22 +0000210 int samples, net = 0, to_play, written;
bellard85571bc2004-11-07 18:04:02 +0000211
bellard1d14ffa2005-10-30 18:58:22 +0000212 samples = free >> SHIFT;
213 if (!(s->active && s->enabled) || !samples) {
214 return;
bellard85571bc2004-11-07 18:04:02 +0000215 }
bellard85571bc2004-11-07 18:04:02 +0000216
bellard1d14ffa2005-10-30 18:58:22 +0000217 to_play = audio_MIN (s->left, samples);
218 while (to_play) {
219 written = write_audio (s, to_play);
bellard85571bc2004-11-07 18:04:02 +0000220
bellard1d14ffa2005-10-30 18:58:22 +0000221 if (written) {
222 s->left -= written;
223 samples -= written;
224 to_play -= written;
225 s->pos = (s->pos + written) % s->samples;
226 }
227 else {
228 return;
229 }
230 }
bellard85571bc2004-11-07 18:04:02 +0000231
232 samples = audio_MIN (samples, s->samples - s->pos);
bellard1d14ffa2005-10-30 18:58:22 +0000233 if (!samples) {
234 return;
235 }
bellard85571bc2004-11-07 18:04:02 +0000236
bellard1d14ffa2005-10-30 18:58:22 +0000237#ifdef HAS_YMF262
bellard85571bc2004-11-07 18:04:02 +0000238 YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
239#else
240 YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
241#endif
242
243 while (samples) {
bellard1d14ffa2005-10-30 18:58:22 +0000244 written = write_audio (s, samples);
bellard85571bc2004-11-07 18:04:02 +0000245
bellard1d14ffa2005-10-30 18:58:22 +0000246 if (written) {
247 net += written;
248 samples -= written;
249 s->pos = (s->pos + written) % s->samples;
250 }
251 else {
252 s->left = samples;
253 return;
254 }
255 }
bellard85571bc2004-11-07 18:04:02 +0000256}
257
258static void Adlib_fini (AdlibState *s)
259{
bellard1d14ffa2005-10-30 18:58:22 +0000260#ifdef HAS_YMF262
bellard85571bc2004-11-07 18:04:02 +0000261 YMF262Shutdown ();
262#else
263 if (s->opl) {
264 OPLDestroy (s->opl);
265 s->opl = NULL;
266 }
267#endif
268
bellard1d14ffa2005-10-30 18:58:22 +0000269 if (s->mixbuf) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500270 g_free (s->mixbuf);
bellard1d14ffa2005-10-30 18:58:22 +0000271 }
bellard85571bc2004-11-07 18:04:02 +0000272
273 s->active = 0;
274 s->enabled = 0;
bellardc0fe3822005-11-05 18:55:28 +0000275 AUD_remove_card (&s->card);
bellard85571bc2004-11-07 18:04:02 +0000276}
277
Hervé Poussineau4a0f0312011-12-15 22:10:01 +0100278int Adlib_init (ISABus *bus)
bellard85571bc2004-11-07 18:04:02 +0000279{
bellardc0fe3822005-11-05 18:55:28 +0000280 AdlibState *s = &glob_adlib;
malc1ea879e2008-12-03 22:48:44 +0000281 struct audsettings as;
bellardc0fe3822005-11-05 18:55:28 +0000282
bellard1d14ffa2005-10-30 18:58:22 +0000283#ifdef HAS_YMF262
bellard85571bc2004-11-07 18:04:02 +0000284 if (YMF262Init (1, 14318180, conf.freq)) {
285 dolog ("YMF262Init %d failed\n", conf.freq);
bellardc0fe3822005-11-05 18:55:28 +0000286 return -1;
bellard85571bc2004-11-07 18:04:02 +0000287 }
288 else {
bellard1d14ffa2005-10-30 18:58:22 +0000289 YMF262SetTimerHandler (0, timer_handler, 0);
bellard85571bc2004-11-07 18:04:02 +0000290 s->enabled = 1;
291 }
292#else
293 s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
294 if (!s->opl) {
295 dolog ("OPLCreate %d failed\n", conf.freq);
bellardc0fe3822005-11-05 18:55:28 +0000296 return -1;
bellard85571bc2004-11-07 18:04:02 +0000297 }
298 else {
bellard1d14ffa2005-10-30 18:58:22 +0000299 OPLSetTimerHandler (s->opl, timer_handler, 0);
bellard85571bc2004-11-07 18:04:02 +0000300 s->enabled = 1;
301 }
302#endif
303
bellardc0fe3822005-11-05 18:55:28 +0000304 as.freq = conf.freq;
305 as.nchannels = SHIFT;
306 as.fmt = AUD_FMT_S16;
bellardd929eba2006-07-04 21:47:22 +0000307 as.endianness = AUDIO_HOST_ENDIANNESS;
bellardc0fe3822005-11-05 18:55:28 +0000308
malc1a7dafc2009-05-14 03:11:35 +0400309 AUD_register_card ("adlib", &s->card);
bellardc0fe3822005-11-05 18:55:28 +0000310
bellard1d14ffa2005-10-30 18:58:22 +0000311 s->voice = AUD_open_out (
bellardc0fe3822005-11-05 18:55:28 +0000312 &s->card,
bellard1d14ffa2005-10-30 18:58:22 +0000313 s->voice,
314 "adlib",
315 s,
316 adlib_callback,
bellardd929eba2006-07-04 21:47:22 +0000317 &as
bellard1d14ffa2005-10-30 18:58:22 +0000318 );
bellard85571bc2004-11-07 18:04:02 +0000319 if (!s->voice) {
320 Adlib_fini (s);
bellardc0fe3822005-11-05 18:55:28 +0000321 return -1;
bellard85571bc2004-11-07 18:04:02 +0000322 }
323
bellard1d14ffa2005-10-30 18:58:22 +0000324 s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
Anthony Liguori7267c092011-08-20 22:09:37 -0500325 s->mixbuf = g_malloc0 (s->samples << SHIFT);
bellard85571bc2004-11-07 18:04:02 +0000326
bellard85571bc2004-11-07 18:04:02 +0000327 register_ioport_read (0x388, 4, 1, adlib_read, s);
328 register_ioport_write (0x388, 4, 1, adlib_write, s);
329
330 register_ioport_read (conf.port, 4, 1, adlib_read, s);
331 register_ioport_write (conf.port, 4, 1, adlib_write, s);
332
333 register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
334 register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
bellardc0fe3822005-11-05 18:55:28 +0000335
336 return 0;
bellard85571bc2004-11-07 18:04:02 +0000337}