bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 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 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 25 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 26 | #include "hw/i386/pc.h" |
| 27 | #include "hw/isa/isa.h" |
Paolo Bonzini | 36cd6f6 | 2013-04-18 18:43:58 +0200 | [diff] [blame] | 28 | #include "hw/audio/audio.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 29 | #include "audio/audio.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 30 | #include "qemu/timer.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 31 | #include "hw/timer/i8254.h" |
| 32 | #include "hw/audio/pcspk.h" |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 33 | |
| 34 | #define PCSPK_BUF_LEN 1792 |
| 35 | #define PCSPK_SAMPLE_RATE 32000 |
| 36 | #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1) |
| 37 | #define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ) |
| 38 | |
Andreas Färber | d367ece | 2013-04-27 22:18:48 +0200 | [diff] [blame] | 39 | #define PC_SPEAKER(obj) OBJECT_CHECK(PCSpkState, (obj), TYPE_PC_SPEAKER) |
| 40 | |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 41 | typedef struct { |
Andreas Färber | d367ece | 2013-04-27 22:18:48 +0200 | [diff] [blame] | 42 | ISADevice parent_obj; |
| 43 | |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 44 | MemoryRegion ioport; |
| 45 | uint32_t iobase; |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 46 | uint8_t sample_buf[PCSPK_BUF_LEN]; |
| 47 | QEMUSoundCard card; |
| 48 | SWVoiceOut *voice; |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 49 | void *pit; |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 50 | unsigned int pit_count; |
| 51 | unsigned int samples; |
| 52 | unsigned int play_pos; |
| 53 | int data_on; |
| 54 | int dummy_refresh_clock; |
| 55 | } PCSpkState; |
| 56 | |
| 57 | static const char *s_spk = "pcspk"; |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 58 | static PCSpkState *pcspk_state; |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 59 | |
| 60 | static inline void generate_samples(PCSpkState *s) |
| 61 | { |
| 62 | unsigned int i; |
| 63 | |
| 64 | if (s->pit_count) { |
| 65 | const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count; |
| 66 | const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m; |
| 67 | |
| 68 | /* multiple of wavelength for gapless looping */ |
| 69 | s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1; |
| 70 | for (i = 0; i < s->samples; ++i) |
| 71 | s->sample_buf[i] = (64 & (n * i >> 25)) - 32; |
| 72 | } else { |
| 73 | s->samples = PCSPK_BUF_LEN; |
| 74 | for (i = 0; i < PCSPK_BUF_LEN; ++i) |
| 75 | s->sample_buf[i] = 128; /* silence */ |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void pcspk_callback(void *opaque, int free) |
| 80 | { |
| 81 | PCSpkState *s = opaque; |
Jan Kiszka | 4aa5d28 | 2012-02-01 20:31:43 +0100 | [diff] [blame] | 82 | PITChannelInfo ch; |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 83 | unsigned int n; |
| 84 | |
Jan Kiszka | 4aa5d28 | 2012-02-01 20:31:43 +0100 | [diff] [blame] | 85 | pit_get_channel_info(s->pit, 2, &ch); |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 86 | |
Jan Kiszka | 4aa5d28 | 2012-02-01 20:31:43 +0100 | [diff] [blame] | 87 | if (ch.mode != 3) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | n = ch.initial_count; |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 92 | /* avoid frequencies that are not reproducible with sample rate */ |
| 93 | if (n < PCSPK_MIN_COUNT) |
| 94 | n = 0; |
| 95 | |
| 96 | if (s->pit_count != n) { |
| 97 | s->pit_count = n; |
| 98 | s->play_pos = 0; |
| 99 | generate_samples(s); |
| 100 | } |
| 101 | |
| 102 | while (free > 0) { |
| 103 | n = audio_MIN(s->samples - s->play_pos, (unsigned int)free); |
| 104 | n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n); |
| 105 | if (!n) |
| 106 | break; |
| 107 | s->play_pos = (s->play_pos + n) % s->samples; |
| 108 | free -= n; |
| 109 | } |
| 110 | } |
| 111 | |
Paolo Bonzini | 36cd6f6 | 2013-04-18 18:43:58 +0200 | [diff] [blame] | 112 | static int pcspk_audio_init(ISABus *bus) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 113 | { |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 114 | PCSpkState *s = pcspk_state; |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 115 | struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0}; |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 116 | |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 117 | AUD_register_card(s_spk, &s->card); |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 118 | |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 119 | s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as); |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 120 | if (!s->voice) { |
| 121 | AUD_log(s_spk, "Could not open voice\n"); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 128 | static uint64_t pcspk_io_read(void *opaque, hwaddr addr, |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 129 | unsigned size) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 130 | { |
| 131 | PCSpkState *s = opaque; |
Jan Kiszka | 4aa5d28 | 2012-02-01 20:31:43 +0100 | [diff] [blame] | 132 | PITChannelInfo ch; |
| 133 | |
| 134 | pit_get_channel_info(s->pit, 2, &ch); |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 135 | |
| 136 | s->dummy_refresh_clock ^= (1 << 4); |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 137 | |
Jan Kiszka | 4aa5d28 | 2012-02-01 20:31:43 +0100 | [diff] [blame] | 138 | return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock | |
| 139 | (ch.out << 5); |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 142 | static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val, |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 143 | unsigned size) |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 144 | { |
| 145 | PCSpkState *s = opaque; |
| 146 | const int gate = val & 1; |
| 147 | |
| 148 | s->data_on = (val >> 1) & 1; |
| 149 | pit_set_gate(s->pit, 2, gate); |
| 150 | if (s->voice) { |
| 151 | if (gate) /* restart */ |
| 152 | s->play_pos = 0; |
| 153 | AUD_set_active_out(s->voice, gate & s->data_on); |
| 154 | } |
| 155 | } |
| 156 | |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 157 | static const MemoryRegionOps pcspk_io_ops = { |
| 158 | .read = pcspk_io_read, |
| 159 | .write = pcspk_io_write, |
| 160 | .impl = { |
| 161 | .min_access_size = 1, |
| 162 | .max_access_size = 1, |
| 163 | }, |
| 164 | }; |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 165 | |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 166 | static void pcspk_initfn(Object *obj) |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 167 | { |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 168 | PCSpkState *s = PC_SPEAKER(obj); |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 169 | |
Paolo Bonzini | 64bde0f | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 170 | memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "elcr", 1); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static void pcspk_realizefn(DeviceState *dev, Error **errp) |
| 174 | { |
| 175 | ISADevice *isadev = ISA_DEVICE(dev); |
| 176 | PCSpkState *s = PC_SPEAKER(dev); |
| 177 | |
| 178 | isa_register_ioport(isadev, &s->ioport, s->iobase); |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 179 | |
| 180 | pcspk_state = s; |
bellard | fd06c37 | 2006-04-24 21:58:30 +0000 | [diff] [blame] | 181 | } |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 182 | |
| 183 | static Property pcspk_properties[] = { |
Paolo Bonzini | c7bcc85 | 2014-02-08 11:01:53 +0100 | [diff] [blame] | 184 | DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, -1), |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 185 | DEFINE_PROP_PTR("pit", PCSpkState, pit), |
| 186 | DEFINE_PROP_END_OF_LIST(), |
| 187 | }; |
| 188 | |
| 189 | static void pcspk_class_initfn(ObjectClass *klass, void *data) |
| 190 | { |
| 191 | DeviceClass *dc = DEVICE_CLASS(klass); |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 192 | |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 193 | dc->realize = pcspk_realizefn; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 194 | set_bit(DEVICE_CATEGORY_SOUND, dc->categories); |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 195 | dc->props = pcspk_properties; |
Markus Armbruster | f3b1764 | 2013-11-28 17:27:02 +0100 | [diff] [blame] | 196 | /* Reason: pointer property "pit", realize sets global pcspk_state */ |
| 197 | dc->cannot_instantiate_with_device_add_yet = true; |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 200 | static const TypeInfo pcspk_info = { |
Andreas Färber | d367ece | 2013-04-27 22:18:48 +0200 | [diff] [blame] | 201 | .name = TYPE_PC_SPEAKER, |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 202 | .parent = TYPE_ISA_DEVICE, |
| 203 | .instance_size = sizeof(PCSpkState), |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 204 | .instance_init = pcspk_initfn, |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 205 | .class_init = pcspk_class_initfn, |
| 206 | }; |
| 207 | |
| 208 | static void pcspk_register(void) |
| 209 | { |
| 210 | type_register_static(&pcspk_info); |
Paolo Bonzini | 36cd6f6 | 2013-04-18 18:43:58 +0200 | [diff] [blame] | 211 | isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init); |
Jan Kiszka | 302fe51 | 2012-02-17 11:24:34 +0100 | [diff] [blame] | 212 | } |
| 213 | type_init(pcspk_register) |