/* DIVIDE_START=cons.s */
/**************************************************************
;
;	コンンソ−ル割り込み設定ル−チン
;
;	1991.01.01	Make by Ken
;
;**************************************************************/

/**************************************************************
;	各種値の設定
;**************************************************************/
#define CONBIOS	$0x29		/* 割り込むベクタ番号 */
#define STACK_SIZE (10*1024)	/* ロ−カルスタックサイズ */

/**************************************************************
;	外部参照関数の定義
;**************************************************************
extrn	CON_bios:near				; メインのコンソ−ル関数
*/


.lcomm nt_vet_off,4
.lcomm nt_vet_seg,2

.lcomm rl_vet_ral,4

.lcomm save_esp,4
.lcomm save_ss,2

.lcomm stack_flg,2

.lcomm new_esp,4
.lcomm new_ss,2

consol_path:	.asciz	"CON"
null_path:	.asciz	"NUL"

break_flg:	.byte	0

	.align	2
.lcomm local_stack,STACK_SIZE
.lcomm local_stack_btm,2

/***************************************************
;
;	割り込み処理ル−チン
;
;***************************************************/
CON_bios_entry:

	cmpw	$0,stack_flg
	jne	go_ret
	incw	stack_flg

	movl	%esp,save_esp
	movw	%ss,save_ss
	lss	new_esp,%esp
	pushal

	sti
	cld
	push	%ds
	pop	%es

	movl	%esp,%ebp
	push	%ebp
	call	_CON_bios
	addl	$4,%esp

	popal
	lss	save_esp,%esp
	decw	stack_flg
go_ret:	iretl



/**************************************************************
;
;	ファイルのリダイレクト処理
;
; al	=	ファイルのオ−プンモ−ド
; bx	=	ファイルパス番号
; edx	=	NEWデバイスネ−ム
;
;**************************************************************/
reopen:
	push	%ax
	movb	$0x3e,%ah
	int	$0x21
	pop	%ax
	movb	$0x3d,%ah
	int	$0x21
reo_ret:ret


/**************************************************************
;
;	コンソ−ル割り込みの設定及び標準入出力の設定
;
;
;void	CON_bios_set(void)
;
;**************************************************************/
	.globl	_CON_bios_set
_CON_bios_set:

	push	%ds
	push	%es
	push	%ebx
	push	%edx

	movw	%ds,new_ss
	leal	local_stack_btm,%ebx
	movl	%ebx,new_esp

	movw	$0x2502,%ax
	movb	CONBIOS,%cl
	int	$0x21
	movw	%es,nt_vet_seg
	movl	%ebx,nt_vet_off

	movw	$0x2503,%ax
	movb	CONBIOS,%cl
	int	$0x21
	movl	%ebx,rl_vet_ral

	movw	$0x2506,%ax
	movb	CONBIOS,%cl
	push	%ds
	movw	%cs,%dx
	movw	%dx,%ds
	leal	CON_bios_entry,%edx
	int	$0x21
	pop	%ds

	movb    $0,%al
	movw    $0,%bx
	leal    consol_path,%edx
	call    reopen
	movb    $1,%al
	movw    $1,%bx
	call    reopen
	movb    $1,%al
	movw    $2,%bx
	call    reopen

	movw	$0x3300,%ax
	int	$0x21
	movb	%dl,break_flg

	movw	$0x3301,%ax
	movb	$0,%dl
	int	$0x21

	pop	%edx
	pop	%ebx
	pop	%es
	pop	%ds
	ret


/**************************************************************
;
;	コンソ−ル割り込みを元に戻し標準入出力をNULにする
;
;void	CON_bios_ret(void)
;
;**************************************************************/
	.globl	_CON_bios_ret
_CON_bios_ret:

	push	%ds
	push	%ebx
	push	%edx

	movb	CONBIOS,%cl
	movl	rl_vet_ral,%ebx
	movl	nt_vet_off,%edx
	movw	nt_vet_seg,%ax
	push	%ds
	movw	%ax,%ds
	movw	$0x2507,%ax
	int	$0x21
	pop	%ds

	movb	$0,%al
	movw	$0,%bx
	leal	null_path,%edx
	call	reopen
	movb	$1,%al
	movw	$1,%bx
	call	reopen
	movb	$1,%al
	movw	$2,%bx
	call	reopen

	movw	$0x3301,%ax
	movb	break_flg,%dl
	int	$0x21

	pop	%edx
	pop	%ebx
	pop	%ds
	ret

/**************************************************************
;	アセンブラル−チンの終了
;**************************************************************/
/* DIVIDE_END */
