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