
; BYE500 Time insert
; Author: Joe Wright
; Date: 28 July 1985
; Version: 1.0

; This insert will read the time from the CCS 2805 Wallclock
; and place it at RTCBUF as follows:
;
; Real-Time clock buffer - is organized as HH:MM:SS YYYY/MM/DD
;
;RTCBUF:
;	DB	99H,99H,99H	;HH:MM:SS (BCD 24HR TIME) 00:00:00-23:59:59
;	DB	19H,84H,01H,31H ;YYYY/MM/DD (BCD ISO DATE)
;
; Binary values are also stored for CCHOUR and CCMIN
;
; BYE500 saves and restores registers before/after calling TIME.
;

BASE	EQU	38H		; Base address of CCS 2805 pio

ADATA	EQU	BASE+4		; Port a data
ACMND	EQU	BASE+5		; Port a command
BDATA	EQU	BASE+6		; Port b data
BCMND	EQU	BASE+7		; Port b command

HOLD	EQU	64		; Hold 5832 to set up read/write
WR	EQU	32		; Read 5832
RD	EQU	16		; Write 5832

BSYA	EQU	128		; Address delay, 6 us.
BSYH	EQU	64		; Main clock hold delay, 150 us.

MODE0	EQU	0FH		; Pio channel a output mode
MODE3	EQU	0CFH		; Pio channel b control mode

;
; set port a to output mode
;
TIME:
	MVI	A,MODE0		; Output mode
	OUT	ACMND		; Cha command port
	MVI	A,3		; Disable interrupts
	OUT	ACMND		; Cha command port
;
; set port b to read the clock
;
	MVI	A,MODE3		; Control mode
	OUT	BCMND		; Chb command port
	MVI	A,0CFH		; To read clock
	OUT	BCMND		; Chb command port

	LXI	H,RTCBUF	; Point to real time clock buffer in Bye500

	MVI	A,5		; High order hours
	CALL	TENS
	ANI	30H		; Mask off high bits in high nybble
	MOV	B,A		; Save it for now
	MVI	A,4		; Low order hours
	CALL	ONES
	CALL	BCDBIN		; Convert to binary
	STA	CCHOUR

	MVI	A,3		; High minutes
	CALL	TENS
	MVI	A,2		; Low minutes
	CALL	ONES
	CALL	BCDBIN
	STA	CCMIN

	MVI	A,1		; High seconds
	CALL	TENS
	XRA	A		; Low seconds
	CALL	ONES

	MVI	M,19H		; Declare 20th century
	INX	H		; Point to year

	MVI	A,12		; High year
	CALL	TENS
	MVI	A,11		; Low year
	CALL	ONES

	MVI	A,10		; High month
	CALL	TENS
	MVI	A,9		; Low month
	CALL	ONES

	MVI	A,8		; High date
	CALL	TENS
	MVI	A,7		; Low date
	CALL	ONES
;
	RET			; End of TIME, return to caller

TENS:	CALL	RDCLK		; Get it
	CALL	SHL4		; To high nybble
	MOV	B,A		; Save it a moment
	RET

ONES:	CALL	RDCLK
	ORA	B		; Or in the high nybble
	MOV	M,A		; Save it in memory
	INX	H		; Point to next byte
	RET

SHL4:
	RAL
	RAL
	RAL
	RAL			; Shift low to high nybble
	RET

RDCLK:	ORI	HOLD		; Acc has clock address
	OUT	ADATA
RDC1:	IN	BDATA
	ANI	BSYH
	JNZ	RDC1
	IN	ADATA
	ORI	RD
	OUT	ADATA
RDC2:	IN	BDATA
	ANI	BSYA
	JNZ	RDC2
	IN	BDATA
	ANI	0FH		; Mask hi order
	PUSH	PSW
	XRA	A
	OUT	ADATA
	POP	PSW
	RET
;
; END OF B5-2805.INS
;
