rmcode	segment word public use16
rmcode	ends
rmdata	segment dword public 'DATA' use16
rmdata	ends
pmdata	segment dword public 'DATA' use32
pmdata	ends
pmcode	segment dword public 'CODE' use32
pmcode	ends
DGROUP	group	rmdata,pmdata

CONBIOS		equ	91h
STACK_SIZE	equ	(8*1024)	; !!! Check of Size !!!

extrn	cons_bios:near			; Consol Bios "C" Funcsion

;**********************************************
;
;	Real Mode Data Segment defs
;
;**********************************************
rmdata	segment

real_ds	dw	?
real_es	dw	?
vdb_prt	dd	far

rmdata	ends

;**********************************************
;
;	Real Mode Code Segment Funcsion
;
;**********************************************
rmcode	segment word public use16
	assume	cs:rmcode,ds:DGROUP

in_dos_call	proc	far

	mov	ah,34h
	int	21h
	mov	ax,es
	ret

in_dos_call	endp

vdbios_real	proc	far

	mov	real_ds,ds
	mov	real_es,es
	jmp	[vdb_prt]

vdbios_real	endp

rmcode	ends

;**********************************************
;
;	Protect Mode Data Segment defs
;
;**********************************************
pmdata	segment

nt_vet_off	dd	?	; offset +
nt_vet_seg	dw	?	; segment = pword !!
rl_vet_ral	dd	?

save_esp	dd	?	; offset +
save_ss		dw	?	; segment = pword !!

stack_flg	dw	0	; stack access flg

new_esp		dd	?	; offset +
new_ss		dw	?	; segment = pword !!

		public	in_dos_adr
in_dos_adr	dd	0	; in dos flg in MS-DOS 1M Selecter

consol_path	db	"CON",0
null_path	db	"NUL",0

		align	4			; align set
local_stack	db	STACK_SIZE dup(?)	; local stack era
local_stack_btm	dw	?			; dummy

pmdata	ends

;**********************************************
;
;	Protect Mode Code Segment Funcsion
;
;**********************************************
pmcode	segment
	assume	cs:pmcode,ds:DGROUP

;***************************************************
;	Consol Bios Old Vectror ReSet
;
;void	cons_bios_ret(void)
;
;call to void cons_bios(struct reg *rp)
;***************************************************
cons_bios_entry	proc	far
	cmp	stack_flg,0
	jne	short go_ret
	inc	stack_flg

	mov	save_esp,esp		; save ss:esp 
	mov	save_ss,ss
	lss	esp,pword ptr new_esp 	; Load Loacl Stack Address
	pushad

	sti
	cld

	push	ds
	pop	es
	mov	ebp,esp
	push	ebp
	call	cons_bios
	add	esp,4

	popad
	lss	esp,pword ptr save_esp

	dec	stack_flg
go_ret:	iretd

cons_bios_entry	endp

;************************************
; File I/O ReDirect Asm Call onry!
;************************************
reopen	proc	near
	push	ax
	mov	ah,3Eh		; close
	int	21h
	pop	ax
	mov	ah,3Dh		; open file
	int	21h
reo_ret:ret

reopen	endp

;***************************************************
;	Consol Bios Entry SetUp
;
;void	cons_bios_set(void)
;***************************************************
	public	cons_bios_set
cons_bios_set	proc	near

	push	ds
	push	es
	push	ebx
	push	edx

	mov	ax,2502h		; get neitive int vect
	mov	cl,CONBIOS
	int	21h
	mov	nt_vet_seg,es
	mov	nt_vet_off,ebx

	mov	ax,2503h
	mov	cl,CONBIOS
	int	21h
	mov	rl_vet_ral,ebx

	mov	ax,2506h
	mov	cl,CONBIOS
	push	ds
	mov	dx,cs
	mov	ds,dx
	lea	edx,cons_bios_entry
	int	21h
	pop	ds

	mov	ax,2503h
	mov	cl,CONBIOS
	int	21h
	mov	vdb_prt,ebx

	mov	ax,250Fh
	push	ds
	pop	es
	lea	ebx,vdbios_real
	mov	ecx,0
	int	21h

	mov	ax,2505h
	mov	ebx,ecx
	mov	cl,CONBIOS
	int	21h

	mov	new_ss,ds
	lea	ebx,local_stack_btm
	mov	new_esp,ebx

	mov     al,0
	mov     bx,0
	lea     edx,consol_path
	call    reopen
	mov     al,1
	mov     bx,1
	call    reopen
	mov     al,1
	mov     bx,2
	call    reopen

	pop	edx
	pop	ebx
	pop	es
	pop	ds
	ret

cons_bios_set	endp

;***************************************************
;	Consol Bios Old Vectror ReSet
;
;void	cons_bios_ret(void)
;***************************************************
	public	cons_bios_ret
cons_bios_ret	proc	near

	push	ds
	push	ebx
	push	edx

	mov	cl,CONBIOS
	mov	ebx,rl_vet_ral
	mov	edx,nt_vet_off
	mov	ax,nt_vet_seg
	push	ds
	mov	ds,ax	
	mov	ax,2507h
	int	21h
	pop	ds

	mov	al,0
	mov	bx,0
	lea	edx,null_path
	call	reopen
	mov	al,1
	mov	bx,1
	call	reopen
	mov	al,1
	mov	bx,2
	call	reopen

	pop	edx
	pop	ebx
	pop	ds
	ret

cons_bios_ret	endp

;***************************************************
;	MS-DOS Funcsion Call Check!
;
;ont	in_dos_check(void)
;***************************************************
	public	in_dos_check
in_dos_check	proc	near
	push	ds
	push	edi

	cmp	in_dos_adr,0
	jne	indos1

	pushad
	push	ds
	push	es

	mov	ax,250Fh
	push	ds
	pop	es
	lea	ebx,in_dos_call
	mov	ecx,0
	int	21h

	mov	ax,250Eh
	mov	ebx,ecx			; real address
	mov	ecx,0			; stack data = 0
	int	21h

	pop	es
	pop	ds
	movzx	eax,ax
	shl	eax,4
	movzx	ebx,bx
	add	eax,ebx			; seg >> 4 | off
	mov	in_dos_adr,eax
	popad

indos1:	mov	edi,in_dos_adr
	mov	ax,0034h		; MS-DOS 1M Real Selecter
	mov	ds,ax
	movzx	eax,byte ptr [edi]
	pop	edi
	pop	ds
	ret

in_dos_check	endp

pmcode	ends

	end
