USING INP and OUT
Create a terminal program in ST BASIC
By Alex Golitsis
There are two mysterious commands in ST BASIC that are perhaps the most powerful, but least known. They allow you to print text to your printer, or even communicate with other computers. The commands are INP(x) and OUT x,y.
For communicating with the outside world, nothing beats this pair. INP is short for INPut, and OUT, of course, is short for OUTput. These are the two basic, primary functions that a computer performs. It inputs to gain information, and it outputs to send information.
Chart 1 shows the values that x can be in INP(x) or OUT x,y. You would probably guess from the chart that 4 takes input from your ST's keyboard, right? Wrong. The consol (2) takes care of both screen output and keyboard input. The "keyboard" refers to the virtual keyboard, not the physical one that you type on.
Have you ever wanted a screen to wait for the user to hit a key before continuing? Well, INP is one way to accomplish this. Since you want the user to hit a key on the keyboard, we use assignment number 2 (console). To check the status of the character buffer for any of the devices, use its negative. It will return zero if there is no character, or -1 if there is a character waiting in the buffer.
So to check if someone has hit a key, you would say:
1000 IF INP(-2)<>-1 THEN 1000
This should keep BASIC busy until someone hits a key. Unfortunately though, there is a hitch. GEM's "Desktop" interaction, consisting of the mouse pointer and key buffering, steals almost all of the characters before line 1000 can get to them. All is not lost, though. It is as easy as turning GEM's "Desktop" interaction OFF with the following command:
And to turn it back on, POKE SYSTAB + 24,0
So, we can now use our original line in a subroutine at the end of your program. We can label it "KEYIN". Here's how it looks:
POKE SYSTAB + 24,1
POKE SYSTAB + 24,0
This subroutine can now be called with a GOSUB KEYIN any time you would like the user to hit a key to continue in your program.
Now that you have the basics of INP, I can introduce you to its sister, OUT. OUT does the opposite of what INP just did. Instead of taking in a character, OUT sends out a character to any of the devices in Chart 1. The format is identical, but you cannot check the status through OUT, because it is up the the program to supply the characters this time. Let's send the characters "HI" to your printer.
For those of you who haven't memorized the ASCII values for every character, 72 is decimal for "H" and 73 for "I". 13 is a carriage return, inserted because many printers with buffers won't print until they receive one.
At this point, you may he asking, "Hmn. . .What would happen if I sent a character to the screen through OUT? Will it show up in the OUTPUT window?" Hmm indeed. Let's try it.
(Notice we don't need the carriage return this time.)
What happened? Instead of looking through all of the windows for "HI", focus your attention to the top left corner After Debug on the menu bar, the letters HI have appeared!
By now, some of the implications of these command might be running through your head. "If I can get two characters on the screen, I must be able to fill the whole screen with my own text". You can, if you do it carefully.
One of the assignments we haven't used yet is number one, the RS-232 port. This is where your modem plugs in. If a little lightbulb just lit up above your head, you're not alone. Now that we can control incoming and outgoing text to the modem, who is going to stop us from writing an ST BASIC terminal program? Or even a Bulletin Board System (BBS)? The answer is no one.
Before we go on, first realize that the all-text part of the ST's operating system uses the VT52 standard terminal. If you have studied the table of escape sequences for the VT52 that have been published in some books, you might have noticed some very exciting features. But if you recall, the screen isn't cleared by an ASCII 12 like most terminals, but rather an [ESCAPE] (ASCII 27) followed by the capital letter E (ASCII 69). Since OUT 2 utilizes VT52 codes, sending these characters through OUT 2 should clear the whole ST BASIC screen. If you try it, you will definitely agree that it does. Windows, drop-down menus and even the grey background have all disappeared. But if you have just entered these lines in COMMAND mode, you will notice that moving the mouse, hitting keys, and dropping menus seems to have a rebuilding effect. You cannot, unfortunately, have the ST BASIC Desktop back in perfect condition without re-booting. You will also notice that the characters you now type are not appearing at the top of the screen with our "HI", but rather back in the COMMAND window.
With this knowledge, we should be able to make a short program that echoes every character you type to the blanked-out screen (and not the OUTPUT or COMMAND windows). The following program will do just that:
1000 ON ERROR GOTO 5000
1010 POKE SYSTAB + 24,1
1020 OUT 2,27:OUT 2,69
1030 CHAR.IN = 0
1040 WHILE CHAR.IN<>13
1060 WHILE A<>-l
1090 CHAR.IN = INP(2)
1100 OUT 2,CHAR.IN
5000 POKE SYSTAB + 24,0
What you type will be shown on the first line of the screen (normally the menu bar) and will continue along until you hit [RETURN].
The ON ERROR GOTO 5000 is included just in case you make a typing error. All errors are fatal unless you trap them because with GEM's interaction turned off, you have no way of talking to ST BASIC, which is a GEM application. If we hadn't included the ON ERROR GOTO and we had made a mistake typing, we would have had to reset the ST.
With this small program, we can send a line of text to the screen, the printer, or even the MIDI port. Or we would have sent it to the modem.
The modem works just like the keyboard. If a character has come through, a -1 will be returned from INP(-1). If no characters are available, a zero will be returned. OUT also works the same with the modem. If you would like to send a character to the modem, OUT 1,(ASCII VALUE) will do it. With this information, we could go about writing a very short terminal program in ST BASIC that, for starters, will be very similar to the VT52 Desk Accessory. But there is one major difference: you cannot customize the VT52 desk accessory.
The main escape codes are listed in Table 2. Most of them we will not need for our terminal, but some will be very important. We definitely need to see the cursor, and word wrap will come in handy (this allows text to flow onto another line if it exceeds 80 columns). We must organize our program into a smooth-flowing, well-structured application if we want it to keep up with the demands of high-speed telecommunication.
VT-52 Escape Codes
ESC A CURSOR UP
ESC B CURSOR DOWN
ESC C CURSOR RIGHT
ESC D CURSOR LEFT
ESC E CLEAR HOME
ESC H CURSOR HOME
ESC p REVERSE ON
ESC q REVERSE OFF
The "main" loop will consist of two IF's: one to check for an incoming character, and the other to check for an outgoing character. With some thought, trial, and error, I found out that giving priority to an incoming character (checking INP(1) first) was the best method. Otherwise, every time you enter text before a prompt has been reached (storing it in the buffer of the host computer), the incoming text being printed to the screen pauses.
These two IF's must then call two other subroutines. These subroutines should handle special characters, and finally process the information.
Special characters to watch out for would be keys that you hit on the keyboard that will serve a function on your side of the terminal. What leaps to mind is the [UNDO] key, which is well-suited for handling the QUIT function. When you hit [UNDO], you don't want its value sent over to the host, so you must trap it before you send it.
Let's trace a character as it goes through the system. If I hit a key, INP(2) will pick it up. If it is not a reserved key it should be sent to the modem with OUT l,(value). Likewise, if a character is coming in, INP(1) will receive it, and it should be printed to screen with OUT 2,(value). These two operations are reciprocals.
Good luck, and happy telecomputing!