home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / FAQSYS18.ZIP / FAQS.DAT / ADLIB_SB.TXT < prev    next >
Text File  |  1995-12-12  |  21KB  |  485 lines

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