home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / dos / prg / dsik205 / dsik.dat / SOURCE / DETECT.C next >
C/C++ Source or Header  |  1995-04-10  |  9KB  |  320 lines

  1. /****************************************************************************
  2. *
  3. *                   Digital Sound Interface Kit (DSIK)
  4. *                            Version 2.00
  5. *
  6. *                           by Carlos Hasan
  7. *
  8. * Filename:     detect.c
  9. * Version:      Revision 1.1
  10. *
  11. * Language:     WATCOM C
  12. * Environment:  IBM PC (DOS/4GW)
  13. *
  14. * Description:  Soundcards autodetection routines.
  15. *
  16. * Revision History:
  17. * ----------------
  18. *
  19. * Revision 1.1  94/12/28  13:02:27  chv
  20. * New Windows Sound System detection routine.
  21. *
  22. * Revision 1.0  94/11/20  18:02:36  chv
  23. * Initial revision
  24. *
  25. ****************************************************************************/
  26.  
  27. #include <i86.h>
  28. #include <dos.h>
  29. #include <env.h>
  30. #include <stdio.h>
  31. #include <conio.h>
  32. #include <string.h>
  33. #include "audio.h"
  34.  
  35. #define TRUE    1
  36. #define FALSE   0
  37.  
  38. struct DPMIREGS {
  39.     long    edi,esi,ebp,reserved,ebx,edx,ecx,eax;
  40.     short   flags,es,ds,fs,gs,ip,cs,sp,ss;
  41. };
  42.  
  43.  
  44. /****************************************************************************
  45. *
  46. * Function:     SBDetect
  47. * Parameters:   SC  - soundcard structure
  48. *
  49. * Returns:      Returns true if a Sound Blaster card is present.
  50. *
  51. * Description:  Autodetect soundcard device.
  52. *
  53. ****************************************************************************/
  54.  
  55. static int SBDetect(SoundCard *SC)
  56. {
  57.     char *Blaster,*Ptr,Buffer[32];
  58.     int Port,Irq,LowDma,HighDma,MidiPort,Type;
  59.  
  60.     /* The BLASTER environment variable parameter meaning:
  61.           A specifies the base address (0x220)
  62.           I specifies the IRQ (7)
  63.           D specifies the low DMA channel (1)
  64.           H specifies the high DMA channel (5)
  65.           P specifies the MIDI base address (0x330)
  66.           T specifies the version of the soundcard:
  67.               1 : Sound Blaster 1.0/1.5
  68.               2 : Sound Blaster Pro
  69.               3 : Sound Blaster 2.0/2.5
  70.               4 : Sound Blaster Pro 3/Pro 4.0
  71.               5 : Sound Blaster Pro (Microchannel)
  72.               6 : Sound Blaster 16
  73.     */
  74.  
  75.     if (!(Blaster = getenv("BLASTER")))
  76.         return FALSE;
  77.  
  78.     Port = 0x220;
  79.     Irq = 7;
  80.     LowDma = 1;
  81.     HighDma = 5;
  82.     MidiPort = 0x330;
  83.     Type = 1;
  84.     strupr(strncpy(Buffer,Blaster,sizeof(Buffer)));
  85.     for (Ptr = strtok(Buffer," "); Ptr != NULL; Ptr = strtok(NULL," ")) {
  86.         switch (*Ptr) {
  87.             case 'A':
  88.                 if (!sscanf(Ptr,"A%03X",&Port)) return FALSE;
  89.                 break;
  90.             case 'I':
  91.                 if (!sscanf(Ptr,"I%d",&Irq)) return FALSE;
  92.                 break;
  93.             case 'D':
  94.                 if (!sscanf(Ptr,"D%d",&LowDma)) return FALSE;
  95.                 break;
  96.             case 'H':
  97.                 if (!sscanf(Ptr,"H%d",&HighDma)) return FALSE;
  98.                 break;
  99.             case 'P':
  100.                 if (!sscanf(Ptr,"P%03X",&MidiPort)) return FALSE;
  101.                 break;
  102.             case 'T':
  103.                 if (!sscanf(Ptr,"T%d",&Type)) return FALSE;
  104.                 break;
  105.             /*
  106.             default:
  107.                 return FALSE;
  108.             */
  109.         }
  110.     }
  111.  
  112.     if (Type >= 6) {
  113.         SC->ID = ID_SB16;
  114.         SC->Modes = AF_16BITS | AF_STEREO;
  115.     }
  116.     else if (Type == 3) {
  117.         SC->ID = ID_SB201;
  118.         SC->Modes = AF_8BITS | AF_MONO;
  119.     }
  120.     else if ((Type == 2) || (Type == 4) || (Type == 5)) {
  121.         SC->ID = ID_SBPRO;
  122.         SC->Modes = AF_8BITS | AF_STEREO;
  123.     }
  124.     else {
  125.         SC->ID = ID_SB;
  126.         SC->Modes = AF_8BITS | AF_MONO;
  127.     }
  128.  
  129.     SC->Port = Port;
  130.     SC->IrqLine = Irq;
  131.     SC->DmaChannel = (SC->Modes & AF_16BITS) ? HighDma : LowDma;
  132.     SC->SampleRate = 22050;
  133.     return TRUE;
  134. }
  135.  
  136. /****************************************************************************
  137. *
  138. * Function:     GUSDetect
  139. * Parameters:   SC  - soundcard structure
  140. *
  141. * Returns:      Returns true if a UltraSound card is present.
  142. *
  143. * Description:  Autodetect soundcard device.
  144. *
  145. ****************************************************************************/
  146.  
  147. static int GUSDetect(SoundCard *SC)
  148. {
  149.     char *Ultrasnd;
  150.     int Port,PlayDma,RecDma,Gf1Irq,MidiIrq;
  151.  
  152.     if (!(Ultrasnd = getenv("ULTRASND")))
  153.         return FALSE;
  154.  
  155.     if (sscanf(Ultrasnd,"%03X,%d,%d,%d,%d",
  156.         &Port,&PlayDma,&RecDma,&Gf1Irq,&MidiIrq) != 5)
  157.         return FALSE;
  158.  
  159.     SC->ID = ID_GUS;
  160.     SC->Modes = AF_16BITS | AF_STEREO;
  161.     SC->Port = Port;
  162.     SC->IrqLine = Gf1Irq;
  163.     SC->DmaChannel = PlayDma;
  164.     SC->SampleRate = 44100;
  165.     return TRUE;
  166. }
  167.  
  168. /****************************************************************************
  169. *
  170. * Function:     PASProbe
  171. * Parameters:   Port    - base I/O port address
  172. *
  173. * Returns:      Returns true if a Pro Audio Spectrum is there.
  174. *
  175. * Description:  Autodetect soundcard device.
  176. *
  177. ****************************************************************************/
  178.  
  179. static int PASProbe(int Port)
  180. {
  181.     int Data;
  182.     if ((Data = inp(Port ^ 0x388 ^ 0xB8B)) == 0xFF) return FALSE;
  183.     outp(Port ^ 0x388 ^ 0xB8B, Data ^ 0xE0);
  184.     return (inp(Port ^ 0x388 ^ 0xB8B) == Data);
  185. }
  186.  
  187. /****************************************************************************
  188. *
  189. * Function:     PASDetect
  190. * Parameters:   SC  - soundcard structure
  191. *
  192. * Returns:      Returns true if a Pro Audio Spectrum is present.
  193. *
  194. * Description:  Autodetect soundcard device.
  195. *
  196. ****************************************************************************/
  197.  
  198. static int PASDetect(SoundCard *SC)
  199. {
  200.     union REGS regs;
  201.     struct SREGS sregs;
  202.     struct DPMIREGS dregs;
  203.     int Port,Irq,Dma;
  204.  
  205.     memset((void*)®s,0,sizeof(regs));
  206.     memset((void*)&sregs,0,sizeof(sregs));
  207.     memset((void*)&dregs,0,sizeof(dregs));
  208.  
  209.     /* detect if MVSOUND.SYS is present */
  210.     regs.w.ax = 0x0300;
  211.     regs.h.bl = 0x2F;
  212.     regs.h.bh = 0;
  213.     regs.w.cx = 0;
  214.     regs.x.edi = FP_OFF(&dregs);
  215.     sregs.es = FP_SEG(&dregs);
  216.     dregs.eax = 0xBC00;
  217.     dregs.ebx = 0x3F3F;
  218.     dregs.ecx = 0x0000;
  219.     dregs.edx = 0x0000;
  220.     int386x(0x31,®s,®s,&sregs);
  221.     if (((dregs.ebx ^ dregs.ecx ^ dregs.edx) & 0xFFFF) != 0x4D56)
  222.         return FALSE;
  223.  
  224.     /* call MVSOUND.SYS to ask for the IRQ and DMA parameters */
  225.     regs.w.ax = 0x0300;
  226.     regs.h.bl = 0x2F;
  227.     regs.h.bh = 0;
  228.     regs.w.cx = 0;
  229.     regs.x.edi = FP_OFF(&dregs);
  230.     sregs.es = FP_SEG(&dregs);
  231.     dregs.eax = 0xBC04;
  232.     int386x(0x31,®s,®s,&sregs);
  233.     Irq = dregs.ecx & 0xFF;
  234.     Dma = dregs.ebx & 0xFF;
  235.  
  236.     /* detect the I/O Port address */
  237.     if (!PASProbe(Port = 0x388) && !PASProbe(Port = 0x384) &&
  238.         !PASProbe(Port = 0x38C) && !PASProbe(Port = 0x288))
  239.         return FALSE;
  240.  
  241.     SC->ID = ID_PAS;
  242.     SC->Modes = AF_8BITS | AF_STEREO;
  243.     SC->Port = Port;
  244.     SC->IrqLine = Irq;
  245.     SC->DmaChannel = Dma;
  246.     SC->SampleRate = 22050;
  247.     return TRUE;
  248. }
  249.  
  250. /****************************************************************************
  251. *
  252. * Function:     WSSProbe
  253. * Parameters:   Port    - base I/O port address
  254. *
  255. * Returns:      Returns true if a Windows Sound System is there.
  256. *
  257. * Description:  Autodetect soundcard device.
  258. *
  259. ****************************************************************************/
  260.  
  261. static int WSSProbe(int Port)
  262. {
  263.     int data;
  264.     if (!(inp(Port+0x04) & 0x80)) {
  265.         outp(Port+0x04,0x0c);
  266.         data = inp(Port+0x05);
  267.         outp(Port+0x05,0x00);
  268.         if ((inp(Port+0x04) == 0x0c) && (inp(Port+0x05) == data))
  269.             return TRUE;
  270.     }
  271.     return FALSE;
  272. }
  273.  
  274. /****************************************************************************
  275. *
  276. * Function:     WSSDetect
  277. * Parameters:   SC  - soundcard structure
  278. *
  279. * Returns:      Returns true if a Windows Sound System card is present.
  280. *
  281. * Description:  Autodetect soundcard device.
  282. *
  283. ****************************************************************************/
  284.  
  285. static int WSSDetect(SoundCard *SC)
  286. {
  287.     int Port;
  288.     if (!WSSProbe(Port = 0x530) && !WSSProbe(Port = 0x604) &&
  289.         !WSSProbe(Port = 0xE80) && !WSSProbe(Port = 0xF40))
  290.         return FALSE;
  291.     SC->ID = ID_WSS;
  292.     SC->Modes = AF_16BITS | AF_STEREO;
  293.     SC->Port = Port;
  294.     SC->IrqLine = 7;
  295.     SC->DmaChannel = 1;
  296.     SC->SampleRate = 22050;
  297.     return TRUE;
  298. }
  299.  
  300.  
  301. /****************************************************************************
  302. *
  303. * Function:     dAutoDetect
  304. * Parameters:   SC  - Soundcard structure
  305. *
  306. * Description:  Autodetect soundcard device.
  307. *
  308. ****************************************************************************/
  309.  
  310. int dAutoDetect(SoundCard *SC)
  311. {
  312.     if (!GUSDetect(SC) && !PASDetect(SC) &&
  313.             !SBDetect(SC) && !WSSDetect(SC)) {
  314.         SC->ID = ID_NONE;
  315.         SC->Modes = AF_8BITS | AF_MONO;
  316.         SC->Port = SC->SampleRate = SC->IrqLine = SC->DmaChannel = 0;
  317.     }
  318.     return (SC->ID == ID_NONE);
  319. }
  320.