IFaceLevel1	EQU	1 
IFaceLevel2	EQU	2 
IFaceLevel3	EQU	3 
IFaceLevel4	EQU	4 
G_COUNTMASK	EQU	1FH 
if      NOT RAS
RASpretrace     macro                   
                endm

RASposttrace    macro                   
                endm
endif







































DefCallGate     macro   lit,name,parms,ord,gatename,cs32bit,auxord,fring
    local x,?parms
    ifndef name
        CODE segment
            extrn name:far
        CODE ends
    endif
    ifb <gatename>
        GDTCGSEG segment
        if1
            ife ord
                %out DefCallGate: lit has zero ordinal
                .err
            endif
        endif
    else
        GDTPUBSEG segment
        public gatename
        gatename label word
    endif
    x label word
    ?parms = ((parms+1)/2)
    .errnz (?parms and not G_COUNTMASK) 
    ifb <cs32bit>
        gate <name,?parms,D_GATE332,0>
    else
        gate <offset FLAT:name,?parms,D_GATE332,-1> 
    endif
    ifb <gatename>
        GDTCGSEG ends
    else
        GDTPUBSEG ends
    endif



    if ord ne 0
    ifb <auxord>
        if parms and 1
            GenEntryTbl lit,ord,IS_GATE,<GSEL x+RPL_RING3>
        else
            GenEntryTbl lit,ord,IS_GATE,<GSEL x+RPL_RING2>
        endif
    else
        GenEntryIndirect lit,ord,auxord,fring,IS_INDGATE,<GSEL x+RPL_RING3>
    endif
    endif
endm


dce_s	STRUC
dce_flags	DB	?
dce_gate	DW	?
dce_s	ENDS

ldrdce_t struc
db size dce_s dup(?)
ldrdce_t ends

IS_DOSCALL1	EQU	0                
IS_ABSOLUTE	EQU	1                
IS_GATE	EQU	2                
IS_INDGATE	EQU	3                
IS_INDABSOLUTE	EQU	4                
IND_RING0	EQU	1                
IND_RING3	EQU	2                
NO_GATE	EQU	0 











GenEntryTbl     macro   lit,ord,flags,callgate
    local a,b
    .386p
    ifnb <lit>
        if (flags eq IS_GATE) or (flags eq IS_ABSOLUTE)
            DOSMTENAME segment





            DOSMTENAME ends
        endif
    endif
    DOSMTEENTRY segment
        org     ((ord - 1) * (size dce_s))
        dce_s   <flags, &callgate>
    DOSMTEENTRY ends
    CpuMode reset
endm









VDDGenEntryTbl  macro   addr,ord,fabs
    .errb <addr>
    .errb <ord>
    if2
        ifb <fabs>
            ifndef addr
                extrn addr:near
            endif
        endif
    endif
    VDDMTEENTRY segment
        org     ((ord - 1) * 4)         
        ifb <fabs>
            dd  offset FLAT:addr
        else
            dd  addr
        endif
    VDDMTEENTRY ends
endm







fsh_GenEntryTbl macro   addr,ord
    .386p
    FSHMTEENTRY segment
        org     ((ord - 1) * 4)         
        dw      offset addr
        dw      seg addr
    FSHMTEENTRY ends
    CpuMode reset
endm











GenEntryIndirect macro  lit,ord,auxord,fring,flags,value
    if (fring eq IND_RING0)
        GenEntryTbl lit,ord,flags,auxord
    endif
    GenAuxEntry auxord,fring,<value>
endm









GenAuxEntry     macro   auxord,flags,value
    .386p
    DOSMTEAUX segment
    if (flags eq IND_RING0)
        org     ((auxord) * (size adce_s) + offset adce_ring0)
    else
        org     ((auxord) * (size adce_s) + offset adce_ring3)
    endif
    dw  value
    DOSMTEAUX ends
    CpuMode Reset
endm















FSHentry macro  procname,ordinal,testarg
    ifb <procname>
        if2
            %out Fatal Error - FSHentry called with no arguments.
        endif
        .err2
        exitm
    endif

    ifb <ordinal>
        if2
            %out Fatal Error - FSHentry &procname called with too few args.
        endif
        .err2
        exitm
    endif

    ifnb <testarg>
        if2
            %out Fatal Error - FSHentry &procname called with too many args.
        endif
        .err2
        exitm
    endif
    Procedure procname,far
    fsh_GenEntryTbl procname,ordinal
endm















sreg            struc
sreg_di         dw      ?       
sreg_si         dw      ?       
sreg_es         dw      ?
sreg_ds         dw      ?
sreg_tempbuf1   dd      ?       
sreg_tempbuf2   dd      ?       
sreg_filler     dw      ?       
sreg_rpl        dw      ?       
sreg            ends











kstk_frame      struc
                db      size sreg dup(?)    
kstk_ssf        db      size ssf_s dup(?)   
kstk_args       dw      ?                   
kstk_frame      ends


?SCI_OK         equ 0
?SCI_ERROR      equ 1

?SCI_UNKNOWN    equ 0
?SCI_FLAT       equ 1

?SCI_NOT_IN_USE equ 0
?SCI_IN_USE     equ 1
?SCI_SS_RPL     equ 2
?SCI_WKREG      equ 3


sci_init macro
    ?sci_ax = ?SCI_NOT_IN_USE
    ?sci_bx = ?SCI_NOT_IN_USE
    ?sci_cx = ?SCI_NOT_IN_USE
    ?sci_dx = ?SCI_NOT_IN_USE
    ?sci_di = ?SCI_NOT_IN_USE
    ?sci_si = ?SCI_SS_RPL
    ?sci_sreg_si = ?SCI_NOT_IN_USE
    ?sci_sreg_di = ?SCI_NOT_IN_USE
    ?sci_ds = ?SCI_FLAT
    ?sci_es = ?SCI_UNKNOWN
    ?sci_argptr = kstk_args
    endm






MAXRETN         =       36              









































SysEntryBegin   macro   usrname,ordinal,argwords,gatename,inline,abase,auxord,fring,nozx,noprepost,presci,testarg  
    ?argwords = argwords
    ?argbytes = ((argwords + 1) / 2) * 4
    if1
        if (?argbytes gt MAXRETN)
            %out SysEntry &usrname, number of argument words too high.
            .err
        endif
    endif

    ifb <usrname>       
        if2
            %out Fatal Error - SysEntry called with no arguments.
        endif
        .err2
        exitm
    endif

    ifnb <testarg>      
        if2
            %out Fatal Error - SysEntry &usrname called with too many arguments.
        endif
        .err2
        exitm
    endif

    ifidn <fring>,<IND_RING0>
        RingEndProc macro usrname2,NoChk        
            EndProc <R0!&usrname2>,<NoChk>
        endm
    else
        RingEndProc macro usrname2,NoChk        
            EndProc <usrname2>,<NoChk>
        endm
    endif

if      RAS
    if ?cstype eq ?CS_16bit
        ifb <noprepost>             
            ifb <inline>            
                RASpretrace macro   
                    if ?sci_sreg_si eq ?SCI_IN_USE
                        mov     si,[bp].sreg_si
                        ?sci_si = ?SCI_IN_USE
                        ?sci_sreg_si = ?SCI_NOT_IN_USE
                    endif
                    if ?sci_sreg_di eq ?SCI_IN_USE
                        mov     di,[bp].sreg_di
                        ?sci_di = ?SCI_IN_USE
                        ?sci_sreg_di = ?SCI_NOT_IN_USE
                    endif
                    public  pre&usrname
                    pre&usrname:
                    ifdef MMPHPre&usrname
                        MMPHPre&usrname 
                    endif
                    endm
            else
                RASpretrace macro   
                    if2
                        %out RASpretrace usage error
                    endif
                    .err
                    endm
            endif
            RASposttrace macro
                if ?sci_sreg_si eq ?SCI_IN_USE
                    mov     si,[bp].sreg_si
                    ?sci_si = ?SCI_IN_USE
                    ?sci_sreg_si = ?SCI_NOT_IN_USE
                endif
                if ?sci_sreg_di eq ?SCI_IN_USE
                    mov     di,[bp].sreg_di
                    ?sci_di = ?SCI_IN_USE
                    ?sci_sreg_di = ?SCI_NOT_IN_USE
                endif
                public  post&usrname
                post&usrname:
                ifdef MMPHPost&usrname
                    MMPHPost&usrname 
                endif
                endm
        endif                       
    endif
endif
    Procedure usrname,far,,,abase
    if (?cstype eq ?CS_16bit)
        DefCallGate usrname,usrname,argwords,<ordinal>,gatename,,auxord,fring
    else
        DefCallGate usrname,usrname,argwords,<ordinal>,gatename,cs32bit,auxord,fring
    endif
    .386p
    ifb <presci>
        ifb <nozx>
            movzx esp,sp
        endif
                                            
        if (argwords and 1)                 
            ifnb <nozx>                     
                movzx  esp,sp
            endif
            sub     dword ptr [esp]+size retf32_s+?argbytes,2
        endif
    endif
    CpuMode reset
endm










CallSCI macro   usrname
    if2
        ifndef sci
            extrn   sci:near
        endif
    endif
    CpuMode 386
    pushfd                      
    PushSCIRet  %?argbytes
    PUSHD       <?argwords * 2> 
                                
    CpuMode 286
    call  sci
    sci_init
endm





PushSCIRet macro cbargs
    if2
        ifndef SCIReturn&cbargs
            extrn SCIReturn&cbargs:near
        endif
    endif
    push    offset DOSHIGH3CODE:SCIReturn&cbargs
endm



























SysEntry macro  usrname,ordinal,argwords,gatename,sctmask,auxord,fring,testarg
        ifidn <fring>,<IND_RING0>
            SysEntryBegin R0&usrname,<ordinal>,argwords,gatename,,,auxord,fring,,,presci,testarg
        else
            SysEntryBegin usrname,<ordinal>,argwords,gatename,,,auxord,fring,,,presci,testarg
        endif
        if2
            ifndef pre_sci
                extrn   pre_sci:near
            endif
        endif
        push    ?argwords * 2  
        CpuMode 286
        call  pre_sci
        sci_init
        endm











EndEntry macro  usrname,NoGen,NoChk
        ifb     <NoGen>
            return
        endif
if      RAS
    if ?cstype eq ?CS_16bit
        purge   RASpretrace
        purge   RASposttrace
    endif
endif
        RingEndProc usrname,NoChk
        purge   RingEndProc
        endm

































SysEntry32 macro usrname,ordinal,argwords,workername,nothunk,pascal,gatename

    ifb <nothunk>
        SysEntryBegin <usrname>,<ordinal>,<argwords+2>,<gatename>,,,,,nozx
    else
        SysEntryBegin <usrname>,<ordinal>,<argwords>,<gatename>,,,,,nozx
    endif

    ifb <pascal>
        KMEnterKmodeAPI32 ?argbytes,pascal,nothunk
    else
        KMEnterKmodeAPI32 (?argwords*2),pascal,nothunk
    endif

    if RAS
        ifdef MMPHPre&usrname
            MMPHPre&usrname     
        endif
        Entry pre&usrname,,,nocheck
    endif

    extrn workername:near







                                                                     
    lea   eax, workername                                            
    push  eax                     
    push  ordinal                 
                                                                     
    call  Sec32Router             


    if RAS
        ifdef MMPHPost&usrname
            MMPHPost&usrname    
        endif
        Entry post&usrname,,,nocheck
    endif

    KMExitKmodeAPI32 ?argbytes,pascal,nothunk

    EndEntry <usrname>,NoGen
endm


sci_getreg macro reg
    ?sci_rc = ?SCI_OK
    if ?sci_ax eq ?SCI_NOT_IN_USE
        reg equ <ax>
        ?sci_ax = ?SCI_WKREG
    elseif ?sci_cx eq ?SCI_NOT_IN_USE
        reg equ <cx>
        ?sci_cx = ?SCI_WKREG
    elseif ?sci_dx eq ?SCI_NOT_IN_USE
        reg equ <dx>
        ?sci_dx = ?SCI_WKREG
    elseif ?sci_bx eq ?SCI_NOT_IN_USE
        reg equ <bx>
        ?sci_bx = ?SCI_WKREG
    elseif ?sci_di eq ?SCI_NOT_IN_USE
        reg equ <di>
        ?sci_di = ?SCI_WKREG
    elseif ?sci_si eq ?SCI_NOT_IN_USE
        reg equ <si>
        ?sci_si = ?SCI_WKREG
    elseif ?sci_ax eq ?SCI_SS_RPL
        reg equ <ax>
        ?sci_ax = ?SCI_WKREG
    elseif ?sci_cx eq ?SCI_SS_RPL
        reg equ <cx>
        ?sci_cx = ?SCI_WKREG
    elseif ?sci_dx eq ?SCI_SS_RPL
        reg equ <dx>
        ?sci_dx = ?SCI_WKREG
    elseif ?sci_bx eq ?SCI_SS_RPL
        reg equ <bx>
        ?sci_bx = ?SCI_WKREG
    elseif ?sci_di eq ?SCI_SS_RPL
        reg equ <di>
        ?sci_di = ?SCI_WKREG
    elseif ?sci_si eq ?SCI_SS_RPL
        reg equ <si>
        ?sci_si = ?SCI_WKREG
    else
        ?sci_rc = ?SCI_ERROR
    endif
    endm


sci_getbasereg macro reg
    ?sci_rc = ?SCI_OK
    if ?sci_bx eq ?SCI_NOT_IN_USE
        reg equ <bx>
        ?sci_bx = ?SCI_WKREG
    elseif ?sci_di eq ?SCI_NOT_IN_USE
        reg equ <di>
        ?sci_di = ?SCI_WKREG
    elseif ?sci_si eq ?SCI_NOT_IN_USE
        reg equ <si>
        ?sci_si = ?SCI_WKREG
    elseif ?sci_bx eq ?SCI_SS_RPL
        reg equ <bx>
        ?sci_bx = ?SCI_WKREG
    elseif ?sci_di eq ?SCI_SS_RPL
        reg equ <di>
        ?sci_di = ?SCI_WKREG
    elseif ?sci_si eq ?SCI_SS_RPL
        reg equ <si>
        ?sci_si = ?SCI_WKREG
    elseif ?sci_di eq ?SCI_IN_USE
        mov     [bp].sreg_di,di
        ?sci_sreg_di = ?SCI_IN_USE
        reg equ <di>
        ?sci_di = ?SCI_WKREG
    elseif ?sci_si eq ?SCI_IN_USE
        mov     [bp].sreg_si,si
        ?sci_sreg_si = ?SCI_IN_USE
        reg equ <si>
        ?sci_si = ?SCI_WKREG
    else
        ?sci_rc = ?SCI_ERROR
    endif
    endm


sci_setstampreg macro
    ?sci_rc = ?SCI_OK
    if ?sci_si eq ?SCI_SS_RPL
        ?sci_rc = ?SCI_OK
    elseif ?sci_ax eq ?SCI_SS_RPL
        ?sci_rc = ?SCI_OK
    elseif ?sci_bx eq ?SCI_SS_RPL
        ?sci_rc = ?SCI_OK
    elseif ?sci_cx eq ?SCI_SS_RPL
        ?sci_rc = ?SCI_OK
    elseif ?sci_dx eq ?SCI_SS_RPL
        ?sci_rc = ?SCI_OK
    elseif ?sci_di eq ?SCI_SS_RPL
        ?sci_rc = ?SCI_OK
    elseif ?sci_si eq ?SCI_NOT_IN_USE
        mov     si,[bp].sreg_rpl
        ?sci_si = ?SCI_SS_RPL
    elseif ?sci_ax eq ?SCI_NOT_IN_USE
        mov     ax,[bp].sreg_rpl
        ?sci_ax = ?SCI_SS_RPL
    elseif ?sci_cx eq ?SCI_NOT_IN_USE
        mov     cx,[bp].sreg_rpl
        ?sci_cx = ?SCI_SS_RPL
    elseif ?sci_dx eq ?SCI_NOT_IN_USE
        mov     dx,[bp].sreg_rpl
        ?sci_dx = ?SCI_SS_RPL
    elseif ?sci_di eq ?SCI_NOT_IN_USE
        mov     di,[bp].sreg_rpl
        ?sci_di = ?SCI_SS_RPL
    elseif ?sci_bx eq ?SCI_NOT_IN_USE
        mov     bx,[bp].sreg_rpl
        ?sci_bx = ?SCI_SS_RPL
    elseif ?sci_si eq ?SCI_IN_USE
        mov     [bp].sreg_si,si
        mov     si,[bp].sreg_rpl
        ?sci_si = ?SCI_SS_RPL
        ?sci_sreg_si = ?SCI_IN_USE
    elseif ?sci_di eq ?SCI_IN_USE
        mov     [bp].sreg_di,di
        mov     di,[bp].sreg_rpl
        ?sci_di = ?SCI_SS_RPL
        ?sci_sreg_di = ?SCI_IN_USE
    else
        ?sci_rc = ?SCI_ERROR
    endif
    endm


sci_stamprpl macro target
    ?sci_rc = ?SCI_OK
    if ?sci_si eq ?SCI_SS_RPL
        or      target,si
    elseif ?sci_ax eq ?SCI_SS_RPL
        or      target,ax
    elseif ?sci_bx eq ?SCI_SS_RPL
        or      target,bx
    elseif ?sci_cx eq ?SCI_SS_RPL
        or      target,cx
    elseif ?sci_dx eq ?SCI_SS_RPL
        or      target,dx
    elseif ?sci_di eq ?SCI_SS_RPL
        or      target,di
    elseif ?sci_si eq ?SCI_NOT_IN_USE
        mov     si,[bp].sreg_rpl
        or      target,si
        ?sci_si = ?SCI_SS_RPL
    elseif ?sci_ax eq ?SCI_NOT_IN_USE
        mov     ax,[bp].sreg_rpl
        or      target,ax
        ?sci_ax = ?SCI_SS_RPL
    elseif ?sci_cx eq ?SCI_NOT_IN_USE
        mov     cx,[bp].sreg_rpl
        or      target,cx
        ?sci_cx = ?SCI_SS_RPL
    elseif ?sci_dx eq ?SCI_NOT_IN_USE
        mov     dx,[bp].sreg_rpl
        or      target,dx
        ?sci_dx = ?SCI_SS_RPL
    elseif ?sci_di eq ?SCI_NOT_IN_USE
        mov     di,[bp].sreg_rpl
        or      target,di
        ?sci_di = ?SCI_SS_RPL
    elseif ?sci_bx eq ?SCI_NOT_IN_USE
        mov     bx,[bp].sreg_rpl
        or      target,bx
        ?sci_bx = ?SCI_SS_RPL
    elseif target eq ax
        or      ax,[bp].sreg_rpl
    elseif target eq bx
        or      bx,[bp].sreg_rpl
    elseif target eq cx
        or      cx,[bp].sreg_rpl
    elseif target eq dx
        or      dx,[bp].sreg_rpl
    elseif target eq si
        or      si,[bp].sreg_rpl
    elseif target eq di
        or      di,[bp].sreg_rpl
    elseif ?sci_si eq ?SCI_IN_USE
        mov     [bp].sreg_si,si
        mov     si,[bp].sreg_rpl
        or      target,si
        ?sci_si = ?SCI_SS_RPL
        ?sci_sreg_si = ?SCI_IN_USE
    elseif ?sci_di eq ?SCI_IN_USE
        mov     [bp].sreg_di,di
        mov     di,[bp].sreg_rpl
        or      target,di
        ?sci_di = ?SCI_SS_RPL
        ?sci_sreg_di = ?SCI_IN_USE
    else
        push    ax
        mov     ax,[bp].sreg_rpl
        or      target,ax
        pop     ax
    endif
    endm


sci_freereg macro reg
    ?sci_rc = ?SCI_OK
    % ifidni <reg>,<ax>
        ?sci_ax = ?SCI_NOT_IN_USE
    % elseifidni <reg>,<cx>
        ?sci_cx = ?SCI_NOT_IN_USE
    % elseifidni <reg>,<dx>
        ?sci_dx = ?SCI_NOT_IN_USE
    % elseifidni <reg>,<bx>
        ?sci_bx = ?SCI_NOT_IN_USE
    % elseifidni <reg>,<di>
        ?sci_di = ?SCI_NOT_IN_USE
    % elseifidni <reg>,<si>
        ?sci_si = ?SCI_NOT_IN_USE
    else
        ?sci_rc = ?SCI_ERROR
    endif
    endm


gendef  macro   thopinstr
        if2
        ifndef to_&thopinstr
                extrn   to_&thopinstr:near
        endif
        endif
        endm












return  macro
        gendef  return
        if ?sci_sreg_di eq ?SCI_IN_USE
           mov     di,[bp].sreg_di
           ?sci_sreg_di = ?SCI_NOT_IN_USE
        endif
        if ?sci_ds eq ?SCI_UNKNOWN
           FlatContext <ds>,ax
           ?sci_ds = ?SCI_FLAT
        endif
        jmp     to_return
        endm





































farcall macro  worker, securityord                                        
        gendef  farcall
HIGH3CODE ends
        IF2
                EXTRNFAR worker
        ENDIF
HIGH3CODE segment
        if ?sci_sreg_si eq ?SCI_IN_USE
           mov     si,[bp].sreg_si
           ?sci_sreg_si = ?SCI_NOT_IN_USE
        endif
        if ?sci_sreg_di eq ?SCI_IN_USE
           mov     di,[bp].sreg_di
           ?sci_sreg_di = ?SCI_NOT_IN_USE
        endif
        call    to_farcall
        DDFAR   worker
        dw      securityord                                               
        ?sci_ax = ?SCI_IN_USE
        ?sci_bx = ?SCI_IN_USE
        ?sci_cx = ?SCI_IN_USE
        ?sci_dx = ?SCI_IN_USE
        ?sci_di = ?SCI_IN_USE
        ?sci_si = ?SCI_SS_RPL
        ?sci_sreg_si = ?SCI_NOT_IN_USE
        ?sci_sreg_di = ?SCI_NOT_IN_USE
        ?sci_ds = ?SCI_UNKNOWN
        ?sci_es = ?SCI_UNKNOWN
        ?sci_argptr = kstk_args
        endm


























chk_devioctl  macro   labelarg
        local   cdi1, cdi2, cdi3
        if ?sci_sreg_si eq ?SCI_IN_USE
           mov     si,[bp].sreg_si
           ?sci_si = ?SCI_IN_USE
           ?sci_sreg_si = ?SCI_NOT_IN_USE
        endif
        if ?sci_sreg_di eq ?SCI_IN_USE
           mov     di,[bp].sreg_di
           ?sci_di = ?SCI_IN_USE
           ?sci_sreg_di = ?SCI_NOT_IN_USE
        endif
        sci_getreg reg1                 



        or      cx,cx                   
        jnz     short cdi2              
        if ?sci_rc EQ ?SCI_OK           
           mov     reg1,[bp].sreg_es    
           .errnz  RPL_RING3-3
           and     reg1,NOT RPL_RING3   
           or      reg1,di              
           jz      short cdi2           
                                        
        else
           mov     cx,[bp].sreg_es      
           .errnz  RPL_RING3-3
           and     cx,NOT RPL_RING3     
           or      cx,di                
           jz      short cdi2           
                                        
           xor     cx,cx                
        endif



cdi1:   mov     di,ERROR_INVALID_PARAMETER  
        if1
            ifdef labelarg
                %out SCI macro variable error possible after backwards jump
            endif
        endif
        jmp     short labelarg              



cdi2:   or      si,si                   
        jnz     short cdi3              
        if ?sci_rc EQ ?SCI_OK           
           mov     reg1,[bp].sreg_ds    
           .errnz  RPL_RING3-3
           and     reg1,NOT RPL_RING3   
           or      reg1,dx              
           jnz     short cdi1           
                                        
        else
           mov     si,[bp].sreg_ds      
           .errnz  RPL_RING3-3
           and     si,NOT RPL_RING3     
           or      si,dx                
           mov     si,0                 
           jnz     short cdi1           
                                        
        endif

cdi3:                                   
        if ?sci_rc EQ ?SCI_OK           
           sci_freereg reg1             
        endif
        endm











uncond_jmp  macro   labelarg
        if1
            ifdef labelarg
                %out SCI macro variable error possible after backwards jump
            endif
        endif
        if ?sci_di eq ?SCI_IN_USE
           mov     [bp].sreg_di,di
           ?sci_sreg_di = ?SCI_IN_USE
        endif
        if ?sci_si eq ?SCI_IN_USE
           mov     [bp].sreg_si,si
           ?sci_sreg_si = ?SCI_IN_USE
        endif
        jmp     short labelarg          
        endm














cmp_je  macro   comparg,labelarg
        if ?sci_di eq ?SCI_IN_USE
           mov     [bp].sreg_di,di
           ?sci_sreg_di = ?SCI_IN_USE
        endif
        if ?sci_si eq ?SCI_IN_USE
           mov     [bp].sreg_si,si
           ?sci_sreg_si = ?SCI_IN_USE
        endif
        cmp     word ptr [bp+?sci_argptr],comparg 
        if1
            ifdef labelarg
                %out SCI macro variable error possible after backwards jump
            endif
        endif
        je      short labelarg          
        endm



















ld_dsdx macro
        mov     dx,[bp+?sci_argptr+2]
        ?sci_dx = ?SCI_IN_USE
        sci_stamprpl <dx>               
        mov     [bp].sreg_ds,dx
        mov     dx,[bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+4
        endm













ld_ds   macro
        sci_getreg reg1                          
        if ?sci_rc EQ ?SCI_ERROR
           sci_getbasereg reg1                   
        endif
        mov     reg1,word ptr [bp+?sci_argptr]
        sci_stamprpl reg1                        
        mov     [bp].sreg_ds,reg1
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+2
        endm














ld_dsdx_norpl macro
        mov     dx,word ptr [bp+?sci_argptr+2]
        mov     [bp].sreg_ds,dx
        mov     dx,word ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+4
        ?sci_dx = ?SCI_IN_USE
        endm













ld_ds_norpl macro
        sci_getreg reg1                          
        if ?sci_rc EQ ?SCI_ERROR
           sci_getbasereg reg1                   
        endif
        mov     reg1,word ptr [bp+?sci_argptr]
        mov     [bp].sreg_ds,reg1
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+2
        endm













ld_es   macro
        rplstampsel                              
        sci_getreg reg1                          
        if ?sci_rc EQ ?SCI_ERROR
           sci_getbasereg reg1                   
        endif
        mov     reg1,word ptr [bp+?sci_argptr]
        mov     [bp].sreg_es,reg1
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+2
        endm













ld_es_norpl macro
        sci_getreg reg1                          
        if ?sci_rc EQ ?SCI_ERROR
           sci_getbasereg reg1                   
        endif
        mov     reg1,word ptr [bp+?sci_argptr]
        mov     [bp].sreg_es,reg1
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+2
        endm


















ld_ax   macro
        mov     ax,word ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_ax = ?SCI_IN_USE
        endm













ld_ah   macro
        mov     ah,byte ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_ax = ?SCI_IN_USE
        endm













ld_al   macro
        mov     al,byte ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_ax = ?SCI_IN_USE
        endm













ld_bx   macro
        mov     bx,word ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_bx = ?SCI_IN_USE
        endm













ld_bh   macro
        mov     bh,byte ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_bx = ?SCI_IN_USE
        endm













ld_bl   macro
        mov     bl,byte ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_bx = ?SCI_IN_USE
        endm










ld_bx_0 macro
        xor     bx,bx
        ?sci_bx = ?SCI_IN_USE
        endm










ld_bx_1 macro
        mov     bx,1
        ?sci_bx = ?SCI_IN_USE
        endm













ld_cx   macro
        mov     cx,word ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_cx = ?SCI_IN_USE
        endm










ld_cx_0 macro
        xor     cx,cx
        ?sci_cx = ?SCI_IN_USE
        endm













ld_dx   macro
        mov     dx,word ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_dx = ?SCI_IN_USE
        endm










ld_dx_0 macro
        xor     dx,dx
        ?sci_dx = ?SCI_IN_USE
        endm













ld_dl   macro
        mov     dl,byte ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_dx = ?SCI_IN_USE
        endm













ld_si   macro
        mov     si,word ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_si = ?SCI_IN_USE
        ?sci_sreg_si = ?SCI_NOT_IN_USE
        endm










ld_si_0 macro
        xor     si,si
        ?sci_si = ?SCI_IN_USE
        ?sci_sreg_si = ?SCI_NOT_IN_USE
        endm









ld_si_1 macro
        mov     si,1
        ?sci_si = ?SCI_IN_USE
        ?sci_sreg_si = ?SCI_NOT_IN_USE
        endm













ld_di   macro
        mov     di,word ptr [bp+?sci_argptr]
        ?sci_argptr = ?sci_argptr+2
        ?sci_di = ?SCI_IN_USE
        ?sci_sreg_di = ?SCI_NOT_IN_USE
        endm














ld_disi macro
        rplstampptr                            
        mov     si,[bp+?sci_argptr]
        mov     di,[bp+?sci_argptr+2]
        ?sci_argptr = ?sci_argptr+4
        ?sci_si = ?SCI_IN_USE
        ?sci_di = ?SCI_IN_USE
        ?sci_sreg_si = ?SCI_NOT_IN_USE
        ?sci_sreg_di = ?SCI_NOT_IN_USE
        endm










ld_di_0 macro
        xor     di,di
        ?sci_di = ?SCI_IN_USE
        ?sci_sreg_di = ?SCI_NOT_IN_USE
        endm




















ld_ind_ax macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     ax,[reg1]                        
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ax = ?SCI_IN_USE
        ?sci_ds = ?SCI_UNKNOWN
        endm
















ld_ind_bx macro
        rplstampptr                            
        lds     bx,dword ptr [bp+?sci_argptr]  
        mov     bx,[bx]                        
        ?sci_argptr = ?sci_argptr+4
        ?sci_bx = ?SCI_IN_USE
        ?sci_ds = ?SCI_UNKNOWN
        endm
















ld_ind_cx macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     cx,[reg1]                        
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_cx = ?SCI_IN_USE
        ?sci_ds = ?SCI_UNKNOWN
        endm
















ld_ind_si macro
        rplstampptr                            
        lds     si,dword ptr [bp+?sci_argptr]  
        mov     si,[si]                        
        ?sci_argptr = ?sci_argptr+4
        ?sci_si = ?SCI_IN_USE
        ?sci_sreg_si = ?SCI_NOT_IN_USE
        ?sci_ds = ?SCI_UNKNOWN
        endm















ld_imm_ax macro immedValue
        if immedValue eq 0
           xor     ax,ax
        else
           mov     ax,immedValue
        endif
        ?sci_ax = ?SCI_IN_USE
        endm










ld_imm_al macro immedValue
        if immedValue eq 0
           xor     al,al
        else
           mov     al,immedValue
        endif
        ?sci_ax = ?SCI_IN_USE
        endm









ld_imm_ah macro immedValue
        if immedValue eq 0
           xor     ah,ah
        else
           mov     ah,immedValue
        endif
        ?sci_ax = ?SCI_IN_USE
        endm









ld_imm_cx macro immedValue
        if immedValue eq 0
           xor     cx,cx
        else
           mov     cx,immedValue
        endif
        ?sci_cx = ?SCI_IN_USE
        endm










ld_imm_dx macro immedValue
        if immedValue eq 0
           xor     dx,dx
        else
           mov     dx,immedValue
        endif
        ?sci_dx = ?SCI_IN_USE
        endm


















mv_esdi_dsdx macro
        mov     di,[bp].sreg_ds
        mov     [bp].sreg_es,di
        mov     di,dx
        ?sci_di = ?SCI_IN_USE
        ?sci_sreg_di = ?SCI_NOT_IN_USE
        endm












mv_si_dx macro
        mov     si,dx
        ?sci_si = ?SCI_IN_USE
        ?sci_sreg_si = ?SCI_NOT_IN_USE
        endm

















mv_si_si macro
        lea     si,?sci_argptr[bp]
        ?sci_si = ?SCI_IN_USE
        ?sci_sreg_si = ?SCI_NOT_IN_USE
        endm










ld_parampkt_bx  macro
        lea     bx,[bp].kstk_args       
        ?sci_bx = ?SCI_IN_USE
        endm


















ld_tcb  macro   tcboffset
        sci_getbasereg reg1                      
        sci_getreg reg2                          
        if ?sci_rc EQ ?SCI_ERROR
           sci_getbasereg reg2                   
        endif
        mov     reg1,ss:[TKOPTDA.CurrTCB]
        mov     reg2,word ptr [bp+?sci_argptr]
        mov     word ptr ss:[reg1+tcboffset],reg2
        sci_freereg reg2                         
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+2
        endm













ld_tcb_ptr macro tcboffset
        rplstampptr                              
        sci_getbasereg reg1                      
        sci_getreg reg2                          
        if ?sci_rc EQ ?SCI_ERROR
           sci_getbasereg reg2                   
        endif
        mov     reg1,ss:[TKOPTDA.CurrTCB]
        db      66h                              
        mov     reg2,word ptr [bp+?sci_argptr]
        db      66h                              
        mov     word ptr ss:[reg1+tcboffset],reg2
        sci_freereg reg2                         
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        endm


















st_ax   macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],ax
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm













st_al   macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     byte ptr [reg1],al
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm













st_bx   macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],bx
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm















stnz_cx macro
        local   stc1
        sci_getbasereg reg1             
        sci_setstampreg                 

        test    word ptr [bp+?sci_argptr+2],NOT RPL_RING3  
        jz      short stc1                       
        rplstampptr                              
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],cx
stc1:
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm













st_cx   macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],cx
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm















stnz_axdx macro
        local   stdad1
        sci_getbasereg reg1             
        sci_setstampreg                 

        test    word ptr [bp+?sci_argptr+2],NOT RPL_RING3  
        jz      short stdad1                     
        rplstampptr                              
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],dx               
        mov     word ptr [reg1+2],ax             
stdad1:
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm














st_axdx macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],dx               
        mov     word ptr [reg1+2],ax             
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm













st_dx   macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],dx
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm















stnz_dxax macro
        local   stda1
        sci_getbasereg reg1             
        sci_setstampreg                 

        test    word ptr [bp+?sci_argptr+2],NOT RPL_RING3  
        jz      short stda1    
        rplstampptr                              
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],ax               
        mov     word ptr [reg1+2],dx             
stda1:
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm














st_dxax macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],ax               
        mov     word ptr [reg1+2],dx             
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm














st_edx macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        CpuMode 386
        mov     dword ptr [reg1],edx             
        CpuMode Reset
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm














st_dxcx macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],cx               
        mov     word ptr [reg1+2],dx             
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm













st_axbxcx macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],ax               
        mov     word ptr [reg1+2],bx             
        mov     word ptr [reg1+4],cx             
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm













st_axbxcxdx macro
        rplstampptr                              
        sci_getbasereg reg1                      
        lds     reg1,dword ptr [bp+?sci_argptr]  
        mov     word ptr [reg1],ax               
        mov     word ptr [reg1+2],bx             
        mov     word ptr [reg1+4],cx             
        mov     word ptr [reg1+6],dx             
        sci_freereg reg1                         
        ?sci_argptr = ?sci_argptr+4
        ?sci_ds = ?SCI_UNKNOWN
        endm



















skip_word macro
        ?sci_argptr = ?sci_argptr+2       
        endm














skip_2word macro
        ?sci_argptr = ?sci_argptr+4       
        endm














skip_ptr macro
        ?sci_argptr = ?sci_argptr+4       
        endm














skip_4word macro
        ?sci_argptr = ?sci_argptr+8       
        endm














skip_2ptr macro
        ?sci_argptr = ?sci_argptr+8       
        endm














skip_5word macro
        ?sci_argptr = ?sci_argptr+10      
        endm














skip_7word macro
        ?sci_argptr = ?sci_argptr+14      
        endm














skip_8word macro
        ?sci_argptr = ?sci_argptr+16      
        endm














skip_9word macro
        ?sci_argptr = ?sci_argptr+18      
        endm














skip_10word macro
        ?sci_argptr = ?sci_argptr+20      
        endm














skip_12word macro
        ?sci_argptr = ?sci_argptr+24      
        endm













backup_2word macro
        ?sci_argptr = ?sci_argptr-4       
        endm





















reser_dd_arg macro
        .386p
        cmp     dword ptr [bp+?sci_argptr],0
        ?sci_argptr = ?sci_argptr+4       
        if2
            ifndef  UsrArgError
                extrn   UsrArgError:near
            endif
        endif
        jne     UsrArgError             
        CPUMode reset
        endm




















getusrbufz macro
        gendef  getusrbufz
        if ?sci_si eq ?SCI_IN_USE
           mov     [bp].sreg_si,si
           ?sci_sreg_si = ?SCI_IN_USE
        endif
        if ?sci_di eq ?SCI_IN_USE
           mov     [bp].sreg_di,di
           ?sci_sreg_di = ?SCI_IN_USE
        endif
        if ?sci_si ne ?SCI_SS_RPL
           mov     si,[bp].sreg_rpl
        endif
        lea     di,?sci_argptr[bp]      
        call    to_getusrbufz           
        ?sci_si = ?SCI_NOT_IN_USE
        ?sci_di = ?SCI_NOT_IN_USE
        ?sci_ds = ?SCI_UNKNOWN
        ?sci_es = ?SCI_UNKNOWN
        endm


















getusrnzbuf macro
        local   guz1
        gendef  getusrbufz
        if ?sci_si eq ?SCI_IN_USE
           mov     [bp].sreg_si,si
           ?sci_sreg_si = ?SCI_IN_USE
        endif
        if ?sci_di eq ?SCI_IN_USE
           mov     [bp].sreg_di,di
           ?sci_sreg_di = ?SCI_IN_USE
        endif
        test    word ptr [bp+?sci_argptr+2],RPL_CLR    
        jz      short guz1                             
        if ?sci_si ne ?SCI_SS_RPL
           mov     si,[bp].sreg_rpl
        endif
        lea     di,?sci_argptr[bp]      
                                        
        call    to_getusrbufz           
guz1:
        ?sci_si = ?SCI_NOT_IN_USE
        ?sci_di = ?SCI_NOT_IN_USE
        ?sci_ds = ?SCI_UNKNOWN
        ?sci_es = ?SCI_UNKNOWN
        endm
















getusrbufz3 macro
        gendef  getusrbufz3
        if ?sci_si eq ?SCI_IN_USE
           mov     [bp].sreg_si,si
           ?sci_sreg_si = ?SCI_IN_USE
        endif
        if ?sci_di eq ?SCI_IN_USE
           mov     [bp].sreg_di,di
           ?sci_sreg_di = ?SCI_IN_USE
        endif
        if ?sci_si ne ?SCI_SS_RPL
           mov     si,[bp].sreg_rpl
        endif
        lea     di,?sci_argptr[bp]      
                                        
        call    to_getusrbufz3          
        ?sci_si = ?SCI_NOT_IN_USE
        ?sci_di = ?SCI_NOT_IN_USE
        ?sci_ds = ?SCI_UNKNOWN
        ?sci_es = ?SCI_UNKNOWN
        endm


















getusrbufz206 macro
        gendef  getusrbufz206
        if ?sci_si eq ?SCI_IN_USE
           mov     [bp].sreg_si,si
           ?sci_sreg_si = ?SCI_IN_USE
        endif
        if ?sci_di eq ?SCI_IN_USE
           mov     [bp].sreg_di,di
           ?sci_sreg_di = ?SCI_IN_USE
        endif
        if ?sci_si ne ?SCI_SS_RPL
           mov     si,[bp].sreg_rpl
        endif
        lea     di,?sci_argptr[bp]      
                                        
        call    to_getusrbufz206        
        ?sci_si = ?SCI_NOT_IN_USE
        ?sci_di = ?SCI_NOT_IN_USE
        ?sci_ds = ?SCI_UNKNOWN
        ?sci_es = ?SCI_UNKNOWN
        endm


























enterfscrit macro
        gendef  enterfscrit
        if ?sci_di eq ?SCI_IN_USE       
           mov     [bp].sreg_di,di
           ?sci_sreg_di = ?SCI_IN_USE
        endif
        ?sci_di = ?SCI_NOT_IN_USE
        if ?sci_ax eq ?SCI_IN_USE
           push    ax
        endif
        if ?sci_cx eq ?SCI_IN_USE
           push    cx
        endif
        if ?sci_dx eq ?SCI_IN_USE
           push    dx
        endif
        if ?sci_ds eq ?SCI_UNKNOWN
           FlatContext <ds>,ax
           ?sci_ds = ?SCI_FLAT
        endif
        call    to_enterfscrit
        ?sci_es = ?SCI_FLAT
        if ?sci_dx eq ?SCI_IN_USE
           pop     dx
        endif
        if ?sci_cx eq ?SCI_IN_USE
           pop     cx
        endif
        if ?sci_ax eq ?SCI_IN_USE
           pop     ax
        endif
        endm













exitfscrit macro
        gendef  exitfscrit
        if ?sci_si eq ?SCI_IN_USE       
           mov     [bp].sreg_si,si
           ?sci_sreg_si = ?SCI_IN_USE
        endif
        ?sci_si = ?SCI_NOT_IN_USE
        call    to_exitfscrit
        ?sci_ds = ?SCI_FLAT
        ?sci_es = ?SCI_FLAT
        endm

















rplstampsel macro
        sci_stamprpl <[bp+?sci_argptr]>      
        endm












rplstampptr macro
        sci_stamprpl <[bp+?sci_argptr+2]>    
        endm



TRC_TASK	EQU	1Q               
TRC_FS	EQU	2Q               
TRC_OTHER	EQU	8000H           
