home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / sphinx / examples / sblaster / sb.h__ < prev    next >
Encoding:
Text File  |  1994-03-27  |  8.4 KB  |  381 lines

  1. /* Include file for C-- Sound Blaster library */
  2.  
  3. /* Defines for Sound Blaster and Sound Blaster Pro IO address */
  4. ?define LEFT_FM_STATUS      0x00    /* Pro only */
  5. ?define LEFT_FM_ADDRESS     0x00    /* Pro only */
  6. ?define LEFT_FM_DATA        0x01    /* Pro only */
  7. ?define RIGHT_FM_STATUS     0x02    /* Pro only */
  8. ?define RIGHT_FM_ADDRESS    0x02    /* Pro only */
  9. ?define RIGHT_FM_DATA       0x03    /* Pro only */
  10. ?define MIXER_ADDRESS       0x04    /* Pro only */
  11. ?define MIXER_DATA          0x05    /* Pro only */
  12. ?define DSP_RESET           0x06
  13. ?define FM_STATUS           0x08
  14. ?define FM_ADDRESS          0x08
  15. ?define FM_DATA             0x09
  16. ?define DSP_READ_DATA       0x0A
  17. ?define DSP_WRITE_DATA      0x0C
  18. ?define DSP_WRITE_STATUS    0x0C
  19. ?define DSP_DATA_AVAIL      0x0E
  20. ?define CD_ROM_DATA         0x10    /* Pro only */
  21. ?define CD_ROM_STATUS       0x11    /* Pro only */
  22. ?define CD_ROM_RESET        0x12    /* Pro only */
  23. ?define CD_ROM_ENABLE       0x13    /* Pro only */
  24.  
  25. ?define ADLIB_FM_STATUS     0x388
  26. ?define ADLIB_FM_ADDRESS    0x388
  27. ?define ADLIB_FM_DATA       0x389
  28.  
  29. /* Defines for 8237 DMA Controller IO addresses (not used currently) */
  30. /*
  31. ?define DMA         0x00
  32. ?define CH0_BASE    DMA+0
  33. ?define CH0_COUNT   DMA+1
  34. ?define CH1_BASE    DMA+2
  35. ?define CH1_COUNT   DMA+3
  36. ?define CH2_BASE    DMA+4
  37. ?define CH2_COUNT   DMA+5
  38. ?define CH3_BASE    DMA+6
  39. ?define CH3_COUNT   DMA+7
  40. ?define DMA_STATUS  DMA+8
  41. ?define DMA_CMD     DMA+8
  42. ?define DMA_REQUEST DMA+9
  43. ?define DMA_MASK    DMA+10
  44. ?define DMA_MODE    DMA+11
  45. ?define DMA_FF      DMA+12
  46. ?define DMA_TMP     DMA+13
  47. ?define DMA_CLEAR   DMA+13
  48. ?define DMA_CLRMSK  DMA+14
  49. ?define DMA_WRMSK   DMA+15
  50. ?define DMAPAGE     0x80
  51. */
  52.  
  53. /* Types of Sound Blaster Cards */
  54. ?define SB15    1
  55. ?define SBPro    2
  56. ?define SB20    3
  57.  
  58. /* DSP Commands */
  59. ?define DIRECT_8_BIT_DAC    0x10
  60. ?define DMA_8_BIT_DAC        0x14
  61. ?define DMA_2_BIT_DAC        0x16
  62. ?define DMA_2_BIT_REF_DAC    0x17
  63. ?define DIRECT_ADC        0x20
  64. ?define DMA_ADC            0x24
  65. ?define MIDI_READ_POLL        0x30
  66. ?define MIDI_READ_IRQ        0x31
  67. ?define MIDI_WRITE_POLL        0x38
  68. ?define TIME_CONSTANT        0x40
  69. ?define DMA_8_BIT_DAC_HI_INIT    0x48   // for high-speed SB Pro transfers
  70. ?define DMA_4_BIT_DAC        0x74
  71. ?define DMA_4_BIT_REF_DAC    0x75
  72. ?define DMA_26_BIT_DAC        0x76
  73. ?define DMA_26_BIT_REF_DAC    0x77
  74. ?define DMA_8_BIT_DAC_HI_START    0x91   // for high-speed SB Pro transfers
  75. ?define DMA_8_BIT_ADC_HI_START    0x99   // for high-speed SB Pro transfers
  76. ?define HALT_DMA        0xD0
  77. ?define CONTINUE_DMA        0xD4
  78. ?define SPEAKER_ON        0xD1
  79. ?define SPEAKER_OFF        0xD3
  80. ?define DSP_ID            0xE0
  81. ?define DSP_VER            0xE1
  82. ?define DSP_COPYRIGHT        0xE3
  83. ?define INT_REQUEST        0xF2
  84. ?define MDAC1            0x61
  85. ?define MDAC2            0x62
  86. ?define MDAC3            0x63
  87. ?define MDAC4            0x64
  88. ?define MDAC5            0x65
  89. ?define MDAC6            0x66
  90. ?define MDAC7            0x67
  91.  
  92. /* Defines for the mixer */
  93. ?define MIXER_RESET 0x00
  94. ?define MASTER_VOL  0x22
  95. ?define VOC_VOL     0x04
  96. ?define LINE_VOL    0x2e
  97. ?define FM_VOL        0x26
  98. ?define CD_VOL        0x28
  99. ?define RECORD_SRC  0x0C
  100. ?define MIC        0x11
  101. ?define LINE        0x17
  102. ?define CD        0x13
  103.  
  104. /*
  105.    These four need to be set to the proper values by SB_GET_PARAMS()
  106.    and SB_INIT or by SB_DETECT() before making calls to any other
  107.    routines!
  108. */
  109. word SbIOaddr;    // == 0x220
  110. word SbIRQ;       // == 7
  111. word SbDMAchan;   // == 1
  112. word SbType;      // == 1
  113.  
  114. /*
  115.    Write data to the DSP
  116.  
  117.    On entry: byte to be written in AL
  118. */
  119. void WRITEDSP()
  120. {
  121.    $ MOV AH,AL     // save a copy of AL for later
  122.    DX = SbIOaddr + DSP_WRITE_STATUS;
  123. WAITLOOP:
  124.    $ IN AL,DX
  125.    $ OR AL,AL
  126.    $ JS WAITLOOP
  127. //   DX = SbIOaddr + DSP_WRITE_DATA;   // not necessary (same port)
  128.    $ MOV AL,AH
  129.    $ OUT DX,AL
  130. }
  131.  
  132. /*
  133.    Read data from the DSP, with a timeout
  134.    Usage: READDSP(,,count)
  135.    On exit: byte read in AL
  136. */
  137. byte READDSP()
  138. {
  139.    DX = SbIOaddr + DSP_DATA_AVAIL;
  140.    loop(CX)
  141.    {
  142.       $ IN AL,DX
  143.       $ OR AL,AL
  144.       $ JS READY
  145.    }
  146. READY:
  147.    $ SUB DL,4         // DX = SbIOaddr + DSP_READ_DATA;
  148.    $ IN AL,DX
  149. }
  150.  
  151. /*
  152.    Write a byte to the SB Pro mixer chip
  153.  
  154.    Usage: WRITEMIXER(x,y);
  155.  
  156.    x specifies to which line the value y should be written
  157. */
  158. void WRITEMIXER()
  159. {
  160.    DX = SbIOaddr+MIXER_ADDRESS;
  161.    $ OUT DX,AL   // output x
  162.    $ INC DX      // DX = SbIOaddr+MIXER_DATA  ==  SbIOaddr+MIXER_ADDRESS+1
  163.    AL = BL;      // output y
  164.    $ OUT DX,AL
  165. }
  166.  
  167. /*
  168.    Read a byte from the SB Pro mixer chip.
  169.    The value is returned in AL.
  170.  
  171.    Usage: READMIXER(byte x);
  172.  
  173.    x specifies from which line the value should be read
  174. */
  175. byte READMIXER()
  176. {
  177.    DX = SbIOaddr+MIXER_ADDRESS;
  178.    $ OUT DX,AL     // output x
  179.    $ INC DX        // DX = SbIOaddr+MIXER_DATA;
  180.    $ IN AL,DX
  181. }
  182.  
  183. /*
  184.    Reset routine for Sound Blaster.
  185.    Returns non-zero value on error.
  186. */
  187. int SB_INIT()
  188. {
  189.     AL = 1;
  190.     DX = SbIOaddr + DSP_RESET;
  191.     $ OUT DX,AL
  192.     $ IN AL,DX
  193.     $ IN AL,DX
  194.     $ IN AL,DX
  195.     $ IN AL,DX
  196.     $ XOR AL,AL
  197.     $ OUT DX,AL
  198.     DX += 4;
  199.     CX = 100;
  200.     loop(CX)
  201.     {
  202.        $ IN AL,DX
  203.        $ CMP AL,0xAA
  204.        $ JE DONE
  205.     }
  206.     AX = -1;
  207.     $ RET            // return();
  208. DONE:
  209.     $ XOR AX,AX
  210. }
  211.  
  212. /*
  213.    Sets the sample rate to be used for digitising or playback.
  214.  
  215.    Usage: SB_SAMPLE_RATE(,word rate);
  216. */
  217. void SB_SAMPLE_RATE()
  218. {
  219.     AX = 0x4240;
  220.     DX = 0x000F;   // DX:AX == 0xF4240 == 1,000,000
  221.     $ DIV BX
  222.     BX = 256;
  223.     $ SUB BX,AX    // tc = (unsigned char) (256 - (1,000,000/rate));
  224.  
  225.     WRITEDSP(TIME_CONSTANT);  /* Command byte for sample rate */
  226.     WRITEDSP(BL);    /* Sample rate time constant */
  227. }
  228.  
  229. /*
  230.    Turns the speaker on or off.
  231.  
  232.    Usage: SB_VOICE(state);
  233. */
  234. void SB_VOICE()
  235. {
  236.     IF (AX == FALSE)
  237.        WRITEDSP(SPEAKER_OFF);
  238.     ELSE
  239.        WRITEDSP(SPEAKER_ON);
  240. }
  241.  
  242. /* Gets the DSP version number from the sound card.
  243.    Returns:
  244.       AH = high byte
  245.       AL = low byte
  246. */
  247. word GET_DSP_VER()
  248. {
  249.    WRITEDSP(DSP_VER);
  250.    WAIT(3);
  251. REGET:
  252.    READDSP(,,100);       // get the high byte
  253.    IF (AL == 0xAA)
  254.       $ JMP REGET
  255.    $ MOV AH,AL
  256.    READDSP(,,100);       // get the low byte
  257.    return(AX);
  258. }
  259.  
  260.  
  261.  
  262. /*
  263.    Parses the BLASTER enviroment variable to get SB settings.
  264.  
  265.    Returns 1 on error and 0 on success.
  266. */
  267. int SB_GET_PARAMS()
  268. {
  269.    /* Set arguments to reasonable values (Sound Blaster defaults) */
  270.    SbIOaddr = 0x220;
  271.    SbIRQ = 7;
  272.    SbDMAchan = 1;
  273.  
  274.    /* Attempt to read environment variable */
  275.    ENVSTR("BLASTER");
  276.    SI = DI;
  277.  
  278.    /* Is the environment variable set? */
  279.    IF(ESBYTE[SI] == 0)
  280.       return(1);
  281.  
  282.    /* Now parse the BLASTER variable */
  283.    do {
  284. NEXT_CHAR:     // loop to bypass "white-space"
  285.       AL = ESBYTE[SI];
  286.       IF (AL == ' ')
  287.          SI++;
  288.       ELSE IF (AL == '\t')
  289.          SI++;
  290.       ELSE
  291.          $ JMP CONTINUE
  292.       $ JMP NEXT_CHAR
  293. CONTINUE:
  294.       IF (AL >= 'a')
  295.          IF (AL <= 'z')
  296.             AL -= 'a' + 'A';   // capitalize character in AL
  297.       IF (AL == 'A')                /* I/O address */
  298.       {
  299.          SI++;
  300.          DIGITSTOHEXWORD();
  301.          SbIOaddr = AX;
  302.       }
  303.       ELSE IF (AL == 'I')           /* Hardware IRQ */
  304.       {
  305.          SI++;
  306.          DIGITSTOWORD();
  307.          SbIRQ = AX;
  308.       }
  309.       ELSE IF (AL == 'D')           /* DMA channel */
  310.       {
  311.          SI++;
  312.          DIGITSTOWORD();
  313.          SbDMAchan = AX;
  314.       }
  315.       ELSE IF (AL == 'T')           /* Sound Blaster type */
  316.       {
  317.          SI++;
  318.          DIGITSTOWORD();
  319.          SbType = AX;
  320.       }
  321.       ELSE
  322.       {
  323.          WRITESTR("Unknown BLASTER option ");
  324.          WRITE(AL);
  325.          WRITELN();
  326.       }
  327.    } while (ESBYTE[SI] != 0);  // are we at the end of the string?
  328.    return(0);
  329. }
  330.  
  331.  
  332.  
  333. /*
  334.     The following are procedures required by SB_GET_PARAMS.
  335. */
  336.  
  337. /*
  338.    On entry:  ES:SI = segment:offset of string
  339.    On return: word in AX
  340. */
  341. word DIGITSTOWORD()
  342. {
  343.    $ XOR AX,AX;
  344.    CX = 10;
  345.    $ XOR BH,BH
  346. NEXT_CHAR:
  347.    BL = ESBYTE[SI];
  348.    IF (BL < '0')
  349.       $ JMP DONE
  350.    IF (BL > '9')
  351.       $ JMP DONE
  352.    $ MUL CX
  353.    BL -= '0';
  354.    AX += BX;
  355.    SI++;
  356.    $ JMP NEXT_CHAR
  357. DONE:
  358. }
  359.  
  360. /*
  361.    On entry:  ES:SI = segment:offset of string
  362.    On return: word in AX
  363. */
  364. word DIGITSTOHEXWORD()
  365. {
  366.    $ XOR AX,AX;
  367.    $ XOR BH,BH
  368. NEXT_CHAR:
  369.    BL = ESBYTE[SI];
  370.    IF (BL < '0')
  371.       $ JMP DONE
  372.    IF (BL > '9')
  373.       $ JMP DONE
  374.    $ SHL AX,4     // == AX *= 16;
  375.    BL -= '0';
  376.    AX += BX;
  377.    SI++;
  378.    $ JMP NEXT_CHAR
  379. DONE:
  380. }
  381.