]> git.proxmox.com Git - qemu.git/blame - hw/audio/pcspk.c
memory: add owner argument to initialization functions
[qemu.git] / hw / audio / pcspk.c
CommitLineData
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
83c9f4ca 25#include "hw/hw.h"
0d09e41a
PB
26#include "hw/i386/pc.h"
27#include "hw/isa/isa.h"
36cd6f6f 28#include "hw/audio/audio.h"
87ecb68b 29#include "audio/audio.h"
1de7afc9 30#include "qemu/timer.h"
0d09e41a
PB
31#include "hw/timer/i8254.h"
32#include "hw/audio/pcspk.h"
fd06c375
FB
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
d367ece5
AF
39#define PC_SPEAKER(obj) OBJECT_CHECK(PCSpkState, (obj), TYPE_PC_SPEAKER)
40
fd06c375 41typedef struct {
d367ece5
AF
42 ISADevice parent_obj;
43
302fe51b
JK
44 MemoryRegion ioport;
45 uint32_t iobase;
fd06c375
FB
46 uint8_t sample_buf[PCSPK_BUF_LEN];
47 QEMUSoundCard card;
48 SWVoiceOut *voice;
302fe51b 49 void *pit;
fd06c375
FB
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
57static const char *s_spk = "pcspk";
302fe51b 58static PCSpkState *pcspk_state;
fd06c375
FB
59
60static 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
79static void pcspk_callback(void *opaque, int free)
80{
81 PCSpkState *s = opaque;
4aa5d285 82 PITChannelInfo ch;
fd06c375
FB
83 unsigned int n;
84
4aa5d285
JK
85 pit_get_channel_info(s->pit, 2, &ch);
86
87 if (ch.mode != 3) {
fd06c375 88 return;
4aa5d285 89 }
fd06c375 90
4aa5d285 91 n = ch.initial_count;
fd06c375
FB
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
36cd6f6f 112static int pcspk_audio_init(ISABus *bus)
fd06c375 113{
302fe51b 114 PCSpkState *s = pcspk_state;
1ea879e5 115 struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0};
fd06c375 116
1a7dafce 117 AUD_register_card(s_spk, &s->card);
fd06c375 118
d929eba5 119 s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
fd06c375
FB
120 if (!s->voice) {
121 AUD_log(s_spk, "Could not open voice\n");
122 return -1;
123 }
124
125 return 0;
126}
127
a8170e5e 128static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
302fe51b 129 unsigned size)
fd06c375
FB
130{
131 PCSpkState *s = opaque;
4aa5d285
JK
132 PITChannelInfo ch;
133
134 pit_get_channel_info(s->pit, 2, &ch);
fd06c375
FB
135
136 s->dummy_refresh_clock ^= (1 << 4);
fd06c375 137
4aa5d285
JK
138 return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
139 (ch.out << 5);
fd06c375
FB
140}
141
a8170e5e 142static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
302fe51b 143 unsigned size)
fd06c375
FB
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
302fe51b
JK
157static 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};
165
db895a1e 166static void pcspk_initfn(Object *obj)
fd06c375 167{
db895a1e 168 PCSpkState *s = PC_SPEAKER(obj);
302fe51b 169
2c9b15ca 170 memory_region_init_io(&s->ioport, NULL, &pcspk_io_ops, s, "elcr", 1);
db895a1e 171}
302fe51b 172
db895a1e
AF
173static void pcspk_realizefn(DeviceState *dev, Error **errp)
174{
175 ISADevice *isadev = ISA_DEVICE(dev);
176 PCSpkState *s = PC_SPEAKER(dev);
302fe51b 177
db895a1e
AF
178 isa_register_ioport(isadev, &s->ioport, s->iobase);
179
180 pcspk_state = s;
302fe51b
JK
181}
182
183static Property pcspk_properties[] = {
184 DEFINE_PROP_HEX32("iobase", PCSpkState, iobase, -1),
185 DEFINE_PROP_PTR("pit", PCSpkState, pit),
186 DEFINE_PROP_END_OF_LIST(),
187};
fd06c375 188
302fe51b
JK
189static void pcspk_class_initfn(ObjectClass *klass, void *data)
190{
191 DeviceClass *dc = DEVICE_CLASS(klass);
302fe51b 192
db895a1e 193 dc->realize = pcspk_realizefn;
302fe51b
JK
194 dc->no_user = 1;
195 dc->props = pcspk_properties;
196}
197
8c43a6f0 198static const TypeInfo pcspk_info = {
d367ece5 199 .name = TYPE_PC_SPEAKER,
302fe51b
JK
200 .parent = TYPE_ISA_DEVICE,
201 .instance_size = sizeof(PCSpkState),
db895a1e 202 .instance_init = pcspk_initfn,
302fe51b
JK
203 .class_init = pcspk_class_initfn,
204};
205
206static void pcspk_register(void)
207{
208 type_register_static(&pcspk_info);
36cd6f6f 209 isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init);
fd06c375 210}
302fe51b 211type_init(pcspk_register)