;
;==================================================================================================
; EZ80 ON-CHIP RTC DRIVER
;==================================================================================================
;
EZ80RTC_BUFSIZ	.EQU	6	; SIX BYTE BUFFER (YYMMDDHHMMSS)
;
; RTC DEVICE INITIALIZATION ENTRY

EZ80RTC_INIT:
	; display driver install message
	; delegate init function to firmware
	; install dispatcher
	; dispatch local routine that delgates to firmware routines

	LD	A, (RTC_DISPACT)	; RTC DISPATCHER ALREADY SET?
	OR	A			; SET FLAGS
	RET	NZ			; IF ALREADY ACTIVE, ABORT

	CALL	NEWLINE			; FORMATTING
	CALL	PRTSTRD
	.TEXT	"EZ80 RTC: $"

	EZ80_RTC_INIT()
	JR	Z, RTC_POWERED

	PUSH	AF
	CALL	PRTSTRD
	.TEXT	"NOT POWERED$"
	POP	AF
	RET

RTC_POWERED:
	CALL	PRTSTRD
	.TEXT	"POWERED $"

	; DISPLAY CURRENT TIME
	LD	HL, EZ80RTC_BCDBUF_EXT	; POINT TO BCD BUF EXTENDED
	EZ80_RTC_GET_TIME()

	LD	HL, EZ80RTC_BCDBUF	; POINT TO BCD BUF
	CALL	PRTDT			; DISPLAY THIS TIME
;
	LD	BC, EZ80RTC_DISPATCH
	CALL	RTC_SETDISP
;
	XOR	A			; SIGNAL SUCCESS
	RET

;
; RTC DEVICE FUNCTION DISPATCH ENTRY
;   A: RESULT (OUT), 0=OK, Z=OK, NZ=ERR
;   B: FUNCTION (IN)
;
EZ80RTC_DISPATCH:
	LD	A,B			; GET REQUESTED FUNCTION
	AND	$0F			; ISOLATE SUB-FUNCTION
	JP	Z,EZ80RTC_GETTIM		; GET TIME
	DEC	A
	JP	Z,EZ80RTC_SETTIM		; SET TIME
	DEC	A
	JP	Z,EZ80RTC_GETBYT		; GET NVRAM BYTE VALUE
	DEC	A
	JP	Z,EZ80RTC_SETBYT		; SET NVRAM BYTE VALUE
	DEC	A
	JP	Z,EZ80RTC_GETBLK		; GET NVRAM DATA BLOCK VALUES
	DEC	A
	JP	Z,EZ80RTC_SETBLK		; SET NVRAM DATA BLOCK VALUES
	DEC	A
	JP	Z,EZ80RTC_GETALM		; GET ALARM
	DEC	A
	JP	Z,EZ80RTC_SETALM		; SET ALARM
	DEC	A
	JP	Z,EZ80RTC_DEVICE		; REPORT RTC DEVICE INFO
	SYSCHKERR(ERR_NOFUNC)
	RET
;
; RTC GET TIME
;   A: RESULT (OUT), 0=OK, Z=OK, NZ=ERR
;   HL: DATE/TIME BUFFER (OUT)
; BUFFER FORMAT IS BCD: YYMMDDHHMMSS
; 24 HOUR TIME FORMAT IS ASSUMED
;
EZ80RTC_GETTIM:
	PUSH	HL
	LD	HL, EZ80RTC_BCDBUF_EXT	; POINT TO BCD BUF EXTENDED
	EZ80_RTC_GET_TIME()		; (HL) <- TIME

	LD	A, BID_BIOS		; COPY FROM BIOS BANK
	LD	(HB_SRCBNK), A		; SET IT
	LD	A, (HB_INVBNK)		; COPY TO CURRENT USER BANK
	LD	(HB_DSTBNK), A		; SET IT
	LD	HL, EZ80RTC_BCDBUF	; SOURCE ADR
	POP	DE			; DEST ADR
	LD	BC, EZ80RTC_BUFSIZ	; LENGTH
	CALL	HB_BNKCPY		; COPY THE CLOCK DATA

	XOR	A			; SIGNAL SUCCESS
	RET
;
;
; RTC SET TIME
;   A: RESULT (OUT), 0=OK, Z=OK, NZ=ERR
;   HL: DATE/TIME BUFFER (IN)
; BUFFER FORMAT IS BCD: YYMMDDHHMMSSWW
; 24 HOUR TIME FORMAT IS ASSUMED
;
EZ80RTC_SETTIM:
	; COPY TO BCD BUF
	LD	A,(HB_INVBNK)		; COPY FROM CURRENT USER BANK
	LD	(HB_SRCBNK),A		; SET IT
	LD	A,BID_BIOS		; COPY TO BIOS BANK
	LD	(HB_DSTBNK),A		; SET IT
	LD	DE,EZ80RTC_BCDBUF	; DEST ADR
	LD	BC,EZ80RTC_BUFSIZ	; LENGTH
	CALL	HB_BNKCPY		; COPY THE RPC DATA

	LD	HL, EZ80RTC_BCDBUF_EXT	; POINT TO BCD BUF EXTENDED
	LD	(HL), $20		; CENTURY NOT SUPPORT BY HBIOS
	EZ80_RTC_SET_TIME()		; (HL) -> SYSTEM TIME

	XOR	A			; SIGNAL SUCCESS
	RET
;
; RTC GET NVRAM BYTE
;   C: INDEX
;   E: VALUE (OUTPUT)
;   A:0 IF OK, ERR_RANGE IF OUT OF RANGE
;
EZ80RTC_GETBYT:
	SYSCHKERR(ERR_NOTIMPL)
	; XOR	A			; SIGNAL SUCCESS
	RET				; AND RETURN


; RTC SET NVRAM BYTE
;   C: INDEX
;   E: VALUE
;   A:0 IF OK, ERR_RANGE IF OUT OF RANGE
;
EZ80RTC_SETBYT:
	SYSCHKERR(ERR_NOTIMPL)
	; XOR	A			; SIGNAL SUCCESS
	RET

EZ80RTC_GETBLK:
EZ80RTC_SETBLK:
EZ80RTC_GETALM:
EZ80RTC_SETALM:
	SYSCHKERR(ERR_NOTIMPL)
	RET
;
; REPORT RTC DEVICE INFO
;
EZ80RTC_DEVICE:
	LD	D, RTCDEV_EZ80		; D := DEVICE TYPE
	LD	E, 0			; E := PHYSICAL DEVICE NUMBER
	LD	HL, 00			; H := 0, DRIVER HAS NO MODES,  L := 0, NO I/O ADDRESS
	XOR	A			; SIGNAL SUCCESS
	RET


; REGISTER EXTRACTED VALUES
;
EZ80RTC_BCDBUF_EXT:
EZ80RTC_CN	.DB	20	; CENTURY
EZ80RTC_BCDBUF:
EZ80RTC_YR	.DB	24
EZ80RTC_MO	.DB	01
EZ80RTC_DT	.DB	01
EZ80RTC_HH	.DB	00
EZ80RTC_MM	.DB	00
EZ80RTC_SS	.DB	00
