	title System utilities
	name SysUtil
	page	60,132

	.xlist
	include ifthen.mac
	include util.mac
	.list

sc = 2
rl = 3
mode = 3
bcd = 0

CtlWord = sc*040h + rl*010h + mode*2 + bcd


SysData	segment 'data'

SP_save	dw	0
	dd	SysUtil
	dd	0

Init	db 1

SysData	ends


SysCode	segment 'code'
	assume cs:Syscode, ds:Sysdata

	dw	seg Sysdata

public	SysUtil, Sound, NoSound, Delay


SysUtil	proc far

	ret

SysUtil	endp


Sound	proc far			; call Sound( Freq )

	mov	ax, SysData
	mov	ds, ax
	mov	SP_save, sp

	lds	si,dword ptr es:[bx]	;ds,si := addr of Freq

	mov	cx, ds:[si]		; cx = Freq

;	Perform initialization if necessary

	if$ Init,e,1
	  mov	al, CtlWord
	  out	043h, al
	  mov	al, 0
	  mov	init, al
	endif$

;	compute n = F/Freq	F=1193182

	mov	dx, 012h
	mov	ax, 034deh
	div	cx
	mov	cx, ax			; cx = n

;	Set given freq

	mov	al, cl
	out	042h, al
	mov	al, ch
	out	042h, al

;	Turn on sound

	in	al, 061h
	or	al, 3
	out	061h, al

	ret

Sound  	endp


NoSound	proc far			; call NoSound

	in	al, 061h
	and	al, 11111100b
	out	061h, al

	ret

NoSound	endp


Delay	proc far			; call Delay( ms )

	mov	ax, SysData
	mov	ds, ax
	mov	SP_save, sp

	lds	si,dword ptr es:[bx]	;ds,si := addr of ms

	mov	cx,ds:[si]

Delay1:
	push	cx
	mov	cx, 451
Delay2:
	loop	Delay2
	pop	cx
	loop	Delay1

	ret

Delay  	endp



SysCode	ends

	end
