home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2MISC / PMODE12S.ZIP / SRC / PMODE / PMODETSR.ASM < prev    next >
Encoding:
Assembly Source File  |  1997-02-22  |  3.4 KB  |  190 lines

  1. ; Copyright (C) 1996 CW Sandmann (sandmann@clio.rice.edu) 1206 Braelinn, Sugarland, TX 77479
  2. ;
  3. ; This file is distributed WITHOUT ANY WARRANTY; without even the implied
  4. ; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  5.  
  6.     title    cws_tsr_pmode
  7.  
  8. SW_FLAGS        = 0002h                 ; detect VCPI first, reprogram pic
  9.  
  10.     p386
  11.     SEGMENT _TEXT PUBLIC USE16 'CODE'
  12.     group DGROUP _DATA, _BSS
  13.     assume cs:_TEXT, ds:DGROUP, es:nothing, ss:nothing
  14.     EXTRN    pm_info:near, pm_init:near
  15.  
  16. _dpmiint2f:
  17.     cmp    ax,1687h
  18.     jne    short not_us
  19. ct:    mov    cl,3
  20.     xor    ax,ax            ;Yes, we are here
  21.     mov    bx,1            ;32 bit programs supported
  22.     mov    dx,5ah            ;0.90 version
  23. pn:    mov    si,1            ;paragraphs needed
  24.     push    cs
  25.     pop    es
  26.     mov    di,offset dpmientry
  27.     iret
  28. not_us:
  29.     db    0eah
  30. oldint2f    label    word
  31.     dw    2 dup (?)
  32.  
  33. dpmientry:                ;far return point cs:ip on stack
  34.     test    al,1
  35.     jnz    short ok_32
  36.     stc
  37.     retf
  38. ok_32:
  39.     pushad
  40.     push    ds
  41.     push    ss            ; so we can compare later
  42.  
  43. ; restore old int 2f vector since pmode is not reentrable and looks at int 2f
  44.     xor    ax,ax
  45.     mov    ds,ax
  46.     mov    eax, dword ptr cs:oldint2f
  47.     mov    ds:[2fh*4], eax        ; atomic, cli not needed
  48.  
  49.     push    DGROUP
  50.     pop    ds
  51.     mov    ax,SW_FLAGS        ; switch flags
  52.     call    pm_init            ; attempt to switch
  53.  
  54.     jnc    short in_pm
  55.     pop    ax
  56.     pop    ds
  57.     popad
  58.     retf
  59. in_pm:
  60.     pop    ax            ; original ss
  61.     pop    dx            ; original ds
  62.     cmp    ax,dx
  63.     mov    bx,ss            ; bx is just ss if same originally
  64.     je    short no_new_ds
  65.  
  66.     call    seg_to_sel
  67. no_new_ds:
  68.     mov    ds,bx            ; just in case
  69.  
  70.     mov    dx,[esp+34]        ; 8 dwords for pushad and 2 bytes for IP
  71.     call    seg_to_sel
  72.     mov    [esp+34],bx
  73.  
  74.     mov    ax,0009h        ; set selector type and access rights
  75.     mov    dx,cs            ; get DPL from current CPL, and access
  76.     lar    cx,dx            ;    rights and type from current CS
  77.     shr    cx,8            ; type is already 16bit code segment
  78.     int    31h
  79.     jc    short immed_exit
  80.  
  81.     popad
  82.     retf
  83.  
  84. ; Enter with segment in dx, exit with selector in bx
  85. seg_to_sel:
  86.     mov    cx,1
  87.     mov    ax,0
  88.     int    31h
  89.     mov    bx,ax
  90.  
  91.     movzx    edx,dx
  92.     shl    edx,4
  93.     mov    ecx,edx
  94.     shr    ecx,16
  95.     mov    ax,007h
  96.     int    31h
  97.  
  98.     mov    cx,0
  99.     mov    dx,-1
  100.     mov    ax,008h
  101.     int    31h
  102.  
  103.     mov    cx,4092h
  104.     mov    ax,009h
  105.     int    31h
  106.     ret
  107.  
  108. immed_exit:
  109.     mov    ax,4cffh
  110.     int    21h
  111.  
  112. ; Called by pmode right before exit to free this memory
  113. exit_hook:
  114.     mov    ax,cs
  115.     sub    ax,10h            ; psp base
  116.     mov    es,ax
  117.     mov    ah,49h
  118.     int    21h
  119.     ret
  120.  
  121. start:                    ; execution starts here
  122.  
  123. ; Deallocate Environment
  124.     mov    es,es:[2ch]
  125.     mov    ah,49h
  126.     int    21h
  127. ; Call PMODE's info routine
  128.     push    _DATA
  129.     pop    ds
  130.     mov    ax, SW_FLAGS
  131.     call    pm_info
  132.     jc    short no_pmode
  133.     cmp    ch,3
  134.     jne    short ok1
  135. ; Fail - can't get to PM
  136. no_pmode:
  137.     mov    dx, offset msg_no_PM
  138.     mov    ah,9
  139.     int    21h
  140.     mov    ax,4c01h
  141.     int    21h
  142.  
  143. ; store PMODE returned values in our code space
  144. ok1:    mov    byte ptr ct+1,cl
  145.     mov    word ptr pn+1,bx
  146. ; Close all the handles (stdin, stdout, stderr, AUX, PRN)
  147.     mov    cx,4
  148. closem:
  149.     mov    ah,3eh
  150.     mov    bx,cx
  151.     int    21h
  152.     loop    closem
  153. ; Chain int 2f
  154.     mov    ax,352fh        ;Get int 2f
  155.     int    21h
  156.     mov    oldint2f+2,es
  157.     mov    oldint2f,bx
  158.     push    cs
  159.     pop    ds
  160.     mov    dx,offset _dpmiint2f
  161.     mov    ax,252fh
  162.     int    21h
  163.  
  164. ; Terminate and stay resident
  165.     push    _DATA
  166.     pop    ds
  167.     mov    pm_exit,offset exit_hook
  168.     mov    ax,3100h
  169.     mov    dx,ss            ;temp SS is at end
  170.     mov    bx,cs            ;CS is above PSP
  171.     sub    dx,bx
  172.     add    dx,10h            ;add for psp
  173.     int    21h
  174.  
  175.     ends
  176.  
  177.     SEGMENT _DATA PUBLIC USE16 'DATA'
  178.     EXTRN    pm_exit:word
  179. msg_no_PM    db "Can't load PMODE.",13,10,"$"
  180.     ends
  181.  
  182.     SEGMENT _BSS PUBLIC USE16 'BSS'
  183.     ends
  184.  
  185.     SEGMENT _STACK STACK 'STACK'
  186.     db 256 dup (?)
  187.     ENDS
  188.  
  189.     end    start
  190.