;*******************************************************************************
;*                                                                             *
;*                     KAYPRO Software / Terminal Version 1.0                  *
;*                                                                             *
;*                       Tyler Sperry  /  9 June 1982                          *
;*                       Steven Fabian /  3 May  1983                          *
;*******************************************************************************
;*                  Copyright (c)1982 Non-Linear Systems, Inc.                 *
;*                                                                             *
;*******************************************************************************
;* EQUATES    ******************************************************************
;*******************************************************************************
;TERM.ASM
;Converted to M80 format by Bill Mckinley on AUG 30, 1983.

.Z80

WARM	EQU	0H		; CP/M warm start
FDOS	EQU	5H		; BDOS functions subroutine
CONOUT	EQU	2H		; BDOS function # for character output
DIRECT	EQU	6H		; BDOS function # for direct console I/O
STRING	EQU	9H		; BDOS function # for string output

SIODPA	EQU	4H		; Serial channel A data port
SIODPB	EQU	5H		; Serial channel B data port
SIOCPA	EQU	6H		; Serial channel A control/status port
SIOCPB	EQU	7H		; Serial channel B control/status port

EOT	EQU	04H
LF	EQU	0AH
CR	EQU	0DH
ESC	EQU	1BH
CRLF	EQU	0D0AH

BAUDA	EQU	0H		; Channel A baud rate port
B110	EQU	02H		; Baud rate IC divisor values
B300	EQU	05H
B600	EQU	06H
B1200	EQU	07H
B2400	EQU	0AH
B4800	EQU	0CH
B9600	EQU	0EH
B19K2	EQU	0FH


;*******************************************************************************
;* MAIN PROGRAM ****************************************************************
;*******************************************************************************


	ORG	100H		; CP/M .COM file

INIT:	CALL	CLS		; Clear the screen
	CALL	HELLO		; and announce choices, etc.
	LD	C,SIOCPA	; Point to the SIO, channel A
	LD	A,(TABLE)	; 1st entry in TABLE is size thereof
	LD	B,A
	LD	HL,TABLE+1
	OTIR			; loop: init the SIO from TABLE

ENTRY:	LD	E,0FFH		; Input a character using
	LD	C,DIRECT	; Direct console I/O
	CALL	FDOS
	OR	A		; Was a key pressed?
	JR	Z,ENTRY		; If not, keep waiting
	CP	ESC
	JR	NZ,CRTEST	; Did they press <ESC>?
	RET			; If so, return to CP/M
CRTEST:	CP	CR		; Did they press <CR>?
	JR	Z,MAIN		; If so, do your terminal stuff
	CALL	BEEP		; Else, signal error
	JR	ENTRY		;  and wait for another entry

MAIN:	CALL	CLS
	CALL	REMIND		; Tell user how to get out
LOOP:	IN	A,(SIOCPA)	; Get SIO/a status byte
	BIT	0,A		; Rx char avail = carry flag
	JR	Z,KEYIN		; Nobody home, check keyboard
	IN	A,(SIODPA)	; Get the character
	AND	7FH		; Mask off possible parity bits
	CP	EOT		; If it's End of Transmission
	JP	Z,WARM		; Exit to CP/M...
	LD	E,A		; Else put the character
	LD	C,CONOUT	;  to the console device
	CALL	FDOS
KEYIN:	CALL	KEYS		; Look for keyboard character
	JR	Z,LOOP		; If no character, do it again...
	LD	C,A
	CALL	XMITA		; Else send the key to the modem
       	JR	LOOP		; Exit loop only on EOT from modem

	;end of program.

;*******************************************************************************
;* SUBROUTINES *****************************************************************
;*******************************************************************************


BEEP:	LD	A,04		; Make it a long beep
	OUT	(SIODPB),A	; send it to the keyboard
	RET			; Short enough for you?

CLS:	LD	E,1AH		; ^Z to console clears screen
	LD	C,CONOUT
	CALL	FDOS
	RET

KEYS:	LD	E,0FFH			; Direct input request
	LD	C,DIRECT
	CALL	FDOS			; A = 0 if no key pressed and
	AND	A			; A = char available otherwise
	RET

XMITA:	IN	A,(SIOCPA)		; Get SIO/a status byte
	BIT	2,A
	JR	Z,XMITA			; Loop until SIO is ready for byte
	LD	A,C			; Data passed in C
	OUT	(SIODPA),A
	RET

HELLO:	LD	C,STRING
	LD	DE,MSG1
	CALL	FDOS		; Print the message below as a prompt
	RET
MSG1:	DW	CRLF
	DB	'KAYPRO Terminal Emulator'
	DW	CRLF
	DW	CRLF
	DB	'This program allows your KAYPRO'
	DB	' computer to imitate a "dumb" terminal.'
	DW	CRLF
	DW	CRLF
	DB	'What you type at the keyboard'
	DB	' is sent through the RS-232'
	DB	' connector to the modem'
	DW	CRLF
	DB	'(or other peripheral)'
	DB	' and data from the modem is'
	DB	' put on this video display.'
	DW	CRLF
	DW	CRLF
	DB	'Technically, this program looks'
	DB	' like a Lear-Siegler ADM-3A terminal'
	DB	' and is set'
	DW	CRLF
	DB	'for full-duplex, no parity check'
	DB	' and 8 bits/character.'
	DW	CRLF
	DW	CRLF
	DB	'Press either <RETURN> to start'
	DB	' using the terminal or'
	DW	CRLF
	DB	'press <ESC> to return to CP/M: '
 	DB	'$'

REMIND:	LD	C,STRING	; Tell user how to exit terminal program
	LD	DE,MSG2
	CALL	FDOS
	RET
MSG2:	DW	CRLF
	DB	'Press <CTRL> and <D> at the'
	DB	' same time when done using TERM.COM'
	DW	CRLF
	DB	'and the program will exit to CP/M.'
	DW	CRLF
	DB	LF
	DB	'$'  


;*******************************************************************************
;* STORAGE *********************************************************************
;*******************************************************************************


TABLE:	DB	9		; Size of TABLE

	DB	18H		; Channel RESET to SIO
	DB	4,44H		; WR4: Clock divider, 1 stop bit,no parity
	DB	1,0		; WR1: no interrupts
	DB	3,0C1H		; WR3: 8 bits/ch, no auto enable, enable RCVR
	DB	5,0EAH		; WR5: 8 bits/ch, set DTR & RTS


	END

;*******************************************************************************
