Pascal: Keypress function

Keypress is a commonly used function residing in the Applestuff unit. If
it's the only routine needed from this unit, it is more efficient to refer to
the routine alone without loading the entire unit.

The following listing of the function Keypress can be assembled for linking
to your host program as an "external" function. Follow the instructions
that accompany the examples shown on pp. 136-182 of the Pascal Operating
System Reference Manual.

.FUNC Keypress,0 ;0 words of parameters passed
;**************************************************
;*
;* Function Keypress: boolean; external
;*
;**************************************************
;
RETURN .EQU 0 ;Storage for return address
CONCKVEC.EQU 0BF0A ;Fixed address in BIOS
RPTR .EQU 0BF18 ;Fixed buffer pointer
WPTR .EQU 0BF19 ;Fixed buffer pointer
Version .EQU 0BF21 ;System version number
Keypress.EQU 0C000 ;Keyboard hardware
CONCK .EQU 0FF5C ;Way to get CONCK in old system

PLA
STA RETURN
PLA
STA RETURN+1
PLA
PLA
PLA
PLA ;Pop 4 bytes stack bias for function
LDA #0
PHA ;Return MSB zero
LDA Keyboard
BMI True
LDA Version
BNE $1 ;Jump if not original Pascal version
JSR CONCK
JMP $2
$1 JSR CONCKVEC ;Check console
$2 LDA RPTR
CMP WPTR ;Char in buffer?
BEQ Empty
TRUE LDA #1 ;Yes, return Keypress=True
BNE KPDONE ;Always taken
EMPTY LDA #0 ;No, return Keypress=False
KPDONE PHA ;Push LSB result
LDA RETURN+1
PHA ;Restore return address
LDA RETURN
PHA
RTS
.END

The sample program below illustrates the use of Keypress as an externally
linked routine. Follow the instructions given on pp. 136-182 of the Pascal
Operating System Reference Manual for assembling and linking external code.

Program Presstest;

Var I: integer;

Function Keypress: boolean; external;

Begin
I:=0;
Repeat
Writeln (I);
I:=I+1;
Until Keypress
End.

Published Date: Feb 18, 2012