<< Home | About Forth | About TurboForth | Download | Language Reference | Resources | Tutorials | YouTube >>
There are a number of console functions which allow a user to interface with the computer via the keyboard, joysticks, and screen. These functions/words are described below.
Single characters may be displayed at the current cursor position with the word EMIT. EMIT requires a character code (an ASCII code, between 0 and 255 decimal) on the stack. For example:
Will display an A at the current cursor position.
Will display an asterisk, and so on.
After displaying the character, the current position is updated, moving to the beginning of the next line if necessary, and scrolling the screen up by one line if the last line has been reached.
In a colon definition, a string constant can be directly displayed with the word ." (pronounced "print"). For example:
: HELLO ." Hello, world!" ;
The above word will display Hello, world! on the screen whenever it is executed.
Strings are declared with the word S" as follows (they should be declared within a colon definition):
: MyString S" This is my string" ;
Note that unlike some languages, the string doesn't have a name. That's because it doesn't need one. Whenever the string is executed it pushes the address of the string, and the length of the string to the stack. This can be used (for example) by words like TYPE, which will 'type' a string to the screen, given an address and number of characters to display:
: TEST MyString TYPE ;
This word executes MyString, which causes the address and length of the string to be pushed onto the stack. Then TYPE is executed, which displays the string.
Note that whilst strings don't have names, it is possible to treat them as if they do have names. For example, the word MyString above could be considered equal to the following line of BASIC code:
MyString$="This is my string"
TurboForth maintains a cursor which is used to determine the location of the next character to be displayed on the screen. The cursor is maintained by the word EMIT. Other words, such as TYPE and ." use EMIT to physically display ASCII characters on the screen. Each time a character is displayed, the cursor advances one position to the right. When the right-most column is reached, the cursor moves to the left-most position, and advances downwards by one screen line. When all screen lines have been exhausted the screen is automatically scrolled.
CR (carriage return) causes the cursor to advance to the next screen line and move to the left-most screen position. The screen will be scrolled if the cursor is already on the bottom line.
The cursor position may be set by the user with the word GOTOXY. GOTOXY expects the column (x) and row (y) on the stack (y on the top of the stack). For example:
5 7 GOTOXY
Will set the cursor position to colum 5 and row 7.
The keyboard can be read with the word KEY. KEY will stop your program and wait for a key to be pressed. When a keypress is detected, it pushes the key code (normally the ASCII code) of the detected key to the stack.
The above program will nag you unti you press X (upper case).
KEY? basically works the same was as KEY, however, KEY? doesn't pause the program and wait for a keypress. It simply scans the keyboard, and if a keycode is detected its keycode (normally its ASCII code) is pushed to the stack. If no key is detected then the value -1 is pushed to the stack.
The joysticks are scanned with the word JOYST. The TI-99/4A can support two joysticks. JOYST needs the joystick unit to scan (0 or 1) on the stack before being called. JOYST will scan the selected joystick and push a code to the stack. The code is a bit-code and is decoded as follows:
1=Fire, 2=Left, 4=Right, 8=Down, 16=Up
As the following program illustrates, bit combinations can be returned. For example, if the joystick is in the North East (up and left) direction, and the fire button is pressed, then the code returned is 19 decimal.
The above program operates in a permanent loop, scanning joystick #0. It tests each bit returned by JOYST and displays the detected results. The various possible combinations are reported correctly.
By default, the screen scrolls upwards when all 24 screen lines are used. This behaviour can be overridden by writing the value FALSE (0) to the variable SSCROLL.
: TEST FALSE SSCROLL ! 1000 0 DO I . LOOP TRUE SSCROLL ! ;
The above program turns off screen scrolling, executes a simple loop 1000 times, then switches screen scrolling back on.
<-- Back to Tutorials