]> git.ipfire.org Git - thirdparty/qemu.git/blame - hw/adlib.c
hw: move headers to include/
[thirdparty/qemu.git] / hw / adlib.c
CommitLineData
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 */
9f0683d9 24
83c9f4ca 25#include "hw/hw.h"
0d09e41a 26#include "hw/audio/audio.h"
e140e05c 27#include "audio/audio.h"
0d09e41a 28#include "hw/isa/isa.h"
85571bc7 29
9f0683d9
TS
30//#define DEBUG
31
1d14ffa9
FB
32#define ADLIB_KILL_TIMERS 1
33
9f0683d9 34#ifdef DEBUG
1de7afc9 35#include "qemu/timer.h"
9f0683d9
TS
36#endif
37
fb065187
FB
38#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
39#ifdef DEBUG
40#define ldebug(...) dolog (__VA_ARGS__)
41#else
42#define ldebug(...)
43#endif
85571bc7 44
1d14ffa9 45#ifdef HAS_YMF262
85571bc7 46#include "ymf262.h"
1d14ffa9 47void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
85571bc7
FB
48#define SHIFT 2
49#else
83c9f4ca 50#include "hw/fmopl.h"
85571bc7
FB
51#define SHIFT 1
52#endif
53
85571bc7
FB
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
59static struct {
60 int port;
61 int freq;
62} conf = {0x220, 44100};
63
64typedef struct {
c0fe3827 65 QEMUSoundCard card;
1d14ffa9 66 int ticking[2];
85571bc7
FB
67 int enabled;
68 int active;
85571bc7 69 int bufpos;
1d14ffa9
FB
70#ifdef DEBUG
71 int64_t exp[2];
72#endif
85571bc7 73 int16_t *mixbuf;
1d14ffa9
FB
74 uint64_t dexp[2];
75 SWVoiceOut *voice;
76 int left, pos, samples;
77 QEMUAudioTimeStamp ats;
78#ifndef HAS_YMF262
85571bc7
FB
79 FM_OPL *opl;
80#endif
81} AdlibState;
82
c0fe3827 83static AdlibState glob_adlib;
85571bc7 84
1d14ffa9
FB
85static 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
c0fe3827 103 delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
1d14ffa9
FB
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
d999f7e0 118static IO_WRITE_PROTO (adlib_write)
85571bc7
FB
119{
120 AdlibState *s = opaque;
121 int a = nport & 3;
85571bc7 122
85571bc7 123 s->active = 1;
1d14ffa9 124 AUD_set_active_out (s->voice, 1);
85571bc7 125
1d14ffa9
FB
126 adlib_kill_timers (s);
127
128#ifdef HAS_YMF262
e8beeae4 129 YMF262Write (0, a, val);
85571bc7 130#else
e8beeae4 131 OPLWrite (s->opl, a, val);
85571bc7
FB
132#endif
133}
134
d999f7e0 135static IO_READ_PROTO (adlib_read)
85571bc7
FB
136{
137 AdlibState *s = opaque;
138 uint8_t data;
139 int a = nport & 3;
140
1d14ffa9
FB
141 adlib_kill_timers (s);
142
143#ifdef HAS_YMF262
85571bc7
FB
144 data = YMF262Read (0, a);
145#else
146 data = OPLRead (s->opl, a);
147#endif
148 return data;
149}
150
1d14ffa9 151static void timer_handler (int c, double interval_Sec)
85571bc7 152{
c0fe3827 153 AdlibState *s = &glob_adlib;
1d14ffa9
FB
154 unsigned n = c & 1;
155#ifdef DEBUG
156 double interval;
c0fe3827 157 int64_t exp;
85571bc7 158#endif
85571bc7 159
85571bc7 160 if (interval_Sec == 0.0) {
1d14ffa9 161 s->ticking[n] = 0;
85571bc7
FB
162 return;
163 }
1d14ffa9
FB
164
165 s->ticking[n] = 1;
166#ifdef DEBUG
cf4dc461 167 interval = get_ticks_per_sec () * interval_Sec;
74475455 168 exp = qemu_get_clock_ns (vm_clock) + interval;
1d14ffa9
FB
169 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);
85571bc7
FB
174}
175
176static int write_audio (AdlibState *s, int samples)
177{
178 int net = 0;
1d14ffa9
FB
179 int pos = s->pos;
180
85571bc7 181 while (samples) {
1d14ffa9
FB
182 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 {
85571bc7 200 break;
1d14ffa9 201 }
85571bc7 202 }
1d14ffa9 203
85571bc7
FB
204 return net;
205}
206
1d14ffa9 207static void adlib_callback (void *opaque, int free)
85571bc7
FB
208{
209 AdlibState *s = opaque;
1d14ffa9 210 int samples, net = 0, to_play, written;
85571bc7 211
1d14ffa9
FB
212 samples = free >> SHIFT;
213 if (!(s->active && s->enabled) || !samples) {
214 return;
85571bc7 215 }
85571bc7 216
1d14ffa9
FB
217 to_play = audio_MIN (s->left, samples);
218 while (to_play) {
219 written = write_audio (s, to_play);
220
221 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 }
85571bc7
FB
231
232 samples = audio_MIN (samples, s->samples - s->pos);
1d14ffa9
FB
233 if (!samples) {
234 return;
235 }
85571bc7 236
1d14ffa9 237#ifdef HAS_YMF262
85571bc7
FB
238 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) {
1d14ffa9
FB
244 written = write_audio (s, samples);
245
246 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 }
85571bc7 255 }
85571bc7
FB
256}
257
258static void Adlib_fini (AdlibState *s)
259{
1d14ffa9 260#ifdef HAS_YMF262
85571bc7
FB
261 YMF262Shutdown ();
262#else
263 if (s->opl) {
264 OPLDestroy (s->opl);
265 s->opl = NULL;
266 }
267#endif
268
1d14ffa9 269 if (s->mixbuf) {
7267c094 270 g_free (s->mixbuf);
1d14ffa9 271 }
85571bc7
FB
272
273 s->active = 0;
274 s->enabled = 0;
c0fe3827 275 AUD_remove_card (&s->card);
85571bc7
FB
276}
277
4a0f031d 278int Adlib_init (ISABus *bus)
85571bc7 279{
c0fe3827 280 AdlibState *s = &glob_adlib;
1ea879e5 281 struct audsettings as;
c0fe3827 282
1d14ffa9 283#ifdef HAS_YMF262
85571bc7
FB
284 if (YMF262Init (1, 14318180, conf.freq)) {
285 dolog ("YMF262Init %d failed\n", conf.freq);
c0fe3827 286 return -1;
85571bc7
FB
287 }
288 else {
1d14ffa9 289 YMF262SetTimerHandler (0, timer_handler, 0);
85571bc7
FB
290 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);
c0fe3827 296 return -1;
85571bc7
FB
297 }
298 else {
1d14ffa9 299 OPLSetTimerHandler (s->opl, timer_handler, 0);
85571bc7
FB
300 s->enabled = 1;
301 }
302#endif
303
c0fe3827
FB
304 as.freq = conf.freq;
305 as.nchannels = SHIFT;
306 as.fmt = AUD_FMT_S16;
d929eba5 307 as.endianness = AUDIO_HOST_ENDIANNESS;
c0fe3827 308
1a7dafce 309 AUD_register_card ("adlib", &s->card);
c0fe3827 310
1d14ffa9 311 s->voice = AUD_open_out (
c0fe3827 312 &s->card,
1d14ffa9
FB
313 s->voice,
314 "adlib",
315 s,
316 adlib_callback,
d929eba5 317 &as
1d14ffa9 318 );
85571bc7
FB
319 if (!s->voice) {
320 Adlib_fini (s);
c0fe3827 321 return -1;
85571bc7
FB
322 }
323
1d14ffa9 324 s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
7267c094 325 s->mixbuf = g_malloc0 (s->samples << SHIFT);
85571bc7 326
85571bc7
FB
327 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);
c0fe3827
FB
335
336 return 0;
85571bc7 337}