;****************************** INCLUDE HEADER (.MDF) ************************;
;*                                                                           *;
;* INCLUDE FILE: Mediator.mdf                                                *;
;*                                                                           *;
;* MACHINE: NGenSGen   LANGUAGE: ASSEMBLER  OS: CTOS                        *;
;*                                                                           *;
;* DESCRIPTION:                                                              *;
;*                                                                           *;
;* Macros for Mediator.asm.                                                  *;
;*                                                                           *;
;* TITLE:  CTOS SYSTEM SOFTWARE                                              *;
;*                                                                           *;
;*            PROPRIETARY PROGRAM MATERIAL                                   *;
;*                                                                           *;
;* THIS MATERIAL IS PROPRIETARY TO UNISYS CORPORATION AND IS NOT TO BE       *;
;* REPRODUCED, USED OR DISCLOSED EXCEPT IN ACCORDANCE WITH PROGRAM LICENSE   *;
;* OR UPON WRITTEN AUTHORIZATION OF THE PATENT DIVISION OF UNISYS            *;
;* CORPORATION, DETROIT, MICHIGAN 48232, USA.                                *;
;*                                                                           *;
;*                                                                           *;
;* COPYRIGHT (C) 1980, 1993 CONVERGENT INCORPORATED. ALL RIGHTS RESERVED     *;
;* COPYRIGHT (C) 1980, 1993 UNISYS CORPORATION. ALL RIGHTS RESERVED          *;
;* ************************************************************************* *;
;*                                                                           *;
;* UNISYS BELIEVES THAT THE SOFTWARE FURNISHED HEREWITH IS ACCURATE AND      *;
;* RELIABLE, AND MUCH CARE HAS BEEN TAKEN IN ITS PREPARATION.  HOWEVER,      *;
;* NO RESPONSIBILITY, FINANCIAL OR OTHERWISE, CAN BE ACCEPTED FOR ANY        *;
;* CONSEQUENCES ARISING OUT OF THE USE OF THIS MATERIAL, INCLUDING LOSS OF   *;
;* PROFIT, INDIRECT, SPECIAL, OR CONSEQUENTIAL DAMAGES, THERE ARE NO         *;
;* WARRANTIES WHICH EXTEND BEYOND THE PROGRAM SPECIFICATION.                 *;
;*                                                                           *;
;* THE CUSTOMER SHOULD EXERCISE CARE TO ASSURE THAT USE OF THE SOFTWARE      *;
;* WILL BE IN FULL COMPLIANCE WITH LAWS, RULES AND REGULATIONS OF THE        *;
;* JURISDICTIONS WITH RESPECT TO WHICH IT IS USED.                           *;
;*                                                                           *;
;*                                                                           *;
;*************************** END OF MODULE HEADER ****************************;

$MOD386

PUBLIC	Mediator
PUBLIC	MediatorTable
PUBLIC	semMediator

EXTRN	_end_Stack	: WORD
EXTRN	SemLock		: FAR
EXTRN	SemClear	: FAR

MediatorTbl		SEGMENT WORD PUBLIC 'MediatorTbl'
	MediatorTable	LABEL BYTE
MediatorTbl		ENDS

Data		SEGMENT WORD PUBLIC 'Data'
	semMediator		DD 0
Data		ENDS

DGroup		GROUP Data

MediatorCode SEGMENT WORD PUBLIC 'Code'
ASSUME CS:MediatorCode, DS:NOTHING

oArgs	EQU	WORD PTR [BP+16]	; 1st word after arguments
pProc	EQU	WORD PTR [BP+6]		; pProc to call
sParms	EQU	WORD PTR [BP+4]		; number of bytes passed to function

Mediator PROC NEAR

	PUSH BP
	MOV  BP, SP
	PUSH DS
	MOV  AX, DGroup
	MOV  DS, AX

; lock data group from other processes (1 lock per user)
	MOV  EAX, DS:DWORD PTR [semMediator]
	PUSH EAX
	MOV  AX, 0FFFFh				; wait indefinitely
	PUSH AX
	CALL SemLock

; set ES:DI to point to first word of DLL stack
	PUSH DS
	POP  ES
	LEA  DI, ES:WORD PTR [_end_Stack]
	SUB  DI, 2

; setup new stack as follows (backwards):
;	current SS
;	current SP
;	function's parameters
	MOV  ES:WORD PTR [DI], SS
	MOV  ES:WORD PTR [DI2], SP
	SUB  DI, 4
	PUSH SS
	POP  DS
	LEA  SI, oArgs				; point to word right after arguments
	MOV  CX, sParms
	ADD  SI, CX					; point to first word of arguments
	STD							; backwards for stack copying
	SHR  CX, 1
	REP  MOVSW
	CLD							; Microsoft C string routines don't clear
	ADD  DI, 2					; point to last word moved

; set SI to stack address of cs:ip of function to call
	LEA  SI, pProc

; set DS for medium model
	PUSH ES
	POP  DS

; save current SS:SP
	MOV  CX, SP
	MOV  DX, SS

; switch stacks and store old one
	CLI
	MOV  AX, ES
	MOV  SS, AX
	MOV  SP, DI
	STI

; call procedure
	MOV  ES, DX					; DX holds old SS
	CALL DWORD PTR ES:[SI]

; restore old stack
	POP  CX
	POP  DX
	CLI
	MOV  SS, DX
	MOV  SP, CX
	STI

	PUSH	AX				; save call proc's return value

; unlock semaphore
	MOV  EAX, DS:DWORD PTR [semMediator]
	PUSH EAX
	CALL SemClear

	POP  AX
	POP  DS
	POP  BP
	RET  6

Mediator ENDP

MediatorCode ENDS


*define(Mediate(Name, sParms)) (
EXTRN	Name	: FAR

' store procedure address in table
PUBLIC	p_Name

MediatorTbl		SEGMENT
	p_Name	DD	Name
MediatorTbl		ENDS

' create stub call to Mediator()
MediatorCode	SEGMENT
ASSUME		CS:MediatorCode
PUBLIC		l_Name

l_Name PROC FAR
	PUSH SI
	PUSH DI
	MOV  AX, MediatorTbl
	MOV  ES, AX
	LES  BX, ES:DWORD PTR [p_Name]
	PUSH ES
	PUSH BX
	PUSH sParms
	CALL Mediator
	POP  DI
	POP  SI
	RET  sParms

l_Name ENDP

MediatorCode ENDS
)
