;**************************************************************
;
; B5C-DCH2.INS A TIME insert for BYE501 and up
;  10/19/85    Modified for BYE501 (clock read on c/r) F.E. Clayton
;  10/08/85    Modified for Zorba   F.E. Clayton
;  08/08/85    Note:  This is an insert -- not an overlay
;			Bucky Carr
;			World Peace RCP/M, 303-320-4822
;
; Adapted from:
; MBC-DCH1.ASM -- Version 1.1 -- 17 Apr 85 -- by Barron F. McIntire III
;
;   TIME routine for BYE5 running on machine with Hayes Chronograph
;   Clock connected to second serial port. This overlay should be
;   easily configurable to any system which has an extra RS-232 port to
;   which the clock may be connected.  
;
;   Insure that your clock set program does NOT set the time and
;   date separators on the Hayes.
;
;   With BYE502 the clock is read only after a c/r is sent. The code below
;   will read both the DATE and TIME when BYE answers the phone and
;   detects a carrier.
;   After the first read the DATE is read every 48 c/r's. This makes
;   the TIME read undetectable at 300 bps, just detectable at 1200 bps,
;   and noticable (but not TOO bad) at 2400.
;   Higher speeds (than 2400) are going to require a different approach.
;
;   NOTE: Your second serial will need to be initialized and set
;   to 1200 bps. 
;
;   Code must be placed in the BYE5 ANSW: area to set the
;   start value into the counter, when the caller's carrier is
;   recognized:
;		 IF HAYCLK
;		LXI	H,0		;when CLKCTR=0 then read clock
;		SHLD	CLKCTR		;location where the count resides
;		 ENDIF			; hayclk
;
;   is all that is necessary.  This makes sure that the clock is read
;   as soon as the carrier is detected.
;
;   When called this routine will check the RTCBUF.  If a '99H' is in
;   the first byte, then clock is initialized.	Next, the seconds are
;   checked, and if changed since last update of RTC buffer, the clock
;   is read and new data copied to the RTCBUF.	The BCD hours and
;   minutes are converted to binary and stored in CCMIN and CCHOUR.
;
;   Notes by - Bucky Carr, World Peace RCP/M
;	       303-320-4822
;   Addional notes and changes by - F.E. Clayton
;              FIDO 12/4 808-245-2080
;              Zorba Net #2 (RCP/M) 808-337-9280
;**********************************************************************
HAYCLK 	EQU	YES		;For code above inserted at ANSW:
;
;		;Next two address points for Zorba 25th line so
;		;My BBS system can find date and time, as well as
;		;for sysop use. (When did last user log off??)
;
TME	EQU	0FEA6H		;Zorba 25th line
DTE	EQU	0FEADH		;Zorba 25th line 
;
CDAT	EQU	22H		; Put your clock data port here
CSTAT	EQU	23H		; Put your clock status port here
;
; The Line Status Register is evaluated for whether a character is waiting
; in the receive buffer (from the clock) or whether the transmit buffer
; is empty and ready for another character (to the clock.)  The actual
; data is sent and fetched, as the case may be, from the data port.  
;
TBE	EQU	01H		; Mask for transmit buffer empty (ready)
RBF	EQU	02H		; Mask for receive buffer full (ready)
;
CENTURY EQU	019H		; BCD for 20th Century
;
TIME:
	PUSH	PSW		; This register pair isn't saved by BYE500
	LHLD	CLKCTR		; Get counter value
	MOV	A,L		; Get lo bite
	CPI	00H		; Is it 0?
	JNZ	DECRMT		; No?  Then decrement and exit
	MOV	A,H		; Get hi bite
	CPI	00H		; Is it 0?
	JZ	CLOCK1		; If 0 read DATE and update RTCBUF
DECRMT:
	DCX	H		; Otherwise decrement counter...
	SHLD	CLKCTR		; save...
	JMP	CLOCK2		; Read TIME but NOT DATE and update RTCBUF
;
CLOCK1:	LHLD	CLKWT
	SHLD	CLKCTR
	LXI	H,RDDATE	; Set pointer to 'ATRD' message
	CALL	CLKRD		; Read date
	LXI	H,CLKASC	; Set pointer to ASCII storage
	CALL	GETASC		; Get ASCII character
	STA	DTE+0		; Put it on 25th line
	CALL	GETCHR		; Get hi year char
	RLC ! RLC ! RLC ! RLC	; Mov to hi nibble for BCD
	MOV	B,A		; Save nibble
	INX	H
	CALL	GETASC		; Get ASCII Char.
	STA	DTE+1		; Add it to 25th line
	CALL	GETCHR		; Get lo year char
	ORA	B		; Put nibbles together
	STA	RTCBUF+4	; Put year in RTCBUF
	INX	H
	CALL	GETASC		; Get ASCII char.
	STA	DTE+2		; add to 25th line
	CALL	GETCHR		; Get hi month char
	RLC ! RLC ! RLC ! RLC	; Mov to hi nibble for BCD
	MOV	B,A		; Save nibble
	INX	H
	CALL	GETASC		; Get ASCII Character
	STA	DTE+3		; Add to 25th line
	CALL	GETCHR		; Get lo month char
	ORA	B		; Put nibbles together
	STA	RTCBUF+5	; Put month in RTCBUF
	INX	H
	CALL	GETASC		; Get ASCII Character
	STA	DTE+4		; Add to 25th line
	CALL	GETCHR		; Get hi day char
	RLC ! RLC ! RLC ! RLC	; Mov to hi nibble for BCD
	MOV	B,A		; Save nibble
	INX	H
	CALL	GETASC		; Get ASCII Character
	STA	DTE+5		; Add to 25th line
	CALL	GETCHR		; Get lo day char	
	ORA	B		; Put nibbles together
	STA	RTCBUF+6	; Put day in RCTBUF
	MVI	A,CENTURY	; Set century value for RTCBUF+3
	STA	RTCBUF+3	; and store it
;
CLOCK2:	LXI	H,RDTIME	; Set pointer to 'ATRT' message
	CALL	CLKRD		; Read time
	LXI	H,CLKASC	; Point to stored ASCII data
	CALL	GETASC		; Get ASCII Character
	STA	TME+0		; Add to 25th line
	CALL	GETCHR		; Get hi hours char
	RLC ! RLC ! RLC ! RLC	; Mov to hi nibble for BCD
	MOV	B,A		; Save nibble
	INX	H	
	CALL	GETASC		; Get ASCII Character
	STA	TME+1		; Add to 25th line
	CALL	GETCHR		; Get lo hours char
	ORA	B		; Put nibbles together	
	STA	RTCBUF+0	; Put hours in RTCBUF
	INX	H
	CALL	GETASC		; Get ASCII Character
	STA	TME+2		; Add to 25th line
	CALL	GETCHR		; Get hi minutes char
	RLC ! RLC ! RLC ! RLC	; Mov to hi nibble for BCD
	MOV	B,A		; Save nibble
	INX	H	
	CALL	GETASC		; Get ASCII Character
	STA	TME+3		; Add to 25th line
	CALL	GETCHR		; Get lo minutes char
	ORA	B		; Put nibbles together
	STA	RTCBUF+1	; Put minutes in RTCBUF
	INX	H
	CALL	GETASC		; Get ASCII Character
	STA	TME+4		; Add to 25th line
	CALL	GETCHR		; Get hi seconds char
	RLC ! RLC ! RLC ! RLC	; Mov to hi nibble for BCD
	MOV	B,A		; Save nibble
	INX	H
	CALL	GETASC		; Get ASCII Character
	STA	TME+5		; Add to 25th line
	CALL	GETCHR		; Get lo seconds char
	ORA	B		; Put nibbles together
	STA	RTCBUF+2	; Put minutes in RTCBUF
;
;
CLOCK3:	LDA	RTCBUF		; get the BCD hours
	CALL	BCDBIN		; convert to binary
	STA	CCHOUR		; store it
	LDA	RTCBUF+1	; now get the BCD minutes
	CALL	BCDBIN		; convert to binary
	STA	CCMIN		; store it
CLKEXIT:
	POP	PSW
	RET			; Return to BYE500
;
CLKRD:
	IN	CSTAT		; Get status of the transmit buffer
	ANI	TBE		; Is it empty
	JZ	CLKRD		; No, loop until it is
	MOV	A,M		; Get char from ATxx string in memory
	CPI	80H		; Are we done with the string?
	JZ	CLKIN		; Yes, get ASCII from clock
	OUT	CDAT		; Otherwise send char to clock
	INX	H		; Point to next char
	JMP	CLKRD		; And do it again
CLKIN:
	LXI	H,CLKASC	; Set pointer to next ASCII storage location
CLKIN2:
	IN	CSTAT		; Get status of the receive buffer
	ANI	RBF		; Is there a character waiting for us?
	JZ	CLKIN2		; No, loop until there is
	IN	CDAT		; Yes, get char
	MOV	M,A		; Store it
	CPI	0DH		; Carriage return?
	RZ			; Return if so
	INX	H		; Else point to next storage location
	JMP	CLKIN2		; Get next char
;
GETCHR:
	MOV	A,M		; Get character
	SUI	30H		; Convert from ASCII to binary
	ANI	0FH		; Clear hi nibble
	RET
;
GETASC:	MOV	A,M		; Get character w/o change
	RET
;
RDTIME: DB	'ATRT',CR,80H
RDDATE: DB	'ATRD',CR,80H
CLKASC: DS	7
CLKCTR: DS	2
SECS:	DS	1
CLKWT:	DW	0030H		; # c/r's sent out between DATE reads
;
;......
;
; End of the CLOCK routine
;
;******************************************************************
