pcspk.c 6.6 KB

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