by R. T. Cunningham (wanderer_rtc@usa.pipeline.com)
Although BASIC is not an "advanced" language, such as Assembly or C, it's been used far more often than any other since the first Commodore computer was introduced. It is not my intent to provide information here that is widely available in user manuals, newsletters, or other forums. What I am going to present is what I have learned from "The School of Hard Knocks" or, in other words, by trial and error.
If your program attempts to access a device number that is not connected to computer in the serial chain, like fetching a directory, your program will halt with "?device not present error in (line number)". How do we prevent this from happening? The answer is to open the device number, close the device number, and check the status variable:
open15,10,15:close15:if st <> 0 then (act on device not being present)
Any number returned by the status variable other than 0 will indicate that the device is either not connected or not turned on.
In my own programs, I like to do this check for all of the drives early in the program by stashing the device numbers in an array. Legal device numbers for disk drives are 8 to 30, with 30 being used as configuration mode for a CMD drive. Since I know of no other drive that can use device #30, I won't include that device number in the loop. The array should be dimensioned to at least 21. I prefer 22 and to use the first variable (0) to hold the number of devices. I keep the "start up" device number in a non-array variable so that I can return to it after the drive checking routine has been completed:
10 dimdv(22):dv(0)=0:rem set number of devices to 0 20 dv=peek(186):rem current device # of last drive accessed 30 ifdv<8thendv=8:rem prevents non-disk device #s 40 fora=1to22:rem device #s 8-29 minus 7 so that array starts at 1 50 open15,a+7,15:close15 60 ifst<>thendv(0)=dv(0)+1:dv(dv(0))=a+7:rem increment number of devices and store device numbers 70 next
If I have device number 8, 10, 12 and 14 connected, dv(0) will now contain 4, dv(1) through dv(4) will contain those device numbers. You can now use this array to check for a valid device number prior to an access command:
100 rem d equals the device number selected in this example 110 fl=0:rem set flag to 0 120 fora=1todv(0):rem check total number of drives attached 130 ifd=(dv(a))thenfl=1:rem set flag to 1 if a match is found 140 next 150 rem continue with drive access only if flag is set to 1
When acting on a number the ON/GOTO command set works by either using a numeric variable or the value of a string, if the string contains a number:
10 on a goto100,200,300,400or
10 on val(a$)goto100,200,300,400
What if we want to use a non-numeric character with ON/GOTO, especially with a list of choices presented to the user?
In the C128's native mode the INSTR function can be used:
10 rem keys accessible by user are A,B,C,D and stored in a$ response 20 on instr("ABCD",a$)goto100,200,300,400or
20 a=instr("ABCD",a$):on a goto100,200,300,400
On the 64, since INSTR is not an available function, we can emulate the function with a different routine:
20 on-1*(a$="A")-2*(a$="B")-3*(a$="C")-3*(a$="D")goto100,200,300,400or
20 a=-1*(a$="A")-2*(a$="B")-3*(a$="C")-3*(a$="D"):onagoto100,200,300,400
Note that the first 1* is not necessary but the - is. I've added it for clarity only.
In the first example, INSTR returns the place number (sequence) of each character in the string. In the second example, the place number is obtained by multiplying the negative sequence number by the signed value of a$. The signed value of a string is always -1 (I think!).
When working with a program that is designed to work in both 64 and 128 modes, the INSTR function should not be used since the other method will also work in 128 mode.
More proficient readers might want to demonstrate the machine language equivalents of both drive detection and INSTR routines? I for one would like to add them to my ML arsenal.
C= Hacking Home | Issue 14 Contents
Copyright © 1992 - 1997 Commodore Hacking Commodore, CBM, its respective computer system names, and the CBM logo are either registered trademarks or trademarks of ESCOM GmbH or VISCorp in the United States and/or other countries. Neither ESCOM nor VISCorp endorse or are affiliated with Commodore Hacking. Commodore Hacking is published by:
10710 Bruhn Avenue Bennington, NE 68007
Last Updated: 1997-03-31 by Jim
Brain