pcspk.c 6.7 KB

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