; Program ....: DBASTIME.ASM
; Author .....: Gary Conway
; Created ....: 7 Feb. 1986
; Update .....:
; Notice .....: Copyright 1986 Infinity Design Concepts

;
;

ESC	EQU	1BH
CLS	EQU	1Ah
LF	EQU	0Ah
CR	EQU	0Dh


ORG	43000		;put it in a safe spot

; ---------------------------------------------
;
;jump table
;
	JMP	MENU	;draw menu lines
	JMP	CLOCK	;clock routine
;
;spares
;
	JMP	0
	JMP	0
	JMP	0
	JMP	0
	JMP	0
	JMP	0
; ---------------------------------------------

;
;the following variables can be read with PEEK() in dBASE
;they are valid after a call to CLOCK
;

MEMCON:
THOURS:		DS	2	; +0,+1 = HOURS
TMINUTES:	DS	2	; +2,+3 = MINUTES
TMONTH:		DS	2	; +4,+5 = MONTH
TDAY:		DS	2	; +6,+7 = DAY
TYEAR:		DS	2	; +8,+9 = YEAR
AMPM:   	DS 	1	; 1 = PM, 0 = AM
		DS	20	;spare

;
;draw menu lines
;

MENU:	LXI	H,MENUTBL
	CALL	PRTABLE
	RET



PRTABLE:
;
; This routine will print lines from a table in the following format
; V1,H1,V2,H2
; HL points to the table at entry
; the last byte of the table must be FF


	DCX	H
PTBL1:	LXI	D,PLINE +1
	MVI	C,4
PTLP:	INX	H
	INX	D
	MOV	A,M
	CPI	0FFH
	RZ
	STAX	D
	DCR	C
	JNZ	PTLP
	PUSH	H
	CALL	ILPRT
PLINE:	DB	ESC,'L',0,0,0,0,00
	POP	H
	JMP	PTBL1

;
;in-line print routine
;

ILPRT:	XTHL
	MOV	A,M
	INX	H
	XTHL
	ORA	A
	RZ
	PUSH	H
	MOV	E,A
	CALL	CO
	POP	H
	JMP	ILPRT
CO:	PUSH B! PUSH D! PUSH H
	MOV	E,A
	MVI	C,2
	CALL	5
	POP H! POP D! POP B
	RET



MENUTBL:	
	DB	36,32,36,190	;top line
	DB	46,32,46,190	;second line
	DB	36,32,88,32	;left line
	DB	36,190,88,190	;right line
	DB	88,32,88,190	;lower line
	DB	0FFH

;
;clock routine
;

CLOCK:



; Ports for NS-MM58167A National Clock Chip
; -----------------------------------------
;      PORT
; 34D	22H	RTCS - Status Port     R/W
; 36D	24H	RTCD - Data Port       R/W
; 32D	20H	RTCA - Attribute Port  R/O
; -----------------------------------------
;

;
;
			; initialize clock
	mvi	a,15
	out	34	; send to clock init port

; THE RETURNED VALUES ARE IN BCD AND MUST BE CONVERTED 
; TO DECIMAL OR SOMETHING IN ORDER TO BE USED


; ***************************************************
; ----------    GET HOURS CODE   ------------    ****
; ***************************************************
HOURS:
	mvi	a,0	; 0 is code for AM
	sta	ampm
	mvi	a,04	; put 4 in A
	out	32	; send to port 32
	in	36	; get hours from port 36
	mov	e,a	; store code temp in E
	in	22h	; check status port
	cpi	01h	; if status bit D0 is 1 => bad read
	jz	HOURS
	mov	a,e	; recall stored code
	cpi	20h	; test for  8 oclock pm
	jz	OCLOCK8
	cpi	21h	; test for 9 oclock pm
	jz	OCLOCK8
	cpi	22h	; test for 10 oclock pm
	jz	OCLOCK10
	cpi	23h	; test for 11 oclock pm
	jz	OCLOCK10
	cpi	00h	; test for 00h
	jnz	pass
	mvi	a,12h	; if zero => make 12h
	jmp	store	; go store
PASS:
	cpi	12h	;
	jm	store	; if < 12h => go store
	cpi	12h
	jz	codepm	; if = 12h => go store
	sui	12h	; subtract 12h if > 12h
CODEPM:
	mov	e,a	; save A into E
	mvi	a,1	; 1 is code for pm
	sta	ampm
	mov	a,e
	jmp	store
OCLOCK8:
	sui	18h	; subtract 18h from 20->21h
			; converts 8->9 oclock to regular time
	jmp	codepm	; go set  the pm code to 1
OCLOCK10:
	sui	12h	; subtract 12h from 22->23
			; converts 10->11 oclock to regular time
	jmp	CODEPM	; go set the pm code to 1
STORE:
	sta	memry   ; store
; ---------------------------------------------------


MIN:
; ***************************************************
; ----------  GET MINUTES CODE   ------------    ****
; ***************************************************
mvi	a,03	; put 3 in A                     ****
out	32	; send to port 32                ****
in	36	; get minutes from port 36       ****
sta	memry+1	; store                          ****
in	22h	; get status code                ****
cpi	01h	; if 1 => bad read               ****
jz	MIN	; if bad => go read again        ****
;                                                ****
; ***************************************************
; ---------------------------------------------------
; ***************************************************


MONTH:
; ***************************************************
; ----------   GET MONTHS CODE   ------------    ****
; ***************************************************
mvi	a,07	; put 7 in A                     ****
out	32	; send to port 32                ****
in	36	; get months from port 36        ****
sta	memry+2	; store                          ****
in	22h	; get status code                ****
cpi	01h	; if 1 => bad read               ****
jz	MONTH	; if bad => go read again        ****
;                                                ****
; ***************************************************
; ---------------------------------------------------
; ***************************************************


DAYS:
; ***************************************************
; ----------     GET DAYS CODE   ------------    ****
; ***************************************************
mvi	a,06	; put 6 in A                     ****
out	32	; send to port 32                ****
in	36	; get days from port 36          ****
sta	memry+3	; store                          ****
in	22h	; get status code                ****
cpi	01h	; if 1 => bad read               ****
jz	DAYS	; if bad => go read again        ****
;                                                ****
; ***************************************************
; ---------------------------------------------------
; ***************************************************

YEARS:
; ***************************************************
; ---------------   GET YEARS CODE   ---------   **** 
; ***************************************************
                                                 ****
mvi	a,09	; put 9 in A                     ****
out	32	; send to port 32                ****
in	36	; get years from port 36         ****
sta	memry+4	; store                          ****
in	22h	; get status code                ****
cpi	01h	; if 1 => bad read               ****
jz	YEARS	; i f1 => go read again          ****
;                                                ****
; ***************************************************
; ---------------------------------------------------
; ***************************************************


WKDAYS:
; ***************************************************
; ---------- GET WEEKDAYS CODE   ------------    ****
; ***************************************************
mvi	a,05	; put 5 in A                     ****
out	32	; send to port 32                ****
in	36	; get weekdays from port 36      ****
sta	memry+5	; store                          ****
in	22h	; get status code                ****
cpi	01h	; if 1 => bad read               ****
jz	WKDAYS  ; if 1 => go read again          ****
;                                                ****
; ***************************************************
; ---------------------------------------------------
; ***************************************************

; This next little piece of code is due to the fact that
; Kaypro does not utilize the power up/down functions of
; the clock chip correctly, and sometimes during this
; transition period, the clock can become disrupted. If
; we leave the chip pointing to hundredth's of seconds
; then if there is a time change during transition, it won't
; make much difference.
;
; ---------- Leave PIO Pointing to  hundredth's of seconds
;
	mvi	a,01
	out	20h
; ---------------------------------------------------------

; ****************************************
;      CONVERT FROM BCD TO DECIMAL
; ****************************************
CONVERT:
	lxi	d,memry
	lxi	h,memcon
	mvi	c,6
LOOP:
	ldax	d
	rar! rar! rar! rar!
	ani	0Fh	; zero high order nibble 
	adi	30h	; convert to ASCII
	mov	m,a	; and store to memcon
	ldax	d	; reload char for low order nibble conversion
	ani	0Fh	; zero high order nibble
	inx	h
	adi	30h	; convert to ASCII
	mov	m,a	; and store to memcon
	inx	d	; increment source pointer
	inx	h	; increment dest. pointer
	dcr	c	; decrement loop ctr.
	jnz	loop

	RET

;
; =============  VARIABLE AREA  =================
memry:	DS 6
; ===============================================
;

END
