adlib.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. int status;
  108. s->active = 1;
  109. AUD_set_active_out (s->voice, 1);
  110. adlib_kill_timers (s);
  111. #ifdef HAS_YMF262
  112. status = YMF262Write (0, a, val);
  113. #else
  114. status = OPLWrite (s->opl, a, val);
  115. #endif
  116. }
  117. static IO_READ_PROTO (adlib_read)
  118. {
  119. AdlibState *s = opaque;
  120. uint8_t data;
  121. int a = nport & 3;
  122. adlib_kill_timers (s);
  123. #ifdef HAS_YMF262
  124. data = YMF262Read (0, a);
  125. #else
  126. data = OPLRead (s->opl, a);
  127. #endif
  128. return data;
  129. }
  130. static void timer_handler (int c, double interval_Sec)
  131. {
  132. AdlibState *s = &glob_adlib;
  133. unsigned n = c & 1;
  134. #ifdef DEBUG
  135. double interval;
  136. int64_t exp;
  137. #endif
  138. if (interval_Sec == 0.0) {
  139. s->ticking[n] = 0;
  140. return;
  141. }
  142. s->ticking[n] = 1;
  143. #ifdef DEBUG
  144. interval = get_ticks_per_sec() * interval_Sec;
  145. exp = qemu_get_clock_ns (vm_clock) + interval;
  146. s->exp[n] = exp;
  147. #endif
  148. s->dexp[n] = interval_Sec * 1000000.0;
  149. AUD_init_time_stamp_out (s->voice, &s->ats);
  150. }
  151. static int write_audio (AdlibState *s, int samples)
  152. {
  153. int net = 0;
  154. int pos = s->pos;
  155. while (samples) {
  156. int nbytes, wbytes, wsampl;
  157. nbytes = samples << SHIFT;
  158. wbytes = AUD_write (
  159. s->voice,
  160. s->mixbuf + (pos << (SHIFT - 1)),
  161. nbytes
  162. );
  163. if (wbytes) {
  164. wsampl = wbytes >> SHIFT;
  165. samples -= wsampl;
  166. pos = (pos + wsampl) % s->samples;
  167. net += wsampl;
  168. }
  169. else {
  170. break;
  171. }
  172. }
  173. return net;
  174. }
  175. static void adlib_callback (void *opaque, int free)
  176. {
  177. AdlibState *s = opaque;
  178. int samples, net = 0, to_play, written;
  179. samples = free >> SHIFT;
  180. if (!(s->active && s->enabled) || !samples) {
  181. return;
  182. }
  183. to_play = audio_MIN (s->left, samples);
  184. while (to_play) {
  185. written = write_audio (s, to_play);
  186. if (written) {
  187. s->left -= written;
  188. samples -= written;
  189. to_play -= written;
  190. s->pos = (s->pos + written) % s->samples;
  191. }
  192. else {
  193. return;
  194. }
  195. }
  196. samples = audio_MIN (samples, s->samples - s->pos);
  197. if (!samples) {
  198. return;
  199. }
  200. #ifdef HAS_YMF262
  201. YMF262UpdateOneQEMU (0, s->mixbuf + s->pos * 2, samples);
  202. #else
  203. YM3812UpdateOne (s->opl, s->mixbuf + s->pos, samples);
  204. #endif
  205. while (samples) {
  206. written = write_audio (s, samples);
  207. if (written) {
  208. net += written;
  209. samples -= written;
  210. s->pos = (s->pos + written) % s->samples;
  211. }
  212. else {
  213. s->left = samples;
  214. return;
  215. }
  216. }
  217. }
  218. static void Adlib_fini (AdlibState *s)
  219. {
  220. #ifdef HAS_YMF262
  221. YMF262Shutdown ();
  222. #else
  223. if (s->opl) {
  224. OPLDestroy (s->opl);
  225. s->opl = NULL;
  226. }
  227. #endif
  228. if (s->mixbuf) {
  229. qemu_free (s->mixbuf);
  230. }
  231. s->active = 0;
  232. s->enabled = 0;
  233. AUD_remove_card (&s->card);
  234. }
  235. int Adlib_init (qemu_irq *pic)
  236. {
  237. AdlibState *s = &glob_adlib;
  238. struct audsettings as;
  239. #ifdef HAS_YMF262
  240. if (YMF262Init (1, 14318180, conf.freq)) {
  241. dolog ("YMF262Init %d failed\n", conf.freq);
  242. return -1;
  243. }
  244. else {
  245. YMF262SetTimerHandler (0, timer_handler, 0);
  246. s->enabled = 1;
  247. }
  248. #else
  249. s->opl = OPLCreate (OPL_TYPE_YM3812, 3579545, conf.freq);
  250. if (!s->opl) {
  251. dolog ("OPLCreate %d failed\n", conf.freq);
  252. return -1;
  253. }
  254. else {
  255. OPLSetTimerHandler (s->opl, timer_handler, 0);
  256. s->enabled = 1;
  257. }
  258. #endif
  259. as.freq = conf.freq;
  260. as.nchannels = SHIFT;
  261. as.fmt = AUD_FMT_S16;
  262. as.endianness = AUDIO_HOST_ENDIANNESS;
  263. AUD_register_card ("adlib", &s->card);
  264. s->voice = AUD_open_out (
  265. &s->card,
  266. s->voice,
  267. "adlib",
  268. s,
  269. adlib_callback,
  270. &as
  271. );
  272. if (!s->voice) {
  273. Adlib_fini (s);
  274. return -1;
  275. }
  276. s->samples = AUD_get_buffer_size_out (s->voice) >> SHIFT;
  277. s->mixbuf = qemu_mallocz (s->samples << SHIFT);
  278. register_ioport_read (0x388, 4, 1, adlib_read, s);
  279. register_ioport_write (0x388, 4, 1, adlib_write, s);
  280. register_ioport_read (conf.port, 4, 1, adlib_read, s);
  281. register_ioport_write (conf.port, 4, 1, adlib_write, s);
  282. register_ioport_read (conf.port + 8, 2, 1, adlib_read, s);
  283. register_ioport_write (conf.port + 8, 2, 1, adlib_write, s);
  284. return 0;
  285. }