blob: 1d81bbe6aa5218c3bd64645a8f7b339e50806fc5 [file] [log] [blame]
bellardfd06c372006-04-24 21:58:30 +00001/*
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
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010026#include "hw/i386/pc.h"
27#include "hw/isa/isa.h"
Paolo Bonzini36cd6f62013-04-18 18:43:58 +020028#include "hw/audio/audio.h"
pbrook87ecb682007-11-17 17:14:51 +000029#include "audio/audio.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/timer.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010031#include "hw/timer/i8254.h"
32#include "hw/audio/pcspk.h"
bellardfd06c372006-04-24 21:58:30 +000033
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
Andreas Färberd367ece2013-04-27 22:18:48 +020039#define PC_SPEAKER(obj) OBJECT_CHECK(PCSpkState, (obj), TYPE_PC_SPEAKER)
40
bellardfd06c372006-04-24 21:58:30 +000041typedef struct {
Andreas Färberd367ece2013-04-27 22:18:48 +020042 ISADevice parent_obj;
43
Jan Kiszka302fe512012-02-17 11:24:34 +010044 MemoryRegion ioport;
45 uint32_t iobase;
bellardfd06c372006-04-24 21:58:30 +000046 uint8_t sample_buf[PCSPK_BUF_LEN];
47 QEMUSoundCard card;
48 SWVoiceOut *voice;
Jan Kiszka302fe512012-02-17 11:24:34 +010049 void *pit;
bellardfd06c372006-04-24 21:58:30 +000050 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";
Jan Kiszka302fe512012-02-17 11:24:34 +010058static PCSpkState *pcspk_state;
bellardfd06c372006-04-24 21:58:30 +000059
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;
Jan Kiszka4aa5d282012-02-01 20:31:43 +010082 PITChannelInfo ch;
bellardfd06c372006-04-24 21:58:30 +000083 unsigned int n;
84
Jan Kiszka4aa5d282012-02-01 20:31:43 +010085 pit_get_channel_info(s->pit, 2, &ch);
bellardfd06c372006-04-24 21:58:30 +000086
Jan Kiszka4aa5d282012-02-01 20:31:43 +010087 if (ch.mode != 3) {
88 return;
89 }
90
91 n = ch.initial_count;
bellardfd06c372006-04-24 21:58:30 +000092 /* 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
Paolo Bonzini36cd6f62013-04-18 18:43:58 +0200112static int pcspk_audio_init(ISABus *bus)
bellardfd06c372006-04-24 21:58:30 +0000113{
Jan Kiszka302fe512012-02-17 11:24:34 +0100114 PCSpkState *s = pcspk_state;
malc1ea879e2008-12-03 22:48:44 +0000115 struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0};
bellardfd06c372006-04-24 21:58:30 +0000116
malc1a7dafc2009-05-14 03:11:35 +0400117 AUD_register_card(s_spk, &s->card);
bellardfd06c372006-04-24 21:58:30 +0000118
bellardd929eba2006-07-04 21:47:22 +0000119 s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
bellardfd06c372006-04-24 21:58:30 +0000120 if (!s->voice) {
121 AUD_log(s_spk, "Could not open voice\n");
122 return -1;
123 }
124
125 return 0;
126}
127
Avi Kivitya8170e52012-10-23 12:30:10 +0200128static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
Jan Kiszka302fe512012-02-17 11:24:34 +0100129 unsigned size)
bellardfd06c372006-04-24 21:58:30 +0000130{
131 PCSpkState *s = opaque;
Jan Kiszka4aa5d282012-02-01 20:31:43 +0100132 PITChannelInfo ch;
133
134 pit_get_channel_info(s->pit, 2, &ch);
bellardfd06c372006-04-24 21:58:30 +0000135
136 s->dummy_refresh_clock ^= (1 << 4);
bellardfd06c372006-04-24 21:58:30 +0000137
Jan Kiszka4aa5d282012-02-01 20:31:43 +0100138 return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
139 (ch.out << 5);
bellardfd06c372006-04-24 21:58:30 +0000140}
141
Avi Kivitya8170e52012-10-23 12:30:10 +0200142static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
Jan Kiszka302fe512012-02-17 11:24:34 +0100143 unsigned size)
bellardfd06c372006-04-24 21:58:30 +0000144{
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
Jan Kiszka302fe512012-02-17 11:24:34 +0100157static 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};
bellardfd06c372006-04-24 21:58:30 +0000165
Andreas Färberdb895a12012-11-25 02:37:14 +0100166static void pcspk_initfn(Object *obj)
Jan Kiszka302fe512012-02-17 11:24:34 +0100167{
Andreas Färberdb895a12012-11-25 02:37:14 +0100168 PCSpkState *s = PC_SPEAKER(obj);
Jan Kiszka302fe512012-02-17 11:24:34 +0100169
Paolo Bonzini64bde0f2013-06-06 21:25:08 -0400170 memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "elcr", 1);
Andreas Färberdb895a12012-11-25 02:37:14 +0100171}
172
173static void pcspk_realizefn(DeviceState *dev, Error **errp)
174{
175 ISADevice *isadev = ISA_DEVICE(dev);
176 PCSpkState *s = PC_SPEAKER(dev);
177
178 isa_register_ioport(isadev, &s->ioport, s->iobase);
Jan Kiszka302fe512012-02-17 11:24:34 +0100179
180 pcspk_state = s;
bellardfd06c372006-04-24 21:58:30 +0000181}
Jan Kiszka302fe512012-02-17 11:24:34 +0100182
183static Property pcspk_properties[] = {
Paolo Bonzinic7bcc852014-02-08 11:01:53 +0100184 DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, -1),
Jan Kiszka302fe512012-02-17 11:24:34 +0100185 DEFINE_PROP_PTR("pit", PCSpkState, pit),
186 DEFINE_PROP_END_OF_LIST(),
187};
188
189static void pcspk_class_initfn(ObjectClass *klass, void *data)
190{
191 DeviceClass *dc = DEVICE_CLASS(klass);
Jan Kiszka302fe512012-02-17 11:24:34 +0100192
Andreas Färberdb895a12012-11-25 02:37:14 +0100193 dc->realize = pcspk_realizefn;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300194 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
Jan Kiszka302fe512012-02-17 11:24:34 +0100195 dc->props = pcspk_properties;
Markus Armbrusterf3b17642013-11-28 17:27:02 +0100196 /* Reason: pointer property "pit", realize sets global pcspk_state */
197 dc->cannot_instantiate_with_device_add_yet = true;
Jan Kiszka302fe512012-02-17 11:24:34 +0100198}
199
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100200static const TypeInfo pcspk_info = {
Andreas Färberd367ece2013-04-27 22:18:48 +0200201 .name = TYPE_PC_SPEAKER,
Jan Kiszka302fe512012-02-17 11:24:34 +0100202 .parent = TYPE_ISA_DEVICE,
203 .instance_size = sizeof(PCSpkState),
Andreas Färberdb895a12012-11-25 02:37:14 +0100204 .instance_init = pcspk_initfn,
Jan Kiszka302fe512012-02-17 11:24:34 +0100205 .class_init = pcspk_class_initfn,
206};
207
208static void pcspk_register(void)
209{
210 type_register_static(&pcspk_info);
Paolo Bonzini36cd6f62013-04-18 18:43:58 +0200211 isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init);
Jan Kiszka302fe512012-02-17 11:24:34 +0100212}
213type_init(pcspk_register)