]>
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" |
83c9f4ca | 26 | #include "hw/hw.h" |
0d09e41a PB |
27 | #include "hw/i386/pc.h" |
28 | #include "hw/isa/isa.h" | |
36cd6f6f | 29 | #include "hw/audio/audio.h" |
87ecb68b | 30 | #include "audio/audio.h" |
1de7afc9 | 31 | #include "qemu/timer.h" |
0d09e41a PB |
32 | #include "hw/timer/i8254.h" |
33 | #include "hw/audio/pcspk.h" | |
873b4d3f | 34 | #include "qapi/error.h" |
fd06c375 FB |
35 | |
36 | #define PCSPK_BUF_LEN 1792 | |
37 | #define PCSPK_SAMPLE_RATE 32000 | |
38 | #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1) | |
b988a650 | 39 | #define PCSPK_MIN_COUNT DIV_ROUND_UP(PIT_FREQ, PCSPK_MAX_FREQ) |
fd06c375 | 40 | |
d367ece5 AF |
41 | #define PC_SPEAKER(obj) OBJECT_CHECK(PCSpkState, (obj), TYPE_PC_SPEAKER) |
42 | ||
fd06c375 | 43 | typedef struct { |
d367ece5 AF |
44 | ISADevice parent_obj; |
45 | ||
302fe51b JK |
46 | MemoryRegion ioport; |
47 | uint32_t iobase; | |
fd06c375 FB |
48 | uint8_t sample_buf[PCSPK_BUF_LEN]; |
49 | QEMUSoundCard card; | |
50 | SWVoiceOut *voice; | |
302fe51b | 51 | void *pit; |
fd06c375 FB |
52 | unsigned int pit_count; |
53 | unsigned int samples; | |
54 | unsigned int play_pos; | |
55 | int data_on; | |
56 | int dummy_refresh_clock; | |
57 | } PCSpkState; | |
58 | ||
59 | static const char *s_spk = "pcspk"; | |
302fe51b | 60 | static PCSpkState *pcspk_state; |
fd06c375 FB |
61 | |
62 | static inline void generate_samples(PCSpkState *s) | |
63 | { | |
64 | unsigned int i; | |
65 | ||
66 | if (s->pit_count) { | |
67 | const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count; | |
68 | const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m; | |
69 | ||
70 | /* multiple of wavelength for gapless looping */ | |
71 | s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1; | |
72 | for (i = 0; i < s->samples; ++i) | |
73 | s->sample_buf[i] = (64 & (n * i >> 25)) - 32; | |
74 | } else { | |
75 | s->samples = PCSPK_BUF_LEN; | |
76 | for (i = 0; i < PCSPK_BUF_LEN; ++i) | |
77 | s->sample_buf[i] = 128; /* silence */ | |
78 | } | |
79 | } | |
80 | ||
81 | static void pcspk_callback(void *opaque, int free) | |
82 | { | |
83 | PCSpkState *s = opaque; | |
4aa5d285 | 84 | PITChannelInfo ch; |
fd06c375 FB |
85 | unsigned int n; |
86 | ||
4aa5d285 JK |
87 | pit_get_channel_info(s->pit, 2, &ch); |
88 | ||
89 | if (ch.mode != 3) { | |
fd06c375 | 90 | return; |
4aa5d285 | 91 | } |
fd06c375 | 92 | |
4aa5d285 | 93 | n = ch.initial_count; |
fd06c375 FB |
94 | /* avoid frequencies that are not reproducible with sample rate */ |
95 | if (n < PCSPK_MIN_COUNT) | |
96 | n = 0; | |
97 | ||
98 | if (s->pit_count != n) { | |
99 | s->pit_count = n; | |
100 | s->play_pos = 0; | |
101 | generate_samples(s); | |
102 | } | |
103 | ||
104 | while (free > 0) { | |
105 | n = audio_MIN(s->samples - s->play_pos, (unsigned int)free); | |
106 | n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n); | |
107 | if (!n) | |
108 | break; | |
109 | s->play_pos = (s->play_pos + n) % s->samples; | |
110 | free -= n; | |
111 | } | |
112 | } | |
113 | ||
36cd6f6f | 114 | static int pcspk_audio_init(ISABus *bus) |
fd06c375 | 115 | { |
302fe51b | 116 | PCSpkState *s = pcspk_state; |
1ea879e5 | 117 | struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0}; |
fd06c375 | 118 | |
1a7dafce | 119 | AUD_register_card(s_spk, &s->card); |
fd06c375 | 120 | |
d929eba5 | 121 | s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as); |
fd06c375 FB |
122 | if (!s->voice) { |
123 | AUD_log(s_spk, "Could not open voice\n"); | |
124 | return -1; | |
125 | } | |
126 | ||
127 | return 0; | |
128 | } | |
129 | ||
a8170e5e | 130 | static uint64_t pcspk_io_read(void *opaque, hwaddr addr, |
302fe51b | 131 | unsigned size) |
fd06c375 FB |
132 | { |
133 | PCSpkState *s = opaque; | |
4aa5d285 JK |
134 | PITChannelInfo ch; |
135 | ||
136 | pit_get_channel_info(s->pit, 2, &ch); | |
fd06c375 FB |
137 | |
138 | s->dummy_refresh_clock ^= (1 << 4); | |
fd06c375 | 139 | |
4aa5d285 JK |
140 | return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock | |
141 | (ch.out << 5); | |
fd06c375 FB |
142 | } |
143 | ||
a8170e5e | 144 | static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val, |
302fe51b | 145 | unsigned size) |
fd06c375 FB |
146 | { |
147 | PCSpkState *s = opaque; | |
148 | const int gate = val & 1; | |
149 | ||
150 | s->data_on = (val >> 1) & 1; | |
151 | pit_set_gate(s->pit, 2, gate); | |
152 | if (s->voice) { | |
153 | if (gate) /* restart */ | |
154 | s->play_pos = 0; | |
155 | AUD_set_active_out(s->voice, gate & s->data_on); | |
156 | } | |
157 | } | |
158 | ||
302fe51b JK |
159 | static const MemoryRegionOps pcspk_io_ops = { |
160 | .read = pcspk_io_read, | |
161 | .write = pcspk_io_write, | |
162 | .impl = { | |
163 | .min_access_size = 1, | |
164 | .max_access_size = 1, | |
165 | }, | |
166 | }; | |
167 | ||
db895a1e | 168 | static void pcspk_initfn(Object *obj) |
fd06c375 | 169 | { |
db895a1e | 170 | PCSpkState *s = PC_SPEAKER(obj); |
302fe51b | 171 | |
ecf2e5a4 | 172 | memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "pcspk", 1); |
873b4d3f | 173 | |
8a0b4de0 | 174 | object_property_add_link(obj, "pit", TYPE_PIT_COMMON, |
873b4d3f EV |
175 | (Object **)&s->pit, |
176 | qdev_prop_allow_set_link_before_realize, | |
177 | 0, &error_abort); | |
db895a1e | 178 | } |
302fe51b | 179 | |
db895a1e AF |
180 | static void pcspk_realizefn(DeviceState *dev, Error **errp) |
181 | { | |
182 | ISADevice *isadev = ISA_DEVICE(dev); | |
183 | PCSpkState *s = PC_SPEAKER(dev); | |
302fe51b | 184 | |
db895a1e AF |
185 | isa_register_ioport(isadev, &s->ioport, s->iobase); |
186 | ||
187 | pcspk_state = s; | |
302fe51b JK |
188 | } |
189 | ||
190 | static Property pcspk_properties[] = { | |
c7bcc85d | 191 | DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, -1), |
302fe51b JK |
192 | DEFINE_PROP_END_OF_LIST(), |
193 | }; | |
fd06c375 | 194 | |
302fe51b JK |
195 | static void pcspk_class_initfn(ObjectClass *klass, void *data) |
196 | { | |
197 | DeviceClass *dc = DEVICE_CLASS(klass); | |
302fe51b | 198 | |
db895a1e | 199 | dc->realize = pcspk_realizefn; |
125ee0ed | 200 | set_bit(DEVICE_CATEGORY_SOUND, dc->categories); |
302fe51b | 201 | dc->props = pcspk_properties; |
873b4d3f | 202 | /* Reason: realize sets global pcspk_state */ |
f3b17640 | 203 | dc->cannot_instantiate_with_device_add_yet = true; |
302fe51b JK |
204 | } |
205 | ||
8c43a6f0 | 206 | static const TypeInfo pcspk_info = { |
d367ece5 | 207 | .name = TYPE_PC_SPEAKER, |
302fe51b JK |
208 | .parent = TYPE_ISA_DEVICE, |
209 | .instance_size = sizeof(PCSpkState), | |
db895a1e | 210 | .instance_init = pcspk_initfn, |
302fe51b JK |
211 | .class_init = pcspk_class_initfn, |
212 | }; | |
213 | ||
214 | static void pcspk_register(void) | |
215 | { | |
216 | type_register_static(&pcspk_info); | |
36cd6f6f | 217 | isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init); |
fd06c375 | 218 | } |
302fe51b | 219 | type_init(pcspk_register) |