1 /* MikMod sound library
2 (c) 1998, 1999 Miodrag Vallat and others - see file AUTHORS for
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 /*==============================================================================
23 Sound Blaster I/O routines, common for SB8, SBPro and SB16
24 Written by Andrew Zabolotny <bit@eltech.ru>
26 ==============================================================================*/
38 #include <sys/nearptr.h>
39 #include <sys/farptr.h>
44 /********************************************* Private variables/routines *****/
48 /* Wait for SoundBlaster for some time */
49 #if !defined(__GNUC__) || (__GNUC__ < 3) || (__GNUC__ == 3 && __GNUC_MINOR__ == 0)
50 # define _func_noinline volatile /* match original code */
51 # define _func_noclone
53 /* avoid warnings from newer gcc:
54 * "function definition has qualified void return type" and
55 * function return types not compatible due to 'volatile' */
56 # define _func_noinline __attribute__((__noinline__))
57 # if (__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 5)
58 # define _func_noclone
60 # define _func_noclone __attribute__((__noclone__))
67 inportb(SB_DSP_RESET);
68 inportb(SB_DSP_RESET);
69 inportb(SB_DSP_RESET);
70 inportb(SB_DSP_RESET);
71 inportb(SB_DSP_RESET);
72 inportb(SB_DSP_RESET);
77 /* Make sure its not a spurious IRQ */
78 if (!irq_check(sb.irq_handle))
83 /* Acknowledge DMA transfer is complete */
84 if (sb.mode & SBMODE_16BITS)
89 /* SoundBlaster 1.x cannot do autoinit ... */
90 if (sb.dspver < SBVER_20)
91 __sb_dspreg_outwlh(SBDSP_DMA_PCM8, (sb.dma_buff->size >> 1) - 1);
94 irq_ack(sb.irq_handle);
97 if (sb.timer_callback)
101 static void sb_irq_end()
105 static boolean __sb_reset()
107 /* Disable the output */
110 /* Clear pending ints if any */
112 __sb_dsp_ack_dma16();
115 outportb(SB_DSP_RESET, SBM_DSP_RESET);
118 outportb(SB_DSP_RESET, 0);
120 /* Now wait for AA coming from datain port */
121 if (__sb_dsp_in() != 0xaa)
124 /* Finally, get the DSP version */
125 if ((sb.dspver = __sb_dsp_version()) == 0xffff)
128 if (sb.dspver != __sb_dsp_version())
134 /***************************************************** SB detection stuff *****/
136 static int __sb_irq_irqdetect(int irqno)
142 static void __sb_irq_dmadetect()
144 /* Make sure its not a spurious IRQ */
145 if (!irq_check(sb.irq_handle))
150 /* Acknowledge DMA transfer is complete */
151 if (sb.mode & SBMODE_16BITS)
152 __sb_dsp_ack_dma16();
157 irq_ack(sb.irq_handle);
160 static boolean __sb_detect()
162 /* First find the port number */
165 for (i = 5; i >= 0; i--) {
166 sb.port = 0x210 + i * 0x10;
176 /* Now detect the IRQ and DMA numbers */
178 unsigned int irqmask, sbirqmask, sbirqcount;
181 /* IRQ can be one of 2,3,5,7,10 */
182 irq_detect_start(0x04ac, __sb_irq_irqdetect);
184 /* Prepare timeout counter */
186 timer = _farnspeekl(0x46c);
189 sbirqcount = 10; /* Emit 10 SB irqs */
191 /* Tell SoundBlaster to emit IRQ for 8-bit transfers */
192 __sb_dsp_out(SBDSP_GEN_IRQ8);
195 irq_detect_get(0, &irqmask);
197 sbirqmask |= irqmask;
200 __sb_dsp_out(SBDSP_GEN_IRQ8);
202 if (_farnspeekl(0x46c) - timer >= 9) /* Wait ~1/2 secs */
206 for (sb.irq = 15; sb.irq > 0; sb.irq--)
207 if (irq_detect_get(sb.irq, &irqmask) == 10)
215 /* Detect the 8-bit and 16-bit DMAs */
216 if (!sb.dma8 || ((sb.dspver >= SBVER_16) && !sb.dma16)) {
217 static int __dma8[] = { 0, 1, 3 };
218 static int __dma16[] = { 5, 6, 7 };
222 /* Temporary hook SB IRQ */
223 sb.irq_handle = irq_hook(sb.irq, __sb_irq_dmadetect, 200);
224 irq_enable(sb.irq_handle);
228 /* Start a short DMA transfer and check if IRQ happened */
231 unsigned int timer, oldcount;
235 else if ((sb.dspver >= SBVER_16) && !sb.dma16)
240 for (i = 0; i < 3; i++) {
243 *dma = (dma == &sb.dma8) ? __dma8[i] : __dma16[i];
244 oldcount = sb.irqcount;
247 dma_set_mode(*dma, DMA_MODE_WRITE);
249 dma_set_count(*dma, 2);
252 __sb_dspreg_out(SBDSP_SET_TIMING, 206); /* 20KHz */
253 if (dma == &sb.dma8) {
255 __sb_dspreg_outwlh(SBDSP_DMA_PCM8, 1);
257 sb.mode = SBMODE_16BITS;
258 __sb_dspreg_out(SBDSP_DMA_GENERIC16, 0);
264 timer = _farnspeekl(0x46c);
266 while (oldcount == sb.irqcount)
267 if (_farnspeekl(0x46c) - timer >= 2) {
280 irq_unhook(sb.irq_handle);
281 sb.irq_handle = NULL;
282 if (!sb.dma8 || ((sb.dspver >= SBVER_16) && !sb.dma16))
288 /*************************************************** High-level interface *****/
290 /* Detect whenever SoundBlaster is present and fill "sb" structure */
295 /* Try to find the port and DMA from environment */
296 env = getenv("BLASTER");
298 while (env && *env) {
299 /* Skip whitespace */
300 while ((*env == ' ') || (*env == '\t'))
309 sb.port = strtol(env, &env, 16);
314 sb.aweport = strtol(env, &env, 16);
319 sb.irq = strtol(env, &env, 10);
324 sb.dma8 = strtol(env, &env, 10);
329 sb.dma16 = strtol(env, &env, 10);
332 /* Skip other values (H == MIDI, T == model, any other?) */
333 while (*env && (*env != ' ') && (*env != '\t'))
339 /* Try to detect missing sound card parameters */
342 if (!sb.port || !sb.irq || !sb.dma8)
348 if ((sb.dspver >= SBVER_16) && !sb.dma16)
351 if (sb.dspver >= SBVER_PRO)
352 sb.caps |= SBMODE_STEREO;
353 if (sb.dspver >= SBVER_16 && sb.dma16)
354 sb.caps |= SBMODE_16BITS;
355 if (sb.dspver < SBVER_20)
356 sb.maxfreq_mono = 22222;
358 sb.maxfreq_mono = 45454;
359 if (sb.dspver <= SBVER_16)
360 sb.maxfreq_stereo = 22727;
362 sb.maxfreq_stereo = 45454;
368 /* Reset SoundBlaster */
375 /* Start working with SoundBlaster */
378 __dpmi_meminfo struct_info;
387 /* Now lock the sb structure in memory */
388 struct_info.address = __djgpp_base_address + (unsigned long)&sb;
389 struct_info.size = sizeof(sb);
390 if (__dpmi_lock_linear_region(&struct_info))
393 /* Hook the SB IRQ */
394 sb.irq_handle = irq_hook(sb.irq, sb_irq, (long)sb_irq_end - (long)sb_irq);
395 if (!sb.irq_handle) {
396 __dpmi_unlock_linear_region(&struct_info);
400 /* Enable the interrupt */
401 irq_enable(sb.irq_handle);
410 /* Finish working with SoundBlaster */
413 __dpmi_meminfo struct_info;
419 /* Stop/free DMA buffer */
423 irq_unhook(sb.irq_handle);
424 sb.irq_handle = NULL;
426 /* Unlock the sb structure */
427 struct_info.address = __djgpp_base_address + (unsigned long)&sb;
428 struct_info.size = sizeof(sb);
429 __dpmi_unlock_linear_region(&struct_info);
434 /* Enable/disable stereo DSP mode */
435 /* Enable/disable speaker output */
436 void sb_output(boolean enable)
438 __sb_dsp_out(enable ? SBDSP_SPEAKER_ENA : SBDSP_SPEAKER_DIS);
441 /* Start playing from DMA buffer */
442 boolean sb_start_dma(unsigned char mode, unsigned int freq)
444 int dmachannel = (mode & SBMODE_16BITS) ? sb.dma16 : sb.dma8;
446 unsigned int tc = 0; /* timing constant (<=sbpro only) */
448 /* Stop DMA transfer if it is enabled */
452 if ((mode & SBMODE_MASK & sb.caps) != (mode & SBMODE_MASK))
455 /* Check this SB can perform at requested frequency */
456 if (((mode & SBMODE_STEREO) && (freq > sb.maxfreq_stereo))
457 || (!(mode & SBMODE_STEREO) && (freq > sb.maxfreq_mono)))
460 /* Check the timing constant here to avoid failing later */
461 if (sb.dspver < SBVER_16) {
462 /* SBpro cannot do signed transfer */
463 if (mode & SBMODE_SIGNED)
466 /* Old SBs have a different way on setting DMA timing constant */
468 if (mode & SBMODE_STEREO)
477 /* Get a DMA buffer enough for a 1/4sec interval... 4K <= dmasize <= 32K */
479 if (mode & SBMODE_STEREO)
481 if (mode & SBMODE_16BITS)
484 if (dmabuffsize < 4096)
486 if (dmabuffsize > 32768)
488 dmabuffsize = (dmabuffsize + 255) & 0xffffff00;
490 sb.dma_buff = dma_allocate(dmachannel, dmabuffsize);
494 /* Fill DMA buffer with silence */
495 dmabuffsize = sb.dma_buff->size;
496 if (mode & SBMODE_SIGNED)
497 memset(sb.dma_buff->linear, 0, dmabuffsize);
499 memset(sb.dma_buff->linear, 0x80, dmabuffsize);
501 /* Prime DMA for transfer */
502 dma_start(sb.dma_buff, dmabuffsize, DMA_MODE_WRITE | DMA_MODE_AUTOINIT);
504 /* Tell SoundBlaster to start transfer */
505 if (sb.dspver >= SBVER_16) { /* SB16 */
506 __sb_dspreg_outwhl(SBDSP_SET_RATE, freq);
508 /* Start DMA->DAC transfer */
509 __sb_dspreg_out(SBM_GENDAC_AUTOINIT | SBM_GENDAC_FIFO |
510 ((mode & SBMODE_16BITS) ? SBDSP_DMA_GENERIC16 :
512 ((mode & SBMODE_SIGNED) ? SBM_GENDAC_SIGNED : 0) |
513 ((mode & SBMODE_STEREO) ? SBM_GENDAC_STEREO : 0));
515 /* Write the length of transfer */
516 dmabuffsize = (dmabuffsize >> 2) - 1;
517 __sb_dsp_out(dmabuffsize);
518 __sb_dsp_out(dmabuffsize >> 8);
520 __sb_dspreg_out(SBDSP_SET_TIMING, 256 - tc);
521 dmabuffsize = (dmabuffsize >> 1) - 1;
522 if (sb.dspver >= SBVER_20) { /* SB 2.0/Pro */
523 /* Set stereo mode */
524 __sb_stereo((mode & SBMODE_STEREO) ? TRUE : FALSE);
525 __sb_dspreg_outwlh(SBDSP_SET_DMA_BLOCK, dmabuffsize);
526 if (sb.dspver >= SBVER_PRO)
527 __sb_dsp_out(SBDSP_HS_DMA_DAC8_AUTO);
529 __sb_dsp_out(SBDSP_DMA_PCM8_AUTO);
530 } else { /* Original SB */
531 /* Start DMA->DAC transfer */
532 __sb_dspreg_outwlh(SBDSP_DMA_PCM8, dmabuffsize);
539 /* Stop playing from DMA buffer */
545 if (sb.mode & SBMODE_16BITS)
546 __sb_dsp_out(SBDSP_DMA_HALT16);
548 __sb_dsp_out(SBDSP_DMA_HALT8);
550 dma_disable(sb.dma_buff->channel);
551 dma_free(sb.dma_buff);
555 /* Query current position/total size of the DMA buffer */
556 void sb_query_dma(unsigned int *dma_size, unsigned int *dma_pos)
558 unsigned int dma_left;
559 *dma_size = sb.dma_buff->size;
560 /* It can happen we try to read DMA count when HI/LO bytes will be
563 unsigned int dma_left_test;
564 dma_clear_ff(sb.dma_buff->channel);
565 dma_left_test = dma_get_count(sb.dma_buff->channel);
566 dma_left = dma_get_count(sb.dma_buff->channel);
567 if ((dma_left >= dma_left_test) && (dma_left - dma_left_test < 10))
570 *dma_pos = *dma_size - dma_left;