;
;    EXPROF.ASM    06/17/85	W. van Riper
;
;    This routine is for the Osborne Executive and provides rudimentary
;    profiling capability.  It avoids the problems caused by the bank-
;    switching arrangement that makes fooling around with the bios
;    jump table so difficult.  It gets a count from the RTC overflow
;    in port 1ah, which cycles from 1 to 255 in about 4.27 sec.
;    The readout is in clock ticks, each tick being about .017 sec.
;
;    Usage:  from c -	       prof(0);    turns on count
;			       prof(0);    turns off count (count is maintained
;					    internally up to 0ffffh ticks,
;					    which is about 18 min.)
;			       prof(3,1); turns on display with label #3
;
;	     from assembly -   lxi   h,0  toggles count
;			       push  h
;			       call  prof
;
;			       lxi   h,2  turns on display of accumulated
;			       push  h	    time with label #2
;			       lxi   h,1
;			       push  h
;			       call  prof
;
; (remember to take care of the stack when passing parameters in this fashion)
;
	public	prof
;
;
called	db	0
then	ds	1
now	ds	1
total	dw	0
;
prof	pop	b
	pop	h
	push	h
	push	b
	mov	a,h		;if hl is 0 then just display total
	cpi	0
	jnz	about
	mov	a,l
	cpi	1
	jz	showit
;
about	lda	called		;get current status (0 or 1)
	xri	1		; =1 if 0 in (called), =0 if 1 in called
	jz	call2		;jump if this is the second call
	sta	called		;set flag if this is the first time through
;
call1	in	1ah		;get number
	lda	then		;and put in (then) for the next time
	ret
;
call2	in	1ah		;get number
	sta	now		;and put in (now) for safe keeping
;
;
	lda	then		;get old time in a
	mov	b,a		;stash old time in b
	lda	now		;get current time in a
	cmp	b		;new - old
	jp	displ0		;if new > old then go display result
;
	lda	then		;put old in b
	mov	b,a
	mvi	a,255		;get difference between 255 and old
	sub	b
	mov	b,a		;put it in b
	lda	now		;add it to current time for displacement
	add	b
	jmp	displ1
;
displ0	sub	b
displ1	lhld	total
	xchg
	mov	l,a
	mvi	h,0
;
	dad	d
	shld	total
	ret
;
;
showit	call	crlf
	call	print
	db	'routine: ',0
	pop	b
	pop	d
	pop	h
	push	h
	push	d
	push	b
	call	prhl
	call	crlf
;
	call	print
	db	'total time: ',0
	lhld	total
	call	prhl
	call	crlf
	lxi	h,0
	shld	total
	ret
;
;
print	pop	h	;in-line print - see R. CONN's SYSLIB - he wrote
	mov	a,m	; the book on doing this right...
	inx	h	;move pointer
	push	h	;
	ora	a
	rz
	mov	e,a	;send character to console
	mvi	c,2
	call	005
	jmp	print
;
;
crlf	push	b
	push	d
	push	h
	push	psw
	mvi	c,2
	mvi	e,0dh
	call	005
	mvi	c,2
	mvi	e,0ah
	call	005
	pop	psw
	pop	h
	pop	d
	pop	b
	ret
;
;
prhl	push	psw
	push	b
	push	d
	push	h
	mvi	b,1
;
	lxi	d,10000
	call	prhl1
	lxi	d,1000
	call	prhl1
	lxi	d,100
	call	prhl1
	lxi	d,10
	call	prhl1
	mov	a,l
	adi	'0'
	call	chout
	pop	h
	pop	d
	pop	b
	pop	psw
	ret
;
prhl1	mvi	c,0		;prints hl in decimal to console - based
prhl2	mov	a,l		;on Richard Conn's PHLFDC
	sub	e
	mov	l,a	
	mov	a,h	
	sbb	d
	mov	h,a	
	jc	prhl3
	inr	c	
	jmp	prhl2
prhl3	mov	a,l	
	add	e
	mov	l,a	
	mov	a,h	
	adc	d
	mov	h,a	
	mov	a,c	
	ora	a	
	jnz	prhl4
	ora	b	
	rnz
prhl4	mvi	b,0	
	mov	a,c	
	adi	'0'	
;
chout	push	b
	push	d
	push	h
	mvi	c,2
	mov	e,a
	call	005
	pop	h
	pop	d
	pop	b
	ret
vi	b,0	
	mov	a,c	
	adi	'0'	
;
chout	push	b
	push	d
	push	h
	mvi	c,2
	mo