Programming
The User Port

This is fairly straight forward to program because the operating system does most of the work for you. Don't worry about the peculiar numbers - they are there for historical reasons. We need to make use of two registers on the VIA chip, the data direction register which determines which of the eight lines are input and which are output and the input/output  register - which connects the port to the outside world. The SWI used is called 'OS_Byte' and the first number we supply it with determines the direction of data flow: 150 is read and 151 is write..

To set all eight lines to be output use:

SYS"OS_Byte",151,&62,&FF :

REMEMBER: 151 writes to a register, 150 reads from it. &62 is the number of the data direction register and &FF (1111 1111 in binary), puts a one into every location making all lines output. If you need four output lines and four input lines you would use:

SYS"OS_Byte",151,&62,&0F

You only need to set the register at &62 once in your program, not every time you write to the port, though it does no harm should you do it. To write to the output port use:

SYS"OS_Byte",151,&60,Value%

Where Value% is the number you want to write to the port and &60 is the offset of the port. 


Here is a small example. Say you have built the LED board and want every other LED to be ON. The pattern in binary would be: 0101 0101 ie 55 in hexadecimal. Here is the program:

SYS"OS_Byte",151,&62,&FF: REM All eight lines output
SYS"OS_Byte",151,&60,&55: REM Turn every other line ON

A small program that counts from 0 to 255 and holds each state for half a second looks like this:

SYS"OS_Byte",151,&62,&FF : REM All lines output
FOR Count%= 0 TO 255
SYS"OS_Byte",151,&60,Count% : Write to port
TIME=0:REPEAT UNTIL TIME > 50: REM Waste time
NEXT

To read from the port you use:

SYS"OS_Byte",150,&60 TO R1%,R2%,Byte%
This can be abbreviated to
SYS"OS_Byte",150,&60 TO ,,Byte%
Notice the two commas in front of Byte% - they must be there.

Byte% will now contain whatever value was in the port register.

Sometimes it is very useful to latch the port before reading it, this avoids spurious signals. Here is a small program which does this. As it stands, it simply prints the number found at the port to the screen.

REPEAT 
PROCread_port(Port%) 
PRINT Port%;" "; 
UNTIL FALSE 
STOP 

DEFPROCread_port(RETURN R2%) 
SYS"OS_Byte",151,&60,0: REM Clear user port 
SYS"OS_Byte",151,&62,0: REM All lines input 
SYS"OS_Byte",151,&6B,2: REM Set port to latching 

REPEAT 
SYS"OS_Byte",150,&6D TO R0%,R1%,R2% :REM Wait for CB1 to got low 
UNTIL R2%=16 

SYS"OS_Byte",150,&60 TO R0%,R1%,R2%: REM Read user port

ENDPROC

The Printer Port

Most of what I know about the printer port comes from a gentleman who contacted me years ago but I have lost his e-mail address. Sorry sir.

Basically we only use two registers and the operating system does most of the work for us - like finding where precisely the port is and so on. To initiate the printer port use:

SYS"Parallel_Op",2,10,0

This will prepare it for output only. You need only do this once in your program. The port can be switched to input as well, more about that later. To write to the port use:

SYS"Parallel_Op",1,Value

This will send the second variable to the printer port. So the above program that counts to the user port looks like this for the printer port:

SYS"Parallel_Op",2,10,0 : REM All lines output
FOR Count%= 0 TO 255
SYS"Parallel_Op",1,Count% : Write to port
TIME=0:REPEAT UNTIL TIME > 50: REM Waste time
NEXT

That's all there is to it. If you built my LED board you have the ideal method to test your program.


Back to the start
Tudor with sign