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