25
#include "qemu/osdep.h"
26
#include "qapi/error.h"
27
#include "qemu/module.h"
28
#include "hw/audio/soundhw.h"
29
#include "audio/audio.h"
30
#include "hw/isa/isa.h"
31
#include "hw/qdev-properties.h"
32
#include "qom/object.h"
36
#define ADLIB_KILL_TIMERS 1
38
#define ADLIB_DESC "Yamaha YM3812 (OPL2)"
41
#include "qemu/timer.h"
44
#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
46
#define ldebug(...) dolog (__VA_ARGS__)
54
#define TYPE_ADLIB "adlib"
55
OBJECT_DECLARE_SIMPLE_TYPE(AdlibState, ADLIB)
73
int left, pos, samples;
74
QEMUAudioTimeStamp ats;
79
static void adlib_stop_opl_timer (AdlibState *s, size_t n)
81
OPLTimerOver (s->opl, n);
85
static void adlib_kill_timers (AdlibState *s)
89
for (i = 0; i < 2; ++i) {
93
delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
95
"delta = %f dexp = %f expired => %d\n",
97
s->dexp[i] / 1000000.0,
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);
108
static void adlib_write(void *opaque, uint32_t nport, uint32_t val)
110
AdlibState *s = opaque;
114
AUD_set_active_out (s->voice, 1);
116
adlib_kill_timers (s);
118
OPLWrite (s->opl, a, val);
121
static uint32_t adlib_read(void *opaque, uint32_t nport)
123
AdlibState *s = opaque;
126
adlib_kill_timers (s);
127
return OPLRead (s->opl, a);
130
static void timer_handler (void *opaque, int c, double interval_Sec)
132
AdlibState *s = opaque;
139
if (interval_Sec == 0.0) {
146
interval = NANOSECONDS_PER_SECOND * interval_Sec;
147
exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval;
151
s->dexp[n] = interval_Sec * 1000000.0;
152
AUD_init_time_stamp_out (s->voice, &s->ats);
155
static int write_audio (AdlibState *s, int samples)
161
int nbytes, wbytes, wsampl;
163
nbytes = samples << SHIFT;
166
s->mixbuf + (pos << (SHIFT - 1)),
171
wsampl = wbytes >> SHIFT;
174
pos = (pos + wsampl) % s->samples;
186
static void adlib_callback (void *opaque, int free)
188
AdlibState *s = opaque;
189
int samples, to_play, written;
191
samples = free >> SHIFT;
192
if (!(s->active && s->enabled) || !samples) {
196
to_play = MIN (s->left, samples);
198
written = write_audio (s, to_play);
204
s->pos = (s->pos + written) % s->samples;
211
samples = MIN (samples, s->samples - s->pos);
216
YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
219
written = write_audio (s, samples);
223
s->pos = (s->pos + written) % s->samples;
232
static void Adlib_fini (AdlibState *s)
243
AUD_remove_card (&s->card);
246
static MemoryRegionPortio adlib_portio_list[] = {
247
{ 0, 4, 1, .read = adlib_read, .write = adlib_write, },
248
{ 0, 2, 1, .read = adlib_read, .write = adlib_write, },
249
{ 0x388, 4, 1, .read = adlib_read, .write = adlib_write, },
250
PORTIO_END_OF_LIST(),
253
static void adlib_realizefn (DeviceState *dev, Error **errp)
255
AdlibState *s = ADLIB(dev);
256
struct audsettings as;
258
if (!AUD_register_card ("adlib", &s->card, errp)) {
262
s->opl = OPLCreate (3579545, s->freq);
264
error_setg (errp, "OPLCreate %d failed", s->freq);
268
OPLSetTimerHandler(s->opl, timer_handler, s);
273
as.nchannels = SHIFT;
274
as.fmt = AUDIO_FORMAT_S16;
275
as.endianness = AUDIO_HOST_ENDIANNESS;
277
s->voice = AUD_open_out (
287
error_setg (errp, "Initializing audio voice failed");
291
s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
292
s->mixbuf = g_malloc0 (s->samples << SHIFT);
294
adlib_portio_list[0].offset = s->port;
295
adlib_portio_list[1].offset = s->port + 8;
296
portio_list_init (&s->port_list, OBJECT(s), adlib_portio_list, s, "adlib");
297
portio_list_add (&s->port_list, isa_address_space_io(&s->parent_obj), 0);
300
static Property adlib_properties[] = {
301
DEFINE_AUDIO_PROPERTIES(AdlibState, card),
302
DEFINE_PROP_UINT32 ("iobase", AdlibState, port, 0x220),
303
DEFINE_PROP_UINT32 ("freq", AdlibState, freq, 44100),
304
DEFINE_PROP_END_OF_LIST (),
307
static void adlib_class_initfn (ObjectClass *klass, void *data)
309
DeviceClass *dc = DEVICE_CLASS (klass);
311
dc->realize = adlib_realizefn;
312
set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
313
dc->desc = ADLIB_DESC;
314
device_class_set_props(dc, adlib_properties);
317
static const TypeInfo adlib_info = {
319
.parent = TYPE_ISA_DEVICE,
320
.instance_size = sizeof (AdlibState),
321
.class_init = adlib_class_initfn,
324
static void adlib_register_types (void)
326
type_register_static (&adlib_info);
327
deprecated_register_soundhw("adlib", ADLIB_DESC, 1, TYPE_ADLIB);
330
type_init (adlib_register_types)