pcspk.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
  109. if (!s->voice) {
  110. AUD_log(s_spk, "Could not open voice\n");
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
  116. unsigned size)
  117. {
  118. PCSpkState *s = opaque;
  119. PITChannelInfo ch;
  120. pit_get_channel_info(s->pit, 2, &ch);
  121. s->dummy_refresh_clock ^= (1 << 4);
  122. return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
  123. (ch.out << 5);
  124. }
  125. static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
  126. unsigned size)
  127. {
  128. PCSpkState *s = opaque;
  129. const int gate = val & 1;
  130. s->data_on = (val >> 1) & 1;
  131. pit_set_gate(s->pit, 2, gate);
  132. if (s->voice) {
  133. if (gate) /* restart */
  134. s->play_pos = 0;
  135. AUD_set_active_out(s->voice, gate & s->data_on);
  136. }
  137. }
  138. static const MemoryRegionOps pcspk_io_ops = {
  139. .read = pcspk_io_read,
  140. .write = pcspk_io_write,
  141. .impl = {
  142. .min_access_size = 1,
  143. .max_access_size = 1,
  144. },
  145. };
  146. static void pcspk_initfn(Object *obj)
  147. {
  148. PCSpkState *s = PC_SPEAKER(obj);
  149. memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "pcspk", 1);
  150. object_property_add_link(obj, "pit", TYPE_PIT_COMMON,
  151. (Object **)&s->pit,
  152. qdev_prop_allow_set_link_before_realize,
  153. 0);
  154. }
  155. static void pcspk_realizefn(DeviceState *dev, Error **errp)
  156. {
  157. ISADevice *isadev = ISA_DEVICE(dev);
  158. PCSpkState *s = PC_SPEAKER(dev);
  159. isa_register_ioport(isadev, &s->ioport, s->iobase);
  160. if (s->card.state && AUD_register_card(s_spk, &s->card, errp)) {
  161. pcspk_audio_init(s);
  162. }
  163. pcspk_state = s;
  164. }
  165. static bool migrate_needed(void *opaque)
  166. {
  167. PCSpkState *s = opaque;
  168. return s->migrate;
  169. }
  170. static const VMStateDescription vmstate_spk = {
  171. .name = "pcspk",
  172. .version_id = 1,
  173. .minimum_version_id = 1,
  174. .needed = migrate_needed,
  175. .fields = (const VMStateField[]) {
  176. VMSTATE_UINT8(data_on, PCSpkState),
  177. VMSTATE_UINT8(dummy_refresh_clock, PCSpkState),
  178. VMSTATE_END_OF_LIST()
  179. }
  180. };
  181. static const Property pcspk_properties[] = {
  182. DEFINE_AUDIO_PROPERTIES(PCSpkState, card),
  183. DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, 0x61),
  184. DEFINE_PROP_BOOL("migrate", PCSpkState, migrate, true),
  185. };
  186. static void pcspk_class_initfn(ObjectClass *klass, void *data)
  187. {
  188. DeviceClass *dc = DEVICE_CLASS(klass);
  189. dc->realize = pcspk_realizefn;
  190. set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
  191. dc->vmsd = &vmstate_spk;
  192. device_class_set_props(dc, pcspk_properties);
  193. /* Reason: realize sets global pcspk_state */
  194. /* Reason: pit object link */
  195. dc->user_creatable = false;
  196. }
  197. static const TypeInfo pcspk_info = {
  198. .name = TYPE_PC_SPEAKER,
  199. .parent = TYPE_ISA_DEVICE,
  200. .instance_size = sizeof(PCSpkState),
  201. .instance_init = pcspk_initfn,
  202. .class_init = pcspk_class_initfn,
  203. };
  204. static void pcspk_register(void)
  205. {
  206. type_register_static(&pcspk_info);
  207. }
  208. type_init(pcspk_register)