;       SCCSID = @(#)d_allc.asm	6.1 91/02/15
;	SCCSID = @(#)d_allc.asm	1.4 90/01/22
; ****************************************************************************
; *									     *
; *			  IBM/Microsoft Confidential			     *
; *									     *
; *		    Copyright (c) IBM Corporation  1987, 1990		     *
; *		    Copyright (c) Microsoft Corp.  1987, 1990		     *
; *			      All Rights Reserved			     *
; *									     *
; ****************************************************************************
;***  d_allc.asm - API stub routine DosAllocSeg
;
;	Member of API library
;	Bill Mershon
;	Copyright MicroSoft Corporation, 1986
;	20-Apr-1986
;
;	library routine for API simulator
;
;	Called from dos 2.x,3.x compatible dos 5.x applications
;
;	Special Information
;		none
;
;	Modification History
;
;	06-Jan-87 (billme): Replace initial pushes and final pops
;	with calls to __api_begin and __api_end resp.  This will
;	reduce the size of FAPI and allow support for signals.
;
;	20-Jul-87 (robertc): Change mask on flags from ffffh to 7fffh
;	to allow the Movable bit to be ignored.

INCLUDE basemaca.inc
INCLUDE dos2equ.inc
INCLUDE error.inc

	EXTRN	__api_begin:near
	EXTRN	__api_end:near


;***	DOSALLOCSEG - stub to emulate DOSALLOCSEG function call
;
;	simulates DOS 5.0 call from DOS 2.x,3.x environment
;
;	errorcode = unsigned far pascal
;	DosAllocSeg(LocSize, ptr_Selector, ShareInd)
;
;	ENTRY:
;		LocSize - size in bytes (rounded up to nearest paragraph)
;		ptr_Selector - pointer to block of mem allocated
;		ShareInd - 0 => not shared ... 1 => shared
;
;	RETURN:
;		AX = 0 if no error, else error code (see DOS 5 spec)
;
;	EXIT:
;		if no error (AX == 0) WORD at ptr_Selector contains
;		segment address of memory block
;
;	USES:
;		AX
;
;	Pseudo Code:
;		save registers
;		use BP as pointer to stack parameters
;		round LocSize up to nearest paragraph (16 bytes)
;		execute int 21 with function 48h
;		restore registers
;		return
;
;	WARNING:
;		may return error code from Dos 2.x, 3.x (see dos manuals)
;
;	EFFECTS:
;		none
;

	title	DOSALLOCSEG - stub to emulate DOSALLOCSEG function call
;
;	EXTRN	DOSALLOCSEG:FAR
;
;	PUSH	WORD LocSize
;	PUSH	@ WORD ptr_Selector
;	PUSH	WORD ShareInd
;	CALL	DOSALLOCSEG
;

_TEXT	segment byte public 'CODE'

	assume	CS:_TEXT,DS:NOTHING

	public	DOSALLOCSEG

LocSize		equ	[BP+12]
ptr_Selector	equ	[BP+8]
ShareInd	equ	[BP+6]

DOSALLOCSEG	proc	far

	call	__api_begin	; init bp and save bp,ds,bx,cx,dx,si,di,es
				; required to save: bp,ds,bx

					;@@(1.11) chg mask from ffffh to 7fffh
	test	ShareInd,07fffh		; if share is zero
	jz	alloc0			; then it is OK
					; else report error
	mov	ax,error_invalid_parameter
	jmp	ErrorExit
alloc0:
	mov	ax,LocSize		; AX <- size requested
	or	ax,ax
	jnz	alloc1			; AX != 0 => less than 65536 desired
	or	ax,0ffffh		; prepare to allocate 65536
alloc1:
	xor	bx,bx			; BX <- round up 0
	test	ax,0fh
	jz	alloc2			; no round up needed
	or	bx,1			; BX <- round up 1
alloc2:
	shr	ax,1
	shr	ax,1			; convert to paragraphs
	shr	ax,1
	shr	ax,1
	add	bx,ax			; BX <- number of paragraphs requested

	mov	ah,D2_AllocMem
	int	21H			; allocate memory
	jc	ErrorExit
	lds	bx,[ptr_Selector]	; DS:BX <- pointer to addr of ret
	mov	ds:[bx],ax

	xor	ax,ax
ErrorExit:

	call	__api_end	; check sigs & restore es,di,si,dx,cx,bx,ds,bp

	ret	8	; clear stack of entry parameters (bytes)
DOSALLOCSEG	endp

_TEXT	ends

	end
