root / hw / sb16.c @ 97eb5b14
History | View | Annotate | Download (16.2 kB)
1 | 27503323 | bellard | /*
|
---|---|---|---|
2 | 27503323 | bellard | * QEMU Soundblaster 16 emulation
|
3 | 27503323 | bellard | *
|
4 | 27503323 | bellard | * Copyright (c) 2003 Vassili Karpov (malc)
|
5 | 27503323 | bellard | *
|
6 | 27503323 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 27503323 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 27503323 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 27503323 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 27503323 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 27503323 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 27503323 | bellard | *
|
13 | 27503323 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 27503323 | bellard | * all copies or substantial portions of the Software.
|
15 | 27503323 | bellard | *
|
16 | 27503323 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 27503323 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 27503323 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 27503323 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 27503323 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 27503323 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 27503323 | bellard | * THE SOFTWARE.
|
23 | 27503323 | bellard | */
|
24 | 27503323 | bellard | #include <stdio.h> |
25 | 27503323 | bellard | #include <stdlib.h> |
26 | 27503323 | bellard | #include <inttypes.h> |
27 | 27503323 | bellard | |
28 | 27503323 | bellard | #include "vl.h" |
29 | 27503323 | bellard | |
30 | 27503323 | bellard | #define MIN(a, b) ((a)>(b)?(b):(a))
|
31 | 27503323 | bellard | #define LENOFA(a) ((int) (sizeof(a)/sizeof(a[0]))) |
32 | 27503323 | bellard | |
33 | 27503323 | bellard | #define DEREF(x) (void)x |
34 | 27503323 | bellard | #define log(...) fprintf (stderr, "sb16: " __VA_ARGS__) |
35 | 27503323 | bellard | #define Fail(...) do { \ |
36 | 27503323 | bellard | fprintf (stderr, "sb16: " __VA_ARGS__); \
|
37 | 27503323 | bellard | abort (); \ |
38 | 27503323 | bellard | } while (0) |
39 | 27503323 | bellard | |
40 | bc0b1dc1 | bellard | /* #define DEBUG_SB16 */
|
41 | 27503323 | bellard | #ifdef DEBUG_SB16
|
42 | 27503323 | bellard | #define lwarn(...) fprintf (stderr, "sb16: " __VA_ARGS__) |
43 | 27503323 | bellard | #define linfo(...) fprintf (stderr, "sb16: " __VA_ARGS__) |
44 | 27503323 | bellard | #define ldebug(...) fprintf (stderr, "sb16: " __VA_ARGS__) |
45 | 27503323 | bellard | #else
|
46 | 27503323 | bellard | #define lwarn(...)
|
47 | 27503323 | bellard | #define linfo(...)
|
48 | 27503323 | bellard | #define ldebug(...)
|
49 | 27503323 | bellard | #endif
|
50 | 27503323 | bellard | |
51 | 27503323 | bellard | #define IO_READ_PROTO(name) \
|
52 | 7138fcfb | bellard | uint32_t name (struct CPUState *env, uint32_t nport)
|
53 | 27503323 | bellard | #define IO_WRITE_PROTO(name) \
|
54 | 7138fcfb | bellard | void name (struct CPUState *env, uint32_t nport, uint32_t val) |
55 | 27503323 | bellard | |
56 | 27503323 | bellard | static struct { |
57 | 27503323 | bellard | int ver_lo;
|
58 | 27503323 | bellard | int ver_hi;
|
59 | 27503323 | bellard | int irq;
|
60 | 27503323 | bellard | int dma;
|
61 | 27503323 | bellard | int hdma;
|
62 | 27503323 | bellard | int port;
|
63 | 27503323 | bellard | int mix_block;
|
64 | bc0b1dc1 | bellard | } sb = {5, 4, 5, 1, 5, 0x220, -1}; |
65 | 27503323 | bellard | |
66 | 27503323 | bellard | static int mix_block, noirq; |
67 | 27503323 | bellard | |
68 | 27503323 | bellard | static struct mixer { |
69 | 27503323 | bellard | int nreg;
|
70 | 27503323 | bellard | uint8_t regs[0x83];
|
71 | 27503323 | bellard | } mixer; |
72 | 27503323 | bellard | |
73 | 27503323 | bellard | static struct dsp { |
74 | 27503323 | bellard | int in_index;
|
75 | 27503323 | bellard | int out_data_len;
|
76 | 27503323 | bellard | int fmt_stereo;
|
77 | 27503323 | bellard | int fmt_signed;
|
78 | 27503323 | bellard | int fmt_bits;
|
79 | 27503323 | bellard | int dma_auto;
|
80 | 27503323 | bellard | int dma_buffer_size;
|
81 | 27503323 | bellard | int fifo;
|
82 | 27503323 | bellard | int freq;
|
83 | 27503323 | bellard | int time_const;
|
84 | 27503323 | bellard | int speaker;
|
85 | 27503323 | bellard | int needed_bytes;
|
86 | 27503323 | bellard | int cmd;
|
87 | 27503323 | bellard | int dma_pos;
|
88 | 27503323 | bellard | int use_hdma;
|
89 | 27503323 | bellard | |
90 | 27503323 | bellard | int v2x6;
|
91 | 27503323 | bellard | |
92 | 27503323 | bellard | uint8_t in_data[10];
|
93 | 27503323 | bellard | uint8_t out_data[10];
|
94 | 27503323 | bellard | |
95 | 27503323 | bellard | int left_till_irq;
|
96 | 27503323 | bellard | } dsp; |
97 | 27503323 | bellard | |
98 | 27503323 | bellard | #define nocmd ~0 |
99 | 27503323 | bellard | |
100 | 27503323 | bellard | static void log_dsp (const char *cap) |
101 | 27503323 | bellard | { |
102 | 27503323 | bellard | DEREF (cap); |
103 | 27503323 | bellard | |
104 | 27503323 | bellard | linfo ("%c:%c:%d:%c:dmabuf=%d:pos=%d:freq=%d:timeconst=%d:speaker=%d\n",
|
105 | 27503323 | bellard | dsp.fmt_stereo ? 'S' : 'M', |
106 | 27503323 | bellard | dsp.fmt_signed ? 'S' : 'U', |
107 | 27503323 | bellard | dsp.fmt_bits, |
108 | 27503323 | bellard | dsp.dma_auto ? 'a' : 's', |
109 | 27503323 | bellard | dsp.dma_buffer_size, |
110 | 27503323 | bellard | dsp.dma_pos, |
111 | 27503323 | bellard | dsp.freq, |
112 | 27503323 | bellard | dsp.time_const, |
113 | 27503323 | bellard | dsp.speaker); |
114 | 27503323 | bellard | } |
115 | 27503323 | bellard | |
116 | 27503323 | bellard | static void control (int hold) |
117 | 27503323 | bellard | { |
118 | 27503323 | bellard | linfo ("%d high %d\n", hold, dsp.use_hdma);
|
119 | 27503323 | bellard | if (hold) {
|
120 | 27503323 | bellard | if (dsp.use_hdma)
|
121 | 27503323 | bellard | DMA_hold_DREQ (sb.hdma); |
122 | 27503323 | bellard | else
|
123 | 27503323 | bellard | DMA_hold_DREQ (sb.dma); |
124 | 27503323 | bellard | } |
125 | 27503323 | bellard | else {
|
126 | 27503323 | bellard | if (dsp.use_hdma)
|
127 | 27503323 | bellard | DMA_release_DREQ (sb.hdma); |
128 | 27503323 | bellard | else
|
129 | 27503323 | bellard | DMA_release_DREQ (sb.dma); |
130 | 27503323 | bellard | } |
131 | 27503323 | bellard | } |
132 | 27503323 | bellard | |
133 | 27503323 | bellard | static void dma_cmd (uint8_t cmd, uint8_t d0, int dma_len) |
134 | 27503323 | bellard | { |
135 | 27503323 | bellard | int bps;
|
136 | 27503323 | bellard | audfmt_e fmt; |
137 | 27503323 | bellard | |
138 | 27503323 | bellard | dsp.use_hdma = cmd < 0xc0;
|
139 | 27503323 | bellard | dsp.fifo = (cmd >> 1) & 1; |
140 | 27503323 | bellard | dsp.dma_auto = (cmd >> 2) & 1; |
141 | 27503323 | bellard | |
142 | 27503323 | bellard | switch (cmd >> 4) { |
143 | 27503323 | bellard | case 11: |
144 | 27503323 | bellard | dsp.fmt_bits = 16;
|
145 | 27503323 | bellard | break;
|
146 | 27503323 | bellard | |
147 | 27503323 | bellard | case 12: |
148 | 27503323 | bellard | dsp.fmt_bits = 8;
|
149 | 27503323 | bellard | break;
|
150 | 27503323 | bellard | } |
151 | 27503323 | bellard | |
152 | 27503323 | bellard | dsp.fmt_signed = (d0 >> 4) & 1; |
153 | 27503323 | bellard | dsp.fmt_stereo = (d0 >> 5) & 1; |
154 | 27503323 | bellard | |
155 | 27503323 | bellard | if (-1 != dsp.time_const) { |
156 | 27503323 | bellard | int tmp;
|
157 | 27503323 | bellard | |
158 | 27503323 | bellard | tmp = 256 - dsp.time_const;
|
159 | 27503323 | bellard | dsp.freq = (1000000 + (tmp / 2)) / tmp; |
160 | 27503323 | bellard | } |
161 | 27503323 | bellard | bps = 1 << (16 == dsp.fmt_bits); |
162 | 27503323 | bellard | |
163 | 27503323 | bellard | if (-1 != dma_len) |
164 | 27503323 | bellard | dsp.dma_buffer_size = (dma_len + 1) * bps;
|
165 | 27503323 | bellard | |
166 | 27503323 | bellard | linfo ("frequency %d, stereo %d, signed %d, bits %d, size %d, auto %d\n",
|
167 | 27503323 | bellard | dsp.freq, dsp.fmt_stereo, dsp.fmt_signed, dsp.fmt_bits, |
168 | 27503323 | bellard | dsp.dma_buffer_size, dsp.dma_auto); |
169 | 27503323 | bellard | |
170 | 27503323 | bellard | if (16 == dsp.fmt_bits) { |
171 | 27503323 | bellard | if (dsp.fmt_signed) {
|
172 | 27503323 | bellard | fmt = AUD_FMT_S16; |
173 | 27503323 | bellard | } |
174 | 27503323 | bellard | else {
|
175 | 27503323 | bellard | fmt = AUD_FMT_U16; |
176 | 27503323 | bellard | } |
177 | 27503323 | bellard | } |
178 | 27503323 | bellard | else {
|
179 | 27503323 | bellard | if (dsp.fmt_signed) {
|
180 | 27503323 | bellard | fmt = AUD_FMT_S8; |
181 | 27503323 | bellard | } |
182 | 27503323 | bellard | else {
|
183 | 27503323 | bellard | fmt = AUD_FMT_U8; |
184 | 27503323 | bellard | } |
185 | 27503323 | bellard | } |
186 | 27503323 | bellard | |
187 | 27503323 | bellard | dsp.dma_pos = 0;
|
188 | 27503323 | bellard | dsp.left_till_irq = dsp.dma_buffer_size; |
189 | 27503323 | bellard | |
190 | 27503323 | bellard | if (sb.mix_block) {
|
191 | 27503323 | bellard | mix_block = sb.mix_block; |
192 | 27503323 | bellard | } |
193 | 27503323 | bellard | else {
|
194 | 27503323 | bellard | int align;
|
195 | 27503323 | bellard | |
196 | 27503323 | bellard | align = bps << dsp.fmt_stereo; |
197 | 27503323 | bellard | mix_block = ((dsp.freq * align) / 100) & ~(align - 1); |
198 | 27503323 | bellard | } |
199 | 27503323 | bellard | |
200 | 27503323 | bellard | AUD_reset (dsp.freq, 1 << dsp.fmt_stereo, fmt);
|
201 | 27503323 | bellard | control (1);
|
202 | 27503323 | bellard | dsp.speaker = 1;
|
203 | 27503323 | bellard | } |
204 | 27503323 | bellard | |
205 | 27503323 | bellard | static void command (uint8_t cmd) |
206 | 27503323 | bellard | { |
207 | 27503323 | bellard | char *msg;
|
208 | 27503323 | bellard | |
209 | bc0b1dc1 | bellard | msg = (char *) -1; |
210 | 27503323 | bellard | |
211 | 27503323 | bellard | linfo ("%#x\n", cmd);
|
212 | 27503323 | bellard | |
213 | 27503323 | bellard | if (cmd > 0xaf && cmd < 0xd0) { |
214 | 27503323 | bellard | if (cmd & 8) |
215 | 27503323 | bellard | goto error;
|
216 | 27503323 | bellard | |
217 | 27503323 | bellard | switch (cmd >> 4) { |
218 | 27503323 | bellard | case 11: |
219 | 27503323 | bellard | case 12: |
220 | 27503323 | bellard | break;
|
221 | 27503323 | bellard | default:
|
222 | 27503323 | bellard | msg = "wrong bits";
|
223 | 27503323 | bellard | goto error;
|
224 | 27503323 | bellard | } |
225 | 27503323 | bellard | dsp.needed_bytes = 3;
|
226 | 27503323 | bellard | } |
227 | 27503323 | bellard | else {
|
228 | 27503323 | bellard | switch (cmd) {
|
229 | bc0b1dc1 | bellard | case 0x00: |
230 | bc0b1dc1 | bellard | case 0x03: |
231 | bc0b1dc1 | bellard | case 0xe7: |
232 | bc0b1dc1 | bellard | /* IMS uses those when probing for sound devices */
|
233 | bc0b1dc1 | bellard | return;
|
234 | bc0b1dc1 | bellard | |
235 | 27503323 | bellard | case 0x10: |
236 | 27503323 | bellard | dsp.needed_bytes = 1;
|
237 | 27503323 | bellard | break;
|
238 | 27503323 | bellard | |
239 | 27503323 | bellard | case 0x14: |
240 | 27503323 | bellard | dsp.needed_bytes = 2;
|
241 | 27503323 | bellard | dsp.dma_buffer_size = 0;
|
242 | 27503323 | bellard | break;
|
243 | 27503323 | bellard | |
244 | 27503323 | bellard | case 0x20: |
245 | 27503323 | bellard | dsp.out_data[dsp.out_data_len++] = 0xff;
|
246 | 27503323 | bellard | break;
|
247 | 27503323 | bellard | |
248 | 27503323 | bellard | case 0x35: |
249 | 27503323 | bellard | lwarn ("MIDI commands not implemented\n");
|
250 | 27503323 | bellard | break;
|
251 | 27503323 | bellard | |
252 | 27503323 | bellard | case 0x40: |
253 | 27503323 | bellard | dsp.freq = -1;
|
254 | 27503323 | bellard | dsp.time_const = -1;
|
255 | 27503323 | bellard | dsp.needed_bytes = 1;
|
256 | 27503323 | bellard | break;
|
257 | 27503323 | bellard | |
258 | 27503323 | bellard | case 0x41: |
259 | 27503323 | bellard | case 0x42: |
260 | 27503323 | bellard | dsp.freq = -1;
|
261 | 27503323 | bellard | dsp.time_const = -1;
|
262 | 27503323 | bellard | dsp.needed_bytes = 2;
|
263 | 27503323 | bellard | break;
|
264 | 27503323 | bellard | |
265 | 27503323 | bellard | case 0x47: /* Continue Auto-Initialize DMA 16bit */ |
266 | 27503323 | bellard | break;
|
267 | 27503323 | bellard | |
268 | 27503323 | bellard | case 0x48: |
269 | 27503323 | bellard | dsp.needed_bytes = 2;
|
270 | 27503323 | bellard | break;
|
271 | 27503323 | bellard | |
272 | 27503323 | bellard | case 0x27: /* ????????? */ |
273 | 27503323 | bellard | case 0x4e: |
274 | 27503323 | bellard | return;
|
275 | 27503323 | bellard | |
276 | 27503323 | bellard | case 0x80: |
277 | 27503323 | bellard | cmd = nocmd; |
278 | 27503323 | bellard | break;
|
279 | 27503323 | bellard | |
280 | 27503323 | bellard | case 0x90: |
281 | 27503323 | bellard | case 0x91: |
282 | 27503323 | bellard | { |
283 | 27503323 | bellard | uint8_t d0; |
284 | 27503323 | bellard | |
285 | 27503323 | bellard | d0 = 4;
|
286 | 27503323 | bellard | if (dsp.fmt_signed) d0 |= 16; |
287 | 27503323 | bellard | if (dsp.fmt_stereo) d0 |= 32; |
288 | 27503323 | bellard | dma_cmd (cmd == 0x90 ? 0xc4 : 0xc0, d0, -1); |
289 | 27503323 | bellard | cmd = nocmd; |
290 | 27503323 | bellard | break;
|
291 | 27503323 | bellard | } |
292 | 27503323 | bellard | |
293 | 27503323 | bellard | case 0xd0: /* XXX */ |
294 | 27503323 | bellard | control (0);
|
295 | 27503323 | bellard | return;
|
296 | 27503323 | bellard | |
297 | 27503323 | bellard | case 0xd1: |
298 | 27503323 | bellard | dsp.speaker = 1;
|
299 | 27503323 | bellard | break;
|
300 | 27503323 | bellard | |
301 | 27503323 | bellard | case 0xd3: |
302 | 27503323 | bellard | dsp.speaker = 0;
|
303 | 27503323 | bellard | return;
|
304 | 27503323 | bellard | |
305 | 27503323 | bellard | case 0xd4: |
306 | 27503323 | bellard | control (1);
|
307 | 27503323 | bellard | break;
|
308 | 27503323 | bellard | |
309 | 27503323 | bellard | case 0xd5: |
310 | 27503323 | bellard | control (0);
|
311 | 27503323 | bellard | break;
|
312 | 27503323 | bellard | |
313 | 27503323 | bellard | case 0xd6: |
314 | 27503323 | bellard | control (1);
|
315 | 27503323 | bellard | break;
|
316 | 27503323 | bellard | |
317 | 27503323 | bellard | case 0xd9: |
318 | 27503323 | bellard | control (0);
|
319 | 27503323 | bellard | dsp.dma_auto = 0;
|
320 | 27503323 | bellard | return;
|
321 | 27503323 | bellard | |
322 | 27503323 | bellard | case 0xda: |
323 | 27503323 | bellard | control (0);
|
324 | 27503323 | bellard | dsp.dma_auto = 0;
|
325 | 27503323 | bellard | break;
|
326 | 27503323 | bellard | |
327 | 27503323 | bellard | case 0xe0: |
328 | 27503323 | bellard | dsp.needed_bytes = 1;
|
329 | 27503323 | bellard | break;
|
330 | 27503323 | bellard | |
331 | 27503323 | bellard | case 0xe1: |
332 | 27503323 | bellard | dsp.out_data[dsp.out_data_len++] = sb.ver_lo; |
333 | 27503323 | bellard | dsp.out_data[dsp.out_data_len++] = sb.ver_hi; |
334 | 27503323 | bellard | return;
|
335 | 27503323 | bellard | |
336 | 27503323 | bellard | case 0xf2: |
337 | 27503323 | bellard | dsp.out_data[dsp.out_data_len++] = 0xaa;
|
338 | bc0b1dc1 | bellard | mixer.regs[0x82] |= mixer.regs[0x80]; |
339 | 27503323 | bellard | pic_set_irq (sb.irq, 1);
|
340 | 27503323 | bellard | return;
|
341 | 27503323 | bellard | |
342 | 27503323 | bellard | default:
|
343 | 27503323 | bellard | msg = "is unknown";
|
344 | 27503323 | bellard | goto error;
|
345 | 27503323 | bellard | } |
346 | 27503323 | bellard | } |
347 | 27503323 | bellard | dsp.cmd = cmd; |
348 | 27503323 | bellard | return;
|
349 | 27503323 | bellard | |
350 | 27503323 | bellard | error:
|
351 | 27503323 | bellard | Fail ("%#x %s", cmd, msg);
|
352 | 27503323 | bellard | return;
|
353 | 27503323 | bellard | } |
354 | 27503323 | bellard | |
355 | 27503323 | bellard | static void complete (void) |
356 | 27503323 | bellard | { |
357 | 27503323 | bellard | linfo ("complete command %#x, in_index %d, needed_bytes %d\n",
|
358 | 27503323 | bellard | dsp.cmd, dsp.in_index, dsp.needed_bytes); |
359 | 27503323 | bellard | |
360 | 27503323 | bellard | if (dsp.cmd > 0xaf && dsp.cmd < 0xd0) { |
361 | 27503323 | bellard | int d0, d1, d2;
|
362 | 27503323 | bellard | |
363 | 27503323 | bellard | d0 = dsp.in_data[0];
|
364 | 27503323 | bellard | d1 = dsp.in_data[1];
|
365 | 27503323 | bellard | d2 = dsp.in_data[2];
|
366 | 27503323 | bellard | |
367 | 27503323 | bellard | ldebug ("d0 = %d, d1 = %d, d2 = %d\n",
|
368 | 27503323 | bellard | d0, d1, d2); |
369 | 27503323 | bellard | dma_cmd (dsp.cmd, d0, d1 + (d2 << 8));
|
370 | 27503323 | bellard | } |
371 | 27503323 | bellard | else {
|
372 | 27503323 | bellard | switch (dsp.cmd) {
|
373 | 27503323 | bellard | |
374 | 27503323 | bellard | case 0x10: |
375 | 27503323 | bellard | break;
|
376 | 27503323 | bellard | |
377 | 27503323 | bellard | case 0x14: |
378 | 27503323 | bellard | { |
379 | 27503323 | bellard | int d0, d1;
|
380 | 27503323 | bellard | int save_left;
|
381 | 27503323 | bellard | int save_pos;
|
382 | 27503323 | bellard | |
383 | 27503323 | bellard | d0 = dsp.in_data[0];
|
384 | 27503323 | bellard | d1 = dsp.in_data[1];
|
385 | 27503323 | bellard | |
386 | 27503323 | bellard | save_left = dsp.left_till_irq; |
387 | 27503323 | bellard | save_pos = dsp.dma_pos; |
388 | 27503323 | bellard | dma_cmd (0xc0, 0, d0 + (d1 << 8)); |
389 | 27503323 | bellard | dsp.left_till_irq = save_left; |
390 | 27503323 | bellard | dsp.dma_pos = save_pos; |
391 | 27503323 | bellard | |
392 | 27503323 | bellard | linfo ("set buffer size data[%d, %d] %d pos %d\n",
|
393 | 27503323 | bellard | d0, d1, dsp.dma_buffer_size, dsp.dma_pos); |
394 | 27503323 | bellard | break;
|
395 | 27503323 | bellard | } |
396 | 27503323 | bellard | |
397 | 27503323 | bellard | case 0x40: |
398 | 27503323 | bellard | dsp.time_const = dsp.in_data[0];
|
399 | 27503323 | bellard | linfo ("set time const %d\n", dsp.time_const);
|
400 | 27503323 | bellard | break;
|
401 | 27503323 | bellard | |
402 | 27503323 | bellard | case 0x41: |
403 | 27503323 | bellard | case 0x42: |
404 | 27503323 | bellard | dsp.freq = dsp.in_data[1] + (dsp.in_data[0] << 8); |
405 | 27503323 | bellard | linfo ("set freq %#x, %#x = %d\n",
|
406 | 27503323 | bellard | dsp.in_data[1], dsp.in_data[0], dsp.freq); |
407 | 27503323 | bellard | break;
|
408 | 27503323 | bellard | |
409 | 27503323 | bellard | case 0x48: |
410 | 27503323 | bellard | dsp.dma_buffer_size = dsp.in_data[1] + (dsp.in_data[0] << 8); |
411 | 27503323 | bellard | linfo ("set dma len %#x, %#x = %d\n",
|
412 | 27503323 | bellard | dsp.in_data[1], dsp.in_data[0], dsp.dma_buffer_size); |
413 | 27503323 | bellard | break;
|
414 | 27503323 | bellard | |
415 | 27503323 | bellard | case 0xe0: |
416 | 27503323 | bellard | dsp.out_data_len = 1;
|
417 | 27503323 | bellard | linfo ("data = %#x\n", dsp.in_data[0]); |
418 | 27503323 | bellard | dsp.out_data[0] = dsp.in_data[0] ^ 0xff; |
419 | 27503323 | bellard | break;
|
420 | 27503323 | bellard | |
421 | 27503323 | bellard | default:
|
422 | 27503323 | bellard | goto error;
|
423 | 27503323 | bellard | } |
424 | 27503323 | bellard | } |
425 | 27503323 | bellard | |
426 | 27503323 | bellard | dsp.cmd = -1;
|
427 | 27503323 | bellard | return;
|
428 | 27503323 | bellard | |
429 | 27503323 | bellard | error:
|
430 | 27503323 | bellard | Fail ("unrecognized command %#x", dsp.cmd);
|
431 | 27503323 | bellard | } |
432 | 27503323 | bellard | |
433 | 27503323 | bellard | static IO_WRITE_PROTO (dsp_write)
|
434 | 27503323 | bellard | { |
435 | 27503323 | bellard | int iport;
|
436 | 27503323 | bellard | |
437 | 27503323 | bellard | iport = nport - sb.port; |
438 | 27503323 | bellard | |
439 | 27503323 | bellard | switch (iport) {
|
440 | 27503323 | bellard | case 0x6: |
441 | 27503323 | bellard | if (0 == val) |
442 | 27503323 | bellard | dsp.v2x6 = 0;
|
443 | 27503323 | bellard | else if ((1 == val) && (0 == dsp.v2x6)) { |
444 | 27503323 | bellard | dsp.v2x6 = 1;
|
445 | 27503323 | bellard | dsp.out_data[dsp.out_data_len++] = 0xaa;
|
446 | 27503323 | bellard | } |
447 | 27503323 | bellard | else
|
448 | 27503323 | bellard | dsp.v2x6 = ~0;
|
449 | 27503323 | bellard | break;
|
450 | 27503323 | bellard | |
451 | 27503323 | bellard | case 0xc: /* write data or command | write status */ |
452 | 27503323 | bellard | if (0 == dsp.needed_bytes) { |
453 | 27503323 | bellard | command (val); |
454 | 27503323 | bellard | if (0 == dsp.needed_bytes) { |
455 | 27503323 | bellard | log_dsp (__func__); |
456 | 27503323 | bellard | } |
457 | 27503323 | bellard | } |
458 | 27503323 | bellard | else {
|
459 | 27503323 | bellard | dsp.in_data[dsp.in_index++] = val; |
460 | 27503323 | bellard | if (dsp.in_index == dsp.needed_bytes) {
|
461 | 27503323 | bellard | dsp.needed_bytes = 0;
|
462 | 27503323 | bellard | dsp.in_index = 0;
|
463 | 27503323 | bellard | complete (); |
464 | 27503323 | bellard | log_dsp (__func__); |
465 | 27503323 | bellard | } |
466 | 27503323 | bellard | } |
467 | 27503323 | bellard | break;
|
468 | 27503323 | bellard | |
469 | 27503323 | bellard | default:
|
470 | 27503323 | bellard | Fail ("(nport=%#x, val=%#x)", nport, val);
|
471 | 27503323 | bellard | } |
472 | 27503323 | bellard | } |
473 | 27503323 | bellard | |
474 | 27503323 | bellard | static IO_READ_PROTO (dsp_read)
|
475 | 27503323 | bellard | { |
476 | 27503323 | bellard | char *msg;
|
477 | 27503323 | bellard | int iport, retval;
|
478 | 27503323 | bellard | |
479 | 27503323 | bellard | msg = (char *) -1; |
480 | 27503323 | bellard | iport = nport - sb.port; |
481 | 27503323 | bellard | |
482 | 27503323 | bellard | switch (iport) {
|
483 | 27503323 | bellard | |
484 | 27503323 | bellard | case 0x6: /* reset */ |
485 | 27503323 | bellard | return 0; |
486 | 27503323 | bellard | |
487 | 27503323 | bellard | case 0xa: /* read data */ |
488 | 27503323 | bellard | if (dsp.out_data_len) {
|
489 | 27503323 | bellard | retval = dsp.out_data[--dsp.out_data_len]; |
490 | 27503323 | bellard | } |
491 | 27503323 | bellard | else {
|
492 | 27503323 | bellard | #if 1 |
493 | 27503323 | bellard | lwarn ("empty output buffer\n");
|
494 | 27503323 | bellard | retval = 0;
|
495 | 27503323 | bellard | #else
|
496 | 27503323 | bellard | msg = "empty output buffer";
|
497 | 27503323 | bellard | goto error;
|
498 | 27503323 | bellard | #endif
|
499 | 27503323 | bellard | } |
500 | 27503323 | bellard | break;
|
501 | 27503323 | bellard | |
502 | 27503323 | bellard | case 0xc: /* 0 can write */ |
503 | 27503323 | bellard | retval = 0;
|
504 | 27503323 | bellard | break;
|
505 | 27503323 | bellard | |
506 | 27503323 | bellard | case 0xd: /* timer interrupt clear */ |
507 | 27503323 | bellard | goto error;
|
508 | 27503323 | bellard | |
509 | 27503323 | bellard | case 0xe: /* data available status | irq 8 ack */ |
510 | bc0b1dc1 | bellard | /* XXX drop pic irq line here? */
|
511 | bc0b1dc1 | bellard | ldebug ("8 ack\n");
|
512 | 27503323 | bellard | retval = (0 == dsp.out_data_len) ? 0 : 0x80; |
513 | bc0b1dc1 | bellard | mixer.regs[0x82] &= ~mixer.regs[0x80]; |
514 | bc0b1dc1 | bellard | pic_set_irq (sb.irq, 0);
|
515 | 27503323 | bellard | break;
|
516 | 27503323 | bellard | |
517 | 27503323 | bellard | case 0xf: /* irq 16 ack */ |
518 | bc0b1dc1 | bellard | /* XXX drop pic irq line here? */
|
519 | 27503323 | bellard | ldebug ("16 ack\n");
|
520 | bc0b1dc1 | bellard | retval = 0xff;
|
521 | bc0b1dc1 | bellard | mixer.regs[0x82] &= ~mixer.regs[0x80]; |
522 | bc0b1dc1 | bellard | pic_set_irq (sb.irq, 0);
|
523 | 27503323 | bellard | break;
|
524 | 27503323 | bellard | |
525 | 27503323 | bellard | default:
|
526 | 27503323 | bellard | goto error;
|
527 | 27503323 | bellard | } |
528 | 27503323 | bellard | |
529 | 27503323 | bellard | if ((0xc != iport) && (0xe != iport)) { |
530 | bc0b1dc1 | bellard | ldebug ("nport=%#x iport %#x = %#x\n",
|
531 | bc0b1dc1 | bellard | nport, iport, retval); |
532 | 27503323 | bellard | } |
533 | 27503323 | bellard | |
534 | 27503323 | bellard | return retval;
|
535 | 27503323 | bellard | |
536 | 27503323 | bellard | error:
|
537 | 27503323 | bellard | Fail ("(nport=%#x) %s", nport, msg);
|
538 | 27503323 | bellard | } |
539 | 27503323 | bellard | |
540 | 27503323 | bellard | static IO_WRITE_PROTO(mixer_write_indexb)
|
541 | 27503323 | bellard | { |
542 | 27503323 | bellard | mixer.nreg = val & 0xff;
|
543 | 27503323 | bellard | } |
544 | 27503323 | bellard | |
545 | 27503323 | bellard | static IO_WRITE_PROTO(mixer_write_datab)
|
546 | 27503323 | bellard | { |
547 | 27503323 | bellard | mixer.regs[mixer.nreg] = val; |
548 | 27503323 | bellard | } |
549 | 27503323 | bellard | |
550 | 27503323 | bellard | static IO_WRITE_PROTO(mixer_write_indexw)
|
551 | 27503323 | bellard | { |
552 | 27503323 | bellard | mixer_write_indexb (env, nport, val & 0xff);
|
553 | 27503323 | bellard | mixer_write_datab (env, nport, (val >> 8) & 0xff); |
554 | 27503323 | bellard | } |
555 | 27503323 | bellard | |
556 | 27503323 | bellard | static IO_READ_PROTO(mixer_read)
|
557 | 27503323 | bellard | { |
558 | 27503323 | bellard | return mixer.regs[mixer.nreg];
|
559 | 27503323 | bellard | } |
560 | 27503323 | bellard | |
561 | 27503323 | bellard | void SB16_run (void) |
562 | 27503323 | bellard | { |
563 | 27503323 | bellard | if (0 == dsp.speaker) |
564 | 27503323 | bellard | return;
|
565 | 27503323 | bellard | |
566 | 27503323 | bellard | AUD_run (); |
567 | 27503323 | bellard | } |
568 | 27503323 | bellard | |
569 | 27503323 | bellard | static int write_audio (uint32_t addr, int len, int size) |
570 | 27503323 | bellard | { |
571 | 27503323 | bellard | int temp, net;
|
572 | 27503323 | bellard | |
573 | 27503323 | bellard | temp = size; |
574 | 27503323 | bellard | |
575 | 27503323 | bellard | net = 0;
|
576 | 27503323 | bellard | |
577 | 27503323 | bellard | while (temp) {
|
578 | 27503323 | bellard | int left_till_end;
|
579 | 27503323 | bellard | int to_copy;
|
580 | 27503323 | bellard | int copied;
|
581 | 27503323 | bellard | |
582 | 27503323 | bellard | left_till_end = len - dsp.dma_pos; |
583 | 27503323 | bellard | |
584 | 27503323 | bellard | to_copy = MIN (temp, left_till_end); |
585 | 27503323 | bellard | |
586 | 27503323 | bellard | copied = AUD_write ((void *) (addr + dsp.dma_pos), to_copy);
|
587 | 27503323 | bellard | |
588 | 27503323 | bellard | temp -= copied; |
589 | 27503323 | bellard | dsp.dma_pos += copied; |
590 | 27503323 | bellard | |
591 | 27503323 | bellard | if (dsp.dma_pos == len) {
|
592 | 27503323 | bellard | dsp.dma_pos = 0;
|
593 | 27503323 | bellard | } |
594 | 27503323 | bellard | |
595 | 27503323 | bellard | net += copied; |
596 | 27503323 | bellard | |
597 | 27503323 | bellard | if (copied != to_copy)
|
598 | 27503323 | bellard | return net;
|
599 | 27503323 | bellard | } |
600 | 27503323 | bellard | |
601 | 27503323 | bellard | return net;
|
602 | 27503323 | bellard | } |
603 | 27503323 | bellard | |
604 | 27503323 | bellard | static int SB_read_DMA (uint32_t addr, int size, int *_irq) |
605 | 27503323 | bellard | { |
606 | 27503323 | bellard | int free, till, copy, written;
|
607 | 27503323 | bellard | |
608 | 27503323 | bellard | if (0 == dsp.speaker) |
609 | 27503323 | bellard | return 0; |
610 | 27503323 | bellard | |
611 | 27503323 | bellard | if (dsp.left_till_irq < 0) { |
612 | 27503323 | bellard | dsp.left_till_irq += dsp.dma_buffer_size; |
613 | 27503323 | bellard | return dsp.dma_pos;
|
614 | 27503323 | bellard | } |
615 | 27503323 | bellard | |
616 | 27503323 | bellard | free = AUD_get_free (); |
617 | 27503323 | bellard | |
618 | 27503323 | bellard | if ((free <= 0) || (0 == size)) { |
619 | 27503323 | bellard | return dsp.dma_pos;
|
620 | 27503323 | bellard | } |
621 | 27503323 | bellard | |
622 | 27503323 | bellard | if (mix_block > 0) { |
623 | 27503323 | bellard | copy = MIN (free, mix_block); |
624 | 27503323 | bellard | } |
625 | 27503323 | bellard | else {
|
626 | 27503323 | bellard | copy = free; |
627 | 27503323 | bellard | } |
628 | 27503323 | bellard | |
629 | 27503323 | bellard | till = dsp.left_till_irq; |
630 | 27503323 | bellard | |
631 | 27503323 | bellard | ldebug ("addr:%#010x free:%d till:%d size:%d\n",
|
632 | 27503323 | bellard | addr, free, till, size); |
633 | 27503323 | bellard | if (till <= copy) {
|
634 | 27503323 | bellard | if (0 == dsp.dma_auto) { |
635 | 27503323 | bellard | copy = till; |
636 | 27503323 | bellard | } |
637 | 27503323 | bellard | } |
638 | 27503323 | bellard | |
639 | 27503323 | bellard | written = write_audio (addr, size, copy); |
640 | 27503323 | bellard | dsp.left_till_irq -= written; |
641 | 27503323 | bellard | AUD_adjust_estimate (free - written); |
642 | 27503323 | bellard | |
643 | 27503323 | bellard | if (dsp.left_till_irq <= 0) { |
644 | 27503323 | bellard | mixer.regs[0x82] |= mixer.regs[0x80]; |
645 | bc0b1dc1 | bellard | if (0 == noirq) { |
646 | bc0b1dc1 | bellard | ldebug ("request irq\n");
|
647 | 27503323 | bellard | *_irq = sb.irq; |
648 | bc0b1dc1 | bellard | } |
649 | 27503323 | bellard | |
650 | 27503323 | bellard | if (0 == dsp.dma_auto) { |
651 | 27503323 | bellard | control (0);
|
652 | 27503323 | bellard | } |
653 | 27503323 | bellard | } |
654 | 27503323 | bellard | |
655 | 27503323 | bellard | ldebug ("pos %5d free %5d size %5d till % 5d copy %5d dma size %5d\n",
|
656 | 27503323 | bellard | dsp.dma_pos, free, size, dsp.left_till_irq, copy, |
657 | 27503323 | bellard | dsp.dma_buffer_size); |
658 | 27503323 | bellard | |
659 | 27503323 | bellard | if (dsp.left_till_irq <= 0) { |
660 | 27503323 | bellard | dsp.left_till_irq += dsp.dma_buffer_size; |
661 | 27503323 | bellard | } |
662 | 27503323 | bellard | |
663 | 27503323 | bellard | return dsp.dma_pos;
|
664 | 27503323 | bellard | } |
665 | 27503323 | bellard | |
666 | 27503323 | bellard | static int dma_misc_handler (int moo) |
667 | 27503323 | bellard | { |
668 | 27503323 | bellard | return -1; |
669 | 27503323 | bellard | } |
670 | 27503323 | bellard | |
671 | 27503323 | bellard | static int magic_of_irq (int irq) |
672 | 27503323 | bellard | { |
673 | 27503323 | bellard | switch (irq) {
|
674 | 27503323 | bellard | case 2: |
675 | 27503323 | bellard | return 1; |
676 | 27503323 | bellard | case 5: |
677 | 27503323 | bellard | return 2; |
678 | 27503323 | bellard | case 7: |
679 | 27503323 | bellard | return 4; |
680 | 27503323 | bellard | case 10: |
681 | 27503323 | bellard | return 8; |
682 | 27503323 | bellard | default:
|
683 | 27503323 | bellard | log ("bad irq %d\n", irq);
|
684 | 27503323 | bellard | return 2; |
685 | 27503323 | bellard | } |
686 | 27503323 | bellard | } |
687 | 27503323 | bellard | |
688 | 27503323 | bellard | static int irq_of_magic (int magic) |
689 | 27503323 | bellard | { |
690 | 27503323 | bellard | switch (magic) {
|
691 | 27503323 | bellard | case 1: |
692 | 27503323 | bellard | return 2; |
693 | 27503323 | bellard | case 2: |
694 | 27503323 | bellard | return 5; |
695 | 27503323 | bellard | case 4: |
696 | 27503323 | bellard | return 7; |
697 | 27503323 | bellard | case 8: |
698 | 27503323 | bellard | return 10; |
699 | 27503323 | bellard | default:
|
700 | 27503323 | bellard | log ("bad irq magic %d\n", magic);
|
701 | 27503323 | bellard | return 2; |
702 | 27503323 | bellard | } |
703 | 27503323 | bellard | } |
704 | 27503323 | bellard | |
705 | 27503323 | bellard | void SB16_init (void) |
706 | 27503323 | bellard | { |
707 | 27503323 | bellard | int i;
|
708 | 27503323 | bellard | static const uint8_t dsp_write_ports[] = {0x6, 0xc}; |
709 | 27503323 | bellard | static const uint8_t dsp_read_ports[] = {0x6, 0xa, 0xc, 0xd, 0xe, 0xf}; |
710 | 27503323 | bellard | |
711 | 27503323 | bellard | mixer.regs[0x0e] = ~0; |
712 | 27503323 | bellard | mixer.regs[0x80] = magic_of_irq (sb.irq);
|
713 | 27503323 | bellard | mixer.regs[0x81] = 0x20 | (sb.dma << 1); |
714 | 27503323 | bellard | |
715 | 27503323 | bellard | DEREF (irq_of_magic); |
716 | 27503323 | bellard | |
717 | 27503323 | bellard | for (i = 0x30; i < 0x48; i++) { |
718 | 27503323 | bellard | mixer.regs[i] = 0x20;
|
719 | 27503323 | bellard | } |
720 | 27503323 | bellard | |
721 | 27503323 | bellard | for (i = 0; i < LENOFA (dsp_write_ports); i++) { |
722 | 27503323 | bellard | register_ioport_write (sb.port + dsp_write_ports[i], 1, dsp_write, 1); |
723 | 27503323 | bellard | } |
724 | 27503323 | bellard | |
725 | 27503323 | bellard | for (i = 0; i < LENOFA (dsp_read_ports); i++) { |
726 | 27503323 | bellard | register_ioport_read (sb.port + dsp_read_ports[i], 1, dsp_read, 1); |
727 | 27503323 | bellard | } |
728 | 27503323 | bellard | |
729 | 27503323 | bellard | register_ioport_write (sb.port + 0x4, 1, mixer_write_indexb, 1); |
730 | 27503323 | bellard | register_ioport_write (sb.port + 0x4, 1, mixer_write_indexw, 2); |
731 | 27503323 | bellard | register_ioport_read (sb.port + 0x5, 1, mixer_read, 1); |
732 | 27503323 | bellard | register_ioport_write (sb.port + 0x5, 1, mixer_write_datab, 1); |
733 | 27503323 | bellard | |
734 | 27503323 | bellard | DMA_register_channel (sb.hdma, SB_read_DMA, dma_misc_handler); |
735 | 27503323 | bellard | DMA_register_channel (sb.dma, SB_read_DMA, dma_misc_handler); |
736 | 27503323 | bellard | } |