;
;  FACILITY.MAC - Facility Functions
;
;  (MS)  (BEEP)  MS  BEEP  AT-XY  PAGE  NORMAL  HIGHLIGHT  CLEAR-LINE
;  INSERT-LINE  DELETE-LINE  INIT-VIDEO  EXIT-VIDEO
;

;	(MS)  ( ms -- )

	hdr	x,'(MS)'
ms0:	pop	de
ms01:	ld	a,e
	or	d
	jp	z,next
	ld	hl,(speed)
	add	hl,hl
	add	hl,hl
	add	hl,hl
ms02:	ex	(sp),hl		; 19T
	ex	(sp),hl		; 19T
	ex	(sp),hl		; 19T
	ex	(sp),hl		; 19T
	push	hl		; 11T
	pop	hl		; 10T
	dec	hl		; 6T
	ld	a,0		; 7T
	ld	a,l		; 4T
	or	h		; 4T
	jp	nz,ms02		; 10T
	dec	de
	jp	ms01

;	(BEEP)  ( -- )		7 emit 75 ms

	hdr	x,'(BEEP)'
beep0:	call	docol
	dw	clit
	db	7
	dw	emit
	dw	clit
	db	75
	dw	ms
	dw	exit

;	MS  ( ms -- )

	hdr	1,'MS'
ms:	call	aexec
	dw	ms0

;	SOUND  ( freq ms -- )
;
;	hdr	1,'SOUND'
;sound:	call	aexec
;	dw	sound0

;	BEEP  ( -- )

	hdr	1,'BEEP'
beep:	call	aexec
	dw	beep0

	cseg
cpbuf:	ds	16

;	AT-XY  ( u1 u2 -- )	position cursor at col u1, row u2

	hdr	1,'AT-XY'
atxy:	pop	de
	pop	hl
	ld	d,l
	push	bc
	push	de
	ld	hl,tgxy
	ld	de,cpbuf
	ld	a,16
	call	amovd
	pop	de
	ld	a,(txpos)
	ld	c,a
	ld	a,(txofs)
	add	a,d
	push	de
	call	atxy3
	pop	de
	ld	a,(typos)
	ld	c,a
	ld	a,(tyofs)
	add	a,e
	call	atxy3
	pop	bc
	ld	de,tgxyd
	ld	hl,cpbuf
atxy1:	push	de
	push	hl
atxy2:	call	docol
	dw	count,typee
	dw	at,ms
	dw	exit

atxy3:	ld	hl,cpbuf
	ld	b,0
	add	hl,bc
	ex	de,hl
	ld	hl,tisbin
	inc	(hl)
	dec	(hl)
	jp	z,atxy4
	ld	(de),a
	ret

atxy4:	dec	de
	dec	de
	ld	hl,atxy8-1
	ld	b,3
atxy5:	inc	hl
	ld	c,'0'-1
atxy6:	inc	c
	sub	(hl)
	jp	nc,atxy6
	add	a,(hl)
	push	af
	ld	a,c
	cp	'0'
	jp	z,atxy7
	ld	(de),a
atxy7:	inc	de
	pop	af
	dec	b
	jp	nz,atxy5
	ret

atxy8:	db	100,10,1

;	PAGE  ( -- )		(vemit) @ (lstout) = if (ff) emit exit
;				then...

	hdr	1,'PAGE'
page:	call	docol
	dw	lit,vemit
	dw	at
	dw	lit,lstout
	dw	equal
	dw	zbran,page1
	dw	clit
	db	ff		; formfeed char
	dw	emit,exit
page1:	dw	lit,tceold	; home cursor and clear-screen sequence
	dw	lit,thome
	dw	atxy2
	dw	lit,tclsd
	dw	lit,tcls
	dw	atxy2
	dw	exit

;	NORMAL  ( -- )

	hdr	1,'NORMAL'
vnorm:	ld	de,tceold
	ld	hl,tlovid
	jp	atxy1

;	HIGHLIGHT  ( -- )

	hdr	1,'HIGHLIGHT'
hilit:	ld	de,tceold
	ld	hl,thivid
	jp	atxy1

;	CLEAR-LINE  ( -- )

	hdr	1,'CLEAR-LINE'
cleol:	ld	de,tceold
	ld	hl,tceol
	jp	atxy1

;	INSERT-LINE  ( -- )

	hdr	1,'INSERT-LINE'
insln:	ld	de,tclsd
	ld	hl,tinsln
	jp	atxy1

;	DELETE-LINE  ( -- )

	hdr	1,'DELETE-LINE'
delln:	ld	de,tclsd
	ld	hl,tdelln
	jp	atxy1

;	INIT-VIDEO  ( -- )

	hdr	1,'INIT-VIDEO'
invid:	ld	de,tclsd
	ld	hl,tinit
	jp	atxy1

;	EXIT-VIDEO  ( -- )

	hdr	1,'EXIT-VIDEO'
exvid:	ld	de,tclsd
	ld	hl,texit
	jp	atxy1

;	end

