* THE SOFTWARE.
*/
+#include "qemu/osdep.h"
+#include "qapi/error.h"
#include "hw/hw.h"
#include "hw/audio/audio.h"
#include "audio/audio.h"
#define ADLIB_KILL_TIMERS 1
+#define ADLIB_DESC "Yamaha YM3812 (OPL2)"
+
#ifdef DEBUG
#include "qemu/timer.h"
#endif
#define ldebug(...)
#endif
-#ifdef HAS_YMF262
-#include "ymf262.h"
-void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
-#define SHIFT 2
-#else
#include "fmopl.h"
#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)
-
-static struct {
- int port;
- int freq;
-} conf = {0x220, 44100};
+#define TYPE_ADLIB "adlib"
+#define ADLIB(obj) OBJECT_CHECK(AdlibState, (obj), TYPE_ADLIB)
typedef struct {
+ ISADevice parent_obj;
+
QEMUSoundCard card;
+ uint32_t freq;
+ uint32_t port;
int ticking[2];
int enabled;
int active;
SWVoiceOut *voice;
int left, pos, samples;
QEMUAudioTimeStamp ats;
-#ifndef HAS_YMF262
FM_OPL *opl;
-#endif
+ PortioList port_list;
} AdlibState;
-static AdlibState glob_adlib;
+static AdlibState *glob_adlib;
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 IO_WRITE_PROTO (adlib_write)
+static void adlib_write(void *opaque, uint32_t nport, uint32_t val)
{
AdlibState *s = opaque;
int a = nport & 3;
adlib_kill_timers (s);
-#ifdef HAS_YMF262
- YMF262Write (0, a, val);
-#else
OPLWrite (s->opl, a, val);
-#endif
}
-static IO_READ_PROTO (adlib_read)
+static uint32_t adlib_read(void *opaque, uint32_t nport)
{
AdlibState *s = opaque;
uint8_t data;
int a = nport & 3;
adlib_kill_timers (s);
-
-#ifdef HAS_YMF262
- data = YMF262Read (0, a);
-#else
data = OPLRead (s->opl, a);
-#endif
+
return data;
}
static void timer_handler (int c, double interval_Sec)
{
- AdlibState *s = &glob_adlib;
+ AdlibState *s = glob_adlib;
unsigned n = c & 1;
#ifdef DEBUG
double interval;
s->ticking[n] = 1;
#ifdef DEBUG
- interval = get_ticks_per_sec () * interval_Sec;
- exp = qemu_get_clock_ns (vm_clock) + interval;
+ interval = NANOSECONDS_PER_SECOND * interval_Sec;
+ exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval;
s->exp[n] = exp;
#endif
return;
}
-#ifdef HAS_YMF262
- YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
-#else
YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
-#endif
while (samples) {
written = write_audio (s, samples);
static void Adlib_fini (AdlibState *s)
{
-#ifdef HAS_YMF262
- YMF262Shutdown ();
-#else
if (s->opl) {
OPLDestroy (s->opl);
s->opl = NULL;
}
-#endif
- if (s->mixbuf) {
- g_free (s->mixbuf);
- }
+ g_free(s->mixbuf);
s->active = 0;
s->enabled = 0;
AUD_remove_card (&s->card);
}
-int Adlib_init (ISABus *bus)
+static MemoryRegionPortio adlib_portio_list[] = {
+ { 0, 4, 1, .read = adlib_read, .write = adlib_write, },
+ { 0, 2, 1, .read = adlib_read, .write = adlib_write, },
+ { 0x388, 4, 1, .read = adlib_read, .write = adlib_write, },
+ PORTIO_END_OF_LIST(),
+};
+
+static void adlib_realizefn (DeviceState *dev, Error **errp)
{
- AdlibState *s = &glob_adlib;
+ AdlibState *s = ADLIB(dev);
struct audsettings as;
-#ifdef HAS_YMF262
- if (YMF262Init (1, 14318180, conf.freq)) {
- dolog ("YMF262Init %d failed\n", conf.freq);
- return -1;
- }
- else {
- YMF262SetTimerHandler (0, timer_handler, 0);
- s->enabled = 1;
+ if (glob_adlib) {
+ error_setg (errp, "Cannot create more than 1 adlib device");
+ return;
}
-#else
- s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
+ glob_adlib = s;
+
+ s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, s->freq);
if (!s->opl) {
- dolog ("OPLCreate %d failed\n", conf.freq);
- return -1;
+ error_setg (errp, "OPLCreate %d failed", s->freq);
+ return;
}
else {
OPLSetTimerHandler (s->opl, timer_handler, 0);
s->enabled = 1;
}
-#endif
- as.freq = conf.freq;
+ as.freq = s->freq;
as.nchannels = SHIFT;
as.fmt = AUD_FMT_S16;
as.endianness = AUDIO_HOST_ENDIANNESS;
);
if (!s->voice) {
Adlib_fini (s);
- return -1;
+ error_setg (errp, "Initializing audio voice failed");
+ return;
}
s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
s->mixbuf = g_malloc0 (s->samples << SHIFT);
- register_ioport_read (0x388, 4, 1, adlib_read, s);
- register_ioport_write (0x388, 4, 1, adlib_write, s);
+ adlib_portio_list[0].offset = s->port;
+ adlib_portio_list[1].offset = s->port + 8;
+ 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);
+}
- register_ioport_read (conf.port, 4, 1, adlib_read, s);
- register_ioport_write (conf.port, 4, 1, adlib_write, s);
+static Property adlib_properties[] = {
+ DEFINE_PROP_UINT32 ("iobase", AdlibState, port, 0x220),
+ DEFINE_PROP_UINT32 ("freq", AdlibState, freq, 44100),
+ DEFINE_PROP_END_OF_LIST (),
+};
- register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
- register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
+static void adlib_class_initfn (ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS (klass);
+
+ dc->realize = adlib_realizefn;
+ set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
+ 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,
+};
+
+static int Adlib_init (ISABus *bus)
+{
+ isa_create_simple (bus, TYPE_ADLIB);
return 0;
}
+
+static void adlib_register_types (void)
+{
+ type_register_static (&adlib_info);
+ isa_register_soundhw("adlib", ADLIB_DESC, Adlib_init);
+}
+
+type_init (adlib_register_types)