Subversion Repositories Kolibri OS

Rev

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

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