	.386p

	extrn	_errno:WORD

_TEXT	SEGMENT	dword public use32 'CODE'
	ASSUME	cs:_TEXT

;	extrn	_set_errno_:NEAR
	public	get_real_window_

;  short get_real_window(paddr, len)
;  int paddr, len;
; /* paddr is EAX */
; /* len is EDX */

get_real_window_	PROC NEAR
;	get selector ????
	push	esi
	push	ebx
	push	ecx
;	; put the base (physical address) in SI:BX and the length
;	; in CX:DX
;	movzx	ebx,ax		; lower 16 bits of paddr in BX
;	shr	eax,16		; get higher 16 bits
;	mov	esi,eax		; upper 16 bits in SI
;
;	mov	ecx,edx		; len
;	shl	edx,16		; zero upper 16 bits
;	shr	edx,16
;	shr	ecx,16		; put upper 15 bits of len in CX
;	mov	ax,0e803h
;	int	21h
;	jc	error
;	pop	ecx
;	pop	ebx
;	pop	esi
;	ret

	INT	3
;error:
;	movzx	eax,ax
;	call	_set_errno_
	xor	eax,eax
	pop	ecx
	pop	ebx
	pop	esi
	ret
get_real_window_	ENDP

	public	delete_selector_
;
;  void delete_selector(selector)
; /* selector is EAX */
;
delete_selector_	PROC NEAR
	push	ebx
	mov	bx,ax	; selector
	mov	eax,1	; function 0001 = free selector
	int	31h
	jc	error1
	xor	eax,eax
error1:
	pop	ebx
	neg	eax
	ret
delete_selector_	ENDP

	public	code2data_

; int code2data(u_char _far *cp)
; /* cp in DX:EAX */
;
; Allocate a R/W selector as an alias to the code selector passed
;
code2data_	PROC NEAR
	push	ebx
	mov	ebx,edx
	mov	eax,0ah
	int	31h
	pop	ebx
	jc	c2d_error
	ret
c2d_error:
	xor	eax,eax
	dec	eax
	ret
code2data_	ENDP

	public	get_base_
; int get_base(short sel)
get_base_	PROC NEAR
	push	ebx
	push	ecx
	push	edx
	mov	ebx,eax		; selector
	mov	eax,06h
	int	31h
	jc	short gb_error
	mov	eax,ecx		; upper 16 bits
	shl	eax,16
	mov	ax,dx		; lower 16 bits
	jmp	short gb_exit
gb_error:
	xor	eax,eax
gb_exit:
	pop	edx
	pop	ecx
	pop	ebx
	ret
get_base_	ENDP


;	public	get_window_
;
; int get_window(u_char _far *cp, u_long len)
; /* cp in DX:EAX */
; /* len in EBX */
;
;get_window_	PROC NEAR
;	; put the base (cp) into si:bx, put the len into cx:dx
;	push	ecx
;	push	ds
;	push	esi
;	mov	ds,dx		; parent selector in DS
;	mov	edx,ebx		; len
;	mov	esi,eax
;	mov	ebx,esi		; lower 16 bits in BX
;	shr	esi,16		; upper 16 bits in SI
;
;	mov	ecx,edx		; len in CX ->
;	shr	ecx,16		; upper 16 bits
;	mov	ax,0e801h
;	int	21h
;	movzx	eax,ax
;	jnc	gw_ok
;	neg	eax		; negative means error
;gw_ok:
;	pop	esi
;	pop	ds
;	pop	ecx
;	ret
;get_window_	ENDP

;	PUBLIC	get_window_info_
; int get_window_info(selector, pinfo)
;	unsigned short selector;	/* EAX */
;	struct win_info *pinfo;		/* EDX */
;get_window_info_	PROC NEAR
;	push	ebx
;	push	ecx
;	push	esi
;	push	edi
;	push	edx		; save pinfo
;	mov	ebx,eax		; selector goes in ebx
;	mov	ah,0edh
;	int	21h
;	jc	gwi_err
;	; convert CX:DX into one 32-bit dword
;	shl	ecx,16
;	mov	cx,dx
;	; convert si:bx into one 32-bit dword
;	shl	esi,16
;	mov	si,bx
;	pop	edx		; get pinfo back
;	mov	[edx],al
;	mov	[edx+1],ecx
;	mov	[edx+5],esi
;	mov	[edx+9],di
;	pop	edi
;	pop	esi
;	pop	ecx
;	pop	ebx
;	xor	eax,eax		; return success
;	ret
;gwi_err:
;	pop	edx
;	pop	edi
;	pop	esi
;	pop	ecx
;	pop	ebx
;	movzx	eax,ax
;	call	_set_errno_
;	ret
;get_window_info_	ENDP

; It's a poor place to put this, but it should be in OS and this is
; the only .asm file in the OS directory.

	PUBLIC  n_clicks_
; u_long n_clicks(void)
;
;	get the number of timer clicks from BIOS
;
n_clicks_  proc	near
	push	ecx
	push	edx
	mov	ah,0
	int	1ah
	mov	ax,cx		; high word
	shl	eax,16
	mov	ax,dx		; low word
	pop	edx
	pop	ecx
	ret
n_clicks_ endp

;PUBLIC  rpc_init_
;
;	short rpc_init(char *name)
;	/* opens the real-mode code named by <name>.  If the call
;	 * fails for some reason (such as <name> not existing), thi
;	 * function returns -1 and set errno.  */
;

;rpc_init_      PROC NEAR
;	push	edx
;	mov	edx,eax		; name
;	mov	ah,0e0h
;	int	21h
;	pop	edx
;	jc	short bad
;	xor	ah,ah
;	ret
;bad:
;	movzx	eax,ax
;	call	_set_errno_
;	xor	eax,eax
;	dec	eax		; return -1;
;	ret
;rpc_init_	ENDP

; Get the country code from DOS

PUBLIC	kbd_country_
kbd_country_	PROC NEAR
	push	ebp
	mov	ebp,esp
	sub	esp,38
	mov	edx,esp
	mov	eax,3800h
	int	21h
	and	eax,0ffh
	cmp	al,-1
	jne	kc_1
	mov	eax,ebx
kc_1:
	mov	esp,ebp
	pop	ebp
	ret
kbd_country_	ENDP

_TEXT	ENDS
	END
