	title tty subroutines
	page	60,132

	.xlist
	include ifthen.mac
	include util.mac
	.list

TTY$data segment 'data'

	db	'TTY     '
SP_save	dw	0
	dd	TTY
	dd	0

Echo    db 1
Extend	db 0
SaveIt	db 0

TTY$data ends

TTY$code segment 'code'
	assume cs:TTY$code, ds:TTY$data

	dw	seg TTY$data

public	TTY, TTYset, TTYrst, TTYIW, TTYO, ChrRdy


TTY   	proc far			; call TTY

	mov	ax, TTY$data
	mov	ds, ax
	mov	SP_save, sp

	mov	ax, 3			; set mode to 80 X 25 color text
	int	10h			; and clear screen

	ret

TTY   	endp


TTYset	proc far			; call TTYset

	mov	ax, TTY$data
	mov	ds, ax
	mov	SP_save, sp

	mov	Echo,0
	ret

TTYset	endp


TTYrst	proc far			; call TTYrst

	mov	ax, TTY$data
	mov	ds, ax
	mov	SP_save, sp

	mov	Echo,1
	ret

TTYrst	endp


TTYIW	proc far			; call TTYIW( Key )

	mov	ax, TTY$data
	mov	ds, ax
	mov	SP_save, sp


	if$ Extend, e, 1
	  mov	Extend, 0
	  mov	ax, 0
	  mov	al, SaveIt
	  lds	si,dword ptr es:[bx]	;ds,si := addr of Key
	  mov	ds:[si], ax		;place extended code in Key
      	  ret				;return
	endif$


	mov	ah, 0
	int 16h


	if$ al, e, 0
	  mov	SaveIt, ah		; Save extended code
	  mov	Extend, 1		; Set "extended code" flag
	  mov	ax, 0
	  lds	si,dword ptr es:[bx]	; ds,si := addr of Key
	  mov	ds:[si], ax		; place the 0 in Key
      	  ret				; return
	endif$


	if$ al, e, 3			; ^C handling
	  int 23h
	  jnc Continue			; don't abort
	  pop ax			; pop flags
	  mov ah, 4Ch
	  mov al, 255			; return code
	  int 21h			; terminate process

Continue:

	elseif$ Echo, ne, 0
	  mov	ah,2			; display char function
	  mov	dl, al
	  int	21h

	endif$

	if$ al, le, 42
	  jmp Normal
	endif$

	if$ al, ge, 58
	  jmp Normal
	endif$

	if$ ah, le, 70
	  jmp Normal
	endif$

	if$ ah, ge, 84
	  jmp Normal
	endif$

	push ax
	mov ah, 2			; if not keypad mode do normal
	int 16h
	test al, 32
	pop ax
	jnz Normal

	cbw
	neg	ax

	lds	si,dword ptr es:[bx]	; ds,si := addr of Key
	mov	ds:[si], ax		; place char in Key

      	ret				; return


Normal:

	mov	ah, 0
	lds	si,dword ptr es:[bx]	; ds,si := addr of Key
	mov	ds:[si], ax		; place char in Key

      	ret				; return

TTYIW	endp

TTYO 	proc far			; call TTYO( Char )

	mov	ax, TTY$data
	mov	ds, ax
	mov	SP_save, sp

	lds	si,dword ptr es:[bx]	;ds,si := addr of Char

	mov	ah,2
	mov	dl,ds:[si]
	int	21h

      	ret				;return

TTYO	endp

ChrRdy	proc far			; if( ChrRdy(0) ) then...

	mov	ax, TTY$data
	mov	ds, ax
	mov	SP_save, sp

	mov	ah,0Bh
	int	21h

	xor	ah,ah

      	ret				;return

ChrRdy	endp

TTY$code ends

	end
