	IDEAL
	MODEL TPASCAL
	DATASEG
EXTRN ExtraKey : BYTE
	CODESEG
; These routines are Copyright 1991 by Reuben Sumner.
; This file is the assembly source code to routines found in FakeCrt.Pas
; Unlike the provided Crt unit in Turbo Pascal these routines are fully
; redirectable (except where noted) and in some cases more reliable
; As well there is no wasteful startup code assosociated with this unit

PROC Sound FAR Hz : WORD
; Procedure Sound (Hz : Word);
; These procedure are almost identical to those in the Crt unit and are
; only provided for compatibility (I was having fun OK!). These interface
; directly with the hardware and are thus not redirectable
FreqDiv = 1193180
PUBLIC Sound
	mov		bx,[Hz]				; Load BX with frequency
	mov		ax,34DCh
	mov		dx,0012h			; Load DX:AX with frequency divisor
	cmp		bx,dx				; Check for a divide overflow
	jle		Done
	div		bx					; Perform division
	mov		bx,ax				; Save result
	in		al,61h				; Read Programmable Peripheral Interface (PPI)
	test	al,11b				; Make sure speaker is enabled
	jnz		SetFrequency		; If it is don't waste our time
	or		al,11b
	out		61h,al				; Send out new value with speaker enabled
	mov		al,0B6h
	out		43h,al				; Get timer ready
SetFrequency:
	mov		al,bl				; Get saved value from BX
	out		42h,al
	mov		al,bh
	out		42h,al				; Write out the high and low order bytes
Done:
	ret							; We're outta here!
ENDP Sound

PROC NoSound FAR
; Procedure NoSound;
PUBLIC NoSound
	in	al,61h					; Read PPI setting
	and	al,(not 0011b)
	out	61h,al					; Disable speaker
	ret
ENDP NoSound

PROC ReadKey FAR
; Function ReadKey : Char;
; This function is identical in usage to the ReadKey in the Crt unit
; however this one can have its input redirected and supports F11 and F12
PUBLIC ReadKey
	mov	ah,07h					; Unfiltered character input without echo
	int	21h
	ret
ENDP ReadKey

PROC KeyPressed
PUBLIC KeyPressed
	mov	ah,0Bh					; Check input status
	int	21h
	and	al,0FEh					; 0FFh if character waiting covert to Boolean
	ret
ENDP KeyPressed

PROC Delay FAR ms : WORD
; Procedure Delay (ms : Word);
; This is a very handy machine independant delay function works well
PUBLIC Delay
	mov	ax,[ms]					; Load ms count
	mov	cx,1000
	mul	cx						; Multiply by 1000
	mov	cx,dx					; Load into correct registers for int 15h
	mov	dx,ax
	mov	ah,86h					; Delay function
	int	15h						; Do it!
	ret
ENDP Delay

PROC RKey FAR
; Function RKey : Word;
; This function requires a bit of explanation. This function is designed
; as a replacement for ReadKey. Unlike ReadKey it reports a word instead of
; a character. If the low byte of the word is 0 then it is a extended key (F1)
; In that case the high byte contains the regular code reported by the second
; call to ReadKey. This should work with F11 and F12 (unlike ReadKey in the Crt
; unit) on almost all computers
PUBLIC RKey
	mov ax,40h
	mov es,ax
	xor ah,ah					; Read keyboard
	test [BYTE PTR es:96h],10000b; 101 keyboard flag at 40:96
	jz  NormalKeyboard
	add ah,10h				; Read Enhanced
NormalKeyboard:
	int 16h					; Call int 16h function 10h or 00h
	cmp	al,0E0h				; hidden E0?
	jne	RKeyDone
	or	ah,ah				; ASCII E0?
	jz	RKeyDone
	xor	al,al
RKeyDone:
	ret
ENDP RKey

PROC SReadKey
; Function SReadKey : Char;
; Totally compatible with Crt's ReadKey but will use F11 and F12 and is
; not redirectable
PUBLIC SReadKey
	cmp	[ExtraKey],0
	jnz	GiveNextCh
	mov ax,40h
	mov es,ax
	xor ah,ah					; Read keyboard
	test [BYTE PTR es:96h],10000b; 101 keyboard flag at 40:96
	jz  SNormalKeyboard
	add ah,10h				; Read Enhanced
SNormalKeyboard:
	int 16h					; Call int 16h function 10h or 00h
	cmp	al,0E0h
	jne	CheckFor0
	or	ah,ah
	jnz	ExtendedKey2
CheckFor0:
	or	al,al
	jz	ExtendedKey
	ret
ExtendedKey2:
	xor	al,al
ExtendedKey:
	mov	[ExtraKey],ah
	ret
GiveNextCh:
	mov	al,[ExtraKey]
	mov	[ExtraKey],0
	ret
ENDP SReadKey

PROC RKeyPressed FAR
; Function RKeyPressed : Boolean
PUBLIC RKeyPressed
	mov ax,40h
	mov es,ax
	mov ah,01h				; Keyboard status
	test [BYTE PTR es:96h],10000b		; 101 keyboard flag
	jz  NormalKeyboard2
	add ah,10h				; Read Enhanced
NormalKeyboard2:
	int 16h					; Call int 16h function 11h or 01h
	jz  NoKeyWaiting		; Zero flag set if no key waiting
	mov al,1
	jmp RKeyPressedX
NoKeyWaiting:
	xor al,al
RKeyPressedX:
	ret
ENDP RKeyPressed
END