]>
Commit | Line | Data |
---|---|---|
fd06c375 FB |
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 | ||
6086a565 | 25 | #include "qemu/osdep.h" |
0d09e41a | 26 | #include "hw/isa/isa.h" |
8a824e4d | 27 | #include "hw/audio/soundhw.h" |
87ecb68b | 28 | #include "audio/audio.h" |
0b8fa32f | 29 | #include "qemu/module.h" |
1de7afc9 | 30 | #include "qemu/timer.h" |
2e16ec05 | 31 | #include "qemu/error-report.h" |
0d09e41a | 32 | #include "hw/timer/i8254.h" |
d6454270 | 33 | #include "migration/vmstate.h" |
0d09e41a | 34 | #include "hw/audio/pcspk.h" |
873b4d3f | 35 | #include "qapi/error.h" |
fd06c375 FB |
36 | |
37 | #define PCSPK_BUF_LEN 1792 | |
38 | #define PCSPK_SAMPLE_RATE 32000 | |
39 | #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1) | |
b988a650 | 40 | #define PCSPK_MIN_COUNT DIV_ROUND_UP(PIT_FREQ, PCSPK_MAX_FREQ) |
fd06c375 | 41 | |
d367ece5 AF |
42 | #define PC_SPEAKER(obj) OBJECT_CHECK(PCSpkState, (obj), TYPE_PC_SPEAKER) |
43 | ||
fd06c375 | 44 | typedef struct { |
d367ece5 AF |
45 | ISADevice parent_obj; |
46 | ||
302fe51b JK |
47 | MemoryRegion ioport; |
48 | uint32_t iobase; | |
fd06c375 FB |
49 | uint8_t sample_buf[PCSPK_BUF_LEN]; |
50 | QEMUSoundCard card; | |
51 | SWVoiceOut *voice; | |
302fe51b | 52 | void *pit; |
fd06c375 FB |
53 | unsigned int pit_count; |
54 | unsigned int samples; | |
55 | unsigned int play_pos; | |
39c88f56 PD |
56 | uint8_t data_on; |
57 | uint8_t dummy_refresh_clock; | |
04e27c6b | 58 | bool migrate; |
fd06c375 FB |
59 | } PCSpkState; |
60 | ||
61 | static const char *s_spk = "pcspk"; | |
28605a22 | 62 | static PCSpkState *pcspk_state; |
fd06c375 FB |
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 */ | |
668c2d1f | 73 | s->samples = (QEMU_ALIGN_DOWN(PCSPK_BUF_LEN * PIT_FREQ, m) / (PIT_FREQ >> 1) + 1) >> 1; |
fd06c375 FB |
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; | |
4aa5d285 | 86 | PITChannelInfo ch; |
fd06c375 FB |
87 | unsigned int n; |
88 | ||
4aa5d285 JK |
89 | pit_get_channel_info(s->pit, 2, &ch); |
90 | ||
91 | if (ch.mode != 3) { | |
fd06c375 | 92 | return; |
4aa5d285 | 93 | } |
fd06c375 | 94 | |
4aa5d285 | 95 | n = ch.initial_count; |
fd06c375 FB |
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) { | |
58935915 | 107 | n = MIN(s->samples - s->play_pos, (unsigned int)free); |
fd06c375 FB |
108 | n = AUD_write(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 | ||
2e16ec05 | 116 | static int pcspk_audio_init(PCSpkState *s) |
28605a22 | 117 | { |
28605a22 GH |
118 | struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUDIO_FORMAT_U8, 0}; |
119 | ||
2e16ec05 GH |
120 | if (s->voice) { |
121 | /* already initialized */ | |
122 | return 0; | |
123 | } | |
124 | ||
28605a22 GH |
125 | AUD_register_card(s_spk, &s->card); |
126 | ||
127 | s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as); | |
128 | if (!s->voice) { | |
129 | AUD_log(s_spk, "Could not open voice\n"); | |
130 | return -1; | |
131 | } | |
132 | ||
133 | return 0; | |
134 | } | |
135 | ||
a8170e5e | 136 | static uint64_t pcspk_io_read(void *opaque, hwaddr addr, |
302fe51b | 137 | unsigned size) |
fd06c375 FB |
138 | { |
139 | PCSpkState *s = opaque; | |
4aa5d285 JK |
140 | PITChannelInfo ch; |
141 | ||
142 | pit_get_channel_info(s->pit, 2, &ch); | |
fd06c375 FB |
143 | |
144 | s->dummy_refresh_clock ^= (1 << 4); | |
fd06c375 | 145 | |
4aa5d285 JK |
146 | return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock | |
147 | (ch.out << 5); | |
fd06c375 FB |
148 | } |
149 | ||
a8170e5e | 150 | static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val, |
302fe51b | 151 | unsigned size) |
fd06c375 FB |
152 | { |
153 | PCSpkState *s = opaque; | |
154 | const int gate = val & 1; | |
155 | ||
156 | s->data_on = (val >> 1) & 1; | |
157 | pit_set_gate(s->pit, 2, gate); | |
158 | if (s->voice) { | |
159 | if (gate) /* restart */ | |
160 | s->play_pos = 0; | |
161 | AUD_set_active_out(s->voice, gate & s->data_on); | |
162 | } | |
163 | } | |
164 | ||
302fe51b JK |
165 | static const MemoryRegionOps pcspk_io_ops = { |
166 | .read = pcspk_io_read, | |
167 | .write = pcspk_io_write, | |
168 | .impl = { | |
169 | .min_access_size = 1, | |
170 | .max_access_size = 1, | |
171 | }, | |
172 | }; | |
173 | ||
db895a1e | 174 | static void pcspk_initfn(Object *obj) |
fd06c375 | 175 | { |
db895a1e | 176 | PCSpkState *s = PC_SPEAKER(obj); |
302fe51b | 177 | |
ecf2e5a4 | 178 | memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "pcspk", 1); |
873b4d3f | 179 | |
8a0b4de0 | 180 | object_property_add_link(obj, "pit", TYPE_PIT_COMMON, |
873b4d3f EV |
181 | (Object **)&s->pit, |
182 | qdev_prop_allow_set_link_before_realize, | |
d2623129 | 183 | 0); |
db895a1e | 184 | } |
302fe51b | 185 | |
db895a1e AF |
186 | static void pcspk_realizefn(DeviceState *dev, Error **errp) |
187 | { | |
188 | ISADevice *isadev = ISA_DEVICE(dev); | |
189 | PCSpkState *s = PC_SPEAKER(dev); | |
302fe51b | 190 | |
db895a1e AF |
191 | isa_register_ioport(isadev, &s->ioport, s->iobase); |
192 | ||
2e16ec05 GH |
193 | if (s->card.state) { |
194 | pcspk_audio_init(s); | |
195 | } | |
196 | ||
28605a22 | 197 | pcspk_state = s; |
302fe51b JK |
198 | } |
199 | ||
04e27c6b DDAG |
200 | static bool migrate_needed(void *opaque) |
201 | { | |
202 | PCSpkState *s = opaque; | |
203 | ||
204 | return s->migrate; | |
205 | } | |
206 | ||
39c88f56 PD |
207 | static const VMStateDescription vmstate_spk = { |
208 | .name = "pcspk", | |
209 | .version_id = 1, | |
210 | .minimum_version_id = 1, | |
211 | .minimum_version_id_old = 1, | |
04e27c6b | 212 | .needed = migrate_needed, |
39c88f56 PD |
213 | .fields = (VMStateField[]) { |
214 | VMSTATE_UINT8(data_on, PCSpkState), | |
215 | VMSTATE_UINT8(dummy_refresh_clock, PCSpkState), | |
216 | VMSTATE_END_OF_LIST() | |
217 | } | |
218 | }; | |
219 | ||
302fe51b | 220 | static Property pcspk_properties[] = { |
88e47b9a | 221 | DEFINE_AUDIO_PROPERTIES(PCSpkState, card), |
2336172d | 222 | DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, 0x61), |
04e27c6b | 223 | DEFINE_PROP_BOOL("migrate", PCSpkState, migrate, true), |
302fe51b JK |
224 | DEFINE_PROP_END_OF_LIST(), |
225 | }; | |
fd06c375 | 226 | |
302fe51b JK |
227 | static void pcspk_class_initfn(ObjectClass *klass, void *data) |
228 | { | |
229 | DeviceClass *dc = DEVICE_CLASS(klass); | |
302fe51b | 230 | |
db895a1e | 231 | dc->realize = pcspk_realizefn; |
125ee0ed | 232 | set_bit(DEVICE_CATEGORY_SOUND, dc->categories); |
39c88f56 | 233 | dc->vmsd = &vmstate_spk; |
4f67d30b | 234 | device_class_set_props(dc, pcspk_properties); |
28605a22 GH |
235 | /* Reason: realize sets global pcspk_state */ |
236 | /* Reason: pit object link */ | |
237 | dc->user_creatable = false; | |
302fe51b JK |
238 | } |
239 | ||
8c43a6f0 | 240 | static const TypeInfo pcspk_info = { |
d367ece5 | 241 | .name = TYPE_PC_SPEAKER, |
302fe51b JK |
242 | .parent = TYPE_ISA_DEVICE, |
243 | .instance_size = sizeof(PCSpkState), | |
db895a1e | 244 | .instance_init = pcspk_initfn, |
302fe51b JK |
245 | .class_init = pcspk_class_initfn, |
246 | }; | |
247 | ||
2e16ec05 GH |
248 | static int pcspk_audio_init_soundhw(ISABus *bus) |
249 | { | |
250 | PCSpkState *s = pcspk_state; | |
251 | ||
252 | warn_report("'-soundhw pcspk' is deprecated, " | |
253 | "please set a backend using '-machine pcspk-audiodev=<name>' instead"); | |
254 | return pcspk_audio_init(s); | |
255 | } | |
256 | ||
302fe51b JK |
257 | static void pcspk_register(void) |
258 | { | |
259 | type_register_static(&pcspk_info); | |
2e16ec05 | 260 | isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init_soundhw); |
fd06c375 | 261 | } |
302fe51b | 262 | type_init(pcspk_register) |