adlib.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * QEMU Proxy for OPL2/3 emulation by MAME team
  3. *
  4. * Copyright (c) 2004-2005 Vassili Karpov (malc)
  5. *
  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. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "qemu/module.h"
  27. #include "hw/audio/soundhw.h"
  28. #include "audio/audio.h"
  29. #include "hw/isa/isa.h"
  30. #include "hw/qdev-properties.h"
  31. #include "qom/object.h"
  32. //#define DEBUG
  33. #define ADLIB_KILL_TIMERS 1
  34. #define ADLIB_DESC "Yamaha YM3812 (OPL2)"
  35. #ifdef DEBUG
  36. #include "qemu/timer.h"
  37. #endif
  38. #define dolog(...) AUD_log ("adlib", __VA_ARGS__)
  39. #ifdef DEBUG
  40. #define ldebug(...) dolog (__VA_ARGS__)
  41. #else
  42. #define ldebug(...)
  43. #endif
  44. #include "fmopl.h"
  45. #define SHIFT 1
  46. #define TYPE_ADLIB "adlib"
  47. OBJECT_DECLARE_SIMPLE_TYPE(AdlibState, ADLIB)
  48. struct AdlibState {
  49. ISADevice parent_obj;
  50. QEMUSoundCard card;
  51. uint32_t freq;
  52. uint32_t port;
  53. int ticking[2];
  54. int enabled;
  55. int active;
  56. int bufpos;
  57. #ifdef DEBUG
  58. int64_t exp[2];
  59. #endif
  60. int16_t *mixbuf;
  61. uint64_t dexp[2];
  62. SWVoiceOut *voice;
  63. int left, pos, samples;
  64. QEMUAudioTimeStamp ats;
  65. FM_OPL *opl;
  66. PortioList port_list;
  67. };
  68. static void adlib_stop_opl_timer (AdlibState *s, size_t n)
  69. {
  70. OPLTimerOver (s->opl, n);
  71. s->ticking[n] = 0;
  72. }
  73. static void adlib_kill_timers (AdlibState *s)
  74. {
  75. size_t i;
  76. for (i = 0; i < 2; ++i) {
  77. if (s->ticking[i]) {
  78. uint64_t delta;
  79. delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
  80. ldebug (
  81. "delta = %f dexp = %f expired => %d\n",
  82. delta / 1000000.0,
  83. s->dexp[i] / 1000000.0,
  84. delta >= s->dexp[i]
  85. );
  86. if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
  87. adlib_stop_opl_timer (s, i);
  88. AUD_init_time_stamp_out (s->voice, &s->ats);
  89. }
  90. }
  91. }
  92. }
  93. static void adlib_write(void *opaque, uint32_t nport, uint32_t val)
  94. {
  95. AdlibState *s = opaque;
  96. int a = nport & 3;
  97. s->active = 1;
  98. AUD_set_active_out (s->voice, 1);
  99. adlib_kill_timers (s);
  100. OPLWrite (s->opl, a, val);
  101. }
  102. static uint32_t adlib_read(void *opaque, uint32_t nport)
  103. {
  104. AdlibState *s = opaque;
  105. int a = nport & 3;
  106. adlib_kill_timers (s);
  107. return OPLRead (s->opl, a);
  108. }
  109. static void timer_handler (void *opaque, int c, double interval_Sec)
  110. {
  111. AdlibState *s = opaque;
  112. unsigned n = c & 1;
  113. #ifdef DEBUG
  114. double interval;
  115. int64_t exp;
  116. #endif
  117. if (interval_Sec == 0.0) {
  118. s->ticking[n] = 0;
  119. return;
  120. }
  121. s->ticking[n] = 1;
  122. #ifdef DEBUG
  123. interval = NANOSECONDS_PER_SECOND * interval_Sec;
  124. exp = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + interval;
  125. s->exp[n] = exp;
  126. #endif
  127. s->dexp[n] = interval_Sec * 1000000.0;
  128. AUD_init_time_stamp_out (s->voice, &s->ats);
  129. }
  130. static int write_audio (AdlibState *s, int samples)
  131. {
  132. int net = 0;
  133. int pos = s->pos;
  134. while (samples) {
  135. int nbytes, wbytes, wsampl;
  136. nbytes = samples << SHIFT;
  137. wbytes = AUD_write (
  138. s->voice,
  139. s->mixbuf + (pos << (SHIFT - 1)),
  140. nbytes
  141. );
  142. if (wbytes) {
  143. wsampl = wbytes >> SHIFT;
  144. samples -= wsampl;
  145. pos = (pos + wsampl) % s->samples;
  146. net += wsampl;
  147. }
  148. else {
  149. break;
  150. }
  151. }
  152. return net;
  153. }
  154. static void adlib_callback (void *opaque, int free)
  155. {
  156. AdlibState *s = opaque;
  157. int samples, net = 0, to_play, written;
  158. samples = free >> SHIFT;
  159. if (!(s->active && s->enabled) || !samples) {
  160. return;
  161. }
  162. to_play = MIN (s->left, samples);
  163. while (to_play) {
  164. written = write_audio (s, to_play);
  165. if (written) {
  166. s->left -= written;
  167. samples -= written;
  168. to_play -= written;
  169. s->pos = (s->pos + written) % s->samples;
  170. }
  171. else {
  172. return;
  173. }
  174. }
  175. samples = MIN (samples, s->samples - s->pos);
  176. if (!samples) {
  177. return;
  178. }
  179. YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
  180. while (samples) {
  181. written = write_audio (s, samples);
  182. if (written) {
  183. net += written;
  184. samples -= written;
  185. s->pos = (s->pos + written) % s->samples;
  186. }
  187. else {
  188. s->left = samples;
  189. return;
  190. }
  191. }
  192. }
  193. static void Adlib_fini (AdlibState *s)
  194. {
  195. if (s->opl) {
  196. OPLDestroy (s->opl);
  197. s->opl = NULL;
  198. }
  199. g_free(s->mixbuf);
  200. s->active = 0;
  201. s->enabled = 0;
  202. AUD_remove_card (&s->card);
  203. }
  204. static MemoryRegionPortio adlib_portio_list[] = {
  205. { 0, 4, 1, .read = adlib_read, .write = adlib_write, },
  206. { 0, 2, 1, .read = adlib_read, .write = adlib_write, },
  207. { 0x388, 4, 1, .read = adlib_read, .write = adlib_write, },
  208. PORTIO_END_OF_LIST(),
  209. };
  210. static void adlib_realizefn (DeviceState *dev, Error **errp)
  211. {
  212. AdlibState *s = ADLIB(dev);
  213. struct audsettings as;
  214. s->opl = OPLCreate (3579545, s->freq);
  215. if (!s->opl) {
  216. error_setg (errp, "OPLCreate %d failed", s->freq);
  217. return;
  218. }
  219. else {
  220. OPLSetTimerHandler(s->opl, timer_handler, s);
  221. s->enabled = 1;
  222. }
  223. as.freq = s->freq;
  224. as.nchannels = SHIFT;
  225. as.fmt = AUDIO_FORMAT_S16;
  226. as.endianness = AUDIO_HOST_ENDIANNESS;
  227. AUD_register_card ("adlib", &s->card);
  228. s->voice = AUD_open_out (
  229. &s->card,
  230. s->voice,
  231. "adlib",
  232. s,
  233. adlib_callback,
  234. &as
  235. );
  236. if (!s->voice) {
  237. Adlib_fini (s);
  238. error_setg (errp, "Initializing audio voice failed");
  239. return;
  240. }
  241. s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
  242. s->mixbuf = g_malloc0 (s->samples << SHIFT);
  243. adlib_portio_list[0].offset = s->port;
  244. adlib_portio_list[1].offset = s->port + 8;
  245. portio_list_init (&s->port_list, OBJECT(s), adlib_portio_list, s, "adlib");
  246. portio_list_add (&s->port_list, isa_address_space_io(&s->parent_obj), 0);
  247. }
  248. static Property adlib_properties[] = {
  249. DEFINE_AUDIO_PROPERTIES(AdlibState, card),
  250. DEFINE_PROP_UINT32 ("iobase", AdlibState, port, 0x220),
  251. DEFINE_PROP_UINT32 ("freq", AdlibState, freq, 44100),
  252. DEFINE_PROP_END_OF_LIST (),
  253. };
  254. static void adlib_class_initfn (ObjectClass *klass, void *data)
  255. {
  256. DeviceClass *dc = DEVICE_CLASS (klass);
  257. dc->realize = adlib_realizefn;
  258. set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
  259. dc->desc = ADLIB_DESC;
  260. device_class_set_props(dc, adlib_properties);
  261. }
  262. static const TypeInfo adlib_info = {
  263. .name = TYPE_ADLIB,
  264. .parent = TYPE_ISA_DEVICE,
  265. .instance_size = sizeof (AdlibState),
  266. .class_init = adlib_class_initfn,
  267. };
  268. static void adlib_register_types (void)
  269. {
  270. type_register_static (&adlib_info);
  271. deprecated_register_soundhw("adlib", ADLIB_DESC, 1, TYPE_ADLIB);
  272. }
  273. type_init (adlib_register_types)