PC speaker emulation (Joachim Henke)

git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1851 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
bellard 2006-04-24 21:58:30 +00:00
parent 52328140e2
commit fd06c37550
7 changed files with 177 additions and 20 deletions

View File

@ -2,6 +2,7 @@ version 0.8.1:
- USB tablet support (Brad Campbell, Anthony Liguori)
- win32 host serial support (Kazu)
- PC speaker support (Joachim Henke)
version 0.8.0:

View File

@ -310,7 +310,7 @@ VL_OBJS+= ne2000.o rtl8139.o
ifeq ($(TARGET_BASE_ARCH), i386)
# Hardware support
VL_OBJS+= ide.o pckbd.o ps2.o vga.o $(SOUND_HW) dma.o $(AUDIODRV)
VL_OBJS+= fdc.o mc146818rtc.o serial.o i8259.o i8254.o pc.o
VL_OBJS+= fdc.o mc146818rtc.o serial.o i8259.o i8254.o pcspk.o pc.o
VL_OBJS+= cirrus_vga.o mixeng.o apic.o parallel.o
DEFINES += -DHAS_AUDIO
endif

View File

@ -209,6 +209,18 @@ int pit_get_gate(PITState *pit, int channel)
return s->gate;
}
int pit_get_initial_count(PITState *pit, int channel)
{
PITChannelState *s = &pit->channels[channel];
return s->count;
}
int pit_get_mode(PITState *pit, int channel)
{
PITChannelState *s = &pit->channels[channel];
return s->mode;
}
static inline void pit_load_count(PITChannelState *s, int val)
{
if (val == 0)

20
hw/pc.c
View File

@ -36,8 +36,6 @@
#define KERNEL_PARAMS_ADDR 0x00090000
#define KERNEL_CMDLINE_ADDR 0x00099000
int speaker_data_on;
int dummy_refresh_clock;
static fdctrl_t *floppy_controller;
static RTCState *rtc_state;
static PITState *pit;
@ -273,21 +271,6 @@ static void cmos_init(int ram_size, int boot_device, BlockDriverState **hd_table
// rtc_set_memory(s, 0x38, 1);
}
static void speaker_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
speaker_data_on = (val >> 1) & 1;
pit_set_gate(pit, 2, val & 1);
}
static uint32_t speaker_ioport_read(void *opaque, uint32_t addr)
{
int out;
out = pit_get_out(pit, 2, qemu_get_clock(vm_clock));
dummy_refresh_clock ^= 1;
return (speaker_data_on << 1) | pit_get_gate(pit, 2) | (out << 5) |
(dummy_refresh_clock << 4);
}
void ioport_set_a20(int enable)
{
/* XXX: send to all CPUs ? */
@ -783,8 +766,6 @@ static void pc_init1(int ram_size, int vga_ram_size, int boot_device,
}
rtc_state = rtc_init(0x70, 8);
register_ioport_read(0x61, 1, 1, speaker_ioport_read, NULL);
register_ioport_write(0x61, 1, 1, speaker_ioport_write, NULL);
register_ioport_read(0x92, 1, 1, ioport92_read, NULL);
register_ioport_write(0x92, 1, 1, ioport92_write, NULL);
@ -794,6 +775,7 @@ static void pc_init1(int ram_size, int vga_ram_size, int boot_device,
}
isa_pic = pic_init(pic_irq_request, first_cpu);
pit = pit_init(0x40, 0);
pcspk_init(pit);
if (pci_enabled) {
pic_set_alt_irq_func(isa_pic, ioapic_set_irq, ioapic);
}

147
hw/pcspk.c Normal file
View File

@ -0,0 +1,147 @@
/*
* QEMU PC speaker emulation
*
* Copyright (c) 2006 Joachim Henke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "vl.h"
#define PCSPK_BUF_LEN 1792
#define PCSPK_SAMPLE_RATE 32000
#define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
#define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ)
typedef struct {
uint8_t sample_buf[PCSPK_BUF_LEN];
QEMUSoundCard card;
SWVoiceOut *voice;
PITState *pit;
unsigned int pit_count;
unsigned int samples;
unsigned int play_pos;
int data_on;
int dummy_refresh_clock;
} PCSpkState;
static const char *s_spk = "pcspk";
static PCSpkState pcspk_state;
static inline void generate_samples(PCSpkState *s)
{
unsigned int i;
if (s->pit_count) {
const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
/* multiple of wavelength for gapless looping */
s->samples = (PCSPK_BUF_LEN * PIT_FREQ / m * m / (PIT_FREQ >> 1) + 1) >> 1;
for (i = 0; i < s->samples; ++i)
s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
} else {
s->samples = PCSPK_BUF_LEN;
for (i = 0; i < PCSPK_BUF_LEN; ++i)
s->sample_buf[i] = 128; /* silence */
}
}
static void pcspk_callback(void *opaque, int free)
{
PCSpkState *s = opaque;
unsigned int n;
if (pit_get_mode(s->pit, 2) != 3)
return;
n = pit_get_initial_count(s->pit, 2);
/* avoid frequencies that are not reproducible with sample rate */
if (n < PCSPK_MIN_COUNT)
n = 0;
if (s->pit_count != n) {
s->pit_count = n;
s->play_pos = 0;
generate_samples(s);
}
while (free > 0) {
n = audio_MIN(s->samples - s->play_pos, (unsigned int)free);
n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
if (!n)
break;
s->play_pos = (s->play_pos + n) % s->samples;
free -= n;
}
}
int pcspk_audio_init(AudioState *audio)
{
PCSpkState *s = &pcspk_state;
audsettings_t as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8};
if (!audio) {
AUD_log(s_spk, "No audio state\n");
return -1;
}
AUD_register_card(audio, s_spk, &s->card);
s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as, 0);
if (!s->voice) {
AUD_log(s_spk, "Could not open voice\n");
return -1;
}
return 0;
}
static uint32_t pcspk_ioport_read(void *opaque, uint32_t addr)
{
PCSpkState *s = opaque;
int out;
s->dummy_refresh_clock ^= (1 << 4);
out = pit_get_out(s->pit, 2, qemu_get_clock(vm_clock)) << 5;
return pit_get_gate(s->pit, 2) | (s->data_on << 1) | s->dummy_refresh_clock | out;
}
static void pcspk_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
PCSpkState *s = opaque;
const int gate = val & 1;
s->data_on = (val >> 1) & 1;
pit_set_gate(s->pit, 2, gate);
if (s->voice) {
if (gate) /* restart */
s->play_pos = 0;
AUD_set_active_out(s->voice, gate & s->data_on);
}
}
void pcspk_init(PITState *pit)
{
PCSpkState *s = &pcspk_state;
s->pit = pit;
register_ioport_read(0x61, 1, 1, pcspk_ioport_read, s);
register_ioport_write(0x61, 1, 1, pcspk_ioport_write, s);
}

9
vl.c
View File

@ -4862,6 +4862,15 @@ void register_machines(void)
#ifdef HAS_AUDIO
struct soundhw soundhw[] = {
#ifdef TARGET_I386
{
"pcspk",
"PC speaker",
0,
1,
{ .init_isa = pcspk_audio_init }
},
#endif
{
"sb16",
"Creative Sound Blaster 16",

6
vl.h
View File

@ -819,8 +819,14 @@ typedef struct PITState PITState;
PITState *pit_init(int base, int irq);
void pit_set_gate(PITState *pit, int channel, int val);
int pit_get_gate(PITState *pit, int channel);
int pit_get_initial_count(PITState *pit, int channel);
int pit_get_mode(PITState *pit, int channel);
int pit_get_out(PITState *pit, int channel, int64_t current_time);
/* pcspk.c */
void pcspk_init(PITState *);
int pcspk_audio_init(AudioState *);
/* pc.c */
extern QEMUMachine pc_machine;
extern QEMUMachine isapc_machine;