root / hw / pcspk.c @ 93148aa5
History | View | Annotate | Download (5.4 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 | 87ecb68b | pbrook | #include "hw.h" |
26 | 87ecb68b | pbrook | #include "pc.h" |
27 | 87ecb68b | pbrook | #include "isa.h" |
28 | 87ecb68b | pbrook | #include "audio/audio.h" |
29 | 87ecb68b | pbrook | #include "qemu-timer.h" |
30 | b1277b03 | Jan Kiszka | #include "i8254.h" |
31 | 302fe51b | Jan Kiszka | #include "pcspk.h" |
32 | fd06c375 | bellard | |
33 | fd06c375 | bellard | #define PCSPK_BUF_LEN 1792 |
34 | fd06c375 | bellard | #define PCSPK_SAMPLE_RATE 32000 |
35 | fd06c375 | bellard | #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1) |
36 | fd06c375 | bellard | #define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ) |
37 | fd06c375 | bellard | |
38 | fd06c375 | bellard | typedef struct { |
39 | 302fe51b | Jan Kiszka | ISADevice dev; |
40 | 302fe51b | Jan Kiszka | MemoryRegion ioport; |
41 | 302fe51b | Jan Kiszka | uint32_t iobase; |
42 | fd06c375 | bellard | uint8_t sample_buf[PCSPK_BUF_LEN]; |
43 | fd06c375 | bellard | QEMUSoundCard card; |
44 | fd06c375 | bellard | SWVoiceOut *voice; |
45 | 302fe51b | Jan Kiszka | void *pit;
|
46 | fd06c375 | bellard | unsigned int pit_count; |
47 | fd06c375 | bellard | unsigned int samples; |
48 | fd06c375 | bellard | unsigned int play_pos; |
49 | fd06c375 | bellard | int data_on;
|
50 | fd06c375 | bellard | int dummy_refresh_clock;
|
51 | fd06c375 | bellard | } PCSpkState; |
52 | fd06c375 | bellard | |
53 | fd06c375 | bellard | static const char *s_spk = "pcspk"; |
54 | 302fe51b | Jan Kiszka | static PCSpkState *pcspk_state;
|
55 | fd06c375 | bellard | |
56 | fd06c375 | bellard | static inline void generate_samples(PCSpkState *s) |
57 | fd06c375 | bellard | { |
58 | fd06c375 | bellard | unsigned int i; |
59 | fd06c375 | bellard | |
60 | fd06c375 | bellard | if (s->pit_count) {
|
61 | fd06c375 | bellard | const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
|
62 | fd06c375 | bellard | const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m; |
63 | fd06c375 | bellard | |
64 | fd06c375 | bellard | /* multiple of wavelength for gapless looping */
|
65 | fd06c375 | bellard | s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1; |
66 | fd06c375 | bellard | for (i = 0; i < s->samples; ++i) |
67 | fd06c375 | bellard | s->sample_buf[i] = (64 & (n * i >> 25)) - 32; |
68 | fd06c375 | bellard | } else {
|
69 | fd06c375 | bellard | s->samples = PCSPK_BUF_LEN; |
70 | fd06c375 | bellard | for (i = 0; i < PCSPK_BUF_LEN; ++i) |
71 | fd06c375 | bellard | s->sample_buf[i] = 128; /* silence */ |
72 | fd06c375 | bellard | } |
73 | fd06c375 | bellard | } |
74 | fd06c375 | bellard | |
75 | fd06c375 | bellard | static void pcspk_callback(void *opaque, int free) |
76 | fd06c375 | bellard | { |
77 | fd06c375 | bellard | PCSpkState *s = opaque; |
78 | 4aa5d285 | Jan Kiszka | PITChannelInfo ch; |
79 | fd06c375 | bellard | unsigned int n; |
80 | fd06c375 | bellard | |
81 | 4aa5d285 | Jan Kiszka | pit_get_channel_info(s->pit, 2, &ch);
|
82 | 4aa5d285 | Jan Kiszka | |
83 | 4aa5d285 | Jan Kiszka | if (ch.mode != 3) { |
84 | fd06c375 | bellard | return;
|
85 | 4aa5d285 | Jan Kiszka | } |
86 | fd06c375 | bellard | |
87 | 4aa5d285 | Jan Kiszka | n = ch.initial_count; |
88 | fd06c375 | bellard | /* avoid frequencies that are not reproducible with sample rate */
|
89 | fd06c375 | bellard | if (n < PCSPK_MIN_COUNT)
|
90 | fd06c375 | bellard | n = 0;
|
91 | fd06c375 | bellard | |
92 | fd06c375 | bellard | if (s->pit_count != n) {
|
93 | fd06c375 | bellard | s->pit_count = n; |
94 | fd06c375 | bellard | s->play_pos = 0;
|
95 | fd06c375 | bellard | generate_samples(s); |
96 | fd06c375 | bellard | } |
97 | fd06c375 | bellard | |
98 | fd06c375 | bellard | while (free > 0) { |
99 | fd06c375 | bellard | n = audio_MIN(s->samples - s->play_pos, (unsigned int)free); |
100 | fd06c375 | bellard | n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n); |
101 | fd06c375 | bellard | if (!n)
|
102 | fd06c375 | bellard | break;
|
103 | fd06c375 | bellard | s->play_pos = (s->play_pos + n) % s->samples; |
104 | fd06c375 | bellard | free -= n; |
105 | fd06c375 | bellard | } |
106 | fd06c375 | bellard | } |
107 | fd06c375 | bellard | |
108 | 4a0f031d | Hervé Poussineau | int pcspk_audio_init(ISABus *bus)
|
109 | fd06c375 | bellard | { |
110 | 302fe51b | Jan Kiszka | PCSpkState *s = pcspk_state; |
111 | 1ea879e5 | malc | struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0}; |
112 | fd06c375 | bellard | |
113 | 1a7dafce | malc | AUD_register_card(s_spk, &s->card); |
114 | fd06c375 | bellard | |
115 | d929eba5 | bellard | s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as); |
116 | fd06c375 | bellard | if (!s->voice) {
|
117 | fd06c375 | bellard | AUD_log(s_spk, "Could not open voice\n");
|
118 | fd06c375 | bellard | return -1; |
119 | fd06c375 | bellard | } |
120 | fd06c375 | bellard | |
121 | fd06c375 | bellard | return 0; |
122 | fd06c375 | bellard | } |
123 | fd06c375 | bellard | |
124 | 302fe51b | Jan Kiszka | static uint64_t pcspk_io_read(void *opaque, target_phys_addr_t addr, |
125 | 302fe51b | Jan Kiszka | unsigned size)
|
126 | fd06c375 | bellard | { |
127 | fd06c375 | bellard | PCSpkState *s = opaque; |
128 | 4aa5d285 | Jan Kiszka | PITChannelInfo ch; |
129 | 4aa5d285 | Jan Kiszka | |
130 | 4aa5d285 | Jan Kiszka | pit_get_channel_info(s->pit, 2, &ch);
|
131 | fd06c375 | bellard | |
132 | fd06c375 | bellard | s->dummy_refresh_clock ^= (1 << 4); |
133 | fd06c375 | bellard | |
134 | 4aa5d285 | Jan Kiszka | return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock | |
135 | 4aa5d285 | Jan Kiszka | (ch.out << 5);
|
136 | fd06c375 | bellard | } |
137 | fd06c375 | bellard | |
138 | 302fe51b | Jan Kiszka | static void pcspk_io_write(void *opaque, target_phys_addr_t addr, uint64_t val, |
139 | 302fe51b | Jan Kiszka | unsigned size)
|
140 | fd06c375 | bellard | { |
141 | fd06c375 | bellard | PCSpkState *s = opaque; |
142 | fd06c375 | bellard | const int gate = val & 1; |
143 | fd06c375 | bellard | |
144 | fd06c375 | bellard | s->data_on = (val >> 1) & 1; |
145 | fd06c375 | bellard | pit_set_gate(s->pit, 2, gate);
|
146 | fd06c375 | bellard | if (s->voice) {
|
147 | fd06c375 | bellard | if (gate) /* restart */ |
148 | fd06c375 | bellard | s->play_pos = 0;
|
149 | fd06c375 | bellard | AUD_set_active_out(s->voice, gate & s->data_on); |
150 | fd06c375 | bellard | } |
151 | fd06c375 | bellard | } |
152 | fd06c375 | bellard | |
153 | 302fe51b | Jan Kiszka | static const MemoryRegionOps pcspk_io_ops = { |
154 | 302fe51b | Jan Kiszka | .read = pcspk_io_read, |
155 | 302fe51b | Jan Kiszka | .write = pcspk_io_write, |
156 | 302fe51b | Jan Kiszka | .impl = { |
157 | 302fe51b | Jan Kiszka | .min_access_size = 1,
|
158 | 302fe51b | Jan Kiszka | .max_access_size = 1,
|
159 | 302fe51b | Jan Kiszka | }, |
160 | 302fe51b | Jan Kiszka | }; |
161 | 302fe51b | Jan Kiszka | |
162 | 302fe51b | Jan Kiszka | static int pcspk_initfn(ISADevice *dev) |
163 | fd06c375 | bellard | { |
164 | 302fe51b | Jan Kiszka | PCSpkState *s = DO_UPCAST(PCSpkState, dev, dev); |
165 | 302fe51b | Jan Kiszka | |
166 | 302fe51b | Jan Kiszka | memory_region_init_io(&s->ioport, &pcspk_io_ops, s, "elcr", 1); |
167 | 302fe51b | Jan Kiszka | isa_register_ioport(dev, &s->ioport, s->iobase); |
168 | 302fe51b | Jan Kiszka | |
169 | 302fe51b | Jan Kiszka | pcspk_state = s; |
170 | 302fe51b | Jan Kiszka | |
171 | 302fe51b | Jan Kiszka | return 0; |
172 | 302fe51b | Jan Kiszka | } |
173 | 302fe51b | Jan Kiszka | |
174 | 302fe51b | Jan Kiszka | static Property pcspk_properties[] = {
|
175 | 302fe51b | Jan Kiszka | DEFINE_PROP_HEX32("iobase", PCSpkState, iobase, -1), |
176 | 302fe51b | Jan Kiszka | DEFINE_PROP_PTR("pit", PCSpkState, pit),
|
177 | 302fe51b | Jan Kiszka | DEFINE_PROP_END_OF_LIST(), |
178 | 302fe51b | Jan Kiszka | }; |
179 | fd06c375 | bellard | |
180 | 302fe51b | Jan Kiszka | static void pcspk_class_initfn(ObjectClass *klass, void *data) |
181 | 302fe51b | Jan Kiszka | { |
182 | 302fe51b | Jan Kiszka | DeviceClass *dc = DEVICE_CLASS(klass); |
183 | 302fe51b | Jan Kiszka | ISADeviceClass *ic = ISA_DEVICE_CLASS(klass); |
184 | 302fe51b | Jan Kiszka | |
185 | 302fe51b | Jan Kiszka | ic->init = pcspk_initfn; |
186 | 302fe51b | Jan Kiszka | dc->no_user = 1;
|
187 | 302fe51b | Jan Kiszka | dc->props = pcspk_properties; |
188 | 302fe51b | Jan Kiszka | } |
189 | 302fe51b | Jan Kiszka | |
190 | 302fe51b | Jan Kiszka | static TypeInfo pcspk_info = {
|
191 | 302fe51b | Jan Kiszka | .name = "isa-pcspk",
|
192 | 302fe51b | Jan Kiszka | .parent = TYPE_ISA_DEVICE, |
193 | 302fe51b | Jan Kiszka | .instance_size = sizeof(PCSpkState),
|
194 | 302fe51b | Jan Kiszka | .class_init = pcspk_class_initfn, |
195 | 302fe51b | Jan Kiszka | }; |
196 | 302fe51b | Jan Kiszka | |
197 | 302fe51b | Jan Kiszka | static void pcspk_register(void) |
198 | 302fe51b | Jan Kiszka | { |
199 | 302fe51b | Jan Kiszka | type_register_static(&pcspk_info); |
200 | fd06c375 | bellard | } |
201 | 302fe51b | Jan Kiszka | type_init(pcspk_register) |