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
|
|
|
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/hw.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/audio/audio.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"
|
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
|
|
|
#ifdef HAS_YMF262
|
|
|
|
#define ADLIB_DESC "Yamaha YMF262 (OPL3)"
|
|
|
|
#else
|
|
|
|
#define ADLIB_DESC "Yamaha YM3812 (OPL2)"
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
#ifdef HAS_YMF262
|
2004-11-07 21:04:02 +03:00
|
|
|
#include "ymf262.h"
|
2005-10-30 21:58:22 +03:00
|
|
|
void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
|
2004-11-07 21:04:02 +03:00
|
|
|
#define SHIFT 2
|
|
|
|
#else
|
2013-03-18 20:36:02 +04:00
|
|
|
#include "fmopl.h"
|
2004-11-07 21:04:02 +03:00
|
|
|
#define SHIFT 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define IO_READ_PROTO(name) \
|
|
|
|
uint32_t name (void *opaque, uint32_t nport)
|
|
|
|
#define IO_WRITE_PROTO(name) \
|
|
|
|
void name (void *opaque, uint32_t nport, uint32_t val)
|
|
|
|
|
2013-04-18 20:43:57 +04:00
|
|
|
#define TYPE_ADLIB "adlib"
|
|
|
|
#define ADLIB(obj) OBJECT_CHECK(AdlibState, (obj), TYPE_ADLIB)
|
2004-11-07 21:04:02 +03:00
|
|
|
|
|
|
|
typedef struct {
|
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;
|
|
|
|
#ifndef HAS_YMF262
|
2004-11-07 21:04:02 +03:00
|
|
|
FM_OPL *opl;
|
|
|
|
#endif
|
|
|
|
} AdlibState;
|
|
|
|
|
2013-04-18 20:43:57 +04:00
|
|
|
static AdlibState *glob_adlib;
|
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)
|
|
|
|
{
|
|
|
|
#ifdef HAS_YMF262
|
|
|
|
YMF262TimerOver (0, n);
|
|
|
|
#else
|
|
|
|
OPLTimerOver (s->opl, n);
|
|
|
|
#endif
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-20 05:13:29 +04:00
|
|
|
static IO_WRITE_PROTO (adlib_write)
|
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);
|
|
|
|
|
|
|
|
#ifdef HAS_YMF262
|
2011-09-18 18:27:23 +04:00
|
|
|
YMF262Write (0, a, val);
|
2004-11-07 21:04:02 +03:00
|
|
|
#else
|
2011-09-18 18:27:23 +04:00
|
|
|
OPLWrite (s->opl, a, val);
|
2004-11-07 21:04:02 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-06-20 05:13:29 +04:00
|
|
|
static IO_READ_PROTO (adlib_read)
|
2004-11-07 21:04:02 +03:00
|
|
|
{
|
|
|
|
AdlibState *s = opaque;
|
|
|
|
uint8_t data;
|
|
|
|
int a = nport & 3;
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
adlib_kill_timers (s);
|
|
|
|
|
|
|
|
#ifdef HAS_YMF262
|
2004-11-07 21:04:02 +03:00
|
|
|
data = YMF262Read (0, a);
|
|
|
|
#else
|
|
|
|
data = OPLRead (s->opl, a);
|
|
|
|
#endif
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
static void timer_handler (int c, double interval_Sec)
|
2004-11-07 21:04:02 +03:00
|
|
|
{
|
2013-04-18 20:43:57 +04:00
|
|
|
AdlibState *s = glob_adlib;
|
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
|
2012-02-07 22:11:04 +04:00
|
|
|
interval = get_ticks_per_sec () * 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;
|
2005-10-30 21:58:22 +03:00
|
|
|
int samples, net = 0, 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
|
|
|
}
|
|
|
|
|
2005-10-30 21:58:22 +03: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 21:04:02 +03:00
|
|
|
|
|
|
|
samples = audio_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
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
#ifdef HAS_YMF262
|
2004-11-07 21:04:02 +03:00
|
|
|
YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
|
|
|
|
#else
|
|
|
|
YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
while (samples) {
|
2005-10-30 21:58:22 +03: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 21:04:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Adlib_fini (AdlibState *s)
|
|
|
|
{
|
2005-10-30 21:58:22 +03:00
|
|
|
#ifdef HAS_YMF262
|
2004-11-07 21:04:02 +03:00
|
|
|
YMF262Shutdown ();
|
|
|
|
#else
|
|
|
|
if (s->opl) {
|
|
|
|
OPLDestroy (s->opl);
|
|
|
|
s->opl = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
if (s->mixbuf) {
|
2011-08-21 07:09:37 +04:00
|
|
|
g_free (s->mixbuf);
|
2005-10-30 21:58:22 +03:00
|
|
|
}
|
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);
|
2013-06-22 10:06:54 +04:00
|
|
|
PortioList *port_list = g_new(PortioList, 1);
|
2008-12-04 01:48:44 +03:00
|
|
|
struct audsettings as;
|
2005-11-05 21:55:28 +03:00
|
|
|
|
2013-04-18 20:43:57 +04:00
|
|
|
if (glob_adlib) {
|
2012-11-25 05:37:14 +04:00
|
|
|
error_setg (errp, "Cannot create more than 1 adlib device");
|
|
|
|
return;
|
2013-04-18 20:43:57 +04:00
|
|
|
}
|
|
|
|
glob_adlib = s;
|
|
|
|
|
2005-10-30 21:58:22 +03:00
|
|
|
#ifdef HAS_YMF262
|
2013-04-18 20:43:57 +04:00
|
|
|
if (YMF262Init (1, 14318180, s->freq)) {
|
2012-11-25 05:37:14 +04:00
|
|
|
error_setg (errp, "YMF262Init %d failed", s->freq);
|
|
|
|
return;
|
2004-11-07 21:04:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2005-10-30 21:58:22 +03:00
|
|
|
YMF262SetTimerHandler (0, timer_handler, 0);
|
2004-11-07 21:04:02 +03:00
|
|
|
s->enabled = 1;
|
|
|
|
}
|
|
|
|
#else
|
2013-04-18 20:43:57 +04:00
|
|
|
s->opl = OPLCreate (OPL_TYPE_YM3812, 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 {
|
2005-10-30 21:58:22 +03:00
|
|
|
OPLSetTimerHandler (s->opl, timer_handler, 0);
|
2004-11-07 21:04:02 +03:00
|
|
|
s->enabled = 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-18 20:43:57 +04:00
|
|
|
as.freq = s->freq;
|
2005-11-05 21:55:28 +03:00
|
|
|
as.nchannels = SHIFT;
|
|
|
|
as.fmt = AUD_FMT_S16;
|
2006-07-05 01:47:22 +04:00
|
|
|
as.endianness = AUDIO_HOST_ENDIANNESS;
|
2005-11-05 21:55:28 +03:00
|
|
|
|
2009-05-14 03:11:35 +04:00
|
|
|
AUD_register_card ("adlib", &s->card);
|
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-06-22 10:06:54 +04:00
|
|
|
adlib_portio_list[1].offset = s->port;
|
|
|
|
adlib_portio_list[2].offset = s->port + 8;
|
2013-06-07 05:19:53 +04:00
|
|
|
portio_list_init (port_list, OBJECT(s), adlib_portio_list, s, "adlib");
|
2013-06-22 10:06:54 +04:00
|
|
|
portio_list_add (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[] = {
|
|
|
|
DEFINE_PROP_HEX32 ("iobase", AdlibState, port, 0x220),
|
|
|
|
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;
|
|
|
|
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 20:43:58 +04:00
|
|
|
static int Adlib_init (ISABus *bus)
|
2013-04-18 20:43:57 +04:00
|
|
|
{
|
|
|
|
isa_create_simple (bus, TYPE_ADLIB);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adlib_register_types (void)
|
|
|
|
{
|
|
|
|
type_register_static (&adlib_info);
|
2013-04-18 20:43:58 +04:00
|
|
|
isa_register_soundhw("adlib", ADLIB_DESC, Adlib_init);
|
2013-04-18 20:43:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type_init (adlib_register_types)
|