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