adlib.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "hw.h"
  25. #include "audiodev.h"
  26. #include "audio/audio.h"
  27. #include "isa.h"
  28. //#define DEBUG
  29. #define ADLIB_KILL_TIMERS 1
  30. #ifdef DEBUG
  31. #include "qemu-timer.h"
  32. #endif
  33. #define dolog(...) AUD_log ("adlib", __VA_ARGS__)
  34. #ifdef DEBUG
  35. #define ldebug(...) dolog (__VA_ARGS__)
  36. #else
  37. #define ldebug(...)
  38. #endif
  39. #ifdef HAS_YMF262
  40. #include "ymf262.h"
  41. void YMF262UpdateOneQEMU (int which, INT16 *dst, int length);
  42. #define SHIFT 2
  43. #else
  44. #include "fmopl.h"
  45. #define SHIFT 1
  46. #endif
  47. #define IO_READ_PROTO(name) \
  48. uint32_t name (void *opaque, uint32_t nport)
  49. #define IO_WRITE_PROTO(name) \
  50. void name (void *opaque, uint32_t nport, uint32_t val)
  51. static struct {
  52. int port;
  53. int freq;
  54. } conf = {0x220, 44100};
  55. typedef struct {
  56. QEMUSoundCard card;
  57. int ticking[2];
  58. int enabled;
  59. int active;
  60. int bufpos;
  61. #ifdef DEBUG
  62. int64_t exp[2];
  63. #endif
  64. int16_t *mixbuf;
  65. uint64_t dexp[2];
  66. SWVoiceOut *voice;
  67. int left, pos, samples;
  68. QEMUAudioTimeStamp ats;
  69. #ifndef HAS_YMF262
  70. FM_OPL *opl;
  71. #endif
  72. } AdlibState;
  73. static AdlibState glob_adlib;
  74. static void adlib_stop_opl_timer (AdlibState *s, size_t n)
  75. {
  76. #ifdef HAS_YMF262
  77. YMF262TimerOver (0, n);
  78. #else
  79. OPLTimerOver (s->opl, n);
  80. #endif
  81. s->ticking[n] = 0;
  82. }
  83. static void adlib_kill_timers (AdlibState *s)
  84. {
  85. size_t i;
  86. for (i = 0; i < 2; ++i) {
  87. if (s->ticking[i]) {
  88. uint64_t delta;
  89. delta = AUD_get_elapsed_usec_out (s->voice, &s->ats);
  90. ldebug (
  91. "delta = %f dexp = %f expired => %d\n",
  92. delta / 1000000.0,
  93. s->dexp[i] / 1000000.0,
  94. delta >= s->dexp[i]
  95. );
  96. if (ADLIB_KILL_TIMERS || delta >= s->dexp[i]) {
  97. adlib_stop_opl_timer (s, i);
  98. AUD_init_time_stamp_out (s->voice, &s->ats);
  99. }
  100. }
  101. }
  102. }
  103. static IO_WRITE_PROTO (adlib_write)
  104. {
  105. AdlibState *s = opaque;
  106. int a = nport & 3;
  107. s->active = 1;
  108. AUD_set_active_out (s->voice, 1);
  109. adlib_kill_timers (s);
  110. #ifdef HAS_YMF262
  111. YMF262Write (0, a, val);
  112. #else
  113. OPLWrite (s->opl, a, val);
  114. #endif
  115. }
  116. static IO_READ_PROTO (adlib_read)
  117. {
  118. AdlibState *s = opaque;
  119. uint8_t data;
  120. int a = nport & 3;
  121. adlib_kill_timers (s);
  122. #ifdef HAS_YMF262
  123. data = YMF262Read (0, a);
  124. #else
  125. data = OPLRead (s->opl, a);
  126. #endif
  127. return data;
  128. }
  129. static void timer_handler (int c, double interval_Sec)
  130. {
  131. AdlibState *s = &glob_adlib;
  132. unsigned n = c & 1;
  133. #ifdef DEBUG
  134. double interval;
  135. int64_t exp;
  136. #endif
  137. if (interval_Sec == 0.0) {
  138. s->ticking[n] = 0;
  139. return;
  140. }
  141. s->ticking[n] = 1;
  142. #ifdef DEBUG
  143. interval = get_ticks_per_sec() * interval_Sec;
  144. exp = qemu_get_clock_ns (vm_clock) + interval;
  145. s->exp[n] = exp;
  146. #endif
  147. s->dexp[n] = interval_Sec * 1000000.0;
  148. AUD_init_time_stamp_out (s->voice, &s->ats);
  149. }
  150. static int write_audio (AdlibState *s, int samples)
  151. {
  152. int net = 0;
  153. int pos = s->pos;
  154. while (samples) {
  155. int nbytes, wbytes, wsampl;
  156. nbytes = samples << SHIFT;
  157. wbytes = AUD_write (
  158. s->voice,
  159. s->mixbuf + (pos << (SHIFT - 1)),
  160. nbytes
  161. );
  162. if (wbytes) {
  163. wsampl = wbytes >> SHIFT;
  164. samples -= wsampl;
  165. pos = (pos + wsampl) % s->samples;
  166. net += wsampl;
  167. }
  168. else {
  169. break;
  170. }
  171. }
  172. return net;
  173. }
  174. static void adlib_callback (void *opaque, int free)
  175. {
  176. AdlibState *s = opaque;
  177. int samples, net = 0, to_play, written;
  178. samples = free >> SHIFT;
  179. if (!(s->active && s->enabled) || !samples) {
  180. return;
  181. }
  182. to_play = audio_MIN (s->left, samples);
  183. while (to_play) {
  184. written = write_audio (s, to_play);
  185. if (written) {
  186. s->left -= written;
  187. samples -= written;
  188. to_play -= written;
  189. s->pos = (s->pos + written) % s->samples;
  190. }
  191. else {
  192. return;
  193. }
  194. }
  195. samples = audio_MIN (samples, s->samples - s->pos);
  196. if (!samples) {
  197. return;
  198. }
  199. #ifdef HAS_YMF262
  200. YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
  201. #else
  202. YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
  203. #endif
  204. while (samples) {
  205. written = write_audio (s, samples);
  206. if (written) {
  207. net += written;
  208. samples -= written;
  209. s->pos = (s->pos + written) % s->samples;
  210. }
  211. else {
  212. s->left = samples;
  213. return;
  214. }
  215. }
  216. }
  217. static void Adlib_fini (AdlibState *s)
  218. {
  219. #ifdef HAS_YMF262
  220. YMF262Shutdown ();
  221. #else
  222. if (s->opl) {
  223. OPLDestroy (s->opl);
  224. s->opl = NULL;
  225. }
  226. #endif
  227. if (s->mixbuf) {
  228. g_free (s->mixbuf);
  229. }
  230. s->active = 0;
  231. s->enabled = 0;
  232. AUD_remove_card (&s->card);
  233. }
  234. int Adlib_init (qemu_irq *pic)
  235. {
  236. AdlibState *s = &glob_adlib;
  237. struct audsettings as;
  238. #ifdef HAS_YMF262
  239. if (YMF262Init (1, 14318180, conf.freq)) {
  240. dolog ("YMF262Init %d failed\n", conf.freq);
  241. return -1;
  242. }
  243. else {
  244. YMF262SetTimerHandler (0, timer_handler, 0);
  245. s->enabled = 1;
  246. }
  247. #else
  248. s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
  249. if (!s->opl) {
  250. dolog ("OPLCreate %d failed\n", conf.freq);
  251. return -1;
  252. }
  253. else {
  254. OPLSetTimerHandler (s->opl, timer_handler, 0);
  255. s->enabled = 1;
  256. }
  257. #endif
  258. as.freq = conf.freq;
  259. as.nchannels = SHIFT;
  260. as.fmt = AUD_FMT_S16;
  261. as.endianness = AUDIO_HOST_ENDIANNESS;
  262. AUD_register_card ("adlib", &s->card);
  263. s->voice = AUD_open_out (
  264. &s->card,
  265. s->voice,
  266. "adlib",
  267. s,
  268. adlib_callback,
  269. &as
  270. );
  271. if (!s->voice) {
  272. Adlib_fini (s);
  273. return -1;
  274. }
  275. s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
  276. s->mixbuf = g_malloc0 (s->samples << SHIFT);
  277. register_ioport_read (0x388, 4, 1, adlib_read, s);
  278. register_ioport_write (0x388, 4, 1, adlib_write, s);
  279. register_ioport_read (conf.port, 4, 1, adlib_read, s);
  280. register_ioport_write (conf.port, 4, 1, adlib_write, s);
  281. register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
  282. register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
  283. return 0;
  284. }