include	clib.inc
include	conio.inc
include	malloc.inc

PPROC	rsopen,	idd:dword
	push	si
	push	di
	push	ds
	lds	si,idd
	mov	ax,[si+8]
	mov	dx,ax
	mul	ah
	mov	word ptr idd,ax
	add	ax,ax
	mov	di,ax
	test	word ptr [si+2],_D_SHADE
	jz	rsopen_00
	add	dl,dh
	add	dl,dh
	mov	dh,0
	sub	dx,2
	add	di,dx
    rsopen_00:
	push	word ptr [si]
	call	malloc
	mov	bx,di
	mov	es,dx
	mov	di,ax
	mov	dx,ax
	mov	cx,[si]
	jz	rsopen_end
	xor	ax,ax
	cld
	rep	stosb
	mov	cx,dx
	mov	di,dx
	lodsw
	lodsw
	or	ax,_D_SETRC
	push	ax
	stosw			; .flag
	movsw			; .count - .index
	movsw			; .rect
	movsw
	xor	ax,ax
	mov	al,[si-6]
	inc	ax
	shl	ax,cl
	add	ax,cx
	stosw			; .wp (off)
	mov	dx,ax
	mov	ax,es
	stosw			; .wp (seg)
	mov	ax,16+4
	stosw			; .object (off)
	mov	ax,es
	stosw			; .object (seg)
	add	dx,bx		; end of wp = start of object alloc
	xor	bx,bx
	mov	bl,[si-6]
	inc	bx
	jmp	rsopen_04
    rsopen_01:
	movsw			; copy 8 byte
	movsw
	movsw
	movsw
	xor	ax,ax		; get alloc size of object
	mov	al,[si-6]
	shl	ax,cl		; * 16
	test	ax,ax
	jz	rsopen_02
	xchg	ax,dx		; offset of mem (.data)
	stosw
	add	dx,ax
	mov	ax,es
	stosw
	xor	ax,ax
	jmp	rsopen_03
    rsopen_02:
	stosw
	stosw
    rsopen_03:
	stosw
	stosw
    rsopen_04:
	dec	bx
	jnz	rsopen_01
	pop	ax
	push	di
	inc	di
	mov	cx,word ptr idd
	and	ax,_D_RESAT
	jz	rsopen_05
	call	@rsunzipat
	jmp	rsopen_06
    rsopen_05:
	call	@rsunzipch
    rsopen_06:
	pop	di
	mov	cx,word ptr idd
	call	@rsunzipch
	mov	dx,es
	mov	bx,0004h
	mov	ax,bx
	test	ax,ax
    rsopen_end:
	pop	ds
	pop	di
	pop	si
	ret
PEND	rsopen

	END