	.TITLE	SWATCH	- Software Stop Watch.
	.IDENT	/V01.01/
	.NLIST	BEX
	.ENABL	LC

	.SBTTL	GEN Control.

;+
;
; MAC:		ob:swatch,mp:swatch=swatch
;
; TKB:		swatch/-cp,mp:swatch/-sp=swatch
;		/
;		units=1
;		pri=150
;		task=...swa
;		//
;
;-

	.PAGE
	.SBTTL	XDT Header

;+
;			--==<< SWATCH >>==--
;
; 		Copyright (C) 1988 by XDT Computer Consultants
;			      All Rights Reserved
;
;		This software  may not be  copied or duplicated
;		in any form or by any means without the written
;		permission of XDT Computer Consultants.
;
; Author:	Adrian Bottoms
;		9 The Square
;		Keyworth
;		Nottngham	NG12 5JT
;		Great Britain	Tel: (+44) (6077) 5295
; Date:		11-Sep-86
; Version:	V01.00
;
; Purpose:	This is a simple programme which takes a command line
;		and spawns it to the CLI and times it's execution.
;
; Modified:	AB
; Date:		20-Jul-88
; Version:	V01.01
; Edit:		AB001
; Purpose:	Make SWATCH give the command line to the users CLI rather than
;		MCR and add MID support.
;
; Usage:	SWATCH command_line
;
; Note:	1)	It might be beneficial to have the programme that SWATCH
;		is starting up FIXed in memory for accurate execution timings.
;		If not then we also time the task activation overhead.
;
;-

	.MCALL	DIR$,GTIM$S,SPWN$S,GMCR$,QIOW$S,EXIT$S,ALUN$S,WTSE$S

TILUN	= 1				; Lun for TI: I/O
FLAG	= 2				; Event flag for SPWN$S

	.PSECT	PURE	D,RO,LCl,REL,CON

MCR:	.RAD50	/CLI.../							; AB001
MSG:	.ASCII	/SWATCH -- Timing : /
MSGL	= . - MSG

	.PSECT	IMPURE	D,RW,LCL,REL,CON

GMCR:	GMCR$				; Get MCR command line
CMDLIN	= GMCR+G.MCRB			; Address of command line

STARTT:	.BLKW	10			; Start time
ENDT:	.BLKW	10			; End time
DIFFT:	.BLKW	10			; Difference.

OPBUF:	.ASCII	<15><12><7>/SWATCH -- Timing finished./<15><12><12>
	.ASCII	/  End : /
ETIM:	.ASCII	/hh:mm:ss.tt/<15><12>
	.ASCII	/Start : /
STIM:	.ASCII	/hh:mm:ss.tt/<15><12>
	.ASCII	/        -----------/<15><12>
	.ASCII	/        /
DTIM:	.ASCII	/hh:mm:ss.tt/<15><12>
	.ASCII	/        -----------/<15><12><12>
	.ASCII	/Copyright (C) 1988 XDT Computer Consultants./<15><12>
OPBUFL	= . - OPBUF

TMPBUF:	.BLKB	5			;

	.EVEN

	.PSECT	CODE	I,RO,LCL,REL,CON

SWATCH:					; Entry point
	ALUN$S	#TILUN,#"TI,#0		; Assign a lin to TI:
	DIR$	#GMCR			; Get a command line
	MOV	@#$DSW,R1		; Got one ??
	BGT	2$			; Yes - Jump
	EXIT$S				; No - Quit

2$:	MOV	#CMDLIN,R0		; Point to command line

4$:	CMPB	#' ,(R0)+		; Is this a <SPACE> ??
	BEQ	6$			; Yes - BR.
	SOB	R1,4$			; No - Look again.
	EXIT$S				; No space - just exit.

6$:	DEC	R1			; Allow for <SPACE>.
	; Tell them we are about to start.
	QIOW$S	#IO.WVB,#TILUN,#TILUN,,,,<#MSG,#MSGL,#'$>
	QIOW$S	#IO.WVB,#TILUN,#TILUN,,,,<R0,R1,#'+>
	SPWN$S	#MCR,,,,,#FLAG,,,R0,R1	; Issue the command.
	BCC	8$
	IOT

8$:	GTIM$S	#STARTT			; Get the start time.
	WTSE$S	#FLAG			; Wait for command to finish.
	GTIM$S	#ENDT			; Get finish time

	; Output end time.
	MOV	#ENDT,R5		; Point to the time data.
	MOV	#ETIM,R4		; Point to output buffer.
	CALL	CNVTIM			; Go convert to ASCII

	; Output start time.
	MOV	#STARTT,R5		; Point to the time data.
	MOV	#STIM,R4		; Point to output buffer.
	CALL	CNVTIM			; Go convert to ASCII.

	; Save brain ache - output the difference.
	CALL	DODIFF			; Calculate the differences.
	MOV	#DIFFT,R5		; Point to the time data.
	MOV	#DTIM,R4		; Point to output buffer.
	CALL	CNVTIM			; Go convert the difference.

	; Now print that lot.
	QIOW$S	#IO.WVB,#TILUN,#TILUN,,,,<#OPBUF,#OPBUFL,#0>

EXIT:	EXIT$S

	.PAGE

DODIFF:	; Calculate the difference in start and end time.
	MOV	#STARTT,R0		; Point to start time vector.
	MOV	#ENDT,R1		; Point to end time vector.
	MOV	#DIFFT,R2		; Point to difference vector.
	MOV	#10,R3			; 10 words to copy.

2$:	MOV	(R1)+,(R2)+		; Copy end to diff.   <--+
	SOB	R3,2$			; -----------------------+

	MOV	#DIFFT,R2		; Point to difference vector.

	; Do the ticks.
	SUB	G.TICT(R0),G.TICT(R2)	; Get difference.
	BGE	4$			; Br if no borrow.

	ADD	G.TICP(R2),G.TICT(R2)	; Else adjust.
	INC	G.TISC(R0)		; And pay back borrow.

4$:	; Do the seconds.
	SUB	G.TISC(R0),G.TISC(R2)	; Get difference.
	BGE	6$			; Br if no borrow.

	ADD	#60.,G.TISC(R2)		; Else adjust seconds.
	INC	G.TIMI(R0)		; And pay back borrow.

6$:	; Do the minutes.
	SUB	G.TIMI(R0),G.TIMI(R2)	; Get difference.
	BGE	8$			; Br if no borrow.

	ADD	#60.,G.TIMI(R2)		; Else adjust minutes.
	INC	G.TIHR(R0)		; And pay back borrow.

8$:	; Do the hours
	SUB	G.TIHR(R0),G.TIHR(R2)	; Get the difference.
	BGE	10$			; Br if no borrow.

	ADD	#24.,G.TIHR(R0)		; Else adjust hours.

10$:	RTS	PC

	.PAGE

CNVTIM:	; Convert to time vector to ASCII in form HH:MM:SS.TT
	MOV	G.TIHR(R5),R1		; Get the hour.
	CALL	2$			; Go convert it.
	MOVB	#':,(R4)+		; Terminate hours.
	MOV	G.TIMI(R5),R1		; Get minutes.
	CALL	2$			; Go convert it.
	MOVB	#':,(R4)+		; Terminate minutes.
	MOV	G.TISC(R5),R1		; Get seconds.
	CALL	2$			; Go convert it.
	MOVB	#'.,(R4)+		; Terminate seconds.
	MOV	G.TICT(R5),R2		; Get no. of ticks.
	CLR	R3			; Set up for double word DIV.
	MUL	#100.,R2		; Convert to decimal ---
	DIV	G.TICP(R5),R2		; --- fraction.
	MOV	R2,R1			; Put it where it can be converted.
	CALL	2$			; Go convert it.
	RTS	PC			; All done.

2$:	; Convert a word into 2 bytes of decimal.
	MOV	#TMPBUF,R0		; Point to temporary one.
	MOV	SP,R2			; Want lead zeros.
	CALL	$CBDMG			; Convert to decimal.
	MOVB	TMPBUF+3,(R4)+		; Get 1st character.
	MOVB	TMPBUF+4,(R4)+		; Get 2nd character.
	RTS	PC

	.END	SWATCH
