home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / doc / sblaster / sbprog.txt
Text File  |  1992-07-12  |  22KB  |  482 lines

  1.  
  2. -------------------------------------------------------------------------------
  3.  
  4.                       Programming the AdLib/Sound Blaster
  5.                                 FM Music Chips
  6.                            Version 2.0 (24 Feb 1992)
  7.  
  8.                   Copyright (c) 1991, 1992 by Jeffrey S. Lee
  9.  
  10.                                jlee@smylex.uucp
  11.  
  12.  
  13.  
  14.                         Warranty and Copyright Policy
  15.  
  16.      This document is provided on an "as-is" basis, and its author makes
  17.      no warranty or representation, express or implied, with respect to
  18.      its quality performance or fitness for a particular purpose.  In no
  19.      event will the author of this document be liable for direct, indirect,
  20.      special, incidental, or consequential damages arising out of the use
  21.      or inability to use the information contained within.  Use of this
  22.      document is at your own risk.
  23.  
  24.      This file may be used and copied freely so long as the applicable
  25.      copyright notices are retained, and no modifications are made to the
  26.      text of the document.  No money shall be charged for its distribution
  27.      beyond reasonable shipping, handling and duplication costs, nor shall
  28.      proprietary changes be made to this document so that it cannot be
  29.      distributed freely.  This document may not be included in published
  30.      material or commercial packages without the written consent of its
  31.      author.
  32.  
  33.  
  34.  
  35.                                    Overview
  36.  
  37.      Two of the most popular sound cards for the IBM-PC, the AdLib and the
  38.      Sound Blaster, suffer from a real dearth of clear documentation for 
  39.      programmers.  AdLib Inc. and Creative Labs, Inc. both sell developers'
  40.      kits for their sound cards, but these are expensive, and (in the case
  41.      of the Sound Blaster developers' kit) can be extremely cryptic.
  42.  
  43.      This document is intended to provide programmers with a FREE source
  44.      of information about the programming of these sound cards.
  45.  
  46.      The information contained in this document is a combination of 
  47.      information found in the Sound Blaster Software Developer's Kit, and
  48.      that learned by painful experience.  Some of the information may not
  49.      be valid for AdLib cards; if this is so, I apologize in advance.
  50.  
  51.      Please note that numbers will be given in hexadecimal, unless otherwise
  52.      indicated.  If a number is written out longhand (sixteen instead of 16)
  53.      it is in decimal.
  54.  
  55.  |   Changes from Version 1 of the file will be indicated by the use of change
  56.  |   bars in the left-hand margin.
  57.  
  58.  
  59.  
  60.                          Chapter One - Sound Card I/O
  61.  
  62.      The sound card is programmed by sending data to its internal registers
  63.      via its two I/O ports:
  64.  
  65.              0388 (hex) - Address/Status port  (R/W)
  66.              0389 (hex) - Data port            (W/O)
  67.  
  68.  |   The Sound Blaster Pro is capable of stereo FM music, which is accessed
  69.  |   in exactly the same manner.  Ports 0220 and 0221 (hex) are the address/
  70.  |   data ports for the left speaker, and ports 0222 and 0223 (hex) are the
  71.  |   ports for the right speaker.  Ports 0388 and 0389 (hex) will cause both
  72.  |   speakers to output sound.
  73.  
  74.      The sound card possesses an array of two hundred forty-four registers;
  75.      to write to a particular register, send the register number (01-F5) to
  76.      the address port, and the desired value to the data port.
  77.  
  78.      After writing to the register port, you must wait twelve cycles before 
  79.      sending the data; after writing the data, eighty-four cycles must elapse
  80.      before any other sound card operation may be performed.
  81.  
  82.  |   The AdLib manual gives the wait times in microseconds: three point three
  83.  |   (3.3) microseconds for the address, and twenty-three (23) microseconds
  84.  |   for the data.
  85.  |
  86.  |   The most accurate method of producing the delay is to read the register
  87.  |   port six times after writing to the register port, and read the register
  88.  |   port thirty-five times after writing to the data port.
  89.  
  90.      The sound card registers are write-only.
  91.  
  92.      The address port also functions as a sound card status byte.  To 
  93.      retrieve the sound card's status, simply read port 388.  The status 
  94.      byte has the following structure:
  95.  
  96.               7      6      5      4      3      2      1      0
  97.           +------+------+------+------+------+------+------+------+
  98.           | both | tmr  | tmr  |              unused              |
  99.           | tmrs |  1   |  2   |                                  |
  100.           +------+------+------+------+------+------+------+------+
  101.  
  102.           Bit 7 - set if either timer has expired.
  103.               6 - set if timer 1 has expired.
  104.               5 - set if timer 2 has expired.
  105.  
  106.  
  107.  
  108.                        Chapter Two - The Registers
  109.  
  110. The following table shows the function of each register in the sound 
  111. card.  Registers will be explained in detail after the table.  Registers
  112. not listed are unused.
  113.  
  114.    Address      Function
  115.    -------      ----------------------------------------------------
  116.      01         Test LSI / Enable waveform control
  117.      02         Timer 1 data
  118.      03         Timer 2 data
  119.      04         Timer control flags
  120.      08         Speech synthesis mode / Keyboard split note select
  121.    20..35       Amp Mod / Vibrato / EG type / Key Scaling / Multiple
  122.    40..55       Key scaling level / Operator output level
  123.    60..75       Attack Rate / Decay Rate
  124.    80..95       Sustain Level / Release Rate
  125.    A0..A8       Frequency (low 8 bits)
  126.    B0..B8       Key On / Octave / Frequency (high 2 bits)
  127.      BD         AM depth / Vibrato depth / Rhythm control
  128.    C0..C8       Feedback strength / Connection type
  129.    E0..F5       Wave Select
  130.  
  131. The groupings of twenty-two registers (20-35, 40-55, etc.) have an odd
  132. order due to the use of two operators for each FM voice.  The following
  133. table shows the offsets within each group of registers for each operator.
  134.  
  135.  
  136.    Channel        1   2   3   4   5   6   7   8   9
  137.    Operator 1    00  01  02  08  09  0A  10  11  12
  138.    Operator 2    03  04  05  0B  0C  0D  13  14  15
  139.  
  140. Thus, the addresses of the attack/decay bytes for channel 3 are 62 for
  141. the first operator, and 65 for the second.  (The address of the second
  142. operator is always the address of the first operator plus three).
  143.  
  144. To further illustrate the relationship, the addresses needed to control
  145. channel 5 are:
  146.  
  147.     29 - Operator 1  AM/VIB/EG/KSR/Multiplier
  148.     2C - Operator 2  AM/VIB/EG/KSR/Multiplier
  149.     49 - Operator 1  KSL/Output Level
  150.     4C - Operator 2  KSL/Output Level
  151.     69 - Operator 1  Attack/Decay
  152.     6C - Operator 2  Attack/Decay
  153.     89 - Operator 1  Sustain/Release
  154.     8C - Operator 2  Sustain/Release
  155.     A4 -             Frequency (low 8 bits)
  156.     B4 -             Key On/Octave/Frequency (high 2 bits)
  157.     C4 -             Feedback/Connection Type
  158.     E9 - Operator 1  Waveform
  159.     EC - Operator 2  Waveform
  160.  
  161.  
  162.  
  163.                        Explanations of Registers
  164.  
  165. Byte 01 - This byte is normally used to test the LSI device.  All bits
  166.           should normally be zero.  Bit 5, if enabled, allows the FM 
  167.           chips to control the waveform of each operator.
  168.  
  169.              7     6     5     4     3     2     1     0
  170.           +-----+-----+-----+-----+-----+-----+-----+-----+
  171.           |   unused  | WS  |            unused           |
  172.           +-----+-----+-----+-----+-----+-----+-----+-----+
  173.  
  174.  
  175. Byte 02 - Timer 1 Data.  If Timer 1 is enabled, the value in this 
  176.           register will be incremented until it overflows.  Upon
  177.           overflow, the sound card will signal a TIMER interrupt
  178.           (INT 08) and set bits 7 and 6 in its status byte.  The
  179.           value for this timer is incremented every eighty (80)
  180.           microseconds.
  181.  
  182.  
  183. Byte 03 - Timer 2 Data.  If Timer 2 is enabled, the value in this 
  184.           register will be incremented until it overflows.  Upon
  185.           overflow, the sound card will signal a TIMER interrupt
  186.           (INT 08) and set bits 7 and 5 in its status byte.  The
  187.           value for this timer is incremented every three hundred
  188.           twenty (320) microseconds.
  189.  
  190.  
  191. Byte 04 - Timer Control Byte
  192.  
  193.         7     6     5     4     3     2     1     0
  194.      +-----+-----+-----+-----+-----+-----+-----+-----+
  195.      | IRQ | T1  | T2  |     unused      | T2  | T1  |
  196.      | RST | MSK | MSK |                 | CTL | CTL |
  197.      +-----+-----+-----+-----+-----+-----+-----+-----+
  198.  
  199.           bit 7 - Resets the flags for timers 1 & 2.  If set,
  200.                   all other bits are ignored.
  201.           bit 6 - Masks Timer 1.  If set, bit 0 is ignored.
  202.           bit 5 - Masks Timer 2.  If set, bit 1 is ignored.
  203.           bit 1 - When clear, Timer 2 does not operate.
  204.                   When set, the value from byte 03 is loaded into
  205.                   Timer 2, and incrementation begins.
  206.           bit 0 - When clear, Timer 1 does not operate.
  207.                   When set, the value from byte 02 is loaded into
  208.                   Timer 1, and incrementation begins.
  209.  
  210.  
  211. Byte 08 - CSM Mode / Keyboard Split.
  212.  
  213.         7     6     5     4     3     2     1     0
  214.      +-----+-----+-----+-----+-----+-----+-----+-----+
  215.      | CSM | Key |              unused               |
  216.      | sel | Spl |                                   |
  217.      +-----+-----+-----+-----+-----+-----+-----+-----+
  218.      
  219.           bit 7 - When set, selects composite sine-wave speech synthesis
  220.                   mode (all KEY-ON bits must be clear).  When clear,
  221.                   selects FM music mode.
  222.  
  223.           bit 6 - Selects the keyboard split point (in conjunction with
  224.                   the F-Number data).  The documentation in the Sound 
  225.                   Blaster manual is utterly incomprehensible on this;
  226.                   I can't reproduce it without violating their copyright.
  227.  
  228.  
  229. Bytes 20-35 - Amplitude Modulation / Vibrato / Envelope Generator Type /
  230.               Keyboard Scaling Rate / Modulator Frequency Multiple
  231.  
  232.         7     6     5     4     3     2     1     0
  233.      +-----+-----+-----+-----+-----+-----+-----+-----+
  234.      | Amp | Vib | EG  | KSR |  Modulator Frequency  |
  235.      | Mod |     | Typ |     |       Multiple        |
  236.      +-----+-----+-----+-----+-----+-----+-----+-----+
  237.  
  238.           bit 7 - Apply amplitude modulation when set; AM depth is
  239.                   controlled by the AM-Depth flag in address BD.
  240.           bit 6 - Apply vibrato when set;  vibrato depth is controlled
  241.                   by the Vib-Depth flag in address BD.
  242.           bit 5 - When set, the sustain level of the voice is maintained
  243.                   until released; when clear, the sound begins to decay
  244.                   immediately after hitting the SUSTAIN phase.
  245.           bit 4 - Keyboard scaling rate.  This is another incomprehensible
  246.                   bit in the Sound Blaster manual.  From experience, if 
  247.                   this bit is set, the sound's envelope is foreshortened as
  248.                   it rises in pitch.
  249.           bits 3-0 - These bits indicate which harmonic the operator will 
  250.                   produce sound (or modulation) in relation to the voice's 
  251.                   specified frequency:
  252.  
  253.                       0 - one octave below
  254.                       1 - at the voice's specified frequency
  255.                       2 - one octave above
  256.                       3 - an octave and a fifth above
  257.                       4 - two octaves above
  258.                       5 - two octaves and a major third above
  259.                       6 - two octaves and a fifth above
  260.                       7 - two octaves and a minor seventh above
  261.                       8 - three octaves above
  262.                       9 - three octaves and a major second above
  263.                       A - three octaves and a major third above
  264.                       B -  "       "     "  "   "     "     "
  265.                       C - three octaves and a fifth above
  266.                       D -   "      "     "  "   "     "
  267.                       E - three octaves and a major seventh above
  268.                       F -   "      "     "  "   "      "      "
  269.                   
  270.  
  271. Bytes 40-55 - Level Key Scaling / Total Level
  272.  
  273.         7     6     5     4     3     2     1     0
  274.      +-----+-----+-----+-----+-----+-----+-----+-----+
  275.      |  Scaling  |             Total Level           |
  276.      |   Level   | 24    12     6     3    1.5   .75 | <-- dB
  277.      +-----+-----+-----+-----+-----+-----+-----+-----+
  278.  
  279.           bits 7-6 - causes output levels to decrease as the frequency
  280.                      rises:
  281.  
  282.                           00   -  no change
  283.                           10   -  1.5 dB/8ve
  284.                           01   -  3 dB/8ve
  285.                           11   -  6 dB/8ve
  286.  
  287.           bits 5-0 - controls the total output level of the operator.
  288.                      all bits CLEAR is loudest; all bits SET is the
  289.                      softest.  Don't ask me why.
  290.  
  291.  
  292. Bytes 60-75 - Attack Rate / Decay Rate
  293.  
  294.         7     6     5     4     3     2     1     0
  295.      +-----+-----+-----+-----+-----+-----+-----+-----+
  296.      |         Attack        |          Decay        |
  297.      |          Rate         |          Rate         |
  298.      +-----+-----+-----+-----+-----+-----+-----+-----+
  299.  
  300.           bits 7-4 - Attack rate.  0 is the slowest, F is the fastest.
  301.           bits 3-0 - Decay rate.  0 is the slowest, F is the fastest.
  302.  
  303.  
  304. Bytes 80-95 - Sustain Level / Release Rate
  305.  
  306.         7     6     5     4     3     2     1     0
  307.      +-----+-----+-----+-----+-----+-----+-----+-----+
  308.      |     Sustain Level     |         Release       |
  309.      | 24    12     6     3  |          Rate         |
  310.      +-----+-----+-----+-----+-----+-----+-----+-----+
  311.  
  312.           bits 7-4 - Sustain Level.  0 is the loudest, F is the softest.
  313.           bits 3-0 - Release Rate.  0 is the slowest, F is the fastest.
  314.  
  315.  
  316. Bytes A0-B8 - Octave / F-Number / Key-On
  317.  
  318.         7     6     5     4     3     2     1     0
  319.      +-----+-----+-----+-----+-----+-----+-----+-----+
  320.      |        F-Number (least significant byte)      |  (A0-A8)
  321.      |                                               |
  322.      +-----+-----+-----+-----+-----+-----+-----+-----+
  323.  
  324.         7     6     5     4     3     2     1     0
  325.      +-----+-----+-----+-----+-----+-----+-----+-----+
  326.      |  Unused   | Key |    Octave       | F-Number  |  (B0-B8)
  327.      |           | On  |                 | most sig. |
  328.      +-----+-----+-----+-----+-----+-----+-----+-----+
  329.  
  330.           bit   5  - Channel is voiced when set, silent when clear.
  331.           bits 4-2 - Octave (0-7).  0 is lowest, 7 is highest.
  332.           bits 1-0 - Most significant bits of F-number.
  333.  
  334.      In octave 4, the F-number values for the chromatic scale and their 
  335.      corresponding frequencies would be:
  336.  
  337.         F Number     Frequency     Note
  338.            16B          277.2       C#
  339.            181          293.7       D
  340.            198          311.1       D#
  341.            1B0          329.6       E
  342.            1CA          349.2       F
  343.            1E5          370.0       F#
  344.            202          392.0       G
  345.            220          415.3       G#
  346.            241          440.0       A
  347.            263          466.2       A#
  348.            287          493.9       B
  349.            2AE          523.3       C
  350.  
  351.  
  352. Bytes C0-C8 - Feedback / Algorithm
  353.  
  354.         7     6     5     4     3     2     1     0
  355.      +-----+-----+-----+-----+-----+-----+-----+-----+
  356.      |         unused        |    Feedback     | Alg |
  357.      |                       |                 |     |
  358.      +-----+-----+-----+-----+-----+-----+-----+-----+
  359.  
  360.           bits 3-1 - Feedback strength.  If all three bits are set to
  361.                      zero, no feedback is present.  With values 1-7,
  362.                      operator 1 will send a portion of its output back
  363.                      into itself.  1 is the least amount of feedback,
  364.                      7 is the most.
  365.           bit 0    - If set to 0, operator 1 modulates operator 2.  In this
  366.                      case, operator 2 is the only one producing sound.
  367.                      If set to 1, both operators produce sound directly.
  368.                      Complex sounds are more easily created if the algorithm
  369.                      is set to 0.
  370.  
  371.  
  372. Byte BD - Amplitude Modulation Depth / Vibrato Depth / Rhythm
  373.  
  374.         7     6     5     4     3     2     1     0
  375.      +-----+-----+-----+-----+-----+-----+-----+-----+
  376.      | AM  | Vib | Rhy | BD  | SD  | TOM | Top | HH  |
  377.      | Dep | Dep | Ena |     |     |     | Cym |     |
  378.      +-----+-----+-----+-----+-----+-----+-----+-----+
  379.  
  380.           bit 7 - Set:    AM depth is 4.8dB
  381.                   Clear:  AM depth is 1 dB
  382.           bit 6 - Set:    Vibrato depth is 14 cent
  383.                   Clear:  Vibrato depth is 7 cent
  384.           bit 5 - Set:    Rhythm enabled  (6 melodic voices) 
  385.                   Clear:  Rhythm disabled (9 melodic voices)
  386.           bit 4 - Bass drum on/off
  387.           bit 3 - Snare drum on/off
  388.           bit 2 - Tom tom on/off
  389.           bit 1 - Cymbal on/off
  390.           bit 0 - Hi Hat on/off
  391.  
  392.           Note:  KEY-ON registers for channels 06, 07, and 08 must be OFF
  393.                  in order to use the rhythm section.  Other parameters
  394.                  such as attack/decay/sustain/release must also be set
  395.                  appropriately.
  396.  
  397.  
  398. Bytes E0-F5 - Waveform Select
  399.  
  400.         7     6     5     4     3     2     1     0
  401.      +-----+-----+-----+-----+-----+-----+-----+-----+
  402.      |               unused              |  Waveform |
  403.      |                                   |  Select   |
  404.      +-----+-----+-----+-----+-----+-----+-----+-----+
  405.  
  406.           bits 1-0 - When bit 5 of address 01 is set, the output waveform
  407.                      will be distorted according to the waveform indicated
  408.                      by these two bits.  I'll try to diagram them here,
  409.                      but this medium is fairly restrictive.
  410.  
  411.          ___              ___            ___    ___       _      _
  412.         /   \            /   \          /   \  /   \     / |    / |
  413.        /_____\_______   /_____\_____   /_____\/_____\   /__|___/__|___
  414.               \     /
  415.                \___/
  416.  
  417.             00              01               10               11
  418.  
  419.  
  420.  
  421.  |                          Detecting a Sound Card
  422.  |
  423.  |   According to the AdLib manual, the 'official' method of checking for a 
  424.  |   sound card is as follows:
  425.  |
  426.  |      1)  Reset both timers by writing 60h to register 4.
  427.  |      2)  Enable the interrupts by writing 80h to register 4.  NOTE: this
  428.  |          must be a separate step from number 1.
  429.  |      3)  Read the status register (port 388h).  Store the result.
  430.  |      4)  Write FFh to register 2 (Timer 1).
  431.  |      5)  Start timer 1 by writing 21h to register 4.
  432.  |      6)  Delay for at least 80 microseconds.
  433.  |      7)  Read the status register (port 388h).  Store the result.
  434.  |      8)  Reset both timers and interrupts (see steps 1 and 2).
  435.  |      9)  Test the stored results of steps 3 and 7 by ANDing them
  436.  |          with E0h.  The result of step 3 should be 00h, and the 
  437.  |          result of step 7 should be C0h.  If both are correct, an
  438.  |          AdLib-compatible board is installed in the computer.
  439.  |
  440.  |
  441.  |                              Making a Sound
  442.  | 
  443.  |   Many people have asked me, upon reading this document, what the proper
  444.  |   register values should be to make a simple sound.  Well, here they are.
  445.  | 
  446.  |   First, clear out all of the registers by setting all of them to zero.
  447.  |   This is the quick-and-dirty method of resetting the sound card, but it
  448.  |   works.  Note that if you wish to use different waveforms, you must then
  449.  |   turn on bit 5 of register 1.  (This reset need be done only once, at the
  450.  |   start of the program, and optionally when the program exits, just to 
  451.  |   make sure that your program doesn't leave any notes on when it exits.)
  452.  |
  453.  |   Now, set the following registers to the indicated value:
  454.  |
  455.  |     REGISTER     VALUE     DESCRIPTION
  456.  |        20          01      Set the modulator's multiple to 1
  457.  |        40          10      Set the modulator's level to about 40 dB
  458.  |        60          F0      Modulator attack:  quick;   decay:   long
  459.  |        80          77      Modulator sustain: medium;  release: medium
  460.  |        A0          98      Set voice frequency's LSB (it'll be a D#)
  461.  |        23          01      Set the carrier's multiple to 1
  462.  |        43          00      Set the carrier to maximum volume (about 47 dB)
  463.  |        63          F0      Carrier attack:  quick;   decay:   long
  464.  |        83          77      Carrier sustain: medium;  release: medium
  465.  |        B0          31      Turn the voice on; set the octave and freq MSB
  466.  |
  467.  |   To turn the voice off, set register B0h to 11h (or, in fact, any value 
  468.  |   which leaves bit 5 clear).  It's generally preferable, of course, to
  469.  |   induce a delay before doing so.
  470.  |
  471.  |
  472.  |                             Acknowledgements
  473.  |
  474.  |   Thanks are due to the following people:
  475.  |
  476.  |   Ezra M. Dreisbach (ed10+@andrew.cmu.edu), for providing the information
  477.  |     about the recommended port write delay from the AdLib manual, and the
  478.  |     'official' method of detecting an AdLib-compatible sound card.
  479.  |
  480.  |   Nathan Isaac Laredo (gt7080a@prism.gatech.edu), for providing the
  481.  |     port numbers for stereo sound on the Sound Blaster Pro.
  482.