Subversion Repositories Kolibri OS

Rev

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

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