| 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 "qemu/osdep.h" |
| 26 | #include "hw/isa/isa.h" |
| 27 | #include "hw/audio/model.h" |
| 28 | #include "qemu/audio.h" |
| 29 | #include "qemu/module.h" |
| 30 | #include "qemu/timer.h" |
| 31 | #include "qemu/error-report.h" |
| 32 | #include "hw/timer/i8254.h" |
| 33 | #include "migration/vmstate.h" |
| 34 | #include "hw/audio/pcspk.h" |
| 35 | #include "qapi/error.h" |
| 36 | #include "qom/object.h" |
| 37 | #include "trace.h" |
| 38 | |
| 39 | #define PCSPK_BUF_LEN 1792 |
| 40 | #define PCSPK_SAMPLE_RATE 32000 |
| 41 | #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1) |
| 42 | #define PCSPK_MIN_COUNT DIV_ROUND_UP(PIT_FREQ, PCSPK_MAX_FREQ) |
| 43 | |
| 44 | OBJECT_DECLARE_SIMPLE_TYPE(PCSpkState, PC_SPEAKER) |
| 45 | |
| 46 | struct PCSpkState { |
| 47 | ISADevice parent_obj; |
| 48 | |
| 49 | MemoryRegion ioport; |
| 50 | uint32_t iobase; |
| 51 | uint8_t sample_buf[PCSPK_BUF_LEN]; |
| 52 | AudioBackend *audio_be; |
| 53 | SWVoiceOut *voice; |
| 54 | PITCommonState *pit; |
| 55 | unsigned int pit_count; |
| 56 | unsigned int samples; |
| 57 | unsigned int play_pos; |
| 58 | uint8_t data_on; |
| 59 | uint8_t dummy_refresh_clock; |
| 60 | }; |
| 61 | |
| 62 | static const char *s_spk = "pcspk"; |
| 63 | |
| 64 | static inline void generate_samples(PCSpkState *s) |
| 65 | { |
| 66 | unsigned int i; |
| 67 | |
| 68 | if (s->pit_count) { |
| 69 | const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count; |
| 70 | const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m; |
| 71 | |
| 72 | /* multiple of wavelength for gapless looping */ |
| 73 | s->samples = (QEMU_ALIGN_DOWN(PCSPK_BUF_LEN * PIT_FREQ, m) / (PIT_FREQ >> 1) + 1) >> 1; |
| 74 | for (i = 0; i < s->samples; ++i) |
| 75 | s->sample_buf[i] = (64 & (n * i >> 25)) - 32; |
| 76 | } else { |
| 77 | s->samples = PCSPK_BUF_LEN; |
| 78 | for (i = 0; i < PCSPK_BUF_LEN; ++i) |
| 79 | s->sample_buf[i] = 128; /* silence */ |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static void pcspk_callback(void *opaque, int free) |
| 84 | { |
| 85 | PCSpkState *s = opaque; |
| 86 | PITChannelInfo ch; |
| 87 | unsigned int n; |
| 88 | |
| 89 | pit_get_channel_info(s->pit, 2, &ch); |
| 90 | |
| 91 | if (ch.mode != 3) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | n = ch.initial_count; |
| 96 | /* avoid frequencies that are not reproducible with sample rate */ |
| 97 | if (n < PCSPK_MIN_COUNT) |
| 98 | n = 0; |
| 99 | |
| 100 | if (s->pit_count != n) { |
| 101 | s->pit_count = n; |
| 102 | s->play_pos = 0; |
| 103 | generate_samples(s); |
| 104 | } |
| 105 | |
| 106 | while (free > 0) { |
| 107 | n = MIN(s->samples - s->play_pos, (unsigned int)free); |
| 108 | n = audio_be_write(s->audio_be, s->voice, &s->sample_buf[s->play_pos], n); |
| 109 | if (!n) |
| 110 | break; |
| 111 | s->play_pos = (s->play_pos + n) % s->samples; |
| 112 | free -= n; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static int pcspk_audio_init(PCSpkState *s) |
| 117 | { |
| 118 | struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUDIO_FORMAT_U8, 0}; |
| 119 | |
| 120 | if (s->voice) { |
| 121 | /* already initialized */ |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | s->voice = audio_be_open_out(s->audio_be, s->voice, s_spk, s, pcspk_callback, &as); |
| 126 | if (!s->voice) { |
| 127 | error_report("pcspk: Could not open voice"); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | static uint64_t pcspk_io_read(void *opaque, hwaddr addr, |
| 135 | unsigned size) |
| 136 | { |
| 137 | PCSpkState *s = opaque; |
| 138 | PITChannelInfo ch; |
| 139 | uint8_t val; |
| 140 | |
| 141 | pit_get_channel_info(s->pit, 2, &ch); |
| 142 | |
| 143 | s->dummy_refresh_clock ^= (1 << 4); |
| 144 | |
| 145 | val = ch.gate | (s->data_on << 1) | s->dummy_refresh_clock | |
| 146 | (ch.out << 5); |
| 147 | |
| 148 | trace_pcspk_io_read(s->iobase, val); |
| 149 | |
| 150 | return val; |
| 151 | } |
| 152 | |
| 153 | static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val, |
| 154 | unsigned size) |
| 155 | { |
| 156 | PCSpkState *s = opaque; |
| 157 | const int gate = val & 1; |
| 158 | |
| 159 | trace_pcspk_io_write(s->iobase, val); |
| 160 | |
| 161 | s->data_on = (val >> 1) & 1; |
| 162 | pit_set_gate(s->pit, 2, gate); |
| 163 | if (s->voice) { |
| 164 | if (gate) /* restart */ |
| 165 | s->play_pos = 0; |
| 166 | audio_be_set_active_out(s->audio_be, s->voice, gate & s->data_on); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | static const MemoryRegionOps pcspk_io_ops = { |
| 171 | .read = pcspk_io_read, |
| 172 | .write = pcspk_io_write, |
| 173 | .impl = { |
| 174 | .min_access_size = 1, |
| 175 | .max_access_size = 1, |
| 176 | }, |
| 177 | }; |
| 178 | |
| 179 | static void pcspk_initfn(Object *obj) |
| 180 | { |
| 181 | PCSpkState *s = PC_SPEAKER(obj); |
| 182 | |
| 183 | memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "pcspk", 1); |
| 184 | } |
| 185 | |
| 186 | static void pcspk_realizefn(DeviceState *dev, Error **errp) |
| 187 | { |
| 188 | ISADevice *isadev = ISA_DEVICE(dev); |
| 189 | PCSpkState *s = PC_SPEAKER(dev); |
| 190 | |
| 191 | if (!s->pit) { |
| 192 | error_setg(errp, "pcspk: No \"pit\" set or available"); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | isa_register_ioport(isadev, &s->ioport, s->iobase); |
| 197 | |
| 198 | if (s->audio_be && audio_be_check(&s->audio_be, errp)) { |
| 199 | pcspk_audio_init(s); |
| 200 | return; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | static const VMStateDescription vmstate_spk = { |
| 205 | .name = "pcspk", |
| 206 | .version_id = 1, |
| 207 | .minimum_version_id = 1, |
| 208 | .fields = (const VMStateField[]) { |
| 209 | VMSTATE_UINT8(data_on, PCSpkState), |
| 210 | VMSTATE_UINT8(dummy_refresh_clock, PCSpkState), |
| 211 | VMSTATE_END_OF_LIST() |
| 212 | } |
| 213 | }; |
| 214 | |
| 215 | static const Property pcspk_properties[] = { |
| 216 | DEFINE_AUDIO_PROPERTIES(PCSpkState, audio_be), |
| 217 | DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, 0x61), |
| 218 | DEFINE_PROP_LINK("pit", PCSpkState, pit, TYPE_PIT_COMMON, PITCommonState *), |
| 219 | }; |
| 220 | |
| 221 | static void pcspk_class_initfn(ObjectClass *klass, const void *data) |
| 222 | { |
| 223 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 224 | |
| 225 | dc->realize = pcspk_realizefn; |
| 226 | set_bit(DEVICE_CATEGORY_SOUND, dc->categories); |
| 227 | dc->vmsd = &vmstate_spk; |
| 228 | device_class_set_props(dc, pcspk_properties); |
| 229 | /* Reason: pit object link */ |
| 230 | dc->user_creatable = false; |
| 231 | } |
| 232 | |
| 233 | static const TypeInfo pcspk_info = { |
| 234 | .name = TYPE_PC_SPEAKER, |
| 235 | .parent = TYPE_ISA_DEVICE, |
| 236 | .instance_size = sizeof(PCSpkState), |
| 237 | .instance_init = pcspk_initfn, |
| 238 | .class_init = pcspk_class_initfn, |
| 239 | }; |
| 240 | |
| 241 | static void pcspk_register(void) |
| 242 | { |
| 243 | type_register_static(&pcspk_info); |
| 244 | } |
| 245 | type_init(pcspk_register) |