;***********************************************************************
; BOS, Christoffer Bubach 2004. Public Domain. NO WARRANTY!
; Textmode screen "driver".
;***********************************************************************


; Change cursor attribs. IN: CX = cursor attribs
;------------------------------------------------
changecursor:
		push	eax
		push	edx

		mov	dx, 3d4h
		mov	al, 0ah
		mov	ah, ch
		out	dx, ax
		inc	ax
		mov	ah, cl
		out	dx, ax

		pop	edx
		pop	eax
		ret


; Clear the screen.
;--------------------
cls:
		push	edx
		push	ecx

		mov	ecx, 25
	  .loop:
		call	scroll_up
		loop	.loop

		mov	dh, 0x00
		mov	dl, 0x00
		call	setcursorxy

		pop	ecx
		pop	edx
		ret


; Get the cursor pos. OUT: DH = X   DL = Y
;------------------------------------------
getcursorxy:
		push	eax
		push	ebx

		call	getcursor

		xor	dx, dx
	   .GC1:
		sub	bx, 80
		inc	dl
		cmp	bx, 80
		ja	.GC1

		mov	dh, bl

		pop	ebx
		pop	eax
		ret


; Get the cursor pos. OUT: BX = Offset
;--------------------------------------
getcursor:
		push	eax
		push	edx

		mov	dx, 3d4h
		mov	al, 0eh
		out	dx, al
		inc	dx
		in	al, dx
		mov	bh, al
		mov	al, 0fh
		dec	dx
		out	dx, al
		inc	dx
		in	al, dx
		mov	bl, al

		pop	edx
		pop	eax
		ret


; Set the cursor to:  DH = X   DL = Y
;--------------------------------------
setcursorxy:
		push	eax
		push	ebx
		push	edx

		movzx	bx, dl
		mov	ax, bx
		shl	ax, 4
		shl	bx, 6
		add	bx, ax
		movzx	ax, dh
		add	bx, ax

		call	setcursor

		pop	edx
		pop	ebx
		pop	eax
		ret


; Set the cursor to:  BX = Offset
;---------------------------------
setcursor:
		push	eax
		push	ebx
		push	edx

		mov	al, 0eh
		mov	ah, bh
		mov	dx, 3d4h
		out	dx, ax
		inc	ax
		mov	ah, bl
		out	dx, ax

		pop	edx
		pop	ebx
		pop	eax
		ret


; cursor position +1
;--------------------
inccursor:
		push	ebx

		call	getcursor
		inc	bx
		call	setcursor

		pop	ebx
		ret


; cursor position -1
;--------------------
deccursor:
		push	ebx

		call	getcursor
		dec	bx
		call	setcursor

		pop	ebx
		ret


; Print char. IN: bl = char, bh = attrib
;----------------------------------------
print_char:
		pushad
		push	es
		push	ebx

		call	getcursorxy

		cmp	dl, 0x18
		jb	.l1
		cmp	dh, 0x50
		jb	.l1
		call	scroll_up

	    .l1:
		call	getcursor

		movzx	eax, bx
		pop	ebx
		mov	[es:(eax*2 + 0xb8000)], bx

		call	inccursor

		pop	es
		popad
		ret


; Display a asciiz message on the screen.
;       Input: ESI = Message, AL = COLOR.
;--------------------------------------------
print:
		pushad

		mov	bh, al

   .DisplayChar:
		lodsb
		or	al, al
		jz	.Done

		cmp	al, 0x0D
		je	.Enter

		cmp	al, 0x0A
		je	.DisplayChar

		mov	bl, al
		call	print_char
		jmp	.DisplayChar

	 .Enter:
		call	getcursorxy

		cmp	dl, 0x18
		jb	.l1

		push	ebx
		call	scroll_up
		pop	ebx

	    .l1:
		mov	dh, 0x00
		inc	dl
		call	setcursorxy
		jmp	.DisplayChar

	  .Done:
		popad
		ret


; Scrolling..
;--------------
scroll_up:	pushad
		call	_scroll_up
		popad

       .mv_curs:
		mov	bx, 80*24
		call	setcursor
		ret

     _scroll_up:
		push	edi
		push	esi

		push	ds
		push	es
		pop	ds

		mov	edi, 0b8000h
		mov	esi, 0b8000h+160
		mov	ecx, (160*24)/4
		rep	movsd

		pop	ds

		mov	edi, 0b8000h+160*24
		mov	ecx, 80/2
		mov	eax, 07000700h
		rep	stosd

		pop	esi
		pop	edi
		ret
