comment *  parint.com (c) 1984 by David G. Hunter.
	this program may be freely copied but not be sold for profit.
	parint.com detects parity errors, logs them on the printer if
	possible, beeps, and returns to program execution.  the system
	does not halt when this program is resident.  parity error 
	checking is terminated after the first error is detected.  The 
	error message includes the time of the event.  If the printer 
	is not available, the errors are logged to the screen.
	(Published in PC Magazine, January 22, 1985 issue, pages309-315).
	(Corrections by Ken Kriesel)
*
;****************addresses of interrupt handlers *******************
book	segment at 0h	;this is where the interrupt address book is
	org	2h*4
int_2	label	dword	;address of nmi handler
book	ends

;***************beginning of parint instructions *******************
cseg 	segment
	assume cs:cseg
	org 100h
start:	jmp	newvec	;install new nmi handler in ram
;***************nmi handler*****************************************
newint	proc	near	;new interrupt handler
	assume ds:cseg, es:cseg
	sti	;this flag was cleared when interrupt was issued
	jmp 	go	;jump over more data
;**********************data****************************
getout:	nop
instruc	db	0EAh	;this is the jump-immediate instruction!
			;it jumps to the address stored in oldint.
oldint	dd		;address of old interrupt 2 routine
device1	dw	0004	;printer
device2	dw	0001	;standard output device
par1	db	0Dh,0Ah,"Parity Error: Main Board     ",07h,0Dh,0Ah
par2	db	0Dh,0Ah,"Parity Error: Expansion Board",07h,0Dh,0Ah
time	db	"    TIME:  "
hour	dw	"00"
	db	":"
min	dw	"00"
	db	"                ",0Dh,0Ah
paroff	db	"PARITY CHECKING NOW DISABLED   ",0Dh,0Ah,0Ah
go:	push 	cx
	push 	ds
	pushf
	push	dx
	push	bx
	push	ax
	pushf
	mov	al,00h
	out	0A0h, al	;turn off parity checking for now
	xor	cx,cx		;cx=error flag: cx=1 if parity ok
	in	al, 62h		;get data in port c
	;	find origin of nmi
	test	al, 40h
	jz 	mother
	mov 	dx, offset par2	;if bit 6 set, error is on expansion bd
	jmp	out		;print message and exit
mother:	test 	al,80h
	jz 	other
	mov	dx, offset par1	;if bit 7 set, error is on main board
	jmp	out		;print message and exit
other:	or	cx, 1h		;if neither bit is set, no parity error occurred,
	mov	al, 80h		;so set parity ok flag
	out 	0A0h, al	;turn parity checking back on.
	jmp	noprt	;don't print message if no parity error
;	print error message and exit.  Type of exit depends on type of error--
out:	mov	ax,cs		;extablish proper data segment to
	mov	ds,ax		;allow access to messages
	call	whatime		;get time of error
	mov	bx, device1
	cmp	bx,0004		;if output is to printer, make sure it's ready
	jne	f1
	call	testprt
f1:	call	print
	mov	dx, offset time ;"time of error" message
	call	print
	mov	dx, offset paroff
	call	print
noprt:	popf
	pop	ax
	pop	bx
	pop	dx
	popf
	pop	ds
	test	cx, 1h		;special exit if parity ok
	jnz	oth
	pop	cx
	iret			;message printed; let execution continue
;	if no parity error, give control to pre-existing nmi handler
; this is done by restoring ds and jumping to the instruction that
; specifies the address of the old interrupt routine.  This unusual 
; exit is necessary because otherwise, once ds was restored, the
; address of the old interrupt routine would not be accessible.
oth:	cli
	pop	cx
	jmp	getout
newint	endp
;********************subroutines************************8
testprt	proc	near
	push	dx
	push	ax
	xor	dx,dx		;printer # 0
	mov	ah,2
	int	17h		;read printer status
	test	ah,00101001b	;error bits
	jz	aok
	mov	bx, device2	;if not ok, try other device
aok:	pop	ax
	pop	dx
	ret
testprt endp
print	proc	near	;output to file named in bx; if error try another file
	push	cx
	mov	cx,34	;34 characters to print
	mov	ah,40h	;dos function call
	int	21h
	pop	cx
	ret
print	endp
whatime	proc	near	;what time is it?  Put result in hour/min
	push	cx
	push	dx
	mov	ah,2Ch	;dos time
	int	21h
	mov	al,ch	;hours (0-23)
	call	convt	;put tens in ah, ones in al
	mov	hour,"00"
	add	hour,ax
	mov	al,cl
	call	convt	;do same for minutes (0-59)
	mov	min,"00"
	add	min,ax
	pop	dx
	pop	cx
	ret
whatime	endp
convt	proc	near	;convert number (100) to ascii. number is in al
			;result: tens in ah, ones in al.
	xor	ah,ah	;input is less than 100 anyway, so clear it
	push	cx
	mov	cl,10
	div	cl	;divide ax by 10
	pop	cx
little:	ret
convt	endp
;*******************install nmi handler*********************8
newvec	proc	near
	jmp	past
loaded	db	0Dh,0Ah,"PARITY ERROR INTERCEPTOR V2.00 BY DAVID HUNTER IS NOW"
	DB	" INSTALLED",0DH,0AH,"$"
past:	mov	dx, offset loaded
	mov	ah,0
	int	21h
	assume	ds:book	;interrupt address book area
	push	ds	;save old ds for future use
	mov	ax,book
	mov	ds,ax
	mov	al, 00h
	out	0A0h, al	;turn off parity checking
	mov	ax,int_2	;get the address
	mov	oldint,ax	;save it for some future use
	mov	ax,int_2[2]	;second part of double word
	mov	oldint[2],ax
	mov	int_2, offset newint	;now load the new address
	mov	int_2[2],cs		;cs is ds in com program
	mov	al, 80h
	out	0A0h, al		;turn parity checking back on
	mov	dx, offset newvec	;leave new interrupt routine resident
	int 	27h			;don't need "newvec" or beyond
newvec	endp
cseg	ends
	end	start
