------------------------------------------------------------------------------- - SECTION TWO PART II - (Programming the SB card) ----------------------------- ------------------------------------------------------------------------------- On the comp.lang.basic.misc and alt.lang.basic newsgroups there are regularly questions like ' How do program a SB from Basic? ' or 'How I detect an SB port address' or 'WAV FILES: playing through SB?' etc. This tute should address many of the problems faced by BASIC programmers. PROGRAMMING THE SB ================== Programming the SB is quite simple but the timing is the hard bit so I'm going into that in this tutorial. I'll just show you how to reset the card , turn the speaker on and produce a square-wave tone. The SB is controlled by a chip called the DSP (this is the Digital Sound Processor) and this is what we use in this tutorial. Before we can send raw data for the card to sound we need to reset the card. ' START OF RESET SECTION OUT &H226, 1: OUT &H226, 0 ' steps 1+2 DO x% = INP(&H22E) ' step 3 IF x% AND 128 THEN ' step 4 x% = INP(&H22A) ' step 5 IF x% = &HAA THEN ' step 6 PRINT "reset!" EXIT DO END IF END IF LOOP That bit of program will reset the card. Here is what it does: 1 - Send a 1 to the reset port (2x6h) 1a - wait 3 microseconds. basic is so slow that i havn't had to bother 2 - Send a 0 to the reset port (2x6h) 3 - Poll the data ready port (2xEh) 4 - If the 7th bit is not set then goto 3 5 - Poll the data port (2xAh) 6 - If it returns h0AA then you're done otherwise go back to step 3 You should do this up to 100 times. If it doesn't work by then then the port address is wrong or the card is faulty. (NOTE: where I put &H226 or &H22E is for a 220 based card. For a 240 they would be &H246 or &H24E OK?) OK. So now we've reset the thing but we still cannot send data because you won't hear it! The speaker has not been turned on. (The first time I programmed the SB, I didn't turn it on and I had to turn the speakers full up to hear anything. I went mad to find the solution!) The way we turn it on is to program the DSP with the hex byte D1. A sample is shown below. ' TURN SPEAKER ON DO x% = INP(&H22C) LOOP WHILE x% AND 128 OUT &H22C, &HD1 ' send speaker on code Before the byte can be sent we have to check if the SB is ready to get it. That's what the DO...LOOP does. So now the SBs speaker is on. Now we can send raw data to the card. But before each byte is sent we need to send 10 hex to tell the SB it is sound data we are doing. DO FOR g% = 100 TO 1 STEP -1 ' START OF LOOP OUT &H22C, &H10 ' send OUT &H22C, f% ' send no. FOR aa% = 1 TO 100: NEXT aa% ' delay IF f% = 255 THEN f% = 0 ELSE f% = 255 ' alternate between 0 and 255 NEXT g% ' END OF LOOP LOOP The f% is the value that is the sound on the sound wave. BTW, if you want to play WAV,VOC or RAW files what you need to do is read a byte from the file each time and send it to the card after sending the &H10. More about that, in the next fanzine. (but you might work it out from I just said ,by then) Here's the full program that produces a tone by alternating from 0 to 255 and back. Change the aa% loop for a different pitched note. ' START OF RESET SECTION OUT &H226, 1: OUT &H226, 0 DO x% = INP(&H22E) IF x% AND 128 THEN x% = INP(&H22A) IF x% = &HAA THEN PRINT "reset!" EXIT DO END IF END IF LOOP ' END OF RESET SECTION ' TURN SPEAKER ON DO x% = INP(&H22C) LOOP WHILE x% AND 128 OUT &H22C, &HD1 ' send speaker on code DO FOR g% = 100 TO 1 STEP -1 ' START OF LOOP OUT &H22C, &H10 ' send OUT &H22C, f% ' send no. FOR aa% = 1 TO 100: NEXT aa% ' delay IF f% = 255 THEN f% = 0 ELSE f% = 255 ' alternate between 0 and 255 NEXT g% ' END OF LOOP LOOP Hope that's useful. -------------------------------------------------------- * EDITOR'S NOTE: * This article was originally printed in Peter Cooper's BASIX Fanzine, * Issue #1 from November 1995. This tutorial was written by Peter Cooper, * and appeared in Section Two, Part II of that issue.