;
;This module samples the TNC CD line 18.2 times a second.
;One long counter is incremented when the CD line is found active,
;another counter is incremented if the CD line is inactive.
;
; This module is part of stats.exe
;
;  Hardware: IBM PC or compatable serial port (8250)
;                  
;   Language = Microsoft Macro Assembler version 4.0
;
;   This source is distributed freely and may be copied and
;   redistributed with the following provisos:
;   
;           You may not sell it, nor may you charge for making 
;           copies beyond the actual cost of mailing and media.
;                      
;   Written by Skip Hansen WB6YMH and Harold Price NK6K.
;
;   Feedback is desired.
;
;   RCP/M (213) 541-2503 300/1200/2400 baud
;   or via packet WB6YMH @ WB6YMH-2 or 
;		 NK6K @ NK6K
;
;   Modification history:
;
;	8/10/87	 	WB6YMH: Initial release.	
;	ver 1.0		 
;

;
;macro to increment a long integer
;
bump	macro	variable
	local	next			;
	inc	word ptr variable	;
	jnz	next			;
	inc	word ptr variable+2	;
next:
	endm

_TEXT   SEGMENT BYTE    PUBLIC  'CODE'
_TEXT   ENDS
_DATA   SEGMENT WORD    PUBLIC  'DATA'
_DATA   ENDS
CONST   SEGMENT WORD    PUBLIC  'CONST'
CONST   ENDS
_BBS    SEGMENT WORD    PUBLIC  'BBS'
_BBS    ENDS
                subttl  Data Areas
                page
DGROUP  GROUP   CONST,  _BBS,   _DATA

        ASSUME  CS:_TEXT, DS:DGROUP, SS:DGROUP

_DATA   SEGMENT
	extrn	port_base:word

_DATA   ENDS

_TEXT   SEGMENT

base		dw	?		;base adr of com port in use
old_8		dd	?		;old interrupt 8 vector
cd_active	dd	0		;
cd_inactive	dd	0		;

;
;Intercept 18.2 hz ticker for CD timing
;

	public	_tick_init

_tick_init:	push	bp			;save registers
		push	si			;
		push	di			;
		push	es			;
		push	ds			;
		mov	ah,35h			;get old ticker interrupt
		mov	al,8			;
		int	21h			;
		mov	word ptr cs:old_8,bx	;save old interrupt vector
		mov	word ptr cs:old_8+2,es	;
		mov	ax,cs			;set up ds
		mov	ds,ax			;
		mov	ah,25h			;set our interrupt
		mov	al,8			;
		mov	dx,offset int8_hdlr	;
		int	21h			;
		pop	ds			;restore registers
		pop	es			;
		pop	di			;
		pop	si			;
		pop	bp			;
		ret				;


;
;Remove cd sampler from int 8 chain
;
	  	public	_tick_stop
_tick_stop:	push	bp			;save registers
		push	si			;
		push	di			;
		push	ds			;
		mov	dx,word ptr cs:old_8	;get old interrupt vector
		mov	ax,word ptr cs:old_8+2	;
		mov	ds,ax			;set up dx
		mov	ah,25h			;reset old interrupt 
		mov	al,8			;
		int	21h			;
		pop	ds			;restore registers
		pop	di			;
		pop	si			;
		pop	bp			;
		ret				;

;
;Save COM port base address for background program
;
		public	_set_base
_set_base:	push    bp              ;save BP
                mov     bp,sp           ;point to passed parms
                mov     ax,[bp+4]       ;get base adr
		mov	cs:base,ax	;save for interrupt handler
		pop	bp		;
		ret			;

;
;Ticker interrupt handler.  Sample current state of CD line and
;increment appropriate counter.
;

int8_hdlr:	push	ax		;save registers
		push	dx		;
		mov	dx,cs:base	;
		add	dx,6		;point to modem status register
		in	al,dx		;
		and	al,80h		;CD active ?
		jnz	inactive	;jump if not
		bump	cs:cd_active	;
		jmp	chain_nxt	;continue

inactive:	bump	cs:cd_inactive	;

chain_nxt:	pop	dx		;restore registers
		pop	ax		;
		jmp	cs:old_8	;chain to next handler in line

;
;Read and clear the cd_active variable
;

		public	_get_cd_active

_get_cd_active:	mov	ax,word ptr cs:cd_active	;get lsb
		mov	dx,word ptr cs:cd_active+2	;and msb
		mov	word ptr cs:cd_active,0		;clear counter
		mov	word ptr cs:cd_active+2,0	;
		ret					;

;
;Read and clear the cd_inactive variable
;

		public	_get_cd_inactive

_get_cd_inactive:	
		mov	ax,word ptr cs:cd_inactive	;get lsb
		mov	dx,word ptr cs:cd_inactive+2	;and msb
		mov	word ptr cs:cd_inactive,0	;clear counter
		mov	word ptr cs:cd_inactive+2,0	;
		ret					;



_TEXT   ENDS
        end

