include	clib.inc
include	conio.inc
include	keyb.inc
include	mouse.inc

	public	consinit
	public	conssetl
	public	console_wp	; pointer to old screen
	public	console_oz	; old line count

_TEXT	SEGMENT

pushscr PROC
	inc	ah
	mov	al,_scrcol
	mov	dx,ax
	xor	ax,ax
	invoke	rcopen, dx::ax, ax, ax, ax::ax, ax::ax
	ret
pushscr ENDP

consinit PROC	DIST
	invoke	getcursor, addr console_xy
	mov	ah,_scrrow	; save old line count
	mov	console_oz,ah
	call	pushscr		; save screen
	stom	console_wp
	mov	al,byte ptr console_xy+1
	mov	ah,40h
	mov	cx,2
	add	al,cl
	mov	dx,offset cp_linefeed+2
	cmp	al,console_oz
	jb	@F
	sub	dx,cx
	add	cx,cx
    @@:	mov	bx,2
	int	21h
	invoke	loadpal, addr at_palett
	call	getkey
	ret
consinit ENDP

conssetl PROC	DIST
	cmp	al,_scrrow
	jne	@F
	cmp	_scrcol,80
	je	conssetl_end
    @@:	push	ax
  ifdef __MOUSE__
	call	mousehide
  endif
	cmp	fastswitch,0
	jne	@F
	mov	al,_scrcol
	mov	ah,50
	push	ax		; save screen (args rcclose)
	push	0
	mov	ah,49
	call	pushscr
	push	_D_DOPEN or _D_ONSCR
	push	dx
	push	ax
    @@:	push	ax		; save cursor
	push	ax
	mov	ax,sp
	invoke	getcursor, ss::ax
	mov	ax,0003h	; this clears the screen
	cmp	_scrseg,0B000h
	jne	@F
	mov	al,07h
    @@:	or	al,fastswitch
	int	10h
	call	setcursor	; restore cursor
	cmp	fastswitch,0
	jne	@F
	call	_screct
	call	rcclose		; restore screen
    @@:	pop	ax
	cmp	al,49
	jne	@F
	mov	ax,1202h	; set scanline to 400
	mov	bx,0030h
	int	10h
	mov	ax,1112h	; load 8x8 font
	xor	bx,bx
	int	10h
    @@:	test	console,CON_COLOR
	jz	@F
	invoke	loadpal, addr at_palett
    @@:
  ifdef __MOUSE__
	call	mouseshow
  endif
    conssetl_end:
	call	_screct
	ret
conssetl ENDP

Install	PROC			; EGA installation check
	mov	ah,12h
	mov	bx,0FF10h
	int	10h
	cmp	bh,0FFh
	je	Install_00
	mov	fastswitch,80h
    Install_00:
	mov	ah,0Fh		; Japheth - 28.08.11
	int	10h
	test	bh,bh		; Test if active display page is 0
	jz	Install_01
	mov	ax,0500h	; Set display page to 0
	int	10h
    Install_01:
	ret
Install	ENDP

_TEXT	ENDS

_DATA	SEGMENT
fastswitch	db	0
console_oz	db	24	; lines on init
console_wp	dd	0
console_xy	dw	0
		dw	CURSOR_NORMAL
cp_linefeed	db	13,10
		db	13,10
_DATA	ENDS

_INIT_	SEGMENT
	dw	Install
_INIT_	ENDS

	END
