Subversion Repositories Kolibri OS

Rev

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