home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / cdfm.zip / PMODE.BAK < prev    next >
Text File  |  1994-09-09  |  68KB  |  2,027 lines

  1. ; PMODE v2.5 raw, DPMI, VCPI, & XMS compliant protected mode header.
  2. ; By Tran (a.k.a. Thomas Pytel).
  3.  
  4.         .386p
  5.  
  6. LOWMIN          = 0             ; minimum free low memory (in K)
  7. EXTMIN          = 0             ; minimum free extended memory (in K)
  8. SELECTORS       = 8             ; extra selectors for allocation
  9. STAKMAIN        = 100h          ; main execution stream stack size (in para)
  10. STAKRMODE       = 10h           ; real mode call stack size (in para)
  11. STAKPMODE       = 20h           ; protected mode call stack size (in para)
  12. MODENESTING     = 8             ; max number of nested mode switches
  13.  
  14. RMODENUM        = (MODENESTING+1) shr 1
  15. PMODENUM        = MODENESTING shr 1
  16. STAKSIZE        = STAKMAIN+(PMODENUM*STAKPMODE)+(RMODENUM*STAKRMODE)
  17.  
  18. .errnz STAKSIZE gt 0fffh        ; error if stack greater than 64k
  19.  
  20. code16  segment para public use16
  21. code16  ends
  22. code32  segment para public use32
  23. code32  ends
  24. codeend segment para stack use32 'stack'
  25. codeend ends
  26.  
  27. ;▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  28. ; Real mode and 16bit code
  29. ;▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  30. code16  segment para public use16
  31.         assume cs:code16, ds:code16
  32.         org 0
  33.  
  34. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  35. ; 16 bit common system data
  36. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  37. errmsg0         db      '386 or better not detected!!!',7,'$'
  38. errmsg1         db      'Not enough low memory!!!',7,'$'
  39. errmsg2         db      'System is already in V86 mode, and no VCPI or DPMI found!!!',7,'$'
  40. errmsg3         db      'Not enough extended memory!!!',7,'$'
  41. errmsg4         db      'Couldn''t enable A20 gate!!!',7,'$'
  42. errmsg5         db      'Extended memory allocation failure. (weird eh???)',7,'$'
  43.  
  44. nullint         db      0cfh            ; IRET instruction
  45. exitrout        dw      exit            ; exit routine, modified if XMS, VCPI
  46.  
  47. savedstakoff    dw      ?               ; current saved stack offset
  48. savedstakseg    dw      ?               ; current saved stack segment
  49.  
  50. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  51. ; 16 bit common system code
  52. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  53. ;═════════════════════════════════════════════════════════════════════════════
  54. intreal:                                ; real mode int, load FS and GS here
  55.         pushf
  56. ;-----------------------------------------------------------------------------
  57. callreal:                               ; real mode call, load FS and GS here
  58.         push cs
  59.         push offset icreald
  60.         mov fs,cs:v86r_fs
  61.         mov gs,cs:v86r_gs
  62.         mov eax,cs:v86r_eax
  63.         mov ecx,cs:v86r_ecx
  64.         mov edx,cs:v86r_edx
  65.         mov ebx,cs:v86r_ebx
  66.         mov esi,cs:v86r_esi
  67.         mov edi,cs:v86r_edi
  68.         mov ebp,cs:v86r_ebp
  69. ;-----------------------------------------------------------------------------
  70. icreal:                                 ; real mode int, call, or IRQ
  71.         db 66h,68h              ; PUSH destination addx
  72. icrealm1        dd      ?       ;
  73. icrealm0        db      ?       ; CLI or STI
  74.         retf
  75. ;-----------------------------------------------------------------------------
  76. icreald:                                ; done with real int or call
  77.         cli
  78.         pushf
  79.         pop cs:v86r_flags
  80.         mov cs:v86r_eax,eax
  81.         mov cs:v86r_ecx,ecx
  82.         mov cs:v86r_edx,edx
  83.         mov cs:v86r_ebx,ebx
  84.         mov cs:v86r_esi,esi
  85.         mov cs:v86r_edi,edi
  86.         mov cs:v86r_ebp,ebp
  87.         mov cs:v86r_ds,ds
  88.         mov cs:v86r_es,es
  89.         mov cs:v86r_fs,fs
  90.         mov cs:v86r_gs,gs
  91. icreald2:
  92.         mov ax,cs
  93.         mov ds,ax
  94. icrealm2        label word              ; return to pmode modifiable to JMP
  95. ;-----------------------------------------------------------------------------
  96.         mov ebx,ds:cp_savedstakoff      ; DPMI return to pmode
  97.         mov dx,ds:dp_savedstaksel
  98.         mov edi,offset dp_int3_d
  99.         mov si,ds:_selcode
  100.         mov cx,dx
  101.         mov ax,ds:_seldata
  102.         jmp ds:d_switchaddx
  103. ;-----------------------------------------------------------------------------
  104. VICREAL1D=(($-(icrealm2+2))shl 8)+0ebh
  105. v_icreal1d:                             ; VCPI return to pmode from safe
  106.         mov edi,offset cp_int3_d
  107. ;-----------------------------------------------------------------------------
  108. ; EDI=offset to jump to in code32
  109. v_switchtopmode:                        ; VCPI switch to pmode
  110.         mov ds:v_ss_dest,edi
  111.         mov esi,offset v_ss_cr3
  112.         add esi,ds:_code16a
  113.         mov ax,0de0ch
  114.         int 67h
  115. ;-----------------------------------------------------------------------------
  116. CICREAL1D=(($-(icrealm2+2))shl 8)+0ebh
  117. c_icreal1d:                             ; custom return to pmode from safe
  118.     mov edi,offset cp_int3_d
  119. ;-----------------------------------------------------------------------------
  120. ; EDI=offset in pmode to jump to
  121. c_retpmode:                             ; reenter 32bit pmode
  122.         lgdt fword ptr c_gdt32addx      ; set up pmode GDT and IDT
  123.     lidt fword ptr c_idt32addx
  124.     mov ds:gdt32task[5],89h     ; set task as not busy
  125.         mov eax,cr0                     ; switch to pmode
  126.         or al,1
  127.         mov cr0,eax
  128.         db 0eah
  129.         dw $+4,20h
  130.         mov ax,30h                      ; load task register
  131.         ltr ax
  132.     jmp c_gotopmode
  133. ;═════════════════════════════════════════════════════════════════════════════
  134. int32:                                  ; real mode INT32: EDX=off
  135.         pushad
  136.         push ds es fs gs
  137.         cli
  138.         mov ax,cs
  139.         mov ds,ax
  140.         mov ds:p_cpmodem0,edx
  141.         mov al,[esp+45]
  142.         shr al,1
  143.         and al,1
  144.         add al,0fah
  145.         mov ds:p_cpmodem1,al
  146.         push savedstakoff
  147.         push savedstakseg
  148.         movzx ebx,ds:nextmodestack
  149.         lea eax,[ebx-STAKPMODE*16]
  150.         mov ds:nextmodestack,ax
  151.         add ebx,ds:realstackbase
  152.         mov savedstakseg,ss
  153. int32m0         label   word            ; jump to pmode, modifiable
  154. ;-----------------------------------------------------------------------------
  155.         sub sp,ds:dp_savelen
  156.         mov savedstakoff,sp
  157.         mov ax,ss                       ; DPMI jump to pmode
  158.         mov es,ax
  159.         mov di,sp
  160.         xor al,al
  161.         call d_saveaddx
  162.         mov ax,ds:_seldata
  163.         mov cx,ax
  164.         mov dx,ax
  165.         mov edi,offset p_cpmode0
  166.         mov si,ds:_selcode
  167.         jmp ds:d_switchaddx
  168. ;-----------------------------------------------------------------------------
  169. VINT32=(($-(int32m0+2))shl 8)+0ebh
  170. v_int32:                                ; VCPI call pmode
  171.         push ds:p_cpmodem2
  172.         mov ds:p_cpmodem2,VCPMODED
  173.         mov savedstakoff,sp
  174.         mov edi,offset p_cpmode1
  175.         jmp v_switchtopmode
  176. ;-----------------------------------------------------------------------------
  177. if ($-(int32m0+2)) gt 127
  178.   err
  179. endif
  180. CINT32=(($-(int32m0+2))shl 8)+0ebh
  181. c_int32:                                ; raw/XMS call pmode
  182.         push ds:p_cpmodem2
  183.         mov ds:p_cpmodem2,CCPMODED
  184.         mov savedstakoff,sp
  185.         mov edi,offset p_cpmode1
  186.         jmp c_retpmode
  187. ;═════════════════════════════════════════════════════════════════════════════
  188. int32d0:                                ; DPMI done with pmode call
  189.         mov di,sp
  190.         mov al,1
  191.         call d_saveaddx
  192.         add sp,ds:dp_savelen
  193. ;-----------------------------------------------------------------------------
  194. int32d2:                                ; done from all
  195.         pop savedstakseg
  196.         pop savedstakoff
  197.         add ds:nextmodestack,STAKPMODE*16
  198.         mov bx,ds:v86r_flags
  199.         mov ax,[esp+44]
  200.         and ax,not 8d5h
  201.         and bx,8d5h
  202.         or ax,bx
  203.         mov [esp+44],ax
  204.         pop gs fs es ds
  205.         popad
  206.         iret
  207. ;═════════════════════════════════════════════════════════════════════════════
  208. int32d1:                                ; VCPI done with pmode call
  209.         mov ss,savedstakseg
  210.         pop ds:p_cpmodem2
  211.         jmp int32d2
  212. ;═════════════════════════════════════════════════════════════════════════════
  213. int32d3:                                ; raw/XMS done with pmode call
  214.         mov c_retreal0m0,offset c_sicreal
  215.         mov ax,cs
  216.         mov ds,ax
  217.         movzx esp,savedstakoff
  218.         mov ss,savedstakseg
  219.         pop ds:p_cpmodem2
  220.         jmp int32d2
  221. ;─────────────────────────────────────────────────────────────────────────────
  222. chek_VCPI:                              ; Chek for VCPI
  223.         xor ax,ax
  224.         mov gs,ax
  225.         mov ax,gs:[67h*4]
  226.         or ax,gs:[(67h*4)+2]
  227.         jz short chekVCPIa
  228.         mov ax,0de00h
  229.         int 67h
  230.         or ah,ah
  231.         clc
  232.         jz short chekVCPId
  233. chekVCPIa:
  234.         stc
  235. chekVCPId:
  236.         ret
  237. ;─────────────────────────────────────────────────────────────────────────────
  238. chek_processor:                         ; Detect if current processor 386
  239.         pushf
  240.         xor ah,ah
  241.         push ax
  242.         popf
  243.         pushf
  244.         pop ax
  245.         and ah,0f0h
  246.         cmp ah,0f0h
  247.         je short detectno386
  248.         mov ah,0f0h
  249.         push ax
  250.         popf
  251.         pushf
  252.         pop ax
  253.         and ah,0f0h
  254.         jz short detectno386
  255.         popf
  256.         ret
  257. detectno386:
  258.         mov dx,offset errmsg0
  259.         jmp short exit16err
  260. ;─────────────────────────────────────────────────────────────────────────────
  261. chek_V86:                               ; Chek if already in V86 mode
  262.         smsw ax
  263.         test al,1
  264.         mov dx,offset errmsg2
  265.         jnz short exit16err
  266.         ret
  267. ;─────────────────────────────────────────────────────────────────────────────
  268. pregetlomem:                            ; Get low memory or abort
  269.         add eax,ds:_lomembase
  270.         mov ebx,ds:_lomemtop
  271.         cmp eax,ebx
  272.         ja short pregetlomema
  273.         mov ecx,eax
  274.         xchg eax,ds:_lomembase
  275.         sub ebx,ecx
  276.         cmp ebx,LOWMIN*1024
  277.         jb short pregetlomema
  278.         ret
  279. pregetlomema:
  280.         mov dx,offset errmsg1
  281. ;─────────────────────────────────────────────────────────────────────────────
  282. exit16err:                              ; Exit program with message
  283.         mov ah,9
  284.         int 21h
  285.         jmp exitrout
  286. ;-----------------------------------------------------------------------------
  287. exit:                                   ; Guess what???
  288.         mov ah,4ch
  289.         mov al,ds:_exitcode
  290.         int 21h
  291. ;═════════════════════════════════════════════════════════════════════════════
  292. start16:                                ; Program begins here
  293.         cli
  294.         cld
  295.         push cs
  296.         pop ds
  297.  
  298.         call chek_processor             ; is it at least a 386¿
  299.  
  300.         mov ax,es                       ; set up a bunch of pointers
  301.         movzx eax,ax
  302.         shl eax,4
  303.         mov ds:_pspa,eax
  304.         mov eax,code16
  305.         shl eax,4
  306.         mov ds:_code16a,eax
  307.         or dword ptr ds:gdt32code16[2],eax
  308.         or dword ptr ds:gdt32data16[2],eax
  309.         mov ebx,code32
  310.         shl ebx,4
  311.         mov ds:_code32a,ebx
  312.         or dword ptr ds:gdt32code32[2],ebx
  313.         or dword ptr ds:gdt32data32[2],ebx
  314.         add dword ptr ds:c_gdt32addx[2],ebx
  315.         mov eax,codeend
  316.         shl eax,4
  317.         sub eax,ebx
  318.         mov ds:_lomembase,eax
  319.         mov ds:realstackbase,eax
  320.         movzx eax,word ptr es:[2]
  321.         shl eax,4
  322.         sub eax,ebx
  323.         mov ds:_lomemtop,eax
  324.  
  325.         mov eax,STAKSIZE*16             ; get stack memory
  326.         call pregetlomem
  327.  
  328.         push es                         ; save PSP seg (DPMI chek kills ES)
  329.         pop fs
  330.  
  331.         mov ax,1687h                    ; chek for DPMI
  332.         int 2fh
  333.         or ax,ax
  334.         jz d_start
  335.  
  336.         call chek_VCPI                  ; chek for VCPI
  337.         jnc v_start
  338.  
  339.         call chek_V86                   ; chek for V86 mode
  340.  
  341.         mov ax,4300h                    ; chek for XMS
  342.         int 2fh
  343.         cmp al,80h
  344.         je x_start
  345.  
  346.         jmp c_start                     ; custom system start
  347. ;─────────────────────────────────────────────────────────────────────────────
  348. enableA20:                              ; hardware enable gate A20
  349.         xor ax,ax
  350.         mov fs,ax
  351.         dec ax
  352.         mov gs,ax
  353.         call testA20
  354.         je short enableA20done
  355.         in al,92h                       ; PS/2 A20 enable
  356.         or al,2
  357.         jmp short $+2
  358.         jmp short $+2
  359.         jmp short $+2
  360.         out 92h,al
  361.         call testA20
  362.         je short enableA20done
  363.         call enableA20o1                ; AT A20 enable
  364.         jnz short enableA20wait
  365.         mov al,0d1h
  366.         out 64h,al
  367.         call enableA20o1
  368.         jnz short enableA20wait
  369.         mov al,0dfh
  370.         out 60h,al
  371.         push offset enableA20wait
  372. enableA20o1:
  373.         mov ecx,20000h
  374. enableA20o1l:
  375.         jmp short $+2
  376.         jmp short $+2
  377.         jmp short $+2
  378.         in al,64h
  379.         test al,2
  380.         loopnz enableA20o1l
  381. enableA20done:
  382.         ret
  383. ;-----------------------------------------------------------------------------
  384. enableA20wait:                          ; wait for A20
  385.         mov al,36h
  386.         out 43h,al
  387.         xor al,al
  388.         out 40h,al
  389.         out 40h,al
  390.         mov cx,800h
  391. enableA20waitl0:
  392.         call testA20
  393.         je enableA20done
  394.         in al,40h
  395.         in al,40h
  396.         mov ah,al
  397. enableA20waitl1:
  398.         in al,40h
  399.         in al,40h
  400.         cmp al,ah
  401.         je enableA20waitl1
  402.         loop enableA20waitl0
  403.         mov dx,offset errmsg4
  404.         jmp exit16err
  405. ;-----------------------------------------------------------------------------
  406. testA20:                                ; Test for enabled A20
  407.         mov al,fs:[0]
  408.         mov ah,al
  409.         not al
  410.         mov gs:[10h],al
  411.         cmp ah,fs:[0]
  412.         mov fs:[0],ah
  413.         ret
  414. ;─────────────────────────────────────────────────────────────────────────────
  415. ; BL=low PIC val, BH=high PIC val
  416. setintslots:                            ; set int nums in table to PIC vals
  417.         mov edi,offset ds:intslottbl
  418.         mov cl,8
  419. setintslotsl0:
  420.         mov [di],bl
  421.         inc di
  422.         inc bl
  423.         dec cl
  424.         jnz setintslotsl0
  425.         mov cl,8
  426. setintslotsl1:
  427.         mov [di],bh
  428.         inc di
  429.         inc bh
  430.         dec cl
  431.         jnz setintslotsl1
  432.         ret
  433.  
  434. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  435. ; 16 bit DPMI system data
  436. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  437. d_errmsg0       db      'DPMI host is not 32bit!!!',7,'$'
  438. d_errmsg1       db      'Ran out of DPMI descriptors!!!',7,'$'
  439. d_errmsg2       db      'Couldn''t set DPMI descriptors as needed!!!',7,'$'
  440. d_errmsg3       db      'Couldn''t enter 32bit protected mode!!!',7,'$'
  441.  
  442. d_enterpmode    dw      ?,?             ; DPMI switch to pmode addx
  443. d_pspsel        dw      ?               ; stupid PSP selector
  444. d_oldenvsegsel  dw      ?               ; stupid selector we dont want
  445.  
  446. d_switchaddx    dd      ?               ; switch to pmode addx
  447. d_saveaddx      dd      ?               ; save/restore state addx
  448.  
  449. d_nintoff       dd      offset dp_irq0,offset dp_irq1,offset dp_irq2,offset dp_irq3
  450.                 dd      offset dp_irq4,offset dp_irq5,offset dp_irq6,offset dp_irq7
  451.                 dd      offset dp_irq8,offset dp_irq9,offset dp_irqa,offset dp_irqb
  452.                 dd      offset dp_irqc,offset dp_irqd,offset dp_irqe,offset dp_irqf
  453.                 dd      offset dp_int33,offset dp_int32
  454. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  455. ; 16 bit DPMI system code
  456. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  457. ;═════════════════════════════════════════════════════════════════════════════
  458. d_retreal:                              ; Return to real mode
  459.         mov ax,205h                     ; restore all int vectors needed
  460.         mov edi,17
  461. d_retreall0:
  462.         mov bl,ds:intslottbl[edi]
  463.         lea ebp,[edi*2+edi]
  464.         mov edx,dword ptr ds:dp_ointbuf[ebp*2]
  465.         mov cx,word ptr ds:dp_ointbuf[ebp*2+4]
  466.         int 31h
  467.         sub di,1
  468.         jnc d_retreall0
  469.         jmp short d_exit
  470. ;─────────────────────────────────────────────────────────────────────────────
  471. d_exit16err:                            ; DPMI Exit with error message
  472.         mov ds:v86r_ds,code16
  473.         mov ds:v86r_ah,9
  474.         mov ax,300h
  475.         mov bx,21h
  476.         xor cx,cx
  477.         mov edi,offset ds:v86r_edi
  478.         push ds
  479.         pop es
  480.         int 31h
  481. ;─────────────────────────────────────────────────────────────────────────────
  482. d_exit:                                 ; DPMI exit to real mode
  483.         mov es,d_pspsel                 ; restore env selector
  484.         mov ax,d_oldenvsegsel
  485.         mov es:[2ch],ax
  486.         jmp exit
  487. ;═════════════════════════════════════════════════════════════════════════════
  488. d_start:                                ; Start in a crappy DPMI system
  489.         or ds:_sysbyte0,3               ; set system type DPMI byte
  490.  
  491.         test bl,1                       ; must be 32bit DPMI
  492.         mov dx,offset d_errmsg0
  493.         jz exit16err
  494.  
  495.         mov d_enterpmode[0],di          ; store enter addx
  496.         mov d_enterpmode[2],es
  497.         push word ptr fs:[2ch]          ; preserve old env seg
  498.  
  499.         movzx eax,si                    ; get mem for DPMI blok
  500.         shl eax,4
  501.         call pregetlomem
  502.         shr eax,4
  503.         add ax,code32
  504.         mov es,ax
  505.  
  506.         mov ax,1                        ; switch to pmode
  507.         call dword ptr d_enterpmode
  508.         cli                             ; I don't trust DPMI
  509.         mov dx,offset d_errmsg3
  510.         jc exit16err
  511.         mov ds:v86r_dx,offset d_errmsg1 ; prepare for abort maybe
  512.         pop ax                          ; swap old env seg with selector
  513.         xchg ax,es:[2ch]
  514.         mov d_oldenvsegsel,ax
  515.         mov d_pspsel,es                 ; store stupid selectors
  516.         mov ds:data16sel,ds
  517.         mov ds:code16sel,cs
  518.         mov ds:code16off,offset d_retreal       ; set return to real mode addx
  519.         mov ds:_setirqvect,offset dp_setirqvect ; modify some crap
  520.         mov ds:_getirqvect,offset dp_getirqvect
  521.         mov ds:_setselector,offset dp_setselector
  522.  
  523.         push ds                         ; no more need for PSP
  524.         pop es
  525.         mov ax,3                        ; get selector increment value
  526.         int 31h
  527.         mov bx,ax
  528.         xor ax,ax                       ; get needed selectors
  529.         mov cx,3+SELECTORS
  530.         int 31h
  531.         jc d_exit16err
  532.  
  533.         mov si,ax                       ; set up descriptors
  534.         mov ds:_selcode,ax
  535.         lea ecx,[eax+ebx]
  536.         mov ds:_seldata,cx
  537.         lea ebp,[ecx+ebx]
  538.         mov ds:_selzero,bp
  539.         lea eax,[ebp+ebx]
  540. if SELECTORS ne 0
  541.         mov ds:selectorbase,ax
  542.         mov ds:selectorinc,bx
  543. endif
  544.         mov ds:v86r_dx,offset d_errmsg2
  545.         mov ax,0ch                      ; set descriptors from GDT
  546.         mov bx,si
  547.         mov edi,offset ds:gdt32code32
  548.         or byte ptr [edi+5],60h
  549.         int 31h
  550.         jc d_exit16err
  551.         mov bx,cx
  552.         mov edi,offset ds:gdt32data32
  553.         or byte ptr [edi+5],60h
  554.         int 31h
  555.         jc d_exit16err
  556.         mov bx,bp
  557.         mov edi,offset ds:gdt32zero32
  558.         or byte ptr [edi+5],60h
  559.         int 31h
  560.         jc d_exit16err
  561. if SELECTORS ne 0
  562.         mov bx,ds:selectorbase          ; set up extra allocatable selectors
  563.         mov dx,SELECTORS
  564. d_startl1:
  565.         int 31h
  566.         jc d_exit16err
  567.         add bx,ds:selectorinc
  568.         dec dx
  569.         jnz d_startl1
  570. endif
  571.         mov es,cx                       ; ES, FS, and GS what they should be
  572.         mov fs,cx
  573.         mov gs,bp
  574.  
  575.         mov edi,ds:_lomembase           ; chek and get extended memory
  576.         mov eax,ds:_lomemtop
  577.         sub eax,edi
  578.         cmp eax,48
  579.         mov ds:v86r_dx,offset errmsg1
  580.         jb d_exit16err
  581.         mov ax,500h
  582.         int 31h
  583.         mov edx,es:[edi]
  584.         mov ds:v86r_dx,offset errmsg3
  585.         jmp short d_startf0
  586. d_startl2:
  587.         sub edx,4096
  588.         jnc short d_startf0
  589.         xor edx,edx
  590. d_startf0:
  591.         mov eax,edx
  592.         shr eax,10
  593.         cmp eax,EXTMIN
  594.         jb d_exit16err
  595.         or edx,edx
  596.         jz short d_startf1
  597.         mov cx,dx
  598.         shld ebx,edx,16
  599.         mov ax,501h
  600.         int 31h
  601.         jc d_startl2
  602.         shl ebx,16
  603.         mov bx,cx
  604.         sub ebx,ds:_code32a
  605.         mov ds:_himembase,ebx
  606.         add ebx,edx
  607.         mov ds:_himemtop,ebx
  608. d_startf1:
  609.  
  610.         mov ax,305h                     ; get save/restore state addxs
  611.         int 31h
  612.         mov ds:dp_savelen,ax
  613.         mov dword ptr ds:dp_saveaddx[0],edi
  614.         mov word ptr ds:dp_saveaddx[4],si
  615.         mov word ptr d_saveaddx[0],cx
  616.         mov word ptr d_saveaddx[2],bx
  617.         mov ax,306h                     ; get switch mode addxs
  618.         int 31h
  619.         mov dword ptr ds:dp_switchaddx[0],edi
  620.         mov word ptr ds:dp_switchaddx[4],si
  621.         mov word ptr d_switchaddx[0],cx
  622.         mov word ptr d_switchaddx[2],bx
  623.  
  624.         mov ax,400h                     ; set IRQ handlers to PIC values
  625.         int 31h
  626.         xchg dl,dh
  627.         mov bx,dx
  628.         call setintslots
  629.  
  630.         mov ah,2                        ; backup and set all int vectors
  631.         mov si,ds:_selcode
  632.         mov edi,17
  633. d_startl0:
  634.         mov bl,ds:intslottbl[edi]
  635.         mov al,4
  636.         int 31h
  637.         lea ebp,[edi*2+edi]
  638.         mov dword ptr ds:dp_ointbuf[ebp*2],edx
  639.         mov word ptr ds:dp_ointbuf[ebp*2+4],cx
  640.         mov al,5
  641.         mov edx,d_nintoff[edi*4]
  642.         mov cx,si
  643.         int 31h
  644.         sub di,1
  645.         jnc d_startl0
  646.  
  647.         mov ax,es                       ; set up needed regs & go on to 32bit
  648.         mov ss,ax
  649.         add esp,ds:realstackbase
  650.         mov ds,ax
  651.         push dword ptr cs:_selcode
  652.         push offset p_start
  653.         db 66h,0cbh             ; 32bit RETF
  654.  
  655. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  656. ; 16bit VCPI system data
  657. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  658. v_errmsg0       db      'Incompatible VCPI PIC mappings!!!',7,'$'
  659.  
  660. v_pagedirseg    dw      ?               ; seg of page directory
  661. v_pagebase      dw      0               ; first page of himem (*4)+1000h
  662. v_pagetop       dw      0               ; top page of himem (*4)+1000h
  663.  
  664. v_ss_cr3        dd      ?               ; new CR3 for pmode (physical)
  665. v_ss_gdtaddxptr dw      c_gdt32addx,0   ; ptr to GDT data for pmode
  666. v_ss_idtaddxptr dw      c_idt32addx,0   ; ptr to IDT data for pmode
  667. v_ss_ldtsel     dw      0               ; don't need no stinkin LDTs
  668. v_ss_trsel      dw      30h             ; task state segment selector
  669. v_ss_dest       dd      ?               ; start in pmode EIP
  670.                 dw      20h             ; start in pmode CS
  671. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  672. ; 16bit VCPI system code
  673. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  674. ;═════════════════════════════════════════════════════════════════════════════
  675. v_retreal:                              ; VCPI return to real mode
  676.         movzx edi,exitrout
  677.         mov esi,esp
  678.         sub esi,ds:realstackbase
  679.         mov cx,code16
  680. ;═════════════════════════════════════════════════════════════════════════════
  681. ; EDI=offset to jump to, ESI=real mode stack ptr, CX=real mode DS
  682. v_retreal0:                             ; VCPI go to real mode
  683.         sub esp,8
  684.         push ecx
  685.         push dword ptr ds:v86r_es
  686.         dw 06866h,codeend,0     ; PUSH dword codeend
  687.         push esi
  688.         pushfd
  689.         dw 06866h,code16,0      ; PUSH dword code16
  690.         push edi
  691.         mov ax,gs
  692.         mov ds,ax
  693.         mov ax,0de0ch
  694.         call cs:vp_vcpipmentry
  695. ;─────────────────────────────────────────────────────────────────────────────
  696. v_exit:                                 ; VCPI exit (clean up pages)
  697.         mov es,v_pagedirseg
  698.         mov si,v_pagebase
  699.         mov cx,v_pagetop
  700.         sub cx,si
  701.         jbe short v_exitf0
  702. v_exitl0:
  703.         mov edx,es:[si]
  704.         and dx,0f000h
  705.         mov ax,0de05h
  706.         int 67h
  707.         add si,4
  708.         sub cx,4
  709.         jnz v_exitl0
  710. v_exitf0:
  711.         jmp exit
  712. ;─────────────────────────────────────────────────────────────────────────────
  713. v_exiterr1:                             ; VCPI not enough low mem exit
  714.         mov dx,offset errmsg1
  715.         jmp exit16err
  716. ;═════════════════════════════════════════════════════════════════════════════
  717. v_start:                                ; start continues from VCPI
  718.         or ds:_sysbyte0,2               ; set system type VCPI byte
  719.         mov ds:code16off,offset v_retreal       ; VCPI return to real mode
  720.         mov c_idt32handler[40h],offset vp_int33 ; VCPI safe int handlers
  721.         mov c_idt32handler[44h],offset vp_int32
  722.         mov ds:cp_v86irqintr,offset vp_int33f0  ; VCPI IRQ safe int routine
  723.         mov int32m0,VINT32              ; VCPI real INT32
  724.  
  725.         mov ax,0de0ah                   ; get PIC mappings
  726.         int 67h
  727.         mov bh,cl
  728.         mov dx,offset v_errmsg0         ; chek for compatible PIC mapping
  729.         cmp bl,bh
  730.         je exit16err
  731.         cmp bl,30h
  732.         je exit16err
  733.         cmp bh,30h
  734.         je exit16err
  735.         mov ax,70h                      ; compatible, get highest needed num
  736.         cmp al,bl
  737.         ja short v_startf1
  738.         mov al,bl
  739. v_startf1:
  740.         cmp al,bh
  741.         ja short v_startf2
  742.         mov al,bh
  743. v_startf2:
  744.         add al,7
  745.         mov c_numofintvects,al
  746.         lea eax,[eax*8+7]               ; set limit of IDT
  747.         mov c_idt32addx,ax
  748.         call setintslots                ; set int slots needed
  749.         movzx eax,ax
  750.         add eax,2068h+1
  751.         call pregetlomem                ; allocate TSS, IO bitmap, and IDT
  752.         mov ds:cp_tssesp0ptr,eax
  753.         mov eax,ds:_code16a             ; adjust mode switch structure
  754.         add dword ptr v_ss_gdtaddxptr,eax
  755.         add dword ptr v_ss_idtaddxptr,eax
  756.  
  757.         mov exitrout,offset v_exit      ; set VCPI cleanup exit
  758.  
  759.         mov eax,ds:_lomembase           ; align lomem base on a page
  760.         mov ebx,ds:_code32a
  761.         add ebx,eax
  762.         lea ecx,[ebx+0fffh]
  763.         and ecx,0fffff000h
  764.         sub ebx,ecx
  765.         sub eax,ebx
  766.         mov ds:_lomembase,eax
  767.         mov ebp,ds:_lomemtop            ; get available low memory
  768.         sub ebp,eax
  769.         sub ebp,LOWMIN*1024             ; die if not enough
  770.         jc v_exiterr1
  771.         cmp ebp,8192
  772.         jb v_exiterr1
  773.  
  774.         shld eax,ecx,28                 ; get segment and clear all pages
  775.         mov v_pagedirseg,ax
  776.         mov es,ax
  777.         xor di,di
  778.         mov cx,2048
  779.         xor eax,eax
  780.         rep stos dword ptr es:[di]
  781.         mov di,1000h                    ; get VCPI pmode interface
  782.         mov esi,offset ds:gdt32vcpi
  783.         mov ax,0de01h
  784.         int 67h
  785.         mov dword ptr ds:vp_vcpipmentry,ebx
  786.  
  787.         mov v_pagebase,di               ; set up and go through allocation
  788.         mov v_pagetop,di
  789.         movzx eax,di
  790.         sub eax,1000h
  791.         shl eax,10
  792.         sub eax,ds:_code32a
  793.         mov ds:_himembase,eax
  794.         mov ebx,8192
  795. v_startl2:
  796.         mov ax,0de04h
  797.         int 67h
  798.         or ah,ah
  799.         jnz short v_startl2d
  800.         test di,0fffh
  801.         jnz short v_startf4
  802.         add ebx,4096
  803.         cmp ebx,ebp
  804.         jbe short v_startf4
  805.         mov v_pagetop,di
  806.         mov ax,0de05h
  807.         int 67h
  808.         jmp v_exiterr1
  809. v_startf4:
  810.         and dx,0f000h
  811.         or dl,7
  812.         mov es:[di],edx
  813.         add di,4
  814.         jnc v_startl2
  815. v_startl2d:
  816.         mov v_pagetop,di
  817.         lea si,[di-1000h]
  818.         movzx eax,si
  819.         shl eax,10
  820.         sub eax,ds:_code32a
  821.         mov ds:_himemtop,eax
  822.         sub di,v_pagebase
  823.         cmp di,EXTMIN
  824.         mov dx,offset errmsg3
  825.         jb exit16err
  826.         add ds:_lomembase,ebx
  827.  
  828.         movzx ebx,v_pagedirseg          ; set up physical addresses
  829.         shr ebx,8
  830.         mov eax,es:[ebx*4+1000h]
  831.         mov v_ss_cr3,eax
  832.         xor di,di
  833. v_startl3:
  834.         inc ebx
  835.         mov eax,es:[ebx*4+1000h]
  836.         and ax,0f000h
  837.         or al,7
  838.         stos dword ptr es:[di]
  839.         sub si,1000h
  840.         ja v_startl3
  841.  
  842.         mov edi,offset c_startf1        ; offset to jump to in pmode
  843.         mov ebx,ds:cp_tssesp0ptr
  844.         jmp v_switchtopmode             ; duh?
  845.  
  846. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  847. ; 16 bit XMS system data
  848. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  849. x_calladdx      dd      ?               ; XMS driver addx
  850. x_handle        dw      0fedch          ; XMS handle of extended memory
  851. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  852. ; 16 bit XMS system code
  853. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  854. ;─────────────────────────────────────────────────────────────────────────────
  855. x_exit:                                 ; XMS exit (clean up allocation)
  856.         mov ax,cs
  857.         mov ds,ax
  858.         mov dx,x_handle
  859.         mov ah,0dh
  860.         call x_calladdx
  861.         mov ah,0ah
  862.         call x_calladdx
  863.         jmp exit
  864. ;─────────────────────────────────────────────────────────────────────────────
  865. x_exiterr5:                             ; exit with error message 5
  866.         mov dx,offset errmsg5
  867.         jmp exit16err
  868. ;═════════════════════════════════════════════════════════════════════════════
  869. x_start:                                ; start in an XMS system
  870.         or ds:_sysbyte0,1               ; set system type XMS byte
  871.  
  872.         mov ax,4310h                    ; get XMS driver addx
  873.         int 2fh
  874.         mov word ptr x_calladdx[0],bx
  875.         mov word ptr x_calladdx[2],es
  876.  
  877.         mov ah,3                        ; XMS enable A20
  878.         call x_calladdx
  879.         or ax,ax
  880.         mov dx,offset errmsg4
  881.         jz exit16err
  882.  
  883.         mov ah,8                        ; chek and get extended memory
  884.         call x_calladdx
  885.         sub ax,64
  886.         jnc short x_startf0
  887.         xor ax,ax
  888. x_startf0:
  889.         cmp ax,EXTMIN
  890.         mov dx,offset errmsg3
  891.         jb exit16err
  892.         mov dx,ax
  893.         movzx ecx,ax
  894.         shl ecx,10
  895.         mov ah,9
  896.         call x_calladdx
  897.         or ax,ax
  898.         jz x_exiterr5
  899.         mov x_handle,dx
  900.         mov exitrout,offset x_exit
  901.         mov ah,0ch
  902.         call x_calladdx
  903.         or ax,ax
  904.         jz x_exiterr5
  905.         shrd eax,edx,16
  906.         mov ax,bx
  907.         sub eax,ds:_code32a
  908.         mov ds:_himembase,eax
  909.         add eax,ecx
  910.         mov ds:_himemtop,eax
  911.  
  912.         jmp c_startf0                   ; go on to custom start
  913.  
  914. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  915. ; 16 bit custom system data
  916. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  917. c_idt16addx     dw      3ffh, 0,0       ; default real mode IDT addx&limit
  918. c_idt32addx     dw      3bfh, ?,?       ; 32bit IDT addx&limit
  919. c_gdt32addx     dw      04fh+SELECTORS*8; 32bit GDT addx&limit
  920.                 dd      offset gdt32    ;
  921.  
  922. c_numofintvects db      77h             ; number of int vects needed -1
  923. c_idt32handler  dd      offset cp_irq0,offset cp_irq1,offset cp_irq2,offset cp_irq3
  924.                 dd      offset cp_irq4,offset cp_irq5
  925.                 dd      offset cp_irq6,offset cp_irq7,offset cp_irq8,offset cp_irq9
  926.                 dd      offset cp_irqa,offset cp_irqb
  927.                 dd      offset cp_irqc,offset cp_irqd,offset cp_irqe,offset cp_irqf
  928.                 dd      offset cp_int33,offset cp_int32,offset cp_int31
  929.                 dd      offset cp_exc0,offset cp_exc1,offset cp_exc2,offset cp_exc3
  930.                 dd      offset cp_exc4,offset cp_exc5
  931.                 dd      offset cp_exc6,offset cp_exc7
  932. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  933. ; 16 bit custom system code
  934. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  935. ;═════════════════════════════════════════════════════════════════════════════
  936. c_irqreal:                              ; real mode IRQ
  937.         pushf
  938.         push cs
  939.         push offset icreald2
  940.         jmp icreal
  941. ;-----------------------------------------------------------------------------
  942. c_retreal1:                             ; load some real mode stuff & exit
  943.         mov ax,codeend
  944.         mov ss,ax
  945.         mov esp,STAKSIZE*10h
  946.         jmp exitrout
  947. ;═════════════════════════════════════════════════════════════════════════════
  948. c_retreal0:                             ; load real mode IDT and set PE=0
  949.         mov ax,28h
  950.         mov ds,ax
  951.         mov es,ax
  952.         mov fs,ax
  953.         mov gs,ax
  954.         mov ss,ax
  955.         lidt fword ptr c_idt16addx
  956.         mov eax,cr0
  957.         and al,0feh
  958.         mov cr0,eax
  959.         db 0eah                 ; JMP FAR PTR c_retreal0m0
  960. c_retreal0m0    dw c_sicreal,code16
  961. ;-----------------------------------------------------------------------------
  962. c_sicreal:                              ; safe real mode int or call
  963.         mov ax,codeend
  964.         mov ss,ax
  965.         mov ds,cs:v86r_ds
  966.         mov es,cs:v86r_es
  967.         db 0eah                 ; JMP FAR PTR c_sicrealm0
  968. c_sicrealm0     dw      ?,code16;
  969. ;═════════════════════════════════════════════════════════════════════════════
  970. c_retreal:                              ; return to real mode
  971.         mov c_retreal0m0,offset c_retreal1
  972.         mov esp,STAKSIZE*10h
  973.         jmp c_retreal0
  974. ;═════════════════════════════════════════════════════════════════════════════
  975. c_start:                                ; custom only start
  976.         call enableA20                  ; enable that stupid A20 thingy
  977.  
  978.         mov ah,88h                      ; chek and get extended mem
  979.         int 15h
  980.         cmp ax,EXTMIN
  981.         mov dx,offset errmsg3
  982.         jb exit16err
  983.         movzx eax,ax
  984.         shl eax,10
  985.         mov ebx,100000h
  986.         sub ebx,ds:_code32a
  987.         mov ds:_himembase,ebx
  988.         add eax,ebx
  989.         mov ds:_himemtop,eax
  990. ;═════════════════════════════════════════════════════════════════════════════
  991. c_startf0:                              ; start continues from custom or XMS
  992.         mov int32m0,CINT32              ; raw/XMS real INT32
  993.         mov eax,2428h                   ; allocate TSS, IO bitmap, and IDT
  994.         call pregetlomem
  995.         mov ebx,eax
  996.  
  997.         lgdt fword ptr c_gdt32addx      ; switch to pmode
  998.         mov eax,cr0
  999.         or al,1
  1000.         mov cr0,eax
  1001.         db 0eah
  1002.         dw $+4,20h
  1003. ;-----------------------------------------------------------------------------
  1004. ; EBX->TSS
  1005. c_startf1:                              ; in 16bit pmode
  1006.         mov ax,28h                      ; set up segregs
  1007.         mov ds,ax
  1008.         mov al,18h
  1009.         mov gs,ax
  1010.         mov al,10h
  1011.         mov es,ax
  1012.         mov fs,ax
  1013.         mov ss,ax
  1014.         mov esp,STAKSIZE*16
  1015.         add esp,ds:realstackbase
  1016.  
  1017.         mov word ptr v_ss_dest[4],8     ; VCPI enter 32bit pmode from now on
  1018.         lea eax,[ebx+4]                 ; addx of ESP0 in TSS
  1019.         mov ds:cp_tssesp0ptr,eax
  1020.         mov ebp,ds:_code32a             ; TSS location in mem to GDT
  1021.         lea eax,[ebx+ebp]
  1022.         mov ecx,offset ds:gdt32task     ; set up task
  1023.         or dword ptr ds:[ecx+2],eax
  1024.         mov byte ptr ds:[ecx+5],89h
  1025.         mov cx,30h
  1026.         ltr cx
  1027.         add eax,2068h                   ; set up IDT
  1028.         mov ecx,offset c_idt32addx
  1029.         mov dword ptr [ecx+2],eax
  1030.         lidt fword ptr [ecx]
  1031.  
  1032.         mov dword ptr es:[ebx+8],10h    ; set up TSS stuff (EBX->TSS)
  1033.         mov edi,104
  1034.         mov es:[ebx+102],di
  1035.         mov word ptr es:[ebx+100],0
  1036.         add edi,ebx                     ; fill IO bitmap with 0
  1037.         xor eax,eax
  1038.         mov ecx,800h
  1039.         rep stos dword ptr es:[edi]
  1040.  
  1041.         mov ds:cp_idt32ptr,edi          ; set up blank IDT entries
  1042.         movzx esi,c_numofintvects
  1043. c_startl0:
  1044.         mov dword ptr es:[edi+esi*8],80000h+offset cp_excf
  1045.         mov dword ptr es:[edi+esi*8+4],8e00h
  1046.         sub si,1
  1047.         jnc c_startl0
  1048.         mov si,1ah                      ; necessary IDT entries
  1049. c_startl1:
  1050.         movzx ebp,ds:intslottbl[esi]
  1051.         mov eax,c_idt32handler[esi*4]
  1052. c_startl1c:
  1053.         mov es:[edi+ebp*8],ax
  1054.         sub si,1
  1055.         jnc c_startl1
  1056.  
  1057.         mov edi,offset p_start          ; set up regs & go on to 32bit
  1058.     mov ax,10h
  1059.         mov ds,ax
  1060. ;-----------------------------------------------------------------------------
  1061. c_gotopmode:                ; jump to 32bit pmode
  1062.         pushfd                          ; set eflags: NT=0, IOPL=3
  1063.         pop eax
  1064.         and ah,0bfh
  1065.         or ah,30h
  1066.         push eax
  1067.         popfd
  1068.         dw 6866h,8,0            ; PUSH dword 8
  1069.     push edi
  1070.         db 66h,0cbh             ; 32bit RETF
  1071.  
  1072. code16  ends
  1073.  
  1074. ;▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  1075. ; 32bit pmode code
  1076. ;▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  1077. code32  segment para public use32
  1078.         assume cs:code32, ds:code32
  1079.         org 0
  1080.  
  1081. extrn   _main:near
  1082.  
  1083. public  _exit, _ret, _getmem, _getlomem, _gethimem, _lomemsize, _himemsize
  1084. public  _getirqmask, _setirqmask, _getselector, _freeselector, _rmpmirqset
  1085. public  _rmpmirqfree
  1086.  
  1087. public  v86r_eax, v86r_ebx, v86r_ecx, v86r_edx, v86r_esi, v86r_edi, v86r_ebp
  1088. public  v86r_ax, v86r_bx, v86r_cx, v86r_dx, v86r_si, v86r_di, v86r_bp
  1089. public  v86r_al, v86r_ah, v86r_bl, v86r_bh, v86r_cl, v86r_ch, v86r_dl, v86r_dh
  1090. public  v86r_ds, v86r_es, v86r_fs, v86r_gs
  1091. public  _selcode, _seldata, _selzero, _lomembase, _lomemtop, _himembase
  1092. public  _himemtop, _pspa, _code16a, _code32a, _getirqvect, _setirqvect
  1093. public  _sysbyte0, _setselector, _exitcode
  1094. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1095. ; 32 bit common system data
  1096. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1097. _lomembase      dd      ?               ; low mem base for allocation
  1098. _lomemtop       dd      ?               ; top of low mem
  1099. _himembase      dd      0               ; high mem base for allocation
  1100. _himemtop       dd      0               ; top of high mem
  1101. _pspa           dd      ?               ; offset of start of PSP from 0
  1102. _code16a        dd      ?               ; offset of start of 16bit code from 0
  1103. _code32a        dd      ?               ; offset of start of 32bit code from 0
  1104. _selcode        dw      8               ; code segment selector
  1105. _seldata        dw      10h             ; data segment alias for code
  1106. _selzero        dw      18h             ; data segment starting at 0:0
  1107. _sysbyte0       db      0               ; system bits:
  1108.                                         ;  0-1: 0=raw, 1=XMS, 2=VCPI, 3=DPMI
  1109. _exitcode       db      0               ; exit code for int21h ah=4ch
  1110.  
  1111. align 4
  1112. _getirqvect     dd      cp_getirqvect   ; get IRQ handler offset routine addx
  1113. _setirqvect     dd      cp_setirqvect   ; set IRQ handler offset routine addx
  1114. _setselector    dd      cp_setselector  ; set a selector addx offset addx
  1115.  
  1116. gdt32           dq      0
  1117. gdt32code32     db      0ffh,0ffh,0,0,0,9ah,0cfh,0
  1118. gdt32data32     db      0ffh,0ffh,0,0,0,92h,0cfh,0
  1119. gdt32zero32     db      0ffh,0ffh,0,0,0,92h,0cfh,0
  1120. gdt32code16     db      0ffh,0ffh,0,0,0,9ah,0,0
  1121. gdt32data16     db      0ffh,0ffh,0,0,0,92h,0,0
  1122. gdt32task       db      0ffh,0ffh,0,0,0,89h,0,0
  1123. gdt32vcpi       dq      3 dup(?)
  1124. if SELECTORS ne 0
  1125. gdt32free       db      SELECTORS dup(0ffh,0ffh,0,0,0,92h,0cfh,0)
  1126. endif
  1127.  
  1128. v86r_edi        label   dword           ; vregs for pmode<>real communication
  1129. v86r_di         dw      ?, ?            ;  needs to stay this way cuz its a
  1130. v86r_esi        label   dword           ;  stupid DPMI structure thingy
  1131. v86r_si         dw      ?, ?
  1132. v86r_ebp        label   dword
  1133. v86r_bp         dw      ?, ?
  1134.                 dd      0
  1135. v86r_ebx        label   dword
  1136. v86r_bx         label   word
  1137. v86r_bl         db      ?
  1138. v86r_bh         db      ?, ?,?
  1139. v86r_edx        label   dword
  1140. v86r_dx         label   word
  1141. v86r_dl         db      ?
  1142. v86r_dh         db      ?, ?,?
  1143. v86r_ecx        label   dword
  1144. v86r_cx         label   word
  1145. v86r_cl         db      ?
  1146. v86r_ch         db      ?, ?,?
  1147. v86r_eax        label   dword
  1148. v86r_ax         label   word
  1149. v86r_al         db      ?
  1150. v86r_ah         db      ?, ?,?
  1151. v86r_flags      dw      ?
  1152. v86r_es         dw      ?
  1153. v86r_ds         dw      ?
  1154. v86r_fs         dw      ?
  1155. v86r_gs         dw      ?
  1156.                 dd      0,0
  1157.  
  1158. oint1bvect      dd      ?               ; old real int 1bh vector (ctrl+break)
  1159. oint32vect      dd      ?               ; old real int 32h vector
  1160. oirqmask        dw      ?               ; old port 21h and 0a1h masks
  1161. intslottbl      db      8,9,0ah,0bh,0ch,0dh,0eh,0fh,70h,71h,72h,73h,74h,75h,76h,77h
  1162.                 db      33h,32h,31h,0,1,2,3,4,5,6,7
  1163.  
  1164. if SELECTORS ne 0
  1165. selectorbase    dw      50h
  1166. selectorinc     dw      8
  1167. selectorfree    db      SELECTORS dup(0)
  1168. endif
  1169.  
  1170. code16off       dw      c_retreal       ; offset in 16bit of exit function
  1171. code16sel       dw      20h             ; 16bit pmode code selector
  1172. data16sel       dw      28h             ; 16bit pmode data selector
  1173.  
  1174. nextmodestack   dw      (STAKSIZE-STAKMAIN)*16  ; stack for next mode switch
  1175. realstackbase   dd      ?               ; linear ptr to beginning of codeend
  1176.  
  1177. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1178. ; 32 bit common system code
  1179. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1180. ;═════════════════════════════════════════════════════════════════════════════
  1181. p_cpmode2:                ; call pmode from V86
  1182.         mov gs,cx
  1183.         mov cl,10h
  1184.         mov ds,cx
  1185.         mov es,cx
  1186.         mov fs,cx
  1187.         sub nextmodestack,STAKPMODE*16
  1188.     push p_cpmodem2
  1189.     mov p_cpmodem2,V86CPMODED
  1190.         mov eax,[esp+22]
  1191.     mov p_cpmodem0,eax
  1192.         mov al,[esp+43]
  1193.     shr al,1
  1194.     and al,1
  1195.     add al,0fah
  1196.     mov p_cpmodem1,al
  1197.         jmp short p_cpmode
  1198. ;═════════════════════════════════════════════════════════════════════════════
  1199. p_cpmode1:                              ; call pmode, load all
  1200.         mov esp,ebx
  1201.         mov ax,10h
  1202.         mov ds,ax
  1203.         mov es,ax
  1204.         mov ss,ax
  1205. ;-----------------------------------------------------------------------------
  1206. p_cpmode0:                              ; call pmode, load FS and GS
  1207.         mov fs,_seldata
  1208.         mov gs,_selzero
  1209. ;-----------------------------------------------------------------------------
  1210. p_cpmode:                               ; call pmode routine from real
  1211.         push offset p_cpmoded
  1212.         cld
  1213.         mov eax,v86r_eax
  1214.         mov ecx,v86r_ecx
  1215.         mov edx,v86r_edx
  1216.         mov ebx,v86r_ebx
  1217.         mov esi,v86r_esi
  1218.         mov edi,v86r_edi
  1219.         mov ebp,v86r_ebp
  1220.         db 68h                  ; PUSH destination address
  1221. p_cpmodem0      dd      ?       ;
  1222. p_cpmodem1      db      ?       ; CLI or STI
  1223.         ret
  1224. ;-----------------------------------------------------------------------------
  1225. p_cpmoded:                              ; call to pmode done
  1226.         cli
  1227.         pushf
  1228.         pop v86r_flags
  1229.         mov v86r_eax,eax
  1230.         mov v86r_ecx,ecx
  1231.         mov v86r_edx,edx
  1232.         mov v86r_ebx,ebx
  1233.         mov v86r_esi,esi
  1234.         mov v86r_edi,edi
  1235.         mov v86r_ebp,ebp
  1236.         mov ecx,_code16a
  1237. p_cpmodem2        label word            ; return to real, modifiable to JMP
  1238. ;-----------------------------------------------------------------------------
  1239.         movzx ebx,gs:savedstakoff[ecx]  ; DPMI return to real mode
  1240.         mov dx,gs:savedstakseg[ecx]
  1241.         mov ax,code16
  1242.         mov cx,dx
  1243.         mov si,ax
  1244.         mov edi,offset int32d0
  1245.         jmp dp_switchaddx
  1246. ;-----------------------------------------------------------------------------
  1247. VCPMODED=(($-(p_cpmodem2+2))shl 8)+0ebh
  1248. p_cpmoded2:                             ; VCPI done with pmode
  1249.         movzx esi,gs:savedstakoff[ecx]
  1250.         mov cx,code16
  1251.         mov edi,offset int32d1
  1252.         db 0eah                 ; 16bit JMP FAR 20h:v_retreal0
  1253.         dw v_retreal0,0,20h     ;
  1254. ;-----------------------------------------------------------------------------
  1255. CCPMODED=(($-(p_cpmodem2+2))shl 8)+0ebh
  1256. p_cpmoded3:                             ; raw/XMS done with pmode
  1257.         mov gs:c_retreal0m0[ecx],offset int32d3
  1258.         db 0eah                 ; 16bit JMP FAR 20h:c_retreal0
  1259.         dw c_retreal0,0,20h     ;
  1260. ;-----------------------------------------------------------------------------
  1261. if ($-(p_cpmodem2+2)) gt 127
  1262.   err
  1263. endif
  1264. V86CPMODED=(($-(p_cpmodem2+2))shl 8)+0ebh
  1265. p_cpmoded4:                ; V86 done with pmode
  1266.     pop p_cpmodem2
  1267.         jmp cp_int3_d3
  1268. ;═════════════════════════════════════════════════════════════════════════════
  1269. p_start:                                ; common 32bit start
  1270.         mov eax,gs:[1bh*4]              ; neutralize crtl+break
  1271.         mov oint1bvect,eax
  1272.         db 65h,67h,0c7h,6       ; MOV DWORD PTR GS:[1bh*4],code16:nullint
  1273.         dw 1bh*4,nullint,code16 ;
  1274.         mov eax,gs:[32h*4]              ; set up for new real mode INT32
  1275.         mov oint32vect,eax
  1276.         db 65h,67h,0c7h,6       ; MOV DWORD PTR GS:[32h*4],code16:int32
  1277.         dw 32h*4,int32,code16   ;
  1278.         in al,21h                       ; save old PIC masks
  1279.         mov ah,al
  1280.         in al,0a1h
  1281.         mov oirqmask,ax
  1282.         jmp _main                       ; go to main code
  1283.  
  1284. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1285. ; Allocate any mem, (first cheks low, then high)
  1286. ; In:
  1287. ;   EAX - size requested
  1288. ; Out:
  1289. ;   CF=0 - memory allocated
  1290. ;   CF=1 - not enough mem
  1291. ;   EAX - linear pointer to mem or ?
  1292. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1293. _getmem:
  1294.         push eax
  1295.         call _getlomem
  1296.         jnc short getmemd
  1297.         pop eax
  1298.         jmp short _gethimem
  1299. getmemd:
  1300.         add esp,4
  1301. _ret:                                   ; generic RET instruction
  1302.         ret
  1303. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1304. ; Allocate some low mem
  1305. ; In:
  1306. ;   EAX - size requested
  1307. ; Out:
  1308. ;   CF=0 - memory allocated
  1309. ;   CF=1 - not enough mem
  1310. ;   EAX - linear pointer to mem or ?
  1311. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1312. _getlomem:
  1313.         add eax,_lomembase
  1314.         cmp eax,_lomemtop
  1315.         ja short getmemerr
  1316.         xchg eax,_lomembase
  1317.         clc
  1318.         ret
  1319. getmemerr:
  1320.         stc
  1321.         ret
  1322. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1323. ; Allocate some high mem
  1324. ; In:
  1325. ;   EAX - size requested
  1326. ; Out:
  1327. ;   CF=0 - memory allocated
  1328. ;   CF=1 - not enough mem
  1329. ;   EAX - linear pointer to mem or ?
  1330. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1331. _gethimem:
  1332.         add eax,_himembase
  1333.         cmp eax,_himemtop
  1334.         ja short getmemerr
  1335.         xchg eax,_himembase
  1336.         clc
  1337.         ret
  1338. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1339. ; Get amount of free low mem
  1340. ; Out:
  1341. ;   EAX - number of bytes free
  1342. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1343. _lomemsize:
  1344.         mov eax,_lomemtop
  1345.         sub eax,_lomembase
  1346.         ret
  1347. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1348. ; Get amount of free high mem
  1349. ; Out:
  1350. ;   EAX - number of bytes free
  1351. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1352. _himemsize:
  1353.         mov eax,_himemtop
  1354.         sub eax,_himembase
  1355.         ret
  1356. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1357. ; Get status of IRQ mask bit
  1358. ; In:
  1359. ;   BL - IRQ num (0-15)
  1360. ; Out:
  1361. ;   AL - status: 0=enabled, 1=disabled
  1362. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1363. _getirqmask:
  1364.         push ax
  1365.         in al,0a1h
  1366.         mov ah,al
  1367.         in al,21h
  1368.         xchg cl,bl
  1369.         shr ax,cl
  1370.         xchg cl,bl
  1371.         and al,1
  1372.         mov [esp],al
  1373.         pop ax
  1374.         ret
  1375. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1376. ; Set status of IRQ mask bit
  1377. ; In:
  1378. ;   BL - IRQ num (0-15)
  1379. ;   AL - status: 0=enabled, 1=disabled
  1380. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1381. _setirqmask:
  1382.         push ax bx cx dx
  1383.         mov cl,bl
  1384.         mov bx,0fffeh
  1385.         movzx dx,al
  1386.         rol bx,cl
  1387.         shl dx,cl
  1388.         in al,0a1h
  1389.         mov ah,al
  1390.         in al,21h
  1391.         and ax,bx
  1392.         or ax,dx
  1393.         out 21h,al
  1394.         mov al,ah
  1395.         out 0a1h,al
  1396.         pop dx cx bx ax
  1397.         ret
  1398. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1399. ; Set a real mode IRQ vect to redirect to pmode
  1400. ; In:
  1401. ;   BL - IRQ number
  1402. ;   EDX - offset of IRQ handler
  1403. ;   EDI -> 21 byte buffer for code stub created
  1404. ; Out:
  1405. ;   EAX - old seg:off of real mode IRQ handler
  1406. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1407. rmpmirqsetd0:
  1408. db 66h,52h              ; PUSH EDX
  1409. db 66h,0bah,?,?,?,?     ; MOV EDX,?
  1410. db 0cdh,32h             ; INT 32h
  1411. db 66h,5ah              ; POP EDX
  1412. db 0cfh                 ; IRET
  1413. db 9ch                  ; PUSHFD
  1414. db 0eh                  ; PUSH CS
  1415. db 0e8h,?,?,?,?         ; CALL ?
  1416. db 0c3h                 ; RET
  1417. ;-----------------------------------------------------------------------------
  1418. _rmpmirqset:
  1419.         push esi edi
  1420.         mov esi,offset rmpmirqsetd0
  1421.         lea eax,[edi+13]
  1422.         mov [esi+4],eax
  1423.         add eax,7
  1424.         sub eax,edx
  1425.         neg eax
  1426.         mov [esi+16],eax
  1427.         mov eax,edi
  1428.         movsd
  1429.         movsd
  1430.         movsd
  1431.         movsd
  1432.         movsd
  1433.         movsb
  1434.         add eax,_code32a
  1435.         shl eax,12
  1436.         shr ax,12
  1437.         movzx edi,bl
  1438.         cmp edi,8
  1439.         jb short rmpmirqsetf0
  1440.         add edi,60h
  1441. rmpmirqsetf0:
  1442.         xchg eax,gs:[edi*4+20h]
  1443.         pop edi esi
  1444.         ret
  1445. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1446. ; Reset a real more IRQ vect back to normal (just sets real mode IRQ vect)
  1447. ; In:
  1448. ;   BL - IRQ number
  1449. ;   EAX - seg:off of real mode IRQ handler
  1450. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1451. _rmpmirqfree:
  1452.         push ebx
  1453.         movzx ebx,bl
  1454.         cmp bl,8
  1455.         jb short rmpmirqfreef0
  1456.         add bl,60h
  1457. rmpmirqfreef0:
  1458.         mov gs:[ebx*4+20h],eax
  1459.         pop ebx
  1460.         ret
  1461. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1462. ; Allocate a selector
  1463. ; Out:
  1464. ;   CF=1 - selector not allocated
  1465. ;   CF=0 - selector allocated
  1466. ;   AX - 4G data selector or ?
  1467. ; Notes:
  1468. ;   The selector returned is for a 4G r/w data segment with an undefined base
  1469. ;    address.
  1470. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1471. _getselector:
  1472. if SELECTORS eq 0
  1473.         stc
  1474.         ret
  1475. else
  1476.         push ecx edi
  1477.         mov edi,offset selectorfree
  1478.         mov ecx,SELECTORS
  1479.         mov al,0
  1480.         repne scasb
  1481.         jne short getselectorf0
  1482.         mov byte ptr [edi-1],1
  1483.         sub ecx,SELECTORS-1
  1484.         neg ecx
  1485.         imul cx,selectorinc
  1486.         mov ax,selectorbase
  1487.         add ax,cx
  1488.         clc
  1489.         jmp short getselectorf1
  1490. getselectorf0:
  1491.         stc
  1492. getselectorf1:
  1493.         pop edi ecx
  1494.         ret
  1495. endif
  1496. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1497. ; Free an allocated selector
  1498. ; In:
  1499. ;   AX - selector
  1500. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1501. _freeselector:
  1502. if SELECTORS ne 0
  1503.         push eax dx
  1504.         sub ax,selectorbase
  1505.         xor dx,dx
  1506.         div selectorinc
  1507.         movzx eax,ax
  1508.         mov selectorfree[eax],0
  1509.         pop dx eax
  1510. endif
  1511.         ret
  1512. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1513. ; Exit to real mode
  1514. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1515. _exit:
  1516.         cli
  1517.         mov eax,oint1bvect              ; restore ctrl+break
  1518.         mov gs:[1bh*4],eax
  1519.         mov eax,oint32vect              ; restore real mode int 32h vector
  1520.         mov gs:[32h*4],eax
  1521.         mov ax,oirqmask                 ; restore PIC masks
  1522.         out 0a1h,al
  1523.         mov al,ah
  1524.         out 21h,al
  1525.         push code16sel                  ; go to 16bit pmode exit code
  1526.         push code16off
  1527.         mov ds,data16sel
  1528.         db 66h,0cbh             ; 16bit RETF
  1529.  
  1530. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1531. ; 32 bit DPMI system data
  1532. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1533. dp_switchaddx   df      ?               ; switch to real mode addx
  1534. dp_saveaddx     df      ?               ; save/restore state addx
  1535. dp_savelen      dw      0,0             ; length of state buffer
  1536. dp_savedstaksel dw      ?               ; current saved stack selector
  1537.  
  1538. dp_ointbuf      df      18 dup(?)       ; saved interrupt addx buffer
  1539. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1540. ; 32 bit DPMI system code
  1541. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1542. ;═════════════════════════════════════════════════════════════════════════════
  1543. dp_int32:                               ; DPMI INT32/34: CX:DX=seg:off
  1544.         pushad
  1545.         shl ecx,16
  1546.         mov cx,dx
  1547.         mov bp,offset callreal
  1548.         mov dl,1
  1549.         jmp short dp_int3_
  1550. ;═════════════════════════════════════════════════════════════════════════════
  1551. dp_int33:                               ; DPMI INT33/35: AL=int num
  1552.         pushad
  1553.         movzx eax,al
  1554.         mov ecx,gs:[eax*4]
  1555.         mov bp,offset intreal
  1556.         xor dl,dl
  1557. ;-----------------------------------------------------------------------------
  1558. dp_int3_:                               ; DPMI int or call to real mode
  1559.         mov ax,900h
  1560.         int 31h
  1561.         push ax
  1562.         and al,dl
  1563.         add al,0fah
  1564.         mov ebx,_code16a
  1565.         mov gs:icrealm0[ebx],al
  1566.         mov gs:icrealm1[ebx],ecx
  1567.         push cp_savedstakoff
  1568.         push dp_savedstaksel
  1569.         movzx ebx,nextmodestack
  1570.         lea eax,[ebx-STAKRMODE*16]
  1571.         mov nextmodestack,ax
  1572.         mov ax,ss
  1573.         mov es,ax
  1574.         sub esp,dword ptr dp_savelen
  1575.         mov edi,esp
  1576.         xor al,al
  1577.         call dp_saveaddx
  1578.         mov cp_savedstakoff,esp
  1579.         mov dp_savedstaksel,ss
  1580.         mov dx,codeend
  1581.         mov ax,v86r_ds
  1582.         mov cx,v86r_es
  1583.         movzx edi,bp
  1584.         mov si,code16
  1585.         jmp dp_switchaddx
  1586. ;-----------------------------------------------------------------------------
  1587. dp_int3_d:                              ; done with real mode int or call
  1588.         mov edi,esp
  1589.         mov al,1
  1590.         call dp_saveaddx
  1591.         add esp,dword ptr dp_savelen
  1592.         pop dp_savedstaksel
  1593.         pop cp_savedstakoff
  1594.         add nextmodestack,STAKRMODE*16
  1595.         mov bx,v86r_flags
  1596.         pop ax
  1597.         int 31h
  1598.         mov ax,ds
  1599.         mov es,ax
  1600.         mov fs,ax
  1601.         mov gs,_selzero
  1602.         jmp cp_int3_d2
  1603. ;═════════════════════════════════════════════════════════════════════════════
  1604. ; DPMI IRQ redirectors (needed to make all IRQ vector selectors = CS)
  1605. dp_irq0:
  1606.         jmp cs:dp_ointbuf[0]
  1607. dp_irq1:
  1608.         jmp cs:dp_ointbuf[6]
  1609. dp_irq2:
  1610.         jmp cs:dp_ointbuf[12]
  1611. dp_irq3:
  1612.         jmp cs:dp_ointbuf[18]
  1613. dp_irq4:
  1614.         jmp cs:dp_ointbuf[24]
  1615. dp_irq5:
  1616.         jmp cs:dp_ointbuf[30]
  1617. dp_irq6:
  1618.         jmp cs:dp_ointbuf[36]
  1619. dp_irq7:
  1620.         jmp cs:dp_ointbuf[42]
  1621. dp_irq8:
  1622.         jmp cs:dp_ointbuf[48]
  1623. dp_irq9:
  1624.         jmp cs:dp_ointbuf[54]
  1625. dp_irqa:
  1626.         jmp cs:dp_ointbuf[60]
  1627. dp_irqb:
  1628.         jmp cs:dp_ointbuf[66]
  1629. dp_irqc:
  1630.         jmp cs:dp_ointbuf[72]
  1631. dp_irqd:
  1632.         jmp cs:dp_ointbuf[78]
  1633. dp_irqe:
  1634.         jmp cs:dp_ointbuf[84]
  1635. dp_irqf:
  1636.         jmp cs:dp_ointbuf[90]
  1637.  
  1638. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1639. ; DPMI get IRQ handler offset
  1640. ; In:
  1641. ;   BL - IRQ num (0-0fh)
  1642. ; Out:
  1643. ;   EDX - offset of IRQ handler
  1644. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1645. dp_getirqvect:
  1646.         push ax ebx cx
  1647.         movzx ebx,bl
  1648.         mov bl,intslottbl[ebx]
  1649.         mov ax,204h
  1650.         int 31h
  1651.         pop cx ebx ax
  1652.         ret
  1653. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1654. ; DPMI set IRQ handler offset
  1655. ; In:
  1656. ;   BL - IRQ num (0-0fh)
  1657. ;   EDX - offset of IRQ handler
  1658. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1659. dp_setirqvect:
  1660.         push ax ebx cx
  1661.         movzx ebx,bl
  1662.         mov bl,intslottbl[ebx]
  1663.         mov cx,cs
  1664.         mov ax,205h
  1665.         int 31h
  1666.         pop cx ebx ax
  1667.         ret
  1668. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1669. ; Set the base addx for a selector
  1670. ; In:
  1671. ;   AX - selector
  1672. ;   EDX - linear base addx for selector
  1673. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1674. dp_setselector:
  1675. if SELECTORS ne 0
  1676.         push ax bx ecx
  1677.         shld ecx,edx,16
  1678.         mov bx,ax
  1679.         mov ax,7
  1680.         int 31h
  1681.         pop ecx bx ax
  1682. endif
  1683.         ret
  1684.  
  1685. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1686. ; 32 bit custom/XMS/VCPI system data
  1687. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1688. vp_vcpipmentry  df      3800000000h     ; VCPI entry point in pmode
  1689.  
  1690. cp_tssesp0ptr   dd      0               ; ptr to ESP0 in TSS, or null in VCPI
  1691. cp_idt32ptr     dd      ?               ; ptr to 32bit IDT
  1692. cp_validexcdesp dd      0               ; valid ESP value for exc 13
  1693.  
  1694. cp_v86irqintr   dd      offset cp_int33f0
  1695. cp_v86irqnum    db      ?               ; IRQ num for V86 mode
  1696. cp_savedstakoff dd      ?               ; current saved stack offset
  1697. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1698. ; 32 bit custom/XMS/VCPI system code
  1699. ;▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
  1700. ;═════════════════════════════════════════════════════════════════════════════
  1701. cp_int31:                               ; INT 31h: AX=900h,901h,902h
  1702.         cmp al,1
  1703.         mov al,[esp+9]
  1704.         jb short cp_int31f0
  1705.         ja short cp_int31f1
  1706.         or byte ptr [esp+9],2
  1707.         jmp short cp_int31f1
  1708. cp_int31f0:
  1709.         and byte ptr [esp+9],0fdh
  1710. cp_int31f1:
  1711.         shr al,1
  1712.         and al,1
  1713.         iretd
  1714. ;═════════════════════════════════════════════════════════════════════════════
  1715. vp_int32:                               ; VCPI INT 32h: safe CX:DX=seg:off
  1716.         pushad
  1717.         mov ebp,offset callreal
  1718.         mov si,VICREAL1D
  1719.         mov bl,2
  1720.         jmp short cp_int34f0
  1721. ;═════════════════════════════════════════════════════════════════════════════
  1722. vp_int33:                               ; VCPI INT 33h: safe AL=int num
  1723.         pushad
  1724.         mov ebp,offset intreal
  1725. ;-----------------------------------------------------------------------------
  1726. vp_int33f0:
  1727.         mov si,VICREAL1D
  1728.         mov bl,2
  1729.         jmp short cp_int35f0
  1730. ;═════════════════════════════════════════════════════════════════════════════
  1731. cp_int32:                               ; INT 32h: safe CX:DX=seg:off
  1732.         pushad
  1733.         mov ebp,offset callreal
  1734.         mov si,CICREAL1D
  1735.         mov bl,1
  1736.         jmp short cp_int34f0
  1737. ;═════════════════════════════════════════════════════════════════════════════
  1738. cp_int33:                               ; INT 33h: safe AL=int num
  1739.         pushad
  1740.         mov ebp,offset intreal
  1741. ;-----------------------------------------------------------------------------
  1742. cp_int33f0:
  1743.         mov si,CICREAL1D
  1744.         mov bl,1
  1745.         jmp short cp_int35f0
  1746. ;-----------------------------------------------------------------------------
  1747. cp_int34f0:                             ; relic from times past
  1748.         shl ecx,16
  1749.         mov cx,dx
  1750.         mov bh,1
  1751.         jmp short cp_int3_
  1752. ;-----------------------------------------------------------------------------
  1753. cp_int35f0:                             ; another one, but functional
  1754.         movzx eax,al
  1755.         mov ecx,gs:[eax*4]
  1756.         xor bh,bh
  1757. ;-----------------------------------------------------------------------------
  1758. cp_int3_:                               ; int or call to real mode
  1759.         mov edi,[esp+40]
  1760.         shld eax,edi,23
  1761.         and al,bh
  1762.         add al,0fah
  1763.         mov edx,_code16a
  1764.         mov gs:icrealm0[edx],al
  1765.         mov gs:icrealm1[edx],ecx
  1766.         xchg gs:icrealm2[edx],si
  1767.         push si
  1768.         movzx esi,nextmodestack
  1769.         lea eax,[esi-STAKRMODE*16]
  1770.         mov nextmodestack,ax
  1771.         add eax,realstackbase
  1772.         mov edx,cp_tssesp0ptr
  1773.         push dword ptr [edx]
  1774.         mov [edx],eax
  1775.         sub eax,40
  1776.         push cp_validexcdesp
  1777.         mov cp_validexcdesp,eax
  1778.         push cp_savedstakoff
  1779.         mov cp_savedstakoff,esp
  1780.         cmp bl,1
  1781.         ja short vp_int3_s
  1782. ;-----------------------------------------------------------------------------
  1783. cp_int3_s:                              ; safe real mode int or call
  1784.         mov edx,_code16a
  1785.         mov gs:c_sicrealm0[edx],bp
  1786.         mov esp,esi
  1787.         db 0eah                 ; 16bit JMP FAR 20h:c_retreal0
  1788.         dw c_retreal0,0,20h     ;
  1789. ;-----------------------------------------------------------------------------
  1790. vp_int3_s:                              ; safe VCPI real mode int or call
  1791.         mov edi,ebp
  1792.         mov cx,v86r_ds
  1793.         mov ax,28h
  1794.         mov ds,ax
  1795.         db 0eah                 ; 16bit JMP FAR 20h:v_retreal0
  1796.         dw v_retreal0,0,20h     ;
  1797. ;-----------------------------------------------------------------------------
  1798. cp_int3_d:                              ; done with real mode int or call
  1799.         mov ax,18h
  1800.         mov gs,ax
  1801.         mov ax,10h
  1802.         mov ds,ax
  1803.         mov es,ax
  1804.         mov fs,ax
  1805.         mov ss,ax
  1806.         mov esp,cp_savedstakoff
  1807.         pop cp_savedstakoff
  1808.         pop cp_validexcdesp
  1809.         mov ebx,cp_tssesp0ptr
  1810.         pop dword ptr [ebx]
  1811.         mov ebx,_code16a
  1812.         pop gs:icrealm2[ebx]
  1813. ;-----------------------------------------------------------------------------
  1814. cp_int3_d3:                             ; done from real mode pmode call
  1815.         add nextmodestack,STAKRMODE*16
  1816.         mov bx,v86r_flags
  1817. ;-----------------------------------------------------------------------------
  1818. cp_int3_d2:                ; done from DPMI also
  1819.         mov ax,[esp+40]
  1820.         and ax,not 8d5h
  1821.         and bx,8d5h
  1822.         or ax,bx
  1823.         mov [esp+40],ax
  1824.         popad
  1825.         iretd
  1826. ;═════════════════════════════════════════════════════════════════════════════
  1827. ; low 8 exceptions and undefined interrupt.
  1828. cp_exc0:
  1829.         push eax
  1830.         xor al,al
  1831.         jmp cp_irq
  1832. cp_exc1:
  1833.         push eax
  1834.         mov al,1
  1835.         jmp cp_irq
  1836. cp_exc2:
  1837.         push eax
  1838.         mov al,2
  1839.         jmp cp_irq
  1840. cp_exc3:
  1841.         push eax
  1842.         mov al,3
  1843.         jmp cp_irq
  1844. cp_exc4:
  1845.         push eax
  1846.         mov al,4
  1847.         jmp cp_irq
  1848. cp_exc5:
  1849.         push eax
  1850.         mov al,5
  1851.         jmp cp_irq
  1852. cp_exc6:
  1853.         pushad
  1854.         mov al,6
  1855.         jmp short cp_exc
  1856. cp_exc7:
  1857.         push eax
  1858.         mov al,7
  1859.         jmp cp_irq
  1860. cp_excf:
  1861.         pushad
  1862.         mov al,0ffh
  1863. ;-----------------------------------------------------------------------------
  1864. cp_exc:                                 ; main exception handler
  1865.         mov bx,10h
  1866.         mov ds,bx
  1867.         mov es,bx
  1868.         mov fs,bx
  1869.         mov gs,_selzero
  1870.         cld
  1871.         jmp _exit
  1872. ;═════════════════════════════════════════════════════════════════════════════
  1873. ; IRQ redirector between modes
  1874. cp_irq0:
  1875.         push eax
  1876.         mov al,08h
  1877.         jmp short cp_irq
  1878. cp_irq1:
  1879.         push eax
  1880.         mov al,09h
  1881.         jmp short cp_irq
  1882. cp_irq2:
  1883.         push eax
  1884.         mov al,0ah
  1885.         jmp short cp_irq
  1886. cp_irq3:
  1887.         push eax
  1888.         mov al,0bh
  1889.         jmp short cp_irq
  1890. cp_irq4:
  1891.         push eax
  1892.         mov al,0ch
  1893.         jmp short cp_irq
  1894. cp_irq5:
  1895.         push eax
  1896.         mov al,0dh
  1897.         jmp short cp_irq
  1898. cp_irq6:
  1899.         push eax
  1900.         mov al,0eh
  1901.         jmp short cp_irq
  1902. cp_irq7:
  1903.         push eax
  1904.         mov al,0fh
  1905.         jmp short cp_irq
  1906. cp_irq8:
  1907.         push eax
  1908.         mov al,70h
  1909.         jmp short cp_irq
  1910. cp_irq9:
  1911.         push eax
  1912.         mov al,71h
  1913.         jmp short cp_irq
  1914. cp_irqa:
  1915.         push eax
  1916.         mov al,72h
  1917.         jmp short cp_irq
  1918. cp_irqb:
  1919.         push eax
  1920.         mov al,73h
  1921.         jmp short cp_irq
  1922. cp_irqc:
  1923.         push eax
  1924.         mov al,74h
  1925.         jmp short cp_irq
  1926. cp_irqd:
  1927.         push eax
  1928.         mov al,75h
  1929.         jmp short cp_irq
  1930. cp_irqe:
  1931.         push eax
  1932.         mov al,76h
  1933.         jmp short cp_irq
  1934. cp_irqf:
  1935.         push eax
  1936.         mov al,77h
  1937. ;─────────────────────────────────────────────────────────────────────────────
  1938. cp_irq:                                 ; main IRQ handler thingy
  1939.         mov ss:cp_v86irqnum,al
  1940.         pop eax
  1941.         push ds es fs gs
  1942.         pushfd
  1943.         push cs
  1944.         push offset cp_irqpd
  1945.         pushad
  1946.         mov ax,10h
  1947.         mov ds,ax
  1948.         mov al,18h
  1949.         mov gs,ax
  1950.         mov al,cp_v86irqnum
  1951.         mov ebp,offset c_irqreal
  1952.         jmp cp_v86irqintr
  1953. cp_irqpd:
  1954.         pop gs fs es ds
  1955.         iretd
  1956.  
  1957. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1958. ; Custom get IRQ handler offset
  1959. ; In:
  1960. ;   BL - IRQ num (0-0fh)
  1961. ; Out:
  1962. ;   EDX - offset of IRQ handler
  1963. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1964. cp_getirqvect:
  1965.         push ebx
  1966.         pushfd
  1967.         cli
  1968.         movzx ebx,bl
  1969.         mov bl,intslottbl[ebx]
  1970.         lea ebx,[ebx*8]
  1971.         add ebx,cp_idt32ptr
  1972.         mov dx,[ebx+6]
  1973.         shl edx,16
  1974.         mov dx,[ebx]
  1975.         popfd
  1976.         pop ebx
  1977.         ret
  1978. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1979. ; Custom set IRQ handler offset
  1980. ; In:
  1981. ;   BL - IRQ num (0-0fh)
  1982. ;   EDX - offset of IRQ handler
  1983. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1984. cp_setirqvect:
  1985.         push ebx edx
  1986.         pushfd
  1987.         cli
  1988.         movzx ebx,bl
  1989.         mov bl,intslottbl[ebx]
  1990.         lea ebx,[ebx*8]
  1991.         add ebx,cp_idt32ptr
  1992.         mov [ebx],dx
  1993.         shr edx,16
  1994.         mov [ebx+6],dx
  1995.         popfd
  1996.         pop edx ebx
  1997.         ret
  1998. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  1999. ; Custom set the base addx for a selector
  2000. ; In:
  2001. ;   AX - selector
  2002. ;   EDX - linear base addx for selector
  2003. ;░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  2004. cp_setselector:
  2005. if SELECTORS ne 0
  2006.         push eax edx
  2007.         movzx eax,ax
  2008.         and edx,0ffffffh
  2009.         or edx,92000000h
  2010.         mov dword ptr gdt32[eax+2],edx
  2011.         mov dl,[esp+3]
  2012.         mov byte ptr gdt32[eax+7],dl
  2013.         pop edx eax
  2014. endif
  2015.         ret
  2016.  
  2017. code32  ends
  2018.  
  2019. ;▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  2020. ; End of program (must be at end of program or you will suffer)
  2021. ;▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
  2022. codeend segment para stack use32 'stack'
  2023. db STAKSIZE*16 dup(?)
  2024. codeend ends
  2025.         end     start16
  2026.  
  2027.