             How to manipulate your internal PC speaker in ASM
                              - By MetGod -


        I wanted to write this short "tutorial" as when I went to find a
document on manipulating the speaker, I failed miserably. The more I program
in ASM, the more I learn. Finally I came to learning this. Originally I got
the idea from the C++ function sound(). This "technique" is definetly not
new, as it's been used before in demos, etc. However, the people I asked,
had never fooled around with it, and just new you had to use direct
manipulation from the speaker ports..

Table of Contents (sort of)

        1. Theory
        2. Turning on the speaker
        3. Sending a certain tone in Hertz.
        4. Turning off speaker
        5. Possible uses.
        6. An Example program. (Read keysound.txt for more info)

[Theory]

        The theory is fairly simple. We use the PPI (Programmable Peripheral 
Interface), and the Programmable Timer. With the PPI, we use port B (61h) to
set permission of the speaker. We need to turn it on, obviously.  We do a
little more with the Programmable timer, however. The Programmable Timer
chip has 3 channels. Port 0,1,2 respectively. I will not be discussing
channel 0 or 1, only 2. Channel 2 can be accessed at port 2 (42h).
        Also note, the Command register can be accessed at port 43h.
Port 42h is connected to the pc speaker. It issues square-wave pulses 
which are used to make sounds. This is how you change the frequency.
The command register, sets the necessary mode to send frequencies
to the 2 channel (42h).  Note that the PPI is based on a frequency of
1.193Mhz. This fact will be important to you by the end of this short
article.




[Turning The Speaker On]

Ok, so now that you hopefully understand the theory, lets begin
with the code.. So, How do you turn on the speaker in ASM?

3 lines of code will do this..

        in al,61h               ;read in port 61h
        or al,03h               ;modify necessary bits
        out 61h,al              ;put the modified al back into port

Next, we set the necessary mode in the control register.

        mov al,0b6h             ;modify necessary bits
        out 43h,al              ;and send it to port 43h

[Sending a Certain Tone]

How do I send the speaker a certain tone?

Now the speaker is ready for what you want to do.
The idea is to put the frequency in AX, and send the data to port 42h
Make sure to put both AL and AH in the port like so:

                                ;this is where the 1.193 value comes in
                                ;.. To get the value of the frequency you want, you
                                ;must divide the value that you want into 1.193
        mov ax,2345             
        out 42h,al              ;put lower bit into 42h
        mov al,ah               ;put higher bit into al
        out 42h,al              ;put higher bit into 42h

The speaker will now be emitting a tone.

Note that lower numbers have a higher frequency.
E.x: 100 is a higher pitch than 1000, and 1000 is higher pitched then
2400, etc.

[Turning Off The PC Speaker]

How do I turn off the speaker?
We once again use the PPI to change the mode of the speaker.

        in al,61h               ;read in from port B (61h) again
        and al,0fdh             ;clear necessary bits
        out 61h,al              ;put back into al, and speaker is off

[Possible uses]

I've had a lot of fun messing with the speaker.
You can incorporate the keyboard with sound.
For instance, make a program that waits for input.
Each key has a different tone that it plays until another
key is pressed. (ESC could turn it off or exit, etc)

The speaker can be very loud too (and annoying).
The possibilities are endless.

I included a program called keysound.
This program clears the screen, and then waits for a keypress.
The alphabetic keys each have a different tone. On each press,
the frequency will change.  Also note, that the letter 'A' and 'a'
are different and have different frequencies
The arrow keys have an effect, as well as the numbers below the F keys..
The F1-F10 keys also have an effect, and so does TAB, Insert, Delete,Home,Pg Up,
Pg Dn, and End..
The Number pad numbers will also have an effect if NUM lock is OFF..
Space bar temporarily turns off speaker (read keysound.txt for more info)
Read keysound.txt for more info... To understand how to exit program,
etc...
I hope this article has helped you in programming sound.


-------------------------------------------------------------------------
MetGod / HFX
E-Mail          lghtspd@pacbell.net  metgod@hfactorx.org
UnderNET IRC    #aos #hfx #vir
"My Terminal is my Soul"
-------------------------------------------------------------------------

Almost everything was explained above
However, a few things were not, about keysound.com
Firstly, to exit the program, hit ESC.
To turn off speaker, press spacebar (sound will be on after another key is
pressed).. When program exits, sound will turn off too.

About the .asm file. I thought about including it, but I decided not too.
If you would really like to see it, email me.
I will probably include it in TI#2 if I get requests for the source.

