Subversion Repositories Kolibri OS

Rev

Rev 846 | Rev 848 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;; Copyright (C) KolibriOS team 2004-2008. All rights reserved.
  4. ;; PROGRAMMING:
  5. ;; Ivan Poddubny
  6. ;; Marat Zakiyanov (Mario79)
  7. ;; VaStaNi
  8. ;; Trans
  9. ;; Mihail Semenyako (mike.dld)
  10. ;; Sergey Kuzmin (Wildwest)
  11. ;; Andrey Halyavin (halyavin)
  12. ;; Mihail Lisovin (Mihasik)
  13. ;; Andrey Ignatiev (andrew_programmer)
  14. ;; NoName
  15. ;; Evgeny Grechnikov (Diamond)
  16. ;; Iliya Mihailov (Ghost)
  17. ;; Sergey Semyonov (Serge)
  18. ;; Johnny_B
  19. ;; SPraid (simba)
  20. ;;
  21. ;; Data in this file was originally part of MenuetOS project which is
  22. ;; distributed under the terms of GNU GPL. It is modified and redistributed as
  23. ;; part of KolibriOS project under the terms of GNU GPL.
  24. ;;
  25. ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa
  26. ;; PROGRAMMING:
  27. ;;
  28. ;; Ville Mikael Turjanmaa, villemt@itu.jyu.fi
  29. ;; - main os coding/design
  30. ;; Jan-Michael Brummer, BUZZ2@gmx.de
  31. ;; Felix Kaiser, info@felix-kaiser.de
  32. ;; Paolo Minazzi, paolo.minazzi@inwind.it
  33. ;; quickcode@mail.ru
  34. ;; Alexey, kgaz@crosswinds.net
  35. ;; Juan M. Caravaca, bitrider@wanadoo.es
  36. ;; kristol@nic.fi
  37. ;; Mike Hibbett, mikeh@oceanfree.net
  38. ;; Lasse Kuusijarvi, kuusijar@lut.fi
  39. ;; Jarek Pelczar, jarekp3@wp.pl
  40. ;;
  41. ;; KolibriOS is distributed in the hope that it will be useful, but WITHOUT ANY
  42. ;; WARRANTY. No author or distributor accepts responsibility to anyone for the
  43. ;; consequences of using it or for whether it serves any particular purpose or
  44. ;; works at all, unless he says so in writing. Refer to the GNU General Public
  45. ;; License (the "GPL") for full details.
  46. ;
  47. ;; Everyone is granted permission to copy, modify and redistribute KolibriOS,
  48. ;; but only under the conditions described in the GPL. A copy of this license
  49. ;; is supposed to have been given to you along with KolibriOS so you can know
  50. ;; your rights and responsibilities. It should be in a file named COPYING.
  51. ;; Among other things, the copyright notice and this notice must be preserved
  52. ;; on all copies.
  53. ;;
  54. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  55.  
  56. include 'macros.inc'
  57.  
  58. $Revision: 847 $
  59.  
  60.  
  61. fastcall equ call
  62.  
  63. USE_COM_IRQ equ 1      ;make irq 3 and irq 4 available for PCI devices
  64.  
  65. include "proc32.inc"
  66. include "kglobals.inc"
  67. include "lang.inc"
  68.  
  69. include "const.inc"
  70. max_processes    equ   255
  71. tss_step         equ   (128+8192) ; tss & i/o - 65535 ports, * 256=557056*4
  72.  
  73.  
  74. os_stack       equ  (os_data_l-gdts)    ; GDTs
  75. os_code        equ  (os_code_l-gdts)
  76. graph_data     equ  (3+graph_data_l-gdts)
  77. tss0           equ  (tss0_l-gdts)
  78. app_code       equ  (3+app_code_l-gdts)
  79. app_data       equ  (3+app_data_l-gdts)
  80. pci_code_sel   equ  (pci_code_32-gdts)
  81. pci_data_sel   equ  (pci_data_32-gdts)
  82.  
  83.  
  84. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  85. ;;
  86. ;;   Included files:
  87. ;;
  88. ;;   Kernel16.inc
  89. ;;    - Booteng.inc   English text for bootup
  90. ;;    - Bootcode.inc  Hardware setup
  91. ;;    - Pci16.inc     PCI functions
  92. ;;
  93. ;;   Kernel32.inc
  94. ;;    - Sys32.inc     Process management
  95. ;;    - Shutdown.inc  Shutdown and restart
  96. ;;    - Fat32.inc     Read / write hd
  97. ;;    - Vesa12.inc    Vesa 1.2 driver
  98. ;;    - Vesa20.inc    Vesa 2.0 driver
  99. ;;    - Vga.inc       VGA driver
  100. ;;    - Stack.inc     Network interface
  101. ;;    - Mouse.inc     Mouse pointer
  102. ;;    - Scincode.inc  Window skinning
  103. ;;    - Pci32.inc     PCI functions
  104. ;;
  105. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  106.  
  107.  
  108. use32
  109. org 0x100000
  110.  
  111. mboot:
  112.   dd  0x1BADB002
  113.   dd  0x00010003
  114.   dd  -(0x1BADB002 + 0x00010003)
  115.   dd  mboot
  116.   dd  0x100000
  117.   dd  __edata - OS_BASE
  118.   dd  LAST_PAGE
  119.   dd  __start
  120.  
  121.  
  122. align 16
  123. __start:
  124.            cld
  125.  
  126.            mov esp, __os_stack-OS_BASE
  127.            push 0
  128.            popf
  129.  
  130.            cmp eax, 0x2BADB002
  131.            mov ecx, sz_invboot
  132.            jne .fault
  133.  
  134.            bt dword [ebx], 3
  135.            mov ecx, sz_nomods
  136.            jnc .fault
  137.  
  138.            bt dword [ebx], 6
  139.            mov ecx, sz_nommap
  140.            jnc .fault
  141.  
  142.            mov [_boot_mbi-OS_BASE], ebx
  143.  
  144.            xor eax, eax
  145.            cpuid
  146.            cmp eax, 0
  147.            mov ecx, sz_nopse
  148.            jbe .fault
  149.  
  150.            mov eax, 1
  151.            cpuid
  152.            bt edx, 3
  153.            mov ecx, sz_nopse
  154.            jnc .fault
  155.  
  156. ; ENABLE PAGING
  157.  
  158.            mov ecx, 32
  159.            mov edi, _sys_pdbr+(OS_BASE shr 20)-OS_BASE
  160.            mov eax, PG_LARGE+PG_SW
  161. @@:
  162.            stosd
  163.            add eax, 4*1024*1024
  164.            loop @B
  165.  
  166.            mov dword [_sys_pdbr-OS_BASE],   PG_LARGE+PG_SW
  167.            mov dword [_sys_pdbr-OS_BASE+4], PG_LARGE+PG_SW+4*1024*1024
  168.            mov dword [_sys_pdbr-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
  169.  
  170.            mov ebx, cr4
  171.            or ebx, CR4_PSE
  172.            and ebx, not CR4_PAE
  173.            mov cr4, ebx
  174.  
  175.            mov eax, _sys_pdbr-OS_BASE
  176.            mov ebx, cr0
  177.            or ebx,CR0_PG+CR0_WP
  178.  
  179.            mov cr3, eax
  180.            mov cr0, ebx
  181.  
  182.            mov ebx, [_boot_mbi]
  183.  
  184.            mov edx, [ebx+20]
  185.            mov esi, [ebx+24]
  186.            mov ecx, LAST_PAGE
  187.            test edx, edx
  188.            jz .no_mods
  189. .scan_mod:
  190.            mov ecx, [esi+4]
  191.            add esi, 16
  192.            dec edx
  193.            jnz .scan_mod
  194.  
  195. .no_mods:
  196.            add ecx, 4095
  197.            and ecx, not 4095
  198.  
  199.            lgdt [gdts]
  200.            jmp pword os_code:high_code
  201.  
  202.  
  203. .fault:
  204. ;           push ecx
  205. ;           call _lcls
  206. ;           call __bprintf
  207. _hlt:
  208.            hlt
  209.            jmp _hlt
  210.  
  211. sz_invboot db 'Invalid multiboot loader magic value',0x0A
  212.            db 'Halted',0
  213.  
  214. sz_nomods  db 'No modules loaded',0x0A
  215.            db 'Halted',0
  216.  
  217. sz_nommap  db 'No memory table', 0x0A
  218.            db 'Halted',0
  219.  
  220. sz_nopse   db 'Page size extensions not supported',0x0A
  221.            db 'Halted',0
  222.  
  223.  
  224.  
  225. ; CLEAR 0x280000 - HEAP_BASE
  226.  
  227.        ;    xor   eax,eax
  228.        ;    mov   edi,0x280000
  229.        ;    mov   ecx,(0x800000-0x280000) / 4
  230.        ;    cld
  231.        ;    rep   stosd
  232.  
  233.        ;    mov   edi,0x40000
  234.        ;    mov   ecx,(0x90000-0x40000)/4
  235.        ;    rep   stosd
  236.  
  237. ; CLEAR KERNEL UNDEFINED GLOBALS
  238.        ;    mov   edi, endofcode-OS_BASE
  239.        ;    mov   ecx, (uglobals_size/4)+4
  240.        ;    rep   stosd
  241.  
  242. ; SAVE & CLEAR 0-0xffff
  243.  
  244.        ;    xor esi, esi
  245.        ;    mov   edi,0x2F0000
  246.        ;    mov   ecx,0x10000 / 4
  247.        ;    rep   movsd
  248.        ;    xor edi, edi
  249.        ;    mov   ecx,0x10000 / 4
  250.        ;    rep   stosd
  251.  
  252.         ;   call test_cpu
  253.            bts [cpu_caps-OS_BASE], CAPS_TSC     ;force use rdtsc
  254.  
  255.         ;   call init_BIOS32
  256.  
  257.            mov dword [sys_pgdir-OS_BASE], PG_LARGE+PG_SW
  258.            mov dword [sys_pgdir-OS_BASE+4], PG_LARGE+PG_SW+4*1024*1024
  259.  
  260.            mov ecx, 32
  261.            lea edi, [sys_pgdir-OS_BASE+0xE00]
  262.            mov eax, PG_LARGE+PG_SW
  263. @@:
  264.            stosd
  265.            add eax, 4*1024*1024
  266.            loop @B
  267.  
  268.            mov ebx, cr4
  269.            or ebx, CR4_PSE
  270.            and ebx, not CR4_PAE
  271.            mov cr4, ebx
  272.  
  273.  
  274.            mov eax, sys_pgdir-OS_BASE
  275.            mov ebx, cr0
  276.            or ebx,CR0_PG+CR0_WP
  277.  
  278.            mov cr3, eax
  279.            mov cr0, ebx
  280.  
  281.            lgdt [gdts]
  282.            jmp pword os_code:high_code
  283.  
  284. align 4
  285. bios32_entry    dd ?
  286. tmp_page_tabs   dd ?
  287.  
  288. ;use16
  289. ;org $-0x10000
  290. ;include "boot/shutdown.inc" ; shutdown or restart
  291. ;org $+0x10000
  292. ;use32
  293.  
  294. __DEBUG__ fix 1
  295. __DEBUG_LEVEL__ fix 1
  296.  
  297. org OS_BASE+$
  298.  
  299. MEM_WB     equ 6               ;write-back memory
  300. MEM_WC     equ 1               ;write combined memory
  301. MEM_UC     equ 0               ;uncached memory
  302.  
  303.  
  304. include 'printf.inc'
  305. include 'core/mm.asm'
  306.  
  307. include 'core/init.asm'
  308.  
  309. align 4
  310. proc test_cpu
  311.            locals
  312.               cpu_type   dd ?
  313.               cpu_id     dd ?
  314.               cpu_Intel  dd ?
  315.               cpu_AMD    dd ?
  316.            endl
  317.  
  318.            mov [cpu_type], 0
  319.            xor eax, eax
  320.            mov [cpu_caps], eax
  321.            mov [cpu_caps+4], eax
  322.  
  323.            pushfd
  324.            pop eax
  325.            mov ecx, eax
  326.            xor eax, 0x40000
  327.            push eax
  328.            popfd
  329.            pushfd
  330.            pop eax
  331.            xor eax, ecx
  332.            mov [cpu_type], CPU_386
  333.            jz .end_cpuid
  334.            push ecx
  335.            popfd
  336.  
  337.            mov [cpu_type], CPU_486
  338.            mov eax, ecx
  339.            xor eax, 0x200000
  340.            push eax
  341.            popfd
  342.            pushfd
  343.            pop eax
  344.            xor eax, ecx
  345.            je .end_cpuid
  346.            mov [cpu_id], 1
  347.  
  348.            xor eax, eax
  349.            cpuid
  350.  
  351.            mov [cpu_vendor], ebx
  352.            mov [cpu_vendor+4], edx
  353.            mov [cpu_vendor+8], ecx
  354.            cmp ebx, dword [intel_str]
  355.            jne .check_AMD
  356.            cmp edx, dword [intel_str+4]
  357.            jne .check_AMD
  358.            cmp ecx, dword [intel_str+8]
  359.            jne .check_AMD
  360.            mov [cpu_Intel], 1
  361.            cmp eax, 1
  362.            jl .end_cpuid
  363.            mov eax, 1
  364.            cpuid
  365.            mov [cpu_sign], eax
  366.            mov [cpu_info],  ebx
  367.            mov [cpu_caps],  edx
  368.            mov [cpu_caps+4],ecx
  369.  
  370.            shr eax, 8
  371.            and eax, 0x0f
  372.            ret
  373. .end_cpuid:
  374.            mov eax, [cpu_type]
  375.            ret
  376.  
  377. .check_AMD:
  378.            cmp ebx, dword [AMD_str]
  379.            jne .unknown
  380.            cmp edx, dword [AMD_str+4]
  381.            jne .unknown
  382.            cmp ecx, dword [AMD_str+8]
  383.            jne .unknown
  384.            mov [cpu_AMD], 1
  385.            cmp eax, 1
  386.            jl .unknown
  387.            mov eax, 1
  388.            cpuid
  389.            mov [cpu_sign], eax
  390.            mov [cpu_info],  ebx
  391.            mov [cpu_caps],  edx
  392.            mov [cpu_caps+4],ecx
  393.            shr eax, 8
  394.            and eax, 0x0f
  395.            ret
  396. .unknown:
  397.            mov eax, 1
  398.            cpuid
  399.            mov [cpu_sign], eax
  400.            mov [cpu_info],  ebx
  401.            mov [cpu_caps],  edx
  402.            mov [cpu_caps+4],ecx
  403.            shr eax, 8
  404.            and eax, 0x0f
  405.            ret
  406. endp
  407.  
  408. align 4
  409. high_code:
  410.            mov ax,os_stack
  411.            mov dx,app_data
  412.            mov ss,ax
  413.            mov esp, __os_stack
  414.  
  415.            mov ds, dx
  416.            mov es, dx
  417.            mov fs, dx
  418.            mov gs, dx
  419.  
  420.            push ecx
  421.            push ebx
  422.  
  423.          ;  mov dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
  424.  
  425.          ;  bt [cpu_caps], CAPS_PGE
  426.          ;  jnc @F
  427.  
  428.          ;  or dword [sys_pgdir-OS_BASE+(OS_BASE shr 20)], PG_GLOBAL
  429.  
  430.          ;  mov ebx, cr4
  431.          ;  or ebx, CR4_PGE
  432.          ;  mov cr4, ebx
  433. @@:
  434.          ;  xor eax, eax
  435.          ;  mov dword [sys_pgdir], eax
  436.          ;  mov dword [sys_pgdir+4], eax
  437.  
  438.          ;  mov eax, cr3
  439.          ;  mov cr3, eax           ; flush TLB
  440.  
  441.            mov edx, 0x3fB
  442.            mov eax, 3
  443.            out dx, al
  444.  
  445.            call test_cpu
  446.            call _init
  447.  
  448.            mov [tss._ss0], os_stack
  449.            mov [tss._esp0], __os_stack
  450.            mov [tss._esp], __os_stack
  451.            mov [tss._cs],os_code
  452.            mov [tss._ss],os_stack
  453.            mov [tss._ds],app_data
  454.            mov [tss._es],app_data
  455.            mov [tss._fs],app_data
  456.            mov [tss._gs],app_data
  457.            mov [tss._io],128
  458. ;Add IO access table - bit array of permitted ports
  459.            mov edi, tss._io_map_0
  460.            xor eax, eax
  461.            not eax
  462.            mov ecx, 8192/4
  463.            rep stosd                 ; access to 4096*8=65536 ports
  464.  
  465.            mov  ax,tss0
  466.            ltr  ax
  467.  
  468.            xchg bx, bx
  469.  
  470.            mov ecx, 1280*1024
  471.            fastcall _balloc
  472.            mov [_display_data], eax
  473.  
  474.            mov ecx, (unpack.LZMA_BASE_SIZE+(unpack.LZMA_LIT_SIZE shl \
  475.                     (unpack.lc+unpack.lp)))*4
  476.            fastcall _balloc
  477.            mov [unpack.p], eax
  478.  
  479. ; MEMORY MODEL
  480.  
  481.            call init_kernel_heap     ; FIXME initialize heap after pager
  482.  
  483.            call _init_mm
  484.            mov [pg_data.pg_mutex], 0
  485.  
  486.            hlt
  487.  
  488.  
  489. ; SAVE REAL MODE VARIABLES
  490.         mov     ax, [BOOT_VAR + 0x9031]
  491.         mov     [IDEContrRegsBaseAddr], ax
  492. ; --------------- APM ---------------------
  493.  
  494. ; init selectors
  495.     mov ebx,    [BOOT_VAR+0x9040]              ; offset of APM entry point
  496.     movzx eax, word [BOOT_VAR+0x9050] ; real-mode segment base address of
  497.                                       ; protected-mode 32-bit code segment
  498.     movzx ecx, word [BOOT_VAR+0x9052] ; real-mode segment base address of
  499.                                       ; protected-mode 16-bit code segment
  500.     movzx edx, word [BOOT_VAR+0x9054] ; real-mode segment base address of
  501.                                       ; protected-mode 16-bit data segment
  502.  
  503.     shl    eax, 4
  504.     mov    [dword apm_code_32 + 2], ax
  505.     shr    eax, 16
  506.     mov    [dword apm_code_32 + 4], al
  507.  
  508.     shl    ecx, 4
  509.     mov    [dword apm_code_16 + 2], cx
  510.     shr    ecx, 16
  511.     mov    [dword apm_code_16 + 4], cl
  512.  
  513.     shl    edx, 4
  514.     mov    [dword apm_data_16 + 2], dx
  515.     shr    edx, 16
  516.     mov    [dword apm_data_16 + 4], dl
  517.  
  518.     mov    dword[apm_entry], ebx
  519.     mov    word [apm_entry + 4], apm_code_32 - gdts
  520.  
  521.     mov    eax, [BOOT_VAR + 0x9044]    ; version & flags
  522.     mov    [apm_vf], eax
  523. ; -----------------------------------------
  524. ;        movzx eax,byte [BOOT_VAR+0x9010]  ; mouse port
  525. ;        mov   [0xF604],byte 1  ;al
  526.         mov     al, [BOOT_VAR+0x901F]   ; DMA access
  527.         mov     [allow_dma_access], al
  528.         mov   al,[BOOT_VAR+0x9000]        ; bpp
  529.         mov   [ScreenBPP],al
  530.  
  531.         movzx eax,word [BOOT_VAR+0x900A]  ; X max
  532.         dec   eax
  533.         mov   [Screen_Max_X],eax
  534.         mov   [screen_workarea.right],eax
  535.         movzx eax,word [BOOT_VAR+0x900C]  ; Y max
  536.         dec   eax
  537.         mov   [Screen_Max_Y],eax
  538.         mov   [screen_workarea.bottom],eax
  539.         movzx eax,word [BOOT_VAR+0x9008]  ; screen mode
  540.         mov   [SCR_MODE],eax
  541.         mov   eax,[BOOT_VAR+0x9014]       ; Vesa 1.2 bnk sw add
  542.         mov   [BANK_SWITCH],eax
  543.         mov   [BytesPerScanLine],word 640*4         ; Bytes PerScanLine
  544.         cmp   [SCR_MODE],word 0x13          ; 320x200
  545.         je    @f
  546.         cmp   [SCR_MODE],word 0x12          ; VGA 640x480
  547.         je    @f
  548.         mov   ax,[BOOT_VAR+0x9001]        ; for other modes
  549.         mov   [BytesPerScanLine],ax
  550. @@:
  551.         mov     esi, BOOT_VAR+0x9080
  552.         movzx   ecx, byte [esi-1]
  553.         mov     [NumBiosDisks], ecx
  554.         mov     edi, BiosDisksData
  555.         rep     movsd
  556.  
  557. ; GRAPHICS ADDRESSES
  558.  
  559.         mov     byte [BOOT_VAR+0x901e],0x0
  560.         mov     eax,[BOOT_VAR+0x9018]
  561.         mov     [LFBAddress],eax
  562.  
  563.         cmp     [SCR_MODE],word 0100000000000000b
  564.         jge     setvesa20
  565.         cmp     [SCR_MODE],word 0x13
  566.         je      v20ga32
  567.         mov     [PUTPIXEL],dword Vesa12_putpixel24  ; Vesa 1.2
  568.         mov     [GETPIXEL],dword Vesa12_getpixel24
  569.         cmp     [ScreenBPP],byte 24
  570.         jz      ga24
  571.         mov     [PUTPIXEL],dword Vesa12_putpixel32
  572.         mov     [GETPIXEL],dword Vesa12_getpixel32
  573.       ga24:
  574.         jmp     v20ga24
  575.       setvesa20:
  576.         mov     [PUTPIXEL],dword Vesa20_putpixel24  ; Vesa 2.0
  577.         mov     [GETPIXEL],dword Vesa20_getpixel24
  578.         cmp     [ScreenBPP],byte 24
  579.         jz      v20ga24
  580.       v20ga32:
  581.         mov     [PUTPIXEL],dword Vesa20_putpixel32
  582.         mov     [GETPIXEL],dword Vesa20_getpixel32
  583.       v20ga24:
  584.         cmp     [SCR_MODE],word 0x12                ; 16 C VGA 640x480
  585.         jne     no_mode_0x12
  586.         mov     [PUTPIXEL],dword VGA_putpixel
  587.         mov     [GETPIXEL],dword Vesa20_getpixel32
  588.       no_mode_0x12:
  589.  
  590. ; -------- Fast System Call init ----------
  591. ; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
  592.            bt [cpu_caps], CAPS_SEP
  593.            jnc .SEnP   ; SysEnter not Present
  594.            xor edx, edx
  595.            mov ecx, MSR_SYSENTER_CS
  596.            mov eax, os_code
  597.            wrmsr
  598.            mov ecx, MSR_SYSENTER_ESP
  599. ;           mov eax, sysenter_stack ; Check it
  600.            xor     eax, eax
  601.            wrmsr
  602.            mov ecx, MSR_SYSENTER_EIP
  603.            mov eax, sysenter_entry
  604.            wrmsr
  605. .SEnP:
  606. ; AMD SYSCALL/SYSRET
  607.            cmp byte[cpu_vendor], 'A'
  608.            jne .noSYSCALL
  609.            mov eax, 0x80000001
  610.            cpuid
  611.            test edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
  612.            jz .noSYSCALL
  613.            mov ecx, MSR_AMD_EFER
  614.            rdmsr
  615.            or eax, 1   ; bit_0 - System Call Extension (SCE)
  616.            wrmsr
  617.  
  618.         ; !!!! It`s dirty hack, fix it !!!
  619.         ; Bits of EDX :
  620.         ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
  621.         ;  and the contents of this field, plus 8, are copied into the SS register.
  622.         ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
  623.         ;  and the contents of this field, plus 8, are copied into the SS register.
  624.  
  625.         ; mov   edx, (os_code + 16) * 65536 + os_code
  626.            mov edx, 0x1B0008
  627.  
  628.            mov eax, syscall_entry
  629.            mov ecx, MSR_AMD_STAR
  630.            wrmsr
  631. .noSYSCALL:
  632. ; -----------------------------------------
  633.  
  634. ; LOAD IDT
  635.  
  636.            call build_interrupt_table
  637.            lidt [idtreg]
  638.  
  639.            mov [LFBSize], 0x800000
  640.            call init_LFB
  641.            call init_fpu
  642.            call init_malloc
  643.  
  644.            stdcall alloc_kernel_space, 0x51000
  645.            mov [default_io_map], eax
  646.  
  647.            add eax, 0x2000
  648.            mov [ipc_tmp], eax
  649.            mov ebx, 0x1000
  650.  
  651.            add eax, 0x40000
  652.            mov [proc_mem_map], eax
  653.  
  654.            add eax, 0x8000
  655.            mov [proc_mem_pdir], eax
  656.  
  657.            add eax, ebx
  658.            mov [proc_mem_tab], eax
  659.  
  660.            add eax, ebx
  661.            mov [tmp_task_pdir], eax
  662.  
  663.            add eax, ebx
  664.            mov [tmp_task_ptab], eax
  665.  
  666.            add eax, ebx
  667.            mov [ipc_pdir], eax
  668.  
  669.            add eax, ebx
  670.            mov [ipc_ptab], eax
  671.  
  672.            call init_events
  673.            mov eax, srv.fd-SRV_FD_OFFSET
  674.            mov [srv.fd], eax
  675.            mov [srv.bk], eax
  676.  
  677.            mov edi, irq_tab
  678.            xor eax, eax
  679.            mov ecx, 16
  680.            rep stosd
  681.  
  682. ;Set base of graphic segment to linear address of LFB
  683.         mov     eax,[LFBAddress]          ; set for gs
  684.         mov     [graph_data_l+2],ax
  685.         shr     eax,16
  686.         mov     [graph_data_l+4],al
  687.         mov     [graph_data_l+7],ah
  688.  
  689.         mov [CURRENT_TASK],dword 1
  690.         mov [TASK_COUNT],dword 1
  691.         mov [TASK_BASE],dword TASK_DATA
  692.         mov [current_slot], SLOT_BASE+256
  693.  
  694. ; set background
  695.         xor  eax,eax
  696.         inc  eax
  697.         mov   [BgrDrawMode],eax
  698.         mov   [BgrDataWidth],eax
  699.         mov   [BgrDataHeight],eax
  700.         mov    [mem_BACKGROUND],4095
  701.         stdcall kernel_alloc, [mem_BACKGROUND]
  702.         mov [img_background], eax
  703.  
  704.         mov     [SLOT_BASE + 256 + APPDATA.dir_table], sys_pgdir - OS_BASE
  705.  
  706. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  707.  
  708.         call  rerouteirqs
  709.  
  710. ; Initialize system V86 machine
  711.         call    init_sys_v86
  712.  
  713. ; TIMER SET TO 1/100 S
  714.  
  715.         mov   al,0x34              ; set to 100Hz
  716.         out   0x43,al
  717.         mov   al,0x9b              ; lsb    1193180 / 1193
  718.         out   0x40,al
  719.         mov   al,0x2e              ; msb
  720.         out   0x40,al
  721.  
  722. ; Enable timer IRQ (IRQ0) and hard drives IRQs (IRQ14, IRQ15)
  723. ; they are used: when partitions are scanned, hd_read relies on timer
  724.         mov     al, 0xFE
  725.         out     0x21, al
  726.         mov     al, 0x3F
  727.         out     0xA1, al
  728.  
  729. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  730. include 'detect/disks.inc'
  731. ;!!!!!!!!!!!!!!!!!!!!!!!!!!
  732.  
  733.   call Parser_params
  734.  
  735. ; READ RAMDISK IMAGE FROM HD
  736.  
  737. ;!!!!!!!!!!!!!!!!!!!!!!!
  738. ;include 'boot/rdload.inc'
  739. ;!!!!!!!!!!!!!!!!!!!!!!!
  740. ;    mov    [dma_hdd],1
  741. ; CALCULATE FAT CHAIN FOR RAMDISK
  742.  
  743.  
  744.         call  calculatefatchain
  745.  
  746.  
  747.  ; mov ax,[OS_BASE+0x10000+bx_from_load]
  748.  ; cmp ax,'r1'           ; if using not ram disk, then load librares and parameters {SPraid.simba}
  749.  ; je  no_lib_load
  750. ; LOADING LIBRARES
  751. ;   stdcall dll.Load,@IMPORT                 ; loading librares for kernel (.obj files)
  752. ;   call load_file_parse_table               ; prepare file parse table
  753. ;   call set_kernel_conf                     ; configure devices and gui
  754. ;no_lib_load:
  755.  
  756. ; LOAD FONTS I and II
  757.  
  758.         stdcall read_file, char, FONT_I, 0, 2304
  759.         stdcall read_file, char2, FONT_II, 0, 2560
  760.  
  761.         mov   esi,boot_fonts
  762.         call  boot_log
  763.  
  764. ; PRINT AMOUNT OF MEMORY
  765.         mov     esi, boot_memdetect
  766.         call    boot_log
  767.  
  768.         movzx   ecx, word [boot_y]
  769.         or      ecx, (10+29*6) shl 16 ; "Determining amount of memory"
  770.         sub     ecx, 10
  771.         mov     edx, 0xFFFFFF
  772.         mov     ebx, [_mem_amount]
  773.         shr     ebx, 20
  774.         mov     edi, 1
  775.         mov     eax, 0x00040000
  776.         call    display_number_force
  777.  
  778. ; BUILD SCHEDULER
  779.  
  780.         call   build_scheduler ; sys32.inc
  781.  
  782.         mov    esi,boot_devices
  783.         call   boot_log
  784.  
  785.         mov  [pci_access_enabled],1
  786.  
  787.  
  788. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  789.  
  790.         mov   esi,boot_windefs
  791.         call  boot_log
  792.         call  setwindowdefaults
  793.  
  794. ; SET BACKGROUND DEFAULTS
  795.  
  796.         mov   esi,boot_bgr
  797.         call  boot_log
  798.         call  init_background
  799.         call  calculatebackground
  800.  
  801. ; RESERVE SYSTEM IRQ'S JA PORT'S
  802.  
  803.         mov   esi,boot_resirqports
  804.         call  boot_log
  805.         call  reserve_irqs_ports
  806.  
  807. ; SET PORTS FOR IRQ HANDLERS
  808.  
  809.         mov  esi,boot_setrports
  810.         call boot_log
  811.         ;call setirqreadports
  812.  
  813. ; SET UP OS TASK
  814.  
  815.         mov  esi,boot_setostask
  816.         call boot_log
  817.  
  818.         xor  eax, eax
  819.         mov  dword [SLOT_BASE+APPDATA.fpu_state], fpu_data
  820.         mov  dword [SLOT_BASE+APPDATA.fpu_handler], eax
  821.         mov  dword [SLOT_BASE+APPDATA.sse_handler], eax
  822.  
  823.         ; name for OS/IDLE process
  824.  
  825.         mov dword [SLOT_BASE+256+APPDATA.app_name],   dword 'OS/I'
  826.         mov dword [SLOT_BASE+256+APPDATA.app_name+4], dword 'DLE '
  827.         mov edi,  __os_stack-8192+512
  828.         mov dword [SLOT_BASE+256+APPDATA.pl0_stack], edi
  829.         add edi, 0x2000-512
  830.         mov dword [SLOT_BASE+256+APPDATA.fpu_state], edi
  831.         mov dword [SLOT_BASE+256+APPDATA.saved_esp0], edi ; just for case
  832.         mov dword [SLOT_BASE+256+APPDATA.io_map],\
  833.                   (tss._io_map_0-OS_BASE+PG_MAP)
  834.         mov dword [SLOT_BASE+256+APPDATA.io_map+4],\
  835.                   (tss._io_map_1-OS_BASE+PG_MAP)
  836.  
  837.         mov esi, fpu_data
  838.         mov ecx, 512/4
  839.         cld
  840.         rep movsd
  841.  
  842.         mov dword [SLOT_BASE+256+APPDATA.fpu_handler], eax
  843.         mov dword [SLOT_BASE+256+APPDATA.sse_handler], eax
  844.  
  845.         mov ebx, SLOT_BASE+256+APP_OBJ_OFFSET
  846.         mov  dword [SLOT_BASE+256+APPDATA.fd_obj], ebx
  847.         mov  dword [SLOT_BASE+256+APPDATA.bk_obj], ebx
  848.  
  849.         mov  dword [SLOT_BASE+256+APPDATA.cur_dir], sysdir_path
  850.  
  851.         ; task list
  852.         mov  [CURRENT_TASK],dword 1
  853.         mov  [TASK_COUNT],dword 1
  854.         mov  [current_slot], SLOT_BASE+256
  855.         mov  [TASK_BASE],dword TASK_DATA
  856.         mov  [TASK_DATA+TASKDATA.wnd_number], 1 ; on screen number
  857.         mov  [TASK_DATA+TASKDATA.pid], 1        ; process id number
  858.         mov  [TASK_DATA+TASKDATA.mem_start], 0  ; process base address
  859.  
  860.         call init_cursors
  861.         mov eax, [def_cursor]
  862.         mov [SLOT_BASE+APPDATA.cursor],eax
  863.         mov [SLOT_BASE+APPDATA.cursor+256],eax
  864.  
  865.        ; stdcall load_pe_driver, szAtiHW
  866.  
  867.   ; READ TSC / SECOND
  868.  
  869.         mov   esi,boot_tsc
  870.         call  boot_log
  871.         cli
  872.         call  _rdtsc
  873.         mov   ecx,eax
  874.         mov   esi,250               ; wait 1/4 a second
  875.         call  delay_ms
  876.         call  _rdtsc
  877.         sti
  878.         sub   eax,ecx
  879.         shl   eax,2
  880.         mov   [CPU_FREQ],eax          ; save tsc / sec
  881.         mov ebx, 1000000
  882.         div ebx
  883.         mov [stall_mcs], eax
  884.  
  885. ; SET VARIABLES
  886.  
  887.         call  set_variables
  888.  
  889. ; SET MOUSE
  890.  
  891.         ;call   detect_devices
  892.         stdcall load_driver, szPS2MDriver
  893.        ; stdcall load_driver, szCOM_MDriver
  894.  
  895.         mov   esi,boot_setmouse
  896.         call  boot_log
  897.         call  setmouse
  898.  
  899.  
  900. ; STACK AND FDC
  901.  
  902.         call  stack_init
  903.         call  fdc_init
  904.  
  905. ; PALETTE FOR 320x200 and 640x480 16 col
  906.  
  907.         cmp   [SCR_MODE],word 0x12
  908.         jne   no_pal_vga
  909.         mov   esi,boot_pal_vga
  910.         call  boot_log
  911.         call  paletteVGA
  912.       no_pal_vga:
  913.  
  914.         cmp   [SCR_MODE],word 0x13
  915.         jne   no_pal_ega
  916.         mov   esi,boot_pal_ega
  917.         call  boot_log
  918.         call  palette320x200
  919.       no_pal_ega:
  920.  
  921. ; LOAD DEFAULT SKIN
  922.  
  923.         call    load_default_skin
  924.  
  925. ;protect io permission map
  926.  
  927.            mov esi, [default_io_map]
  928.            stdcall map_page,esi,(tss._io_map_0-OS_BASE), PG_MAP
  929.            add esi, 0x1000
  930.            stdcall map_page,esi,(tss._io_map_1-OS_BASE), PG_MAP
  931.  
  932.            stdcall map_page,tss._io_map_0,\
  933.                    (tss._io_map_0-OS_BASE), PG_MAP
  934.            stdcall map_page,tss._io_map_1,\
  935.                    (tss._io_map_1-OS_BASE), PG_MAP
  936.  
  937. ;  mov ax,[OS_BASE+0x10000+bx_from_load]
  938. ;  cmp ax,'r1'           ; if not rused ram disk - load network configuration from files {SPraid.simba}
  939. ;  je  no_st_network
  940. ;        call set_network_conf
  941. ;  no_st_network:
  942.  
  943. ; LOAD FIRST APPLICATION
  944.         cli
  945.  
  946.         cmp   byte [BOOT_VAR+0x9030],1
  947.         jne   no_load_vrr_m
  948.  
  949.         mov     ebp, vrr_m
  950.         call    fs_execute_from_sysdir
  951.  
  952.         cmp   eax,2                  ; if vrr_m app found (PID=2)
  953.         je    first_app_found
  954.  
  955. no_load_vrr_m:
  956.  
  957.  
  958.         mov     ebp, firstapp
  959.         call    fs_execute_from_sysdir
  960.  
  961.         cmp   eax,2                  ; continue if a process has been loaded
  962.         je    first_app_found
  963.  
  964.         mov     esi, boot_failed
  965.         call    boot_log
  966.  
  967.         mov   eax, 0xDEADBEEF        ; otherwise halt
  968.         hlt
  969.  
  970. first_app_found:
  971.  
  972.         cli
  973.  
  974.         ;mov   [TASK_COUNT],dword 2
  975.         mov   [CURRENT_TASK],dword 1       ; set OS task fisrt
  976.  
  977. ; SET KEYBOARD PARAMETERS
  978.         mov   al, 0xf6         ; reset keyboard, scan enabled
  979.         call  kb_write
  980.  
  981.         ; wait until 8042 is ready
  982.         xor ecx,ecx
  983. @@:
  984.         in     al,64h
  985.         and    al,00000010b
  986.         loopnz @b
  987.  
  988.         mov   al, 0xF3       ; set repeat rate & delay
  989.         call  kb_write
  990. ;        call  kb_read
  991.         mov   al, 0 ; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  992.         call  kb_write
  993. ;        call  kb_read
  994.      ;// mike.dld [
  995.         call  set_lights
  996.      ;// mike.dld ]
  997.  
  998. ; START MULTITASKING
  999.  
  1000. ;if preboot_blogesc
  1001. ;        mov     esi, boot_tasking
  1002. ;        call    boot_log
  1003. ;.bll1:  in      al, 0x60        ; wait for ESC key press
  1004. ;        cmp     al, 129
  1005. ;        jne     .bll1
  1006. ;end if
  1007.  
  1008. ;       mov   [ENABLE_TASKSWITCH],byte 1        ; multitasking enabled
  1009.  
  1010. ; UNMASK ALL IRQ'S
  1011.  
  1012.         mov   esi,boot_allirqs
  1013.         call  boot_log
  1014.  
  1015.         cli                          ;guarantee forbidance of interrupts.
  1016.         mov   al,0                   ; unmask all irq's
  1017.         out   0xA1,al
  1018.         out   0x21,al
  1019.  
  1020.         mov   ecx,32
  1021.  
  1022.      ready_for_irqs:
  1023.  
  1024.         mov   al,0x20                ; ready for irqs
  1025.         out   0x20,al
  1026.         out   0xa0,al
  1027.  
  1028.         loop  ready_for_irqs         ; flush the queue
  1029.  
  1030.         stdcall attach_int_handler, dword 1, irq1, dword 0
  1031.  
  1032. ;        mov    [dma_hdd],1
  1033.         cmp     [IDEContrRegsBaseAddr], 0
  1034.         setnz   [dma_hdd]
  1035.         mov [timer_ticks_enable],1              ; for cd driver
  1036.  
  1037. ;        stdcall init_uart_service, DRV_ENTRY
  1038.  
  1039.         sti
  1040.         call change_task
  1041.  
  1042.         jmp osloop
  1043.  
  1044. ;        jmp   $                      ; wait here for timer to take control
  1045.  
  1046.         ; Fly :)
  1047.  
  1048. include 'unpacker.inc'
  1049. include 'fdo.inc'
  1050.  
  1051. align 4
  1052. boot_log:
  1053.          pushad
  1054.  
  1055.          mov   ebx,10*65536
  1056.          mov   bx,word [boot_y]
  1057.          add   [boot_y],dword 10
  1058.          mov   ecx,0x80ffffff   ; ASCIIZ string with white color
  1059.          mov   edx,esi
  1060.          mov   edi,1
  1061.          call  dtext
  1062.  
  1063.          mov   [novesachecksum],1000
  1064.          call  checkVga_N13
  1065.  
  1066.          popad
  1067.  
  1068.          ret
  1069.  
  1070.  
  1071. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1072. ;                                                                    ;
  1073. ;                    MAIN OS LOOP START                              ;
  1074. ;                                                                    ;
  1075. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1076. align 32
  1077. osloop:
  1078.         call   [draw_pointer]
  1079.         call   checkbuttons
  1080.         call   checkwindows
  1081. ;       call   check_window_move_request
  1082.         call   checkmisc
  1083.         call   checkVga_N13
  1084.         call   stack_handler
  1085.         call   checkidle
  1086.         call   check_fdd_motor_status
  1087.         call   check_ATAPI_device_event
  1088.         jmp    osloop
  1089. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1090. ;                                                                    ;
  1091. ;                      MAIN OS LOOP END                              ;
  1092. ;                                                                    ;
  1093. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1094.  
  1095. checkidle:
  1096.         pushad
  1097.  
  1098.         cmp  [check_idle_semaphore],0
  1099.         jne  no_idle_state
  1100.  
  1101.         call change_task
  1102.         mov  eax,[idlemem]
  1103.         mov  ebx,[timer_ticks] ;[0xfdf0]
  1104.         cmp  eax,ebx
  1105.         jnz  idle_exit
  1106.         call _rdtsc
  1107.         mov  ecx,eax
  1108.       idle_loop:
  1109.         hlt
  1110.         cmp  [check_idle_semaphore],0
  1111.         jne  idle_loop_exit
  1112.         mov  eax,[timer_ticks] ;[0xfdf0]
  1113.         cmp  ebx,eax
  1114.         jz   idle_loop
  1115.       idle_loop_exit:
  1116.         mov  [idlemem],eax
  1117.         call _rdtsc
  1118.         sub  eax,ecx
  1119.         mov  ebx,[idleuse]
  1120.         add  ebx,eax
  1121.         mov  [idleuse],ebx
  1122.  
  1123.         popad
  1124.         ret
  1125.  
  1126.       idle_exit:
  1127.  
  1128.         mov  ebx,[timer_ticks] ;[0xfdf0]
  1129.         mov  [idlemem],ebx
  1130.         call change_task
  1131.  
  1132.         popad
  1133.         ret
  1134.  
  1135.       no_idle_state:
  1136.  
  1137.         dec  [check_idle_semaphore]
  1138.  
  1139.         mov  ebx,[timer_ticks] ;[0xfdf0]
  1140.         mov  [idlemem],ebx
  1141.         call change_task
  1142.  
  1143.         popad
  1144.         ret
  1145.  
  1146. uglobal
  1147.   idlemem               dd   0x0
  1148.   idleuse               dd   0x0
  1149.   idleusesec            dd   0x0
  1150.   check_idle_semaphore  dd   0x0
  1151. endg
  1152.  
  1153.  
  1154.  
  1155. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1156. ;                                                                      ;
  1157. ;                   INCLUDED SYSTEM FILES                              ;
  1158. ;                                                                      ;
  1159. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1160.  
  1161.  
  1162. include "kernel32.inc"
  1163.  
  1164.  
  1165. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1166. ;                                                                      ;
  1167. ;                       KERNEL FUNCTIONS                               ;
  1168. ;                                                                      ;
  1169. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1170.  
  1171. reserve_irqs_ports:
  1172.  
  1173.         pushad
  1174.  
  1175.         mov  [irq_owner+4*0], 1    ; timer
  1176.         ;mov  [irq_owner+4*1], 1    ; keyboard
  1177.         mov  [irq_owner+4*6], 1    ; floppy diskette
  1178.         mov  [irq_owner+4*13], 1   ; math co-pros
  1179.         mov  [irq_owner+4*14], 1   ; ide I
  1180.         mov  [irq_owner+4*15], 1   ; ide II
  1181.  
  1182.         ; RESERVE PORTS
  1183.         mov   edi,1                    ; 0x00-0x2d
  1184.         mov   [RESERVED_PORTS],edi
  1185.         shl   edi,4
  1186.         mov   [RESERVED_PORTS+edi+0],dword 1
  1187.         mov   [RESERVED_PORTS+edi+4],dword 0x0
  1188.         mov   [RESERVED_PORTS+edi+8],dword 0x2d
  1189.  
  1190.         inc   dword [RESERVED_PORTS]          ; 0x30-0x4d
  1191.         mov   edi,[RESERVED_PORTS]
  1192.         shl   edi,4
  1193.         mov   [RESERVED_PORTS+edi+0],dword 1
  1194.         mov   [RESERVED_PORTS+edi+4],dword 0x30
  1195.         mov   [RESERVED_PORTS+edi+8],dword 0x4d
  1196.  
  1197.         inc   dword [RESERVED_PORTS]          ; 0x50-0xdf
  1198.         mov   edi,[RESERVED_PORTS]
  1199.         shl   edi,4
  1200.         mov   [RESERVED_PORTS+edi+0],dword 1
  1201.         mov   [RESERVED_PORTS+edi+4],dword 0x50
  1202.         mov   [RESERVED_PORTS+edi+8],dword 0xdf
  1203.  
  1204.         inc   dword [RESERVED_PORTS]          ; 0xe5-0xff
  1205.         mov   edi,[RESERVED_PORTS]
  1206.         shl   edi,4
  1207.         mov   [RESERVED_PORTS+edi+0],dword 1
  1208.         mov   [RESERVED_PORTS+edi+4],dword 0xe5
  1209.         mov   [RESERVED_PORTS+edi+8],dword 0xff
  1210.  
  1211.         popad
  1212.         ret
  1213.  
  1214. setirqreadports:
  1215.  
  1216.         mov   [irq12read+0],dword 0x60 + 0x01000000  ; read port 0x60 , byte
  1217.         mov   [irq12read+4],dword 0                  ; end of port list
  1218.         ;mov   [irq04read+0],dword 0x3f8 + 0x01000000 ; read port 0x3f8 , byte
  1219.         ;mov   [irq04read+4],dword 0                  ; end of port list
  1220.         ;mov   [irq03read+0],dword 0x2f8 + 0x01000000 ; read port 0x2f8 , byte
  1221.         ;mov   [irq03read+4],dword 0                  ; end of port list
  1222.  
  1223.         ret
  1224.  
  1225. iglobal
  1226.   process_number dd 0x1
  1227. endg
  1228.  
  1229. set_variables:
  1230.  
  1231.         mov   ecx,0x100                       ; flush port 0x60
  1232. .fl60:  in    al,0x60
  1233.         loop  .fl60
  1234.         mov   [MOUSE_BUFF_COUNT],byte 0                 ; mouse buffer
  1235.         mov   [KEY_COUNT],byte 0                 ; keyboard buffer
  1236.         mov   [BTN_COUNT],byte 0                 ; button buffer
  1237. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  1238.  
  1239.         push  eax
  1240.         mov   ax,[BOOT_VAR+0x900c]
  1241.         shr   ax,1
  1242.         shl   eax,16
  1243.         mov   ax,[BOOT_VAR+0x900A]
  1244.         shr   ax,1
  1245.         mov   [MOUSE_X],eax
  1246.         pop   eax
  1247.  
  1248.         mov   [BTN_ADDR],dword BUTTON_INFO    ; address of button list
  1249.  
  1250.      ;!! IP 04.02.2005:
  1251.         mov   [next_usage_update], 100
  1252.         mov   byte [DONT_SWITCH], 0 ; change task if possible
  1253.  
  1254.         ret
  1255.  
  1256. ;* mouse centered - start code- Mario79
  1257. mouse_centered:
  1258.         push  eax
  1259.         mov   eax,[Screen_Max_X]
  1260.         shr   eax,1
  1261.         mov   [MOUSE_X],ax
  1262.         mov   eax,[Screen_Max_Y]
  1263.         shr   eax,1
  1264.         mov   [MOUSE_Y],ax
  1265.         pop   eax
  1266.         ret
  1267. ;* mouse centered - end code- Mario79
  1268.  
  1269. align 4
  1270.  
  1271. sys_outport:
  1272.  
  1273.     mov   edi,ebx          ; separate flag for read / write
  1274.     and   ebx,65535
  1275.  
  1276.     mov   ecx,[RESERVED_PORTS]
  1277.     test  ecx,ecx
  1278.     jne   sopl8
  1279.     mov   [esp+36],dword 1
  1280.     ret
  1281.  
  1282.   sopl8:
  1283.     mov   edx,[TASK_BASE]
  1284.     mov   edx,[edx+0x4]
  1285.     and   ebx,65535
  1286.     cld
  1287.   sopl1:
  1288.  
  1289.     mov   esi,ecx
  1290.     shl   esi,4
  1291.     add   esi,RESERVED_PORTS
  1292.     cmp   edx,[esi+0]
  1293.     jne   sopl2
  1294.     cmp   ebx,[esi+4]
  1295.     jb    sopl2
  1296.     cmp   ebx,[esi+8]
  1297.     jg    sopl2
  1298.     jmp   sopl3
  1299.  
  1300.   sopl2:
  1301.  
  1302.     dec   ecx
  1303.     jnz   sopl1
  1304.     mov   [esp+36],dword 1
  1305.     ret
  1306.  
  1307.   sopl3:
  1308.  
  1309.     test  edi,0x80000000 ; read ?
  1310.     jnz   sopl4
  1311.  
  1312.     mov   dx,bx          ; write
  1313.     out   dx,al
  1314.     mov   [esp+36],dword 0
  1315.     ret
  1316.  
  1317.   sopl4:
  1318.  
  1319.     mov   dx,bx          ; read
  1320.     in    al,dx
  1321.     and   eax,0xff
  1322.     mov   [esp+36],dword 0
  1323.     mov   [esp+24],eax
  1324.     ret
  1325.  
  1326. display_number:
  1327.  
  1328. ; eax = print type, al=0 -> ebx is number
  1329. ;                   al=1 -> ebx is pointer
  1330. ;                   ah=0 -> display decimal
  1331. ;                   ah=1 -> display hexadecimal
  1332. ;                   ah=2 -> display binary
  1333. ;                   eax bits 16-21 = number of digits to display (0-32)
  1334. ;                   eax bits 22-31 = reserved
  1335. ;
  1336. ; ebx = number or pointer
  1337. ; ecx = x shl 16 + y
  1338. ; edx = color
  1339.         xor     edi, edi
  1340. display_number_force:
  1341.      push  eax
  1342.      and   eax,0x3fffffff
  1343.      cmp   eax,0xffff            ; length > 0 ?
  1344.      pop   eax
  1345.      jge   cont_displ
  1346.      ret
  1347.    cont_displ:
  1348.      push  eax
  1349.      and   eax,0x3fffffff
  1350.      cmp   eax,61*0x10000        ; length <= 60 ?
  1351.      pop   eax
  1352.      jb    cont_displ2
  1353.      ret
  1354.    cont_displ2:
  1355.  
  1356.      pushad
  1357.  
  1358.      cmp   al,1                  ; ecx is a pointer ?
  1359.      jne   displnl1
  1360.      mov   ebp,ebx
  1361.      add   ebp,4
  1362.      mov   ebp,[ebp]
  1363.      mov   ebx,[ebx]
  1364.    displnl1:
  1365.      sub   esp,64
  1366.  
  1367.      cmp   ah,0                  ; DECIMAL
  1368.      jne   no_display_desnum
  1369.      shr   eax,16
  1370.      and   eax,0xC03f
  1371. ;     and   eax,0x3f
  1372.      push  eax
  1373.      and   eax,0x3f
  1374.      mov   edi,esp
  1375.      add   edi,4+64-1
  1376.      mov   ecx,eax
  1377.      mov   eax,ebx
  1378.      mov   ebx,10
  1379.    d_desnum:
  1380.      xor   edx,edx
  1381.      call  division_64_bits
  1382.      div   ebx
  1383.      add   dl,48
  1384.      mov   [edi],dl
  1385.      dec   edi
  1386.      loop  d_desnum
  1387.      pop   eax
  1388.      call  normalize_number
  1389.      call  draw_num_text
  1390.      add   esp,64
  1391.      popad
  1392.      ret
  1393.    no_display_desnum:
  1394.  
  1395.      cmp   ah,0x01               ; HEXADECIMAL
  1396.      jne   no_display_hexnum
  1397.      shr   eax,16
  1398.      and   eax,0xC03f
  1399. ;     and   eax,0x3f
  1400.      push  eax
  1401.      and   eax,0x3f
  1402.      mov   edi,esp
  1403.      add   edi,4+64-1
  1404.      mov   ecx,eax
  1405.      mov   eax,ebx
  1406.      mov   ebx,16
  1407.    d_hexnum:
  1408.      xor   edx,edx
  1409.      call  division_64_bits
  1410.      div   ebx
  1411.      add   edx,hexletters
  1412.      mov   dl,[edx]
  1413.      mov   [edi],dl
  1414.      dec   edi
  1415.      loop  d_hexnum
  1416.      pop   eax
  1417.      call  normalize_number
  1418.      call  draw_num_text
  1419.      add   esp,64
  1420.      popad
  1421.      ret
  1422.    no_display_hexnum:
  1423.  
  1424.      cmp   ah,0x02               ; BINARY
  1425.      jne   no_display_binnum
  1426.      shr   eax,16
  1427.      and   eax,0xC03f
  1428. ;     and   eax,0x3f
  1429.      push  eax
  1430.      and   eax,0x3f
  1431.      mov   edi,esp
  1432.      add   edi,4+64-1
  1433.      mov   ecx,eax
  1434.      mov   eax,ebx
  1435.      mov   ebx,2
  1436.    d_binnum:
  1437.      xor   edx,edx
  1438.      call  division_64_bits
  1439.      div   ebx
  1440.      add   dl,48
  1441.      mov   [edi],dl
  1442.      dec   edi
  1443.      loop  d_binnum
  1444.      pop   eax
  1445.      call  normalize_number
  1446.      call  draw_num_text
  1447.      add   esp,64
  1448.      popad
  1449.      ret
  1450.    no_display_binnum:
  1451.  
  1452.      add   esp,64
  1453.      popad
  1454.      ret
  1455.  
  1456. normalize_number:
  1457.      test  ah,0x80
  1458.      jz   .continue
  1459.      mov  ecx,48
  1460.      and   eax,0x3f
  1461. @@:
  1462.      inc   edi
  1463.      cmp   [edi],cl
  1464.      jne   .continue
  1465.      dec   eax
  1466.      cmp   eax,1
  1467.      ja    @r
  1468.      mov   al,1
  1469. .continue:
  1470.      and   eax,0x3f
  1471.      ret
  1472.  
  1473. division_64_bits:
  1474.      test  [esp+1+4],byte 0x40
  1475.      jz   .continue
  1476.      push  eax
  1477.      mov   eax,ebp
  1478.      div   ebx
  1479.      mov   ebp,eax
  1480.      pop   eax
  1481. .continue:
  1482.      ret
  1483.  
  1484. draw_num_text:
  1485.      mov   esi,eax
  1486.      mov   edx,64+4
  1487.      sub   edx,eax
  1488.      add   edx,esp
  1489.      mov   ebx,[esp+64+32-8+4]
  1490. ; add window start x & y
  1491.      mov   ecx,[TASK_BASE]
  1492.  
  1493.      mov   edi,[CURRENT_TASK]
  1494.      shl   edi,8
  1495.  
  1496.      mov   eax,[ecx-twdw+WDATA.box.left]
  1497.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1498.      shl   eax,16
  1499.      add   eax,[ecx-twdw+WDATA.box.top]
  1500.      add   eax,[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1501.      add   ebx,eax
  1502.      mov   ecx,[esp+64+32-12+4]
  1503.         and     ecx, not 0x80000000     ; force counted string
  1504.         mov     eax, [esp+64+8]         ; background color (if given)
  1505.         mov     edi, [esp+64+4]
  1506.      jmp   dtext
  1507.  
  1508. align 4
  1509.  
  1510. sys_setup:
  1511.  
  1512. ; 1=roland mpu midi base , base io address
  1513. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1514. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1515. ; 5=system language, 1eng 2fi 3ger 4rus
  1516. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1517. ; 8=fat32 partition in hd
  1518. ; 9
  1519. ; 10 = sound dma channel
  1520. ; 11 = enable lba read
  1521. ; 12 = enable pci access
  1522.  
  1523.  
  1524.      mov  [esp+36],dword 0
  1525.      cmp  eax,1                      ; MIDI
  1526.      jnz  nsyse1
  1527.      cmp  ebx,0x100
  1528.      jb   nsyse1
  1529.      mov  edx,65535
  1530.      cmp  edx,ebx
  1531.      jb   nsyse1
  1532.      mov  [midi_base],bx
  1533.      mov  word [mididp],bx
  1534.      inc  bx
  1535.      mov  word [midisp],bx
  1536.      ret
  1537.  
  1538. iglobal
  1539. midi_base dw 0
  1540. endg
  1541.  
  1542.    nsyse1:
  1543.  
  1544.      cmp  eax,2                      ; KEYBOARD
  1545.      jnz  nsyse2
  1546.      cmp  ebx,1
  1547.      jnz  kbnobase
  1548.      mov  edi,[TASK_BASE]
  1549.      add  ecx,[edi+TASKDATA.mem_start]
  1550.      mov  eax,ecx
  1551.      mov  ebx,keymap
  1552.      mov  ecx,128
  1553.      call memmove
  1554.      ret
  1555.    kbnobase:
  1556.      cmp  ebx,2
  1557.      jnz  kbnoshift
  1558.      mov  edi,[TASK_BASE]
  1559.      add  ecx,[edi+TASKDATA.mem_start]
  1560.      mov  eax,ecx
  1561.      mov  ebx,keymap_shift
  1562.      mov  ecx,128
  1563.      call memmove
  1564.      ret
  1565.    kbnoshift:
  1566.      cmp  ebx,3
  1567.      jne  kbnoalt
  1568.      mov  edi,[TASK_BASE]
  1569.      add  ecx,[edi+TASKDATA.mem_start]
  1570.      mov  eax,ecx
  1571.      mov  ebx,keymap_alt
  1572.      mov  ecx,128
  1573.      call memmove
  1574.      ret
  1575.    kbnoalt:
  1576.      cmp  ebx,9
  1577.      jnz  kbnocountry
  1578.      mov  word [keyboard],cx
  1579.      ret
  1580.    kbnocountry:
  1581.      mov  [esp+36],dword 1
  1582.      ret
  1583.    nsyse2:
  1584.      cmp  eax,3                      ; CD
  1585.      jnz  nsyse4
  1586.      test ebx,ebx
  1587.      jz   nosesl
  1588.      cmp  ebx, 4
  1589.      ja   nosesl
  1590.      mov  [cd_base],bl
  1591.      cmp  ebx,1
  1592.      jnz  noprma
  1593.      mov  [cdbase],0x1f0
  1594.      mov  [cdid],0xa0
  1595.    noprma:
  1596.      cmp  ebx,2
  1597.      jnz  noprsl
  1598.      mov  [cdbase],0x1f0
  1599.      mov  [cdid],0xb0
  1600.    noprsl:
  1601.      cmp  ebx,3
  1602.      jnz  nosema
  1603.      mov  [cdbase],0x170
  1604.      mov  [cdid],0xa0
  1605.    nosema:
  1606.      cmp  ebx,4
  1607.      jnz  nosesl
  1608.      mov  [cdbase],0x170
  1609.      mov  [cdid],0xb0
  1610.    nosesl:
  1611.      ret
  1612.  
  1613. cd_base db 0
  1614.  
  1615.    nsyse4:
  1616.  
  1617.      cmp  eax,5                      ; SYSTEM LANGUAGE
  1618.      jnz  nsyse5
  1619.      mov  [syslang],ebx
  1620.      ret
  1621.    nsyse5:
  1622.  
  1623.      cmp  eax,7                      ; HD BASE
  1624.      jne  nsyse7
  1625.      test ebx,ebx
  1626.      jz   nosethd
  1627.      cmp  ebx,4
  1628.      ja   nosethd
  1629.      mov  [hd_base],bl
  1630.      cmp  ebx,1
  1631.      jnz  noprmahd
  1632.      mov  [hdbase],0x1f0
  1633.      mov  [hdid],0x0
  1634.      mov  [hdpos],1
  1635. ;     call set_FAT32_variables
  1636.    noprmahd:
  1637.      cmp  ebx,2
  1638.      jnz  noprslhd
  1639.      mov  [hdbase],0x1f0
  1640.      mov  [hdid],0x10
  1641.      mov  [hdpos],2
  1642. ;     call set_FAT32_variables
  1643.    noprslhd:
  1644.      cmp  ebx,3
  1645.      jnz  nosemahd
  1646.      mov  [hdbase],0x170
  1647.      mov  [hdid],0x0
  1648.      mov  [hdpos],3
  1649. ;     call set_FAT32_variables
  1650.    nosemahd:
  1651.      cmp  ebx,4
  1652.      jnz  noseslhd
  1653.      mov  [hdbase],0x170
  1654.      mov  [hdid],0x10
  1655.      mov  [hdpos],4
  1656. ;     call set_FAT32_variables
  1657.    noseslhd:
  1658.     call  reserve_hd1
  1659.     call  reserve_hd_channel
  1660.     call  free_hd_channel
  1661.     mov   [hd1_status],0        ; free
  1662.    nosethd:
  1663.      ret
  1664.  
  1665. iglobal
  1666. hd_base db 0
  1667. endg
  1668.  
  1669. nsyse7:
  1670.  
  1671.      cmp  eax,8                      ; HD PARTITION
  1672.      jne  nsyse8
  1673.      mov  [fat32part],ebx
  1674. ;     call set_FAT32_variables
  1675.     call  reserve_hd1
  1676.     call  reserve_hd_channel
  1677.     call  free_hd_channel
  1678.      pusha
  1679.      call  choice_necessity_partition_1
  1680.      popa
  1681.     mov   [hd1_status],0        ; free
  1682.      ret
  1683.  
  1684. nsyse8:
  1685.      cmp  eax,11                     ; ENABLE LBA READ
  1686.      jne  no_set_lba_read
  1687.      and  ebx,1
  1688.      mov  [lba_read_enabled],ebx
  1689.      ret
  1690.  
  1691. no_set_lba_read:
  1692.      cmp  eax,12                     ; ENABLE PCI ACCESS
  1693.      jne  no_set_pci_access
  1694.      and  ebx,1
  1695.      mov  [pci_access_enabled],ebx
  1696.      ret
  1697.    no_set_pci_access:
  1698.  
  1699. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1700. include 'vmodeint.inc'
  1701. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1702.  
  1703. sys_setup_err:
  1704.      mov  [esp+36],dword -1
  1705.      ret
  1706.  
  1707. align 4
  1708.  
  1709. sys_getsetup:
  1710.  
  1711. ; 1=roland mpu midi base , base io address
  1712. ; 2=keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1713. ; 3=cd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1714. ; 5=system language, 1eng 2fi 3ger 4rus
  1715. ; 7=hd base    1, pri.master 2, pri slave 3 sec master, 4 sec slave
  1716. ; 8=fat32 partition in hd
  1717. ; 9=get hs timer tic
  1718.  
  1719.      cmp  eax,1
  1720.      jne  ngsyse1
  1721.      movzx eax,[midi_base]
  1722.      mov  [esp+36],eax
  1723.      ret
  1724. ngsyse1:
  1725.  
  1726.      cmp  eax,2
  1727.      jne  ngsyse2
  1728.      cmp  ebx,1
  1729.      jnz  kbnobaseret
  1730.      mov  edi,[TASK_BASE]
  1731.      add  ecx,[edi+TASKDATA.mem_start]
  1732.      mov  ebx,ecx
  1733.      mov  eax,keymap
  1734.      mov  ecx,128
  1735.      call memmove
  1736.      ret
  1737. kbnobaseret:
  1738.      cmp  ebx,2
  1739.      jnz  kbnoshiftret
  1740.      mov  edi,[TASK_BASE]
  1741.      add  ecx,[edi+TASKDATA.mem_start]
  1742.      mov  ebx,ecx
  1743.      mov  eax,keymap_shift
  1744.      mov  ecx,128
  1745.      call memmove
  1746.      ret
  1747. kbnoshiftret:
  1748.      cmp  ebx,3
  1749.      jne  kbnoaltret
  1750.      mov  edi,[TASK_BASE]
  1751.      add  ecx,[edi+TASKDATA.mem_start]
  1752.      mov  ebx,ecx
  1753.      mov  eax,keymap_alt
  1754.      mov  ecx,128
  1755.      call memmove
  1756.      ret
  1757. kbnoaltret:
  1758.      cmp  ebx,9
  1759.      jnz  ngsyse2
  1760.      movzx eax,word [keyboard]
  1761.      mov  [esp+36],eax
  1762.      ret
  1763. ngsyse2:
  1764.  
  1765.          cmp  eax,3
  1766.          jnz  ngsyse3
  1767.          movzx eax,[cd_base]
  1768.          mov  [esp+36],eax
  1769.          ret
  1770. ngsyse3:
  1771.          cmp  eax,5
  1772.          jnz  ngsyse5
  1773.          mov  eax,[syslang]
  1774.          mov  [esp+36],eax
  1775.          ret
  1776. ngsyse5:
  1777.      cmp  eax,7
  1778.      jnz  ngsyse7
  1779.      movzx eax,[hd_base]
  1780.      mov  [esp+36],eax
  1781.      ret
  1782. ngsyse7:
  1783.      cmp  eax,8
  1784.      jnz  ngsyse8
  1785.      mov eax,[fat32part]
  1786.      mov  [esp+36],eax
  1787.      ret
  1788. ngsyse8:
  1789.      cmp  eax,9
  1790.      jne  ngsyse9
  1791.      mov  eax,[timer_ticks] ;[0xfdf0]
  1792.      mov  [esp+36],eax
  1793.      ret
  1794. ngsyse9:
  1795.      cmp  eax,11
  1796.      jnz  ngsyse11
  1797.      mov eax,[lba_read_enabled]
  1798.      mov  [esp+36],eax
  1799.      ret
  1800. ngsyse11:
  1801.      cmp  eax,12
  1802.      jnz  ngsyse12
  1803.      mov eax,[pci_access_enabled]
  1804.      mov  [esp+36],eax
  1805.      ret
  1806. ngsyse12:
  1807.      mov  [esp+36],dword 1
  1808.      ret
  1809.  
  1810. get_timer_ticks:
  1811.     mov eax,[timer_ticks]
  1812.     ret
  1813.  
  1814. iglobal
  1815. align 4
  1816. mousefn dd msscreen, mswin, msbutton, msset
  1817.         dd app_load_cursor
  1818.         dd app_set_cursor
  1819.         dd app_delete_cursor
  1820.         dd msz
  1821. endg
  1822.  
  1823. readmousepos:
  1824.  
  1825. ; eax=0 screen relative
  1826. ; eax=1 window relative
  1827. ; eax=2 buttons pressed
  1828. ; eax=3 set mouse pos   ; reserved
  1829. ; eax=4 load cursor
  1830. ; eax=5 set cursor
  1831. ; eax=6 delete cursor   ; reserved
  1832. ; eax=7 get mouse_z
  1833.  
  1834.            cmp eax, 7
  1835.            ja msset
  1836.            jmp [mousefn+eax*4]
  1837. msscreen:
  1838.            mov  eax,[MOUSE_X]
  1839.            shl  eax,16
  1840.            mov  ax,[MOUSE_Y]
  1841.            mov  [esp+36],eax
  1842.            ret
  1843. mswin:
  1844.            mov  eax,[MOUSE_X]
  1845.            shl  eax,16
  1846.            mov  ax,[MOUSE_Y]
  1847.            mov  esi,[TASK_BASE]
  1848.            mov  bx, word [esi-twdw+WDATA.box.left]
  1849.            shl  ebx,16
  1850.            mov  bx, word [esi-twdw+WDATA.box.top]
  1851.            sub  eax,ebx
  1852.  
  1853.            mov  edi,[CURRENT_TASK]
  1854.            shl  edi,8
  1855.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1856.            rol  eax,16
  1857.            sub  ax,word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1858.            rol  eax,16
  1859.            mov  [esp+36],eax
  1860.            ret
  1861. msbutton:
  1862.            movzx eax,byte [BTN_DOWN]
  1863.            mov  [esp+36],eax
  1864.            ret
  1865. msz:
  1866.            mov   edi, [TASK_COUNT]
  1867.            movzx edi, word [WIN_POS + edi*2]
  1868.            cmp   edi, [CURRENT_TASK]
  1869.            jne   @f
  1870.            mov   ax,[MOUSE_SCROLL_H]
  1871.            shl   eax,16
  1872.            mov   ax,[MOUSE_SCROLL_V]
  1873.            mov   [esp+36],eax
  1874.            mov   [MOUSE_SCROLL_H],word 0
  1875.            mov   [MOUSE_SCROLL_V],word 0
  1876.            ret
  1877.        @@:
  1878.            mov  [esp+36],dword 0
  1879.            ret
  1880. msset:
  1881.            ret
  1882.  
  1883. app_load_cursor:
  1884.       ;     add ebx, new_app_base
  1885.            cmp ebx, OS_BASE
  1886.            jae msset
  1887.            stdcall load_cursor, ebx, ecx
  1888.            mov [esp+36], eax
  1889.            ret
  1890.  
  1891. app_set_cursor:
  1892.            stdcall set_cursor, ebx
  1893.            mov [esp+36], eax
  1894.            ret
  1895.  
  1896. app_delete_cursor:
  1897.            stdcall delete_cursor, ebx
  1898.            mov [esp+36], eax
  1899.            ret
  1900.  
  1901. is_input:
  1902.  
  1903.    push edx
  1904.    mov  dx,word [midisp]
  1905.    in   al,dx
  1906.    and  al,0x80
  1907.    pop  edx
  1908.    ret
  1909.  
  1910. is_output:
  1911.  
  1912.    push edx
  1913.    mov  dx,word [midisp]
  1914.    in   al,dx
  1915.    and  al,0x40
  1916.    pop  edx
  1917.    ret
  1918.  
  1919.  
  1920. get_mpu_in:
  1921.  
  1922.    push edx
  1923.    mov  dx,word [mididp]
  1924.    in   al,dx
  1925.    pop  edx
  1926.    ret
  1927.  
  1928.  
  1929. put_mpu_out:
  1930.  
  1931.    push edx
  1932.    mov  dx,word [mididp]
  1933.    out  dx,al
  1934.    pop  edx
  1935.    ret
  1936.  
  1937.  
  1938. setuart:
  1939.  
  1940.  su1:
  1941.    call is_output
  1942.    cmp  al,0
  1943.    jnz  su1
  1944.    mov  dx,word [midisp]
  1945.    mov  al,0xff
  1946.    out  dx,al
  1947.  su2:
  1948.    mov  dx,word [midisp]
  1949.    mov  al,0xff
  1950.    out  dx,al
  1951.    call is_input
  1952.    cmp  al,0
  1953.    jnz  su2
  1954.    call get_mpu_in
  1955.    cmp  al,0xfe
  1956.    jnz  su2
  1957.  su3:
  1958.    call is_output
  1959.    cmp  al,0
  1960.    jnz  su3
  1961.    mov  dx,word [midisp]
  1962.    mov  al,0x3f
  1963.    out  dx,al
  1964.  
  1965.    ret
  1966.  
  1967.  
  1968. align 4
  1969.  
  1970. sys_midi:
  1971.  
  1972.      cmp  [mididp],0
  1973.      jnz  sm0
  1974.      mov  [esp+36],dword 1
  1975.      ret
  1976.    sm0:
  1977.  
  1978.      cmp  eax,1
  1979.      mov  [esp+36],dword 0
  1980.      jnz  smn1
  1981.      call setuart
  1982.      ret
  1983.    smn1:
  1984.  
  1985.      cmp  eax,2
  1986.      jnz  smn2
  1987.    sm10:
  1988.      call get_mpu_in
  1989.      call is_output
  1990.      test al,al
  1991.      jnz  sm10
  1992.      mov  al,bl
  1993.      call put_mpu_out
  1994.      ret
  1995.    smn2:
  1996.  
  1997.      ret
  1998.  
  1999.  
  2000. detect_devices:
  2001. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2002. ;include 'detect/commouse.inc'
  2003. ;include 'detect/ps2mouse.inc'
  2004. ;include 'detect/dev_fd.inc'
  2005. ;include 'detect/dev_hdcd.inc'
  2006. ;include 'detect/sear_par.inc'
  2007. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2008.     ret
  2009.  
  2010.  
  2011. sys_end:
  2012.  
  2013.      mov   eax,[TASK_BASE]
  2014.      mov   [eax+TASKDATA.state], 3  ; terminate this program
  2015.  
  2016.     waitterm:            ; wait here for termination
  2017.      mov   ebx,100
  2018.      call  delay_hs
  2019.      jmp   waitterm
  2020.  
  2021. iglobal
  2022. align 4
  2023. sys_system_table:
  2024.         dd      exit_for_anyone         ; 1 = obsolete
  2025.         dd      sysfn_terminate         ; 2 = terminate thread
  2026.         dd      sysfn_activate          ; 3 = activate window
  2027.         dd      sysfn_getidletime       ; 4 = get idle time
  2028.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  2029.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  2030.         dd      sysfn_getactive         ; 7 = get active window
  2031.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  2032.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  2033.         dd      sysfn_minimize          ; 10 = minimize window
  2034.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  2035.         dd      sysfn_lastkey           ; 12 = get last pressed key
  2036.         dd      sysfn_getversion        ; 13 = get kernel version
  2037.         dd      sysfn_waitretrace       ; 14 = wait retrace
  2038.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  2039.         dd      sysfn_getfreemem        ; 16 = get free memory size
  2040.         dd      sysfn_getallmem         ; 17 = get total memory size
  2041.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  2042.                                         ;                 instead of slot
  2043.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  2044.         dd      sysfn_meminfo           ; 20 = get extended memory info
  2045.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  2046.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  2047. sysfn_num = ($ - sys_system_table)/4
  2048. endg
  2049.  
  2050. sys_system:
  2051.         dec     ebx
  2052.         cmp     ebx, sysfn_num
  2053.         jae     @f
  2054.         jmp     dword [sys_system_table + ebx*4]
  2055. @@:
  2056.         ret
  2057.  
  2058.  
  2059. sysfn_shutdown:          ; 18.9 = system shutdown
  2060.      cmp  ecx,1
  2061.      jl   exit_for_anyone
  2062.      cmp  ecx,4
  2063.      jg   exit_for_anyone
  2064.      mov  [BOOT_VAR+0x9030],cl
  2065.  
  2066.      mov  eax,[TASK_COUNT]
  2067.      mov  [SYS_SHUTDOWN],al
  2068.      mov  [shutdown_processes],eax
  2069.      and  dword [esp+32], 0
  2070.  exit_for_anyone:
  2071.      ret
  2072.   uglobal
  2073.    shutdown_processes: dd 0x0
  2074.   endg
  2075.  
  2076. sysfn_terminate:        ; 18.2 = TERMINATE
  2077.      cmp  ecx,2
  2078.      jb   noprocessterminate
  2079.      mov  edx,[TASK_COUNT]
  2080.      cmp  ecx,edx
  2081.      ja   noprocessterminate
  2082.      mov  eax,[TASK_COUNT]
  2083.      shl  ecx,5
  2084.      mov  edx,[ecx+CURRENT_TASK+TASKDATA.pid]
  2085.      add  ecx,CURRENT_TASK+TASKDATA.state
  2086.      cmp  byte [ecx], 9
  2087.      jz   noprocessterminate
  2088.  
  2089.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2090.      mov  [ecx],byte 3       ; clear possible i40's
  2091.      ;call MEM_Heap_UnLock
  2092.  
  2093.      cmp  edx,[application_table_status]    ; clear app table stat
  2094.      jne  noatsc
  2095.      mov  [application_table_status],0
  2096.    noatsc:
  2097.    noprocessterminate:
  2098.      ret
  2099.  
  2100. sysfn_terminate2:
  2101. ;lock application_table_status mutex
  2102. .table_status:
  2103.     cli
  2104.     cmp    [application_table_status],0
  2105.     je     .stf
  2106.     sti
  2107.     call   change_task
  2108.     jmp    .table_status
  2109. .stf:
  2110.     call   set_application_table_status
  2111.     mov    eax,ecx
  2112.     call   pid_to_slot
  2113.     test   eax,eax
  2114.     jz     .not_found
  2115.     mov    ecx,eax
  2116.     cli
  2117.     call   sysfn_terminate
  2118.     mov    [application_table_status],0
  2119.     sti
  2120.     and    dword [esp+32],0
  2121.     ret
  2122. .not_found:
  2123.     mov    [application_table_status],0
  2124.     or     dword [esp+32],-1
  2125.     ret
  2126.  
  2127. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2128.      cmp  ecx,2
  2129.      jb   .nowindowactivate
  2130.      cmp  ecx,[TASK_COUNT]
  2131.      ja   .nowindowactivate
  2132.  
  2133.      mov   [window_minimize], 2   ; restore window if minimized
  2134.  
  2135.      movzx esi, word [WIN_STACK + ecx*2]
  2136.      cmp   esi, [TASK_COUNT]
  2137.      je    .nowindowactivate ; already active
  2138.  
  2139.      mov   edi, ecx
  2140.      shl   edi, 5
  2141.      add   edi, window_data
  2142.      movzx esi, word [WIN_STACK + ecx * 2]
  2143.      lea   esi, [WIN_POS + esi * 2]
  2144.      call  waredraw
  2145. .nowindowactivate:
  2146.      ret
  2147.  
  2148. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2149.      mov  eax,[idleusesec]
  2150.      mov  [esp+32], eax
  2151.      ret
  2152.  
  2153. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2154.      mov  eax,[CPU_FREQ]
  2155.      mov  [esp+32], eax
  2156.      ret
  2157.  
  2158. ;  SAVE ramdisk to /hd/1/menuet.img
  2159. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2160.    include 'blkdev/rdsave.inc'
  2161. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2162.  
  2163. sysfn_getactive:        ; 18.7 = get active window
  2164.      mov  eax, [TASK_COUNT]
  2165.    movzx  eax, word [WIN_POS + eax*2]
  2166.      mov  [esp+32],eax
  2167.      ret
  2168.  
  2169. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2170.      cmp  ecx,1
  2171.      jne  nogetsoundflag
  2172.      movzx  eax,byte [sound_flag] ; get sound_flag
  2173.      mov  [esp+32],eax
  2174.      ret
  2175.  nogetsoundflag:
  2176.      cmp  ecx,2
  2177.      jnz  nosoundflag
  2178.      xor  byte [sound_flag], 1
  2179.  nosoundflag:
  2180.      ret
  2181.  
  2182. sysfn_minimize:         ; 18.10 = minimize window
  2183.      mov   [window_minimize],1
  2184.      ret
  2185.  
  2186. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2187.      cmp  ecx,1
  2188.      jnz  full_table
  2189.   small_table:
  2190.      call for_all_tables
  2191.      mov ecx,10
  2192.      cld
  2193.      rep movsb
  2194.      ret
  2195.    for_all_tables:
  2196.      mov edi,edx
  2197.      mov esi,DRIVE_DATA
  2198.      ret
  2199.   full_table:
  2200.      cmp  ecx,2
  2201.      jnz  exit_for_anyone
  2202.      call for_all_tables
  2203.      mov ecx,16384
  2204.      cld
  2205.      rep movsd
  2206.      ret
  2207.  
  2208. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2209.         and     dword [esp+32], 0
  2210.         ret
  2211.  
  2212. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2213.      mov edi,ebx
  2214.      mov esi,version_inf
  2215.      mov ecx,version_end-version_inf
  2216.      rep movsb
  2217.      ret
  2218.  
  2219. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2220.      ;wait retrace functions
  2221.  sys_wait_retrace:
  2222.      mov edx,0x3da
  2223.  WaitRetrace_loop:
  2224.      in al,dx
  2225.      test al,1000b
  2226.      jz WaitRetrace_loop
  2227.      and [esp+32],dword 0
  2228.      ret
  2229.  
  2230. sysfn_centermouse:      ; 18.15 = mouse centered
  2231.      call  mouse_centered
  2232.      and [esp+32],dword 0
  2233.      ret
  2234.  
  2235. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2236.      cmp  ecx,0  ; get mouse speed factor
  2237.      jnz  .set_mouse_acceleration
  2238.      xor  eax,eax
  2239.      mov  ax,[mouse_speed_factor]
  2240.      mov  [esp+32],eax
  2241.      ret
  2242.  .set_mouse_acceleration:
  2243.      cmp  ecx,1  ; set mouse speed factor
  2244.      jnz  .get_mouse_delay
  2245.      mov  [mouse_speed_factor],dx
  2246.      ret
  2247.  .get_mouse_delay:
  2248.      cmp  ecx,2  ; get mouse delay
  2249.      jnz  .set_mouse_delay
  2250.      mov  eax,[mouse_delay]
  2251.      mov  [esp+32],eax
  2252.      ret
  2253.  .set_mouse_delay:
  2254.      cmp  ecx,3  ; set mouse delay
  2255.      jnz  .set_pointer_position
  2256.      mov  [mouse_delay],edx
  2257.      ret
  2258.  .set_pointer_position:
  2259.      cmp  ecx,4  ; set mouse pointer position
  2260.      jnz  .set_mouse_button
  2261.      mov   [MOUSE_Y],dx    ;y
  2262.      ror   edx,16
  2263.      mov   [MOUSE_X],dx    ;x
  2264.      rol   edx,16
  2265.      ret
  2266.  .set_mouse_button:
  2267.      cmp   ecx,5  ; set mouse button features
  2268.      jnz  .end
  2269.      mov   [BTN_DOWN],dl
  2270.      mov   [mouse_active],1
  2271.  .end:
  2272.      ret
  2273.  
  2274. sysfn_getfreemem:
  2275.      mov eax, [pg_data.pages_free]
  2276.      shl eax, 2
  2277.      mov [esp+32],eax
  2278.      ret
  2279.  
  2280. sysfn_getallmem:
  2281.      mov  eax,[_mem_amount]
  2282.      shr eax, 10
  2283.      mov  [esp+32],eax
  2284.      ret
  2285.  
  2286. ; // Alver, 2007-22-08 // {
  2287. sysfn_pid_to_slot:
  2288.      mov   eax, ecx
  2289.      call  pid_to_slot
  2290.      mov   [esp+32], eax
  2291.      ret
  2292.  
  2293. sysfn_min_rest_window:
  2294.      pushad
  2295.      mov   eax, edx      ; ebx - operating
  2296.      shr   ecx, 1
  2297.      jnc    @f
  2298.      call  pid_to_slot
  2299. @@:
  2300.      or    eax, eax      ; eax - number of slot
  2301.      jz    .error
  2302.      cmp   eax, 255         ; varify maximal slot number
  2303.      ja    .error
  2304.      movzx eax, word [WIN_STACK + eax*2]
  2305.      shr   ecx, 1
  2306.      jc    .restore
  2307.  ; .minimize:
  2308.      call  minimize_window
  2309.      jmp   .exit
  2310. .restore:
  2311.      call  restore_minimized_window
  2312. .exit:
  2313.      popad
  2314.      xor   eax, eax
  2315.      mov   [esp+32], eax
  2316.      ret
  2317. .error:
  2318.      popad
  2319.      xor   eax, eax
  2320.      dec   eax
  2321.      mov   [esp+32], eax
  2322.      ret
  2323. ; } \\ Alver, 2007-22-08 \\
  2324.  
  2325. uglobal
  2326. ;// mike.dld, 2006-29-01 [
  2327. screen_workarea RECT
  2328. ;// mike.dld, 2006-29-01 ]
  2329. window_minimize db 0
  2330. sound_flag      db 0
  2331. endg
  2332.  
  2333. iglobal
  2334. version_inf:
  2335.   db 0,7,1,0  ; version 0.7.1.0
  2336.   db UID_KOLIBRI
  2337.   dd __REV__
  2338. version_end:
  2339. endg
  2340.  
  2341. UID_NONE=0
  2342. UID_MENUETOS=1   ;official
  2343. UID_KOLIBRI=2    ;russian
  2344.  
  2345. sys_cachetodiskette:
  2346.         cmp     ebx, 1
  2347.         jne     .no_floppy_a_save
  2348.         mov     [flp_number], 1
  2349.         jmp     .save_image_on_floppy
  2350. .no_floppy_a_save:
  2351.         cmp     ebx, 2
  2352.         jne     .no_floppy_b_save
  2353.         mov     [flp_number], 2
  2354. .save_image_on_floppy:
  2355.         call    save_image
  2356.         mov     [esp + 32], dword 0
  2357.         cmp     [FDC_Status], 0
  2358.         je      .yes_floppy_save
  2359. .no_floppy_b_save:
  2360.         mov     [esp + 32], dword 1
  2361. .yes_floppy_save:
  2362.         ret
  2363.  
  2364. uglobal
  2365. ;  bgrchanged  dd  0x0
  2366. bgrlock db 0
  2367. bgrlockpid dd 0
  2368. endg
  2369.  
  2370. sys_background:
  2371.  
  2372.     cmp   ebx,1                            ; BACKGROUND SIZE
  2373.     jnz   nosb1
  2374.     cmp   ecx,0
  2375.     je    sbgrr
  2376.     cmp   edx,0
  2377.     je    sbgrr
  2378. @@:
  2379.         mov     al, 1
  2380.         xchg    [bgrlock], al
  2381.         test    al, al
  2382.         jz      @f
  2383.         call    change_task
  2384.         jmp     @b
  2385. @@:
  2386.     mov   [BgrDataWidth],ecx
  2387.     mov   [BgrDataHeight],edx
  2388. ;    mov   [bgrchanged],1
  2389.  
  2390.     pushad
  2391. ; return memory for old background
  2392.     stdcall kernel_free, [img_background]
  2393. ; calculate RAW size
  2394.     xor  eax,eax
  2395.     inc  eax
  2396.     cmp  [BgrDataWidth],eax
  2397.     jae   @f
  2398.     mov [BgrDataWidth],eax
  2399. @@:
  2400.     cmp  [BgrDataHeight],eax
  2401.     jae   @f
  2402.     mov [BgrDataHeight],eax
  2403. @@:
  2404.     mov  eax,[BgrDataWidth]
  2405.     imul eax,[BgrDataHeight]
  2406.     lea  eax,[eax*3]
  2407.     mov  [mem_BACKGROUND],eax
  2408. ; get memory for new background
  2409.     stdcall kernel_alloc, eax
  2410.     test eax, eax
  2411.     jz .exit_mem
  2412.     mov [img_background], eax
  2413. .exit_mem:
  2414.     popad
  2415.         mov     [bgrlock], 0
  2416.  
  2417.   sbgrr:
  2418.     ret
  2419.  
  2420.   nosb1:
  2421.  
  2422.     cmp   ebx,2                            ; SET PIXEL
  2423.     jnz   nosb2
  2424.     cmp   ecx,[mem_BACKGROUND]
  2425.     jae   nosb2
  2426.     mov   eax,[img_background]
  2427.     mov   ebx,[eax+ecx]
  2428.     and   ebx,0xFF000000 ;255*256*256*256
  2429.     and   edx,0x00FFFFFF ;255*256*256+255*256+255
  2430.     add   edx,ebx
  2431.     mov   [eax+ecx],edx
  2432. ;    mov   [bgrchanged],1
  2433.     ret
  2434.   nosb2:
  2435.  
  2436.     cmp   ebx,3                            ; DRAW BACKGROUND
  2437.     jnz   nosb3
  2438. draw_background_temp:
  2439. ;    cmp   [bgrchanged],1 ;0
  2440. ;    je    nosb31
  2441. ;draw_background_temp:
  2442. ;    mov   [bgrchanged],1 ;0
  2443.     mov    [background_defined], 1
  2444.     call  force_redraw_background
  2445.     mov    [REDRAW_BACKGROUND], byte 2
  2446.    nosb31:
  2447.     ret
  2448.   nosb3:
  2449.  
  2450.     cmp   ebx,4                            ; TILED / STRETCHED
  2451.     jnz   nosb4
  2452.     cmp   ecx,[BgrDrawMode]
  2453.     je    nosb41
  2454.     mov   [BgrDrawMode],ecx
  2455. ;    mov   [bgrchanged],1
  2456.    nosb41:
  2457.     ret
  2458.   nosb4:
  2459.  
  2460.     cmp   ebx,5                            ; BLOCK MOVE TO BGR
  2461.     jnz   nosb5
  2462.   ; bughere
  2463.     mov   eax, ecx
  2464.     mov   ebx, edx
  2465.     add   ebx, [img_background]   ;IMG_BACKGROUND
  2466.     mov   ecx, esi
  2467.     call  memmove
  2468.   .fin:
  2469.     ret
  2470.   nosb5:
  2471.  
  2472.         cmp     ebx, 6
  2473.         jnz     nosb6
  2474. @@:
  2475.         mov     al, 1
  2476.         xchg    [bgrlock], al
  2477.         test    al, al
  2478.         jz      @f
  2479.         call    change_task
  2480.         jmp     @b
  2481. @@:
  2482.         mov     eax, [CURRENT_TASK]
  2483.         mov     [bgrlockpid], eax
  2484.         stdcall user_alloc, [mem_BACKGROUND]
  2485.         mov     [esp+32], eax
  2486.         test    eax, eax
  2487.         jz      .nomem
  2488.         mov     ebx, eax
  2489.         shr     ebx, 12
  2490.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2491.         mov     esi, [img_background]
  2492.         shr     esi, 12
  2493.         mov     ecx, [mem_BACKGROUND]
  2494.         add     ecx, 0xFFF
  2495.         shr     ecx, 12
  2496. .z:
  2497.         mov     eax, [page_tabs+ebx*4]
  2498.         test    al, 1
  2499.         jz      @f
  2500.         call    free_page
  2501. @@:
  2502.         mov     eax, [page_tabs+esi*4]
  2503.         or      al, PG_UW
  2504.         mov     [page_tabs+ebx*4], eax
  2505.         mov     eax, ebx
  2506.         shl     eax, 12
  2507.         invlpg  [eax]
  2508.         inc     ebx
  2509.         inc     esi
  2510.         loop    .z
  2511.         ret
  2512. .nomem:
  2513.         and     [bgrlockpid], 0
  2514.         mov     [bgrlock], 0
  2515. nosb6:
  2516.         cmp     ebx, 7
  2517.         jnz     nosb7
  2518.         cmp     [bgrlock], 0
  2519.         jz      .err
  2520.         mov     eax, [CURRENT_TASK]
  2521.         cmp     [bgrlockpid], eax
  2522.         jnz     .err
  2523.         mov     eax, ecx
  2524.         mov     ebx, ecx
  2525.         shr     eax, 12
  2526.         mov     ecx, [page_tabs+(eax-1)*4]
  2527.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2528.         jz      .err
  2529.         jnp     .err
  2530.         push    eax
  2531.         shr     ecx, 12
  2532. @@:
  2533.         and     dword [page_tabs+eax*4], 0
  2534.         mov     edx, eax
  2535.         shl     edx, 12
  2536.         push eax
  2537.         invlpg  [edx]
  2538.         pop eax
  2539.         inc     eax
  2540.         loop    @b
  2541.         pop     eax
  2542.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2543.         stdcall user_free, ebx
  2544.         mov     [esp+32], eax
  2545.         and     [bgrlockpid], 0
  2546.         mov     [bgrlock], 0
  2547.         ret
  2548. .err:
  2549.         and     dword [esp+32], 0
  2550.         ret
  2551.  
  2552. nosb7:
  2553.     ret
  2554.  
  2555. force_redraw_background:
  2556.     mov   [draw_data+32 + RECT.left],dword 0
  2557.     mov   [draw_data+32 + RECT.top],dword 0
  2558.     push  eax ebx
  2559.     mov   eax,[Screen_Max_X]
  2560.     mov   ebx,[Screen_Max_Y]
  2561.     mov   [draw_data+32 + RECT.right],eax
  2562.     mov   [draw_data+32 + RECT.bottom],ebx
  2563.     pop   ebx eax
  2564.     mov   byte [REDRAW_BACKGROUND], 1
  2565.     ret
  2566.  
  2567. align 4
  2568.  
  2569. sys_getbackground:
  2570.  
  2571.     cmp   eax,1                                  ; SIZE
  2572.     jnz   nogb1
  2573.     mov   eax,[BgrDataWidth]
  2574.     shl   eax,16
  2575.     mov   ax,[BgrDataHeight]
  2576.     mov   [esp+36],eax
  2577.     ret
  2578.   nogb1:
  2579.  
  2580.     cmp   eax,2                                  ; PIXEL
  2581.     jnz   nogb2
  2582. ;    mov   edx,0x160000-16
  2583. ;    cmp   edx,ebx
  2584. ;    jbe   nogb2
  2585. ;    mov   eax, [ebx+IMG_BACKGROUND]
  2586.     mov   eax,[img_background]
  2587.     mov   eax,[ebx+eax]
  2588.  
  2589.     and   eax, 0xFFFFFF
  2590.     mov   [esp+36],eax
  2591.     ret
  2592.   nogb2:
  2593.  
  2594.     cmp   eax,4                                  ; TILED / STRETCHED
  2595.     jnz   nogb4
  2596.     mov   eax,[BgrDrawMode]
  2597.   nogb4:
  2598.     mov   [esp+36],eax
  2599.     ret
  2600.  
  2601.  
  2602. align 4
  2603.  
  2604. sys_getkey:
  2605.         mov     [esp + 32],dword 1
  2606.         ; test main buffer
  2607.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2608.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2609.         mov     edx, [TASK_COUNT]
  2610.         cmp     ecx, edx
  2611.         jne     .finish
  2612.         cmp     [KEY_COUNT], byte 0
  2613.         je      .finish
  2614.         movzx   eax, byte [KEY_BUFF]
  2615.         shl     eax, 8
  2616.         push    eax
  2617.         dec     byte [KEY_COUNT]
  2618.         and     byte [KEY_COUNT], 127
  2619.         movzx   ecx, byte [KEY_COUNT]
  2620.         add     ecx, 2
  2621.         mov     eax, KEY_BUFF + 1
  2622.         mov     ebx, KEY_BUFF
  2623.         call    memmove
  2624.         pop     eax
  2625. .ret_eax:
  2626.         mov     [esp + 32], eax
  2627.         ret
  2628. .finish:
  2629. ; test hotkeys buffer
  2630.         mov     ecx, hotkey_buffer
  2631. @@:
  2632.         cmp     [ecx], ebx
  2633.         jz      .found
  2634.         add     ecx, 8
  2635.         cmp     ecx, hotkey_buffer + 120 * 8
  2636.         jb      @b
  2637.         ret
  2638. .found:
  2639.         mov     ax, [ecx + 6]
  2640.         shl     eax, 16
  2641.         mov     ah, [ecx + 4]
  2642.         mov     al, 2
  2643.         and     dword [ecx + 4], 0
  2644.         and     dword [ecx], 0
  2645.         jmp     .ret_eax
  2646.  
  2647. align 4
  2648.  
  2649. sys_getbutton:
  2650.  
  2651.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2652.         mov     [esp + 32], dword 1
  2653.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2654.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2655.         cmp     ecx, edx
  2656.         jne     .exit
  2657.         movzx   eax, byte [BTN_COUNT]
  2658.         test    eax, eax
  2659.         jz      .exit
  2660.         mov     eax, [BTN_BUFF]
  2661.         shl     eax, 8
  2662. ; // Alver 22.06.2008 // {
  2663.         mov       al, byte [btn_down_determ]
  2664.         and       al,0xFE                                       ; delete left button bit
  2665. ; } \\ Alver \\
  2666.         mov     [BTN_COUNT], byte 0
  2667.         mov     [esp + 32], eax
  2668. .exit:
  2669.         ret
  2670.  
  2671.  
  2672. align 4
  2673.  
  2674. sys_cpuusage:
  2675.  
  2676. ;  RETURN:
  2677. ;
  2678. ;  +00 dword     process cpu usage
  2679. ;  +04  word     position in windowing stack
  2680. ;  +06  word     windowing stack value at current position (cpu nro)
  2681. ;  +10 12 bytes  name
  2682. ;  +22 dword     start in mem
  2683. ;  +26 dword     used mem
  2684. ;  +30 dword     PID , process idenfification number
  2685. ;
  2686.  
  2687.     cmp  ecx,-1         ; who am I ?
  2688.     jne  .no_who_am_i
  2689.     mov  ecx,[CURRENT_TASK]
  2690.   .no_who_am_i:
  2691.         cmp     ecx, max_processes
  2692.         ja      .nofillbuf
  2693.  
  2694. ; +4: word: position of the window of thread in the window stack
  2695.         mov     ax, [WIN_STACK + ecx * 2]
  2696.         mov     [ebx+4], ax
  2697. ; +6: word: number of the thread slot, which window has in the window stack
  2698. ;           position ecx (has no relation to the specific thread)
  2699.         mov     ax, [WIN_POS + ecx * 2]
  2700.         mov     [ebx+6], ax
  2701.  
  2702.         shl     ecx, 5
  2703.  
  2704. ; +0: dword: memory usage
  2705.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  2706.         mov     [ebx], eax
  2707. ; +10: 11 bytes: name of the process
  2708.         push    ecx
  2709.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  2710.         add     ebx, 10
  2711.         mov     ecx, 11
  2712.         call    memmove
  2713.         pop     ecx
  2714.  
  2715. ; +22: address of the process in memory
  2716. ; +26: size of used memory - 1
  2717.         push    edi
  2718.         lea     edi, [ebx+12]
  2719.         xor     eax, eax
  2720.         mov     edx, 0x100000*16
  2721.         cmp     ecx, 1 shl 5
  2722.         je      .os_mem
  2723.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  2724.         xor eax, eax
  2725. .os_mem:
  2726.         stosd
  2727.         lea     eax, [edx-1]
  2728.         stosd
  2729.  
  2730. ; +30: PID/TID
  2731.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  2732.         stosd
  2733.  
  2734.     ; window position and size
  2735.         push    esi
  2736.         lea     esi, [ecx + window_data + WDATA.box]
  2737.         movsd
  2738.         movsd
  2739.         movsd
  2740.         movsd
  2741.  
  2742.     ; Process state (+50)
  2743.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  2744.         stosd
  2745.  
  2746.     ; Window client area box
  2747.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2748.         movsd
  2749.         movsd
  2750.         movsd
  2751.         movsd
  2752.  
  2753.     ; Window state
  2754.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2755.         stosb
  2756.  
  2757.         pop     esi
  2758.         pop     edi
  2759.  
  2760. .nofillbuf:
  2761.     ; return number of processes
  2762.  
  2763.     mov    eax,[TASK_COUNT]
  2764.     mov    [esp+32],eax
  2765.     ret
  2766.  
  2767. align 4
  2768. sys_clock:
  2769.         cli
  2770.   ; Mikhail Lisovin  xx Jan 2005
  2771.   @@:   mov   al, 10
  2772.         out   0x70, al
  2773.         in    al, 0x71
  2774.         test  al, al
  2775.         jns   @f
  2776.         mov   esi, 1
  2777.         call  delay_ms
  2778.         jmp   @b
  2779.   @@:
  2780.   ; end Lisovin's fix
  2781.  
  2782.         xor   al,al           ; seconds
  2783.         out   0x70,al
  2784.         in    al,0x71
  2785.         movzx ecx,al
  2786.         mov   al,02           ; minutes
  2787.         shl   ecx,16
  2788.         out   0x70,al
  2789.         in    al,0x71
  2790.         movzx edx,al
  2791.         mov   al,04           ; hours
  2792.         shl   edx,8
  2793.         out   0x70,al
  2794.         in    al,0x71
  2795.         add   ecx,edx
  2796.         movzx edx,al
  2797.         add   ecx,edx
  2798.         sti
  2799.         mov     [esp + 32], ecx
  2800.         ret
  2801.  
  2802.  
  2803. align 4
  2804.  
  2805. sys_date:
  2806.  
  2807.         cli
  2808.   @@:   mov   al, 10
  2809.         out   0x70, al
  2810.         in    al, 0x71
  2811.         test  al, al
  2812.         jns   @f
  2813.         mov   esi, 1
  2814.         call  delay_ms
  2815.         jmp   @b
  2816.   @@:
  2817.  
  2818.         mov     ch,0
  2819.         mov     al,7            ; date
  2820.         out     0x70,al
  2821.         in      al,0x71
  2822.         mov     cl,al
  2823.         mov     al,8            ; month
  2824.         shl     ecx,16
  2825.         out     0x70,al
  2826.         in      al,0x71
  2827.         mov     ch,al
  2828.         mov     al,9            ; year
  2829.         out     0x70,al
  2830.         in      al,0x71
  2831.         mov     cl,al
  2832.         sti
  2833.         mov     [esp+32], ecx
  2834.         ret
  2835.  
  2836.  
  2837. ; redraw status
  2838.  
  2839. sys_redrawstat:
  2840.         cmp     ebx, 1
  2841.         jne     no_widgets_away
  2842.         ; buttons away
  2843.         mov     ecx,[CURRENT_TASK]
  2844.   sys_newba2:
  2845.         mov     edi,[BTN_ADDR]
  2846.         cmp     [edi], dword 0  ; empty button list ?
  2847.         je      end_of_buttons_away
  2848.         movzx   ebx, word [edi]
  2849.         inc     ebx
  2850.         mov     eax,edi
  2851.   sys_newba:
  2852.         dec     ebx
  2853.         jz      end_of_buttons_away
  2854.  
  2855.         add     eax, 0x10
  2856.         cmp     cx, [eax]
  2857.         jnz     sys_newba
  2858.  
  2859.         push    eax ebx ecx
  2860.         mov     ecx,ebx
  2861.         inc     ecx
  2862.         shl     ecx, 4
  2863.         mov     ebx, eax
  2864.         add     eax, 0x10
  2865.         call    memmove
  2866.         dec     dword [edi]
  2867.         pop     ecx ebx eax
  2868.  
  2869.         jmp     sys_newba2
  2870.  
  2871.   end_of_buttons_away:
  2872.  
  2873.         ret
  2874.  
  2875.   no_widgets_away:
  2876.  
  2877.         cmp     ebx, 2
  2878.         jnz     srl1
  2879.  
  2880.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2881.         add     edx, draw_data - CURRENT_TASK
  2882.         mov     [edx + RECT.left], 0
  2883.         mov     [edx + RECT.top], 0
  2884.         mov     eax, [Screen_Max_X]
  2885.         mov     [edx + RECT.right], eax
  2886.         mov     eax, [Screen_Max_Y]
  2887.         mov     [edx + RECT.bottom], eax
  2888.  
  2889.         mov     edi, [TASK_BASE]
  2890.         or      [edi - twdw + WDATA.fl_wdrawn], 1   ; no new position & buttons from app
  2891.         call    sys_window_mouse
  2892.         ret
  2893.  
  2894.   srl1:
  2895.         ret
  2896.  
  2897.  
  2898. sys_drawwindow:
  2899.  
  2900.     mov   eax,edx
  2901.     shr   eax,16+8
  2902.     and   eax,15
  2903.  
  2904. ;    cmp   eax,0   ; type I    - original style
  2905.     jne   nosyswI
  2906.     inc   [mouse_pause]
  2907.     call  [disable_mouse]
  2908.     call  sys_set_window
  2909.     call  [disable_mouse]
  2910.     call  drawwindow_I
  2911.     ;dec   [mouse_pause]
  2912.     ;call   [draw_pointer]
  2913.     ;ret
  2914.     jmp   draw_window_caption.2
  2915.   nosyswI:
  2916.  
  2917.     cmp   al,1    ; type II   - only reserve area, no draw
  2918.     jne   nosyswII
  2919.     inc   [mouse_pause]
  2920.     call  [disable_mouse]
  2921.     call  sys_set_window
  2922.     call  [disable_mouse]
  2923.     call  sys_window_mouse
  2924.     dec   [mouse_pause]
  2925.     call   [draw_pointer]
  2926.     ret
  2927.   nosyswII:
  2928.  
  2929.     cmp   al,2    ; type III  - new style
  2930.     jne   nosyswIII
  2931.     inc   [mouse_pause]
  2932.     call  [disable_mouse]
  2933.     call  sys_set_window
  2934.     call  [disable_mouse]
  2935.     call  drawwindow_III
  2936.     ;dec   [mouse_pause]
  2937.     ;call   [draw_pointer]
  2938.     ;ret
  2939.     jmp   draw_window_caption.2
  2940.   nosyswIII:
  2941.  
  2942.     cmp   al,3    ; type IV - skinned window
  2943.     je    draw_skin_window
  2944.     cmp   al,4    ; type V - skinned window not sized! {not_sized_skin_window}
  2945.     jne   nosyswV
  2946.   draw_skin_window:
  2947.  
  2948.     inc   [mouse_pause]
  2949.     call  [disable_mouse]
  2950.     call  sys_set_window
  2951.     call  [disable_mouse]
  2952.     mov   eax, [TASK_COUNT]
  2953.     movzx eax, word [WIN_POS + eax*2]
  2954.     cmp   eax, [CURRENT_TASK]
  2955.     setz  al
  2956.     movzx eax, al
  2957.     push  eax
  2958.     call  drawwindow_IV
  2959.     ;dec   [mouse_pause]
  2960.     ;call   [draw_pointer]
  2961.     ;ret
  2962.     jmp   draw_window_caption.2
  2963.   nosyswV:
  2964.  
  2965.     ret
  2966.  
  2967.  
  2968. draw_window_caption:
  2969.         inc     [mouse_pause]
  2970.         call    [disable_mouse]
  2971.  
  2972.         xor     eax,eax
  2973.         mov     edx,[TASK_COUNT]
  2974.         movzx   edx,word[WIN_POS+edx*2]
  2975.         cmp     edx,[CURRENT_TASK]
  2976.         jne     @f
  2977.         inc     eax
  2978.     @@: mov     edx,[CURRENT_TASK]
  2979.         shl     edx,5
  2980.         add     edx,window_data
  2981.         movzx   ebx,[edx+WDATA.fl_wstyle]
  2982.         and     bl,0x0F
  2983.         cmp     bl,3
  2984.         je      .draw_caption_style_3           ;{for 3 and 4 style write caption}
  2985.         cmp     bl,4
  2986.         je      .draw_caption_style_3
  2987.  
  2988.         jmp     .not_style_3
  2989.   .draw_caption_style_3:
  2990.  
  2991.         push    edx
  2992.         call    drawwindow_IV_caption
  2993.         add     esp,4
  2994.         jmp     .2
  2995.  
  2996.   .not_style_3:
  2997.         cmp     bl,2
  2998.         jne     .not_style_2
  2999.  
  3000.         call    drawwindow_III_caption
  3001.         jmp     .2
  3002.  
  3003.   .not_style_2:
  3004.         cmp     bl,0
  3005.         jne     .2
  3006.  
  3007.         call    drawwindow_I_caption
  3008.  
  3009. ;--------------------------------------------------------------
  3010.   .2:   ;jmp     @f
  3011.         mov     edi,[CURRENT_TASK]
  3012.         shl     edi,5
  3013.         test    [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3014.         jz      @f
  3015.         mov     edx,[edi*8+SLOT_BASE+APPDATA.wnd_caption]
  3016.         or      edx,edx
  3017.         jz      @f
  3018.  
  3019.         movzx   eax,[edi+window_data+WDATA.fl_wstyle]
  3020.         and     al,0x0F
  3021.         cmp     al,3
  3022.         je      .skinned
  3023.         cmp     al,4
  3024.         je      .skinned
  3025.  
  3026.         jmp     .not_skinned
  3027.   .skinned:
  3028.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3029.         mov     bp,word[edi+window_data+WDATA.box.top]
  3030.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3031.         sub     ax,[_skinmargins.left]
  3032.         sub     ax,[_skinmargins.right]
  3033.         push    edx
  3034.         cwde
  3035.         cdq
  3036.         mov     ebx,6
  3037.         idiv    ebx
  3038.         pop     edx
  3039.         or      eax,eax
  3040.         js      @f
  3041.         mov     esi,eax
  3042.         mov     ebx,dword[_skinmargins.left-2]
  3043.         mov     bx,word[_skinh]
  3044.         sub     bx,[_skinmargins.bottom]
  3045.         sub     bx,[_skinmargins.top]
  3046.         sar     bx,1
  3047.         adc     bx,0
  3048.         add     bx,[_skinmargins.top]
  3049.         add     bx,-3
  3050.         add     ebx,ebp
  3051.         jmp     .dodraw
  3052.  
  3053.   .not_skinned:
  3054.         cmp     al,1
  3055.         je      @f
  3056.  
  3057.         mov     ebp,[edi+window_data+WDATA.box.left-2]
  3058.         mov     bp,word[edi+window_data+WDATA.box.top]
  3059.         movzx   eax,word[edi+window_data+WDATA.box.width]
  3060.         sub     eax,16
  3061.         push    edx
  3062.         cwde
  3063.         cdq
  3064.         mov     ebx,6
  3065.         idiv    ebx
  3066.         pop     edx
  3067.         or      eax,eax
  3068.         js      @f
  3069.         mov     esi,eax
  3070.         mov     ebx,0x00080007
  3071.         add     ebx,ebp
  3072. .dodraw:
  3073.         mov     ecx,[common_colours+16];0x00FFFFFF
  3074.         or      ecx, 0x80000000
  3075.         xor     edi,edi
  3076. ; // Alver 22.06.2008 // {
  3077. ;       call    dtext
  3078.         call dtext_asciiz_esi
  3079. ; } \\ Alver \\
  3080.  
  3081.     @@:
  3082. ;--------------------------------------------------------------
  3083.         dec     [mouse_pause]
  3084.         call    [draw_pointer]
  3085.         ret
  3086.  
  3087. iglobal
  3088. align 4
  3089. window_topleft dd \
  3090.   1, 21,\               ;type 0
  3091.   0,  0,\       ;type 1
  3092.   5, 20,\       ;type 2
  3093.   5,  ?,\       ;type 3 {set by skin}
  3094.   5,  ?         ;type 4 {set by skin}
  3095. endg
  3096.  
  3097. set_window_clientbox:
  3098.         push    eax ecx edi
  3099.  
  3100.         mov     eax,[_skinh]
  3101.         mov     [window_topleft+4*7],eax
  3102.         mov     [window_topleft+4*9],eax
  3103.  
  3104.         mov     ecx,edi
  3105.         sub     edi,window_data
  3106.         shl     edi,3
  3107.         test    [ecx+WDATA.fl_wstyle],WSTYLE_CLIENTRELATIVE
  3108.         jz      @f
  3109.  
  3110.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3111.         and     eax,0x0F
  3112.         mov     eax,[eax*8+window_topleft+0]
  3113.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3114.         shl     eax,1
  3115.         neg     eax
  3116.         add     eax,[ecx+WDATA.box.width]
  3117.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3118.  
  3119.         movzx   eax,[ecx+WDATA.fl_wstyle]
  3120.         and     eax,0x0F
  3121.         push    [eax*8+window_topleft+0]
  3122.         mov     eax,[eax*8+window_topleft+4]
  3123.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3124.         neg     eax
  3125.         sub     eax,[esp]
  3126.         add     eax,[ecx+WDATA.box.height]
  3127.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3128.         add     esp,4
  3129.  
  3130.         pop     edi ecx eax
  3131.         ret
  3132.     @@:
  3133.         xor     eax,eax
  3134.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.left],eax
  3135.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.top],eax
  3136.         mov     eax,[ecx+WDATA.box.width]
  3137.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.width],eax
  3138.         mov     eax,[ecx+WDATA.box.height]
  3139.         mov     [edi+SLOT_BASE+APPDATA.wnd_clientbox.height],eax
  3140.  
  3141.         pop     edi ecx eax
  3142.         ret
  3143.  
  3144. sys_set_window:
  3145.  
  3146.     mov   eax,[CURRENT_TASK]
  3147.     shl   eax,5
  3148.     add   eax,window_data
  3149.  
  3150.     ; colors
  3151.     mov   [eax+WDATA.cl_workarea],edx
  3152.     mov   [eax+WDATA.cl_titlebar],esi
  3153.     mov   [eax+WDATA.cl_frames],edi
  3154.  
  3155.     mov   edi, eax
  3156.  
  3157.     ; check flag (?)
  3158.     test  [edi+WDATA.fl_wdrawn],1
  3159.     jnz   newd
  3160.  
  3161.     mov   eax,[timer_ticks] ;[0xfdf0]
  3162.     add   eax,100
  3163.     mov   [new_window_starting],eax
  3164.  
  3165.     mov   word[edi+WDATA.box.width],bx
  3166.     mov   word[edi+WDATA.box.height],cx
  3167.     sar   ebx,16
  3168.     sar   ecx,16
  3169.     mov   word[edi+WDATA.box.left],bx
  3170.     mov   word[edi+WDATA.box.top],cx
  3171.  
  3172.     call  check_window_position
  3173.  
  3174.     call  set_window_clientbox
  3175.  
  3176.     push  ecx esi edi               ; save for window fullscreen/resize
  3177.     ;mov   esi,edi
  3178.  
  3179.         mov     cl, [edi+WDATA.fl_wstyle]
  3180.         mov     eax, [edi+WDATA.cl_frames]
  3181.  
  3182.     sub   edi,window_data
  3183.     shl   edi,3
  3184.     add   edi,SLOT_BASE
  3185.  
  3186.         and     cl,0x0F
  3187.         mov     [edi+APPDATA.wnd_caption],0
  3188.         cmp     cl,3
  3189.         je      set_APPDATA_wnd_caption
  3190.         cmp     cl,4                                                            ; {SPraid.simba}
  3191.         je      set_APPDATA_wnd_caption
  3192.  
  3193.         jmp     @f
  3194.     set_APPDATA_wnd_caption:
  3195.         mov     [edi+APPDATA.wnd_caption],eax
  3196.     @@: mov     esi,[esp+0]
  3197.  
  3198.     add   edi, APPDATA.saved_box
  3199.         movsd
  3200.         movsd
  3201.         movsd
  3202.         movsd
  3203.     pop   edi esi ecx
  3204.  
  3205.         mov     esi, [CURRENT_TASK]
  3206.         movzx   esi, word [WIN_STACK+esi*2]
  3207.         lea     esi, [WIN_POS+esi*2]
  3208.         call    waredraw
  3209.  
  3210. ;;;    mov   ebx, 1
  3211. ;;;    call  delay_hs
  3212.     mov   eax, [edi+WDATA.box.left]
  3213.     mov   ebx, [edi+WDATA.box.top]
  3214.     mov   ecx, [edi+WDATA.box.width]
  3215.     mov   edx, [edi+WDATA.box.height]
  3216.     add   ecx, eax
  3217.     add   edx, ebx
  3218.     call  calculatescreen
  3219.  
  3220.     mov   [KEY_COUNT],byte 0           ; empty keyboard buffer
  3221.     mov   [BTN_COUNT],byte 0           ; empty button buffer
  3222.  
  3223.   newd:
  3224.     mov   [edi+WDATA.fl_redraw],byte 0   ; no redraw
  3225.     mov   edx,edi
  3226.  
  3227.     ret
  3228.  
  3229. syscall_windowsettings:
  3230.  
  3231.   .set_window_caption:
  3232.         dec     eax     ; subfunction #1 - set window caption
  3233.         jnz     .get_window_caption
  3234.  
  3235.         ; NOTE: only window owner thread can set its caption,
  3236.         ;       so there's no parameter for PID/TID
  3237.  
  3238.         mov     edi,[CURRENT_TASK]
  3239.         shl     edi,5
  3240.  
  3241.         ; have to check if caption is within application memory limit
  3242.         ; check is trivial, and if application resizes its memory,
  3243.         ;   caption still can become over bounds
  3244. ; diamond, 31.10.2006: check removed because with new memory manager
  3245. ; there can be valid data after APPDATA.mem_size bound
  3246. ;        mov     ecx,[edi*8+SLOT_BASE+APPDATA.mem_size]
  3247. ;        add     ecx,255 ; max caption length
  3248. ;        cmp     ebx,ecx
  3249. ;        ja      .exit_fail
  3250.  
  3251.         mov     [edi*8+SLOT_BASE+APPDATA.wnd_caption],ebx
  3252.         or      [edi+window_data+WDATA.fl_wstyle],WSTYLE_HASCAPTION
  3253.  
  3254.         call    draw_window_caption
  3255.  
  3256.         xor     eax,eax ; eax = 0 (success)
  3257.         ret
  3258.  
  3259.   .get_window_caption:
  3260.         dec     eax     ; subfunction #2 - get window caption
  3261.         jnz     .exit_fail
  3262.  
  3263.         ; not implemented yet
  3264.  
  3265.   .exit_fail:
  3266.         xor     eax,eax
  3267.         inc     eax     ; eax = 1 (fail)
  3268.         ret
  3269.  
  3270.  
  3271. sys_window_move:
  3272.  
  3273.         mov     edi,[CURRENT_TASK]
  3274.         shl     edi,5
  3275.         add     edi,window_data
  3276.  
  3277.         test    [edi+WDATA.fl_wstate],WSTATE_MAXIMIZED
  3278.         jnz     .window_move_return
  3279.  
  3280.         push    dword [edi + WDATA.box.left]  ; save old coordinates
  3281.         push    dword [edi + WDATA.box.top]
  3282.         push    dword [edi + WDATA.box.width]
  3283.         push    dword [edi + WDATA.box.height]
  3284.  
  3285.         cmp   eax,-1                  ; set new position and size
  3286.         je    .no_x_reposition
  3287.         mov     [edi + WDATA.box.left], eax
  3288.       .no_x_reposition:
  3289.         cmp   ebx,-1
  3290.         je    .no_y_reposition
  3291.         mov     [edi + WDATA.box.top], ebx
  3292.       .no_y_reposition:
  3293.  
  3294.         test    [edi+WDATA.fl_wstate],WSTATE_ROLLEDUP
  3295.         jnz     .no_y_resizing
  3296.  
  3297.         cmp   ecx,-1
  3298.         je    .no_x_resizing
  3299.         mov     [edi + WDATA.box.width], ecx
  3300.       .no_x_resizing:
  3301.         cmp   edx,-1
  3302.         je    .no_y_resizing
  3303.         mov     [edi + WDATA.box.height], edx
  3304.       .no_y_resizing:
  3305.  
  3306.         call  check_window_position
  3307.         call  set_window_clientbox
  3308.  
  3309.         pushad                       ; save for window fullscreen/resize
  3310.         mov   esi,edi
  3311.         sub   edi,window_data
  3312.         shr   edi,5
  3313.         shl   edi,8
  3314.         add   edi, SLOT_BASE + APPDATA.saved_box
  3315.         mov   ecx,4
  3316.         cld
  3317.         rep   movsd
  3318.         popad
  3319.  
  3320.         pushad                       ; calculcate screen at new position
  3321.         mov   eax, [edi + WDATA.box.left]
  3322.         mov   ebx, [edi + WDATA.box.top]
  3323.         mov   ecx, [edi + WDATA.box.width]
  3324.         mov   edx, [edi + WDATA.box.height]
  3325.         add   ecx,eax
  3326.         add   edx,ebx
  3327.  
  3328.         call  calculatescreen
  3329.         popad
  3330.  
  3331.         pop   edx                   ; calculcate screen at old position
  3332.         pop   ecx
  3333.         pop   ebx
  3334.         pop   eax
  3335.         add   ecx,eax
  3336.         add   edx,ebx
  3337.         mov   [dlx],eax             ; save for drawlimits
  3338.         mov   [dly],ebx
  3339.         mov   [dlxe],ecx
  3340.         mov   [dlye],edx
  3341.         call  calculatescreen
  3342.  
  3343.         mov   [edi + WDATA.fl_redraw], 1 ; flag the process as redraw
  3344.  
  3345.         mov   eax,edi               ; redraw screen at old position
  3346.         xor   esi,esi
  3347.         call  redrawscreen
  3348.  
  3349.         mov   [DONT_DRAW_MOUSE],byte 0 ; mouse pointer
  3350.         mov   [MOUSE_BACKGROUND],byte 0 ; no mouse under
  3351.         mov   [MOUSE_DOWN],byte 0 ; react to mouse up/down
  3352.  
  3353.         call  [draw_pointer]
  3354.  
  3355.         mov   [window_move_pr],0
  3356.  
  3357.       .window_move_return:
  3358.  
  3359.         ret
  3360.  
  3361. uglobal
  3362.   window_move_pr   dd  0x0
  3363.   window_move_eax  dd  0x0
  3364.   window_move_ebx  dd  0x0
  3365.   window_move_ecx  dd  0x0
  3366.   window_move_edx  dd  0x0
  3367. endg
  3368.  
  3369. ;ok - 100% work
  3370. ;nt - not tested
  3371. ;---------------------------------------------------------------------------------------------
  3372. ;eax
  3373. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3374. ;1 - change task. Ret nothing. Block. ok.
  3375. ;2 - performance control
  3376. ; ebx
  3377. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3378. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3379. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3380. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3381. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3382. ;eax
  3383. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3384. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3385. ;---------------------------------------------------------------------------------------------
  3386. sys_sheduler: ;noname & halyavin
  3387.     cmp eax,0
  3388.     je shed_counter
  3389.     cmp eax,2
  3390.     je perf_control
  3391.     cmp eax,3
  3392.     je rdmsr_instr
  3393.     cmp eax,4
  3394.     je wrmsr_instr
  3395.     cmp eax,1
  3396.     jne not_supported
  3397.     call change_task ;delay,0
  3398. ret
  3399. shed_counter:
  3400.     mov eax,[context_counter]
  3401.     mov [esp+36],eax
  3402. not_supported:
  3403. ret
  3404. perf_control:
  3405.     inc eax ;now eax=3
  3406.     cmp ebx,eax
  3407.     je cache_disable
  3408.     dec eax
  3409.     cmp ebx,eax
  3410.     je cache_enable
  3411.     dec eax
  3412.     cmp ebx,eax
  3413.     je is_cache_enabled
  3414.     dec eax
  3415.     cmp ebx,eax
  3416.     je modify_pce
  3417. ret
  3418.  
  3419. rdmsr_instr:
  3420. ;now counter in ecx
  3421. ;(edx:eax) esi:edi => edx:esi
  3422. mov eax,esi
  3423. rdmsr
  3424. mov [esp+36],eax
  3425. mov [esp+24],edx ;ret in ebx?
  3426. ret
  3427.  
  3428. wrmsr_instr:
  3429. ;now counter in ecx
  3430. ;(edx:eax) esi:edi => edx:esi
  3431.         ; Fast Call MSR can't be destroy
  3432.         ; Íî MSR_AMD_EFER ìîæíî èçìåíÿòü, ò.ê. â ýòîì ðåãèñòðå ëèø
  3433.         ; âêëþ÷àþòñÿ/âûêëþ÷àþòñÿ ðàñøèðåííûå âîçìîæíîñòè
  3434.         cmp     ecx, MSR_SYSENTER_CS
  3435.         je      @f
  3436.         cmp     ecx, MSR_SYSENTER_ESP
  3437.         je      @f
  3438.         cmp     ecx, MSR_SYSENTER_EIP
  3439.         je      @f
  3440.         cmp     ecx, MSR_AMD_STAR
  3441.         je      @f
  3442.  
  3443.         mov     eax, esi
  3444.         wrmsr
  3445.         ; mov   [esp + 36], eax
  3446.         ; mov   [esp + 24], edx ;ret in ebx?
  3447. @@:
  3448. ret
  3449.  
  3450. cache_disable:
  3451.        mov eax,cr0
  3452.        or  eax,01100000000000000000000000000000b
  3453.        mov cr0,eax
  3454.        wbinvd ;set MESI
  3455. ret
  3456.  
  3457. cache_enable:
  3458.        mov eax,cr0
  3459.        and eax,10011111111111111111111111111111b
  3460.        mov cr0,eax
  3461. ret
  3462.  
  3463. is_cache_enabled:
  3464.        mov eax,cr0
  3465.        mov ebx,eax
  3466.        and eax,01100000000000000000000000000000b
  3467.        jz cache_disabled
  3468.        mov [esp+36],ebx
  3469. cache_disabled:
  3470.        mov dword [esp+36],eax ;0
  3471. ret
  3472.  
  3473. modify_pce:
  3474.        mov eax,cr4
  3475. ;       mov ebx,0
  3476. ;       or  bx,100000000b ;pce
  3477. ;       xor eax,ebx ;invert pce
  3478.        bts eax,8 ;pce=cr4[8]
  3479.        mov cr4,eax
  3480.        mov [esp+36],eax
  3481. ret
  3482. ;---------------------------------------------------------------------------------------------
  3483.  
  3484.  
  3485. ; check if pixel is allowed to be drawn
  3486.  
  3487. checkpixel:
  3488.         push eax edx
  3489.  
  3490.         mov  edx,[Screen_Max_X]     ; screen x size
  3491.         mov  ecx, [_display_data]
  3492.  
  3493.         inc  edx
  3494.         imul edx, ebx
  3495.  
  3496.         add edx, ecx
  3497.         mov  dl, [eax+edx] ; lea eax, [...]
  3498.  
  3499.         xor  ecx, ecx
  3500.         mov  eax, [CURRENT_TASK]
  3501.         cmp  al, dl
  3502.         setne cl
  3503.  
  3504.         pop  edx eax
  3505.         ret
  3506.  
  3507. iglobal
  3508.   cpustring db 'CPU',0
  3509. endg
  3510.  
  3511. uglobal
  3512. background_defined    db    0    ; diamond, 11.04.2006
  3513. endg
  3514.  
  3515. align 4
  3516. ; check misc
  3517.  
  3518. checkmisc:
  3519.  
  3520.     cmp   [ctrl_alt_del], 1
  3521.     jne   nocpustart
  3522.  
  3523.         mov     ebp, cpustring
  3524.         call    fs_execute_from_sysdir
  3525.  
  3526.     mov   [ctrl_alt_del], 0
  3527.  
  3528. nocpustart:
  3529.     cmp   [mouse_active], 1
  3530.     jne   mouse_not_active
  3531.     mov   [mouse_active], 0
  3532.     xor   edi, edi
  3533.     mov   ecx,  [TASK_COUNT]
  3534. set_mouse_event:
  3535.     add   edi, 256
  3536.     or    [edi+SLOT_BASE+APPDATA.event_mask], dword 100000b
  3537.     loop  set_mouse_event
  3538.  
  3539. mouse_not_active:
  3540.     cmp   [REDRAW_BACKGROUND],byte 0               ; background update ?
  3541.     jz    nobackgr
  3542.     cmp    [background_defined], 0
  3543.     jz    nobackgr
  3544.     cmp   [REDRAW_BACKGROUND], byte 2
  3545.     jnz   no_set_bgr_event
  3546.     xor   edi, edi
  3547.     mov   ecx,  [TASK_COUNT]
  3548. set_bgr_event:
  3549.     add   edi, 256
  3550.     or    [edi+SLOT_BASE+APPDATA.event_mask], 16
  3551.     loop  set_bgr_event
  3552. no_set_bgr_event:
  3553. ;    mov   [draw_data+32 + RECT.left],dword 0
  3554. ;    mov   [draw_data+32 + RECT.top],dword 0
  3555. ;    mov   eax,[Screen_Max_X]
  3556. ;    mov   ebx,[Screen_Max_Y]
  3557. ;    mov   [draw_data+32 + RECT.right],eax
  3558. ;    mov   [draw_data+32 + RECT.bottom],ebx
  3559.     call  drawbackground
  3560.     mov   [REDRAW_BACKGROUND],byte 0
  3561.     mov   [MOUSE_BACKGROUND],byte 0
  3562.  
  3563. nobackgr:
  3564.  
  3565.     ; system shutdown request
  3566.  
  3567.     cmp  [SYS_SHUTDOWN],byte 0
  3568.     je   noshutdown
  3569.  
  3570.     mov  edx,[shutdown_processes]
  3571.  
  3572.     cmp  [SYS_SHUTDOWN],dl
  3573.     jne  no_mark_system_shutdown
  3574.  
  3575.     lea   ecx,[edx-1]
  3576.     mov   edx,OS_BASE+0x3040
  3577.     jecxz @f
  3578. markz:
  3579.     mov   [edx+TASKDATA.state],byte 3
  3580.     add   edx,0x20
  3581.     loop  markz
  3582. @@:
  3583.  
  3584.   no_mark_system_shutdown:
  3585.  
  3586.     call [disable_mouse]
  3587.  
  3588.     dec  byte [SYS_SHUTDOWN]
  3589.     je   system_shutdown
  3590.  
  3591. noshutdown:
  3592.  
  3593.  
  3594.     mov   eax,[TASK_COUNT]                  ; termination
  3595.     mov   ebx,TASK_DATA+TASKDATA.state
  3596.     mov   esi,1
  3597.  
  3598. newct:
  3599.     mov   cl,[ebx]
  3600.     cmp   cl,byte 3
  3601.     jz    terminate
  3602.     cmp   cl,byte 4
  3603.     jz    terminate
  3604.  
  3605.     add   ebx,0x20
  3606.     inc   esi
  3607.     dec   eax
  3608.     jnz   newct
  3609.     ret
  3610.  
  3611. ; redraw screen
  3612.  
  3613. redrawscreen:
  3614.  
  3615. ; eax , if process window_data base is eax, do not set flag/limits
  3616.  
  3617.          pushad
  3618.          push  eax
  3619.  
  3620. ;;;         mov   ebx,2
  3621. ;;;         call  delay_hs
  3622.  
  3623.          ;mov   ecx,0               ; redraw flags for apps
  3624.          xor   ecx,ecx
  3625.        newdw2:
  3626.  
  3627.          inc   ecx
  3628.          push  ecx
  3629.  
  3630.          mov   eax,ecx
  3631.          shl   eax,5
  3632.          add   eax,window_data
  3633.  
  3634.          cmp   eax,[esp+4]
  3635.          je    not_this_task
  3636.                                    ; check if window in redraw area
  3637.          mov   edi,eax
  3638.  
  3639.          cmp   ecx,1               ; limit for background
  3640.          jz    bgli
  3641.  
  3642.          mov   eax, [edi + WDATA.box.left]
  3643.          mov   ebx, [edi + WDATA.box.top]
  3644.          mov   ecx, [edi + WDATA.box.width]
  3645.          mov   edx, [edi + WDATA.box.height]
  3646.          add   ecx,eax
  3647.          add   edx,ebx
  3648.  
  3649.          mov   ecx,[dlye]   ; ecx = area y end     ebx = window y start
  3650.          cmp   ecx,ebx
  3651.          jb    ricino
  3652.  
  3653.          mov   ecx,[dlxe]   ; ecx = area x end     eax = window x start
  3654.          cmp   ecx,eax
  3655.          jb    ricino
  3656.  
  3657.          mov   eax, [edi + WDATA.box.left]
  3658.          mov   ebx, [edi + WDATA.box.top]
  3659.          mov   ecx, [edi + WDATA.box.width]
  3660.          mov   edx, [edi + WDATA.box.height]
  3661.          add   ecx, eax
  3662.          add   edx, ebx
  3663.  
  3664.          mov   eax,[dly]    ; eax = area y start     edx = window y end
  3665.          cmp   edx,eax
  3666.          jb    ricino
  3667.  
  3668.          mov   eax,[dlx]    ; eax = area x start     ecx = window x end
  3669.          cmp   ecx,eax
  3670.          jb    ricino
  3671.  
  3672.         bgli:
  3673.  
  3674.          cmp   ecx,1
  3675.          jnz   .az
  3676.          mov   al,[REDRAW_BACKGROUND]
  3677.          cmp   al,2
  3678.          jz    newdw8
  3679.          test  al,al
  3680.          jz    .az
  3681.          lea   eax,[edi+draw_data-window_data]
  3682.          mov   ebx,[dlx]
  3683.          cmp   ebx,[eax+RECT.left]
  3684.          jae   @f
  3685.          mov   [eax+RECT.left],ebx
  3686.         @@:
  3687.          mov   ebx,[dly]
  3688.          cmp   ebx,[eax+RECT.top]
  3689.          jae   @f
  3690.          mov   [eax+RECT.top],ebx
  3691.         @@:
  3692.          mov   ebx,[dlxe]
  3693.          cmp   ebx,[eax+RECT.right]
  3694.          jbe   @f
  3695.          mov   [eax+RECT.right],ebx
  3696.         @@:
  3697.          mov   ebx,[dlye]
  3698.          cmp   ebx,[eax+RECT.bottom]
  3699.          jbe   @f
  3700.          mov   [eax+RECT.bottom],ebx
  3701.         @@:
  3702.          jmp   newdw8
  3703.         .az:
  3704.  
  3705.          mov   eax,edi
  3706.          add   eax,draw_data-window_data
  3707.  
  3708.          mov   ebx,[dlx]          ; set limits
  3709.          mov   [eax + RECT.left], ebx
  3710.          mov   ebx,[dly]
  3711.          mov   [eax + RECT.top], ebx
  3712.          mov   ebx,[dlxe]
  3713.          mov   [eax + RECT.right], ebx
  3714.          mov   ebx,[dlye]
  3715.          mov   [eax + RECT.bottom], ebx
  3716.  
  3717.          sub   eax,draw_data-window_data
  3718.  
  3719.          cmp   dword [esp],1
  3720.          jne   nobgrd
  3721.          mov   byte [REDRAW_BACKGROUND], 1
  3722.  
  3723.        newdw8:
  3724.        nobgrd:
  3725.  
  3726.          mov   [eax + WDATA.fl_redraw],byte 1    ; mark as redraw
  3727.  
  3728.        ricino:
  3729.  
  3730.        not_this_task:
  3731.  
  3732.          pop   ecx
  3733.  
  3734.          cmp   ecx,[TASK_COUNT]
  3735.          jle   newdw2
  3736.  
  3737.          pop  eax
  3738.          popad
  3739.  
  3740.          ret
  3741.  
  3742. calculatebackground:   ; background
  3743.  
  3744.         ; all black
  3745.  
  3746.         mov   edi, [img_background]  ;IMG_BACKGROUND                 ; set background to black
  3747.         xor   eax, eax
  3748.         mov   ecx, 1023    ;0x0fff00 / 4
  3749.         cld
  3750.         rep   stosd
  3751.  
  3752.         mov   edi, [_display_data]              ; set os to use all pixels
  3753.         mov   eax,0x01010101
  3754.         mov   ecx,1280*1024 / 4
  3755.         rep   stosd
  3756.  
  3757.         mov   byte [REDRAW_BACKGROUND], 0              ; do not draw background!
  3758.  
  3759.         ret
  3760.  
  3761. uglobal
  3762.   imax    dd 0x0
  3763. endg
  3764.  
  3765.  
  3766.  
  3767. delay_ms:     ; delay in 1/1000 sec
  3768.  
  3769.  
  3770.         push  eax
  3771.         push  ecx
  3772.  
  3773.         mov   ecx,esi
  3774.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3775.         imul  ecx, 33941
  3776.         shr   ecx, 9
  3777.         ; </CPU clock fix>
  3778.  
  3779.         in    al,0x61
  3780.         and   al,0x10
  3781.         mov   ah,al
  3782.         cld
  3783.  
  3784.  cnt1:  in    al,0x61
  3785.         and   al,0x10
  3786.         cmp   al,ah
  3787.         jz    cnt1
  3788.  
  3789.         mov   ah,al
  3790.         loop  cnt1
  3791.  
  3792.         pop   ecx
  3793.         pop   eax
  3794.  
  3795.         ret
  3796.  
  3797.  
  3798. set_app_param:
  3799.         mov     edi, [TASK_BASE]
  3800.         mov     [edi + TASKDATA.event_mask], ebx
  3801.         ret
  3802.  
  3803.  
  3804.  
  3805. delay_hs:     ; delay in 1/100 secs
  3806. ; ebx = delay time
  3807.         push  ecx
  3808.         push  edx
  3809.  
  3810.         mov   edx,[timer_ticks]
  3811.  
  3812.       newtic:
  3813.         mov   ecx,[timer_ticks]
  3814.         sub   ecx,edx
  3815.         cmp   ecx,ebx
  3816.         jae   zerodelay
  3817.  
  3818.         call  change_task
  3819.  
  3820.         jmp   newtic
  3821.  
  3822.       zerodelay:
  3823.         pop   edx
  3824.         pop   ecx
  3825.  
  3826.         ret
  3827.  
  3828.  
  3829. memmove:       ; memory move in bytes
  3830.  
  3831. ; eax = from
  3832. ; ebx = to
  3833. ; ecx = no of bytes
  3834.     test ecx, ecx
  3835.     jle  .ret
  3836.  
  3837.  
  3838.     push esi edi ecx
  3839.  
  3840.     mov  edi, ebx
  3841.     mov  esi, eax
  3842.  
  3843.     test ecx, not 11b
  3844.     jz   @f
  3845.  
  3846.     push ecx
  3847.     shr  ecx, 2
  3848.     rep  movsd
  3849.     pop  ecx
  3850.     and  ecx, 11b
  3851.     jz   .finish
  3852.   @@:
  3853.     rep  movsb
  3854.  
  3855.   .finish:
  3856.     pop  ecx edi esi
  3857.   .ret:
  3858.     ret
  3859.  
  3860.  
  3861. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3862. ;align 4
  3863. ;
  3864. ;read_floppy_file:
  3865. ;
  3866. ;; as input
  3867. ;;
  3868. ;; eax pointer to file
  3869. ;; ebx file lenght
  3870. ;; ecx start 512 byte block number
  3871. ;; edx number of blocks to read
  3872. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3873. ;;
  3874. ;;
  3875. ;; on return
  3876. ;;
  3877. ;; eax = 0 command succesful
  3878. ;;       1 no fd base and/or partition defined
  3879. ;;       2 yet unsupported FS
  3880. ;;       3 unknown FS
  3881. ;;       4 partition not defined at hd
  3882. ;;       5 file not found
  3883. ;; ebx = size of file
  3884. ;
  3885. ;     mov   edi,[TASK_BASE]
  3886. ;     add   edi,0x10
  3887. ;     add   esi,[edi]
  3888. ;     add   eax,[edi]
  3889. ;
  3890. ;     pushad
  3891. ;     mov  edi,esi
  3892. ;     add  edi,1024
  3893. ;     mov  esi,0x100000+19*512
  3894. ;     sub  ecx,1
  3895. ;     shl  ecx,9
  3896. ;     add  esi,ecx
  3897. ;     shl  edx,9
  3898. ;     mov  ecx,edx
  3899. ;     cld
  3900. ;     rep  movsb
  3901. ;     popad
  3902. ;
  3903. ;     mov   [esp+36],eax
  3904. ;     mov   [esp+24],ebx
  3905. ;     ret
  3906.  
  3907.  
  3908.  
  3909. align 4
  3910.  
  3911. sys_programirq:
  3912.  
  3913.     mov   eax, [TASK_BASE]
  3914.     add   ebx, [eax + TASKDATA.mem_start]
  3915.  
  3916.     cmp   ecx, 16
  3917.     jae   .not_owner
  3918.     mov   edi, [eax + TASKDATA.pid]
  3919.     cmp   edi, [irq_owner + 4 * ecx]
  3920.     je    .spril1
  3921. .not_owner:
  3922.     xor   ecx, ecx
  3923.     jmp   .end
  3924.   .spril1:
  3925.  
  3926.     shl   ecx, 6
  3927.     mov   esi, ebx
  3928.     lea   edi, [irq00read + ecx]
  3929.     push  16
  3930.     pop   ecx
  3931.  
  3932.     cld
  3933.     rep   movsd
  3934.   .end:
  3935.     mov   [esp+32], ecx
  3936.     ret
  3937.  
  3938.  
  3939. align 4
  3940.  
  3941. get_irq_data:
  3942.      movzx esi, bh                       ; save number of subfunction, if bh = 1, return data size, otherwise, read data
  3943.      xor   bh, bh
  3944.      cmp   ebx, 16
  3945.      jae   .not_owner
  3946.      mov   edx, [4 * ebx + irq_owner]    ; check for irq owner
  3947.  
  3948.      mov   eax,[TASK_BASE]
  3949.  
  3950.      cmp   edx,[eax+TASKDATA.pid]
  3951.      je    gidril1
  3952. .not_owner:
  3953.      xor   edx, edx
  3954.      dec   edx
  3955.      jmp   gid1
  3956.  
  3957.   gidril1:
  3958.  
  3959.      shl   ebx, 12
  3960.      lea   eax, [ebx + IRQ_SAVE]         ; calculate address of the beginning of buffer + 0x0 - data size
  3961.      mov   edx, [eax]                    ;                                              + 0x4 - data offset
  3962.      dec   esi
  3963.      jz    gid1
  3964.      test  edx, edx                      ; check if buffer is empty
  3965.      jz    gid1
  3966.  
  3967.      mov   ebx, [eax + 0x4]
  3968.      mov   edi, ecx
  3969.  
  3970.      mov   ecx, 4000                     ; buffer size, used frequently
  3971.  
  3972.      cmp   ebx, ecx                      ; check for the end of buffer, if end of buffer, begin cycle again
  3973.      jb    @f
  3974.  
  3975.      xor   ebx, ebx
  3976.  
  3977.    @@:
  3978.  
  3979.      lea   esi, [ebx + edx]              ; calculate data size and offset
  3980.      cld
  3981.      cmp   esi, ecx                      ; if greater than the buffer size, begin cycle again
  3982.      jbe   @f
  3983.  
  3984.      sub   ecx, ebx
  3985.      sub   edx, ecx
  3986.  
  3987.      lea   esi, [eax + ebx + 0x10]
  3988.      rep   movsb
  3989.  
  3990.      xor   ebx, ebx
  3991.    @@:
  3992.      lea   esi, [eax + ebx + 0x10]
  3993.      mov   ecx, edx
  3994.      add   ebx, edx
  3995.  
  3996.      rep   movsb
  3997.      mov   edx, [eax]
  3998.      mov   [eax], ecx                    ; set data size to zero
  3999.      mov   [eax + 0x4], ebx              ; set data offset
  4000.  
  4001.    gid1:
  4002.      mov   [esp+32], edx                 ; eax
  4003.      ret
  4004.  
  4005.  
  4006. set_io_access_rights:
  4007.  
  4008.      pushad
  4009.  
  4010.      mov edi, tss._io_map_0
  4011.  
  4012. ;     mov   ecx,eax
  4013. ;     and   ecx,7    ; offset in byte
  4014.  
  4015. ;     shr   eax,3    ; number of byte
  4016. ;     add   edi,eax
  4017.  
  4018. ;     mov   ebx,1
  4019. ;     shl   ebx,cl
  4020.  
  4021.      cmp   ebp,0                ; enable access - ebp = 0
  4022.      jne   siar1
  4023.  
  4024. ;     not   ebx
  4025. ;     and   [edi],byte bl
  4026.      btr [edi], eax
  4027.  
  4028.      popad
  4029.  
  4030.      ret
  4031.  
  4032. siar1:
  4033.  
  4034.      bts [edi], eax
  4035.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4036.  
  4037.      popad
  4038.  
  4039.      ret
  4040.  
  4041. r_f_port_area:
  4042.  
  4043.      test  eax, eax
  4044.      jnz   free_port_area
  4045. ;     je    r_port_area
  4046. ;     jmp   free_port_area
  4047.  
  4048. ;   r_port_area:
  4049.  
  4050.      pushad
  4051.  
  4052.      cmp   ebx,ecx            ; beginning > end ?
  4053.      ja    rpal1
  4054.      cmp   ecx,65536
  4055.      jae   rpal1
  4056.      mov   esi,[RESERVED_PORTS]
  4057.      test  esi,esi            ; no reserved areas ?
  4058.      je    rpal2
  4059.      cmp   esi,255            ; max reserved
  4060.      jae   rpal1
  4061.  rpal3:
  4062.      mov   edi,esi
  4063.      shl   edi,4
  4064.      add   edi,RESERVED_PORTS
  4065.      cmp   ebx,[edi+8]
  4066.      ja    rpal4
  4067.      cmp   ecx,[edi+4]
  4068.      jae   rpal1
  4069. ;     jb    rpal4
  4070. ;     jmp   rpal1
  4071.  rpal4:
  4072.  
  4073.      dec   esi
  4074.      jnz   rpal3
  4075.      jmp   rpal2
  4076.    rpal1:
  4077.      popad
  4078.      mov   eax,1
  4079.      ret
  4080.  
  4081.    rpal2:
  4082.      popad
  4083.  
  4084.  
  4085.      ; enable port access at port IO map
  4086.      cli
  4087.      pushad                        ; start enable io map
  4088.  
  4089.      cmp   ecx,65536 ;16384
  4090.      jae   no_unmask_io ; jge
  4091.  
  4092.      mov   eax,ebx
  4093.  
  4094.    new_port_access:
  4095.  
  4096.      pushad
  4097.  
  4098.      xor   ebp,ebp                ; enable - eax = port
  4099.      call  set_io_access_rights
  4100.  
  4101.      popad
  4102.  
  4103.      inc   eax
  4104.      cmp   eax,ecx
  4105.      jbe   new_port_access
  4106.  
  4107.    no_unmask_io:
  4108.  
  4109.      popad                         ; end enable io map
  4110.      sti
  4111.  
  4112.      mov   edi,[RESERVED_PORTS]
  4113.      add   edi,1
  4114.      mov   [RESERVED_PORTS],edi
  4115.      shl   edi,4
  4116.      add   edi,RESERVED_PORTS
  4117.      mov   esi,[TASK_BASE]
  4118.      mov   esi,[esi+TASKDATA.pid]
  4119.      mov   [edi],esi
  4120.      mov   [edi+4],ebx
  4121.      mov   [edi+8],ecx
  4122.  
  4123.      xor   eax, eax
  4124.      ret
  4125.  
  4126. free_port_area:
  4127.  
  4128.      pushad
  4129.  
  4130.      mov   esi,[RESERVED_PORTS]     ; no reserved areas ?
  4131.      test  esi,esi
  4132.      je    frpal2
  4133.      mov   edx,[TASK_BASE]
  4134.      mov   edx,[edx+TASKDATA.pid]
  4135.    frpal3:
  4136.      mov   edi,esi
  4137.      shl   edi,4
  4138.      add   edi,RESERVED_PORTS
  4139.      cmp   edx,[edi]
  4140.      jne   frpal4
  4141.      cmp   ebx,[edi+4]
  4142.      jne   frpal4
  4143.      cmp   ecx,[edi+8]
  4144.      jne   frpal4
  4145.      jmp   frpal1
  4146.    frpal4:
  4147.      dec   esi
  4148.      jnz   frpal3
  4149.    frpal2:
  4150.      popad
  4151.      mov   eax,1
  4152.      ret
  4153.    frpal1:
  4154.      mov   ecx,256
  4155.      sub   ecx,esi
  4156.      shl   ecx,4
  4157.      mov   esi,edi
  4158.      add   esi,16
  4159.      cld
  4160.      rep   movsb
  4161.  
  4162.      dec   dword [RESERVED_PORTS]
  4163.  
  4164.      popad
  4165.  
  4166.  
  4167.      ; disable port access at port IO map
  4168.  
  4169.      pushad                        ; start disable io map
  4170.  
  4171.      cmp   ecx,65536 ;16384
  4172.      jge   no_mask_io
  4173.  
  4174.      mov   eax,ebx
  4175.  
  4176.    new_port_access_disable:
  4177.  
  4178.      pushad
  4179.  
  4180.      mov   ebp,1                  ; disable - eax = port
  4181.      call  set_io_access_rights
  4182.  
  4183.      popad
  4184.  
  4185.      inc   eax
  4186.      cmp   eax,ecx
  4187.      jbe   new_port_access_disable
  4188.  
  4189.    no_mask_io:
  4190.  
  4191.      popad                         ; end disable io map
  4192.  
  4193.      xor   eax, eax
  4194.      ret
  4195.  
  4196.  
  4197. reserve_free_irq:
  4198.  
  4199.      xor   esi, esi
  4200.      inc   esi
  4201.      cmp   ecx, 16
  4202.      jae   ril1
  4203.  
  4204.      push  ecx
  4205.      lea   ecx, [irq_owner + 4 * ecx]
  4206.      mov   edx, [ecx]
  4207.      mov   eax, [TASK_BASE]
  4208.      mov   edi, [eax + TASKDATA.pid]
  4209.      pop   eax
  4210.      dec   ebx
  4211.      jnz   reserve_irq
  4212.  
  4213.      cmp   edx, edi
  4214.      jne   ril1
  4215.      dec   esi
  4216.      mov   [ecx], esi
  4217.  
  4218.      jmp   ril1
  4219.  
  4220.   reserve_irq:
  4221.  
  4222.      cmp   dword [ecx], 0
  4223.      jne   ril1
  4224.  
  4225.      mov   ebx, [f_irqs + 4 * eax]
  4226.  
  4227.      stdcall attach_int_handler, eax, ebx, dword 0
  4228.  
  4229.      mov   [ecx], edi
  4230.  
  4231.      dec   esi
  4232.    ril1:
  4233.      mov   [esp+32], esi ; return in eax
  4234.      ret
  4235.  
  4236. iglobal
  4237. f_irqs:
  4238.      dd 0x0
  4239.      dd 0x0
  4240.      dd p_irq2
  4241.      dd p_irq3
  4242.      dd p_irq4
  4243.      dd p_irq5
  4244.      dd p_irq6
  4245.      dd p_irq7
  4246.      dd p_irq8
  4247.      dd p_irq9
  4248.      dd p_irq10
  4249.      dd p_irq11
  4250.      dd 0x0
  4251.      dd 0x0
  4252.      dd p_irq14
  4253.      dd p_irq15
  4254.  
  4255. endg
  4256.  
  4257. drawbackground:
  4258.        inc   [mouse_pause]
  4259.        cmp   [SCR_MODE],word 0x12
  4260.        je   dbrv20
  4261.      dbrv12:
  4262.        cmp  [SCR_MODE],word 0100000000000000b
  4263.        jge  dbrv20
  4264.        cmp  [SCR_MODE],word 0x13
  4265.        je   dbrv20
  4266.        call  vesa12_drawbackground
  4267.        dec   [mouse_pause]
  4268.        call   [draw_pointer]
  4269.        ret
  4270.      dbrv20:
  4271.        cmp   [BgrDrawMode],dword 1
  4272.        jne   bgrstr
  4273.        call  vesa20_drawbackground_tiled
  4274.        dec   [mouse_pause]
  4275.        call   [draw_pointer]
  4276.        ret
  4277.      bgrstr:
  4278.        call  vesa20_drawbackground_stretch
  4279.        dec   [mouse_pause]
  4280.        call   [draw_pointer]
  4281.        ret
  4282.  
  4283. align 4
  4284.  
  4285. syscall_putimage:                       ; PutImage
  4286. sys_putimage:
  4287.      test  ecx,0x80008000
  4288.      jnz   .exit
  4289.      test  ecx,0x0000FFFF
  4290.      jz    .exit
  4291.      test  ecx,0xFFFF0000
  4292.      jnz   @f
  4293.   .exit:
  4294.      ret
  4295.  @@:
  4296.         mov     edi,[current_slot]
  4297.         add     dx,word[edi+APPDATA.wnd_clientbox.top]
  4298.         rol     edx,16
  4299.         add     dx,word[edi+APPDATA.wnd_clientbox.left]
  4300.         rol     edx,16
  4301.   .forced:
  4302.         push    ebp esi 0
  4303.         mov     ebp, putimage_get24bpp
  4304.         mov     esi, putimage_init24bpp
  4305. sys_putimage_bpp:
  4306. ;        call    [disable_mouse] ; this will be done in xxx_putimage
  4307. ;        mov     eax, vga_putimage
  4308.         cmp     [SCR_MODE], word 0x12
  4309.         jz      @f   ;.doit
  4310.         mov     eax, vesa12_putimage
  4311.         cmp     [SCR_MODE], word 0100000000000000b
  4312.         jae     @f
  4313.         cmp     [SCR_MODE], word 0x13
  4314.         jnz     .doit
  4315. @@:
  4316.         mov     eax, vesa20_putimage
  4317. .doit:
  4318.         inc     [mouse_pause]
  4319.         call    eax
  4320.         dec     [mouse_pause]
  4321.         pop     ebp esi ebp
  4322.         jmp     [draw_pointer]
  4323.  
  4324. syscall_putimage_palette:
  4325.         mov     edi, esi
  4326.         mov     esi, edx
  4327.         mov     edx, ecx
  4328.         mov     ecx, ebx
  4329.         mov     ebx, eax
  4330. sys_putimage_palette:
  4331. ; ebx = pointer to image
  4332. ; ecx = [xsize]*65536 + [ysize]
  4333. ; edx = [xstart]*65536 + [ystart]
  4334. ; esi = number of bits per pixel, must be 8, 24 or 32
  4335. ; edi = pointer to palette
  4336. ; ebp = row delta
  4337.         mov     eax, [CURRENT_TASK]
  4338.         shl     eax, 8
  4339.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4340.         rol     edx, 16
  4341.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4342.         rol     edx, 16
  4343. .forced:
  4344.         push    ebp esi ebp
  4345.         cmp     esi, 8
  4346.         jnz     @f
  4347.         mov     ebp, putimage_get8bpp
  4348.         mov     esi, putimage_init8bpp
  4349.         jmp     sys_putimage_bpp
  4350. @@:
  4351.         cmp     esi, 16
  4352.         jnz     @f
  4353.         mov     ebp, putimage_get16bpp
  4354.         mov     esi, putimage_init16bpp
  4355.         jmp     sys_putimage_bpp
  4356. @@:
  4357.         cmp     esi, 24
  4358.         jnz     @f
  4359.         mov     ebp, putimage_get24bpp
  4360.         mov     esi, putimage_init24bpp
  4361.         jmp     sys_putimage_bpp
  4362. @@:
  4363.         cmp     esi, 32
  4364.         jnz     @f
  4365.         mov     ebp, putimage_get32bpp
  4366.         mov     esi, putimage_init32bpp
  4367.         jmp     sys_putimage_bpp
  4368. @@:
  4369.         pop     ebp esi ebp
  4370.         ret
  4371.  
  4372. putimage_init24bpp:
  4373.         lea     eax, [eax*3]
  4374. putimage_init8bpp:
  4375.         ret
  4376.  
  4377. putimage_get24bpp:
  4378.         mov     eax, [esi]
  4379.         add     esi, 3
  4380.         ret     4
  4381. putimage_get8bpp:
  4382.         movzx   eax, byte [esi]
  4383.         push    edx
  4384.         mov     edx, [esp+8]
  4385.         mov     eax, [edx+eax*4]
  4386.         pop     edx
  4387.         inc     esi
  4388.         ret     4
  4389.  
  4390. putimage_init32bpp:
  4391.         shl     eax, 2
  4392.         ret
  4393. putimage_get32bpp:
  4394.         lodsd
  4395.         ret     4
  4396.  
  4397. putimage_init16bpp:
  4398.         add     eax, eax
  4399.         ret
  4400. putimage_get16bpp:
  4401. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4402.         push    ecx edx
  4403.         movzx   eax, word [esi]
  4404.         add     esi, 2
  4405.         mov     ecx, eax
  4406.         mov     edx, eax
  4407.         and     eax, 0x1F
  4408.         and     ecx, 0x1F shl 5
  4409.         and     edx, 0x1F shl 10
  4410.         shl     eax, 3
  4411.         shl     ecx, 6
  4412.         shl     edx, 9
  4413.         or      eax, ecx
  4414.         or      eax, edx
  4415.         pop     edx ecx
  4416.         ret
  4417.  
  4418. ; eax x beginning
  4419. ; ebx y beginning
  4420. ; ecx x end
  4421.         ; edx y end
  4422. ; edi color
  4423.  
  4424. __sys_drawbar:
  4425.         mov     esi,[current_slot]
  4426.         add     eax,[esi+APPDATA.wnd_clientbox.left]
  4427.         add     ecx,[esi+APPDATA.wnd_clientbox.left]
  4428.         add     ebx,[esi+APPDATA.wnd_clientbox.top]
  4429.         add     edx,[esi+APPDATA.wnd_clientbox.top]
  4430.   .forced:
  4431.     inc   [mouse_pause]
  4432. ;        call    [disable_mouse]
  4433.     cmp   [SCR_MODE],word 0x12
  4434.     je   dbv20
  4435.    sdbv20:
  4436.     cmp  [SCR_MODE],word 0100000000000000b
  4437.     jge  dbv20
  4438.     cmp  [SCR_MODE],word 0x13
  4439.     je   dbv20
  4440.     call vesa12_drawbar
  4441.     dec   [mouse_pause]
  4442.     call   [draw_pointer]
  4443.     ret
  4444.   dbv20:
  4445.     call vesa20_drawbar
  4446.     dec   [mouse_pause]
  4447.     call   [draw_pointer]
  4448.     ret
  4449.  
  4450.  
  4451.  
  4452. kb_read:
  4453.  
  4454.         push    ecx edx
  4455.  
  4456.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4457.       kr_loop:
  4458.         in      al,0x64
  4459.         test    al,1
  4460.         jnz     kr_ready
  4461.         loop    kr_loop
  4462.         mov     ah,1
  4463.         jmp     kr_exit
  4464.       kr_ready:
  4465.         push    ecx
  4466.         mov     ecx,32
  4467.       kr_delay:
  4468.         loop    kr_delay
  4469.         pop     ecx
  4470.         in      al,0x60
  4471.         xor     ah,ah
  4472.       kr_exit:
  4473.  
  4474.         pop     edx ecx
  4475.  
  4476.         ret
  4477.  
  4478.  
  4479. kb_write:
  4480.  
  4481.         push    ecx edx
  4482.  
  4483.         mov     dl,al
  4484. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4485. ;      kw_loop1:
  4486. ;        in      al,0x64
  4487. ;        test    al,0x20
  4488. ;        jz      kw_ok1
  4489. ;        loop    kw_loop1
  4490. ;        mov     ah,1
  4491. ;        jmp     kw_exit
  4492. ;      kw_ok1:
  4493.         in      al,0x60
  4494.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4495.       kw_loop:
  4496.         in      al,0x64
  4497.         test    al,2
  4498.         jz      kw_ok
  4499.         loop    kw_loop
  4500.         mov     ah,1
  4501.         jmp     kw_exit
  4502.       kw_ok:
  4503.         mov     al,dl
  4504.         out     0x60,al
  4505.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4506.       kw_loop3:
  4507.         in      al,0x64
  4508.         test    al,2
  4509.         jz      kw_ok3
  4510.         loop    kw_loop3
  4511.         mov     ah,1
  4512.         jmp     kw_exit
  4513.       kw_ok3:
  4514.         mov     ah,8
  4515.       kw_loop4:
  4516.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4517.       kw_loop5:
  4518.         in      al,0x64
  4519.         test    al,1
  4520.         jnz     kw_ok4
  4521.         loop    kw_loop5
  4522.         dec     ah
  4523.         jnz     kw_loop4
  4524.       kw_ok4:
  4525.         xor     ah,ah
  4526.       kw_exit:
  4527.  
  4528.         pop     edx ecx
  4529.  
  4530.         ret
  4531.  
  4532.  
  4533. kb_cmd:
  4534.  
  4535.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4536.       c_wait:
  4537.         in      al,0x64
  4538.         test    al,2
  4539.         jz      c_send
  4540.         loop    c_wait
  4541.         jmp     c_error
  4542.       c_send:
  4543.         mov     al,bl
  4544.         out     0x64,al
  4545.         mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4546.       c_accept:
  4547.         in      al,0x64
  4548.         test    al,2
  4549.         jz      c_ok
  4550.         loop    c_accept
  4551.       c_error:
  4552.         mov     ah,1
  4553.         jmp     c_exit
  4554.       c_ok:
  4555.         xor     ah,ah
  4556.       c_exit:
  4557.         ret
  4558.  
  4559.  
  4560. setmouse:  ; set mousepicture -pointer
  4561.            ; ps2 mouse enable
  4562.  
  4563.      mov     [MOUSE_PICTURE],dword mousepointer
  4564.  
  4565.      cli
  4566.  
  4567.      ret
  4568.  
  4569.  
  4570. _rdtsc:
  4571.      bt [cpu_caps], CAPS_TSC
  4572.      jnc ret_rdtsc
  4573.      rdtsc
  4574.      ret
  4575.    ret_rdtsc:
  4576.      mov   edx,0xffffffff
  4577.      mov   eax,0xffffffff
  4578.      ret
  4579.  
  4580. rerouteirqs:
  4581.  
  4582.         cli
  4583.  
  4584.         mov     al,0x11         ;  icw4, edge triggered
  4585.         out     0x20,al
  4586.         call    pic_delay
  4587.         out     0xA0,al
  4588.         call    pic_delay
  4589.  
  4590.         mov     al,0x20         ;  generate 0x20 +
  4591.         out     0x21,al
  4592.         call    pic_delay
  4593.         mov     al,0x28         ;  generate 0x28 +
  4594.         out     0xA1,al
  4595.         call    pic_delay
  4596.  
  4597.         mov     al,0x04         ;  slave at irq2
  4598.         out     0x21,al
  4599.         call    pic_delay
  4600.         mov     al,0x02         ;  at irq9
  4601.         out     0xA1,al
  4602.         call    pic_delay
  4603.  
  4604.         mov     al,0x01         ;  8086 mode
  4605.         out     0x21,al
  4606.         call    pic_delay
  4607.         out     0xA1,al
  4608.         call    pic_delay
  4609.  
  4610.         mov     al,255          ; mask all irq's
  4611.         out     0xA1,al
  4612.         call    pic_delay
  4613.         out     0x21,al
  4614.         call    pic_delay
  4615.  
  4616.         mov     ecx,0x1000
  4617.         cld
  4618. picl1:  call    pic_delay
  4619.         loop    picl1
  4620.  
  4621.         mov     al,255          ; mask all irq's
  4622.         out     0xA1,al
  4623.         call    pic_delay
  4624.         out     0x21,al
  4625.         call    pic_delay
  4626.  
  4627.         cli
  4628.  
  4629.         ret
  4630.  
  4631.  
  4632. pic_delay:
  4633.  
  4634.         jmp     pdl1
  4635. pdl1:   ret
  4636.  
  4637.  
  4638. sys_msg_board_str:
  4639.  
  4640.      pushad
  4641.    @@:
  4642.      cmp    [esi],byte 0
  4643.      je     @f
  4644.      mov    eax,1
  4645.      movzx  ebx,byte [esi]
  4646.      call   sys_msg_board
  4647.      inc    esi
  4648.      jmp    @b
  4649.    @@:
  4650.      popad
  4651.      ret
  4652.  
  4653. sys_msg_board_byte:
  4654. ; in: al = byte to display
  4655. ; out: nothing
  4656. ; destroys: nothing
  4657.         pushad
  4658.         mov     ecx, 2
  4659.         shl     eax, 24
  4660.         jmp     @f
  4661.  
  4662. sys_msg_board_word:
  4663. ; in: ax = word to display
  4664. ; out: nothing
  4665. ; destroys: nothing
  4666.         pushad
  4667.         mov     ecx, 4
  4668.         shl     eax, 16
  4669.         jmp     @f
  4670.  
  4671. sys_msg_board_dword:
  4672. ; in: eax = dword to display
  4673. ; out: nothing
  4674. ; destroys: nothing
  4675.         pushad
  4676.         mov     ecx, 8
  4677. @@:
  4678.         push    ecx
  4679.         rol     eax, 4
  4680.         push    eax
  4681.         and     al, 0xF
  4682.         cmp     al, 10
  4683.         sbb     al, 69h
  4684.         das
  4685.         mov     bl, al
  4686.         xor     eax, eax
  4687.         inc     eax
  4688.         call    sys_msg_board
  4689.         pop     eax
  4690.         pop     ecx
  4691.         loop    @b
  4692.         popad
  4693.         ret
  4694.  
  4695. uglobal
  4696.   msg_board_data: times 4096 db 0
  4697.   msg_board_count dd 0x0
  4698. endg
  4699.  
  4700. sys_msg_board:
  4701.  
  4702. ; eax=1 : write :  bl byte to write
  4703. ; eax=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4704.  
  4705.         mov     ecx, [msg_board_count]
  4706.         cmp     eax, 1
  4707.         jne     .smbl1
  4708.  
  4709.  
  4710.         mov     [msg_board_data+ecx],bl
  4711.         inc     ecx
  4712.         and     ecx, 4095
  4713.         mov     [msg_board_count], ecx
  4714.         mov     [check_idle_semaphore], 5
  4715.         ret
  4716. .smbl1:
  4717.         cmp     eax, 2
  4718.         jne     .smbl2
  4719.         test    ecx, ecx
  4720.         jz      .smbl21
  4721.         mov     eax, msg_board_data+1
  4722.         mov     ebx, msg_board_data
  4723.         movzx   edx, byte [ebx]
  4724.         call    memmove
  4725.         dec     [msg_board_count]
  4726.         mov     [esp + 36], edx ;eax
  4727.         mov     [esp + 24], dword 1
  4728.         ret
  4729. .smbl21:
  4730.         mov     [esp+36], ecx
  4731.         mov     [esp+24], ecx
  4732. .smbl2:
  4733.         ret
  4734.  
  4735.  
  4736.  
  4737. sys_process_def:
  4738.         mov     edi, [CURRENT_TASK]
  4739.  
  4740.         dec     eax             ; 1 = set keyboard mode
  4741.      jne   no_set_keyboard_setup
  4742.  
  4743.      shl   edi,8
  4744.      mov   [edi+SLOT_BASE + APPDATA.keyboard_mode],bl
  4745.  
  4746.      ret
  4747.  
  4748.    no_set_keyboard_setup:
  4749.  
  4750.         dec     eax             ; 2 = get keyboard mode
  4751.      jne   no_get_keyboard_setup
  4752.  
  4753.      shl   edi,8
  4754.      movzx eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4755.  
  4756.      mov   [esp+36],eax
  4757.  
  4758.      ret
  4759.  
  4760.    no_get_keyboard_setup:
  4761.  
  4762.         dec     eax             ; 3 = get keyboard ctrl, alt, shift
  4763.      jne   no_get_keyboard_cas
  4764.  
  4765. ;     xor   eax,eax
  4766. ;     movzx eax,byte [shift]
  4767. ;     movzx ebx,byte [ctrl]
  4768. ;     shl   ebx,2
  4769. ;     add   eax,ebx
  4770. ;     movzx ebx,byte [alt]
  4771. ;     shl   ebx,3
  4772. ;     add   eax,ebx
  4773.  
  4774.  ;// mike.dld [
  4775.      mov   eax, [kb_state]
  4776.  ;// mike.dld ]
  4777.  
  4778.      mov   [esp+36],eax
  4779.  
  4780.      ret
  4781.  
  4782.    no_get_keyboard_cas:
  4783.  
  4784.         dec     eax
  4785.         jnz     no_add_keyboard_hotkey
  4786.  
  4787.         mov     eax, hotkey_list
  4788. @@:
  4789.         cmp     dword [eax+8], 0
  4790.         jz      .found_free
  4791.         add     eax, 16
  4792.         cmp     eax, hotkey_list+16*256
  4793.         jb      @b
  4794.         mov     dword [esp+36], 1
  4795.         ret
  4796. .found_free:
  4797.         mov     [eax+8], edi
  4798.         mov     [eax+4], ecx
  4799.         movzx   ebx, bl
  4800.         lea     ebx, [hotkey_scancodes+ebx*4]
  4801.         mov     ecx, [ebx]
  4802.         mov     [eax], ecx
  4803.         mov     [ebx], eax
  4804.         mov     [eax+12], ebx
  4805.         jecxz   @f
  4806.         mov     [ecx+12], eax
  4807. @@:
  4808.         and     dword [esp+36], 0
  4809.         ret
  4810.  
  4811. no_add_keyboard_hotkey:
  4812.  
  4813.         dec     eax
  4814.         jnz     no_del_keyboard_hotkey
  4815.  
  4816.         movzx   ebx, bl
  4817.         lea     ebx, [hotkey_scancodes+ebx*4]
  4818.         mov     eax, [ebx]
  4819. .scan:
  4820.         test    eax, eax
  4821.         jz      .notfound
  4822.         cmp     [eax+8], edi
  4823.         jnz     .next
  4824.         cmp     [eax+4], ecx
  4825.         jz      .found
  4826. .next:
  4827.         mov     eax, [eax]
  4828.         jmp     .scan
  4829. .notfound:
  4830.         mov     dword [esp+36], 1
  4831.         ret
  4832. .found:
  4833.         mov     ecx, [eax]
  4834.         jecxz   @f
  4835.         mov     edx, [eax+12]
  4836.         mov     [ecx+12], edx
  4837. @@:
  4838.         mov     ecx, [eax+12]
  4839.         mov     edx, [eax]
  4840.         mov     [ecx], edx
  4841.         xor     edx, edx
  4842.         mov     [eax+4], edx
  4843.         mov     [eax+8], edx
  4844.         mov     [eax+12], edx
  4845.         mov     [eax], edx
  4846.         mov     [esp+36], edx
  4847.         ret
  4848.  
  4849. no_del_keyboard_hotkey:
  4850.      ret
  4851.  
  4852.  
  4853. align 4
  4854.  
  4855. sys_gs:                         ; direct screen access
  4856.  
  4857.      cmp  eax,1                 ; resolution
  4858.      jne  no_gs1
  4859.      mov  eax,[Screen_Max_X]
  4860.      shl  eax,16
  4861.      mov  ax,[Screen_Max_Y]
  4862.      add  eax,0x00010001
  4863.      mov  [esp+36],eax
  4864.      ret
  4865.    no_gs1:
  4866.  
  4867.      cmp   eax,2                ; bits per pixel
  4868.      jne   no_gs2
  4869.      movzx eax,byte [ScreenBPP]
  4870.      mov   [esp+36],eax
  4871.      ret
  4872.    no_gs2:
  4873.  
  4874.      cmp   eax,3                ; bytes per scanline
  4875.      jne   no_gs3
  4876.      mov   eax,[BytesPerScanLine]
  4877.      mov   [esp+36],eax
  4878.      ret
  4879.    no_gs3:
  4880.  
  4881.      mov  [esp+36],dword -1
  4882.      ret
  4883.  
  4884.  
  4885. align 4 ; PCI functions
  4886.  
  4887. sys_pci:
  4888.  
  4889.      call  pci_api
  4890.      mov   [esp+36],eax
  4891.      ret
  4892.  
  4893.  
  4894. align 4  ;  system functions
  4895.  
  4896. syscall_setpixel:                       ; SetPixel
  4897.  
  4898.         mov     eax, ebx
  4899.         mov     ebx, ecx
  4900.         mov     ecx, edx
  4901.         mov     edx, [TASK_BASE]
  4902.         add     eax, [edx-twdw+WDATA.box.left]
  4903.         add     ebx, [edx-twdw+WDATA.box.top]
  4904.         mov     edi, [current_slot]
  4905.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  4906.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  4907.         xor     edi, edi ; no force
  4908. ;       mov     edi, 1
  4909.         call    [disable_mouse]
  4910.         jmp     [putpixel]
  4911.  
  4912. align 4
  4913.  
  4914. syscall_writetext:                      ; WriteText
  4915.  
  4916.         mov   eax,[TASK_BASE]
  4917.         mov   ebp,[eax-twdw+WDATA.box.left]
  4918.         push  esi
  4919.         mov   esi,[current_slot]
  4920.         add   ebp,[esi+APPDATA.wnd_clientbox.left]
  4921.         shl   ebp,16
  4922.         add   ebp,[eax-twdw+WDATA.box.top]
  4923.         add   bp,word[esi+APPDATA.wnd_clientbox.top]
  4924.         pop   esi
  4925.         add   ebx,ebp
  4926.         mov   eax,edi
  4927.         xor   edi,edi
  4928.         jmp   dtext
  4929.  
  4930. align 4
  4931.  
  4932. syscall_openramdiskfile:                ; OpenRamdiskFile
  4933.  
  4934.         mov     eax, ebx
  4935.         mov     ebx, ecx
  4936.         mov     ecx, edx
  4937.         mov     edx, esi
  4938.         mov     esi, 12
  4939.         call    fileread
  4940.         mov     [esp+32], eax
  4941.         ret
  4942.  
  4943. align 4
  4944.  
  4945. syscall_drawrect:                       ; DrawRect
  4946.  
  4947.         mov     edi, edx ; color + gradient
  4948.         and     edi, 0x80FFFFFF
  4949.         test    bx, bx  ; x.size
  4950.         je      .drectr
  4951.         test    cx, cx ; y.size
  4952.         je      .drectr
  4953.  
  4954.         mov     eax, ebx ; bad idea
  4955.         mov     ebx, ecx
  4956.  
  4957.         movzx   ecx, ax ; ecx - x.size
  4958.         shr     eax, 16 ; eax - x.coord
  4959.         movzx   edx, bx ; edx - y.size
  4960.         shr     ebx, 16 ; ebx - y.coord
  4961.         mov     esi, [current_slot]
  4962.  
  4963.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  4964.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  4965.         add     ecx, eax
  4966.         add     edx, ebx
  4967.         jmp     [drawbar]
  4968. .drectr:
  4969.         ret
  4970.  
  4971. align 4
  4972. syscall_getscreensize:                  ; GetScreenSize
  4973.         mov     ax, [Screen_Max_X]
  4974.         shl     eax, 16
  4975.         mov     ax, [Screen_Max_Y]
  4976.         mov     [esp + 32], eax
  4977.         ret
  4978.  
  4979. align 4
  4980.  
  4981. syscall_cdaudio:                        ; CD
  4982.  
  4983.         cmp     eax, 4
  4984.         jb      .audio
  4985.         jz      .eject
  4986.         cmp     eax, 5
  4987.         jnz     .ret
  4988. .load:
  4989.         call    .reserve
  4990.         call    LoadMedium
  4991.         call    .free
  4992.         ret
  4993. .eject:
  4994.         call    .reserve
  4995.         call    clear_CD_cache
  4996.         call    allow_medium_removal
  4997.         call    EjectMedium
  4998.         call    .free
  4999.         ret
  5000. .audio:
  5001.      call  sys_cd_audio
  5002.      mov   [esp+36],eax
  5003. .ret:
  5004.      ret
  5005.  
  5006. .reserve:
  5007.         call    reserve_cd
  5008.         mov     eax, ebx
  5009.         shr     eax, 1
  5010.         and     eax, 1
  5011.         inc     eax
  5012.         mov     [ChannelNumber], ax
  5013.         mov     eax, ebx
  5014.         and     eax, 1
  5015.         mov     [DiskNumber], al
  5016.         call    reserve_cd_channel
  5017.         and     ebx, 3
  5018.         inc     ebx
  5019.         mov     [cdpos], ebx
  5020.         add     ebx, ebx
  5021.         mov     cl, 8
  5022.         sub     cl, bl
  5023.         mov     al, [DRIVE_DATA+1]
  5024.         shr     al, cl
  5025.         test    al, 2
  5026.         jz      .err
  5027.         ret
  5028. .free:
  5029.         call    free_cd_channel
  5030.         and     [cd_status], 0
  5031.         ret
  5032. .err:
  5033.         call    .free
  5034.         pop     eax
  5035.         ret
  5036.  
  5037. align 4
  5038.  
  5039. syscall_getpixel:                       ; GetPixel
  5040.      mov   ecx, [Screen_Max_X]
  5041.      inc   ecx
  5042.      xor   edx, edx
  5043.      mov   eax, ebx
  5044.      div   ecx
  5045.      mov   ebx, edx
  5046.      xchg  eax, ebx
  5047.      call  dword [GETPIXEL] ; eax - x, ebx - y
  5048.      mov   [esp + 32], ecx
  5049.      ret
  5050.  
  5051.  
  5052. align 4
  5053.  
  5054. syscall_drawline:                       ; DrawLine
  5055.  
  5056.         mov     edi, [TASK_BASE]
  5057.         movzx   eax, word[edi-twdw+WDATA.box.left]
  5058.         mov     ebp, eax
  5059.         mov     esi, [current_slot]
  5060.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5061.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  5062.         add     ebp,ebx
  5063.         shl     eax, 16
  5064.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  5065.         add     eax, ebp
  5066.         mov     ebp, ebx
  5067.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  5068.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  5069.         add     ebp, ecx
  5070.         shl     ebx, 16
  5071.         xor     edi, edi
  5072.         add     ebx, ebp
  5073.         mov     ecx, edx
  5074.         jmp     [draw_line]
  5075.  
  5076. align 4
  5077.  
  5078. syscall_getirqowner:                    ; GetIrqOwner
  5079.  
  5080.      cmp   ebx,16
  5081.      jae   .err
  5082.  
  5083.      cmp   [irq_rights + 4 * ebx], dword 2
  5084.      je    .err
  5085.  
  5086.      mov   eax,[4 * ebx + irq_owner]
  5087.      mov   [esp+32],eax
  5088.  
  5089.      ret
  5090. .err:
  5091.      or    dword [esp+32], -1
  5092.      ret
  5093.  
  5094. align 4
  5095.  
  5096. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5097.  
  5098.      call  r_f_port_area
  5099.      mov   [esp+36],eax
  5100.      ret
  5101.  
  5102. align 4
  5103.  
  5104. syscall_threads:                        ; CreateThreads
  5105.  
  5106.      call  sys_threads
  5107.      mov   [esp+36],eax
  5108.      ret
  5109.  
  5110. align 4
  5111.  
  5112. stack_driver_stat:
  5113.  
  5114.      call  app_stack_handler            ; Stack status
  5115.  
  5116. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5117. ;     call  change_task                 ; between sent packet
  5118.  
  5119.      mov   [esp+36],eax
  5120.      ret
  5121.  
  5122. align 4
  5123.  
  5124. socket:                                 ; Socket interface
  5125.      call  app_socket_handler
  5126.  
  5127. ;     mov   [check_idle_semaphore],5    ; enable these for zero delay
  5128. ;     call  change_task                 ; between sent packet
  5129.  
  5130.      mov   [esp+36],eax
  5131.      mov   [esp+24],ebx
  5132.      ret
  5133.  
  5134. align 4
  5135.  
  5136. read_from_hd:                           ; Read from hd - fn not in use
  5137.  
  5138.      mov   edi,[TASK_BASE]
  5139.      add   edi,TASKDATA.mem_start
  5140.      add   eax,[edi]
  5141.      add   ecx,[edi]
  5142.      add   edx,[edi]
  5143.      call  file_read
  5144.  
  5145.      mov   [esp+36],eax
  5146.      mov   [esp+24],ebx
  5147.  
  5148.      ret
  5149.  
  5150. paleholder:
  5151.         ret
  5152.  
  5153. align 4
  5154. set_screen:
  5155.         cmp eax, [Screen_Max_X]
  5156.         jne .set
  5157.  
  5158.         cmp edx, [Screen_Max_Y]
  5159.         jne .set
  5160.         ret
  5161. .set:
  5162.         pushfd
  5163.         cli
  5164.  
  5165.         mov [Screen_Max_X], eax
  5166.         mov [Screen_Max_Y], edx
  5167.  
  5168.         mov [screen_workarea.right],eax
  5169.         mov [screen_workarea.bottom], edx
  5170.         inc eax
  5171.         shl eax, 2                      ;32 bpp
  5172.         mov [BytesPerScanLine], eax
  5173.         push ebx
  5174.         push esi
  5175.         push edi
  5176.         call    repos_windows
  5177.         mov     eax, 0
  5178.         mov     ebx, 0
  5179.         mov     ecx, [Screen_Max_X]
  5180.         mov     edx, [Screen_Max_Y]
  5181.         call    calculatescreen
  5182.         pop edi
  5183.         pop esi
  5184.         pop ebx
  5185.  
  5186.         popfd
  5187.         ret
  5188.  
  5189. ; --------------- APM ---------------------
  5190. apm_entry    dp    0
  5191. apm_vf        dd    0
  5192. align 4
  5193. sys_apm:
  5194.     cmp    word [apm_vf], 0    ; Check APM BIOS enable
  5195.     jne    @f
  5196.     or    [esp + 56], byte 1    ; error
  5197.     mov    [esp + 36], dword 8    ; 32-bit protected-mode interface not supported
  5198.     ret
  5199.  
  5200. @@:
  5201.     xchg    eax, ecx
  5202.     xchg    ebx, ecx
  5203.  
  5204.     cmp    al, 3
  5205.     ja    @f
  5206.     and    [esp + 56], byte 0xfe    ; emulate func 0..3 as func 0
  5207.     mov    eax, [apm_vf]
  5208.     mov    [esp + 36], eax
  5209.     shr    eax, 16
  5210.     mov    [esp + 32], eax
  5211.     ret
  5212.  
  5213. @@:
  5214.  
  5215.     mov esi, [master_tab+(OS_BASE shr 20)]
  5216.     xchg [master_tab], esi
  5217.     push esi
  5218.     mov edi, cr3
  5219.     mov cr3, edi                 ;flush TLB
  5220.  
  5221.     call    pword [apm_entry]    ; call APM BIOS
  5222.  
  5223.     xchg eax, [esp]
  5224.     mov [master_tab], eax
  5225.     mov eax, cr3
  5226.     mov cr3, eax
  5227.     pop eax
  5228.  
  5229.     mov    [esp + 8 ], edi
  5230.     mov    [esp + 12], esi
  5231.     mov    [esp + 24], ebx
  5232.     mov    [esp + 28], edx
  5233.     mov    [esp + 32], ecx
  5234.     mov    [esp + 36], eax
  5235.     setc    al
  5236.     and    [esp + 56], byte 0xfe
  5237.     or    [esp + 56], al
  5238.  
  5239.  
  5240.     ret
  5241. ; -----------------------------------------
  5242.  
  5243. align 4
  5244.  
  5245. undefined_syscall:                      ; Undefined system call
  5246.      mov   [esp + 32], dword -1
  5247.      ret
  5248.  
  5249. align 4
  5250. system_shutdown:          ; shut down the system
  5251.  
  5252.            cmp byte [BOOT_VAR+0x9030], 1
  5253.            jne @F
  5254.            ret
  5255. @@:
  5256.            call stop_all_services
  5257.            push 3                ; stop playing cd
  5258.            pop  eax
  5259.            call sys_cd_audio
  5260.  
  5261. yes_shutdown_param:
  5262.            cli
  5263.  
  5264.            mov  eax, kernel_file ; load kernel.mnt to 0x7000:0
  5265.            push 12
  5266.            pop  esi
  5267.            xor  ebx,ebx
  5268.            or   ecx,-1
  5269.            mov  edx, OS_BASE+0x70000
  5270.            call fileread
  5271.  
  5272.    ;        mov  esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  5273.    ;        mov  edi,OS_BASE+0x40000
  5274.    ;        mov  ecx,1000
  5275.    ;        rep  movsb
  5276.  
  5277.            mov  esi,OS_BASE+0x2F0000    ; restore 0x0 - 0xffff
  5278.            mov  edi, OS_BASE
  5279.            mov  ecx,0x10000/4
  5280.            cld
  5281.            rep movsd
  5282.  
  5283.            call restorefatchain
  5284.  
  5285.            mov al, 0xFF
  5286.            out 0x21, al
  5287.            out 0xA1, al
  5288.  
  5289. if 1
  5290.        ;    mov  word [OS_BASE+0x467+0],pr_mode_exit
  5291.        ;    mov  word [OS_BASE+0x467+2],0x1000
  5292.  
  5293.            mov  al,0x0F
  5294.            out  0x70,al
  5295.            mov  al,0x05
  5296.            out  0x71,al
  5297.  
  5298.            mov  al,0xFE
  5299.            out  0x64,al
  5300.  
  5301.            hlt
  5302.  
  5303. else
  5304.         cmp     byte [OS_BASE + 0x9030], 2
  5305.         jnz     no_acpi_power_off
  5306.  
  5307. ; scan for RSDP
  5308. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  5309.         movzx   eax, word [OS_BASE + 0x40E]
  5310.         shl     eax, 4
  5311.         jz      @f
  5312.         mov     ecx, 1024/16
  5313.         call    scan_rsdp
  5314.         jnc     .rsdp_found
  5315. @@:
  5316. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  5317.         mov     eax, 0xE0000
  5318.         mov     ecx, 0x2000
  5319.         call    scan_rsdp
  5320.         jc      no_acpi_power_off
  5321. .rsdp_found:
  5322.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  5323.         mov     ebp, [ipc_tmp]
  5324.         stdcall map_page, ebp, esi, PG_MAP
  5325.         lea     eax, [esi+1000h]
  5326.         lea     edx, [ebp+1000h]
  5327.         stdcall map_page, edx, eax, PG_MAP
  5328.         and     esi, 0xFFF
  5329.         add     esi, ebp
  5330.         cmp     dword [esi], 'RSDT'
  5331.         jnz     no_acpi_power_off
  5332.         mov     ecx, [esi+4]
  5333.         sub     ecx, 24h
  5334.         jbe     no_acpi_power_off
  5335.         shr     ecx, 2
  5336.         add     esi, 24h
  5337. .scan_fadt:
  5338.         lodsd
  5339.         mov     ebx, eax
  5340.         lea     eax, [ebp+2000h]
  5341.         stdcall map_page, eax, ebx, PG_MAP
  5342.         lea     eax, [ebp+3000h]
  5343.         add     ebx, 0x1000
  5344.         stdcall map_page, eax, ebx, PG_MAP
  5345.         and     ebx, 0xFFF
  5346.         lea     ebx, [ebx+ebp+2000h]
  5347.         cmp     dword [ebx], 'FACP'
  5348.         jz      .fadt_found
  5349.         loop    .scan_fadt
  5350.         jmp     no_acpi_power_off
  5351. .fadt_found:
  5352. ; ebx is linear address of FADT
  5353.         mov     edx, [ebx+48]
  5354.         test    edx, edx
  5355.         jz      .nosmi
  5356.         mov     al, [ebx+52]
  5357.         out     dx, al
  5358.         mov     edx, [ebx+64]
  5359. @@:
  5360.         in      ax, dx
  5361.         test    al, 1
  5362.         jz      @b
  5363. .nosmi:
  5364.         mov     edx, [ebx+64]
  5365.         in      ax, dx
  5366.         and     ax, 203h
  5367.         or      ax, 3C00h
  5368.         out     dx, ax
  5369.         mov     edx, [ebx+68]
  5370.         test    edx, edx
  5371.         jz      @f
  5372.         in      ax, dx
  5373.         and     ax, 203h
  5374.         or      ax, 3C00h
  5375.         out     dx, ax
  5376. @@:
  5377.         jmp     $
  5378.  
  5379.  
  5380. no_acpi_power_off:
  5381.            mov  word [OS_BASE+0x467+0],pr_mode_exit
  5382.            mov  word [OS_BASE+0x467+2],0x1000
  5383.  
  5384.            mov  al,0x0F
  5385.            out  0x70,al
  5386.            mov  al,0x05
  5387.            out  0x71,al
  5388.  
  5389.            mov  al,0xFE
  5390.            out  0x64,al
  5391.  
  5392.            hlt
  5393.  
  5394. scan_rsdp:
  5395.         add     eax, OS_BASE
  5396. .s:
  5397.         cmp     dword [eax], 'RSD '
  5398.         jnz     .n
  5399.         cmp     dword [eax+4], 'PTR '
  5400.         jnz     .n
  5401.         xor     edx, edx
  5402.         xor     esi, esi
  5403. @@:
  5404.         add     dl, [eax+esi]
  5405.         inc     esi
  5406.         cmp     esi, 20
  5407.         jnz     @b
  5408.         test    dl, dl
  5409.         jz      .ok
  5410. .n:
  5411.         add     eax, 10h
  5412.         loop    .s
  5413.         stc
  5414. .ok:
  5415.         ret
  5416. end if
  5417.  
  5418. include "data32.inc"
  5419.  
  5420. __REV__ = __REV
  5421.  
  5422. uglobals_size = $ - endofcode
  5423. diff16 "end of kernel code",0,$
  5424.  
  5425. align 16
  5426.  
  5427. __end:
  5428.  
  5429.