;*******************************************;
;* EINSTEIN REAL TIME CLOCK ACCESS PROGRAM *;
;* COPYRIGHT (C) 1980/85  A.J.CORNISH BSc. *;
;*	     Crystal Electronics	   *;
;* Originally written for SHARP MZ80K 1980 *;
;* Adapted for  TATUNG EINSTEIN,  Nov 1985 *;
;*******************************************;

	ORG	0100H

CR	EQU	0DH
LF	EQU	0AH

DOS	EQU	0005H
FDESC	EQU	005CH
STACK	EQU	0300H
TIMBUF	EQU	0FB8CH		; AREA FOR STORING TIME (SAVING TIME?!)

TIME:	LD	(OLDSP),SP	; SAVE STACK FROM DOS MONITOR
	LD	SP,STACK
	LD	HL,FDESC+1
	LD	A,(HL)
	CP	' '
	JR	NZ,SETIME

; NOTHING AFTER TIME, SO WANT TO KNOW THE TIME

RDTIME:	LD	HL,TIMBUF
	LD	DE,TIMTX1
	LD	BC,0
	LDI
	LDI
	INC	DE		; SKIP COLONS
	LDI
	LDI
	INC	DE
	LDI
	LDI
	LD	DE,TIMTXT
FINIS:	CALL	PRMESS
	LD	SP,(OLDSP)	; GO STRAIGHT BACK TO DOS, TO SAVE TIME
	RET	


; DOS PRINT MESSAGE ROUTINE

PRMESS:	PUSH	BC
	PUSH	DE
	PUSH	HL
	LD	C,9
	CALL	DOS
	POP	HL
	POP	DE
	POP	BC
	RET	


; BUILD TIME OUTPUT TEXT HERE

TIMTXT:	DEFB	CR,LF
TIMTX1:	DEFB	'00:00:00',CR,LF,'$'
	DEFS	0005H
OLDSP:	DEFS	0002H


; ROUTINE TO SET THE TIME

SETIME:	PUSH	HL
	LD	HL,INTEXT
	PUSH	HL
	LD	A,'0'
	LD	B,6
SET0:	LD	(HL),A
	INC	HL
	DJNZ	SET0
	LD	(HL),' '
	POP	DE
	POP	HL
	LD	B,6
SET1:	LD	A,(HL)		; FILL INTEXT FROM CMD LINE BUFFER
	CP	' '
	JR	Z,SET2
	CP	'0'
	JR	C,ERR
	CP	'0'+10
	JR	NC,ERR
	LD	(DE),A
	INC	HL
	INC	DE
	DJNZ	SET1
SET2:	LD	HL,INTEXT
	LD	A,(HL)
	CP	'2'
	INC	HL
	JR	NZ,SET3
	LD	A,(HL)		; HOURS CAN'T EXCEED 23!
	CP	'4'
SET3:	JR	NC,ERR
	INC	HL
	LD	A,(HL)		; MINS & SECS CAN'T EXCEED 59!
	CP	'6'
	JR	NC,ERR
	INC	HL
	INC	HL
	LD	A,(HL)
	CP	'6'
	JR	NC,ERR
	LD	HL,INTEXT
	LD	DE,TIMBUF
	LD	BC,6
	LDIR			; COPY TO TIME BUFFER
	JP	RDTIME		; ALL SET, GO BACK AND DISPLAY

ERR:	LD	DE,INTEXT	; ERROR, JUST DO ERROR DISPLAY
	LD	A,'?'
	LD	(INTEXT+6),A
	JP	FINIS


; BUILD INPUT TIME HERE

INTEXT:	DEFB	'000000 ',CR,LF,'$'

	END

