; =============================================================================
;
;                           Litos8 CMOS memory driver
;
; =============================================================================

; -----------------------------------------------------------------------------
; Exported user functions (4):
;	CMOSGetByte - read byte from CMOS memory
;	CMOSSetByte - write byte to CMOS memory
;	CMOSGetData - read data from CMOS memory
;	CMOSSetData - write data to CMOS memory
; -----------------------------------------------------------------------------

		CODE_SECTION

; -----------------------------------------------------------------------------
;                         Read byte from CMOS memory
; -----------------------------------------------------------------------------
; INPUT:	AL = index (0 to 127)
; OUTPUT:	AL = data
; -----------------------------------------------------------------------------

CMOSGetByte:	out	CMOS_INDEX,al	; set index of the byte
		SHORT_DELAY		; short delay
		in	al,CMOS_DATA	; read byte from the CMOS
		ret

; -----------------------------------------------------------------------------
;                       Write byte to CMOS memory
; -----------------------------------------------------------------------------
; INPUT:	AL = data
;		DL = index (0 to 127)
; -----------------------------------------------------------------------------

CMOSSetByte:	xchg	ax,dx		; AL <- index, DL <- data
		out	CMOS_INDEX,al	; set index of the byte
		SHORT_DELAY		; short delay
		xchg	ax,dx		; AL <- data, DL <- index
		out	CMOS_DATA,al	; write byte into CMOS
		ret

; -----------------------------------------------------------------------------
;                       Read data from CMOS memory
; -----------------------------------------------------------------------------
; INPUT:	AL = start index (0 to 127)
;		BX = destination buffer
;		CL = number of bytes (0 to 128)
; -----------------------------------------------------------------------------

; ------------- push registers

CMOSGetData:	push	ax		; push AX
		push	bx		; push BX
		push	cx		; push CX

; ------------- read data from CMOS

		mov	ch,0		; CX = number of bytes
		jcxz	CMOSGetData8	; no data
CMOSGetData4:	out	CMOS_INDEX,al	; set index of the byte
		xchg	ah,al		; AH <- index of the byte
		SHORT_DELAY		; short delay
		in	al,CMOS_DATA	; read byte from the CMOS
		mov	[bx],al		; store data into buffer
		inc	bx		; BX <- increase pointer
		xchg	al,ah		; AL <- index of the byte
		loop	CMOSGetData4	; next byte

; ------------- pop registers

CMOSGetData8:	pop	cx		; pop CX
		pop	bx		; pop BX
		pop	ax		; pop AX
		ret

; -----------------------------------------------------------------------------
;                       Write data to CMOS memory
; -----------------------------------------------------------------------------
; INPUT:	AL = start index (0 to 127)
;		BX = source buffer
;		CL = number of bytes (0 to 128)
; -----------------------------------------------------------------------------

; ------------- push registers

CMOSSetData:	push	ax		; push AX
		push	bx		; push BX
		push	cx		; push CX

; ------------- write data to CMOS

		mov	ch,0		; CX = number of bytes
		jcxz	CMOSSetData8	; no data
CMOSSetData4:	out	CMOS_INDEX,al	; set index of the byte
		xchg	ah,al		; AH <- index of the byte
		SHORT_DELAY		; short delay
		mov	al,[bx]		; data from buffer
		out	CMOS_DATA,al	; write byte to CMOS
		inc	bx		; BX <- increase pointer
		xchg	al,ah		; AL <- index of the byte
		loop	CMOSSetData4	; next byte

; ------------- pop registers

CMOSSetData8:	pop	cx		; pop CX
		pop	bx		; pop BX
		pop	ax		; pop AX
		ret
