; Gr DPMI support, Copyright 1994 by Matthias K”ppe

		INCLUDE grdef.asm

DATA		SEGMENT WORD PUBLIC

Segs		DW	6 DUP (?)

DATA		ENDS

CODE		SEGMENT BYTE PUBLIC

		PUBLIC	SelOfs, SelOfsZero, FillSegTbl

SelOfs		PROC FAR

		; does not destroy register values

		ARG	ParOfs: DWORD = ArgSize

		push	bp
		mov	bp, sp
		MOV	DX, WORD PTR ParOfs[2]
		MOV	AX, WORD PTR ParOfs[0]
		CMP	DX, 0A000H
		JB	@@1
		PUSH	DX
		SHL	DX, 4
		ADD	AX, DX
		POP	DX
		SUB	DX, 0A000H
		SHR	DX, 11
		AND	DL, 0FEH
		XCHG	DX, SI
		MOV	SI, Segs[SI]
		XCHG	DX, SI
@@1:            pop	bp
		retf	ArgSize
SelOfs  	ENDP

SelOfsZero	PROC FAR
		ARG     ParOfs: DWORD = ArgSize
		push	bp
		mov	bp, sp
		mov	bx, WORD PTR ParOfs[2]
		mov	ax, 0002H		; segment to descriptor
		int	31h
		jnc	@@1
		xor	ax, ax
@@1:		mov	dx, ax
		mov	ax, WORD PTR ParOfs[0]
		pop	bp
		retf	ArgSize
SelOfsZero	ENDP

FillSegTbl	PROC NEAR
		MOV	BX, 0A000H
		MOV	SI, OFFSET Segs
@@1:		push	bx si
		MOV	AX, 0002H		; segment to descriptor
		INT	31H
		JNC	@@2
		XOR	AX, AX
@@2:		pop	si bx
		MOV	[SI], AX
		ADD	SI, 2
		ADD	BX, 1000H
		JNZ	@@1
		retn
FillSegTbl	ENDP

CODE		ENDS

		END
