	model	large,pascal
	locals	@@

Offs		equ	(word ptr 0)
Segm		equ	(word ptr 2)

ECB_S	struc
	Env_Seg	dw	?
	Cmd_L	dd	?
	FCB1	dd	?
	FCB2	dd	?
	SS_SP	dd	?
	CS_IP	dd	?
	ends

	extrn	DoneDebug:far,InitDebug:far

	.data

	extrn	CurRegs:dword,DebugSP:word

DPID	dw	0

	.data?

	extrn	PrefixSeg:word

ECB		ECB_S	<>
SaveSP		dw	?
SaveStk	dd	?

	.code

	public	BuildPSP
BuildPSP	proc	pascal far CmdLine:dword
	uses	ds
	mov	es,PrefixSeg
	mov	di,80h
	lds	si,CmdLine
	cld
	lodsb
	stosb
	xor	ah,ah
	xchg	ax,cx
	rep	movsb
	mov	al,0dh
	stosb
	push	es
	pop	ds
	mov	si,81h
	mov	di,5ch
	mov	ax,2901h
	int	21h
	mov	di,6ch
	mov	ax,2901h
	int	21h
	ret
BuildPSP	endp

GetStack	proc	near
	mov	es,PrefixSeg
	mov	ax,es:[2eh].Offs
	mov	SaveStk.Offs,ax
	mov	ax,es:[2eh].Segm
	mov	SaveStk.Segm,ax
	ret
GetStack	endp

SetStack	proc	near
	mov	es,PrefixSeg
	mov	ax,SaveStk.Offs
	mov	es:[2eh].Offs,ax
	mov	ax,SaveStk.Segm
	mov	es:[2eh].Segm,ax
	ret
SetStack	endp

	public	LoadProgram
LoadProgram	proc	pascal far Path:dword
	lea	bx,ECB
	push	ds
	pop	es
	mov	ds,PrefixSeg
	mov	di,bx
	cld
	mov	ax,ds:[2ch]
	stosw
	mov	ax,80h
	stosw
	mov	ax,ds
	stosw
	mov	ax,5ch
	stosw
	mov	ax,ds
	stosw
	mov	ax,6ch
	stosw
	mov	ax,ds
	stosw
	push	es
	pop	ds
	push	ds:[2eh].Offs ds:[2eh].Segm
	mov	SaveSP,sp
	mov	sp,DebugSP
	lds	dx,Path
	mov	ax,4b01h
	int	21h
	mov	dx,seg _Data
	mov	ds,dx
	cli
	mov	ss,dx
	mov	sp,SaveSP
	sti
	pushf
	push	ax
	mov	ah,30h
	int	21h
	xchg	al,ah
	cmp	ax,2*256+10
	jae	@@1
	mov	es,PrefixSeg
	mov	ax,ds:[2eh].Offs
	mov	es:[2eh].Offs,ax
	mov	ax,ds:[2eh].Segm
	mov	es:[2eh].Segm,ax
@@1:	pop	ax
	popf
	pop	ds:[2eh].Segm ds:[2eh].Offs
	jc	@@2
	call	GetStack
	xor	ax,ax
	jmp	short @@3
@@2:	xchg	ax,dx
	mov	ax,-2
	cmp	dl,2
	je	@@3
	mov	al,-4
	cmp	dl,8
	je	@@3
	mov	al,-6
@@3:	ret
LoadProgram	endp

	public	EmulateLoad
EmulateLoad	proc	pascal far PID:word,MemTop:word,EntryPoint:dword,StackPtr:dword
	les	ax,EntryPoint
	mov	ECB.CS_IP.Offs,ax
	mov	ECB.CS_IP.Segm,es
	les	ax,StackPtr
	mov	ECB.SS_SP.Offs,ax
	mov	ECB.SS_SP.Segm,es
	mov	SaveSP,sp
	mov	sp,DebugSP
	mov	dx,PID
	mov	si,MemTop
	mov	ah,55h
	int	21h
	mov	es,PID
	mov	ax,MemTop
	mov	es:[2],ax
	mov	sp,SaveSP
	call	GetStack
	ret
EmulateLoad	endp

	public	RunProgram
RunProgram	proc	far
	push	bp
	mov	ah,51h
	int	21h
	call	SetStack
	mov	SaveSP,sp
	cli
	mov	ss,ECB.SS_SP.Segm
	mov	sp,ECB.SS_SP.Offs
	sti
	les	ax,ECB.CS_IP
	push	es ax
	mov	ds,bx
	mov	es,bx
	mov	ds:[0ah].Offs,offset @@1
	mov	ds:[0ah].Segm,cs
	retf
@@1:	mov	ax,seg _Data
	mov	ds,ax
	cli
	mov	ss,ax
	mov	sp,SaveSP
	sti
	mov	ah,4dh
	int	21h
	pop	bp
	ret
RunProgram	endp

	public	InitProgram
InitProgram	proc	far
	push	bp
	mov	ah,51h
	int	21h
	mov	DPID,bx
	les	di,CurRegs
	cld
	xor	ax,ax
	mov	cx,7
	rep	stosw
	mov	ax,bx
	stosw
	stosw
	mov	ax,ECB.CS_IP.Offs
	stosw
	mov	ax,ECB.CS_IP.Segm
	stosw
	pushf
	pop	ax
	stosw
	mov	ax,ECB.SS_SP.Offs
	stosw
	mov	ax,ECB.SS_SP.Segm
	stosw
	mov	es,bx
	mov	es:[0ah].Offs,offset @@1
	mov	es:[0ah].Segm,cs
	push	bx
	call	InitDebug
	mov	bx,PrefixSeg
	mov	ah,50h
	int	21h
	mov	ax,DPID
	add	ax,10h
	pop	bp
	ret
@@1:	mov	ax,seg _Data
	mov	ds,ax
	cli
	mov	ss,ax
	mov	sp,DebugSP
	sti
	mov	ah,4dh
	int	21h
	push	ax
	xor	ax,ax
	mov	DPID,ax
	call	DoneDebug
InitProgram	endp

	public	ExitProgram
ExitProgram	proc	far
	push	bp
	mov	bx,DPID
	or	bx,bx
	jz	@@2
	call	SetStack
	mov	SaveSP,sp
	mov	es,bx
	mov	es:[0ah].Offs,offset @@1
	mov	es:[0ah].Segm,cs
	mov	ah,50h
	int	21h
	mov	ax,4c00h
	int	21h
@@1:	mov	ax,seg _Data
	mov	ds,ax
	cli
	mov	ss,ax
	mov	sp,SaveSP
	sti
	xor	ax,ax
	mov	DPID,ax
@@2:	les	di,CurRegs
	cld
	xor	ax,ax
	mov	cx,14
	rep	stosw
	pop	bp
	ret
ExitProgram	endp

	end
