;
;**************************************************************
;
; B5C-OKI1.INS A TIME insert for BYE500 and up
;  08/14/85    Note:  This is an insert -- not an overlay
;			Bucky Carr
;			DenHUG (ZBBS) RCP/M, 303-423-3224
;
; Adapted from:
;   A clock insert for BYE and MBYE written by Donald E Roth, Denver.
;
;   This is an insert for addressing and reading from the OKI MSM5832
;   clock chip as addressed by an 8255 USART.  In our setup, it happens
;   to be connected to a Heathkit H89 and is addressed at port 320Q.
;
;   It should be possible to use this code for any ported system, and
;   the 8255 and 5832 lend themselves to relatively inexpensive wire-
;   wrapping should one desire to roll his own clock board.  Schematics
;   are available, and perhaps somewhere down the line I will include
;   a schematic with this insert....
;
;   To set the clock initially we use a clock setting program written for
;   that specific purpose (which I cannot lay my hands on at the moment,
;   but will search for, for the truly interested.  It is written in BASIC.
;
;   If you also desire the MBASIC code to read the clock from within
;   a BBS program, let me know and I will offer that up, too.
;
;
;
;   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.
;
;
;				    - Bucky Carr, SYSOP,
;					World Peace (ZBBS) RCP/M, 303-320-4822
;							and
;					DenHUG (ZBBS) RCP/M, 303-423-3224
;
;**********************************************************************
;
;these equates are needed to set up the 8255 PPI to read from
;the OKI MSM5832 real time clock chip

MODSET	EQU	10000000B	;set mode bit
MODA0	EQU	0		;Group A mode 0
MODA1	EQU	00100000B	;group A mode 1
MODA2	EQU	01000000B	;group A mode 2
MODB0	EQU	0		;group B mode 0
MODB1	EQU	00000100B	;group B mode 1
PRTAIN	EQU	00010000B	;port A input
PRTAOT	EQU	0		;port A output
PRTBIN	EQU	00000010B	;port B input
PRTBOT	EQU	0		;port B output
PRTCUI	EQU	00001000B	;port C upper nibble input
PRTCUO	EQU	0		;port C upper nibble output
PRTCLI	EQU	1		;port C lower nibble input
PRTCLO	EQU	0		;port C lower nibble output

RTCPTA	EQU	320O		;port A address of PPI for real time clock
RTCPTB	EQU	RTCPTA+1	;port B address of PPI for real time clock
RTCPTC	EQU	RTCPTA+2	;port C address of PPI for real time clock
RTCCTL	EQU	RTCPTA+3	;real time clock PPI control port
;address inputs for MSM5832 real time clock chip
RTCS1	EQU	0		;seconds
RTCS10	EQU	10H		;seconds X10
RTCM1	EQU	20H		;minutes
RTCM10	EQU	30H		;minutes X10
RTCH1	EQU	40H		;hours
RTCH10	EQU	50H		;hours X10
RTCWK	EQU	60H		;day of the week
RTCD1	EQU	70H		;days
RTCD10	EQU	80H		;days X10
RTC1M	EQU	90H		;month
RTC10M	EQU	0A0H		;months X10
RTCY1	EQU	0B0H		;year
RTCY10	EQU	0C0H		;years X10

RDCLK	EQU	10000000B	;read from clock bit
WRTCLK	EQU	01000000B	;write to clock
HLDCLK	EQU	00000010B	;hold clock
;
CENTURY EQU	19H		;BCD FOR CENTURY #19
;
;  The following sets up the 8255 for addressing
;  It just happens to be called with each clock read, but need not
;  be re-initialized each time, your pleasure.	It runs fine like this
;
TIME:	MVI	A,MODSET+MODA0+MODB0+PRTAIN+PRTBOT+PRTCUO+PRTCLO
	OUT	RTCCTL
;
RDTIM:	MVI	L,RTCH1 	;24 hour format, lower hours byte here
	MVI	A,RTCH10	;upper hours byte here
	CALL	RDRTC		;get it
	ANI	3		;mask off extraneous bits
	CALL	RDBCD1		;return value as BCD
	STA	RTCBUF		;save it
	CALL	BCDBIN		;convert it to binary
	STA	CCHOUR		;stuff it in here for BYE5xx to use
	MVI	H,RTCM10	;upper minutes byte here
	MVI	L,RTCM1 	;lower minutes byte here
	CALL	RDBCD		;convert to BCD
	STA	RTCBUF+1	;save it
	CALL	BCDBIN		;convert to binary
	STA	CCMIN		;stuff it here for BYE5xx to use
	MVI	H,RTCS10	;upper seconds byte here
	MVI	L,RTCS1 	;lower seconds byte here
	CALL	RDBCD		;convert to BCD
	STA	RTCBUF+2	;save it
	MVI	H,RTCY10	;upper years byte here
	MVI	L,RTCY1 	;lower years byte here
	CALL	RDBCD		;covert to BCD
	STA	RTCBUF+4	;save it
	MVI	H,RTC10M	;upper months byte here
	MVI	L,RTC1M 	;lower months byte here
	CALL	RDBCD		;convert...
	STA	RTCBUF+5	;save....
	MVI	H,RTCD10	;upper days byte....
	MVI	L,RTCD1 	;lower....
	CALL	RDBCD		;convert....
	STA	RTCBUF+6	;save....
	MVI	A,CENTURY	;get century value (19xx)
	STA	RTCBUF+3	;save....
	RET			;whew....
;
RDRTC:
	OUT	RTCPTB		;output address to clock
	MVI	A,RDCLK		;turn on for read
	OUT	RTCPTC		;data comes back all at once
	IN	RTCPTA		;read data from clock
	PUSH	PSW
	XRA	A
	OUT	RTCPTC		;turn off read
	POP	PSW
	RET
;
RDBCD:	MOV	A,H
	CALL	RDRTC
;
RDBCD1: RLC ! RLC ! RLC ! RLC
	MOV	H,A
	MOV	A,L
	CALL	RDRTC
	ADD	H
	RET
;.
;. End
;...............
	