2004-11-07 19:04:02 +01:00
|
|
|
/*
|
2005-10-30 19:58:22 +01:00
|
|
|
* QEMU Proxy for OPL2/3 emulation by MAME team
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004-2005 Vassili Karpov (malc)
|
|
|
|
*
|
2004-11-07 19:04:02 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2007-12-02 18:47:33 +01:00
|
|
|
|
2016-01-18 18:33:52 +01:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/hw.h"
|
2017-05-08 22:57:35 +02:00
|
|
|
#include "hw/audio/soundhw.h"
|
2007-12-02 18:17:45 +01:00
|
|
|
#include "audio/audio.h"
|
2013-02-05 17:06:20 +01:00
|
|
|
#include "hw/isa/isa.h"
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2007-12-02 18:47:33 +01:00
|
|
|
//#define DEBUG
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
#define ADLIB_KILL_TIMERS 1
|
|
|
|
|
2013-04-18 18:43:57 +02:00
|
|
|
#define ADLIB_DESC "Yamaha YM3812 (OPL2)"
|
|
|
|
|
2007-12-02 18:47:33 +01:00
|
|
|
#ifdef DEBUG
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2007-12-02 18:47:33 +01:00
|
|
|
#endif
|
|
|
|
|
2004-11-10 00:09:44 +01:00
|
|
|
#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define ldebug(...) dolog (__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define ldebug(...)
|
|
|
|
#endif
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2013-03-18 17:36:02 +01:00
|
|
|
#include "fmopl.h"
|
2004-11-07 19:04:02 +01:00
|
|
|
#define SHIFT 1
|
|
|
|
|
2013-04-18 18:43:57 +02:00
|
|
|
#define TYPE_ADLIB "adlib"
|
|
|
|
#define ADLIB(obj) OBJECT_CHECK(AdlibState, (obj), TYPE_ADLIB)
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
typedef struct {
|
2013-04-18 18:43:57 +02:00
|
|
|
ISADevice parent_obj;
|
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
QEMUSoundCard card;
|
2013-04-18 18:43:57 +02:00
|
|
|
uint32_t freq;
|
|
|
|
uint32_t port;
|
2005-10-30 19:58:22 +01:00
|
|
|
int ticking[2];
|
2004-11-07 19:04:02 +01:00
|
|
|
int enabled;
|
|
|
|
int active;
|
|
|
|
int bufpos;
|
2005-10-30 19:58:22 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
int64_t exp[2];
|
|
|
|
#endif
|
2004-11-07 19:04:02 +01:00
|
|
|
int16_t *mixbuf;
|
2005-10-30 19:58:22 +01:00
|
|
|
uint64_t dexp[2];
|
|
|
|
SWVoiceOut *voice;
|
|
|
|
int left, pos, samples;
|
|
|
|
QEMUAudioTimeStamp ats;
|
2004-11-07 19:04:02 +01:00
|
|
|
FM_OPL *opl;
|
2014-04-29 15:38:39 +02:00
|
|
|
PortioList port_list;
|
2004-11-07 19:04:02 +01:00
|
|
|
} AdlibState;
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void adlib_stop_opl_timer (AdlibState *s, size_t n)
|
|
|
|
{
|
|
|
|
OPLTimerOver (s->opl, n);
|
|
|
|
s->ticking[n] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adlib_kill_timers (AdlibState *s)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; ++i) {
|
|
|
|
if (s->ticking[i]) {
|
|
|
|
uint64_t delta;
|
|
|
|
|
2005-11-05 19:55:28 +01:00
|
|
|
delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
|
2005-10-30 19:58:22 +01:00
|
|
|
ldebug (
|
|
|
|
"delta = %f dexp = %f expired => %d\n",
|
|
|
|
delta / 1000000.0,
|
|
|
|
s->dexp[i] / 1000000.0,
|
|
|
|
delta >= s->dexp[i]
|
|
|
|
);
|
|
|
|
if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
|
|
|
|
adlib_stop_opl_timer (s, i);
|
|
|
|
AUD_init_time_stamp_out (s->voice, &s->ats);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-07 18:32:54 +02:00
|
|
|
static void adlib_write(void *opaque, uint32_t nport, uint32_t val)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
|
|
|
AdlibState *s = opaque;
|
|
|
|
int a = nport & 3;
|
|
|
|
|
|
|
|
s->active = 1;
|
2005-10-30 19:58:22 +01:00
|
|
|
AUD_set_active_out (s->voice, 1);
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
adlib_kill_timers (s);
|
|
|
|
|
2011-09-18 16:27:23 +02:00
|
|
|
OPLWrite (s->opl, a, val);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2015-10-07 18:32:54 +02:00
|
|
|
static uint32_t adlib_read(void *opaque, uint32_t nport)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
|
|
|
AdlibState *s = opaque;
|
|
|
|
uint8_t data;
|
|
|
|
int a = nport & 3;
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
adlib_kill_timers (s);
|
2004-11-07 19:04:02 +01:00
|
|
|
data = OPLRead (s->opl, a);
|
2017-04-26 00:37:14 +02:00
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2017-06-21 06:34:00 +02:00
|
|
|
static void timer_handler (void *opaque, int c, double interval_Sec)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2017-06-21 06:34:01 +02:00
|
|
|
AdlibState *s = opaque;
|
2005-10-30 19:58:22 +01:00
|
|
|
unsigned n = c & 1;
|
|
|
|
#ifdef DEBUG
|
|
|
|
double interval;
|
2005-11-05 19:55:28 +01:00
|
|
|
int64_t exp;
|
2004-11-07 19:04:02 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (interval_Sec == 0.0) {
|
2005-10-30 19:58:22 +01:00
|
|
|
s->ticking[n] = 0;
|
2004-11-07 19:04:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
|
|
|
s->ticking[n] = 1;
|
|
|
|
#ifdef DEBUG
|
2016-03-21 17:02:30 +01:00
|
|
|
interval = NANOSECONDS_PER_SECOND * interval_Sec;
|
2013-08-21 17:03:08 +02:00
|
|
|
exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval;
|
2005-10-30 19:58:22 +01:00
|
|
|
s->exp[n] = exp;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
s->dexp[n] = interval_Sec * 1000000.0;
|
|
|
|
AUD_init_time_stamp_out (s->voice, &s->ats);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int write_audio (AdlibState *s, int samples)
|
|
|
|
{
|
|
|
|
int net = 0;
|
2005-10-30 19:58:22 +01:00
|
|
|
int pos = s->pos;
|
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
while (samples) {
|
2005-10-30 19:58:22 +01:00
|
|
|
int nbytes, wbytes, wsampl;
|
|
|
|
|
|
|
|
nbytes = samples << SHIFT;
|
|
|
|
wbytes = AUD_write (
|
|
|
|
s->voice,
|
|
|
|
s->mixbuf + (pos << (SHIFT - 1)),
|
|
|
|
nbytes
|
|
|
|
);
|
|
|
|
|
|
|
|
if (wbytes) {
|
|
|
|
wsampl = wbytes >> SHIFT;
|
|
|
|
|
|
|
|
samples -= wsampl;
|
|
|
|
pos = (pos + wsampl) % s->samples;
|
|
|
|
|
|
|
|
net += wsampl;
|
|
|
|
}
|
|
|
|
else {
|
2004-11-07 19:04:02 +01:00
|
|
|
break;
|
2005-10-30 19:58:22 +01:00
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2005-10-30 19:58:22 +01:00
|
|
|
|
2004-11-07 19:04:02 +01:00
|
|
|
return net;
|
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
static void adlib_callback (void *opaque, int free)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
|
|
|
AdlibState *s = opaque;
|
2005-10-30 19:58:22 +01:00
|
|
|
int samples, net = 0, to_play, written;
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
samples = free >> SHIFT;
|
|
|
|
if (!(s->active && s->enabled) || !samples) {
|
|
|
|
return;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
to_play = audio_MIN (s->left, samples);
|
|
|
|
while (to_play) {
|
|
|
|
written = write_audio (s, to_play);
|
|
|
|
|
|
|
|
if (written) {
|
|
|
|
s->left -= written;
|
|
|
|
samples -= written;
|
|
|
|
to_play -= written;
|
|
|
|
s->pos = (s->pos + written) % s->samples;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
samples = audio_MIN (samples, s->samples - s->pos);
|
2005-10-30 19:58:22 +01:00
|
|
|
if (!samples) {
|
|
|
|
return;
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
|
|
|
|
|
|
|
|
while (samples) {
|
2005-10-30 19:58:22 +01:00
|
|
|
written = write_audio (s, samples);
|
|
|
|
|
|
|
|
if (written) {
|
|
|
|
net += written;
|
|
|
|
samples -= written;
|
|
|
|
s->pos = (s->pos + written) % s->samples;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s->left = samples;
|
|
|
|
return;
|
|
|
|
}
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Adlib_fini (AdlibState *s)
|
|
|
|
{
|
|
|
|
if (s->opl) {
|
|
|
|
OPLDestroy (s->opl);
|
|
|
|
s->opl = NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-06 18:35:13 +02:00
|
|
|
g_free(s->mixbuf);
|
2004-11-07 19:04:02 +01:00
|
|
|
|
|
|
|
s->active = 0;
|
|
|
|
s->enabled = 0;
|
2005-11-05 19:55:28 +01:00
|
|
|
AUD_remove_card (&s->card);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2013-06-22 08:06:54 +02:00
|
|
|
static MemoryRegionPortio adlib_portio_list[] = {
|
|
|
|
{ 0, 4, 1, .read = adlib_read, .write = adlib_write, },
|
|
|
|
{ 0, 2, 1, .read = adlib_read, .write = adlib_write, },
|
2013-08-14 11:49:04 +02:00
|
|
|
{ 0x388, 4, 1, .read = adlib_read, .write = adlib_write, },
|
2013-06-22 08:06:54 +02:00
|
|
|
PORTIO_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2012-11-25 02:37:14 +01:00
|
|
|
static void adlib_realizefn (DeviceState *dev, Error **errp)
|
2004-11-07 19:04:02 +01:00
|
|
|
{
|
2013-04-18 18:43:57 +02:00
|
|
|
AdlibState *s = ADLIB(dev);
|
2008-12-03 23:48:44 +01:00
|
|
|
struct audsettings as;
|
2005-11-05 19:55:28 +01:00
|
|
|
|
2017-04-26 00:37:26 +02:00
|
|
|
s->opl = OPLCreate (3579545, s->freq);
|
2004-11-07 19:04:02 +01:00
|
|
|
if (!s->opl) {
|
2012-11-25 02:37:14 +01:00
|
|
|
error_setg (errp, "OPLCreate %d failed", s->freq);
|
|
|
|
return;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-06-21 06:34:01 +02:00
|
|
|
OPLSetTimerHandler(s->opl, timer_handler, s);
|
2004-11-07 19:04:02 +01:00
|
|
|
s->enabled = 1;
|
|
|
|
}
|
|
|
|
|
2013-04-18 18:43:57 +02:00
|
|
|
as.freq = s->freq;
|
2005-11-05 19:55:28 +01:00
|
|
|
as.nchannels = SHIFT;
|
2019-03-08 23:34:13 +01:00
|
|
|
as.fmt = AUDIO_FORMAT_S16;
|
2006-07-04 23:47:22 +02:00
|
|
|
as.endianness = AUDIO_HOST_ENDIANNESS;
|
2005-11-05 19:55:28 +01:00
|
|
|
|
2009-05-14 01:11:35 +02:00
|
|
|
AUD_register_card ("adlib", &s->card);
|
2005-11-05 19:55:28 +01:00
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
s->voice = AUD_open_out (
|
2005-11-05 19:55:28 +01:00
|
|
|
&s->card,
|
2005-10-30 19:58:22 +01:00
|
|
|
s->voice,
|
|
|
|
"adlib",
|
|
|
|
s,
|
|
|
|
adlib_callback,
|
2006-07-04 23:47:22 +02:00
|
|
|
&as
|
2005-10-30 19:58:22 +01:00
|
|
|
);
|
2004-11-07 19:04:02 +01:00
|
|
|
if (!s->voice) {
|
|
|
|
Adlib_fini (s);
|
2012-11-25 02:37:14 +01:00
|
|
|
error_setg (errp, "Initializing audio voice failed");
|
|
|
|
return;
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
|
|
|
|
2005-10-30 19:58:22 +01:00
|
|
|
s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
|
2011-08-21 05:09:37 +02:00
|
|
|
s->mixbuf = g_malloc0 (s->samples << SHIFT);
|
2004-11-07 19:04:02 +01:00
|
|
|
|
2013-12-02 10:16:18 +01:00
|
|
|
adlib_portio_list[0].offset = s->port;
|
|
|
|
adlib_portio_list[1].offset = s->port + 8;
|
2014-04-29 15:38:39 +02:00
|
|
|
portio_list_init (&s->port_list, OBJECT(s), adlib_portio_list, s, "adlib");
|
|
|
|
portio_list_add (&s->port_list, isa_address_space_io(&s->parent_obj), 0);
|
2004-11-07 19:04:02 +01:00
|
|
|
}
|
2013-04-18 18:43:57 +02:00
|
|
|
|
|
|
|
static Property adlib_properties[] = {
|
2014-02-08 11:01:53 +01:00
|
|
|
DEFINE_PROP_UINT32 ("iobase", AdlibState, port, 0x220),
|
2013-04-18 18:43:57 +02:00
|
|
|
DEFINE_PROP_UINT32 ("freq", AdlibState, freq, 44100),
|
|
|
|
DEFINE_PROP_END_OF_LIST (),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void adlib_class_initfn (ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS (klass);
|
2012-11-25 02:37:14 +01:00
|
|
|
|
|
|
|
dc->realize = adlib_realizefn;
|
2013-07-29 16:17:45 +02:00
|
|
|
set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
|
2013-04-18 18:43:57 +02:00
|
|
|
dc->desc = ADLIB_DESC;
|
|
|
|
dc->props = adlib_properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo adlib_info = {
|
|
|
|
.name = TYPE_ADLIB,
|
|
|
|
.parent = TYPE_ISA_DEVICE,
|
|
|
|
.instance_size = sizeof (AdlibState),
|
|
|
|
.class_init = adlib_class_initfn,
|
|
|
|
};
|
|
|
|
|
2013-04-18 18:43:58 +02:00
|
|
|
static int Adlib_init (ISABus *bus)
|
2013-04-18 18:43:57 +02:00
|
|
|
{
|
|
|
|
isa_create_simple (bus, TYPE_ADLIB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adlib_register_types (void)
|
|
|
|
{
|
|
|
|
type_register_static (&adlib_info);
|
2013-04-18 18:43:58 +02:00
|
|
|
isa_register_soundhw("adlib", ADLIB_DESC, Adlib_init);
|
2013-04-18 18:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init (adlib_register_types)
|