2006-04-24 23:58:30 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2007-11-17 18:14:51 +01:00
|
|
|
#include "hw.h"
|
|
|
|
#include "pc.h"
|
|
|
|
#include "isa.h"
|
|
|
|
#include "audio/audio.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2012-02-01 20:31:39 +01:00
|
|
|
#include "i8254.h"
|
2012-02-17 11:24:34 +01:00
|
|
|
#include "pcspk.h"
|
2006-04-24 23:58:30 +02:00
|
|
|
|
|
|
|
#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 {
|
2012-02-17 11:24:34 +01:00
|
|
|
ISADevice dev;
|
|
|
|
MemoryRegion ioport;
|
|
|
|
uint32_t iobase;
|
2006-04-24 23:58:30 +02:00
|
|
|
uint8_t sample_buf[PCSPK_BUF_LEN];
|
|
|
|
QEMUSoundCard card;
|
|
|
|
SWVoiceOut *voice;
|
2012-02-17 11:24:34 +01:00
|
|
|
void *pit;
|
2006-04-24 23:58:30 +02:00
|
|
|
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";
|
2012-02-17 11:24:34 +01:00
|
|
|
static PCSpkState *pcspk_state;
|
2006-04-24 23:58:30 +02:00
|
|
|
|
|
|
|
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;
|
2012-02-01 20:31:43 +01:00
|
|
|
PITChannelInfo ch;
|
2006-04-24 23:58:30 +02:00
|
|
|
unsigned int n;
|
|
|
|
|
2012-02-01 20:31:43 +01:00
|
|
|
pit_get_channel_info(s->pit, 2, &ch);
|
|
|
|
|
|
|
|
if (ch.mode != 3) {
|
2006-04-24 23:58:30 +02:00
|
|
|
return;
|
2012-02-01 20:31:43 +01:00
|
|
|
}
|
2006-04-24 23:58:30 +02:00
|
|
|
|
2012-02-01 20:31:43 +01:00
|
|
|
n = ch.initial_count;
|
2006-04-24 23:58:30 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 22:10:01 +01:00
|
|
|
int pcspk_audio_init(ISABus *bus)
|
2006-04-24 23:58:30 +02:00
|
|
|
{
|
2012-02-17 11:24:34 +01:00
|
|
|
PCSpkState *s = pcspk_state;
|
2008-12-03 23:48:44 +01:00
|
|
|
struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUD_FMT_U8, 0};
|
2006-04-24 23:58:30 +02:00
|
|
|
|
2009-05-14 01:11:35 +02:00
|
|
|
AUD_register_card(s_spk, &s->card);
|
2006-04-24 23:58:30 +02:00
|
|
|
|
2006-07-04 23:47:22 +02:00
|
|
|
s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
|
2006-04-24 23:58:30 +02:00
|
|
|
if (!s->voice) {
|
|
|
|
AUD_log(s_spk, "Could not open voice\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
|
2012-02-17 11:24:34 +01:00
|
|
|
unsigned size)
|
2006-04-24 23:58:30 +02:00
|
|
|
{
|
|
|
|
PCSpkState *s = opaque;
|
2012-02-01 20:31:43 +01:00
|
|
|
PITChannelInfo ch;
|
|
|
|
|
|
|
|
pit_get_channel_info(s->pit, 2, &ch);
|
2006-04-24 23:58:30 +02:00
|
|
|
|
|
|
|
s->dummy_refresh_clock ^= (1 << 4);
|
|
|
|
|
2012-02-01 20:31:43 +01:00
|
|
|
return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
|
|
|
|
(ch.out << 5);
|
2006-04-24 23:58:30 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
|
2012-02-17 11:24:34 +01:00
|
|
|
unsigned size)
|
2006-04-24 23:58:30 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 11:24:34 +01:00
|
|
|
static const MemoryRegionOps pcspk_io_ops = {
|
|
|
|
.read = pcspk_io_read,
|
|
|
|
.write = pcspk_io_write,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 1,
|
|
|
|
.max_access_size = 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static int pcspk_initfn(ISADevice *dev)
|
2006-04-24 23:58:30 +02:00
|
|
|
{
|
2012-02-17 11:24:34 +01:00
|
|
|
PCSpkState *s = DO_UPCAST(PCSpkState, dev, dev);
|
|
|
|
|
|
|
|
memory_region_init_io(&s->ioport, &pcspk_io_ops, s, "elcr", 1);
|
|
|
|
isa_register_ioport(dev, &s->ioport, s->iobase);
|
|
|
|
|
|
|
|
pcspk_state = s;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Property pcspk_properties[] = {
|
|
|
|
DEFINE_PROP_HEX32("iobase", PCSpkState, iobase, -1),
|
|
|
|
DEFINE_PROP_PTR("pit", PCSpkState, pit),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
2006-04-24 23:58:30 +02:00
|
|
|
|
2012-02-17 11:24:34 +01:00
|
|
|
static void pcspk_class_initfn(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
ic->init = pcspk_initfn;
|
|
|
|
dc->no_user = 1;
|
|
|
|
dc->props = pcspk_properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TypeInfo pcspk_info = {
|
|
|
|
.name = "isa-pcspk",
|
|
|
|
.parent = TYPE_ISA_DEVICE,
|
|
|
|
.instance_size = sizeof(PCSpkState),
|
|
|
|
.class_init = pcspk_class_initfn,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void pcspk_register(void)
|
|
|
|
{
|
|
|
|
type_register_static(&pcspk_info);
|
2006-04-24 23:58:30 +02:00
|
|
|
}
|
2012-02-17 11:24:34 +01:00
|
|
|
type_init(pcspk_register)
|