; NUNH2-1.INS - NUBYE insert for Northstar Horizon Computers - 04/21/86
;
; 8251A I/O on 2nd Serial Port of Northstar Horizon
; Supports 300 and 1200 baud automatic switching
;
; Note:  This is an insert, not an overlay.
;
; ========
;
; 04/21/86  Modified for NUBYE
; 08/30/85  Written to work with Northstar Horizon      - Adam Grow
; 07/17/85  Written to work with BYE5			- Irv Hoff
; 03/30/85  Written for Morrow Micro Decision Computers - Jim Sinning
;
; ========
;
;The following define the port address to use
;
PORT	EQU	4		; Data port
MDCTL1	EQU	PORT+1		; Status/control port
STPORT	EQU	6		; Motherboard status port supports
				;   carrier detect
;
; NOTE: The baud rate header for the 2nd Serial Port must be wired for  
; 1200 baud and the configuration header for the 2nd Serial Port must
; be wired as a terminal (not modem) for this to work.
;
BPORT	EQU	MDCTL1		; Baud rate port=8251 control
CPORT	EQU	MDCTL1		; Control port=8251 control
MODINS1	EQU	4EH		; 8 bits, 1 stop, 64x, no parity
;
; See if we still have a carrier - if not, return with the zero flag set
;
MDCARCK:IN	STPORT		; Get status from motherboard status port
	ANI	10H		; Check CD bit for a carrier
	CPI	10H		; Set zero flag if no carrier
	RET
;
; Disconnect and wait for an incoming call
;
MDINIT:	MVI	A,10H		; Clear DTR
	OUT	MDCTL1		; Causing hangup
	PUSH	B		; Preserve in case we need it
	MVI	B,20		; 2 second delay
;
OFFTI:	CALL	DELAY		; 0.1 second delay
	DCR	B
	JNZ	OFFTI		; Keep looping until finnished
	POP	B		; Restore BC
	MVI	A,17H		; Assert DTR so that modem
	OUT	MDCTL1		; Can answer phone
	CALL	UDELAY		; Added to be safe
;
	 IF	IMODEM
	CALL	IMINIT
	 ENDIF			; IMODEM
;
	RET
;
; Input a character from the modem port
;
MDINP:	IN	PORT		; Get character
	RET
;
; Check the status to see if a character is available.	If not, return
; with the zero flag set.  If yes, use 0FFH to clear the flag.
;
MDINST:	IN	MDCTL1		; Get status
	ANI	2		; Check the receive ready bit
	RZ			; Return if none
	ORI	0FFH		; We have a character
	RET
;
; Send a character to the modem
;
MDOUTP:	OUT	PORT		; Send it
	RET
;
; See if the output is ready for another character
;
MDOUTST:IN	MDCTL1
	ANI	1		; Check the transmit ready bit
	RET
;
; Reinitialize the modem and hang up the phone by dropping DTR and
; leaving it inactive.
;
MDQUIT:	 IF	IMODEM
	CALL	IMQUIT		; Tell smartmodem to quit
	 ENDIF			; IMODEM
;
; Called by the main program after caller types BYE
;
MDSTOP:	MVI	A,10H		; Turn off DTR
	OUT	MDCTL1
	RET
;
; The following routine sets the baudrate.  BYE5 asks for the maximum
; speed you have available.
;
SET2400	EQU	$		; Only supports 300/1200
;
SETINV:	ORI	0FFH		; Make sure zero flag not set
	RET
;
SET300:	MVI	B,BD300
	JMP	SETBAUD
;
SET1200:MVI	B,BD1200
;
SETBAUD:MVI	A,47H		; Reset, enable Rx, DTR, Tx
	OUT	MDCTL1		; Rx,Tx enabled
	CALL	UDELAY
	MOV	A,B		; Recover Baud Rate Word
	OUT	BPORT
	CALL	UDELAY		; Wait for USART
	MVI	A,17H		; ERR reset, DTR, Rx, Tx all on
	OUT	MDCTL1
	CALL	UDELAY
	XRA	A		; Say rate OK
	RET
;
; Values to switch the 8251 between x16 and x64
;
BD300	EQU	4FH		; 300 baud
BD1200	EQU	4EH		; 1200 bps
;
; IN8251 specific USART delay
;
UDELAY:	NOP
	NOP
	NOP
	RET
;
; end of insert
; -------------