pcspk.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * QEMU PC speaker emulation
  3. *
  4. * Copyright (c) 2006 Joachim Henke
  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 "hw/isa/isa.h"
  26. #include "hw/audio/soundhw.h"
  27. #include "audio/audio.h"
  28. #include "qemu/module.h"
  29. #include "qemu/timer.h"
  30. #include "qemu/error-report.h"
  31. #include "hw/timer/i8254.h"
  32. #include "migration/vmstate.h"
  33. #include "hw/audio/pcspk.h"
  34. #include "qapi/error.h"
  35. #include "qom/object.h"
  36. #define PCSPK_BUF_LEN 1792
  37. #define PCSPK_SAMPLE_RATE 32000
  38. #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
  39. #define PCSPK_MIN_COUNT DIV_ROUND_UP(PIT_FREQ, PCSPK_MAX_FREQ)
  40. OBJECT_DECLARE_SIMPLE_TYPE(PCSpkState, PC_SPEAKER)
  41. struct PCSpkState {
  42. ISADevice parent_obj;
  43. MemoryRegion ioport;
  44. uint32_t iobase;
  45. uint8_t sample_buf[PCSPK_BUF_LEN];
  46. QEMUSoundCard card;
  47. SWVoiceOut *voice;
  48. void *pit;
  49. unsigned int pit_count;
  50. unsigned int samples;
  51. unsigned int play_pos;
  52. uint8_t data_on;
  53. uint8_t dummy_refresh_clock;
  54. bool migrate;
  55. };
  56. static const char *s_spk = "pcspk";
  57. static PCSpkState *pcspk_state;
  58. static inline void generate_samples(PCSpkState *s)
  59. {
  60. unsigned int i;
  61. if (s->pit_count) {
  62. const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
  63. const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
  64. /* multiple of wavelength for gapless looping */
  65. s->samples = (QEMU_ALIGN_DOWN(PCSPK_BUF_LEN * PIT_FREQ, m) / (PIT_FREQ >> 1) + 1) >> 1;
  66. for (i = 0; i < s->samples; ++i)
  67. s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
  68. } else {
  69. s->samples = PCSPK_BUF_LEN;
  70. for (i = 0; i < PCSPK_BUF_LEN; ++i)
  71. s->sample_buf[i] = 128; /* silence */
  72. }
  73. }
  74. static void pcspk_callback(void *opaque, int free)
  75. {
  76. PCSpkState *s = opaque;
  77. PITChannelInfo ch;
  78. unsigned int n;
  79. pit_get_channel_info(s->pit, 2, &ch);
  80. if (ch.mode != 3) {
  81. return;
  82. }
  83. n = ch.initial_count;
  84. /* avoid frequencies that are not reproducible with sample rate */
  85. if (n < PCSPK_MIN_COUNT)
  86. n = 0;
  87. if (s->pit_count != n) {
  88. s->pit_count = n;
  89. s->play_pos = 0;
  90. generate_samples(s);
  91. }
  92. while (free > 0) {
  93. n = MIN(s->samples - s->play_pos, (unsigned int)free);
  94. n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
  95. if (!n)
  96. break;
  97. s->play_pos = (s->play_pos + n) % s->samples;
  98. free -= n;
  99. }
  100. }
  101. static int pcspk_audio_init(PCSpkState *s)
  102. {
  103. struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUDIO_FORMAT_U8, 0};
  104. if (s->voice) {
  105. /* already initialized */
  106. return 0;
  107. }
  108. AUD_register_card(s_spk, &s->card);
  109. s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
  110. if (!s->voice) {
  111. AUD_log(s_spk, "Could not open voice\n");
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
  117. unsigned size)
  118. {
  119. PCSpkState *s = opaque;
  120. PITChannelInfo ch;
  121. pit_get_channel_info(s->pit, 2, &ch);
  122. s->dummy_refresh_clock ^= (1 << 4);
  123. return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
  124. (ch.out << 5);
  125. }
  126. static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
  127. unsigned size)
  128. {
  129. PCSpkState *s = opaque;
  130. const int gate = val & 1;
  131. s->data_on = (val >> 1) & 1;
  132. pit_set_gate(s->pit, 2, gate);
  133. if (s->voice) {
  134. if (gate) /* restart */
  135. s->play_pos = 0;
  136. AUD_set_active_out(s->voice, gate & s->data_on);
  137. }
  138. }
  139. static const MemoryRegionOps pcspk_io_ops = {
  140. .read = pcspk_io_read,
  141. .write = pcspk_io_write,
  142. .impl = {
  143. .min_access_size = 1,
  144. .max_access_size = 1,
  145. },
  146. };
  147. static void pcspk_initfn(Object *obj)
  148. {
  149. PCSpkState *s = PC_SPEAKER(obj);
  150. memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "pcspk", 1);
  151. object_property_add_link(obj, "pit", TYPE_PIT_COMMON,
  152. (Object **)&s->pit,
  153. qdev_prop_allow_set_link_before_realize,
  154. 0);
  155. }
  156. static void pcspk_realizefn(DeviceState *dev, Error **errp)
  157. {
  158. ISADevice *isadev = ISA_DEVICE(dev);
  159. PCSpkState *s = PC_SPEAKER(dev);
  160. isa_register_ioport(isadev, &s->ioport, s->iobase);
  161. if (s->card.state) {
  162. pcspk_audio_init(s);
  163. }
  164. pcspk_state = s;
  165. }
  166. static bool migrate_needed(void *opaque)
  167. {
  168. PCSpkState *s = opaque;
  169. return s->migrate;
  170. }
  171. static const VMStateDescription vmstate_spk = {
  172. .name = "pcspk",
  173. .version_id = 1,
  174. .minimum_version_id = 1,
  175. .minimum_version_id_old = 1,
  176. .needed = migrate_needed,
  177. .fields = (VMStateField[]) {
  178. VMSTATE_UINT8(data_on, PCSpkState),
  179. VMSTATE_UINT8(dummy_refresh_clock, PCSpkState),
  180. VMSTATE_END_OF_LIST()
  181. }
  182. };
  183. static Property pcspk_properties[] = {
  184. DEFINE_AUDIO_PROPERTIES(PCSpkState, card),
  185. DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, 0x61),
  186. DEFINE_PROP_BOOL("migrate", PCSpkState, migrate, true),
  187. DEFINE_PROP_END_OF_LIST(),
  188. };
  189. static void pcspk_class_initfn(ObjectClass *klass, void *data)
  190. {
  191. DeviceClass *dc = DEVICE_CLASS(klass);
  192. dc->realize = pcspk_realizefn;
  193. set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
  194. dc->vmsd = &vmstate_spk;
  195. device_class_set_props(dc, pcspk_properties);
  196. /* Reason: realize sets global pcspk_state */
  197. /* Reason: pit object link */
  198. dc->user_creatable = false;
  199. }
  200. static const TypeInfo pcspk_info = {
  201. .name = TYPE_PC_SPEAKER,
  202. .parent = TYPE_ISA_DEVICE,
  203. .instance_size = sizeof(PCSpkState),
  204. .instance_init = pcspk_initfn,
  205. .class_init = pcspk_class_initfn,
  206. };
  207. static int pcspk_audio_init_soundhw(ISABus *bus)
  208. {
  209. PCSpkState *s = pcspk_state;
  210. warn_report("'-soundhw pcspk' is deprecated, "
  211. "please set a backend using '-machine pcspk-audiodev=<name>' instead");
  212. return pcspk_audio_init(s);
  213. }
  214. static void pcspk_register(void)
  215. {
  216. type_register_static(&pcspk_info);
  217. isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init_soundhw);
  218. }
  219. type_init(pcspk_register)