blob: 5f979b1487d1bc5c1ce82851d64c02b5af9dc3f3 [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"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020027#include "qemu/module.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"
Markus Armbrustera27bd6c2019-08-12 07:23:51 +020031#include "hw/qdev-properties.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040032#include "qom/object.h"
bellard85571bc2004-11-07 18:04:02 +000033
ths9f0683d2007-12-02 17:47:33 +000034//#define DEBUG
35
bellard1d14ffa2005-10-30 18:58:22 +000036#define ADLIB_KILL_TIMERS 1
37
Paolo Bonzini8c444a12013-04-18 18:43:57 +020038#define ADLIB_DESC "Yamaha YM3812 (OPL2)"
Paolo Bonzini8c444a12013-04-18 18:43:57 +020039
ths9f0683d2007-12-02 17:47:33 +000040#ifdef DEBUG
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/timer.h"
ths9f0683d2007-12-02 17:47:33 +000042#endif
43
bellardfb065182004-11-09 23:09:44 +000044#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
45#ifdef DEBUG
46#define ldebug(...) dolog (__VA_ARGS__)
47#else
48#define ldebug(...)
49#endif
bellard85571bc2004-11-07 18:04:02 +000050
Paolo Bonzini47b43a12013-03-18 17:36:02 +010051#include "fmopl.h"
bellard85571bc2004-11-07 18:04:02 +000052#define SHIFT 1
bellard85571bc2004-11-07 18:04:02 +000053
Paolo Bonzini8c444a12013-04-18 18:43:57 +020054#define TYPE_ADLIB "adlib"
Eduardo Habkost80633962020-09-16 14:25:19 -040055OBJECT_DECLARE_SIMPLE_TYPE(AdlibState, ADLIB)
bellard85571bc2004-11-07 18:04:02 +000056
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040057struct AdlibState {
Paolo Bonzini8c444a12013-04-18 18:43:57 +020058 ISADevice parent_obj;
59
bellardc0fe3822005-11-05 18:55:28 +000060 QEMUSoundCard card;
Paolo Bonzini8c444a12013-04-18 18:43:57 +020061 uint32_t freq;
62 uint32_t port;
bellard1d14ffa2005-10-30 18:58:22 +000063 int ticking[2];
bellard85571bc2004-11-07 18:04:02 +000064 int enabled;
65 int active;
bellard85571bc2004-11-07 18:04:02 +000066 int bufpos;
bellard1d14ffa2005-10-30 18:58:22 +000067#ifdef DEBUG
68 int64_t exp[2];
69#endif
bellard85571bc2004-11-07 18:04:02 +000070 int16_t *mixbuf;
bellard1d14ffa2005-10-30 18:58:22 +000071 uint64_t dexp[2];
72 SWVoiceOut *voice;
73 int left, pos, samples;
74 QEMUAudioTimeStamp ats;
bellard85571bc2004-11-07 18:04:02 +000075 FM_OPL *opl;
Kirill Batuzov848696b2014-04-29 17:38:39 +040076 PortioList port_list;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040077};
bellard85571bc2004-11-07 18:04:02 +000078
bellard1d14ffa2005-10-30 18:58:22 +000079static void adlib_stop_opl_timer (AdlibState *s, size_t n)
80{
bellard1d14ffa2005-10-30 18:58:22 +000081 OPLTimerOver (s->opl, n);
bellard1d14ffa2005-10-30 18:58:22 +000082 s->ticking[n] = 0;
83}
84
85static void adlib_kill_timers (AdlibState *s)
86{
87 size_t i;
88
89 for (i = 0; i < 2; ++i) {
90 if (s->ticking[i]) {
91 uint64_t delta;
92
bellardc0fe3822005-11-05 18:55:28 +000093 delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
bellard1d14ffa2005-10-30 18:58:22 +000094 ldebug (
95 "delta = %f dexp = %f expired => %d\n",
96 delta / 1000000.0,
97 s->dexp[i] / 1000000.0,
98 delta >= s->dexp[i]
99 );
100 if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
101 adlib_stop_opl_timer (s, i);
102 AUD_init_time_stamp_out (s->voice, &s->ats);
103 }
104 }
105 }
106}
107
Nutan Shinde8307c292015-10-07 22:02:54 +0530108static void adlib_write(void *opaque, uint32_t nport, uint32_t val)
bellard85571bc2004-11-07 18:04:02 +0000109{
110 AdlibState *s = opaque;
111 int a = nport & 3;
bellard85571bc2004-11-07 18:04:02 +0000112
bellard85571bc2004-11-07 18:04:02 +0000113 s->active = 1;
bellard1d14ffa2005-10-30 18:58:22 +0000114 AUD_set_active_out (s->voice, 1);
bellard85571bc2004-11-07 18:04:02 +0000115
bellard1d14ffa2005-10-30 18:58:22 +0000116 adlib_kill_timers (s);
117
Hervé Poussineaue8beeae2011-09-18 16:27:23 +0200118 OPLWrite (s->opl, a, val);
bellard85571bc2004-11-07 18:04:02 +0000119}
120
Nutan Shinde8307c292015-10-07 22:02:54 +0530121static uint32_t adlib_read(void *opaque, uint32_t nport)
bellard85571bc2004-11-07 18:04:02 +0000122{
123 AdlibState *s = opaque;
bellard85571bc2004-11-07 18:04:02 +0000124 int a = nport & 3;
125
bellard1d14ffa2005-10-30 18:58:22 +0000126 adlib_kill_timers (s);
Simran Singhalb3ac2b92020-04-01 22:23:14 +0530127 return OPLRead (s->opl, a);
bellard85571bc2004-11-07 18:04:02 +0000128}
129
Hervé Poussineauc57fbf52017-06-21 06:34:00 +0200130static void timer_handler (void *opaque, int c, double interval_Sec)
bellard85571bc2004-11-07 18:04:02 +0000131{
Hervé Poussineau639b49e2017-06-21 06:34:01 +0200132 AdlibState *s = opaque;
bellard1d14ffa2005-10-30 18:58:22 +0000133 unsigned n = c & 1;
134#ifdef DEBUG
135 double interval;
bellardc0fe3822005-11-05 18:55:28 +0000136 int64_t exp;
bellard1d14ffa2005-10-30 18:58:22 +0000137#endif
138
bellard85571bc2004-11-07 18:04:02 +0000139 if (interval_Sec == 0.0) {
bellard1d14ffa2005-10-30 18:58:22 +0000140 s->ticking[n] = 0;
bellard85571bc2004-11-07 18:04:02 +0000141 return;
142 }
bellard1d14ffa2005-10-30 18:58:22 +0000143
144 s->ticking[n] = 1;
145#ifdef DEBUG
Rutuja Shah73bcb242016-03-21 21:32:30 +0530146 interval = NANOSECONDS_PER_SECOND * interval_Sec;
Alex Blighbc72ad62013-08-21 16:03:08 +0100147 exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval;
bellard1d14ffa2005-10-30 18:58:22 +0000148 s->exp[n] = exp;
149#endif
150
151 s->dexp[n] = interval_Sec * 1000000.0;
152 AUD_init_time_stamp_out (s->voice, &s->ats);
bellard85571bc2004-11-07 18:04:02 +0000153}
154
155static int write_audio (AdlibState *s, int samples)
156{
157 int net = 0;
bellard1d14ffa2005-10-30 18:58:22 +0000158 int pos = s->pos;
159
bellard85571bc2004-11-07 18:04:02 +0000160 while (samples) {
bellard1d14ffa2005-10-30 18:58:22 +0000161 int nbytes, wbytes, wsampl;
162
163 nbytes = samples << SHIFT;
164 wbytes = AUD_write (
165 s->voice,
166 s->mixbuf + (pos << (SHIFT - 1)),
167 nbytes
168 );
169
170 if (wbytes) {
171 wsampl = wbytes >> SHIFT;
172
173 samples -= wsampl;
174 pos = (pos + wsampl) % s->samples;
175
176 net += wsampl;
177 }
178 else {
bellard85571bc2004-11-07 18:04:02 +0000179 break;
bellard1d14ffa2005-10-30 18:58:22 +0000180 }
bellard85571bc2004-11-07 18:04:02 +0000181 }
bellard1d14ffa2005-10-30 18:58:22 +0000182
bellard85571bc2004-11-07 18:04:02 +0000183 return net;
184}
185
bellard1d14ffa2005-10-30 18:58:22 +0000186static void adlib_callback (void *opaque, int free)
bellard85571bc2004-11-07 18:04:02 +0000187{
188 AdlibState *s = opaque;
Richard Henderson79fe9e42021-07-12 19:57:41 +0000189 int samples, to_play, written;
bellard85571bc2004-11-07 18:04:02 +0000190
bellard1d14ffa2005-10-30 18:58:22 +0000191 samples = free >> SHIFT;
192 if (!(s->active && s->enabled) || !samples) {
193 return;
bellard85571bc2004-11-07 18:04:02 +0000194 }
bellard85571bc2004-11-07 18:04:02 +0000195
Kővágó, Zoltán58935912019-08-19 01:06:54 +0200196 to_play = MIN (s->left, samples);
bellard1d14ffa2005-10-30 18:58:22 +0000197 while (to_play) {
198 written = write_audio (s, to_play);
bellard85571bc2004-11-07 18:04:02 +0000199
bellard1d14ffa2005-10-30 18:58:22 +0000200 if (written) {
201 s->left -= written;
202 samples -= written;
203 to_play -= written;
204 s->pos = (s->pos + written) % s->samples;
205 }
206 else {
207 return;
208 }
209 }
bellard85571bc2004-11-07 18:04:02 +0000210
Kővágó, Zoltán58935912019-08-19 01:06:54 +0200211 samples = MIN (samples, s->samples - s->pos);
bellard1d14ffa2005-10-30 18:58:22 +0000212 if (!samples) {
213 return;
214 }
bellard85571bc2004-11-07 18:04:02 +0000215
bellard85571bc2004-11-07 18:04:02 +0000216 YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
bellard85571bc2004-11-07 18:04:02 +0000217
218 while (samples) {
bellard1d14ffa2005-10-30 18:58:22 +0000219 written = write_audio (s, samples);
bellard85571bc2004-11-07 18:04:02 +0000220
bellard1d14ffa2005-10-30 18:58:22 +0000221 if (written) {
bellard1d14ffa2005-10-30 18:58:22 +0000222 samples -= written;
223 s->pos = (s->pos + written) % s->samples;
224 }
225 else {
226 s->left = samples;
227 return;
228 }
229 }
bellard85571bc2004-11-07 18:04:02 +0000230}
231
232static void Adlib_fini (AdlibState *s)
233{
bellard85571bc2004-11-07 18:04:02 +0000234 if (s->opl) {
235 OPLDestroy (s->opl);
236 s->opl = NULL;
237 }
bellard85571bc2004-11-07 18:04:02 +0000238
Markus Armbrusterfb7da622014-06-06 18:35:13 +0200239 g_free(s->mixbuf);
bellard85571bc2004-11-07 18:04:02 +0000240
241 s->active = 0;
242 s->enabled = 0;
bellardc0fe3822005-11-05 18:55:28 +0000243 AUD_remove_card (&s->card);
bellard85571bc2004-11-07 18:04:02 +0000244}
245
Jan Kiszkaa8aec292013-06-22 08:06:54 +0200246static MemoryRegionPortio adlib_portio_list[] = {
Jan Kiszkaa8aec292013-06-22 08:06:54 +0200247 { 0, 4, 1, .read = adlib_read, .write = adlib_write, },
248 { 0, 2, 1, .read = adlib_read, .write = adlib_write, },
Hervé Poussineau2b21fb52013-08-14 11:49:04 +0200249 { 0x388, 4, 1, .read = adlib_read, .write = adlib_write, },
Jan Kiszkaa8aec292013-06-22 08:06:54 +0200250 PORTIO_END_OF_LIST(),
251};
252
Andreas Färberdb895a12012-11-25 02:37:14 +0100253static void adlib_realizefn (DeviceState *dev, Error **errp)
bellard85571bc2004-11-07 18:04:02 +0000254{
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200255 AdlibState *s = ADLIB(dev);
malc1ea879e2008-12-03 22:48:44 +0000256 struct audsettings as;
bellardc0fe3822005-11-05 18:55:28 +0000257
Juan Quintela8f7e2c22017-04-26 00:37:26 +0200258 s->opl = OPLCreate (3579545, s->freq);
bellard85571bc2004-11-07 18:04:02 +0000259 if (!s->opl) {
Andreas Färberdb895a12012-11-25 02:37:14 +0100260 error_setg (errp, "OPLCreate %d failed", s->freq);
261 return;
bellard85571bc2004-11-07 18:04:02 +0000262 }
263 else {
Hervé Poussineau639b49e2017-06-21 06:34:01 +0200264 OPLSetTimerHandler(s->opl, timer_handler, s);
bellard85571bc2004-11-07 18:04:02 +0000265 s->enabled = 1;
266 }
bellard85571bc2004-11-07 18:04:02 +0000267
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200268 as.freq = s->freq;
bellardc0fe3822005-11-05 18:55:28 +0000269 as.nchannels = SHIFT;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100270 as.fmt = AUDIO_FORMAT_S16;
bellardd929eba2006-07-04 21:47:22 +0000271 as.endianness = AUDIO_HOST_ENDIANNESS;
bellardc0fe3822005-11-05 18:55:28 +0000272
malc1a7dafc2009-05-14 03:11:35 +0400273 AUD_register_card ("adlib", &s->card);
bellardc0fe3822005-11-05 18:55:28 +0000274
bellard1d14ffa2005-10-30 18:58:22 +0000275 s->voice = AUD_open_out (
bellardc0fe3822005-11-05 18:55:28 +0000276 &s->card,
bellard1d14ffa2005-10-30 18:58:22 +0000277 s->voice,
278 "adlib",
279 s,
280 adlib_callback,
bellardd929eba2006-07-04 21:47:22 +0000281 &as
bellard1d14ffa2005-10-30 18:58:22 +0000282 );
bellard85571bc2004-11-07 18:04:02 +0000283 if (!s->voice) {
284 Adlib_fini (s);
Andreas Färberdb895a12012-11-25 02:37:14 +0100285 error_setg (errp, "Initializing audio voice failed");
286 return;
bellard85571bc2004-11-07 18:04:02 +0000287 }
288
bellard1d14ffa2005-10-30 18:58:22 +0000289 s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
Anthony Liguori7267c092011-08-20 22:09:37 -0500290 s->mixbuf = g_malloc0 (s->samples << SHIFT);
bellard85571bc2004-11-07 18:04:02 +0000291
Paolo Bonzini7f0ba7b2013-12-02 10:16:18 +0100292 adlib_portio_list[0].offset = s->port;
293 adlib_portio_list[1].offset = s->port + 8;
Kirill Batuzov848696b2014-04-29 17:38:39 +0400294 portio_list_init (&s->port_list, OBJECT(s), adlib_portio_list, s, "adlib");
295 portio_list_add (&s->port_list, isa_address_space_io(&s->parent_obj), 0);
bellard85571bc2004-11-07 18:04:02 +0000296}
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200297
298static Property adlib_properties[] = {
Kővágó, Zoltán88e47b92019-08-19 01:06:49 +0200299 DEFINE_AUDIO_PROPERTIES(AdlibState, card),
Paolo Bonzinic7bcc852014-02-08 11:01:53 +0100300 DEFINE_PROP_UINT32 ("iobase", AdlibState, port, 0x220),
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200301 DEFINE_PROP_UINT32 ("freq", AdlibState, freq, 44100),
302 DEFINE_PROP_END_OF_LIST (),
303};
304
305static void adlib_class_initfn (ObjectClass *klass, void *data)
306{
307 DeviceClass *dc = DEVICE_CLASS (klass);
Andreas Färberdb895a12012-11-25 02:37:14 +0100308
309 dc->realize = adlib_realizefn;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300310 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200311 dc->desc = ADLIB_DESC;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400312 device_class_set_props(dc, adlib_properties);
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200313}
314
315static const TypeInfo adlib_info = {
316 .name = TYPE_ADLIB,
317 .parent = TYPE_ISA_DEVICE,
318 .instance_size = sizeof (AdlibState),
319 .class_init = adlib_class_initfn,
320};
321
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200322static void adlib_register_types (void)
323{
324 type_register_static (&adlib_info);
Gerd Hoffmann86388a32020-07-02 15:25:11 +0200325 deprecated_register_soundhw("adlib", ADLIB_DESC, 1, TYPE_ADLIB);
Paolo Bonzini8c444a12013-04-18 18:43:57 +0200326}
327
328type_init (adlib_register_types)