.model small,c
.code
public	write_text
public	writetext
public	init_f8x14
public	init_f8x8
public	init_fuser
public	set_seg_video


seg_video	dw	0a000h
font_points	dw	14

pointer_font	dd	?


set_seg_video	proc	far _segment:word
	mov	ax,_segment
	mov	cs:seg_video,ax
set_seg_video	endp

init_f8x14	proc	far	uses es;

	mov	ax,3543h                  ;ds:bx   font
	int	21h
	mov	cs:word ptr pointer_font,bx
	mov	ax,es
	mov	cs:word ptr pointer_font+2,ax	
	mov	cs:word ptr font_points,14
	ret
init_f8x14	endp

init_f8x8	proc	far	uses es;
	mov	ax,351fh                  ;ds:bx   font
	int	21h
	sub	bx,128*8
	mov	cs:word ptr pointer_font,bx
	mov	ax,es
	mov	cs:word ptr pointer_font+2,ax	
	mov	cs:word ptr font_points,8
	ret
init_f8x8	endp

init_fuser	proc	far	font:word,_offset:word,_seg:word 
	mov	ax,_offset
	mov	cs:word ptr pointer_font,ax
	mov	ax,_seg
	mov	cs:word ptr pointer_font+2,ax
	mov	ax,font	
	mov	cs:font_points,ax
	ret
init_fuser	endp




write_text	proc	far	uses ds es si di,x:word,y:word,colt:byte,colb:byte,string:far ptr ;
local   ofs_buffer:word;

	lds	si,string
	mov	ax,cs
	mov	es,ax
	mov	di,offset buffer
	mov	ofs_buffer,di
	mov	cx,128
rep	movsw	

	lds	bx,pointer_font
	
	mov	ax,cs:seg_video              ;es:di    video
	mov	es,ax

	mov	cx,y
	mov	ax,80
	mul	cx
	mov	di,ax
	add	di,x


	cld
next:	mov	ah,0
	mov	dx,bx
	mov	bx,ofs_buffer	
	mov	al,cs:[bx]
	mov	bx,dx
	cmp	al,0
	jne	_no_end
	jmp	_end
_no_end:
	push	di
		
	mov	cx,cs:font_points
	mul	cx
	mov	si,ax
	
	mov	cx,cs:font_points
;---------------set mask
	mov	ax,0ff08h
	mov	dx,3ceh
	out	dx,ax
;---------------clear gate
	mov	al,es:[di]
;---------------set color 
	mov	dx,3c4h
	mov	ax,0ff02h
	out	dx,ax
	mov	al,0h
	mov	dx,di
do1:
	mov	es:[di],al
	add	di,80
	loop	do1
	mov	di,dx
	

	mov	cx,cs:font_points
do:

	mov	ah,ds:[si][bx]
;---------------set mask
	xor	ah,0ffh
	mov	dx,3ceh
	mov	al,08h
	out	dx,ax
;---------------clear gate
	mov	al,es:[di]
;---------------set color ground
	mov	dx,3c4h
	mov	ah,colb
	mov	al,2
	out	dx,ax
;---------------------------
	mov	al,0ffh
	mov	es:[di],al
	mov	ah,ds:[si][bx]
;---------------set mask
	mov	dx,3ceh
	mov	al,08h
	out	dx,ax
;---------------clear gate
	mov	al,es:[di]
;---------------set color text
	mov	dx,3c4h
	mov	ah,colt
	mov	al,2
	out	dx,ax
;---------------------------
	mov	al,0ffh
	mov	es:[di],al
	add	di,80


	inc	si
	loop	do
	
	pop	di
	inc	di
	inc	ofs_buffer
	jmp 	next
_end:	
	mov	dx,3c4h
	mov	ax,0ff02h
	out	dx,ax
;---------------set mask
	mov	ax,0ff08h
	mov	dx,3ceh
	out	dx,ax

	ret
write_text   endp


writetext	proc	far	uses ds es si di,x:word,y:word,colt:byte,string:far ptr ;
local   ofs_buffer:word;

	lds	si,string
	mov	ax,cs
	mov	es,ax
	mov	di,offset buffer
	mov	ofs_buffer,di
	mov	cx,128
rep	movsw	

	lds	bx,pointer_font

	mov	ax,cs:seg_video              ;es:di    video
	mov	es,ax

	mov	cx,y
	mov	ax,80
	mul	cx
	mov	di,ax
	add	di,x


	cld
next1:	mov	ah,0
	mov	dx,bx
	mov	bx,ofs_buffer	
	mov	al,cs:[bx]
	mov	bx,dx
	cmp	al,0
	je	_end1
	push	di
		
	mov	cx,cs:font_points
	mul	cx
	mov	si,ax
	
	

	mov	cx,cs:font_points

do2:

	mov	ah,ds:[si][bx]
;---------------set mask
	mov	dx,3ceh
	mov	al,08h
	out	dx,ax
;---------------clear gate
	mov	al,es:[di]
;---------------set color ground
	mov	dx,3c4h
	mov	ah,0ffh
	mov	al,2
	out	dx,ax
;---------------------------
	mov	al,0h
	mov	es:[di],al

;---------------set color text
	mov	dx,3c4h
	mov	ah,colt
	mov	al,2
	out	dx,ax
;---------------------------
	mov	al,0ffh
	mov	es:[di],al
	
	add	di,80
	inc	si
	loop	do2

	pop	di
	inc	di
	inc	ofs_buffer
	jmp 	next1
_end1:	
	mov	dx,3c4h
	mov	ax,0ff02h
	out	dx,ax
;---------------set mask
	mov	ax,0ff08h
	mov	dx,3ceh
	out	dx,ax

	ret
writetext	endp


buffer	db	257 dup(0)
	end
