]> git.proxmox.com Git - mirror_qemu.git/blame - hw/pcspk.c
i8254: Rework & fix interaction with HPET in legacy mode
[mirror_qemu.git] / hw / 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
87ecb68b
PB
25#include "hw.h"
26#include "pc.h"
27#include "isa.h"
28#include "audio/audio.h"
29#include "qemu-timer.h"
b1277b03 30#include "i8254.h"
fd06c375
FB
31
32#define PCSPK_BUF_LEN 1792
33#define PCSPK_SAMPLE_RATE 32000
34#define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
35#define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ)
36
37typedef struct {
38 uint8_t sample_buf[PCSPK_BUF_LEN];
39 QEMUSoundCard card;
40 SWVoiceOut *voice;
64d7e9a4 41 ISADevice *pit;
fd06c375
FB
42 unsigned int pit_count;
43 unsigned int samples;
44 unsigned int play_pos;
45 int data_on;
46 int dummy_refresh_clock;
47} PCSpkState;
48
49static const char *s_spk = "pcspk";
50static PCSpkState pcspk_state;
51
52static inline void generate_samples(PCSpkState *s)
53{
54 unsigned int i;
55
56 if (s->pit_count) {
57 const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
58 const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
59
60 /* multiple of wavelength for gapless looping */
61 s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1;
62 for (i = 0; i < s->samples; ++i)
63 s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
64 } else {
65 s->samples = PCSPK_BUF_LEN;
66 for (i = 0; i < PCSPK_BUF_LEN; ++i)
67 s->sample_buf[i] = 128; /* silence */
68 }
69}
70
71static void pcspk_callback(void *opaque, int free)
72{
73 PCSpkState *s = opaque;
74 unsigned int n;
75
76 if (pit_get_mode(s->pit, 2) != 3)
77 return;
78
79 n = pit_get_initial_count(s->pit, 2);
80 /* avoid frequencies that are not reproducible with sample rate */
81 if (n < PCSPK_MIN_COUNT)
82 n = 0;
83
84 if (s->pit_count != n) {
85 s->pit_count = n;
86 s->play_pos = 0;
87 generate_samples(s);
88 }
89
90 while (free > 0) {
91 n = audio_MIN(s->samples - s->play_pos, (unsigned int)free);
92 n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
93 if (!n)
94 break;
95 s->play_pos = (s->play_pos + n) % s->samples;
96 free -= n;
97 }
98}
99
4a0f031d 100int pcspk_audio_init(ISABus *bus)
fd06c375
FB
101{
102 PCSpkState *s = &pcspk_state;
1ea879e5 103 struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0};
fd06c375 104
1a7dafce 105 AUD_register_card(s_spk, &s->card);
fd06c375 106
d929eba5 107 s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
fd06c375
FB
108 if (!s->voice) {
109 AUD_log(s_spk, "Could not open voice\n");
110 return -1;
111 }
112
113 return 0;
114}
115
116static uint32_t pcspk_ioport_read(void *opaque, uint32_t addr)
117{
118 PCSpkState *s = opaque;
119 int out;
120
121 s->dummy_refresh_clock ^= (1 << 4);
74475455 122 out = pit_get_out(s->pit, 2, qemu_get_clock_ns(vm_clock)) << 5;
fd06c375
FB
123
124 return pit_get_gate(s->pit, 2) | (s->data_on << 1) | s->dummy_refresh_clock | out;
125}
126
127static void pcspk_ioport_write(void *opaque, uint32_t addr, uint32_t val)
128{
129 PCSpkState *s = opaque;
130 const int gate = val & 1;
131
132 s->data_on = (val >> 1) & 1;
133 pit_set_gate(s->pit, 2, gate);
134 if (s->voice) {
135 if (gate) /* restart */
136 s->play_pos = 0;
137 AUD_set_active_out(s->voice, gate & s->data_on);
138 }
139}
140
64d7e9a4 141void pcspk_init(ISADevice *pit)
fd06c375
FB
142{
143 PCSpkState *s = &pcspk_state;
144
145 s->pit = pit;
146 register_ioport_read(0x61, 1, 1, pcspk_ioport_read, s);
147 register_ioport_write(0x61, 1, 1, pcspk_ioport_write, s);
148}