Subversion Repositories Kolibri OS

Rev

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