include	clib.inc
include	math.inc

_TEXT	SEGMENT

T1	dw ?
T2	dw ?

dummy:	push	si
	push	di
	pop	di
	pop	si
	ret

read_timer:
	pushf
	cli
	xor	ax,ax
	out	43h,al
	call	dummy
	in	al,40h
	mov	dl,al
	call	dummy
	in	al,40h
	mov	dh,al
	not	dx
	popf
	ret

delay proc dist pascal public uses si di time:word
        mov	ax,cs:[T1]
	mov	dx,cs:[T2]
	xor	cx,cx
	mov	bx,time
	call	MUL32
	mov	si,dx
	mov	di,ax
	call	read_timer
	mov	bx,dx
	add	di,dx
	adc	si,0
	jmp	delay_02
    delay_loop:
	cmp	dx,bx
	jnb	delay_01
	or	si,si
	jz	delay_end
	sub	si,1
	sbb	di,0
    delay_01:
	mov	bx,dx
    delay_02:
	call	read_timer
	or	si,si
	jnz	delay_loop
	cmp	dx,di
	jb	delay_loop
    delay_end:
	ret
delay endp

Install	proc
	mov	cx,100
    Install_00:
	call	read_timer
	and	dx,1
	jz	Install_01
        dec	cx
	jnz	Install_00
	mov	cs:[T1],2*1193
	mov	cs:[T2],cx
	ret
    Install_01:
	mov	cs:[T1],1193
	mov	cs:[T2],dx
	ret
Install	endp

_TEXT	ENDS

_INIT_	SEGMENT
	dw	Install
_INIT_	ENDS

	END
