;       SCCSID = @(#)tefti.asm	6.1 90/11/18
;      SCCSID = @(#)tefti.asm	13.2 90/01/25
comment /

The TEFTI card is controlled by doing outs or ins to various ports
according to the counter and function you want to access.  The data
for outs is irrelevant except when setting the pre divide values.

Timer (TEFTI) Card Register Programming Specifications:

Register Access Map:

 		     Write			Read

BASE + 0	Reset Counter 0		Read Counter 0 LS byte
BASE + 1	Start Counter 0		Read Counter 0 MS byte
BASE + 2	Stop Counter 0		Read Counter 1 LS byte
BASE + 3	Snapshot Counter 0	Read Counter 1 MS byte

BASE + 4	Reset Counter 1		Read Counter 2 LS byte
BASE + 5	Start Counter 1		Read Counter 2 MS byte
BASE + 6	Stop Counter 1		
BASE + 7	Snapshot Counter 1	

BASE + 8	Reset Counter 2		
BASE + 9	Start Counter 2		
BASE + 10	Stop Counter 2		
BASE + 11	Snapshot Counter 2	

BASE + 12	Select Predivide

NOTE:	Data is significant only when writing to the Predivide Register.
	All other writes accomplish their desired function without regard
	to specific data.

Predivide Register programming:

	7 6 5 4 3 2 1 0
	          X X X			Counter 0 Predivide (Table 1)
  	    X X X			Counter 1 Predivide (Table 1)
	X X				Counter 2 Predivide (Table 2)

Value written to Predivide Register selects counter resolution as follows:

	Table 1  (Counters 0 and 1)

	Data		    Resolution	       Max Timer Count

	  0		    1 microsec	       65.536 millisec
	  1		    4 microsec	      262.140 millisec
	  2		   16 microsec	       1.0486 seconds
	  3		   64 microsec	       4.1943 seconds
	  4		  256 microsec	      16.7770 seconds
	  5		1.024 millisec	      67.1090 seconds
	  6		8.196 millisec	       8.9478 minutes
	  7	       32.784 millisec	      35.7910 minutes

	Table 2  (Counter 2)

	Data		    Resolution	       Max Timer Count

	  0		    4 microsec	      262.140 millisec
	  1		   32 microsec	       2.0971 seconds
	  2		CNT1_out * 65,536      4.4597 years (1)
	  3		CNT2_out * 65,536      4.4597 years (2)

(1) Counter 0 predivide programmed with 7
(2) Counter 1 predivide programmed with 7


The general procedure when using the TEFTI board timers is:

	1. Stop
	2. Clear
	3. Set pre divide values to fix the time per count
	4. Start
	5. Stop
	6. Latch
	7. Read

	8a. Clear and return to 4 if you want to start timing again with
	    the same pre divide values.

	or

	8b. return to 4 if you want to add to the current previous time.

	or

	return to 3 if you want to change the count rates.


end comment /

;---------------------------------------------------------------;
;			TEFTI PROCS				;
;---------------------------------------------------------------;

title	Timer Routines

.MODEL SMALL, PASCAL

BASE	equ	220h
SEL_DIV equ	BASE+12
CLR	equ	0
GO	equ	1
HALT	equ	2
SNAP	equ	3

; this macro provides for entries to be made into the history_buf on
; 4 byte boundaries.  The buffer is circular and the next entry position
; is marked with a -1.

RESTS equ 1

rest macro
if RESTS
	jmp	$+2
endif
endm


;-----------------------------------------------------------------------

;-----------------------------------------------------------------------

.CODE



;-----------------------------------------------------------------------;
; ClearCounter                                                          ;
;  Outs 0 to port (BASE + CLR + (cntr * 4))				;
;                                                                       ;
; Arguments:                                                            ;
;	cntr = 0, 1 or 2						;
; Returns:                                                              ;
;       nothing                                                         ;
; Alters:                                                               ;
;	AX, DX								;
; Calls:                                                                ;
;       nothing                                                         ;
;                                                                       ;
; History:                                                              ;
;                                                                       ;
;  Fri Sep 12, 1986 04:00:40p  -by-  Tom Bates  [tdb]		        ;
;-----------------------------------------------------------------------;

PUBLIC ClearCounter
align 4
ClearCounter	PROC	cntr:WORD

	mov	dx,cntr
	shl	dx,1
	shl	dx,1		; now DX = cntr * 4
	add	dx,BASE+CLR
	xor	al,al
	out	dx,al
	rest

	ret

ClearCounter	ENDP


PUBLIC ClearInt
align 4
ClearInt    PROC
	cli
	ret
ClearInt    ENDP


PUBLIC SetInt
align 4
SetInt	PROC
	sti
	ret
SetInt	ENDP

;-----------------------------------------------------------------------;
; DisableInterrupts							;
;  Outs 0FFh to port 21h						;
;                                                                       ;
; Arguments:                                                            ;
;	none								;
; Returns:                                                              ;
;	AX = previous mask value					;
; Alters:                                                               ;
;	nothing 							;
; Calls:                                                                ;
;       nothing                                                         ;
;                                                                       ;
; History:                                                              ;
;                                                                       ;
;  11-Jan-1988 19:10:20 -by- Bob Grudem [bobgru]			;
;-----------------------------------------------------------------------;

PUBLIC	DisableInterrupts
align 4
DisableInterrupts   PROC

	in	al,21h		;read 8259 mask register
	mov	ah,al		;save value in AH
	mov	al,0FFh 	;set up to disable all hardware interrupts
	out	21h,al
	mov	al,ah		;make return value a word
	xor	ah,ah

	ret
DisableInterrupts   ENDP

;-----------------------------------------------------------------------;
; EnableInterrupts							;
;  Outs imask to port 21h						;
;                                                                       ;
; Arguments:                                                            ;
;	imask = mask value to output					;
; Returns:                                                              ;
;	nothing 							;
; Alters:                                                               ;
;	AL								;
; Calls:                                                                ;
;       nothing                                                         ;
;                                                                       ;
; History:                                                              ;
;                                                                       ;
;  11-Jan-1988 19:10:20 -by- Bob Grudem [bobgru]			;
;-----------------------------------------------------------------------;

PUBLIC	EnableInterrupts
align 4
EnableInterrupts    PROC    imask:WORD

	mov	al,byte ptr (imask)
	out	21h,al

	ret
EnableInterrupts    ENDP


;-----------------------------------------------------------------------;
; StartCounter                                                          ;
;  Outs 0 to port (BASE + GO + (cntr * 4))				;
;                                                                       ;
; Arguments:                                                            ;
;	cntr = 0, 1 or 2						;
; Returns:                                                              ;
;       nothing                                                         ;
; Alters:                                                               ;
;	AX, DX								;
; Calls:                                                                ;
;       nothing                                                         ;
;                                                                       ;
; History:                                                              ;
;                                                                       ;
;  Fri Sep 12, 1986 04:00:40p  -by-  Tom Bates  [tdb]		        ;
;-----------------------------------------------------------------------;

PUBLIC	StartCounter
align 4
StartCounter	PROC	cntr:WORD

	mov	dx,cntr
	shl	dx,1
	shl	dx,1		; now DX = cntr * 4
	add	dx,BASE+GO
	xor	al,al
	out	dx,al
	rest

	ret
StartCounter	ENDP

;-----------------------------------------------------------------------;
; StopCounter                                                           ;
;  Outs 0 to port (BASE + HALT + (cntr * 4))				;
;                                                                       ;
; Arguments:                                                            ;
;	cntr = 0, 1 or 2						;
; Returns:                                                              ;
;       nothing                                                         ;
; Alters:                                                               ;
;	AX, DX								;
; Calls:                                                                ;
;       nothing                                                         ;
;                                                                       ;
; History:                                                              ;
;                                                                       ;
;  Fri Sep 12, 1986 04:00:40p  -by-  Tom Bates  [tdb]		        ;
;-----------------------------------------------------------------------;

PUBLIC	StopCounter
align 4
StopCounter PROC    cntr:WORD

	mov	dx,cntr
	shl	dx,1
	shl	dx,1		; now DX = cntr * 4
	add	dx,BASE+HALT
	xor	al,al
	out	dx,al
	rest

	ret
StopCounter ENDP

;-----------------------------------------------------------------------;
; LatchCounter                                                          ;
;  Outs 0 to port (BASE + SNAP + (cntr * 4))				;
;                                                                       ;
;  Before reading a counter it must be latched using this proc.		;
;  Otherwise the result returned by Get_cntCounter is unreliable.	;
;                                                                       ;
; Arguments:                                                            ;
;	cntr = 0, 1 or 2						;
; Returns:                                                              ;
;       nothing                                                         ;
; Alters:                                                               ;
;	AX, DX								;
; Calls:                                                                ;
;       nothing                                                         ;
;                                                                       ;
; History:                                                              ;
;                                                                       ;
;  Fri Sep 12, 1986 04:00:40p  -by-  Tom Bates  [tdb]		        ;
;-----------------------------------------------------------------------;

PUBLIC	LatchCounter
align 4
LatchCounter	PROC	cntr:WORD

	mov	dx,cntr
	shl	dx,1
	shl	dx,1		; now DX = cntr * 4
	add	dx,BASE+SNAP
	xor	al,al
	out	dx,al
	rest

	ret
LatchCounter	ENDP

;-----------------------------------------------------------------------;
; Pre_devCounter                                                        ;
;  Outs ((64 * c3) + (8 * c2) + c1) to port SEL_DIV			;
;                                                                       ;
; Arguments:                                                            ;
;	c1 = pre divide value (0 to 7) for counter 0			;
;	c2 = pre divide value (0 to 7) for counter 1			;
;	c3 = pre divide value (0 to 3) for counter 2			;
;                                                                       ;
; Returns:                                                              ;
;       nothing                                                         ;
; Alters:                                                               ;
;	AX, CX, DX							;
; Calls:                                                                ;
;       nothing                                                         ;
;                                                                       ;
; History:                                                              ;
;                                                                       ;
;  Fri Sep 12, 1986 04:00:40p  -by-  Tom Bates  [tdb]		        ;
;-----------------------------------------------------------------------;

PUBLIC	Pre_devCounter
align 4
Pre_devCounter	PROC	c1:WORD, c2:WORD, c3:WORD

	xor	ax,ax
	mov	al,byte ptr (c2)
	mov	ah,byte ptr (c3)
	mov	cl,3
	shl	al,cl		    ; after shift AL = 8 * c2
	mov	cl,6
	shl	ah,cl		    ; after shift AH = 64 * c3
	add	al,ah
	add	al,byte ptr (c1)    ; after add AL = (64 * c3) + (8 * c2) + c1
	mov	dx,SEL_DIV
	out	dx,al
	rest

	ret
Pre_devCounter	ENDP

;-----------------------------------------------------------------------;
; Get_cntCounter                                                        ;
;  Gets the count for counter cntr.					;
;                                                                       ;
;  Ins the low byte of the count from port BASE + (cntr * 2)		;
;  Ins the high byte of the count from port BASE + (cntr * 2) + 1	;
;                                                                       ;
; Arguments:                                                            ;
;	cntr								;
; Returns:                                                              ;
;       AX = 16 bit count for counter cntr                              ;
; Alters:                                                               ;
;	AX, DX								;
;                                                                       ;
; Calls:                                                                ;
;       nothing                                                         ;
;                                                                       ;
; History:                                                              ;
;                                                                       ;
;  Fri Sep 12, 1986 04:00:40p  -by-  Tom Bates  [tdb]		        ;
;-----------------------------------------------------------------------;

PUBLIC	Get_cntCounter
align 4
Get_cntCounter	PROC	cntr:WORD

	mov	dx,cntr
	shl	dx,1
	add	dx,BASE		; after add DX = BASE + (cntr * 2)
	inc	dx
	in	al,dx
	rest
	mov	ah,al
	dec	dx
	in	al,dx
	rest

	ret
Get_cntCounter	ENDP


;-----------------------------------------------------------------------;
; CmndCounter                                                           ;
;  Outs 0 to port (BASE + func + (cntr * 4))				;
;                                                                       ;
;  Can be used to perform most of the functions seperated into		;
;  individual procs above.  In general it is clearer to use the		;
;  the individual procs rather than this routine.			;
;                                                                       ;
; Arguments:                                                            ;
;	cntr = 0, 1 or 2						;
; Returns:                                                              ;
;       nothing                                                         ;
; Alters:                                                               ;
;	AX, DX								;
; Calls:                                                                ;
;       nothing                                                         ;
;                                                                       ;
; History:                                                              ;
;                                                                       ;
;  Fri Sep 12, 1986 04:00:40p  -by-  Tom Bates  [tdb]		        ;
;-----------------------------------------------------------------------;

PUBLIC	CmndCounter
align 4
CmndCounter PROC    cntr:WORD, func:WORD

	mov	dx,cntr
	shl	dx,1
	shl	dx,1		; now DX = cntr * 4
	add	dx,BASE+func
	xor	al,al
	out	dx,al
	rest

	ret

CmndCounter ENDP

END
