; Used for TSR debugging

;------- INT 61H ----------------------

IF		DEBUG

d_str		db	'AX=0000   BX=0000   CX=0000   DX=0000   '
		db	'SP=0000   BP=0000   SI=0000   DI=0000   '
		db	'DS=0000   ES=0000   SS=0000   CS=0000   '
		db	'IP=0000    NV UP DI PL NZ NA PO NC      ',0
state1		dw	'VO','ND','IE','GN','RZ','CA','EP','YC'
state2		dw	'VN','PU','ID','LP','ZN','AN','OP','CN'
flag_mask	dw	0000100000000000B
		dw	0000010000000000B
		dw	0000001000000000B
		dw	0000000010000000B
		dw	0000000001000000B
		dw	0000000000010000B
		dw	0000000000000100B
		dw	0000000000000001B
d_hex		db	'0123456789ABCDEF',0
d_rtemp		dw	16 dup (0)

ENDIF

new_int61	proc	far

IF	DEBUG

	sti
	assume	ds:nothing		; Override segment with CS

	mov	d_rtemp[00],ax		; Save all the register to d_rtemp
	mov	d_rtemp[02],bx
	mov	d_rtemp[04],cx
	mov	d_rtemp[06],dx
	mov	d_rtemp[08],sp
	mov	d_rtemp[10],bp
	mov	d_rtemp[12],si
	mov	d_rtemp[14],di
	mov	d_rtemp[16],ds
	mov	d_rtemp[18],es
	mov	d_rtemp[20],ss

	pop	ax			; Get IP
	pop	bx			; Get CS
	pop	cx			; Get FLAG
	push	cx
	push	bx
	push	ax

	mov	d_rtemp[22],bx		; Save CS   register
	mov	d_rtemp[24],ax		; Save IP   register
	mov	d_rtemp[26],cx		; Save FLAG register
	add	word ptr d_rtemp[8],6	; Adjust SP value

	assume	cs:cseg,ds:cseg		; Does not override segment
	mov	ax,cs
	mov	ds,ax

	mov	si,0			; Translate register value to ascii
	mov	di,offset d_str
	add	di,3
debug1:	mov	ax,d_rtemp[si]
	push	si
	mov	dl,4
debug2:	mov	cl,4
	rol     ax,cl
	mov	si,ax
	and	si,000FH
	mov	bl,d_hex[si]
	mov	[di],bl
	inc	di
	dec	dl
	jnz	debug2
	pop	si
	add	di,6
	add	si,2
	cmp	si,26
	jne	debug1

	mov	ax,d_rtemp[26]		; Translate FLAG to ascii
	mov	di,131
	mov	si,0
	mov	bx,offset flag_mask
	mov	cx,8
debug3:	mov	dx,state2[si]
	test	ax,[bx+si]
	jz	debug4
	mov	dx,state1[si]
debug4:	mov	word ptr d_str[di],dx
	add	di,3
	add	si,2
	loop	debug3

	mov	si,0			; Display
	mov	ah,0EH
	mov	bh,1
	mov	bl,5
debug5:	mov	al,d_str[si]
	int	10H
	inc	si
	cmp	si,160
	jne	debug5

	mov	ax,d_rtemp[00]		; Restore all the registers value
	mov	bx,d_rtemp[02]		; that have been modified
	mov	cx,d_rtemp[04]
	mov	dx,d_rtemp[06]
	mov	si,d_rtemp[12]
	mov	di,d_rtemp[14]
	mov	ds,d_rtemp[16]
ENDIF
	iret

new_int61	endp

