home *** CD-ROM | disk | FTP | other *** search
/ Internet MPEG Audio Archive / IMAA.mdf / util / dos / l3v100n / rsx / source / dpmi16.asm < prev    next >
Assembly Source File  |  1994-01-19  |  28KB  |  1,569 lines

  1. ;
  2. ; DPMI16.ASM (c) Rainer Schnitker 91,92,93
  3. ;
  4.  
  5. ;
  6. ; 16bit library for DPMI 0.9 calls
  7. ;
  8. ; define 'HAVE386' to use 32bit DPMI mode
  9. ;
  10.  
  11.     .286
  12.         .model SMALL, C
  13.  
  14. ; macro to call dpmi in ax
  15. ;
  16. DPMI MACRO function
  17.         mov ax,function
  18.         int 31h
  19.         ENDM
  20.  
  21. ; error check for dpmi-functions
  22. ; return -1 for dpmi 0.9
  23. ; for dpmi 1.0 replace with "jc @@end" , error code = ax
  24. ;
  25. CHECKERR MACRO
  26.         jnc     short @@ok
  27.     mov    ax, -1
  28.         jmp     short @@end
  29.     ENDM
  30.  
  31. getdword MACRO high,low,address
  32.         mov low,word ptr address
  33.         mov high,word ptr address.2
  34.     ENDM
  35.  
  36. setdword MACRO address,high,low
  37.         mov word ptr address,low
  38.         mov word ptr address.2,high
  39.     ENDM
  40.  
  41.  
  42.         .data
  43.  
  44.         .code
  45.  
  46.  
  47.    ;    
  48.    ;    int AllocLDT(WORD anzahl,WORD *sel)
  49.    ;    
  50.         public  C AllocLDT
  51. AllocLDT PROC C \
  52.         anzahl  :WORD, \
  53.         sel     :PTR
  54.  
  55.         mov     cx, anzahl
  56.         DPMI 0000h
  57.     CHECKERR
  58. @@ok:
  59.         mov     bx, sel
  60.     mov    word ptr [bx], ax
  61.     xor    ax, ax
  62. @@end:
  63.     ret    
  64. AllocLDT        endp
  65.  
  66.    ;
  67.    ;    int FreeLDT(WORD sel)
  68.    ;    
  69.         public  C FreeLDT
  70. FreeLDT PROC C \
  71.         sel     :WORD
  72.  
  73.         mov     bx, sel
  74.         DPMI 0001h
  75.     CHECKERR
  76. @@ok:
  77.     xor    ax, ax
  78. @@end:
  79.     ret    
  80. FreeLDT         endp
  81.  
  82.    ;    
  83.    ;    int SegToSel(WORD seg,WORD *sel)
  84.    ;    
  85.         public  C SegToSel
  86. SegToSel PROC C \
  87.         paragr  : WORD , \
  88.         sel     : PTR
  89.  
  90.         mov     bx,paragr
  91.         DPMI 0002h
  92.     CHECKERR
  93. @@ok:
  94.         mov     bx, sel
  95.     mov    word ptr [bx], ax
  96.     xor    ax, ax
  97. @@end:
  98.     ret    
  99. SegToSel       endp
  100.  
  101.    ;
  102.    ;    WORD SelInc(void)
  103.    ;
  104.         public  C SelInc
  105. SelInc PROC C
  106.         DPMI 0003h
  107.     ret    
  108. SelInc          endp
  109.  
  110.    ;
  111.    ;    int LockSel(WORD sel)  undocumented in dpmi 0.9/1.0
  112.    ;    
  113.         public  C LockSel
  114. LockSel PROC C \
  115.         sel     :WORD
  116.  
  117.         mov     bx, sel
  118.         DPMI 0004h
  119.     CHECKERR
  120. @@ok:
  121.     xor    ax, ax
  122. @@end:
  123.     ret    
  124. LockSel        endp
  125.  
  126.    ;
  127.    ;    int UnlockSel(WORD sel)  undocumented in dpmi 0.9/1.0
  128.    ;    
  129.         public  C UnlockSel
  130. UnlockSel PROC C \
  131.         sel     :WORD
  132.  
  133.         mov     bx,sel
  134.         DPMI 0005h
  135.     CHECKERR
  136. @@ok:
  137.     xor    ax, ax
  138. @@end:
  139.     ret    
  140. UnlockSel      endp
  141.  
  142.    ;
  143.    ;    int GetBaseAddress(WORD sel,DWORD *address)
  144.    ;    
  145.         public  C GetBaseAddress
  146. GetBaseAddress PROC C \
  147.         sel     :WORD , \
  148.         address :PTR
  149.  
  150.         mov     bx, sel
  151.         DPMI 0006h
  152.     CHECKERR
  153. @@ok:
  154.         mov     bx, address
  155.         setdword [bx],cx,dx
  156.     xor    ax, ax
  157. @@end:
  158.     ret    
  159. GetBaseAddress endp
  160.  
  161.    ;
  162.    ;    int SetBaseAddress(WORD sel,DWORD address)
  163.    ;    
  164.         public  C SetBaseAddress
  165. SetBaseAddress PROC C \
  166.         sel     :WORD , \
  167.         base    :DWORD
  168.  
  169.     getdword cx,dx,base
  170.         mov     bx, sel
  171.         DPMI 0007h
  172.     CHECKERR
  173. @@ok:
  174.     xor    ax, ax
  175. @@end:
  176.     ret    
  177. SetBaseAddress endp
  178.  
  179.    ;    
  180.    ;    int SetLimit(WORD sel,DWORD limit)
  181.    ;    
  182.         public  C SetLimit
  183. SetLimit PROC C \
  184.         sel     :WORD , \
  185.         limit   :DWORD
  186.  
  187.         mov     bx,sel
  188.         getdword cx,dx,limit
  189.         DPMI 0008h
  190.     CHECKERR
  191. @@ok:
  192.     xor    ax, ax
  193. @@end:
  194.     ret    
  195. SetLimit       endp
  196.  
  197.    ;
  198.    ;    int SetAccess(WORD sel,BYTE access,BYTE extaccess)
  199.    ;    
  200.         public  C SetAccess
  201. SetAccess PROC C \
  202.         sel     :WORD, \
  203.         access  :BYTE, \
  204.         extaccess :BYTE
  205.  
  206.         mov     bx,sel
  207.         mov     cl,access
  208.         mov     ch,extaccess
  209.         DPMI 0009h
  210.     CHECKERR
  211. @@ok:
  212.     xor    ax, ax
  213. @@end:
  214.     ret    
  215. SetAccess      endp
  216.  
  217.    ;
  218.    ;    int CreatAlias(WORD segsel,WORD *alias)
  219.    ;    
  220.         public  C CreatAlias
  221. CreatAlias  PROC C \
  222.         segsel  :WORD , \
  223.         alias   :PTR
  224.  
  225.         mov     bx,sel
  226.         DPMI 000Ah
  227.     CHECKERR
  228. @@ok:
  229.         mov     bx,alias
  230.         mov     word ptr [bx],ax
  231.     xor    ax, ax
  232. @@end:
  233.     ret    
  234. CreatAlias     endp
  235.  
  236.    ;    
  237.    ;    int GetDescriptor(WORD sel,NPDESCRIPTOR desc)
  238.    ;    
  239.         public  C GetDescriptor
  240. GetDescriptor PROC C USES DI, \
  241.         sel     :WORD , \
  242.         desc    :PTR
  243.  
  244.     push    ds
  245.     pop    es
  246.         mov     bx,sel
  247. ifdef HAVE386
  248.     .386
  249.     movzx    edi, desc
  250.     .286
  251. else
  252.         mov     di,desc
  253. endif
  254.         DPMI 000Bh
  255.     CHECKERR
  256. @@ok:
  257.     xor    ax, ax
  258. @@end:
  259.     ret    
  260. GetDescriptor  endp
  261.  
  262.    ;
  263.    ;    int SetDescriptor(WORD sel,NPDESCRIPTOR desc)
  264.    ;
  265.         public  C SetDescriptor
  266. SetDescriptor PROC C USES DI, \
  267.         sel     :WORD, \
  268.         desc    :PTR
  269.  
  270.     push    ds
  271.     pop    es
  272. ifdef HAVE386
  273.     .386
  274.     movzx    edi, sel
  275.     .286
  276. else
  277.         mov     di,desc
  278. endif
  279.         mov     bx,sel
  280.         DPMI 000Ch
  281.     CHECKERR
  282. @@ok:
  283.     xor    ax, ax
  284. @@end:
  285.     ret    
  286. SetDescriptor  endp
  287.  
  288.    ;    
  289.    ;    int AllocSpecialLDT(WORD sel)
  290.    ;    
  291.         public  C AllocSpecialLDT
  292. AllocSpecialLDT PROC C \
  293.         sel     :WORD
  294.  
  295.         mov     bx,sel
  296.         DPMI 000Dh
  297.     CHECKERR
  298. @@ok:
  299.     xor    ax, ax
  300. @@end:
  301.     ret    
  302. AllocSpecialLDT        endp
  303.  
  304.    ;    
  305.    ;    int AllocDosMem(WORD paragraphs,WORD *segment,WORD *selector)
  306.    ;    
  307.         public  C AllocDosMem
  308. AllocDosMem  PROC C \
  309.         request :WORD , \
  310.         result  :PTR , \
  311.         sel     :PTR
  312.  
  313.         mov     bx,request
  314.         DPMI 0100h
  315.         jnc     short @@ok
  316.     mov    dx,bx
  317.         mov     bx,result
  318.         mov     word ptr [bx],dx
  319.         jmp     short @@end
  320. @@ok:
  321.         mov     bx,result
  322.         mov     word ptr [bx],ax
  323.         mov     bx,sel
  324.         mov     word ptr [bx],dx
  325.     xor    ax, ax
  326. @@end:
  327.     ret    
  328. AllocDosMem    endp
  329.  
  330.    ;
  331.    ;    int FreeDosMem(WORD selector)
  332.    ;    
  333.         public  C FreeDosMem
  334. FreeDosMem PROC C \
  335.         sel     :WORD
  336.  
  337.         mov     dx,sel
  338.         DPMI 0101h
  339.     CHECKERR
  340. @@ok:
  341.     xor    ax, ax
  342. @@end:
  343.     ret    
  344. FreeDosMem     endp
  345.  
  346.    ;    
  347.    ;    int ResizeDosMem(WORD paragraphs,WORD oldsel,WORD *maxpara)
  348.    ;    
  349.         public  C ResizeDosMem
  350. ResizeDosMem PROC C \
  351.         paragr  :WORD , \
  352.         oldsel  :WORD , \
  353.         maxpara :PTR
  354.  
  355.         mov     bx,paragr
  356.         mov     dx,oldsel
  357.         DPMI 0102h
  358.         jnc     short @@ok
  359.         mov     dx,bx
  360.         mov     bx,maxpara
  361.         mov     word ptr [bx],dx
  362.         jmp     short @@end
  363. @@ok:
  364.     xor    ax, ax
  365. @@end:
  366.     ret    
  367. ResizeDosMem   endp
  368.  
  369.    ;    
  370.    ;    int GetRealModeVector(BYTE b,WORD *seg,WORD *off)
  371.    ;    
  372.         public  C GetRealModeVector
  373. GetRealModeVector PROC C \
  374.         no      :BYTE , \
  375.         segm    :PTR , \
  376.         offs    :PTR
  377.  
  378.         mov     bl,no
  379.         DPMI 0200h
  380.     CHECKERR
  381. @@ok:
  382.         mov     bx,segm
  383.         mov     word ptr [bx],cx
  384.         mov     bx,offs
  385.         mov     word ptr [bx],dx
  386.     xor    ax, ax
  387. @@end:
  388.     ret    
  389. GetRealModeVector      endp
  390.  
  391.    ;
  392.    ;    int SetRealModeVector(BYTE b,WORD seg,WORD off)
  393.    ;    
  394.         public  C SetRealModeVector
  395. SetRealModeVector PROC C \
  396.         b       :BYTE , \
  397.         segm    :WORD , \
  398.         offs    :WORD
  399.  
  400.         mov     bl,b
  401.         mov     cx,segm
  402.         mov     dx,offs
  403.         DPMI 0201h
  404.     CHECKERR
  405. @@ok:
  406.     xor    ax, ax
  407. @@end:
  408.     ret    
  409. SetRealModeVector      endp
  410.  
  411.    ;    
  412.    ;    int GetExceptionVector(BYTE b,WORD *sel,WORD *off)
  413.    ;    
  414.         public  C GetExceptionVector
  415. GetExceptionVector PROC C \
  416.         no      :BYTE , \
  417.         segm    :PTR , \
  418.         offs    :PTR
  419.  
  420.         mov     bl,no
  421.         DPMI 0202h
  422.     CHECKERR
  423. @@ok:
  424.         mov     bx,segm
  425.         mov     word ptr [bx],cx
  426.         mov     bx,offs
  427.         mov     word ptr [bx],dx
  428.     xor    ax, ax
  429. @@end:
  430.     ret    
  431. GetExceptionVector     endp
  432.  
  433. ifdef HAVE386
  434.    ;
  435.    ;    int GetExceptionVector32(BYTE b,WORD *sel,DWORD *off)
  436.    ;    
  437.         public  C GetExceptionVector32
  438. GetExceptionVector32 PROC C \
  439.         no      :BYTE , \
  440.         segm    :PTR , \
  441.         offs    :PTR
  442.  
  443.         mov     bl,no
  444.         DPMI 0202h
  445.     CHECKERR
  446. @@ok:
  447.     mov    bx,segm
  448.     mov    word ptr [bx],cx
  449.     mov    bx,offs
  450.     mov    word ptr [bx],dx
  451.     .386
  452.         shr     edx,16
  453.     .286
  454.     mov    word ptr [bx].2,dx
  455.         xor     ax,ax
  456. @@end:
  457.     ret    
  458. GetExceptionVector32   endp
  459. endif
  460.  
  461.    ;
  462.    ;    int SetExceptionVector(BYTE b,WORD sel,WORD off)
  463.    ;    
  464.         public  C SetExceptionVector
  465. SetExceptionVector PROC C \
  466.         b       :BYTE , \
  467.         segm    :WORD , \
  468.         offs    :WORD
  469.  
  470.         mov     bl,b
  471.         mov     cx,segm
  472.         mov     dx,offs
  473.         DPMI 0203h
  474.     CHECKERR
  475. @@ok:
  476.     xor    ax, ax
  477. @@end:
  478.     ret    
  479. SetExceptionVector     endp
  480.  
  481. ifdef HAVE386
  482.    ;
  483.    ;    int SetExceptionVector32(BYTE b,WORD sel,DWORD off)
  484.    ;    
  485.     public    C SetExceptionVector32
  486. SetExceptionVector32 PROC C \
  487.         b       :BYTE , \
  488.         segm    :WORD , \
  489.     offs    :DWORD
  490.  
  491.     mov    bl,b
  492.     mov    cx,segm
  493.     .386
  494.     mov    edx,offs
  495.     .286
  496.         DPMI 0203h
  497.     CHECKERR
  498. @@ok:
  499.     xor    ax,ax
  500. @@end:
  501.     .386
  502.     xor    edx,edx
  503.     .286
  504.     ret    
  505. SetExceptionVector32   endp
  506. endif
  507.  
  508.    ;
  509.    ;    int GetProtModeVector(BYTE b,WORD *sel,WORD *off)
  510.    ;    
  511.     public    C GetProtModeVector
  512. GetProtModeVector PROC C \
  513.         no      :BYTE , \
  514.         segm    :PTR , \
  515.         offs    :PTR
  516.  
  517.         mov     bl,no
  518.         DPMI 0204h
  519.     CHECKERR
  520. @@ok:
  521.         mov     bx,segm
  522.         mov     word ptr [bx],cx
  523.         mov     bx,offs
  524.         mov     word ptr [bx],dx
  525.     xor    ax, ax
  526. @@end:
  527.     ret    
  528. GetProtModeVector   endp
  529.  
  530. ifdef HAVE386
  531.    ;
  532.    ;    int GetProtModeVector32(BYTE b,WORD *sel,DWORD *off)
  533.    ;    
  534.     public    C GetProtModeVector32
  535. GetProtModeVector32 PROC C \
  536.         no      :BYTE , \
  537.         segm    :PTR , \
  538.         offs    :PTR
  539.  
  540.         mov     bl,no
  541.         DPMI 0204h
  542.     CHECKERR
  543. @@ok:
  544.         mov     bx,segm
  545.         mov     word ptr [bx],cx
  546.         mov     bx,offs
  547.         mov     word ptr [bx],dx
  548.     .386
  549.     shr    edx,16
  550.     .286
  551.     mov    word ptr [bx].2,dx
  552.     xor    ax,ax
  553. @@end:
  554.     ret    
  555. GetProtModeVector32 endp
  556. endif
  557.  
  558.    ;    
  559.    ;    int SetProtModeVector(BYTE b,WORD sel,WORD off)
  560.    ;    
  561.     public    C SetProtModeVector
  562. SetProtModeVector PROC C \
  563.         b       :BYTE , \
  564.         segm    :WORD , \
  565.         offs    :WORD
  566.  
  567.         mov     bl,b
  568.         mov     cx,segm
  569.         mov     dx,offs
  570.         DPMI 0205h
  571.     CHECKERR
  572. @@ok:
  573.     xor    ax, ax
  574. @@end:
  575.     ret    
  576. SetProtModeVector   endp
  577.  
  578. ifdef HAVE386
  579.    ;
  580.    ;    int SetProtModeVector32(BYTE b,WORD sel,DWORD off)
  581.    ;    
  582.     public    C SetProtModeVector32
  583. SetProtModeVector32 PROC C \
  584.         b       :BYTE , \
  585.         segm    :WORD , \
  586.     offs    :DWORD
  587.  
  588.         mov     bl,b
  589.         mov     cx,segm
  590.     .386
  591.     mov    edx,offs
  592.     .286
  593.         DPMI 0205h
  594.     CHECKERR
  595. @@ok:
  596.     xor    ax, ax
  597. @@end:
  598.     ret    
  599. SetProtModeVector32 endp
  600. endif
  601.  
  602.    ;    
  603.    ;    int SimulateRMint(BYTE int,BYTE flag,WORD args,NPTRANSLATION call,..)
  604.    ;    
  605.         public  C SimulateRMint
  606. SimulateRMint PROC C USES SI DI, \
  607.         intnr   :BYTE, \
  608.         flags   :BYTE, \
  609.         stwords :WORD, \
  610.         trans   :PTR, \
  611.         argv    :BYTE
  612.  
  613.         push    ds              ; small model! ds=ss=es
  614.         pop     es
  615.         mov     cx,stwords
  616.         jcxz    @@simulate
  617. ; copy
  618.         sub     sp, cx          ;
  619.         sub     sp, cx          ; space for cx words
  620.         mov     di, sp
  621.         lea     si, argv
  622.     cld
  623.         rep movsw
  624.  
  625.         mov     cx, stwords     ; reload after movs
  626. @@simulate:
  627.         mov     bh, flags
  628.         mov     bl, intnr
  629. ifdef HAVE386
  630.     .386
  631.     movzx    edi, trans
  632.     .286
  633. else
  634.     mov    di,trans
  635. endif
  636.         DPMI 0300h
  637.     CHECKERR
  638. @@ok:
  639.         xor     ax, ax
  640. @@end:
  641.         mov     cx, stwords
  642.         add     sp,stwords
  643.         add     sp,stwords
  644.     ret    
  645. SimulateRMint  endp
  646.  
  647.    ;
  648.    ;    int CallRMprocFar(BYTE flag,WORD args,NPTRANSLATION call,...)
  649.    ;    
  650.         public  C CallRMprocFar
  651. CallRMprocFar PROC C USES SI DI , \
  652.         flags   :BYTE, \
  653.         stwords :WORD, \
  654.         trans   :PTR, \
  655.         argv    :BYTE
  656.  
  657.         push    ds              ; small model! ds=ss=es
  658.         pop     es
  659.         mov     cx,stwords
  660.         jcxz    @@simulate
  661. ; copy
  662.         sub     sp, cx
  663.         sub     sp, cx          ; space for cx words
  664.         mov     di, sp
  665.         lea     si, argv
  666.     cld
  667.         rep movsw
  668.  
  669.         mov     cx, stwords
  670. @@simulate:
  671.         xor     bx,bx   ; don't use flags
  672. ifdef HAVE386
  673.     .386
  674.     movzx    edi, trans
  675.     .286
  676. else
  677.     mov    di,trans
  678. endif
  679.         DPMI 0301h
  680.     CHECKERR
  681. @@ok:
  682.         xor     ax, ax
  683. @@end:
  684.         mov     cx, stwords
  685.         add     sp,cx
  686.         add     sp,cx
  687.     ret    
  688. CallRMprocFar  endp
  689.  
  690.    ;    
  691.    ;    int CallRMprocIret(BYTE flag,WORD args,NPTRANSLATION call,...)
  692.    ;    
  693.         public  C CallRMprocIret
  694. CallRMprocIret PROC C USES SI DI, \
  695.         flags   :BYTE, \
  696.         stwords :WORD, \
  697.         trans   :PTR, \
  698.         argv    :BYTE
  699.  
  700.         push    ds              ; small model! ds=ss=es
  701.         pop     es
  702.         mov     cx,stwords
  703.         mov     cx,stwords
  704.         jcxz    @@simulate
  705. ; copy
  706.         sub     sp, cx
  707.         mov     di, sp
  708.         lea     si, argv
  709.     cld
  710.         rep movsw
  711.  
  712.         mov     cx, stwords
  713. @@simulate:
  714.         mov     bh, flags
  715. ifdef HAVE386
  716.     .386
  717.     movzx    edi, trans
  718.     .286
  719. else
  720.     mov    di,trans
  721. endif
  722.         DPMI 0302h
  723.     CHECKERR
  724. @@ok:
  725.         xor     ax, ax
  726. @@end:
  727.         mov     cx, stwords
  728.         add     sp,stwords
  729.         add     sp,stwords
  730.     ret    
  731. CallRMprocIret endp
  732.  
  733.    ;    
  734.    ;    int AllocRMcallAddress(WORD pmsel,WORD pmoff,NPTRANSLATION call,
  735.    ;                            WORD *rmseg,WORD *rmoff)
  736.    ;    
  737.         public  C AllocRMcallAddress
  738. AllocRMcallAddress PROC C USES SI DI, \
  739.         pmsel   :WORD , \
  740.         pmoff   :WORD , \
  741.         trans   :PTR ,  \
  742.         rmseg   :PTR ,  \
  743.         rmoff   :PTR
  744.  
  745.         push    ds
  746.         pop     es
  747.         push    ds
  748.         mov     ds,pmsel
  749. ifdef HAVE386
  750.     .386
  751.     movzx    esi, pmoff
  752.     movzx    edi, trans
  753.     .286
  754. else
  755.     mov    si, pmoff
  756.     mov    di, trans
  757. endif
  758.         DPMI 0303h
  759.         pop     ds
  760.     CHECKERR
  761. @@ok:
  762.         mov     bx,rmseg
  763.     mov    word ptr [bx],cx
  764.         mov     bx,rmoff
  765.     mov    word ptr [bx],dx
  766.     xor    ax, ax
  767. @@end:
  768.     ret    
  769. AllocRMcallAddress     endp
  770.  
  771.    ;
  772.    ;    int FreeRMcallAddress(WORD rmseg,WORD rmoff)
  773.    ;    
  774.         public  C FreeRMcallAddress
  775. FreeRMcallAddress PROC C \
  776.         rmseg   :WORD , \
  777.         rmoff   :WORD
  778.  
  779.         mov     cx,rmseg
  780.         mov     dx,rmoff
  781.         DPMI 0304h
  782.     CHECKERR
  783. @@ok:
  784.     xor    ax, ax
  785. @@end:
  786.     ret    
  787. FreeRMcallAddress      endp
  788.  
  789.    ;
  790.    ;    int GetStateSaveAddress(WORD *size,POINTER16_16 *,POINTER16_16 *)
  791.    ;    
  792.     public    C GetStateSaveAddress
  793. GetStateSaveAddress PROC C USES SI DI, \
  794.     bytes    :PTR ,    \
  795.     realaddr:PTR ,    \
  796.     protaddr:PTR ,    \
  797.  
  798.     DPMI 0305h
  799.     CHECKERR
  800. @@ok:
  801.     mov    dx, bx
  802.     mov    bx, bytes
  803.     mov    word ptr [bx],ax
  804.     mov    bx, realaddr
  805.     setdword [bx],dx,cx
  806.     mov    bx, protaddr
  807. ifdef HAVE386
  808.     .386
  809.     xor    edi, edi
  810.     .286
  811. endif
  812.     setdword [bx],si,di
  813.     xor    ax, ax
  814. @@end:
  815.     ret    
  816. GetStateSaveAddress    endp
  817.  
  818.    ;
  819.    ;    int SaveState(WORD *buffer,POINTER16_16);
  820.    ;
  821.     public    C SaveState
  822. SaveState PROC C USES DI, \
  823.     buffer    :PTR ,    \
  824.         offaddr :BYTE
  825.  
  826.     push    ds
  827.     pop    es
  828. ifdef HAVE386
  829.     .386
  830.     movzx    edi, buffer
  831.     .286
  832. else
  833.     mov    di, buffer
  834. endif
  835.         mov     ax, 0
  836.     call    dword ptr offaddr
  837.     ret
  838. SaveState    endp
  839.  
  840.    ;
  841.    ;    int RestoreState(WORD *buffer,POINTER16_16);
  842.    ;    
  843.     public    C RestoreState
  844. RestoreState PROC C USES DI, \
  845.     buffer    :PTR ,    \
  846.         offaddr :BYTE
  847.  
  848.     push    ds
  849.     pop    es
  850. ifdef HAVE386
  851.     .386
  852.     movzx    edi, buffer
  853.     .286
  854. else
  855.     mov    di, buffer
  856. endif
  857.         mov     ax, 1
  858.         call    dword ptr offaddr
  859.     ret
  860. RestoreState       endp
  861.  
  862.    ;
  863.    ;    void GetDPMIVersion(DPMIVERSION *v)
  864.    ;    
  865.         public  C GetDPMIVersion
  866. GetDPMIVersion PROC C USES SI, \
  867.         verptr  :PTR
  868.                                 ; packed struct req!
  869.         mov     si,verptr
  870.         DPMI 0400h
  871.     mov    byte ptr [si],ah
  872.     mov    byte ptr [si+1],al
  873.     mov    word ptr [si+2],bx
  874.     mov    byte ptr [si+4],cl
  875.     mov    byte ptr [si+5],dh
  876.     mov    byte ptr [si+6],dl
  877.     ret    
  878. GetDPMIVersion endp
  879.  
  880.    ;    
  881.    ;    int GetFreeMemInfo(NPFREEMEMINFO fm)
  882.    ;    
  883.         public  C GetFreeMemInfo
  884. GetFreeMemInfo PROC C USES SI, \
  885.         fm      :PTR
  886.  
  887.      push     ds 
  888.     pop     es
  889. ifdef HAVE386
  890.     .386
  891.     movzx    edi,fm
  892.     .286
  893. else
  894.         mov     di,fm
  895. endif
  896.         DPMI 0500h
  897.     CHECKERR
  898. @@ok:
  899.     xor    ax, ax
  900. @@end:
  901.     ret    
  902. GetFreeMemInfo endp
  903.  
  904.    ;    
  905.    ;    int AllocMem(DWORD bytes,DWORD *handle,DWORD *memaddress)
  906.    ;    
  907.         public  C AllocMem
  908. AllocMem PROC C USES SI DI, \
  909.         nbytes  :DWORD , \
  910.         handle  :PTR , \
  911.         memadr  :PTR
  912.  
  913.         getdword bx,cx,nbytes
  914.         DPMI 0501h
  915.     CHECKERR
  916. @@ok:
  917.     mov    ax,bx
  918.         mov     bx,handle
  919.         setdword [bx],si,di
  920.         mov     bx,memadr
  921.         setdword [bx],ax,cx
  922.     xor    ax, ax
  923. @@end:
  924.     ret    
  925. AllocMem       endp
  926.  
  927.    ;
  928.    ;    int FreeMem(DWORD handle)
  929.    ;    
  930.         public  C FreeMem
  931. FreeMem PROC C USES SI DI, \
  932.         handle  :DWORD
  933.  
  934.         getdword si,di,handle
  935.         DPMI 0502h
  936.     CHECKERR
  937. @@ok:
  938.     xor    ax, ax
  939. @@end:
  940.     ret    
  941. FreeMem        endp
  942.  
  943.    ;
  944.    ;    int ResizeMem(DWORD bytes,DWORD oldhandle,DWORD *newhandle,DWORD *newaddress)
  945.    ;    
  946.         public  C ResizeMem
  947. ResizeMem PROC C USES SI DI , \
  948.         nbytes  :DWORD , \
  949.         oldh    :DWORD , \
  950.         newh    :PTR   , \
  951.         newadr  : PTR
  952.  
  953.         getdword bx,cx,nbytes
  954.         getdword si,di,oldh
  955.         DPMI 0503h
  956.     CHECKERR
  957. @@ok:
  958.     mov    ax,bx
  959.         mov     bx,newadr
  960.         setdword [bx],ax,cx
  961.         mov     bx,newh
  962.         setdword [bx],si,di
  963.     xor    ax, ax
  964. @@end:
  965.     ret    
  966. ResizeMem      endp
  967.  
  968.    ;    
  969.    ;    int LockLinRegion(DWORD size,DWORD address)
  970.    ;    
  971.         public  C LockLinRegion
  972. LockLinRegion PROC C USES SI DI, \
  973.         sizeb   :DWORD , \
  974.         addr    :DWORD
  975.  
  976.         getdword bx,cx,addr
  977.         getdword si,di,sizeb
  978.         DPMI 0600h
  979.     CHECKERR
  980. @@ok:
  981.     xor    ax, ax
  982. @@end:
  983.     ret    
  984. LockLinRegion  endp
  985.  
  986.    ;    
  987.    ;    int UnlockLinRegion(DWORD size,DWORD address)
  988.    ;    
  989.         public  C UnlockLinRegion
  990. UnlockLinRegion PROC C USES SI DI, \
  991.         sizeb   :DWORD , \
  992.         address :DWORD
  993.  
  994.         getdword si,di,sizeb
  995.         getdword bx,cx,address
  996.         DPMI 0601h
  997.     CHECKERR
  998. @@ok:
  999.     xor    ax, ax
  1000. @@end:
  1001.     ret    
  1002. UnlockLinRegion        endp
  1003.  
  1004.    ;
  1005.    ;    int MarkRealModePageable(DWORD size,DWORD address)
  1006.    ;    
  1007.         public  C MarkRealModePageable
  1008. MarkRealModePageable PROC C USES SI DI , \
  1009.         sizeb   :DWORD , \
  1010.         addr    :DWORD
  1011.  
  1012.         getdword bx,cx,addr
  1013.         getdword si,di,sizeb
  1014.         DPMI 0602h
  1015.     CHECKERR
  1016. @@ok:
  1017.     xor    ax, ax
  1018. @@end:
  1019.     ret    
  1020. MarkRealModePageable   endp
  1021.  
  1022.    ;
  1023.    ;    int RelockRealModeRegion(DWORD size,DWORD address)
  1024.    ;    
  1025.         public  C RelockRealModeRegion
  1026. RelockRealModeRegion PROC C USES SI DI, \
  1027.         sizeb   :DWORD , \
  1028.         addr    :DWORD
  1029.  
  1030.         getdword bx,cx,addr
  1031.         getdword si,di,sizeb
  1032.         DPMI 0603h
  1033.     CHECKERR
  1034. @@ok:
  1035.     xor    ax, ax
  1036. @@end:
  1037.     ret    
  1038. RelockRealModeRegion   endp
  1039.  
  1040.    ;    
  1041.    ;    int GetPageSize(DWORD *size)
  1042.    ;    
  1043.         public  C GetPageSize
  1044. GetPageSize PROC C \
  1045.         sizeb   :PTR
  1046.  
  1047.         DPMI 0604h
  1048.     CHECKERR
  1049. @@ok:
  1050.         mov     ax,bx
  1051.         mov     bx,sizeb
  1052.         setdword [bx],ax,cx
  1053.     xor    ax, ax
  1054. @@end:
  1055.     ret    
  1056. GetPageSize    endp
  1057.  
  1058.    ;    
  1059.    ;    int MarkPageDemand(DWORD address,DWORD n)
  1060.    ;    
  1061.         public  C MarkPageDemand
  1062. MarkPageDemand PROC C USES SI DI , \
  1063.         addr    :DWORD , \
  1064.         nbytes  :DWORD
  1065.  
  1066.         getdword bx,cx,addr
  1067.         getdword si,di,nbytes
  1068.         DPMI 0702h
  1069.     CHECKERR
  1070. @@ok:
  1071.     xor    ax, ax
  1072. @@end:
  1073.     ret    
  1074. MarkPageDemand endp
  1075.  
  1076.    ;    
  1077.    ;    int DiscardPage(DWORD address,DWORD size)
  1078.    ;    
  1079.         public  C DiscardPage
  1080. DiscardPage PROC C USES SI DI , \
  1081.         addr    :DWORD , \
  1082.         sizeb   :DWORD
  1083.  
  1084.         getdword bx,cx,addr
  1085.         getdword si,di,sizeb
  1086.         DPMI 0703h
  1087.     CHECKERR
  1088. @@ok:
  1089.     xor    ax, ax
  1090. @@end:
  1091.     ret    
  1092. DiscardPage    endp
  1093.  
  1094.    ;    
  1095.    ;    int PhysicalMap(DWORD physical,DWORD size,DWORD *linear)
  1096.    ;    
  1097.         public  C PhysicalMap
  1098. PhysicalMap PROC C USES SI DI , \
  1099.         physic  :DWORD , \
  1100.         sizeb   :DWORD , \
  1101.         linear  :PTR
  1102.  
  1103.         getdword bx,cx,physic
  1104.         getdword si,di,sizeb
  1105.         DPMI 0800h
  1106.     CHECKERR
  1107. @@ok:
  1108.         mov     di,linear
  1109.         setdword [di],bx,cx
  1110.     xor    ax, ax
  1111. @@end:
  1112.         ret     
  1113. PhysicalMap    endp
  1114.  
  1115.    ;    
  1116.    ;    BYTE DisableVirtuelInterruptState()
  1117.    ;    
  1118.     public    C DisableVirtuelInterruptState
  1119. DisableVirtuelInterruptState PROC C
  1120.         DPMI 0900h
  1121.         xor     ah,ah
  1122.     ret    
  1123. DisableVirtuelInterruptState    endp
  1124.  
  1125.    ;    
  1126.    ;    BYTE EnableVirtuelInterruptState()
  1127.    ;    
  1128.     public    C EnableVirtuelInterruptState
  1129. EnableVirtuelInterruptState PROC C
  1130.         DPMI 0901h
  1131.         xor     ah,ah
  1132.     ret    
  1133. EnableVirtuelInterruptState    endp
  1134.  
  1135.    ;    
  1136.    ;    BYTE GetVirtuelInterruptState()
  1137.    ;    
  1138.     public    C GetVirtuelInterruptState
  1139. GetVirtuelInterruptState PROC C
  1140.         DPMI 0902h
  1141.         xor     ah,ah
  1142.     ret    
  1143. GetVirtuelInterruptState    endp
  1144.  
  1145.    ;    
  1146.    ;    int GetVendorEntry(BYTE *p,WORD *sel,WORD *off)
  1147.    ;    
  1148.         public  C GetVendorEntry
  1149. GetVendorEntry PROC C USES ES SI DI , \
  1150.         string  :PTR , \
  1151.         sel     :PTR , \
  1152.         offs    :PTR
  1153.  
  1154. ifdef HAVE386
  1155.     .386
  1156.     movzx    edi,string
  1157.     .286
  1158. else
  1159.     mov    di,string
  1160. endif
  1161.         DPMI 0A00h
  1162.     CHECKERR
  1163. @@ok:
  1164.         mov     bx,sel
  1165.         mov     word ptr [bx],es
  1166.         mov     bx,offs
  1167.     mov    word ptr [bx],si
  1168.     xor    ax, ax
  1169. @@end:
  1170.     ret    
  1171. GetVendorEntry endp
  1172.  
  1173.    ;    
  1174.    ;    int SetDebugWatchpoint(DWORD address,WORD type,WORD *handle)
  1175.    ;    
  1176.         public  C SetDebugWatchpoint
  1177. SetDebugWatchpoint PROC C , \
  1178.         addr    :DWORD , \
  1179.         types   :WORD , \
  1180.         handle  :PTR
  1181.  
  1182.         getdword bx,cx,addr
  1183.         mov     dx,types
  1184.         DPMI 0B00h
  1185.     CHECKERR
  1186. @@ok:
  1187.     mov    dx,bx
  1188.         mov     bx,handle
  1189.         mov     word ptr [bx],dx
  1190.     xor    ax, ax
  1191. @@end:
  1192.     ret    
  1193. SetDebugWatchpoint     endp
  1194.  
  1195.    ;    
  1196.    ;    int ClearDebugWatchpoint(WORD handle)
  1197.    ;    
  1198.         public  C ClearDebugWatchpoint
  1199. ClearDebugWatchpoint PROC C \
  1200.         handle  :WORD
  1201.  
  1202.         mov     bx,handle
  1203.         DPMI 0B01h
  1204.     CHECKERR
  1205. @@ok:
  1206.     xor    ax, ax
  1207. @@end:
  1208.     ret    
  1209. ClearDebugWatchpoint   endp
  1210.  
  1211.    ;    
  1212.    ;    int GetStateDebugWatchpoint(WORD handle,WORD *execute)
  1213.    ;    
  1214.         public  C GetStateDebugWatchpoint
  1215. GetStateDebugWatchpoint PROC C \
  1216.         handle  :WORD , \
  1217.         execute :PTR
  1218.  
  1219.         mov     bx,handle
  1220.         DPMI 0B02h
  1221.     CHECKERR
  1222. @@ok:
  1223.         mov     bx,execute
  1224.         mov     [bx],ax
  1225.     xor    ax, ax
  1226. @@end:
  1227.     ret    
  1228. GetStateDebugWatchpoint        endp
  1229.  
  1230.    ;    
  1231.    ;    int ResetDebugWatchpoint(WORD handle)
  1232.    ;    
  1233.         public  C ResetDebugWatchpoint
  1234. ResetDebugWatchpoint PROC C \
  1235.         handle  :WORD
  1236.  
  1237.         mov     bx,handle
  1238.         DPMI 0B03h
  1239.     CHECKERR
  1240. @@ok:
  1241.     xor    ax, ax
  1242. @@end:
  1243.     ret    
  1244. ResetDebugWatchpoint   endp
  1245.  
  1246.  
  1247. ;
  1248. ; Protected mode instructions
  1249. ;
  1250.  
  1251.     .286p
  1252.  
  1253.    ;    
  1254.    ;    WORD lsl16(WORD sel)    16-bit version of lsl for 80286
  1255.    ;    
  1256.         public  C lsl16
  1257. lsl16 PROC C \
  1258.         sel     :WORD
  1259.  
  1260.         xor      ax, ax
  1261.         lsl      ax,sel
  1262.     ret    
  1263. lsl16  endp
  1264.  
  1265. ifdef HAVE386
  1266.    ;
  1267.    ;    DWORD lsl32(WORD sel)  /* load selector limit for 80386 */
  1268.    ;    
  1269.     public    C lsl32
  1270. lsl32  PROC C \
  1271.     sel    :WORD
  1272.  
  1273.     .386p
  1274.     movzx    ebx,sel
  1275.         lsl     eax,ebx
  1276.         mov     edx,eax
  1277.         shr     edx,16
  1278.         and     eax,dword ptr 0ffffh
  1279.     .286p
  1280.     ret    
  1281. lsl32  endp
  1282. endif
  1283.  
  1284.    ;
  1285.    ;    WORD lar16(WORD sel)    16-bit version of lar for 80286
  1286.    ;    
  1287.         public  C lar16
  1288. lar16 PROC C \
  1289.         sel     :WORD
  1290.  
  1291.         xor      ax, ax
  1292.         lar      ax,sel
  1293.      shr     ax,8 
  1294.         ret
  1295. lar16  endp
  1296.  
  1297. ifdef HAVE386
  1298.    ;
  1299.    ;    WORD lar16(WORD sel)    16-bit version of lar for 80286
  1300.    ;    
  1301.     public    C lar32
  1302. lar32  PROC C \
  1303.     sel    :WORD
  1304.     .386p
  1305.     movzx    ebx,sel
  1306.         lar     eax,ebx
  1307.         mov     edx,eax
  1308.         shr     edx,16
  1309.         and     eax,dword ptr 0ffffh
  1310.     .286p
  1311.     ret    
  1312. lar32  endp
  1313. endif
  1314.  
  1315.    ;
  1316.    ;    WORD verr(WORD sel)    verify read
  1317.    ;    
  1318.         public  C verr16
  1319. verr16 PROC C \
  1320.         sel     :WORD
  1321.  
  1322.         mov     ax,1
  1323.         verr    sel
  1324.         je      short @@nok
  1325.         dec     ax
  1326. @@nok:
  1327.     ret    
  1328. verr16 endp
  1329.  
  1330.    ;
  1331.    ;    WORD verw(WORD sel)    verify write
  1332.    ;    
  1333.         public  C verw16
  1334. verw16 PROC C \
  1335.         sel     :WORD
  1336.  
  1337.         mov     ax,1
  1338.         verw    sel
  1339.         je      short @@nok
  1340.         dec     ax
  1341. @@nok:
  1342.     ret    
  1343. verw16 endp
  1344.  
  1345.    ;
  1346.    ;    void sgdt(LPGDTR g)    save gdt table
  1347.    ;    
  1348.         public  C sgdt16
  1349. sgdt16 PROC C \
  1350.         g       :DWORD
  1351.  
  1352.         les      bx,g
  1353.      sgdt     es:[bx] 
  1354.     ret    
  1355. sgdt16 endp
  1356.  
  1357.    ;
  1358.    ;    void sidt(LPGDTR g)    save idt table
  1359.    ;    
  1360.         public  C sidt16
  1361. sidt16 PROC C \
  1362.         g       :DWORD
  1363.  
  1364.         les      bx,g
  1365.      sidt     es:[bx] 
  1366.     ret    
  1367. sidt16 endp
  1368.  
  1369.    ;
  1370.    ;    WORD sldt(void)     save ldt-selector
  1371.    ;    
  1372.         public  C sldt16
  1373. sldt16 PROC C
  1374.      sldt     ax 
  1375.     ret    
  1376. sldt16 endp
  1377.  
  1378.    ;
  1379.    ;    WORD str16(void)      save task-register
  1380.    ;    
  1381.         public  C str16
  1382. str16 PROC C
  1383.      str     ax 
  1384.     ret    
  1385. str16  endp
  1386.  
  1387.    ;
  1388.    ;    void far * IncFP(void far *a)    increment far piointer
  1389.    ;    
  1390.         public  C IncFP
  1391. IncFP PROC C \
  1392.     aptr    :DWORD
  1393.  
  1394.         call    near ptr SelInc
  1395.     mov    cx, ax
  1396.     getdword dx,ax,aptr
  1397.     add    dx,cx
  1398.     ret    
  1399. IncFP  endp
  1400.  
  1401.    ;
  1402.    ;    void far * DecFP(void far *a)    decrement far pointer
  1403.    ;    
  1404.         public  C DecFP
  1405. DecFP PROC C \
  1406.     aptr    :DWORD
  1407.  
  1408.         call    near ptr SelInc
  1409.     mov    cx, ax
  1410.     getdword dx,ax,aptr
  1411.     sub    dx,cx
  1412.     ret    
  1413. DecFP  endp
  1414.  
  1415.    ;
  1416.    ;    int IsPM()    execute in prot-mode ? ax==0, PMode under DPMI
  1417.    ;    
  1418.         public  C IsPM
  1419. IsPM PROC C
  1420.         mov     ax,1686h
  1421.         int     02FH
  1422.     ret    
  1423. IsPM   endp
  1424.  
  1425.    ;
  1426.    ;    int IsWindowsEnhanced(void)
  1427.    ;    
  1428.         public  C IsWindowsEnhanced
  1429. IsWindowsEnhanced PROC C
  1430.     mov    ax,01600H
  1431.     int    02fH
  1432.         cmp     al,3
  1433.     jb    short @@not
  1434.     cmp    al,10
  1435.     ja    short @@not
  1436.     mov    ax, 1
  1437.     jmp    short @@end
  1438. @@not:
  1439.         xor     ax, ax
  1440. @@end:
  1441.     ret    
  1442. IsWindowsEnhanced      endp
  1443.  
  1444.    ;
  1445.    ;    void yield() /* give control to operating system (like GetMassage) */
  1446.    ;    
  1447.         public  C yield
  1448. yield PROC C
  1449.         mov     ax,1680H
  1450.         int     2Fh
  1451.     ret    
  1452. yield  endp
  1453.  
  1454.     public dos_exit
  1455. dos_exit PROC C \
  1456.     errorlevel:WORD
  1457.  
  1458.     mov    ax, errorlevel
  1459.     mov    ah, 04Ch
  1460.     int    021h
  1461.     ret
  1462. dos_exit endp
  1463.  
  1464.     public DpmiEnableFpu
  1465. DpmiEnableFpu PROC C \
  1466.     bits    :WORD
  1467.  
  1468.     mov    bx, bits
  1469.     DPMI 0E01h
  1470.     CHECKERR
  1471. @@ok:
  1472.     xor    ax, ax
  1473. @@end:
  1474.     ret
  1475. DpmiEnableFpu endp
  1476.  
  1477.     public C DpmiDisableFpu
  1478. DpmiDisableFpu PROC C
  1479.     mov    bx, 0
  1480.     DPMI 0E01h
  1481.     CHECKERR
  1482. @@ok:
  1483.     xor    ax, ax
  1484. @@end:
  1485.     ret
  1486. DpmiDisableFpu endp
  1487.  
  1488.     public C GetCS
  1489. GetCS PROC C
  1490.     mov    ax,cs
  1491.     ret
  1492. GetCS endp
  1493.  
  1494.     public C GetDS
  1495. GetDS PROC C
  1496.     mov    ax,ds
  1497.     ret
  1498. GetDS endp
  1499.  
  1500.     public C GetES
  1501. GetES PROC C
  1502.     mov    ax,es
  1503.     ret
  1504. GetES endp
  1505.  
  1506.     public C GetDpmiEntryPoint
  1507. GetDpmiEntryPoint PROC C USES SI DI , \
  1508.     entry    :PTR, \
  1509.     dpmipara:PTR, \
  1510.     flags    :PTR, \
  1511.     version :PTR, \
  1512.     cpu    :PTR
  1513.  
  1514.     mov    ax, 1687h
  1515.     int    2Fh
  1516.     or    ax,ax
  1517.     jnz    noDPMI
  1518.     mov    ax, bx        ; save bx = bits
  1519.  
  1520.     mov    bx, entry
  1521.     mov    [bx], di
  1522.     mov    [bx+2], es
  1523.     mov    bx, flags
  1524.     mov    [bx], ax
  1525.     mov    bx, version
  1526.     mov    [bx], dx
  1527.     mov    bx, cpu
  1528.     mov    [bx], cl
  1529.     mov    bx, dpmipara
  1530.     mov    [bx], si
  1531.         xor     ax, ax
  1532. noDPMI:
  1533.     ret
  1534. GetDpmiEntryPoint endp
  1535.  
  1536.     public C GetDpmiHostParagraph
  1537. GetDpmiHostParagraph PROC C \
  1538.     dpmipara:WORD
  1539.  
  1540.     mov    bx, dpmipara
  1541.     mov    ah, 48h
  1542.     int    21h
  1543.     jnc    @@end
  1544.     or    ax,ax
  1545. @@end:
  1546.     ret
  1547. GetDpmiHostParagraph endp
  1548.  
  1549.     public C DpmiEnterProtectedMode
  1550. DpmiEnterProtectedMode PROC C \
  1551.     entry    :DWORD, \
  1552.     flags    :WORD, \
  1553.     parasegm:WORD
  1554.  
  1555.     mov    ax, parasegm
  1556.     mov    es, ax
  1557.     mov    ax, flags
  1558.     call    dword ptr entry
  1559.     jnc    @@ok
  1560.     mov    ax, -1
  1561.     jmp    short @@end
  1562. @@ok:
  1563.     xor    ax, ax
  1564. @@end:
  1565.     ret
  1566. DpmiEnterProtectedMode endp
  1567.  
  1568.     end
  1569.