;TP4.MAC contains primitive functions of TRAC80
;Look under entry points for names.
;08/05/86

	PAGE 80
	.Z80
	TITLE TP4

	ENTRY PFTM

	SUBTTL TM

PFTM:
	EXTERNAL CHAR,BASIS
	EXTERNAL ARGST,$VALST
	EXTERNAL SCHEM1,CHECK,DIVA,CVRTBA
	INCLUDE PSTFC.INC

;The following program area can be used for a user patch
;that loads the system time as 8-bit binary values into addresses
;PFTMHH, PFTMMM, and PFTMSS.
;These bytes serve as an interface to the subsequent regular subroutine 
;that encodes the hours, minutes and seconds into VALST.
;VALST will have the format hh_mm_ss. _ denotes the first character of the 
;first argument string (ARGST) which serves as a separator. It may be 
;the null character, thus leading to the format hhmmss.
;If any of the bytes PFTMHR, PFTMMM or PFTMSS is negative (intial value = FFH),
;it will not contribute to the value of TM. Neither will the corresponding 
;seperator.

HH$	EQU 0FEFCH	;address of hour-byte in BIOS
MM$	EQU 0FEFBH	;address of minute-byte
SS$	EQU 0FA35H	;address of (60-seconds)-byte

	DI		;disable interrupt for correct read-out
	LD HL,HH$	;get hour-byte
	LD A,(HL)
	LD (PFTMHH),A
	LD HL,MM$	;get minute-byte
	LD A,(HL)
	LD (PFTMMM),A
	LD HL,SS$	;get second-byte
	LD A,(HL)
	NEG
	ADD A,60
	LD (PFTMSS),A
	EI		;enable interrupt after read-out

;End of user patch area.
;The regular encoding subroutine begins here.

	XOR A		;get separator
	LD (CHAR),A	;CHAR.=0
	CALL SCHEM1	;IF ARGST>$ALIST GOTO PFTM1
	JP C,PFTM1
	LD HL,(ARGST)	;CHAR.=c(ARGST)
	LD A,(HL)
	LD (CHAR),A
PFTM1:	LD HL,8		;check space for maximum value format
	CALL CHECK

	LD A,(PFTMHH)	;hours
	OR A
	JP M,PFTM2	;skip hours if not provided
	CALL PSTFXX	;convert hour byte and append at VALST
	LD A,(CHAR)
	OR A
	JP Z,PFTM2
	PSTFC CHAR,VALST	;append separator byte (if not null)
PFTM2:	LD A,(PFTMMM)	;minutes
	OR A
	JP M,PFTM3	;skip minutes if not provided
	CALL PSTFXX	;convert minute byte and append at VALST
	LD A,(CHAR)
	OR A
	JP Z,PFTM3
	PSTFC CHAR,VALST	;append separator byte (if not null)
PFTM3:	LD A,(PFTMSS)	;seconds
	OR A
	JP M,PFTM4	;skip seconds if not provided
	CALL PSTFXX	;convert second-byte and append at VALST

PFTM4:	RET

PFTMHH:	DB 0FFH		;hours (8-bit binary)
PFTMMM:	DB 0FFH		;minutes
PFTMSS:	DB 0FFH		;seconds

PSTFXX:	;converts the binary in A into two hour/minute/second ASCII-bytes 
	;and postfixes them at VALST
	LD H,0
	LD L,A
	LD A,(BASIS)
	CALL DIVA
	EX DE,HL
	LD HL,($VALST)
	LD A,E		;quotient
	CALL CVRTBA
	INC HL
	LD (HL),A
	LD A,D		;residuum
	CALL CVRTBA
	INC HL
	LD (HL),A
	LD ($VALST),HL
	RET

	END
