2 * QEMU PC speaker emulation
4 * Copyright (c) 2006 Joachim Henke
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
28 #include "audio/audio.h"
29 #include "qemu-timer.h"
33 #define PCSPK_BUF_LEN 1792
34 #define PCSPK_SAMPLE_RATE 32000
35 #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
36 #define PCSPK_MIN_COUNT ((PIT_FREQ + PCSPK_MAX_FREQ - 1) / PCSPK_MAX_FREQ)
42 uint8_t sample_buf
[PCSPK_BUF_LEN
];
46 unsigned int pit_count
;
48 unsigned int play_pos
;
50 int dummy_refresh_clock
;
53 static const char *s_spk
= "pcspk";
54 static PCSpkState
*pcspk_state
;
56 static inline void generate_samples(PCSpkState
*s
)
61 const uint32_t m
= PCSPK_SAMPLE_RATE
* s
->pit_count
;
62 const uint32_t n
= ((uint64_t)PIT_FREQ
<< 32) / m
;
64 /* multiple of wavelength for gapless looping */
65 s
->samples
= (PCSPK_BUF_LEN
* PIT_FREQ
/ m
* m
/ (PIT_FREQ
>> 1) + 1) >> 1;
66 for (i
= 0; i
< s
->samples
; ++i
)
67 s
->sample_buf
[i
] = (64 & (n
* i
>> 25)) - 32;
69 s
->samples
= PCSPK_BUF_LEN
;
70 for (i
= 0; i
< PCSPK_BUF_LEN
; ++i
)
71 s
->sample_buf
[i
] = 128; /* silence */
75 static void pcspk_callback(void *opaque
, int free
)
77 PCSpkState
*s
= opaque
;
80 if (pit_get_mode(s
->pit
, 2) != 3)
83 n
= pit_get_initial_count(s
->pit
, 2);
84 /* avoid frequencies that are not reproducible with sample rate */
85 if (n
< PCSPK_MIN_COUNT
)
88 if (s
->pit_count
!= n
) {
95 n
= audio_MIN(s
->samples
- s
->play_pos
, (unsigned int)free
);
96 n
= AUD_write(s
->voice
, &s
->sample_buf
[s
->play_pos
], n
);
99 s
->play_pos
= (s
->play_pos
+ n
) % s
->samples
;
104 int pcspk_audio_init(ISABus
*bus
)
106 PCSpkState
*s
= pcspk_state
;
107 struct audsettings as
= {PCSPK_SAMPLE_RATE
, 1, AUD_FMT_U8
, 0};
109 AUD_register_card(s_spk
, &s
->card
);
111 s
->voice
= AUD_open_out(&s
->card
, s
->voice
, s_spk
, s
, pcspk_callback
, &as
);
113 AUD_log(s_spk
, "Could not open voice\n");
120 static uint64_t pcspk_io_read(void *opaque
, target_phys_addr_t addr
,
123 PCSpkState
*s
= opaque
;
126 s
->dummy_refresh_clock
^= (1 << 4);
127 out
= pit_get_out(s
->pit
, 2, qemu_get_clock_ns(vm_clock
)) << 5;
129 return pit_get_gate(s
->pit
, 2) | (s
->data_on
<< 1) | s
->dummy_refresh_clock
| out
;
132 static void pcspk_io_write(void *opaque
, target_phys_addr_t addr
, uint64_t val
,
135 PCSpkState
*s
= opaque
;
136 const int gate
= val
& 1;
138 s
->data_on
= (val
>> 1) & 1;
139 pit_set_gate(s
->pit
, 2, gate
);
141 if (gate
) /* restart */
143 AUD_set_active_out(s
->voice
, gate
& s
->data_on
);
147 static const MemoryRegionOps pcspk_io_ops
= {
148 .read
= pcspk_io_read
,
149 .write
= pcspk_io_write
,
151 .min_access_size
= 1,
152 .max_access_size
= 1,
156 static int pcspk_initfn(ISADevice
*dev
)
158 PCSpkState
*s
= DO_UPCAST(PCSpkState
, dev
, dev
);
160 memory_region_init_io(&s
->ioport
, &pcspk_io_ops
, s
, "elcr", 1);
161 isa_register_ioport(dev
, &s
->ioport
, s
->iobase
);
168 static Property pcspk_properties
[] = {
169 DEFINE_PROP_HEX32("iobase", PCSpkState
, iobase
, -1),
170 DEFINE_PROP_PTR("pit", PCSpkState
, pit
),
171 DEFINE_PROP_END_OF_LIST(),
174 static void pcspk_class_initfn(ObjectClass
*klass
, void *data
)
176 DeviceClass
*dc
= DEVICE_CLASS(klass
);
177 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
179 ic
->init
= pcspk_initfn
;
181 dc
->props
= pcspk_properties
;
184 static TypeInfo pcspk_info
= {
186 .parent
= TYPE_ISA_DEVICE
,
187 .instance_size
= sizeof(PCSpkState
),
188 .class_init
= pcspk_class_initfn
,
191 static void pcspk_register(void)
193 type_register_static(&pcspk_info
);
195 type_init(pcspk_register
)