2
0

pcspk.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #define PCSPK_BUF_LEN 1792
  30. #define PCSPK_SAMPLE_RATE 32000
  31. #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
  32. #define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ)
  33. typedef struct {
  34. uint8_t sample_buf[PCSPK_BUF_LEN];
  35. QEMUSoundCard card;
  36. SWVoiceOut *voice;
  37. ISADevice *pit;
  38. unsigned int pit_count;
  39. unsigned int samples;
  40. unsigned int play_pos;
  41. int data_on;
  42. int dummy_refresh_clock;
  43. } PCSpkState;
  44. static const char *s_spk = "pcspk";
  45. static PCSpkState pcspk_state;
  46. static inline void generate_samples(PCSpkState *s)
  47. {
  48. unsigned int i;
  49. if (s->pit_count) {
  50. const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
  51. const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
  52. /* multiple of wavelength for gapless looping */
  53. s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1;
  54. for (i = 0; i < s->samples; ++i)
  55. s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
  56. } else {
  57. s->samples = PCSPK_BUF_LEN;
  58. for (i = 0; i < PCSPK_BUF_LEN; ++i)
  59. s->sample_buf[i] = 128; /* silence */
  60. }
  61. }
  62. static void pcspk_callback(void *opaque, int free)
  63. {
  64. PCSpkState *s = opaque;
  65. unsigned int n;
  66. if (pit_get_mode(s->pit, 2) != 3)
  67. return;
  68. n = pit_get_initial_count(s->pit, 2);
  69. /* avoid frequencies that are not reproducible with sample rate */
  70. if (n < PCSPK_MIN_COUNT)
  71. n = 0;
  72. if (s->pit_count != n) {
  73. s->pit_count = n;
  74. s->play_pos = 0;
  75. generate_samples(s);
  76. }
  77. while (free > 0) {
  78. n = audio_MIN(s->samples - s->play_pos, (unsigned int)free);
  79. n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
  80. if (!n)
  81. break;
  82. s->play_pos = (s->play_pos + n) % s->samples;
  83. free -= n;
  84. }
  85. }
  86. int pcspk_audio_init(qemu_irq *pic)
  87. {
  88. PCSpkState *s = &pcspk_state;
  89. struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0};
  90. AUD_register_card(s_spk, &s->card);
  91. s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
  92. if (!s->voice) {
  93. AUD_log(s_spk, "Could not open voice\n");
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. static uint32_t pcspk_ioport_read(void *opaque, uint32_t addr)
  99. {
  100. PCSpkState *s = opaque;
  101. int out;
  102. s->dummy_refresh_clock ^= (1 << 4);
  103. out = pit_get_out(s->pit, 2, qemu_get_clock_ns(vm_clock)) << 5;
  104. return pit_get_gate(s->pit, 2) | (s->data_on << 1) | s->dummy_refresh_clock | out;
  105. }
  106. static void pcspk_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  107. {
  108. PCSpkState *s = opaque;
  109. const int gate = val & 1;
  110. s->data_on = (val >> 1) & 1;
  111. pit_set_gate(s->pit, 2, gate);
  112. if (s->voice) {
  113. if (gate) /* restart */
  114. s->play_pos = 0;
  115. AUD_set_active_out(s->voice, gate & s->data_on);
  116. }
  117. }
  118. void pcspk_init(ISADevice *pit)
  119. {
  120. PCSpkState *s = &pcspk_state;
  121. s->pit = pit;
  122. register_ioport_read(0x61, 1, 1, pcspk_ioport_read, s);
  123. register_ioport_write(0x61, 1, 1, pcspk_ioport_write, s);
  124. }