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