Annotation of uae/src/md-i386-dos/sound/sb.c, revision 1.1.1.1

1.1       root        1:  /*
                      2:   * UAE - The Un*x Amiga Emulator
                      3:   *
                      4:   * DOS Sound Blaster interface.
                      5:   *
                      6:   * (c) 1996 Peter Remmers
                      7:   *
                      8:   * Changes by Gustavo Goedert and Michael Sontheimer ***MS
                      9:   */
                     10: 
                     11: 
                     12: #include "sysconfig.h"
                     13: #include "sysdeps.h"
                     14: 
                     15: #include <stdlib.h>
                     16: #include <stdio.h>
                     17: #include <string.h>
                     18: #include <ctype.h>
                     19: #include <dos.h>
                     20: #include <pc.h>
                     21: #include <sys/farptr.h>
                     22: #include <dpmi.h>
                     23: #include <go32.h>
                     24: 
                     25: #include "sound/sb.h"
                     26: #include "sound/dma.h"
                     27: 
                     28: /* some useful macros */
                     29: #define LOBYTE(x) ((UBYTE)(((UWORD)(x)) &  0xFF))
                     30: #define HIBYTE(x) ((UBYTE)(((UWORD)(x)) >> 8))
                     31: #define LOWORD(x) ((UWORD)(((ULONG)(x)) & 0xFFFF))
                     32: #define HIWORD(x) ((UWORD)(((ULONG)(x)) >> 16))
                     33: #define NUMPARAS(bytesize) (((bytesize)+15) >> 4)
                     34: 
                     35: #ifndef TRUE
                     36: #define TRUE 1
                     37: #endif
                     38: #ifndef FALSE
                     39: #define FALSE 0
                     40: #endif
                     41: 
                     42: /* Offsets relative to base I/O address. */
                     43: #define SB_MIXER_ADDRESS        0x04
                     44: #define SB_MIXER_DATA           0x05
                     45: #define SB_DSP_RESET            0x06
                     46: #define SB_DSP_READ_DATA        0x0A
                     47: #define SB_DSP_WRITE_DATA       0x0C
                     48: #define SB_DSP_WRITE_STATUS     0x0C
                     49: #define SB_DSP_DATA_AVAIL       0x0E
                     50: #define SB_DSP_INT_CLEAR        0x0F
                     51: 
                     52: /* DSP Commands */
                     53: #define CMD_PLAY_8BIT                0x14
                     54: #define CMD_SET_TIME_CONSTANT        0x40
                     55: #define CMD_DEFINE_SILENCE_BLOCK     0x80
                     56: #define CMD_PAUSE_8BIT_DMA           0xD0
                     57: #define CMD_CONTINUE_8BIT_DMA        0xD4
                     58: #define CMD_SPEAKER_ON               0xD1
                     59: #define CMD_SPEAKER_OFF              0xD3
                     60: #define CMD_GET_SPEAKER_SETTING      0xD8
                     61: #define CMD_DSP_VER                  0xE1
                     62: 
                     63: /* SbPro-only commands */
                     64: #define CMD_SET_BLOCK_SIZE           0x48
                     65: #define CMD_PLAY_8BIT_HISPEED        0x91
                     66: 
                     67: /* SB16-only commands */
                     68: #define CMD_SET_OUTPUT_RATE          0x41
                     69: #define CMD_PLAY_16BIT_SINGLE        0xB0
                     70: #define CMD_PLAY_16BIT_AUTOINIT      0xB6
                     71: #define CMD_PLAY_8BIT_SINGLE         0xC0
                     72: #define CMD_PLAY_8BIT_AUTOINIT       0xC6
                     73: #define CMD_PAUSE_16BIT_DMA          0xD5
                     74: #define CMD_CONTINUE_16BIT_DMA       0xD6
                     75: 
                     76: #define DMA_BUF_SIZE     14700 /* absolute maximum (FIFO_BUF_SIZE / 3) */
                     77: #define FIFO_BUF_SIZE    58800 /* max size for 1/4th second sound at   */
                     78:                               /* 44100Hz 16Bits Stereo                */
                     79: #define DESIRED_INT_RATE 70 /* The interrupt rate in Hz. The block size is  */
                     80:                            /* calculated from sampling rate and this value */
                     81: 
                     82: typedef void (*tPlayProc)(UWORD bytes);
                     83: typedef struct
                     84: {
                     85:   ULONG Data;
                     86:   UWORD Size;
                     87: }
                     88: tDMABuffer;
                     89: 
                     90: tSBType SB_Type;
                     91: 
                     92: UWORD SB_Base;      /* the Base Address  e.g. 220h                         */
                     93: UWORD SB_IRQ;       /* The IRQ           e.g. 5                            */
                     94: UWORD SB_DMAlo;     /* Low DMA channel   e.g. 1                            */
                     95: UWORD SB_DMAhi;     /* high DMA channel  e.g. 5                            */
                     96: UWORD SB_INT;       /* The Interrupt vector corresponding to IRQ e.g. 0Dh  */
                     97: UWORD SB_DMA;       /* The current DMA channel                             */
                     98:                    /* (depends on if we're playing 8Bit or 16Bit sound)   */
                     99: UBYTE SB_VersMaj;   /* Major DSP Version byte                              */
                    100: UBYTE SB_VersMin;   /* Minor DSP Version byte                              */
                    101: UBYTE SB_ModeByte;  /* Mode Byte for SB16 (Mono/Stereo, Signed/Unsigned )  */
                    102: UBYTE SB_StopByte;  /* DSP command to stop DMA                             */
                    103: UBYTE SB_ContByte;  /* DSP command to continue DMA                         */
                    104: UWORD SB_IntClearPort; /* SB Port to read from to acknoledge interrupt     */
                    105: 
                    106: tPlayProc SB_Single_Play_Proc; /* single-cycle        play function */
                    107: tPlayProc SB_Play_Proc;        /* autoinitialized DMA play function */
                    108: 
                    109: volatile int           SB_Playing;      /* Sound is currently playing    */
                    110: volatile unsigned long SB_Bytes_Played; /* number of bytes played so far */
                    111: 
                    112: volatile tDMABuffer dma_buf[2]; /* the two DMA buffers                  */
                    113: volatile UBYTE active_buf;      /* the currently playing buffer         */
                    114: volatile UBYTE last_buf;        /* last buffer to play                  */
                    115: volatile UWORD last_block_size; /* size of the previously played block  */
                    116: UWORD dma_block_size;           /* size of one DMA block                */
                    117: 
                    118: UBYTE fifobuf[FIFO_BUF_SIZE];   /* a ring buffer for sound buffering      */
                    119: UWORD fifo_max;                 /* max fill size of fifo buffer           */
                    120: UWORD fifo_write_index;         /* the index to write to the fifo buffer  */
                    121: UWORD fifo_read_index;          /* the index to read from the fifo buffer */
                    122: volatile UWORD fifo_size;       /* the current fill size                  */
                    123: 
                    124: BOOL  SB_Autoinit;    /* we're currently using autoinitialized DMA mode   */
                    125: BOOL  SB_Stopped;     /* Sound is stopped                                 */
                    126: BOOL  SB_16Bit;       /* we're playing in 16 Bits                         */
                    127: BOOL  SB_Stereo;      /* we're playing in stereo                          */
                    128: UWORD SB_Active_Rate; /* the active sampling rate                         */
                    129: 
                    130: volatile UBYTE IRQ_Detected; /* the detected IRQ number                   */
                    131: 
                    132: _go32_dpmi_registers cb_regs;    /* the registers for the int handler */
                    133: _go32_dpmi_seginfo   old_pmint;  /* the old interrupt handler         */
                    134: _go32_dpmi_seginfo   pm_wrapper; /* the new interrupt handler         */
                    135: _go32_dpmi_seginfo   dma_mem;    /* info for DMA memory               */
                    136: 
                    137: /* Reset SB at SB_Base, returns TRUE if it worked */
                    138: BOOL SB_Reset(void)
                    139: {
                    140:   outportb (SB_Base+SB_DSP_RESET, 1);
                    141:   delay(100);               // ***MS  We should wait 100ms, not 10ms
                    142:   outportb (SB_Base+SB_DSP_RESET, 0);
                    143:   delay(100);
                    144:   return (inportb(SB_Base+SB_DSP_READ_DATA) == 0xAA);
                    145: }
                    146: 
                    147: /* Write a command to DSP chip */
                    148: void SB_WriteDSP (UBYTE value)
                    149: {
                    150:   while (inportb(SB_Base + SB_DSP_WRITE_STATUS) & 0x80);
                    151:   outportb (SB_Base + SB_DSP_WRITE_DATA, value);
                    152: }
                    153: 
                    154: /* read a byte from DSP chip */
                    155: UBYTE SB_ReadDSP (void)
                    156: {
                    157:   while (!(inportb (SB_Base + SB_DSP_DATA_AVAIL) & 0x80));
                    158:   return  (inportb (SB_Base + SB_DSP_READ_DATA));
                    159: }
                    160: 
                    161: /* write a value to the mixer chip */
                    162: void SB_WriteMixer (UBYTE reg, UBYTE data)
                    163: {
                    164:   outportb (SB_Base + SB_MIXER_ADDRESS, reg);
                    165:   outportb (SB_Base + SB_MIXER_DATA,    data);
                    166: }
                    167: 
                    168: /* read a value from the mixer chip */
                    169: UBYTE SB_ReadMixer (UBYTE reg)
                    170: {
                    171:   outportb (SB_Base + SB_MIXER_ADDRESS, reg);
                    172:   return (inportb (SB_Base + SB_MIXER_DATA));
                    173: }
                    174: 
                    175: /* try to detect, if SB has a mixer chip, returns TRUE if found */
                    176: BOOL SB_DetectMixer (void)
                    177: {
                    178:   UBYTE orgval;
                    179: 
                    180:   orgval = SB_ReadMixer (0x22);
                    181: 
                    182:   // ***MS
                    183:   // Some SB-Pro-clones always set the bit 0 and 4, if reading
                    184:   // the volume (the bit 0 and 4 aren't used for setting the volume)
                    185: 
                    186:   SB_WriteMixer (0x22, 0);
                    187:   delay(200);
                    188:   if ((SB_ReadMixer (0x22)&(0xee)) != 0)
                    189:     return (FALSE);
                    190: 
                    191:   SB_WriteMixer (0x22, 0xEE);
                    192:   delay(200);
                    193:   if ((SB_ReadMixer (0x22)&(0xee)) != 0xEE)
                    194:     return (FALSE);
                    195: 
                    196: //  SB_WriteMixer (0x22, orgval);   // Why not setting the volume to maximum ?
                    197: 
                    198:   return (TRUE);
                    199: }
                    200: 
                    201: /* read a byte from the fifo buffer */
                    202: UBYTE fifo_read (void)
                    203: {
                    204:   UBYTE ret;
                    205: 
                    206:   if (fifo_size == 0) return(0);
                    207:   ret = fifobuf[fifo_read_index++];
                    208:   if (fifo_read_index == fifo_max) fifo_read_index = 0;
                    209:   fifo_size--;
                    210: 
                    211:   return (ret);
                    212: }
                    213: 
                    214: /* write a byte to the fifo buffer */
                    215: void fifo_write (UBYTE val)
                    216: {
                    217:   if (fifo_size == fifo_max) return;
                    218:   fifobuf[fifo_write_index++] = val;
                    219:   if (fifo_write_index == fifo_max) fifo_write_index = 0;
                    220:   fifo_size++;
                    221: }
                    222: 
                    223: /* fill a DMA buffer with data from the fifo buffer   */
                    224: /* if fifo buffer is empty after this, the DMA buffer */
                    225: /* is marked to be the last one to be played          */
                    226: void fill_buffer (UBYTE buf)
                    227: {
                    228:   register int   i;
                    229:   register UWORD size;
                    230:   register ULONG p;
                    231: 
                    232:   if (fifo_size == 0) return;
                    233: 
                    234:   disable();
                    235: 
                    236:   if (fifo_size < dma_block_size) size = fifo_size;
                    237:                             else size = dma_block_size;
                    238:   dma_buf[buf].Size = size;
                    239: 
                    240:   p = dma_buf[buf].Data;
                    241: 
                    242:   _farsetsel (_dos_ds);
                    243:   for (i=0; i < size; i++) _farnspokeb (p++, fifo_read());
                    244: 
                    245:   if (fifo_size == 0) last_buf = buf;
                    246: 
                    247:   enable ();
                    248: }
                    249: 
                    250: /* play a block in 8 Bits on a Standard SB */
                    251: void SB_Play_8Bit_Std (UWORD bytes)
                    252: {
                    253:   SB_WriteDSP (CMD_PLAY_8BIT);
                    254:   SB_WriteDSP (LOBYTE (bytes-1));
                    255:   SB_WriteDSP (HIBYTE (bytes-1));
                    256: }
                    257: 
                    258: /* Play a block in 8 Bits on an SB PRO */
                    259: void SB_Play_8Bit_SbPro (UWORD bytes)
                    260: {
                    261:   if (bytes != last_block_size)
                    262:   {
                    263:     SB_WriteDSP (CMD_SET_BLOCK_SIZE);
                    264:     SB_WriteDSP (LOBYTE (bytes-1));
                    265:     SB_WriteDSP (HIBYTE (bytes-1));
                    266:     last_block_size = bytes;
                    267:   }
                    268:   SB_WriteDSP (CMD_PLAY_8BIT_HISPEED);
                    269: }
                    270: 
                    271: /* play a block in 8 Bits on an SB 16 */
                    272: void SB_Play_8Bit_SB16 (UWORD bytes)
                    273: {
                    274:   SB_WriteDSP (CMD_PLAY_8BIT_SINGLE);
                    275:   SB_WriteDSP (SB_ModeByte);
                    276:   SB_WriteDSP (LOBYTE (bytes-1));
                    277:   SB_WriteDSP (HIBYTE (bytes-1));
                    278: }
                    279: 
                    280: /* play a block in 8 Bits on an SB 16 using autoinitialized DMA */
                    281: void SB_Play_8Bit_SB16_Autoinit (UWORD bytes)
                    282: {
                    283:   SB_WriteDSP (CMD_PLAY_8BIT_AUTOINIT);
                    284:   SB_WriteDSP (SB_ModeByte);
                    285:   SB_WriteDSP (LOBYTE (bytes-1));
                    286:   SB_WriteDSP (HIBYTE (bytes-1));
                    287: }
                    288: 
                    289: /* play a block in 16 Bits on an SB 16 */
                    290: void SB_Play_16Bit (UWORD bytes)
                    291: {
                    292:   SB_WriteDSP (CMD_PLAY_16BIT_SINGLE);
                    293:   SB_WriteDSP (SB_ModeByte);
                    294:   SB_WriteDSP (LOBYTE ((bytes>>1) -1));
                    295:   SB_WriteDSP (HIBYTE ((bytes>>1) -1));
                    296: }
                    297: 
                    298: /* play a block in 16 Bits on an SB 16 using autoinitialized DMA */
                    299: void SB_Play_16Bit_Autoinit (UWORD bytes)
                    300: {
                    301:   SB_WriteDSP (CMD_PLAY_16BIT_AUTOINIT);
                    302:   SB_WriteDSP (SB_ModeByte);
                    303:   SB_WriteDSP (LOBYTE ((bytes>>1) -1));
                    304:   SB_WriteDSP (HIBYTE ((bytes>>1) -1));
                    305: }
                    306: 
                    307: /* the interrupt handler for playing in single-cycle mode */
                    308: void SB_IntHandler_Single (void)
                    309: {
                    310:   UBYTE h = 1-active_buf;
                    311: 
                    312:   if (last_buf != 0xFF && fifo_size > 0)
                    313:   {
                    314:     last_buf        = 0xFF;
                    315:     last_block_size = 0;
                    316: 
                    317:     /* if we just played the last block and there's already new data */
                    318:     /* in the fifo buffer, then continue playing with the new data   */
                    319:     if (active_buf == last_buf)
                    320:     {
                    321:       fill_buffer (0);
                    322:       DMA_InitTransfer (SB_DMA, DMA_READ_AUTOINIT,
                    323:                         dma_buf[0].Data, dma_block_size*2);
                    324:       h = 0;
                    325:     }
                    326:   }
                    327: 
                    328:   (*SB_Single_Play_Proc) (dma_buf[h].Size);
                    329:   fill_buffer (active_buf);
                    330:   active_buf = h;
                    331: }
                    332: 
                    333: /* interrupt handler for playing in autoinitialized DMA mode */
                    334: void SB_IntHandler_Autoinit (void)
                    335: {
                    336:   if (last_buf != 0xFF)
                    337:   {
                    338:     if (active_buf == last_buf && fifo_size > 0)
                    339:     {
                    340:       last_buf   = 0xFF;
                    341:       active_buf = 0;
                    342: 
                    343:       DMA_InitTransfer (SB_DMA, DMA_READ_AUTOINIT,
                    344:                         dma_buf[0].Data, dma_block_size*2);
                    345: 
                    346:       fill_buffer (0);
                    347:       if (fifo_size == 0)
                    348:         (*SB_Single_Play_Proc) (dma_buf[0].Size);
                    349:       else
                    350:       {
                    351:         (*SB_Play_Proc) (dma_block_size);
                    352:         fill_buffer(1);
                    353:         if (fifo_size == 0)
                    354:           (*SB_Single_Play_Proc) (dma_buf[1].Size);
                    355:       }
                    356:     }
                    357:     else active_buf = 1 - active_buf;
                    358:   }
                    359:   else
                    360:   {
                    361:     fill_buffer (active_buf);
                    362:   
                    363:     if (active_buf == last_buf)
                    364:       (*SB_Single_Play_Proc) (dma_buf[active_buf].Size);
                    365:   
                    366:     active_buf = 1 - active_buf;
                    367:   }
                    368: }
                    369: 
                    370: /* the main interrupt handler */
                    371: void SB_IntHandler (void)
                    372: {
                    373:   SB_Bytes_Played += dma_buf[active_buf].Size;
                    374:   
                    375:   if (active_buf == last_buf && fifo_size == 0) SB_Playing = FALSE;
                    376:   else if (SB_Autoinit)                         SB_IntHandler_Autoinit();
                    377:   else                                          SB_IntHandler_Single();
                    378: 
                    379:   inportb (SB_IntClearPort);
                    380:   if (SB_IRQ > 7) outportb (0xA0, 0x20);
                    381: 
                    382:   outportb (0x20, 0x20);
                    383:   enable();
                    384: }
                    385: 
                    386: /* set sampling rate on a standard SB */
                    387: void SB_SetRate_LoSpeed (UWORD rate)
                    388: {
                    389:   SB_WriteDSP (CMD_SET_TIME_CONSTANT);
                    390:   SB_WriteDSP (0x100 - 1000000 / rate);
                    391: }
                    392: 
                    393: /* set sampling rate on a SB PRO */
                    394: void SB_SetRate_HiSpeed (UWORD rate)
                    395: {
                    396:   SB_WriteDSP (CMD_SET_TIME_CONSTANT);
                    397:   SB_WriteDSP (HIBYTE (0x10000 - 256000000 / rate));
                    398: }
                    399: 
                    400: /* set sampling rate on an SB 16 */
                    401: void SB_SetRate_SB16 (UWORD rate)
                    402: {
                    403:   SB_WriteDSP (CMD_SET_OUTPUT_RATE);
                    404:   SB_WriteDSP (HIBYTE (rate));
                    405:   SB_WriteDSP (LOBYTE (rate));
                    406: }
                    407: 
                    408: /* calculates the DMA block size and the fifo size   */
                    409: /* from sampling rate and the desired interrupt rate */
                    410: void calculate_buf_sizes(void)
                    411: {
                    412:   ULONG rate;
                    413: 
                    414:   rate = SB_Active_Rate;
                    415:   if (SB_16Bit)  rate <<= 1;
                    416:   if (SB_Stereo) rate <<= 1;
                    417:   /* rate is now bytes per second */
                    418:   dma_block_size = (rate / DESIRED_INT_RATE) & ~3; /* make multiples of 4 */
                    419:   dma_buf[1].Data = dma_buf[0].Data + dma_block_size;
                    420:   fifo_max = rate / 4; /* make fifo size 1/4th second of sound */
                    421: }
                    422: 
                    423: /* set the sampling rate */
                    424: int SB_SetRate (unsigned short rate)
                    425: {
                    426:   if (rate > 23000 && SB_Type < SB_Type_SbPro) return(FALSE);
                    427: 
                    428:   switch (SB_Type)
                    429:   {
                    430:   case SB_Type_StdSB: SB_SetRate_LoSpeed (rate); break;
                    431:   case SB_Type_SbPro: SB_SetRate_HiSpeed (rate); break;
                    432:   case SB_Type_SB16:  SB_SetRate_SB16    (rate); break;
                    433:   }
                    434: 
                    435:   SB_Active_Rate = rate;
                    436:   
                    437:   calculate_buf_sizes();
                    438: 
                    439:   return (TRUE);
                    440: }
                    441: 
                    442: /* set the number of bits, i.e. 8 Bit / 16 Bit mode */
                    443: int SB_SetBits (unsigned char bits)
                    444: {
                    445:   if (bits==16 && SB_Type < SB_Type_SB16) return (FALSE);
                    446: 
                    447:   if (SB_Type == SB_Type_SB16)
                    448:   {
                    449:     if (bits==16)
                    450:     {
                    451:       SB_Play_Proc        = SB_Play_16Bit_Autoinit;
                    452:       SB_Single_Play_Proc = SB_Play_16Bit;
                    453:       SB_IntClearPort     = SB_Base + SB_DSP_INT_CLEAR;
                    454:       SB_DMA              = SB_DMAhi;
                    455:       SB_StopByte         = CMD_PAUSE_16BIT_DMA;
                    456:       SB_ContByte         = CMD_CONTINUE_16BIT_DMA;
                    457:       SB_16Bit            = TRUE;
                    458:     }
                    459:     else
                    460:     {
                    461:       SB_Play_Proc        = SB_Play_8Bit_SB16_Autoinit;
                    462:       SB_Single_Play_Proc = SB_Play_8Bit_SB16;
                    463:       SB_IntClearPort     = SB_Base + SB_DSP_DATA_AVAIL;
                    464:       SB_DMA              = SB_DMAlo;
                    465:       SB_StopByte         = CMD_PAUSE_8BIT_DMA;
                    466:       SB_ContByte         = CMD_CONTINUE_8BIT_DMA;
                    467:       SB_16Bit            = FALSE;
                    468:     }
                    469:   }
                    470: 
                    471:   calculate_buf_sizes();
                    472: 
                    473:   return (TRUE);
                    474: }
                    475: 
                    476: /* set the number of channels, i.e. Mono / Stereo */
                    477: int SB_SetChannels (unsigned char channels)
                    478: {
                    479:   if (channels==2 && SB_Type < SB_Type_SbPro) return (FALSE);
                    480: 
                    481:   switch (SB_Type)
                    482:   {
                    483:   case SB_Type_StdSB: break;
                    484: 
                    485:   case SB_Type_SbPro:
                    486:     if (channels==2) SB_WriteMixer (0x0E, SB_ReadMixer (0x0E) | 0x02);
                    487:                 else SB_WriteMixer (0x0E, SB_ReadMixer (0x0E) & 0xFD);
                    488:     break;
                    489:     
                    490:   case SB_Type_SB16:
                    491:     if (channels==2) SB_ModeByte |= 0x20;
                    492:                 else SB_ModeByte &= 0xDF;
                    493:     break;
                    494:   }
                    495:   SB_Stereo = (channels==2);
                    496: 
                    497:   calculate_buf_sizes();
                    498: 
                    499:   return (TRUE);
                    500: }
                    501: 
                    502: /* set Signed / Unsigned mode (only on SB 16) */
                    503: int SB_SetSigned (int sign)
                    504: {
                    505:   if (sign && SB_Type < SB_Type_SB16) return (FALSE);
                    506:   
                    507:   if (SB_Type == SB_Type_SB16)
                    508:   {
                    509:     if (sign) SB_ModeByte |= 0x10;
                    510:          else SB_ModeByte &= 0xEF;
                    511:   }
                    512:   return (TRUE);
                    513: }
                    514: 
                    515: /* turn speaker off (doesn't seem to work on SB 16) */
                    516: void SB_SpeakerOff(void)
                    517: {
                    518:   SB_WriteDSP(CMD_SPEAKER_OFF);
                    519: }
                    520: 
                    521: /* turn speaker on (doesn't seem to work on SB 16) */
                    522: void SB_SpeakerOn(void)
                    523: {
                    524:   SB_WriteDSP(CMD_SPEAKER_ON);
                    525: }
                    526: 
                    527: /* start playing the sound data in the fifo buffer */
                    528: void SB_Play (void)
                    529: {
                    530:   if (fifo_size == 0) return;
                    531:   
                    532:   active_buf      = 0;
                    533:   last_buf        = 0xFF;
                    534:   last_block_size = 0;
                    535:   
                    536:   DMA_InitTransfer (SB_DMA, DMA_READ_AUTOINIT,
                    537:                     dma_buf[0].Data, dma_block_size*2);
                    538:   if (SB_Autoinit)
                    539:   {
                    540:     fill_buffer (0);
                    541:     if (fifo_size == 0)
                    542:       (*SB_Single_Play_Proc) (dma_buf[0].Size);
                    543:     else
                    544:     {
                    545:       (*SB_Play_Proc) (dma_block_size);
                    546:       fill_buffer(1);
                    547:       if (fifo_size == 0) (*SB_Single_Play_Proc) (dma_buf[1].Size);
                    548:     }
                    549:   }
                    550:   else
                    551:   {
                    552:     fill_buffer(0);
                    553:     (*SB_Single_Play_Proc) (dma_buf[0].Size);
                    554:     fill_buffer(1);
                    555:   }
                    556: 
                    557:   SB_Playing = TRUE;  
                    558: }
                    559: 
                    560: /* interrupt handler functions used for IRQ/DMA detection */
                    561: void irq2 (void)
                    562: {
                    563:   IRQ_Detected = 2;
                    564:   inportb (SB_IntClearPort);
                    565:   outportb (0x20, 0x20);
                    566: }
                    567: void irq5 (void)
                    568: {
                    569:   IRQ_Detected = 5;
                    570:   inportb (SB_IntClearPort);
                    571:   outportb (0x20, 0x20);
                    572: }
                    573: void irq7 (void)
                    574: {
                    575:   IRQ_Detected = 7;
                    576:   inportb (SB_IntClearPort);
                    577:   outportb (0x20, 0x20);
                    578: }
                    579: void irq10 (void)
                    580: {
                    581:   IRQ_Detected = 10;
                    582:   inportb (SB_IntClearPort);
                    583:   outportb (0xA0, 0x20);
                    584:   outportb (0x20, 0x20);
                    585: }
                    586: 
                    587: /* detect the sound blaster Base/IRQ/DMA settings (may hang computer) */
                    588: int SB_Detect (unsigned short *base, unsigned short *irq, 
                    589:                unsigned short *dmalo, unsigned short *dmahi)
                    590: {
                    591: #define TESTBUFSIZE 0x80
                    592:   static const UBYTE lowdma[3]  = { 1,3,0 };
                    593:   static const UBYTE highdma[3] = { 5,6,7 };
                    594:   
                    595:   UBYTE old21,oldA1,old0F,oldDE;
                    596:   _go32_dpmi_seginfo oldirq2,oldirq5,oldirq7,oldirq10;
                    597:   _go32_dpmi_seginfo newirq2,newirq5,newirq7,newirq10;
                    598:   _go32_dpmi_seginfo testbuf_info;
                    599:   ULONG testbuf;
                    600:   int   ret1,ret2,ret3,ret4;
                    601:   UBYTE i;
                    602:   BOOL  found;
                    603: 
                    604:   found = FALSE;
                    605: 
                    606:   *base  = 0;
                    607:   *irq   = 0;
                    608:   *dmalo = 0;
                    609:   *dmahi = 0;
                    610:   
                    611:   SB_Base = 0x210;
                    612:   while (SB_Base != 0x290)
                    613:   {
                    614:     if (SB_Reset()) break;
                    615:     SB_Base += 0x10;
                    616:   }
                    617:   if (SB_Base == 0x290) return (FALSE);
                    618:   *base = SB_Base;
                    619: 
                    620:   /* install detection interrupt handlers */
                    621:   newirq2.pm_offset  = (int) irq2;
                    622:   newirq5.pm_offset  = (int) irq5;
                    623:   newirq7.pm_offset  = (int) irq7;
                    624:   newirq10.pm_offset = (int) irq10;
                    625:   
                    626:   ret1 = _go32_dpmi_allocate_iret_wrapper (&newirq2);
                    627:   ret2 = _go32_dpmi_allocate_iret_wrapper (&newirq5);
                    628:   ret3 = _go32_dpmi_allocate_iret_wrapper (&newirq7);
                    629:   ret4 = _go32_dpmi_allocate_iret_wrapper (&newirq10);
                    630: 
                    631:   if (ret1|ret2|ret3|ret4)
                    632:   {
                    633:     if (!ret1) _go32_dpmi_free_iret_wrapper (&newirq2);
                    634:     if (!ret2) _go32_dpmi_free_iret_wrapper (&newirq5);
                    635:     if (!ret3) _go32_dpmi_free_iret_wrapper (&newirq7);
                    636:     if (!ret4) _go32_dpmi_free_iret_wrapper (&newirq10);
                    637:     return (FALSE);
                    638:   }
                    639:   _go32_dpmi_lock_code (irq2, (int)SB_Detect - (int)irq2);
                    640:   _go32_dpmi_lock_data ((void*)&IRQ_Detected,    sizeof(IRQ_Detected));
                    641:   _go32_dpmi_lock_data (&SB_IntClearPort, sizeof(SB_IntClearPort));
                    642: 
                    643:   _go32_dpmi_get_protected_mode_interrupt_vector (0x0A, &oldirq2);
                    644:   _go32_dpmi_get_protected_mode_interrupt_vector (0x0D, &oldirq5);
                    645:   _go32_dpmi_get_protected_mode_interrupt_vector (0x0F, &oldirq7);
                    646:   _go32_dpmi_get_protected_mode_interrupt_vector (0x72, &oldirq10);
                    647: 
                    648:   _go32_dpmi_set_protected_mode_interrupt_vector (0x0A, &newirq2);
                    649:   _go32_dpmi_set_protected_mode_interrupt_vector (0x0D, &newirq5);
                    650:   _go32_dpmi_set_protected_mode_interrupt_vector (0x0F, &newirq7);
                    651:   _go32_dpmi_set_protected_mode_interrupt_vector (0x72, &newirq10);
                    652: 
                    653:   testbuf_info.size = NUMPARAS(TESTBUFSIZE);
                    654:   testbuf = DMA_AllocDMABuf (&testbuf_info);
                    655: 
                    656:   /* turn on interrupts 2,5,7 and 10 */
                    657:   old21 = inportb (0x21);
                    658:   oldA1 = inportb (0xA1);
                    659:   outportb (0x21, old21 & 0x5B);
                    660:   outportb (0xA1, oldA1 & 0xFB);
                    661:   
                    662:   /* turn off DMA 0,1,3,5,6 and 7 */
                    663:   old0F = inportb (0x0F);
                    664:   oldDE = inportb (0xDE);
                    665:   outportb (0x0F, old0F | 0x0B);
                    666:   outportb (0xDE, oldDE | 0x0E);
                    667:   
                    668:   /* fill testbuffer with silence */
                    669:   _farsetsel (_dos_ds);
                    670:   for (i=0; i<TESTBUFSIZE; i++) _farnspokeb (testbuf+i, 128);
                    671:   
                    672:   /* detect low DMA and IRQ */
                    673:   SB_IntClearPort = SB_Base + SB_DSP_DATA_AVAIL;
                    674:   SB_SetRate_LoSpeed (22050);
                    675:   /* program SB to play testbuffer, will start playing */
                    676:   /* when we hit the right DMA channel                 */
                    677:   SB_Play_8Bit_Std (TESTBUFSIZE);
                    678:                                   
                    679:   /* test all DMA channels */
                    680:   IRQ_Detected = 0;
                    681:   for (i=0; i<3; i++)
                    682:   {
                    683:     DMA_InitTransfer (lowdma[i], DMA_READ, testbuf, TESTBUFSIZE);
                    684:     delay (TESTBUFSIZE*2000 / 22050);
                    685:     if (IRQ_Detected)
                    686:     {
                    687:       *dmalo = lowdma[i];
                    688:       *irq   = IRQ_Detected;
                    689:       break;
                    690:     }
                    691:   }
                    692:   
                    693:   if (IRQ_Detected)
                    694:   {
                    695:     found = TRUE;
                    696: 
                    697:     SB_WriteDSP (CMD_DSP_VER);
                    698:     SB_VersMaj = SB_ReadDSP();
                    699:     SB_VersMin = SB_ReadDSP();
                    700:     
                    701:     /* if SoundBlaster is an SB16 then test high DMA channel */
                    702:     if (SB_VersMaj >= 4)
                    703:     {
                    704:       outportb (0x0F, old0F | 0x0B);
                    705:       outportb (0xDE, oldDE | 0x0E);
                    706:       
                    707:       _farsetsel (_dos_ds);
                    708:       for (i=0; i<TESTBUFSIZE; i+=2) _farnspokew (testbuf+i, 0x8000);
                    709: 
                    710:       SB_ModeByte = 0x00;
                    711:       SB_IntClearPort = SB_Base + SB_DSP_INT_CLEAR;
                    712:       SB_SetRate_SB16(44100);
                    713:       SB_Play_16Bit(TESTBUFSIZE);
                    714:       
                    715:       IRQ_Detected = 0;
                    716:       for (i=0; i<3; i++)
                    717:       {
                    718:        DMA_InitTransfer (highdma[i], DMA_READ, testbuf, TESTBUFSIZE);
                    719:        delay (TESTBUFSIZE*1000 / 44100);
                    720:        if (IRQ_Detected)
                    721:         {
                    722:           *dmahi = highdma[i];
                    723:           break;
                    724:         }
                    725:       }
                    726:       if (!IRQ_Detected) *dmahi = *dmalo;
                    727:     }
                    728:   }
                    729:   
                    730:   /* clean up */
                    731:   _go32_dpmi_free_dos_memory (&testbuf_info);
                    732:   
                    733:   outportb (0x0F, old0F);
                    734:   outportb (0xDE, oldDE);
                    735:   outportb (0x21, old21);
                    736:   outportb (0xA1, oldA1);
                    737:   
                    738:   _go32_dpmi_set_protected_mode_interrupt_vector (0x0A, &oldirq2);
                    739:   _go32_dpmi_set_protected_mode_interrupt_vector (0x0D, &oldirq5);
                    740:   _go32_dpmi_set_protected_mode_interrupt_vector (0x0F, &oldirq7);
                    741:   _go32_dpmi_set_protected_mode_interrupt_vector (0x72, &oldirq10);
                    742:   
                    743:   _go32_dpmi_free_iret_wrapper (&newirq2);
                    744:   _go32_dpmi_free_iret_wrapper (&newirq5);
                    745:   _go32_dpmi_free_iret_wrapper (&newirq7);
                    746:   _go32_dpmi_free_iret_wrapper (&newirq10);
                    747:   
                    748:   return (found);
                    749: }
                    750: 
                    751: /* init Sound blaster using the given settings */
                    752: int SB_Init (unsigned short base, unsigned short irq, 
                    753:              unsigned short dmalo, unsigned short dmahi)
                    754: {
                    755:   SB_Base  = base;
                    756:   SB_IRQ   = irq;
                    757:   SB_DMAlo = dmalo;
                    758:   SB_DMAhi = dmahi;
                    759: 
                    760:   if (SB_IRQ > 7) SB_INT = 0x68 + SB_IRQ;
                    761:              else SB_INT = 0x08 + SB_IRQ;
                    762: 
                    763: 
                    764:   if (!SB_Reset()) return (FALSE);
                    765: 
                    766:   dma_mem.size = NUMPARAS(DMA_BUF_SIZE);
                    767:   dma_buf[0].Data = DMA_AllocDMABuf (&dma_mem);
                    768: 
                    769:   if (dma_buf[0].Data == NULL)
                    770:   {
                    771:     printf ("Cannot allocate DOS Memory for DMA buffers!\n");
                    772:     return (FALSE);
                    773:   }
                    774: 
                    775:   fifo_write_index = 0;
                    776:   fifo_read_index  = 0;
                    777:   fifo_size        = 0;
                    778: 
                    779:   pm_wrapper.pm_offset = (int) SB_IntHandler;
                    780:   if (_go32_dpmi_allocate_iret_wrapper (&pm_wrapper))
                    781:   {
                    782:     printf ("Cannot allocate protected mode wrapper for SB interrupt!\n");
                    783:     _go32_dpmi_free_dos_memory (&dma_mem);
                    784:     return (FALSE);
                    785:   }
                    786: 
                    787:   _go32_dpmi_lock_code (SB_WriteDSP, (ULONG)SB_WriteMixer - (ULONG)SB_WriteDSP);
                    788:   _go32_dpmi_lock_code (fifo_read, (ULONG)SB_SetRate_LoSpeed - (ULONG)fifo_read);
                    789:   _go32_dpmi_lock_data (&SB_Base, (ULONG)&old_pmint - (ULONG)&SB_Base);
                    790: 
                    791:   _go32_dpmi_get_protected_mode_interrupt_vector (SB_INT, &old_pmint);
                    792:   _go32_dpmi_set_protected_mode_interrupt_vector (SB_INT, &pm_wrapper);
                    793: 
                    794:   if (SB_IRQ>7)
                    795:     outportb(0xA1, inportb(0xA1) & ~(1 << (SB_IRQ-8)));
                    796:   else
                    797:     outportb(0x21, inportb(0x21) & ~(1 << SB_IRQ));
                    798: 
                    799:   SB_Type = SB_Type_StdSB;
                    800: 
                    801:   if( SB_DetectMixer() != FALSE )
                    802:     SB_Type = SB_Type_SbPro;
                    803: 
                    804:   SB_WriteDSP (CMD_DSP_VER);
                    805:   SB_VersMaj = SB_ReadDSP ();
                    806:   SB_VersMin = SB_ReadDSP ();
                    807: 
                    808:   if (SB_VersMaj >= 4) SB_Type = SB_Type_SB16;
                    809: 
                    810: //  SB_Type = SB_Type_SbPro;  /* for testing */
                    811: 
                    812:   /* initialize SB to default 8Bit Mono 22050Hz */
                    813:   /* (available on all SoundBlaster Versions)   */
                    814:   switch (SB_Type)
                    815:   {
                    816:   case SB_Type_StdSB:
                    817:     SB_Single_Play_Proc = SB_Play_8Bit_Std;
                    818:     SB_Autoinit         = FALSE;
                    819:     SB_SetRate_LoSpeed(22050);
                    820:     break;
                    821:   case SB_Type_SbPro:
                    822:     SB_Single_Play_Proc = SB_Play_8Bit_SbPro;
                    823:     SB_Autoinit         = FALSE;
                    824:     SB_SetRate_HiSpeed(22050);
                    825:     break;
                    826:   case SB_Type_SB16:
                    827:     SB_Single_Play_Proc = SB_Play_8Bit_SB16;
                    828:     SB_Play_Proc        = SB_Play_8Bit_SB16_Autoinit;
                    829:     SB_Autoinit         = TRUE;
                    830:     SB_SetRate_SB16(22050);
                    831:     break;
                    832:   }
                    833:   SB_IntClearPort = SB_Base + SB_DSP_DATA_AVAIL;
                    834:   SB_DMA          = SB_DMAlo;
                    835:   SB_Playing      = FALSE;
                    836:   SB_Stopped      = FALSE;
                    837:   SB_16Bit        = FALSE;
                    838:   SB_Stereo       = FALSE;
                    839:   SB_Active_Rate  = 22050;
                    840:   SB_ModeByte     = 0x00;
                    841:   SB_StopByte     = CMD_PAUSE_8BIT_DMA;
                    842:   SB_ContByte     = CMD_CONTINUE_8BIT_DMA;
                    843:   SB_Bytes_Played = 0;
                    844: 
                    845:   calculate_buf_sizes();
                    846: 
                    847:   SB_SpeakerOn();
                    848:   SB_SetVolume(6);
                    849: 
                    850:   return (TRUE);
                    851: }
                    852: 
                    853: /* clean up Sound Blaster */
                    854: void SB_Done (void)
                    855: {
                    856:   SB_Reset ();
                    857:   if (SB_IRQ > 7)
                    858:     outportb (0xA1, inportb(0xA1) | (1 << (SB_IRQ-8)));
                    859:   else
                    860:     outportb (0x21, inportb(0x21) | (1 << SB_IRQ));
                    861: 
                    862:   _go32_dpmi_set_protected_mode_interrupt_vector (SB_INT, &old_pmint);
                    863:   _go32_dpmi_free_iret_wrapper (&pm_wrapper);
                    864:   _go32_dpmi_free_dos_memory (&dma_mem);
                    865: }
                    866: 
                    867: /* write sound data to be played to the fifo buffer */
                    868: void SB_Write (void *buf, unsigned long size)
                    869: {
                    870: #define MIN(a,b) ((a)<(b) ? (a) : (b))
                    871: 
                    872:   UWORD s;
                    873:   int   i;
                    874: 
                    875:   if (SB_Stopped) SB_Continue();
                    876:   while (size > 0)
                    877:   {
                    878:     s = MIN (size, (UWORD) (fifo_max - dma_block_size));
                    879: 
                    880:     while (fifo_size+s > fifo_max);
                    881: 
                    882:     disable ();
                    883:     for (i=0; i<s; i++) fifo_write (*((UBYTE*)buf)++);
                    884:     enable ();
                    885: 
                    886:     if (!SB_Playing && fifo_size > dma_block_size*2) SB_Play();
                    887: 
                    888:     size -= s;
                    889:   }
                    890: }
                    891: 
                    892: /* start any unplayed sound */
                    893: void SB_Flush (void)
                    894: {
                    895:   if (SB_Stopped) SB_Continue();
                    896:   if (fifo_size>0 && !SB_Playing) SB_Play();
                    897: }
                    898: 
                    899: /* stop/pause sound output */
                    900: void SB_Stop (void)
                    901: {
                    902:   if (SB_Stopped) return;
                    903:   SB_WriteDSP (SB_StopByte);
                    904:   SB_Stopped = TRUE;
                    905: }
                    906: 
                    907: /* continue sound output */
                    908: void SB_Continue (void)
                    909: {
                    910:   if (!SB_Stopped) return;
                    911:   SB_WriteDSP (SB_ContByte);
                    912:   SB_Stopped = FALSE;
                    913: }
                    914: 
                    915: /* return thee DSP chip version */
                    916: void SB_GetVersion (unsigned char *Maj, unsigned char *Min)
                    917: {
                    918:   *Maj = SB_VersMaj;
                    919:   *Min = SB_VersMin;
                    920: }
                    921: 
                    922: /* return the current DMA block size */
                    923: unsigned short SB_GetFragmentSize (void)
                    924: {
                    925:   return (dma_block_size);
                    926: }
                    927: 
                    928: /* return the current fifo buffer size (not the fifo fill size) */
                    929: UWORD SB_GetBufferSize (void)
                    930: {
                    931:   return (fifo_max);
                    932: }
                    933: 
                    934: /* set sound blaster master volume, range from 0 to 7 by  */
                    935: void SB_SetVolume(UBYTE vol) {
                    936: 
                    937:   /* no mixer on standard sb */
                    938:   if (SB_Type < SB_Type_SbPro)
                    939:       return;
                    940: 
                    941:   if (vol>7)
                    942:       vol = 7;
                    943:   vol = (vol<<1);
                    944:   if (SB_Type == SB_Type_SbPro)
                    945:       vol = vol | 1;
                    946:   vol = vol | (vol<<4);
                    947: 
                    948:   SB_WriteMixer (0x22, vol);
                    949: }
                    950: 
                    951: /* Detection Routine */
                    952: int SB_DetectInitSound(int *dspbits, int *dsprate, int *sndbufsize)
                    953: {
                    954:   UWORD base,irq,dmalo,dmahi;
                    955:   char *blaster;
                    956:   char *tok;
                    957:   int have_sound;
                    958:   extern void (*SND_Write)(void *buf, unsigned long size);
                    959: 
                    960:   blaster = getenv ("BLASTER");
                    961:   if (blaster == NULL)
                    962:   {
                    963:     printf ("No BLASTER variable found!\n"
                    964:            "Detecting SoundBlaster Settings...."); fflush (stdout);
                    965:     have_sound = SB_Detect (&base, &irq, &dmalo, &dmahi);
                    966:     if (!have_sound)
                    967:     {
                    968:       printf ("Not found!!\n");
                    969:       return (0);
                    970:     }
                    971:     printf ("Found!  Base: %hx  IRQ: %hd  DMA: %hd,%hd\n",
                    972:                           base,     irq,    dmalo,dmahi);
                    973:   }
                    974:   else
                    975:   {
                    976:     base = 0; irq = 0; dmalo = 0; dmahi = 0;
                    977: 
                    978:     tok = strtok(blaster, " \t");
                    979:     while (tok != NULL)
                    980:     {
                    981:       switch (toupper(*tok))
                    982:       {
                    983:       case 'A': sscanf(tok+1, "%hx", &base);  break;
                    984:       case 'I': sscanf(tok+1, "%hd", &irq);   break;
                    985:       case 'D': sscanf(tok+1, "%hd", &dmalo); break;
                    986:       case 'H': sscanf(tok+1, "%hd", &dmahi); break;
                    987:       default: break;
                    988:       }
                    989:       tok = strtok (NULL, " \t");
                    990:     }
                    991:     printf ("SoundBlaster Settings taken from BLASTER variable:\n"
                    992:            "  Base: %hx  IRQ: %hd  DMA: %hd,%hd\n", base,irq,dmalo,dmahi);
                    993:   }
                    994: 
                    995:   have_sound = SB_Init (base, irq, dmalo, dmahi);
                    996:   if (!have_sound)
                    997:   {
                    998:     printf ("Cannot Initialize Soundblaster!\n");
                    999:     return (0);
                   1000:   }
                   1001: 
                   1002:   if (((*dspbits) == 16) && (SB_SetBits (16)))
                   1003:     SB_SetSigned (1);
                   1004:   else
                   1005:     *dspbits = 8;
                   1006: 
                   1007:   SB_SetRate(22050);
                   1008: 
                   1009:   if (((*dsprate) == 44100) && (!SB_SetRate(44100)))
                   1010:   {
                   1011:     printf ("SoundCard does not support playing at 44100 Hz, using 22050 Hz\n");
                   1012:     SB_SetRate(22050);
                   1013:     *dsprate = 22050;
                   1014:   }
                   1015: 
                   1016:   *sndbufsize = SB_GetFragmentSize();
                   1017: 
                   1018:   printf ("SoundBlaster%s found and configured for %d bits at %d Hz,\n"
                   1019:          "  Fragment size is %d Bytes, Buffer size is %d bytes.\n",
                   1020:           SB_Type==SB_Type_StdSB ? "" :
                   1021:          (SB_Type==SB_Type_SbPro ? " PRO" : " 16"),
                   1022:           *dspbits, *dsprate, *sndbufsize, SB_GetBufferSize());
                   1023: 
                   1024:   SND_Write = SB_Write;
                   1025: 
                   1026:   atexit (SB_Done);
                   1027: 
                   1028:   return 1;
                   1029: }
                   1030: 
                   1031: /* that's it... */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.