2004-11-07 21:04:02 +03:00
|
|
|
/*
|
2005-10-30 21:58:22 +03:00
|
|
|
* QEMU Proxy for OPL2/3 emulation by MAME team
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004-2005 Vassili Karpov (malc)
|
|
|
|
*
|
2004-11-07 21:04:02 +03: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 20:47:33 +03:00
|
|
|
|
2016-01-18 20:33:52 +03: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 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2017-05-08 23:57:35 +03:00
|
|
|
#include "hw/audio/soundhw.h"
|
2007-12-02 20:17:45 +03:00
|
|
|
#include "audio/audio.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/isa/isa.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2004-11-07 21:04:02 +03:00
|
|
|
|
2007-12-02 20:47:33 +03:00
|
|
|
//#define DEBUG
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
#define ADLIB_KILL_TIMERS 1
|
|
|
|
|
2013-04-18 20:43:57 +04:00
|
|
|
#define ADLIB_DESC "Yamaha YM3812 (OPL2)"
|
|
|
|
|
2007-12-02 20:47:33 +03:00
|
|
|
#ifdef DEBUG
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
2007-12-02 20:47:33 +03:00
|
|
|
#endif
|
|
|
|
|
2004-11-10 02:09:44 +03:00
|
|
|
#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define ldebug(...) dolog (__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define ldebug(...)
|
|
|
|
#endif
|
2004-11-07 21:04:02 +03:00
|
|
|
|
2013-03-18 20:36:02 +04:00
|
|
|
#include "fmopl.h"
|
2004-11-07 21:04:02 +03:00
|
|
|
#define SHIFT 1
|
|
|
|
|
2013-04-18 20:43:57 +04:00
|
|
|
#define TYPE_ADLIB "adlib"
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(AdlibState, ADLIB)
|
2004-11-07 21:04:02 +03:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct AdlibState {
|
2013-04-18 20:43:57 +04:00
|
|
|
ISADevice parent_obj;
|
|
|
|
|
2005-11-05 21:55:28 +03:00
|
|
|
QEMUSoundCard card;
|
2013-04-18 20:43:57 +04:00
|
|
|
uint32_t freq;
|
|
|
|
uint32_t port;
|
2005-10-30 21:58:22 +03:00
|
|
|
int ticking[2];
|
2004-11-07 21:04:02 +03:00
|
|
|
int enabled;
|
|
|
|
int active;
|
|
|
|
int bufpos;
|
2005-10-30 21:58:22 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
int64_t exp[2];
|
|
|
|
#endif
|
2004-11-07 21:04:02 +03:00
|
|
|
int16_t *mixbuf;
|
2005-10-30 21:58:22 +03:00
|
|
|
uint64_t dexp[2];
|
|
|
|
SWVoiceOut *voice;
|
|
|
|
int left, pos, samples;
|
|
|
|
QEMUAudioTimeStamp ats;
|
2004-11-07 21:04:02 +03:00
|
|
|
FM_OPL *opl;
|
2014-04-29 17:38:39 +04:00
|
|
|
PortioList port_list;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2004-11-07 21:04:02 +03:00
|
|
|
|
2005-10-30 21:58:22 +03: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 21:55:28 +03:00
|
|
|
delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
|
2005-10-30 21:58:22 +03: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 19:32:54 +03:00
|
|
|
static void adlib_write(void *opaque, uint32_t nport, uint32_t val)
|
2004-11-07 21:04:02 +03:00
|
|
|
{
|
|
|
|
AdlibState *s = opaque;
|
|
|
|
int a = nport & 3;
|
|
|
|
|
|
|
|
s->active = 1;
|
2005-10-30 21:58:22 +03:00
|
|
|
AUD_set_active_out (s->voice, 1);
|
2004-11-07 21:04:02 +03:00
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
adlib_kill_timers (s);
|
|
|
|
|
2011-09-18 18:27:23 +04:00
|
|
|
OPLWrite (s->opl, a, val);
|
2004-11-07 21:04:02 +03:00
|
|
|
}
|
|
|
|
|
2015-10-07 19:32:54 +03:00
|
|
|
static uint32_t adlib_read(void *opaque, uint32_t nport)
|
2004-11-07 21:04:02 +03:00
|
|
|
{
|
|
|
|
AdlibState *s = opaque;
|
|
|
|
int a = nport & 3;
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
adlib_kill_timers (s);
|
2020-04-01 19:53:14 +03:00
|
|
|
return OPLRead (s->opl, a);
|
2004-11-07 21:04:02 +03:00
|
|
|
}
|
|
|
|
|
2017-06-21 07:34:00 +03:00
|
|
|
static void timer_handler (void *opaque, int c, double interval_Sec)
|
2004-11-07 21:04:02 +03:00
|
|
|
{
|
2017-06-21 07:34:01 +03:00
|
|
|
AdlibState *s = opaque;
|
2005-10-30 21:58:22 +03:00
|
|
|
unsigned n = c & 1;
|
|
|
|
#ifdef DEBUG
|
|
|
|
double interval;
|
2005-11-05 21:55:28 +03:00
|
|
|
int64_t exp;
|
2004-11-07 21:04:02 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (interval_Sec == 0.0) {
|
2005-10-30 21:58:22 +03:00
|
|
|
s->ticking[n] = 0;
|
2004-11-07 21:04:02 +03:00
|
|
|
return;
|
|
|
|
}
|
2005-10-30 21:58:22 +03:00
|
|
|
|
|
|
|
s->ticking[n] = 1;
|
|
|
|
#ifdef DEBUG
|
2016-03-21 19:02:30 +03:00
|
|
|
interval = NANOSECONDS_PER_SECOND * interval_Sec;
|
2013-08-21 19:03:08 +04:00
|
|
|
exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval;
|
2005-10-30 21:58:22 +03: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 21:04:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int write_audio (AdlibState *s, int samples)
|
|
|
|
{
|
|
|
|
int net = 0;
|
2005-10-30 21:58:22 +03:00
|
|
|
int pos = s->pos;
|
|
|
|
|
2004-11-07 21:04:02 +03:00
|
|
|
while (samples) {
|
2005-10-30 21:58:22 +03: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 21:04:02 +03:00
|
|
|
break;
|
2005-10-30 21:58:22 +03:00
|
|
|
}
|
2004-11-07 21:04:02 +03:00
|
|
|
}
|
2005-10-30 21:58:22 +03:00
|
|
|
|
2004-11-07 21:04:02 +03:00
|
|
|
return net;
|
|
|
|
}
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
static void adlib_callback (void *opaque, int free)
|
2004-11-07 21:04:02 +03:00
|
|
|
{
|
|
|
|
AdlibState *s = opaque;
|
2021-07-12 22:57:41 +03:00
|
|
|
int samples, to_play, written;
|
2004-11-07 21:04:02 +03:00
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
samples = free >> SHIFT;
|
|
|
|
if (!(s->active && s->enabled) || !samples) {
|
|
|
|
return;
|
2004-11-07 21:04:02 +03:00
|
|
|
}
|
|
|
|
|
2019-08-19 02:06:54 +03:00
|
|
|
to_play = MIN (s->left, samples);
|
2005-10-30 21:58:22 +03:00
|
|
|
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 21:04:02 +03:00
|
|
|
|
2019-08-19 02:06:54 +03:00
|
|
|
samples = MIN (samples, s->samples - s->pos);
|
2005-10-30 21:58:22 +03:00
|
|
|
if (!samples) {
|
|
|
|
return;
|
|
|
|
}
|
2004-11-07 21:04:02 +03:00
|
|
|
|
|
|
|
YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
|
|
|
|
|
|
|
|
while (samples) {
|
2005-10-30 21:58:22 +03:00
|
|
|
written = write_audio (s, samples);
|
|
|
|
|
|
|
|
if (written) {
|
|
|
|
samples -= written;
|
|
|
|
s->pos = (s->pos + written) % s->samples;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s->left = samples;
|
|
|
|
return;
|
|
|
|
}
|
2004-11-07 21:04:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Adlib_fini (AdlibState *s)
|
|
|
|
{
|
|
|
|
if (s->opl) {
|
|
|
|
OPLDestroy (s->opl);
|
|
|
|
s->opl = NULL;
|
|
|
|
}
|
|
|
|
|
2014-06-06 20:35:13 +04:00
|
|
|
g_free(s->mixbuf);
|
2004-11-07 21:04:02 +03:00
|
|
|
|
|
|
|
s->active = 0;
|
|
|
|
s->enabled = 0;
|
2005-11-05 21:55:28 +03:00
|
|
|
AUD_remove_card (&s->card);
|
2004-11-07 21:04:02 +03:00
|
|
|
}
|
|
|
|
|
2013-06-22 10:06:54 +04: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 13:49:04 +04:00
|
|
|
{ 0x388, 4, 1, .read = adlib_read, .write = adlib_write, },
|
2013-06-22 10:06:54 +04:00
|
|
|
PORTIO_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2012-11-25 05:37:14 +04:00
|
|
|
static void adlib_realizefn (DeviceState *dev, Error **errp)
|
2004-11-07 21:04:02 +03:00
|
|
|
{
|
2013-04-18 20:43:57 +04:00
|
|
|
AdlibState *s = ADLIB(dev);
|
2008-12-04 01:48:44 +03:00
|
|
|
struct audsettings as;
|
2005-11-05 21:55:28 +03:00
|
|
|
|
2023-10-02 17:27:57 +03:00
|
|
|
if (!AUD_register_card ("adlib", &s->card, errp)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-26 01:37:26 +03:00
|
|
|
s->opl = OPLCreate (3579545, s->freq);
|
2004-11-07 21:04:02 +03:00
|
|
|
if (!s->opl) {
|
2012-11-25 05:37:14 +04:00
|
|
|
error_setg (errp, "OPLCreate %d failed", s->freq);
|
|
|
|
return;
|
2004-11-07 21:04:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2017-06-21 07:34:01 +03:00
|
|
|
OPLSetTimerHandler(s->opl, timer_handler, s);
|
2004-11-07 21:04:02 +03:00
|
|
|
s->enabled = 1;
|
|
|
|
}
|
|
|
|
|
2013-04-18 20:43:57 +04:00
|
|
|
as.freq = s->freq;
|
2005-11-05 21:55:28 +03:00
|
|
|
as.nchannels = SHIFT;
|
2019-03-09 01:34:13 +03:00
|
|
|
as.fmt = AUDIO_FORMAT_S16;
|
2006-07-05 01:47:22 +04:00
|
|
|
as.endianness = AUDIO_HOST_ENDIANNESS;
|
2005-11-05 21:55:28 +03:00
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
s->voice = AUD_open_out (
|
2005-11-05 21:55:28 +03:00
|
|
|
&s->card,
|
2005-10-30 21:58:22 +03:00
|
|
|
s->voice,
|
|
|
|
"adlib",
|
|
|
|
s,
|
|
|
|
adlib_callback,
|
2006-07-05 01:47:22 +04:00
|
|
|
&as
|
2005-10-30 21:58:22 +03:00
|
|
|
);
|
2004-11-07 21:04:02 +03:00
|
|
|
if (!s->voice) {
|
|
|
|
Adlib_fini (s);
|
2012-11-25 05:37:14 +04:00
|
|
|
error_setg (errp, "Initializing audio voice failed");
|
|
|
|
return;
|
2004-11-07 21:04:02 +03:00
|
|
|
}
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
|
2011-08-21 07:09:37 +04:00
|
|
|
s->mixbuf = g_malloc0 (s->samples << SHIFT);
|
2004-11-07 21:04:02 +03:00
|
|
|
|
2013-12-02 13:16:18 +04:00
|
|
|
adlib_portio_list[0].offset = s->port;
|
|
|
|
adlib_portio_list[1].offset = s->port + 8;
|
2014-04-29 17:38:39 +04: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 21:04:02 +03:00
|
|
|
}
|
2013-04-18 20:43:57 +04:00
|
|
|
|
|
|
|
static Property adlib_properties[] = {
|
2019-08-19 02:06:49 +03:00
|
|
|
DEFINE_AUDIO_PROPERTIES(AdlibState, card),
|
2014-02-08 14:01:53 +04:00
|
|
|
DEFINE_PROP_UINT32 ("iobase", AdlibState, port, 0x220),
|
2013-04-18 20:43:57 +04: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 05:37:14 +04:00
|
|
|
|
|
|
|
dc->realize = adlib_realizefn;
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
|
2013-04-18 20:43:57 +04:00
|
|
|
dc->desc = ADLIB_DESC;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, adlib_properties);
|
2013-04-18 20:43:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo adlib_info = {
|
|
|
|
.name = TYPE_ADLIB,
|
|
|
|
.parent = TYPE_ISA_DEVICE,
|
|
|
|
.instance_size = sizeof (AdlibState),
|
|
|
|
.class_init = adlib_class_initfn,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void adlib_register_types (void)
|
|
|
|
{
|
|
|
|
type_register_static (&adlib_info);
|
2020-07-02 16:25:11 +03:00
|
|
|
deprecated_register_soundhw("adlib", ADLIB_DESC, 1, TYPE_ADLIB);
|
2013-04-18 20:43:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init (adlib_register_types)
|