Statistics
| Branch: | Revision:

root / hw / audio / pcspk.c @ a8aec295

History | View | Annotate | Download (5.7 kB)

1 fd06c375 bellard
/*
2 fd06c375 bellard
 * QEMU PC speaker emulation
3 fd06c375 bellard
 *
4 fd06c375 bellard
 * Copyright (c) 2006 Joachim Henke
5 fd06c375 bellard
 *
6 fd06c375 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 fd06c375 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 fd06c375 bellard
 * in the Software without restriction, including without limitation the rights
9 fd06c375 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 fd06c375 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 fd06c375 bellard
 * furnished to do so, subject to the following conditions:
12 fd06c375 bellard
 *
13 fd06c375 bellard
 * The above copyright notice and this permission notice shall be included in
14 fd06c375 bellard
 * all copies or substantial portions of the Software.
15 fd06c375 bellard
 *
16 fd06c375 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 fd06c375 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 fd06c375 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 fd06c375 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 fd06c375 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 fd06c375 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 fd06c375 bellard
 * THE SOFTWARE.
23 fd06c375 bellard
 */
24 fd06c375 bellard
25 83c9f4ca Paolo Bonzini
#include "hw/hw.h"
26 0d09e41a Paolo Bonzini
#include "hw/i386/pc.h"
27 0d09e41a Paolo Bonzini
#include "hw/isa/isa.h"
28 36cd6f6f Paolo Bonzini
#include "hw/audio/audio.h"
29 87ecb68b pbrook
#include "audio/audio.h"
30 1de7afc9 Paolo Bonzini
#include "qemu/timer.h"
31 0d09e41a Paolo Bonzini
#include "hw/timer/i8254.h"
32 0d09e41a Paolo Bonzini
#include "hw/audio/pcspk.h"
33 fd06c375 bellard
34 fd06c375 bellard
#define PCSPK_BUF_LEN 1792
35 fd06c375 bellard
#define PCSPK_SAMPLE_RATE 32000
36 fd06c375 bellard
#define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
37 fd06c375 bellard
#define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ)
38 fd06c375 bellard
39 d367ece5 Andreas Färber
#define PC_SPEAKER(obj) OBJECT_CHECK(PCSpkState, (obj), TYPE_PC_SPEAKER)
40 d367ece5 Andreas Färber
41 fd06c375 bellard
typedef struct {
42 d367ece5 Andreas Färber
    ISADevice parent_obj;
43 d367ece5 Andreas Färber
44 302fe51b Jan Kiszka
    MemoryRegion ioport;
45 302fe51b Jan Kiszka
    uint32_t iobase;
46 fd06c375 bellard
    uint8_t sample_buf[PCSPK_BUF_LEN];
47 fd06c375 bellard
    QEMUSoundCard card;
48 fd06c375 bellard
    SWVoiceOut *voice;
49 302fe51b Jan Kiszka
    void *pit;
50 fd06c375 bellard
    unsigned int pit_count;
51 fd06c375 bellard
    unsigned int samples;
52 fd06c375 bellard
    unsigned int play_pos;
53 fd06c375 bellard
    int data_on;
54 fd06c375 bellard
    int dummy_refresh_clock;
55 fd06c375 bellard
} PCSpkState;
56 fd06c375 bellard
57 fd06c375 bellard
static const char *s_spk = "pcspk";
58 302fe51b Jan Kiszka
static PCSpkState *pcspk_state;
59 fd06c375 bellard
60 fd06c375 bellard
static inline void generate_samples(PCSpkState *s)
61 fd06c375 bellard
{
62 fd06c375 bellard
    unsigned int i;
63 fd06c375 bellard
64 fd06c375 bellard
    if (s->pit_count) {
65 fd06c375 bellard
        const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
66 fd06c375 bellard
        const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
67 fd06c375 bellard
68 fd06c375 bellard
        /* multiple of wavelength for gapless looping */
69 fd06c375 bellard
        s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1;
70 fd06c375 bellard
        for (i = 0; i < s->samples; ++i)
71 fd06c375 bellard
            s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
72 fd06c375 bellard
    } else {
73 fd06c375 bellard
        s->samples = PCSPK_BUF_LEN;
74 fd06c375 bellard
        for (i = 0; i < PCSPK_BUF_LEN; ++i)
75 fd06c375 bellard
            s->sample_buf[i] = 128; /* silence */
76 fd06c375 bellard
    }
77 fd06c375 bellard
}
78 fd06c375 bellard
79 fd06c375 bellard
static void pcspk_callback(void *opaque, int free)
80 fd06c375 bellard
{
81 fd06c375 bellard
    PCSpkState *s = opaque;
82 4aa5d285 Jan Kiszka
    PITChannelInfo ch;
83 fd06c375 bellard
    unsigned int n;
84 fd06c375 bellard
85 4aa5d285 Jan Kiszka
    pit_get_channel_info(s->pit, 2, &ch);
86 4aa5d285 Jan Kiszka
87 4aa5d285 Jan Kiszka
    if (ch.mode != 3) {
88 fd06c375 bellard
        return;
89 4aa5d285 Jan Kiszka
    }
90 fd06c375 bellard
91 4aa5d285 Jan Kiszka
    n = ch.initial_count;
92 fd06c375 bellard
    /* avoid frequencies that are not reproducible with sample rate */
93 fd06c375 bellard
    if (n < PCSPK_MIN_COUNT)
94 fd06c375 bellard
        n = 0;
95 fd06c375 bellard
96 fd06c375 bellard
    if (s->pit_count != n) {
97 fd06c375 bellard
        s->pit_count = n;
98 fd06c375 bellard
        s->play_pos = 0;
99 fd06c375 bellard
        generate_samples(s);
100 fd06c375 bellard
    }
101 fd06c375 bellard
102 fd06c375 bellard
    while (free > 0) {
103 fd06c375 bellard
        n = audio_MIN(s->samples - s->play_pos, (unsigned int)free);
104 fd06c375 bellard
        n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
105 fd06c375 bellard
        if (!n)
106 fd06c375 bellard
            break;
107 fd06c375 bellard
        s->play_pos = (s->play_pos + n) % s->samples;
108 fd06c375 bellard
        free -= n;
109 fd06c375 bellard
    }
110 fd06c375 bellard
}
111 fd06c375 bellard
112 36cd6f6f Paolo Bonzini
static int pcspk_audio_init(ISABus *bus)
113 fd06c375 bellard
{
114 302fe51b Jan Kiszka
    PCSpkState *s = pcspk_state;
115 1ea879e5 malc
    struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0};
116 fd06c375 bellard
117 1a7dafce malc
    AUD_register_card(s_spk, &s->card);
118 fd06c375 bellard
119 d929eba5 bellard
    s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
120 fd06c375 bellard
    if (!s->voice) {
121 fd06c375 bellard
        AUD_log(s_spk, "Could not open voice\n");
122 fd06c375 bellard
        return -1;
123 fd06c375 bellard
    }
124 fd06c375 bellard
125 fd06c375 bellard
    return 0;
126 fd06c375 bellard
}
127 fd06c375 bellard
128 a8170e5e Avi Kivity
static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
129 302fe51b Jan Kiszka
                              unsigned size)
130 fd06c375 bellard
{
131 fd06c375 bellard
    PCSpkState *s = opaque;
132 4aa5d285 Jan Kiszka
    PITChannelInfo ch;
133 4aa5d285 Jan Kiszka
134 4aa5d285 Jan Kiszka
    pit_get_channel_info(s->pit, 2, &ch);
135 fd06c375 bellard
136 fd06c375 bellard
    s->dummy_refresh_clock ^= (1 << 4);
137 fd06c375 bellard
138 4aa5d285 Jan Kiszka
    return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
139 4aa5d285 Jan Kiszka
       (ch.out << 5);
140 fd06c375 bellard
}
141 fd06c375 bellard
142 a8170e5e Avi Kivity
static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
143 302fe51b Jan Kiszka
                           unsigned size)
144 fd06c375 bellard
{
145 fd06c375 bellard
    PCSpkState *s = opaque;
146 fd06c375 bellard
    const int gate = val & 1;
147 fd06c375 bellard
148 fd06c375 bellard
    s->data_on = (val >> 1) & 1;
149 fd06c375 bellard
    pit_set_gate(s->pit, 2, gate);
150 fd06c375 bellard
    if (s->voice) {
151 fd06c375 bellard
        if (gate) /* restart */
152 fd06c375 bellard
            s->play_pos = 0;
153 fd06c375 bellard
        AUD_set_active_out(s->voice, gate & s->data_on);
154 fd06c375 bellard
    }
155 fd06c375 bellard
}
156 fd06c375 bellard
157 302fe51b Jan Kiszka
static const MemoryRegionOps pcspk_io_ops = {
158 302fe51b Jan Kiszka
    .read = pcspk_io_read,
159 302fe51b Jan Kiszka
    .write = pcspk_io_write,
160 302fe51b Jan Kiszka
    .impl = {
161 302fe51b Jan Kiszka
        .min_access_size = 1,
162 302fe51b Jan Kiszka
        .max_access_size = 1,
163 302fe51b Jan Kiszka
    },
164 302fe51b Jan Kiszka
};
165 302fe51b Jan Kiszka
166 db895a1e Andreas Färber
static void pcspk_initfn(Object *obj)
167 fd06c375 bellard
{
168 db895a1e Andreas Färber
    PCSpkState *s = PC_SPEAKER(obj);
169 302fe51b Jan Kiszka
170 302fe51b Jan Kiszka
    memory_region_init_io(&s->ioport, &pcspk_io_ops, s, "elcr", 1);
171 db895a1e Andreas Färber
}
172 302fe51b Jan Kiszka
173 db895a1e Andreas Färber
static void pcspk_realizefn(DeviceState *dev, Error **errp)
174 db895a1e Andreas Färber
{
175 db895a1e Andreas Färber
    ISADevice *isadev = ISA_DEVICE(dev);
176 db895a1e Andreas Färber
    PCSpkState *s = PC_SPEAKER(dev);
177 302fe51b Jan Kiszka
178 db895a1e Andreas Färber
    isa_register_ioport(isadev, &s->ioport, s->iobase);
179 db895a1e Andreas Färber
180 db895a1e Andreas Färber
    pcspk_state = s;
181 302fe51b Jan Kiszka
}
182 302fe51b Jan Kiszka
183 302fe51b Jan Kiszka
static Property pcspk_properties[] = {
184 302fe51b Jan Kiszka
    DEFINE_PROP_HEX32("iobase", PCSpkState, iobase,  -1),
185 302fe51b Jan Kiszka
    DEFINE_PROP_PTR("pit", PCSpkState, pit),
186 302fe51b Jan Kiszka
    DEFINE_PROP_END_OF_LIST(),
187 302fe51b Jan Kiszka
};
188 fd06c375 bellard
189 302fe51b Jan Kiszka
static void pcspk_class_initfn(ObjectClass *klass, void *data)
190 302fe51b Jan Kiszka
{
191 302fe51b Jan Kiszka
    DeviceClass *dc = DEVICE_CLASS(klass);
192 302fe51b Jan Kiszka
193 db895a1e Andreas Färber
    dc->realize = pcspk_realizefn;
194 302fe51b Jan Kiszka
    dc->no_user = 1;
195 302fe51b Jan Kiszka
    dc->props = pcspk_properties;
196 302fe51b Jan Kiszka
}
197 302fe51b Jan Kiszka
198 8c43a6f0 Andreas Färber
static const TypeInfo pcspk_info = {
199 d367ece5 Andreas Färber
    .name           = TYPE_PC_SPEAKER,
200 302fe51b Jan Kiszka
    .parent         = TYPE_ISA_DEVICE,
201 302fe51b Jan Kiszka
    .instance_size  = sizeof(PCSpkState),
202 db895a1e Andreas Färber
    .instance_init  = pcspk_initfn,
203 302fe51b Jan Kiszka
    .class_init     = pcspk_class_initfn,
204 302fe51b Jan Kiszka
};
205 302fe51b Jan Kiszka
206 302fe51b Jan Kiszka
static void pcspk_register(void)
207 302fe51b Jan Kiszka
{
208 302fe51b Jan Kiszka
    type_register_static(&pcspk_info);
209 36cd6f6f Paolo Bonzini
    isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init);
210 fd06c375 bellard
}
211 302fe51b Jan Kiszka
type_init(pcspk_register)