audio: Rename audio_init() to soundhw_init()
[qemu.git] / hw / audio / adlib.c
CommitLineData
85571bc7 1/*
1d14ffa9
FB
2 * QEMU Proxy for OPL2/3 emulation by MAME team
3 *
4 * Copyright (c) 2004-2005 Vassili Karpov (malc)
5 *
85571bc7
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
9f0683d9 24
6086a565 25#include "qemu/osdep.h"
da34e65c 26#include "qapi/error.h"
83c9f4ca 27#include "hw/hw.h"
0d09e41a 28#include "hw/audio/audio.h"
e140e05c 29#include "audio/audio.h"
0d09e41a 30#include "hw/isa/isa.h"
85571bc7 31
9f0683d9
TS
32//#define DEBUG
33
1d14ffa9
FB
34#define ADLIB_KILL_TIMERS 1
35
8c444a19 36#define ADLIB_DESC "Yamaha YM3812 (OPL2)"
8c444a19 37
9f0683d9 38#ifdef DEBUG
1de7afc9 39#include "qemu/timer.h"
9f0683d9
TS
40#endif
41
fb065187
FB
42#define dolog(...) AUD_log ("adlib", __VA_ARGS__)
43#ifdef DEBUG
44#define ldebug(...) dolog (__VA_ARGS__)
45#else
46#define ldebug(...)
47#endif
85571bc7 48
47b43a1f 49#include "fmopl.h"
85571bc7 50#define SHIFT 1
85571bc7 51
8c444a19
PB
52#define TYPE_ADLIB "adlib"
53#define ADLIB(obj) OBJECT_CHECK(AdlibState, (obj), TYPE_ADLIB)
85571bc7
FB
54
55typedef struct {
8c444a19
PB
56 ISADevice parent_obj;
57
c0fe3827 58 QEMUSoundCard card;
8c444a19
PB
59 uint32_t freq;
60 uint32_t port;
1d14ffa9 61 int ticking[2];
85571bc7
FB
62 int enabled;
63 int active;
85571bc7 64 int bufpos;
1d14ffa9
FB
65#ifdef DEBUG
66 int64_t exp[2];
67#endif
85571bc7 68 int16_t *mixbuf;
1d14ffa9
FB
69 uint64_t dexp[2];
70 SWVoiceOut *voice;
71 int left, pos, samples;
72 QEMUAudioTimeStamp ats;
85571bc7 73 FM_OPL *opl;
848696bf 74 PortioList port_list;
85571bc7
FB
75} AdlibState;
76
8c444a19 77static AdlibState *glob_adlib;
85571bc7 78
1d14ffa9
FB
79static void adlib_stop_opl_timer (AdlibState *s, size_t n)
80{
1d14ffa9 81 OPLTimerOver (s->opl, n);
1d14ffa9
FB
82 s->ticking[n] = 0;
83}
84
85static void adlib_kill_timers (AdlibState *s)
86{
87 size_t i;
88
89 for (i = 0; i < 2; ++i) {
90 if (s->ticking[i]) {
91 uint64_t delta;
92
c0fe3827 93 delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
1d14ffa9
FB
94 ldebug (
95 "delta = %f dexp = %f expired => %d\n",
96 delta / 1000000.0,
97 s->dexp[i] / 1000000.0,
98 delta >= s->dexp[i]
99 );
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);
103 }
104 }
105 }
106}
107
8307c294 108static void adlib_write(void *opaque, uint32_t nport, uint32_t val)
85571bc7
FB
109{
110 AdlibState *s = opaque;
111 int a = nport & 3;
85571bc7 112
85571bc7 113 s->active = 1;
1d14ffa9 114 AUD_set_active_out (s->voice, 1);
85571bc7 115
1d14ffa9
FB
116 adlib_kill_timers (s);
117
e8beeae4 118 OPLWrite (s->opl, a, val);
85571bc7
FB
119}
120
8307c294 121static uint32_t adlib_read(void *opaque, uint32_t nport)
85571bc7
FB
122{
123 AdlibState *s = opaque;
124 uint8_t data;
125 int a = nport & 3;
126
1d14ffa9 127 adlib_kill_timers (s);
85571bc7 128 data = OPLRead (s->opl, a);
68883a40 129
85571bc7
FB
130 return data;
131}
132
1d14ffa9 133static void timer_handler (int c, double interval_Sec)
85571bc7 134{
8c444a19 135 AdlibState *s = glob_adlib;
1d14ffa9
FB
136 unsigned n = c & 1;
137#ifdef DEBUG
138 double interval;
c0fe3827 139 int64_t exp;
85571bc7 140#endif
85571bc7 141
85571bc7 142 if (interval_Sec == 0.0) {
1d14ffa9 143 s->ticking[n] = 0;
85571bc7
FB
144 return;
145 }
1d14ffa9
FB
146
147 s->ticking[n] = 1;
148#ifdef DEBUG
73bcb24d 149 interval = NANOSECONDS_PER_SECOND * interval_Sec;
bc72ad67 150 exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval;
1d14ffa9
FB
151 s->exp[n] = exp;
152#endif
153
154 s->dexp[n] = interval_Sec * 1000000.0;
155 AUD_init_time_stamp_out (s->voice, &s->ats);
85571bc7
FB
156}
157
158static int write_audio (AdlibState *s, int samples)
159{
160 int net = 0;
1d14ffa9
FB
161 int pos = s->pos;
162
85571bc7 163 while (samples) {
1d14ffa9
FB
164 int nbytes, wbytes, wsampl;
165
166 nbytes = samples << SHIFT;
167 wbytes = AUD_write (
168 s->voice,
169 s->mixbuf + (pos << (SHIFT - 1)),
170 nbytes
171 );
172
173 if (wbytes) {
174 wsampl = wbytes >> SHIFT;
175
176 samples -= wsampl;
177 pos = (pos + wsampl) % s->samples;
178
179 net += wsampl;
180 }
181 else {
85571bc7 182 break;
1d14ffa9 183 }
85571bc7 184 }
1d14ffa9 185
85571bc7
FB
186 return net;
187}
188
1d14ffa9 189static void adlib_callback (void *opaque, int free)
85571bc7
FB
190{
191 AdlibState *s = opaque;
1d14ffa9 192 int samples, net = 0, to_play, written;
85571bc7 193
1d14ffa9
FB
194 samples = free >> SHIFT;
195 if (!(s->active && s->enabled) || !samples) {
196 return;
85571bc7 197 }
85571bc7 198
1d14ffa9
FB
199 to_play = audio_MIN (s->left, samples);
200 while (to_play) {
201 written = write_audio (s, to_play);
202
203 if (written) {
204 s->left -= written;
205 samples -= written;
206 to_play -= written;
207 s->pos = (s->pos + written) % s->samples;
208 }
209 else {
210 return;
211 }
212 }
85571bc7
FB
213
214 samples = audio_MIN (samples, s->samples - s->pos);
1d14ffa9
FB
215 if (!samples) {
216 return;
217 }
85571bc7 218
85571bc7 219 YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
85571bc7
FB
220
221 while (samples) {
1d14ffa9
FB
222 written = write_audio (s, samples);
223
224 if (written) {
225 net += written;
226 samples -= written;
227 s->pos = (s->pos + written) % s->samples;
228 }
229 else {
230 s->left = samples;
231 return;
232 }
85571bc7 233 }
85571bc7
FB
234}
235
236static void Adlib_fini (AdlibState *s)
237{
85571bc7
FB
238 if (s->opl) {
239 OPLDestroy (s->opl);
240 s->opl = NULL;
241 }
85571bc7 242
fb7da626 243 g_free(s->mixbuf);
85571bc7
FB
244
245 s->active = 0;
246 s->enabled = 0;
c0fe3827 247 AUD_remove_card (&s->card);
85571bc7
FB
248}
249
a8aec295 250static MemoryRegionPortio adlib_portio_list[] = {
a8aec295
JK
251 { 0, 4, 1, .read = adlib_read, .write = adlib_write, },
252 { 0, 2, 1, .read = adlib_read, .write = adlib_write, },
2b21fb57 253 { 0x388, 4, 1, .read = adlib_read, .write = adlib_write, },
a8aec295
JK
254 PORTIO_END_OF_LIST(),
255};
256
db895a1e 257static void adlib_realizefn (DeviceState *dev, Error **errp)
85571bc7 258{
8c444a19 259 AdlibState *s = ADLIB(dev);
1ea879e5 260 struct audsettings as;
c0fe3827 261
8c444a19 262 if (glob_adlib) {
db895a1e
AF
263 error_setg (errp, "Cannot create more than 1 adlib device");
264 return;
8c444a19
PB
265 }
266 glob_adlib = s;
267
8f7e2c2c 268 s->opl = OPLCreate (3579545, s->freq);
85571bc7 269 if (!s->opl) {
db895a1e
AF
270 error_setg (errp, "OPLCreate %d failed", s->freq);
271 return;
85571bc7
FB
272 }
273 else {
1d14ffa9 274 OPLSetTimerHandler (s->opl, timer_handler, 0);
85571bc7
FB
275 s->enabled = 1;
276 }
85571bc7 277
8c444a19 278 as.freq = s->freq;
c0fe3827
FB
279 as.nchannels = SHIFT;
280 as.fmt = AUD_FMT_S16;
d929eba5 281 as.endianness = AUDIO_HOST_ENDIANNESS;
c0fe3827 282
1a7dafce 283 AUD_register_card ("adlib", &s->card);
c0fe3827 284
1d14ffa9 285 s->voice = AUD_open_out (
c0fe3827 286 &s->card,
1d14ffa9
FB
287 s->voice,
288 "adlib",
289 s,
290 adlib_callback,
d929eba5 291 &as
1d14ffa9 292 );
85571bc7
FB
293 if (!s->voice) {
294 Adlib_fini (s);
db895a1e
AF
295 error_setg (errp, "Initializing audio voice failed");
296 return;
85571bc7
FB
297 }
298
1d14ffa9 299 s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
7267c094 300 s->mixbuf = g_malloc0 (s->samples << SHIFT);
85571bc7 301
7f0ba7bb
PB
302 adlib_portio_list[0].offset = s->port;
303 adlib_portio_list[1].offset = s->port + 8;
848696bf
KB
304 portio_list_init (&s->port_list, OBJECT(s), adlib_portio_list, s, "adlib");
305 portio_list_add (&s->port_list, isa_address_space_io(&s->parent_obj), 0);
85571bc7 306}
8c444a19
PB
307
308static Property adlib_properties[] = {
c7bcc85d 309 DEFINE_PROP_UINT32 ("iobase", AdlibState, port, 0x220),
8c444a19
PB
310 DEFINE_PROP_UINT32 ("freq", AdlibState, freq, 44100),
311 DEFINE_PROP_END_OF_LIST (),
312};
313
314static void adlib_class_initfn (ObjectClass *klass, void *data)
315{
316 DeviceClass *dc = DEVICE_CLASS (klass);
db895a1e
AF
317
318 dc->realize = adlib_realizefn;
125ee0ed 319 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
8c444a19
PB
320 dc->desc = ADLIB_DESC;
321 dc->props = adlib_properties;
322}
323
324static const TypeInfo adlib_info = {
325 .name = TYPE_ADLIB,
326 .parent = TYPE_ISA_DEVICE,
327 .instance_size = sizeof (AdlibState),
328 .class_init = adlib_class_initfn,
329};
330
36cd6f6f 331static int Adlib_init (ISABus *bus)
8c444a19
PB
332{
333 isa_create_simple (bus, TYPE_ADLIB);
334 return 0;
335}
336
337static void adlib_register_types (void)
338{
339 type_register_static (&adlib_info);
36cd6f6f 340 isa_register_soundhw("adlib", ADLIB_DESC, Adlib_init);
8c444a19
PB
341}
342
343type_init (adlib_register_types)
This page took 0.757592 seconds and 4 git commands to generate.