pcspk.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "hw.h"
  25. #include "pc.h"
  26. #include "isa.h"
  27. #include "audio/audio.h"
  28. #include "qemu/timer.h"
  29. #include "i8254.h"
  30. #include "pcspk.h"
  31. #define PCSPK_BUF_LEN 1792
  32. #define PCSPK_SAMPLE_RATE 32000
  33. #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
  34. #define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ)
  35. typedef struct {
  36. ISADevice dev;
  37. MemoryRegion ioport;
  38. uint32_t iobase;
  39. uint8_t sample_buf[PCSPK_BUF_LEN];
  40. QEMUSoundCard card;
  41. SWVoiceOut *voice;
  42. void *pit;
  43. unsigned int pit_count;
  44. unsigned int samples;
  45. unsigned int play_pos;
  46. int data_on;
  47. int dummy_refresh_clock;
  48. } PCSpkState;
  49. static const char *s_spk = "pcspk";
  50. static PCSpkState *pcspk_state;
  51. static inline void generate_samples(PCSpkState *s)
  52. {
  53. unsigned int i;
  54. if (s->pit_count) {
  55. const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
  56. const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
  57. /* multiple of wavelength for gapless looping */
  58. s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1;
  59. for (i = 0; i < s->samples; ++i)
  60. s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
  61. } else {
  62. s->samples = PCSPK_BUF_LEN;
  63. for (i = 0; i < PCSPK_BUF_LEN; ++i)
  64. s->sample_buf[i] = 128; /* silence */
  65. }
  66. }
  67. static void pcspk_callback(void *opaque, int free)
  68. {
  69. PCSpkState *s = opaque;
  70. PITChannelInfo ch;
  71. unsigned int n;
  72. pit_get_channel_info(s->pit, 2, &ch);
  73. if (ch.mode != 3) {
  74. return;
  75. }
  76. n = ch.initial_count;
  77. /* avoid frequencies that are not reproducible with sample rate */
  78. if (n < PCSPK_MIN_COUNT)
  79. n = 0;
  80. if (s->pit_count != n) {
  81. s->pit_count = n;
  82. s->play_pos = 0;
  83. generate_samples(s);
  84. }
  85. while (free > 0) {
  86. n = audio_MIN(s->samples - s->play_pos, (unsigned int)free);
  87. n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
  88. if (!n)
  89. break;
  90. s->play_pos = (s->play_pos + n) % s->samples;
  91. free -= n;
  92. }
  93. }
  94. int pcspk_audio_init(ISABus *bus)
  95. {
  96. PCSpkState *s = pcspk_state;
  97. struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0};
  98. AUD_register_card(s_spk, &s->card);
  99. s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
  100. if (!s->voice) {
  101. AUD_log(s_spk, "Could not open voice\n");
  102. return -1;
  103. }
  104. return 0;
  105. }
  106. static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
  107. unsigned size)
  108. {
  109. PCSpkState *s = opaque;
  110. PITChannelInfo ch;
  111. pit_get_channel_info(s->pit, 2, &ch);
  112. s->dummy_refresh_clock ^= (1 << 4);
  113. return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
  114. (ch.out << 5);
  115. }
  116. static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
  117. unsigned size)
  118. {
  119. PCSpkState *s = opaque;
  120. const int gate = val & 1;
  121. s->data_on = (val >> 1) & 1;
  122. pit_set_gate(s->pit, 2, gate);
  123. if (s->voice) {
  124. if (gate) /* restart */
  125. s->play_pos = 0;
  126. AUD_set_active_out(s->voice, gate & s->data_on);
  127. }
  128. }
  129. static const MemoryRegionOps pcspk_io_ops = {
  130. .read = pcspk_io_read,
  131. .write = pcspk_io_write,
  132. .impl = {
  133. .min_access_size = 1,
  134. .max_access_size = 1,
  135. },
  136. };
  137. static int pcspk_initfn(ISADevice *dev)
  138. {
  139. PCSpkState *s = DO_UPCAST(PCSpkState, dev, dev);
  140. memory_region_init_io(&s->ioport, &pcspk_io_ops, s, "elcr", 1);
  141. isa_register_ioport(dev, &s->ioport, s->iobase);
  142. pcspk_state = s;
  143. return 0;
  144. }
  145. static Property pcspk_properties[] = {
  146. DEFINE_PROP_HEX32("iobase", PCSpkState, iobase, -1),
  147. DEFINE_PROP_PTR("pit", PCSpkState, pit),
  148. DEFINE_PROP_END_OF_LIST(),
  149. };
  150. static void pcspk_class_initfn(ObjectClass *klass, void *data)
  151. {
  152. DeviceClass *dc = DEVICE_CLASS(klass);
  153. ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
  154. ic->init = pcspk_initfn;
  155. dc->no_user = 1;
  156. dc->props = pcspk_properties;
  157. }
  158. static const TypeInfo pcspk_info = {
  159. .name = "isa-pcspk",
  160. .parent = TYPE_ISA_DEVICE,
  161. .instance_size = sizeof(PCSpkState),
  162. .class_init = pcspk_class_initfn,
  163. };
  164. static void pcspk_register(void)
  165. {
  166. type_register_static(&pcspk_info);
  167. }
  168. type_init(pcspk_register)