Chapter 4

P21Forth Serial and Parallel I/O


P21Forth 1.02

P21Forth supports colored text and graphics output to a video display, and it also supports a serial or parallel terminal or both. The user may select the type of input and output that they want to use. A terminal or at least a keyboard must be attached to the MuP21 system to use P21Forth.

When P21Forth starts it puts the message P21Forth 1.0 on the video screen and looks for a parallel or serial keyboard on the input port. If a serial terminal or keyboard is being used P21Forth will try to determine the speed of the attached device. P21Forth reads a `b' character from the '' serial device and initializes the serial input output routine.

This is done by the Forth word KEYTEST. It checks the keyboard and switches the input vector to a parallel keyboard if it finds one. Otherwise it switches the input vector to the code for the serial keyboard, and waits for a `b' character to get the serial timing using the word INITSERIAL.

The word SERIAL sets the input vector stored in the variable `?RX to the routine ?RS which reads the serial keyboard. The word PARALLEL sets the input vector to ?RX which reads a parallel keyboard. The word BOTH sets the input vector to both which will read from either keyboard if two are attached.

The multitasker is also involved in keyboard input. To turn on the multitasker to switch tasks while it waits for keyboard input with the parallel keyboard set the input vector to P?RX with the command:

            ` P?RX `?RX !
To turn on the multitasker to switch tasks while it waits for keyboard input with the serial keyboard you need to first create a word to do this. This can be done with the command:
            : P?RS  ( -- c -1 ) PAUSE ?RS ;
You should then set the input vector to P?RS with the command:
            ` P?RS `?RX !
The word OUT will output one character from the stack to the parallel port. The word TX! outputs one character from the stack to the video display via OK. The word TS sends one character from the stack to the serial port.

EMIT is the Forth word to output a character to the system output device. It is vectored though the word `EMIT. The word `EMIT is a variable that holds the execution vector for the EMIT. At boot the variable `EMIT will contain the address of TX! so the default output device is the video display. To set the output device to the serial port issue the command:

            ` TS `EMIT !
Or you can define a word to do this with the command SERIAL- OUT with the command:
            : SERIAL-OUT ( c -- ) ` TS `EMIT ! ;
To set the output device to the parallel port issue the command:
            ` OUT `EMIT !
Or you can define a word to do this with the command PARALLEL-OUT with the command:
            : PARALLEL-OUT ( c -- ) ` OUT `EMIT ! ;
To set the output device to the video display issue the command:
            ` TX! `EMIT !
Or you can define a word to do this with the command VIDEO- OUT with the command:
            : VIDEO-OUT ( c -- ) ` OUT `EMIT ! ;
To set the output device to both the video display and the serial port you need to first define a word to peform this function. To do this issue the command:
            : VIDEO-SERIAL-OUT ( c -- ) DUP TX! TS ;
Then you can set the vector for EMIT with the command:
            ` VIDEO-SERIAL-OUT `EMIT !
Or you can define a word to do this with the command:
: SET-VIDEO-SERIAL-OUT ( c -- ) ` VIDEO-SERIAL-OUT `EMIT ! ;

A glossary of words related to serial and parallel i/o.

OUT ( c -- )           8 bit parallel output
IN ( -- c )            8 bit parallel output
KEYTEST ( -- )         check for parallel or serial keyboard
keytest ( -- )         used with KEYTEST
BOTH ( -- )            set up SERIAL and PARALLEL keyboards
both ( -- )            used in BOTH
PARALLEL ( -- )        set up PARALLEL keyboard
SERIAL ( -- )          set up SERIAL keyboard
TS ( c -- )            transmit character to serial port
INITSERIAL ( -- )      read a `b' character to set timing
?RS ( -- 0 | c -1 )    check serial keyboard status
KEY ( -- c )           read key from whatever keyboard
P?RX ( -- 0 | c -1 )   multitasking parallel keyboard input
P?RS ( -- 0 | c -1 )   multitasking serial keyboard input
TX! ( c -- )           send character to video output
?RX ( -- 0 | c -1 )    check parallel keyboard status
`?KEY ( -- n )         vector for ?KEY in KEY
`EMIT ( -- n )          ector for EMIT
?KEY ( 0 | c -1 )      check system keyboard status
EMIT ( c -- )          output character
P?RS is not compiled in the boot image of P21Forth 1.02, instead it can be loaded from a disk block by the command B LOAD. This will load P?RS, MULTI-SERIAL, MULTI-PARALLEL, SINGLE-SERIAL, and SINGLE-PARALLEL. These words are discussed in the multitasking demo explanation in chapter 7.

From the required ANS documentation:

character set (3.1.2 Character types, 6.1.1320 EMIT, 6.1.1750 KEY);

P21Forth provides a vectored EMIT and KEY. P21Forth serial I/O routines process 8 bit characters. MuP21 parallel keyboard and video output routines support 7 bit characters. MuP21 represents characters as one cell, and therefore supports I/O of 20 bit characters. Serial and video output routines use only the lower 7 or 8 bits of a cell for output.

method of selecting 3.2.4.1 User input device;

P21Forth uses vectored input. The variable `?KEY holds the execution vector for ?KEY. ' ?RX '?KEY ! will set the user input device to the parallel without a multitasking pause. ' P?RX '?KEY ! will set the user input device to the parallel keyboard with a multitasking pause. ' ?RS '?KEY ! will set the user input device to the serial keyboard. ' both '?RX ! will enable both serial and parallel keyboards at the same time. PARALLEL is an alternate way to set the input vector to the parallel keyboard. SERIAL is an alternate way to set the input vector to the serial keyboard, and BOTH is an alternate way to enable both keyboards.

method of selecting 3.2.4.2 User output device;

P21Forth uses vectored output. The variable 'EMIT holds the execution vector for EMIT. ' TX! 'EMIT ! will set the user output device to the MuP21 video output. ' TS! 'EMIT ! will set the user output device to the serial port.


End of Chapter 4

Return to Table of Contents

Previous Chapter

Next Chapter