blob: 97b876c7e08cfe4c7379aa5e630033a0bfa3d4a9 [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
Peter Maydell6086a562016-01-18 17:33:52 +000025#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010026#include "qapi/error.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010027#include "hw/hw.h"
Eduardo Habkost8a824e42017-05-08 17:57:35 -030028#include "hw/audio/soundhw.h"
thse140e052007-12-02 17:17:45 +000029#include "audio/audio.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "hw/isa/isa.h"
bellard85571bc2004-11-07 18:04:02 +000031
ths9f0683d2007-12-02 17:47:33 +000032//#define DEBUG
33
bellard1d14ffa2005-10-30 18:58:22 +000034#define ADLIB_KILL_TIMERS 1
35
Paolo Bonzini8c444a12013-04-18 18:43:57 +020036#define ADLIB_DESC "Yamaha YM3812 (OPL2)"
Paolo Bonzini8c444a12013-04-18 18:43:57 +020037
ths9f0683d2007-12-02 17:47:33 +000038#ifdef DEBUG
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/timer.h"
ths9f0683d2007-12-02 17:47:33 +000040#endif
41
bellardfb065182004-11-09 23:09:44 +000042#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
43#ifdef DEBUG
44#define ldebug(...) dolog (__VA_ARGS__)
45#else
46#define ldebug(...)
47#endif
bellard85571bc2004-11-07 18:04:02 +000048
Paolo Bonzini47b43a12013-03-18 17:36:02 +010049#include "fmopl.h"
bellard85571bc2004-11-07 18:04:02 +000050#define SHIFT 1
bellard85571bc2004-11-07 18:04:02 +000051
Paolo Bonzini8c444a12013-04-18 18:43:57 +020052#define TYPE_ADLIB "adlib"
53#define ADLIB(obj) OBJECT_CHECK(AdlibState, (obj), TYPE_ADLIB)
bellard85571bc2004-11-07 18:04:02 +000054
55typedef struct {
Paolo Bonzini8c444a12013-04-18 18:43:57 +020056 ISADevice parent_obj;
57
bellardc0fe3822005-11-05 18:55:28 +000058 QEMUSoundCard card;
Paolo Bonzini8c444a12013-04-18 18:43:57 +020059 uint32_t freq;
60 uint32_t port;
bellard1d14ffa2005-10-30 18:58:22 +000061 int ticking[2];
bellard85571bc2004-11-07 18:04:02 +000062 int enabled;
63 int active;
bellard85571bc2004-11-07 18:04:02 +000064 int bufpos;
bellard1d14ffa2005-10-30 18:58:22 +000065#ifdef DEBUG
66 int64_t exp[2];
67#endif
bellard85571bc2004-11-07 18:04:02 +000068 int16_t *mixbuf;
bellard1d14ffa2005-10-30 18:58:22 +000069 uint64_t dexp[2];
70 SWVoiceOut *voice;
71 int left, pos, samples;
72 QEMUAudioTimeStamp ats;
bellard85571bc2004-11-07 18:04:02 +000073 FM_OPL *opl;
Kirill Batuzov848696b2014-04-29 17:38:39 +040074 PortioList port_list;
bellard85571bc2004-11-07 18:04:02 +000075} AdlibState;
76
bellard1d14ffa2005-10-30 18:58:22 +000077static void adlib_stop_opl_timer (AdlibState *s, size_t n)
78{
bellard1d14ffa2005-10-30 18:58:22 +000079 OPLTimerOver (s->opl, n);
bellard1d14ffa2005-10-30 18:58:22 +000080 s->ticking[n] = 0;
81}
82
83static void adlib_kill_timers (AdlibState *s)
84{
85 size_t i;
86
87 for (i = 0; i < 2; ++i) {
88 if (s->ticking[i]) {
89 uint64_t delta;
90
bellardc0fe3822005-11-05 18:55:28 +000091 delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
bellard1d14ffa2005-10-30 18:58:22 +000092 ldebug (
93 "delta = %f dexp = %f expired => %d\n",
94 delta / 1000000.0,
95 s->dexp[i] / 1000000.0,
96 delta >= s->dexp[i]
97 );
98 if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
99 adlib_stop_opl_timer (s, i);
100 AUD_init_time_stamp_out (s->voice, &s->ats);
101 }
102 }
103 }
104}
105
Nutan Shinde8307c292015-10-07 22:02:54 +0530106static void adlib_write(void *opaque, uint32_t nport, uint32_t val)
bellard85571bc2004-11-07 18:04:02 +0000107{
108 AdlibState *s = opaque;
109 int a = nport & 3;
bellard85571bc2004-11-07 18:04:02 +0000110
bellard85571bc2004-11-07 18:04:02 +0000111 s->active = 1;
bellard1d14ffa2005-10-30 18:58:22 +0000112 AUD_set_active_out (s->voice, 1);
bellard85571bc2004-11-07 18:04:02 +0000113
bellard1d14ffa2005-10-30 18:58:22 +0000114 adlib_kill_timers (s);
115
Hervé Poussineaue8beeae2011-09-18 16:27:23 +0200116 OPLWrite (s->opl, a, val);
bellard85571bc2004-11-07 18:04:02 +0000117}
118
Nutan Shinde8307c292015-10-07 22:02:54 +0530119static uint32_t adlib_read(void *opaque, uint32_t nport)
bellard85571bc2004-11-07 18:04:02 +0000120{
121 AdlibState *s = opaque;
122 uint8_t data;
123 int a = nport & 3;
124
bellard1d14ffa2005-10-30 18:58:22 +0000125 adlib_kill_timers (s);
bellard85571bc2004-11-07 18:04:02 +0000126 data = OPLRead (s->opl, a);
Juan Quintela68883a42017-04-26 00:37:14 +0200127
bellard85571bc2004-11-07 18:04:02 +0000128 return data;
129}
130
Hervé Poussineauc57fbf52017-06-21 06:34:00 +0200131static void timer_handler (void *opaque, int c, double interval_Sec)
bellard85571bc2004-11-07 18:04:02 +0000132{
Hervé Poussineau639b49e2017-06-21 06:34:01 +0200133 AdlibState *s = opaque;
bellard1d14ffa2005-10-30 18:58:22 +0000134 unsigned n = c & 1;
135#ifdef DEBUG
136 double interval;
bellardc0fe3822005-11-05 18:55:28 +0000137 int64_t exp;
bellard1d14ffa2005-10-30 18:58:22 +0000138#endif
139
bellard85571bc2004-11-07 18:04:02 +0000140 if (interval_Sec == 0.0) {
bellard1d14ffa2005-10-30 18:58:22 +0000141 s->ticking[n] = 0;
bellard85571bc2004-11-07 18:04:02 +0000142 return;
143 }
bellard1d14ffa2005-10-30 18:58:22 +0000144
145 s->ticking[n] = 1;
146#ifdef DEBUG
Rutuja Shah73bcb242016-03-21 21:32:30 +0530147 interval = NANOSECONDS_PER_SECOND * interval_Sec;
Alex Blighbc72ad62013-08-21 16:03:08 +0100148 exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval;
bellard1d14ffa2005-10-30 18:58:22 +0000149 s->exp[n] = exp;
150#endif
151
152 s->dexp[n] = interval_Sec * 1000000.0;
153 AUD_init_time_stamp_out (s->voice, &s->ats);
bellard85571bc2004-11-07 18:04:02 +0000154}
155
156static int write_audio (AdlibState *s, int samples)
157{
158 int net = 0;
bellard1d14ffa2005-10-30 18:58:22 +0000159 int pos = s->pos;
160
bellard85571bc2004-11-07 18:04:02 +0000161 while (samples) {
bellard1d14ffa2005-10-30 18:58:22 +0000162 int nbytes, wbytes, wsampl;
163
164 nbytes = samples << SHIFT;
165 wbytes = AUD_write (
166 s->voice,
167 s->mixbuf + (pos << (SHIFT - 1)),
168 nbytes
169 );
170
171 if (wbytes) {
172 wsampl = wbytes >> SHIFT;
173
174 samples -= wsampl;
175 pos = (pos + wsampl) % s->samples;
176
177 net += wsampl;
178 }
179 else {
bellard85571bc2004-11-07 18:04:02 +0000180 break;
bellard1d14ffa2005-10-30 18:58:22 +0000181 }
bellard85571bc2004-11-07 18:04:02 +0000182 }
bellard1d14ffa2005-10-30 18:58:22 +0000183
bellard85571bc2004-11-07 18:04:02 +0000184 return net;
185}
186
bellard1d14ffa2005-10-30 18:58:22 +0000187static void adlib_callback (void *opaque, int free)
bellard85571bc2004-11-07 18:04:02 +0000188{
189 AdlibState *s = opaque;
bellard1d14ffa2005-10-30 18:58:22 +0000190 int samples, net = 0, to_play, written;
bellard85571bc2004-11-07 18:04:02 +0000191
bellard1d14ffa2005-10-30 18:58:22 +0000192 samples = free >> SHIFT;
193 if (!(s->active && s->enabled) || !samples) {
194 return;
bellard85571bc2004-11-07 18:04:02 +0000195 }
bellard85571bc2004-11-07 18:04:02 +0000196
bellard1d14ffa2005-10-30 18:58:22 +0000197 to_play = audio_MIN (s->left, samples);
198 while (to_play) {
199 written = write_audio (s, to_play);
bellard85571bc2004-11-07 18:04:02 +0000200
bellard1d14ffa2005-10-30 18:58:22 +0000201 if (written) {
202 s->left -= written;
203 samples -= written;
204 to_play -= written;
205 s->pos = (s->pos + written) % s->samples;
206 }
207 else {
208 return;
209 }
210 }
bellard85571bc2004-11-07 18:04:02 +0000211
212 samples = audio_MIN (samples, s->samples - s->pos);
bellard1d14ffa2005-10-30 18:58:22 +0000213 if (!samples) {
214 return;
215 }
bellard85571bc2004-11-07 18:04:02 +0000216
bellard85571bc2004-11-07 18:04:02 +0000217 YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
bellard85571bc2004-11-07 18:04:02 +0000218
219 while (samples) {
bellard1d14ffa2005-10-30 18:58:22 +0000220 written = write_audio (s, samples);
bellard85571bc2004-11-07 18:04:02 +0000221
bellard1d14ffa2005-10-30 18:58:22 +0000222 if (written) {
223 net += written;
224 samples -= written;
225 s->pos = (s->pos + written) % s->samples;
226 }
227 else {
228 s->left = samples;
229 return;
230 }
231 }
bellard85571bc2004-11-07 18:04:02 +0000232}
233
234static void Adlib_fini (AdlibState *s)
235{
bellard85571bc2004-11-07 18:04:02 +0000236 if (s->opl) {
237 OPLDestroy (s->opl);
238 s->opl = NULL;
239 }
bellard85571bc2004-11-07 18:04:02 +0000240
Markus Armbrusterfb7da622014-06-06 18:35:13 +0200241 g_free(s->mixbuf);
bellard85571bc2004-11-07 18:04:02 +0000242
243 s->active = 0;
244 s->enabled = 0;
bellardc0fe3822005-11-05 18:55:28 +0000245 AUD_remove_card (&s->card);
bellard85571bc2004-11-07 18:04:02 +0000246}
247
Jan Kiszkaa8aec292013-06-22 08:06:54 +0200248static MemoryRegionPortio adlib_portio_list[] = {
Jan Kiszkaa8aec292013-06-22 08:06:54 +0200249 { 0, 4, 1, .read = adlib_read, .write = adlib_write, },
250 { 0, 2, 1, .read = adlib_read, .write = adlib_write, },
Hervé Poussineau2b21fb52013-08-14 11:49:04 +0200251 { 0x388, 4, 1, .read = adlib_read, .write = adlib_write, },
Jan Kiszkaa8aec292013-06-22 08:06:54 +0200252 PORTIO_END_OF_LIST(),
253};
254
Andreas Färberdb895a12012-11-25 02:37:14 +0100255static void adlib_realizefn (DeviceState *dev, Error **errp)
bellard85571bc2004-11-07 18:04:02 +0000256{
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200257 AdlibState *s = ADLIB(dev);
malc1ea879e2008-12-03 22:48:44 +0000258 struct audsettings as;
bellardc0fe3822005-11-05 18:55:28 +0000259
Juan Quintela8f7e2c22017-04-26 00:37:26 +0200260 s->opl = OPLCreate (3579545, s->freq);
bellard85571bc2004-11-07 18:04:02 +0000261 if (!s->opl) {
Andreas Färberdb895a12012-11-25 02:37:14 +0100262 error_setg (errp, "OPLCreate %d failed", s->freq);
263 return;
bellard85571bc2004-11-07 18:04:02 +0000264 }
265 else {
Hervé Poussineau639b49e2017-06-21 06:34:01 +0200266 OPLSetTimerHandler(s->opl, timer_handler, s);
bellard85571bc2004-11-07 18:04:02 +0000267 s->enabled = 1;
268 }
bellard85571bc2004-11-07 18:04:02 +0000269
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200270 as.freq = s->freq;
bellardc0fe3822005-11-05 18:55:28 +0000271 as.nchannels = SHIFT;
272 as.fmt = AUD_FMT_S16;
bellardd929eba2006-07-04 21:47:22 +0000273 as.endianness = AUDIO_HOST_ENDIANNESS;
bellardc0fe3822005-11-05 18:55:28 +0000274
malc1a7dafc2009-05-14 03:11:35 +0400275 AUD_register_card ("adlib", &s->card);
bellardc0fe3822005-11-05 18:55:28 +0000276
bellard1d14ffa2005-10-30 18:58:22 +0000277 s->voice = AUD_open_out (
bellardc0fe3822005-11-05 18:55:28 +0000278 &s->card,
bellard1d14ffa2005-10-30 18:58:22 +0000279 s->voice,
280 "adlib",
281 s,
282 adlib_callback,
bellardd929eba2006-07-04 21:47:22 +0000283 &as
bellard1d14ffa2005-10-30 18:58:22 +0000284 );
bellard85571bc2004-11-07 18:04:02 +0000285 if (!s->voice) {
286 Adlib_fini (s);
Andreas Färberdb895a12012-11-25 02:37:14 +0100287 error_setg (errp, "Initializing audio voice failed");
288 return;
bellard85571bc2004-11-07 18:04:02 +0000289 }
290
bellard1d14ffa2005-10-30 18:58:22 +0000291 s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
Anthony Liguori7267c092011-08-20 22:09:37 -0500292 s->mixbuf = g_malloc0 (s->samples << SHIFT);
bellard85571bc2004-11-07 18:04:02 +0000293
Paolo Bonzini7f0ba7b2013-12-02 10:16:18 +0100294 adlib_portio_list[0].offset = s->port;
295 adlib_portio_list[1].offset = s->port + 8;
Kirill Batuzov848696b2014-04-29 17:38:39 +0400296 portio_list_init (&s->port_list, OBJECT(s), adlib_portio_list, s, "adlib");
297 portio_list_add (&s->port_list, isa_address_space_io(&s->parent_obj), 0);
bellard85571bc2004-11-07 18:04:02 +0000298}
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200299
300static Property adlib_properties[] = {
Paolo Bonzinic7bcc852014-02-08 11:01:53 +0100301 DEFINE_PROP_UINT32 ("iobase", AdlibState, port, 0x220),
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200302 DEFINE_PROP_UINT32 ("freq", AdlibState, freq, 44100),
303 DEFINE_PROP_END_OF_LIST (),
304};
305
306static void adlib_class_initfn (ObjectClass *klass, void *data)
307{
308 DeviceClass *dc = DEVICE_CLASS (klass);
Andreas Färberdb895a12012-11-25 02:37:14 +0100309
310 dc->realize = adlib_realizefn;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300311 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200312 dc->desc = ADLIB_DESC;
313 dc->props = adlib_properties;
314}
315
316static const TypeInfo adlib_info = {
317 .name = TYPE_ADLIB,
318 .parent = TYPE_ISA_DEVICE,
319 .instance_size = sizeof (AdlibState),
320 .class_init = adlib_class_initfn,
321};
322
Paolo Bonzini36cd6f62013-04-18 18:43:58 +0200323static int Adlib_init (ISABus *bus)
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200324{
325 isa_create_simple (bus, TYPE_ADLIB);
326 return 0;
327}
328
329static void adlib_register_types (void)
330{
331 type_register_static (&adlib_info);
Paolo Bonzini36cd6f62013-04-18 18:43:58 +0200332 isa_register_soundhw("adlib", ADLIB_DESC, Adlib_init);
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200333}
334
335type_init (adlib_register_types)