Subversion Repositories Kolibri OS

Rev

Rev 8216 | Rev 8247 | 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: 8246 $
  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     eax, -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. sysfn_terminate:        ; 18.2 = TERMINATE
  2201.         push    ecx
  2202.         cmp     ecx, 2
  2203.         jb      noprocessterminate
  2204.         mov     edx, [TASK_COUNT]
  2205.         cmp     ecx, edx
  2206.         ja      noprocessterminate
  2207.         mov     eax, [TASK_COUNT]
  2208.         shl     ecx, 5
  2209.         mov     edx, [ecx+CURRENT_TASK+TASKDATA.pid]
  2210.         add     ecx, CURRENT_TASK+TASKDATA.state
  2211.         cmp     byte [ecx], 9
  2212.         jz      noprocessterminate
  2213.         push    ecx edx
  2214.         lea     edx, [(ecx-(CURRENT_TASK and 1FFFFFFFh)-TASKDATA.state)*8+SLOT_BASE]
  2215.         call    request_terminate
  2216.         pop     edx ecx
  2217.         test    eax, eax
  2218.         jz      noprocessterminate
  2219. ;--------------------------------------
  2220. ; terminate all network sockets it used
  2221.         pusha
  2222.         mov     eax, edx
  2223.         call    socket_process_end
  2224.         popa
  2225. ;--------------------------------------
  2226.         cmp     [_display.select_cursor], 0
  2227.         je      .restore_end
  2228. ; restore default cursor before killing
  2229.         pusha
  2230.         mov     ecx, [esp+32]
  2231.         shl     ecx, 8
  2232.         add     ecx, SLOT_BASE
  2233.         mov     eax, [def_cursor]
  2234.         cmp     [ecx+APPDATA.cursor], eax
  2235.         je      @f
  2236.         call    restore_default_cursor_before_killing
  2237. @@:
  2238.         popa
  2239. .restore_end:
  2240. ;--------------------------------------
  2241.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2242.         mov     [ecx], byte 3; clear possible i40's
  2243.         call    wakeup_osloop
  2244.      ;call MEM_Heap_UnLock
  2245.  
  2246.         cmp     edx, [application_table_owner]; clear app table stat
  2247.         jne     noatsc
  2248.         call    unlock_application_table
  2249. noatsc:
  2250. noprocessterminate:
  2251.         add     esp, 4
  2252.         ret
  2253. ;------------------------------------------------------------------------------
  2254. sysfn_terminate2:
  2255. ;lock application_table_status mutex
  2256. .table_status:
  2257.         call    lock_application_table
  2258.         mov     eax, ecx
  2259.         call    pid_to_slot
  2260.         test    eax, eax
  2261.         jz      .not_found
  2262.         mov     ecx, eax
  2263.         cli
  2264.         call    sysfn_terminate
  2265.         call    unlock_application_table
  2266.         sti
  2267.         and     dword [esp+32], 0
  2268.         ret
  2269. .not_found:
  2270.         call    unlock_application_table
  2271.         or      dword [esp+32], -1
  2272.         ret
  2273. ;------------------------------------------------------------------------------
  2274. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2275.         cmp     ecx, 2
  2276.         jb      .nowindowdeactivate
  2277.         cmp     ecx, [TASK_COUNT]
  2278.         ja      .nowindowdeactivate
  2279.  
  2280.         movzx   esi, word [WIN_STACK + ecx*2]
  2281.         cmp     esi, 1
  2282.         je      .nowindowdeactivate ; already deactive
  2283.  
  2284.         mov     edi, ecx
  2285.         shl     edi, 5
  2286.         add     edi, window_data
  2287.         movzx   esi, word [WIN_STACK + ecx * 2]
  2288.         lea     esi, [WIN_POS + esi * 2]
  2289.         call    window._.window_deactivate
  2290.         call    syscall_display_settings.calculateScreen
  2291.         call    syscall_display_settings.redrawScreen
  2292. .nowindowdeactivate:
  2293.         ret
  2294. ;------------------------------------------------------------------------------
  2295. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2296.         cmp     ecx, 2
  2297.         jb      .nowindowactivate
  2298.         cmp     ecx, [TASK_COUNT]
  2299.         ja      .nowindowactivate
  2300. ;-------------------------------------
  2301. @@:
  2302. ; If the window is captured and moved by the user,
  2303. ; then you can't change the position in window stack!!!
  2304.         mov     al, [mouse.active_sys_window.action]
  2305.         and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
  2306.         test    al, al
  2307.         jz      @f
  2308.         call    change_task
  2309.         jmp     @b
  2310. @@:
  2311. ;-------------------------------------
  2312.         mov     [window_minimize], 2; restore window if minimized
  2313.         call    wakeup_osloop
  2314.  
  2315.         movzx   esi, word [WIN_STACK + ecx*2]
  2316.         cmp     esi, [TASK_COUNT]
  2317.         je      .nowindowactivate; already active
  2318.  
  2319.         mov     edi, ecx
  2320.         shl     edi, 5
  2321.         add     edi, window_data
  2322.         movzx   esi, word [WIN_STACK + ecx * 2]
  2323.         lea     esi, [WIN_POS + esi * 2]
  2324.         call    waredraw
  2325. .nowindowactivate:
  2326.         ret
  2327. ;------------------------------------------------------------------------------
  2328. align 4
  2329. sysfn_zmodif:
  2330. ;18,25,1 - get z_modif
  2331. ;18,25,2 - set z_modif
  2332. ;edx = -1(for current task) or TID
  2333. ;esi(for 2) = new value z_modif
  2334. ;return:
  2335. ;1:   eax = z_modif
  2336. ;2: eax=0(fail),1(success) for set z_modif
  2337.  
  2338.         cmp     edx, -1
  2339.         jne     @f
  2340.         mov     edx, [CURRENT_TASK]
  2341.      @@:
  2342.         cmp     edx, [TASK_COUNT]
  2343.         ja      .fail
  2344.         cmp     edx, 1
  2345.         je      .fail
  2346.  
  2347.         mov     eax, edx
  2348.         shl     edx, 5
  2349.  
  2350.         cmp     [edx + CURRENT_TASK + TASKDATA.state], 9
  2351.         je      .fail
  2352.  
  2353.         cmp     ecx, 1
  2354.         jnz     .set_zmod
  2355.  
  2356.         mov     al, [edx + window_data + WDATA.z_modif]
  2357.         jmp     .exit
  2358.  
  2359. .set_zmod:
  2360.         cmp     ecx, 2
  2361.         jnz     .fail
  2362.  
  2363.         mov     ebx, esi
  2364.         mov     esi, eax
  2365.  
  2366.         cmp     bl, ZPOS_ALWAYS_TOP
  2367.         jg      .fail
  2368.  
  2369.         mov     [edx + window_data + WDATA.z_modif], bl
  2370.  
  2371.         mov     eax, [edx + window_data + WDATA.box.left]
  2372.         mov     ebx, [edx + window_data + WDATA.box.top]
  2373.         mov     ecx, [edx + window_data + WDATA.box.width]
  2374.         mov     edx, [edx + window_data + WDATA.box.height]
  2375.         add     ecx, eax
  2376.         add     edx, ebx
  2377.         call    window._.set_screen
  2378.         call    window._.set_top_wnd
  2379.         call    window._.redraw_top_wnd
  2380.  
  2381.         shl     esi, 5
  2382.         mov     [esi + window_data + WDATA.fl_redraw], 1
  2383.  
  2384.  
  2385.         mov     eax, 1
  2386.         jmp     .exit
  2387. .fail:
  2388.         xor     eax, eax
  2389. .exit:
  2390.         mov     [esp+32], eax
  2391.         ret
  2392.  
  2393. ;------------------------------------------------------------------------------
  2394. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2395.         mov     eax, [CURRENT_TASK+32+TASKDATA.cpu_usage]
  2396.         mov     [esp+32], eax
  2397.         ret
  2398. ;------------------------------------------------------------------------------
  2399. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2400.         mov     eax, dword [cpu_freq]
  2401.         mov     [esp+32], eax
  2402.         ret
  2403. ;------------------------------------------------------------------------------
  2404. get_cpu_freq:
  2405.         mov     eax, dword [cpu_freq]
  2406.         mov     edx, dword [cpu_freq+4]
  2407.         ret
  2408. ;  SAVE ramdisk to /hd/1/menuet.img
  2409. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2410.    include 'blkdev/rdsave.inc'
  2411. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2412. ;------------------------------------------------------------------------------
  2413. align 4
  2414. sysfn_getactive:        ; 18.7 = get active window
  2415.         mov     eax, [TASK_COUNT]
  2416.         movzx   eax, word [WIN_POS + eax*2]
  2417.         mov     [esp+32], eax
  2418.         ret
  2419. ;------------------------------------------------------------------------------
  2420. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2421. ;     cmp  ecx,1
  2422.         dec     ecx
  2423.         jnz     nogetsoundflag
  2424.         movzx   eax, byte [sound_flag]; get sound_flag
  2425.         mov     [esp+32], eax
  2426.         ret
  2427.  nogetsoundflag:
  2428. ;     cmp  ecx,2
  2429.         dec     ecx
  2430.         jnz     nosoundflag
  2431.         xor     byte [sound_flag], 1
  2432.  nosoundflag:
  2433.         ret
  2434. ;------------------------------------------------------------------------------
  2435. sysfn_minimize:         ; 18.10 = minimize window
  2436.         mov     [window_minimize], 1
  2437.         call    wakeup_osloop
  2438.         ret
  2439. ;------------------------------------------------------------------------------
  2440. align 4
  2441. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2442.         dec     ecx
  2443.         jnz     .exit
  2444. .small_table:
  2445.         mov     edi, edx
  2446.         mov     esi, DRIVE_DATA
  2447.         mov     ecx, DRIVE_DATA_SIZE ;10
  2448.         cld
  2449.         rep movsb
  2450. .exit:
  2451.         ret
  2452. ;------------------------------------------------------------------------------
  2453. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2454.         and     dword [esp+32], 0
  2455.         ret
  2456. ;------------------------------------------------------------------------------
  2457. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2458.         mov     edi, ecx
  2459.         mov     esi, version_inf
  2460.         mov     ecx, version_end-version_inf
  2461.         rep movsb
  2462.         ret
  2463. ;------------------------------------------------------------------------------
  2464. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2465.      ;wait retrace functions
  2466.  sys_wait_retrace:
  2467.         mov     edx, 0x3da
  2468.  WaitRetrace_loop:
  2469.         in      al, dx
  2470.         test    al, 1000b
  2471.         jz      WaitRetrace_loop
  2472.         and     [esp+32], dword 0
  2473.         ret
  2474. ;------------------------------------------------------------------------------
  2475. align 4
  2476. sysfn_centermouse:      ; 18.15 = mouse centered
  2477.         mov     eax, [_display.width]
  2478.         shr     eax, 1
  2479.         mov     [MOUSE_X], ax
  2480.         mov     eax, [_display.height]
  2481.         shr     eax, 1
  2482.         mov     [MOUSE_Y], ax
  2483.         call    wakeup_osloop
  2484.         xor     eax, eax
  2485.         and     [esp+32], eax
  2486.         ret
  2487. ;------------------------------------------------------------------------------
  2488. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2489.         cmp     ecx, 8
  2490.         jnc     @f
  2491.         jmp     dword [.table+ecx*4]
  2492. .get_mouse_acceleration:
  2493.         xor     eax, eax
  2494.         mov     ax, [mouse_speed_factor]
  2495.         mov     [esp+32], eax
  2496.         ret
  2497. .set_mouse_acceleration:
  2498.         mov     [mouse_speed_factor], dx
  2499.         ret
  2500. .get_mouse_delay:
  2501.         xor     eax, eax
  2502.         mov     al, [mouse_delay]
  2503.         mov     [esp+32], eax
  2504.         ret
  2505. .set_mouse_delay:
  2506.         mov     [mouse_delay], dl
  2507. @@:
  2508.         ret
  2509. .set_pointer_position:
  2510.         cmp     dx, word[_display.height]
  2511.         jae     @b
  2512.         rol     edx, 16
  2513.         cmp     dx, word[_display.width]
  2514.         jae     @b
  2515.         mov     [MOUSE_X], edx
  2516.         mov     [mouse_active], 1
  2517.         jmp     wakeup_osloop
  2518. .set_mouse_button:
  2519.         mov     [BTN_DOWN], edx
  2520.         mov     [mouse_active], 1
  2521.         jmp     wakeup_osloop
  2522. .get_doubleclick_delay:
  2523.         xor     eax, eax
  2524.         mov     al, [mouse_doubleclick_delay]
  2525.         mov     [esp+32], eax
  2526.         ret
  2527. .set_doubleclick_delay:
  2528.         mov     [mouse_doubleclick_delay], dl
  2529.         ret
  2530. align 4
  2531. .table:
  2532. dd      .get_mouse_acceleration
  2533. dd      .set_mouse_acceleration
  2534. dd      .get_mouse_delay
  2535. dd      .set_mouse_delay
  2536. dd      .set_pointer_position
  2537. dd      .set_mouse_button
  2538. dd      .get_doubleclick_delay
  2539. dd      .set_doubleclick_delay
  2540. ;------------------------------------------------------------------------------
  2541. sysfn_getfreemem:
  2542.         mov     eax, [pg_data.pages_free]
  2543.         shl     eax, 2
  2544.         mov     [esp+32], eax
  2545.         ret
  2546. ;------------------------------------------------------------------------------
  2547. sysfn_getallmem:
  2548.         mov     eax, [MEM_AMOUNT]
  2549.         shr     eax, 10
  2550.         mov     [esp+32], eax
  2551.         ret
  2552. ;------------------------------------------------------------------------------
  2553. sysfn_pid_to_slot:
  2554.         mov     eax, ecx
  2555.         call    pid_to_slot
  2556.         mov     [esp+32], eax
  2557.         ret
  2558. ;------------------------------------------------------------------------------
  2559. sysfn_min_rest_window:
  2560.         pushad
  2561.         mov     eax, edx ; ebx - operating
  2562.         shr     ecx, 1
  2563.         jnc     @f
  2564.         call    pid_to_slot
  2565. @@:
  2566.         or      eax, eax ; eax - number of slot
  2567.         jz      .error
  2568.         cmp     eax, 255    ; varify maximal slot number
  2569.         ja      .error
  2570.         movzx   eax, word [WIN_STACK + eax*2]
  2571.         shr     ecx, 1
  2572.         jc      .restore
  2573.  ; .minimize:
  2574.         call    minimize_window
  2575.         jmp     .exit
  2576. .restore:
  2577.         call    restore_minimized_window
  2578. .exit:
  2579.         popad
  2580.         xor     eax, eax
  2581.         mov     [esp+32], eax
  2582.         ret
  2583. .error:
  2584.         popad
  2585.         xor     eax, eax
  2586.         dec     eax
  2587.         mov     [esp+32], eax
  2588.         ret
  2589. ;------------------------------------------------------------------------------
  2590. sysfn_min_windows:
  2591.         call    minimize_all_window
  2592.         mov     [esp+32], eax
  2593.         call    change_task
  2594.         ret
  2595. ;------------------------------------------------------------------------------
  2596. sysfn_set_screen_sizes:
  2597.         cmp     [SCR_MODE], word 0x13
  2598.         jbe     .exit
  2599.  
  2600.         cmp     [_display.select_cursor], select_cursor
  2601.         jne     .exit
  2602.  
  2603.         cmp     ecx, [display_width_standard]
  2604.         ja      .exit
  2605.  
  2606.         cmp     edx, [display_height_standard]
  2607.         ja      .exit
  2608.  
  2609.         pushfd
  2610.         cli
  2611.         mov     eax, ecx
  2612.         mov     ecx, [_display.lfb_pitch]
  2613.         mov     [_display.width], eax
  2614.         dec     eax
  2615.         mov     [_display.height], edx
  2616.         dec     edx
  2617. ; eax - new Screen_Max_X
  2618. ; edx - new Screen_Max_Y
  2619.         mov     [do_not_touch_winmap], 1
  2620.         call    set_screen
  2621.         mov     [do_not_touch_winmap], 0
  2622.         popfd
  2623.         call    change_task
  2624. .exit:
  2625.         ret
  2626. ;------------------------------------------------------------------------------
  2627. uglobal
  2628. screen_workarea RECT
  2629. display_width_standard dd 0
  2630. display_height_standard dd 0
  2631. do_not_touch_winmap db 0
  2632. window_minimize db 0
  2633. sound_flag      db 0
  2634.  
  2635. endg
  2636.  
  2637. UID_NONE=0
  2638. UID_MENUETOS=1   ;official
  2639. UID_KOLIBRI=2    ;russian
  2640.  
  2641. iglobal
  2642. version_inf:
  2643.         db 0,7,7,0  ; version 0.7.7.0
  2644.         db 0
  2645. .rev    dd __REV__
  2646. version_end:
  2647. endg
  2648. ;------------------------------------------------------------------------------
  2649. align 4
  2650. sys_cachetodiskette:
  2651.         cmp     ebx, 1
  2652.         jb      .no_floppy_save
  2653.         cmp     ebx, 2
  2654.         ja      .no_floppy_save
  2655.         call    save_image
  2656.         mov     [esp + 32], eax
  2657.         ret
  2658. .no_floppy_save:
  2659.         mov     [esp + 32], dword 1
  2660.         ret
  2661. ;------------------------------------------------------------------------------
  2662. uglobal
  2663. ;  bgrchanged  dd  0x0
  2664. align 4
  2665. bgrlockpid dd 0
  2666. bgrlock db 0
  2667. endg
  2668. ;------------------------------------------------------------------------------
  2669. align 4
  2670. sys_background:
  2671.         cmp     ebx, 1                     ; BACKGROUND SIZE
  2672.         jnz     nosb1
  2673.         test    ecx, ecx
  2674.         jz      sbgrr
  2675.  
  2676.         test    edx, edx
  2677.         jz      sbgrr
  2678. ;--------------------------------------
  2679. align 4
  2680. @@:
  2681. ;;Maxis use atomic bts for mutexes  4.4.2009
  2682.         bts     dword [bgrlock], 0
  2683.         jnc     @f
  2684.         call    change_task
  2685.         jmp     @b
  2686. ;--------------------------------------
  2687. align 4
  2688. @@:
  2689.         mov     [BgrDataWidth], ecx
  2690.         mov     [BgrDataHeight], edx
  2691. ;    mov   [bgrchanged],1
  2692.  
  2693.         pushad
  2694. ; return memory for old background
  2695.         mov     eax, [img_background]
  2696.         cmp     eax, static_background_data
  2697.         jz      @f
  2698.         stdcall kernel_free, eax
  2699. ;--------------------------------------
  2700. align 4
  2701. @@:
  2702. ; calculate RAW size
  2703.         xor     eax, eax
  2704.         inc     eax
  2705.         cmp     [BgrDataWidth], eax
  2706.         jae     @f
  2707.         mov     [BgrDataWidth], eax
  2708. ;--------------------------------------
  2709. align 4
  2710. @@:
  2711.         cmp     [BgrDataHeight], eax
  2712.         jae     @f
  2713.         mov     [BgrDataHeight], eax
  2714. ;--------------------------------------
  2715. align 4
  2716. @@:
  2717.         mov     eax, [BgrDataWidth]
  2718.         imul    eax, [BgrDataHeight]
  2719.         lea     eax, [eax*3]
  2720. ; it is reserved with aligned to the boundary of 4 KB pages,
  2721. ; otherwise there may be exceptions a page fault for vesa20_drawbackground_tiled
  2722. ; because the 32 bit read is used for  high performance: "mov eax,[esi]"
  2723.         shr     eax, 12
  2724.         inc     eax
  2725.         shl     eax, 12
  2726.         mov     [mem_BACKGROUND], eax
  2727. ; get memory for new background
  2728.         stdcall kernel_alloc, eax
  2729.         test    eax, eax
  2730.         jz      .memfailed
  2731.         mov     [img_background], eax
  2732.         jmp     .exit
  2733. ;--------------------------------------
  2734. align 4
  2735. .memfailed:
  2736. ; revert to static monotone data
  2737.         mov     [img_background], static_background_data
  2738.         xor     eax, eax
  2739.         inc     eax
  2740.         mov     [BgrDataWidth], eax
  2741.         mov     [BgrDataHeight], eax
  2742.         mov     [mem_BACKGROUND], 4
  2743. ;--------------------------------------
  2744. align 4
  2745. .exit:
  2746.         popad
  2747.         mov     [bgrlock], 0
  2748. ;--------------------------------------
  2749. align 4
  2750. sbgrr:
  2751.         ret
  2752. ;------------------------------------------------------------------------------
  2753. align 4
  2754. nosb1:
  2755.         cmp     ebx, 2                     ; SET PIXEL
  2756.         jnz     nosb2
  2757.  
  2758.         mov     eax, [img_background]
  2759.         test    ecx, ecx
  2760.         jz      @f
  2761.         cmp     eax, static_background_data
  2762.         jz      .ret
  2763. ;--------------------------------------
  2764. align 4
  2765. @@:
  2766.         mov     ebx, [mem_BACKGROUND]
  2767.         add     ebx, 4095
  2768.         and     ebx, -4096
  2769.         sub     ebx, 4
  2770.         cmp     ecx, ebx
  2771.         ja      .ret
  2772.  
  2773.         mov     ebx, [eax+ecx]
  2774.         and     ebx, 0xFF000000;255*256*256*256
  2775.         and     edx, 0x00FFFFFF;255*256*256+255*256+255
  2776.         add     edx, ebx
  2777.         mov     [eax+ecx], edx
  2778. ;--------------------------------------
  2779. align 4
  2780. .ret:
  2781.         ret
  2782. ;------------------------------------------------------------------------------
  2783. align 4
  2784. nosb2:
  2785.         cmp     ebx, 3                     ; DRAW BACKGROUND
  2786.         jnz     nosb3
  2787. ;--------------------------------------
  2788. align 4
  2789. draw_background_temp:
  2790.         mov     [background_defined], 1
  2791.         call    force_redraw_background
  2792. ;--------------------------------------
  2793. align 4
  2794. nosb31:
  2795.         ret
  2796. ;------------------------------------------------------------------------------
  2797. align 4
  2798. nosb3:
  2799.         cmp     ebx, 4                     ; TILED / STRETCHED
  2800.         jnz     nosb4
  2801.         cmp     ecx, [BgrDrawMode]
  2802.         je      nosb41
  2803.         mov     [BgrDrawMode], ecx
  2804. ;--------------------------------------
  2805. align 4
  2806. nosb41:
  2807.         ret
  2808. ;------------------------------------------------------------------------------
  2809. align 4
  2810. nosb4:
  2811.         cmp     ebx, 5                     ; BLOCK MOVE TO BGR
  2812.         jnz     nosb5
  2813.         cmp     [img_background], static_background_data
  2814.         jnz     @f
  2815.         test    edx, edx
  2816.         jnz     .fin
  2817.         cmp     esi, 4
  2818.         ja      .fin
  2819. ;--------------------------------------
  2820. align 4
  2821. @@:
  2822.   ; bughere
  2823.         mov     eax, ecx
  2824.         mov     ebx, edx
  2825.         add     ebx, [img_background];IMG_BACKGROUND
  2826.         mov     ecx, esi
  2827.         call    memmove
  2828. ;--------------------------------------
  2829. align 4
  2830. .fin:
  2831.         ret
  2832. ;------------------------------------------------------------------------------
  2833. align 4
  2834. nosb5:
  2835.         cmp     ebx, 6
  2836.         jnz     nosb6
  2837. ;--------------------------------------
  2838. align 4
  2839. ;;Maxis use atomic bts for mutex 4.4.2009
  2840. @@:
  2841.         bts     dword [bgrlock], 0
  2842.         jnc     @f
  2843.         call    change_task
  2844.         jmp     @b
  2845. ;--------------------------------------
  2846. align 4
  2847. @@:
  2848.         mov     eax, [CURRENT_TASK]
  2849.         mov     [bgrlockpid], eax
  2850.         cmp     [img_background], static_background_data
  2851.         jz      .nomem
  2852.         stdcall user_alloc, [mem_BACKGROUND]
  2853.         mov     [esp+32], eax
  2854.         test    eax, eax
  2855.         jz      .nomem
  2856.         mov     ebx, eax
  2857.         shr     ebx, 12
  2858.         or      dword [page_tabs+(ebx-1)*4], MEM_BLOCK_DONT_FREE
  2859.         mov     esi, [img_background]
  2860.         shr     esi, 12
  2861.         mov     ecx, [mem_BACKGROUND]
  2862.         add     ecx, 0xFFF
  2863.         shr     ecx, 12
  2864. ;--------------------------------------
  2865. align 4
  2866. .z:
  2867.         mov     eax, [page_tabs+ebx*4]
  2868.         test    al, 1
  2869.         jz      @f
  2870.         call    free_page
  2871. ;--------------------------------------
  2872. align 4
  2873. @@:
  2874.         mov     eax, [page_tabs+esi*4]
  2875.         or      al, PG_UWR
  2876.         mov     [page_tabs+ebx*4], eax
  2877.         mov     eax, ebx
  2878.         shl     eax, 12
  2879.         invlpg  [eax]
  2880.         inc     ebx
  2881.         inc     esi
  2882.         loop    .z
  2883.         ret
  2884. ;--------------------------------------
  2885. align 4
  2886. .nomem:
  2887.         and     [bgrlockpid], 0
  2888.         mov     [bgrlock], 0
  2889. ;------------------------------------------------------------------------------
  2890. align 4
  2891. nosb6:
  2892.         cmp     ebx, 7
  2893.         jnz     nosb7
  2894.         cmp     [bgrlock], 0
  2895.         jz      .err
  2896.         mov     eax, [CURRENT_TASK]
  2897.         cmp     [bgrlockpid], eax
  2898.         jnz     .err
  2899.         mov     eax, ecx
  2900.         mov     ebx, ecx
  2901.         shr     eax, 12
  2902.         mov     ecx, [page_tabs+(eax-1)*4]
  2903.         test    cl, MEM_BLOCK_USED or MEM_BLOCK_DONT_FREE
  2904.         jz      .err
  2905.         jnp     .err
  2906.         push    eax
  2907.         shr     ecx, 12
  2908.         dec     ecx
  2909. ;--------------------------------------
  2910. align 4
  2911. @@:
  2912.         and     dword [page_tabs+eax*4], 0
  2913.         mov     edx, eax
  2914.         shl     edx, 12
  2915.         push    eax
  2916.         invlpg  [edx]
  2917.         pop     eax
  2918.         inc     eax
  2919.         loop    @b
  2920.         pop     eax
  2921.         and     dword [page_tabs+(eax-1)*4], not MEM_BLOCK_DONT_FREE
  2922.         stdcall user_free, ebx
  2923.         mov     [esp+32], eax
  2924.         and     [bgrlockpid], 0
  2925.         mov     [bgrlock], 0
  2926.         ret
  2927. ;--------------------------------------
  2928. align 4
  2929. .err:
  2930.         and     dword [esp+32], 0
  2931.         ret
  2932. ;------------------------------------------------------------------------------
  2933. align 4
  2934. nosb7:
  2935.         cmp     ebx, 8
  2936.         jnz     nosb8
  2937.  
  2938.         mov     ecx, [current_slot]
  2939.         xor     eax, eax
  2940.         xchg    eax, [ecx+APPDATA.draw_bgr_x]
  2941.         mov     [esp + 32], eax ; eax = [left]*65536 + [right]
  2942.         xor     eax, eax
  2943.         xchg    eax, [ecx+APPDATA.draw_bgr_y]
  2944.         mov     [esp + 20], eax ; ebx = [top]*65536 + [bottom]
  2945.         ret
  2946. ;------------------------------------------------------------------------------
  2947. align 4
  2948. nosb8:
  2949.         cmp     ebx, 9
  2950.         jnz     nosb9
  2951. ; ecx = [left]*65536 + [right]
  2952. ; edx = [top]*65536 + [bottom]
  2953.         mov     eax, [_display.width]
  2954.         mov     ebx, [_display.height]
  2955. ; check [right]
  2956.         cmp     cx, ax
  2957.         jae     .exit
  2958. ; check [left]
  2959.         ror     ecx, 16
  2960.         cmp     cx, ax
  2961.         jae     .exit
  2962. ; check [bottom]
  2963.         cmp     dx, bx
  2964.         jae     .exit
  2965. ; check [top]
  2966.         ror     edx, 16
  2967.         cmp     dx, bx
  2968.         jae     .exit
  2969.  
  2970.         movzx   eax, cx  ; [left]
  2971.         movzx   ebx, dx  ; [top]
  2972.  
  2973.         shr     ecx, 16 ; [right]
  2974.         shr     edx, 16 ; [bottom]
  2975.  
  2976.         mov     [background_defined], 1
  2977.  
  2978.         mov     [draw_data+32 + RECT.left], eax
  2979.         mov     [draw_data+32 + RECT.top], ebx
  2980.  
  2981.         mov     [draw_data+32 + RECT.right], ecx
  2982.         mov     [draw_data+32 + RECT.bottom], edx
  2983.  
  2984.         inc     [REDRAW_BACKGROUND]
  2985.         call    wakeup_osloop
  2986. ;--------------------------------------
  2987. align 4
  2988. .exit:
  2989.         ret
  2990. ;------------------------------------------------------------------------------
  2991. align 4
  2992. nosb9:
  2993.         ret
  2994. ;------------------------------------------------------------------------------
  2995. align 4
  2996. uglobal
  2997.   BG_Rect_X_left_right  dd   0x0
  2998.   BG_Rect_Y_top_bottom  dd   0x0
  2999. endg
  3000. ;------------------------------------------------------------------------------
  3001. align 4
  3002. force_redraw_background:
  3003.         and     [draw_data+32 + RECT.left], 0
  3004.         and     [draw_data+32 + RECT.top], 0
  3005.         push    eax ebx
  3006.         mov     eax, [_display.width]
  3007.         mov     ebx, [_display.height]
  3008.         dec     eax
  3009.         dec     ebx
  3010.         mov     [draw_data+32 + RECT.right], eax
  3011.         mov     [draw_data+32 + RECT.bottom], ebx
  3012.         pop     ebx eax
  3013.         inc     [REDRAW_BACKGROUND]
  3014.         call    wakeup_osloop
  3015.         ret
  3016. ;------------------------------------------------------------------------------
  3017. align 4
  3018. sys_getbackground:
  3019. ;    cmp   eax,1                                  ; SIZE
  3020.         dec     ebx
  3021.         jnz     nogb1
  3022.         mov     eax, [BgrDataWidth]
  3023.         shl     eax, 16
  3024.         mov     ax, word [BgrDataHeight]
  3025.         mov     [esp+32], eax
  3026.         ret
  3027. ;------------------------------------------------------------------------------
  3028. align 4
  3029. nogb1:
  3030. ;    cmp   eax,2                                  ; PIXEL
  3031.         dec     ebx
  3032.         jnz     nogb2
  3033.  
  3034.         mov     eax, [img_background]
  3035.         test    ecx, ecx
  3036.         jz      @f
  3037.         cmp     eax, static_background_data
  3038.         jz      .ret
  3039. ;--------------------------------------
  3040. align 4
  3041. @@:
  3042.         mov     ebx, [mem_BACKGROUND]
  3043.         add     ebx, 4095
  3044.         and     ebx, -4096
  3045.         sub     ebx, 4
  3046.         cmp     ecx, ebx
  3047.         ja      .ret
  3048.  
  3049.         mov     eax, [ecx+eax]
  3050.  
  3051.         and     eax, 0xFFFFFF
  3052.         mov     [esp+32], eax
  3053. ;--------------------------------------
  3054. align 4
  3055. .ret:
  3056.         ret
  3057. ;------------------------------------------------------------------------------
  3058. align 4
  3059. nogb2:
  3060.  
  3061. ;    cmp   eax,4                                  ; TILED / STRETCHED
  3062.         dec     ebx
  3063.         dec     ebx
  3064.         jnz     nogb4
  3065.         mov     eax, [BgrDrawMode]
  3066. ;--------------------------------------
  3067. align 4
  3068. nogb4:
  3069.         mov     [esp+32], eax
  3070.         ret
  3071. ;------------------------------------------------------------------------------
  3072. align 4
  3073. sys_getkey:
  3074.         mov     [esp + 32], dword 1
  3075.         ; test main buffer
  3076.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  3077.         movzx   ecx, word [WIN_STACK + ebx * 2]
  3078.         mov     edx, [TASK_COUNT]
  3079.         cmp     ecx, edx
  3080.         jne     .finish
  3081.         cmp     [KEY_COUNT], byte 0
  3082.         je      .finish
  3083.         movzx   ax, byte [KEY_BUFF + 120 + 2]
  3084.         shl     eax, 8
  3085.         mov     al, byte [KEY_BUFF]
  3086.         shl     eax, 8
  3087.         push    eax
  3088.         dec     byte [KEY_COUNT]
  3089.         and     byte [KEY_COUNT], 127
  3090.         movzx   ecx, byte [KEY_COUNT]
  3091.         add     ecx, 2
  3092.         mov     eax, KEY_BUFF + 1
  3093.         mov     ebx, KEY_BUFF
  3094.         call    memmove
  3095.         add     eax, 120 + 2
  3096.         add     ebx, 120 + 2
  3097.         call    memmove
  3098.         pop     eax
  3099. ;--------------------------------------
  3100. align 4
  3101. .ret_eax:
  3102.         mov     [esp + 32], eax
  3103.         ret
  3104. ;--------------------------------------
  3105. align 4
  3106. .finish:
  3107. ; test hotkeys buffer
  3108.         mov     ecx, hotkey_buffer
  3109. ;--------------------------------------
  3110. align 4
  3111. @@:
  3112.         cmp     [ecx], ebx
  3113.         jz      .found
  3114.         add     ecx, 8
  3115.         cmp     ecx, hotkey_buffer + 120 * 8
  3116.         jb      @b
  3117.         ret
  3118. ;--------------------------------------
  3119. align 4
  3120. .found:
  3121.         mov     ax, [ecx + 6]
  3122.         shl     eax, 16
  3123.         mov     ah, [ecx + 4]
  3124.         mov     al, 2
  3125.         and     dword [ecx + 4], 0
  3126.         and     dword [ecx], 0
  3127.         jmp     .ret_eax
  3128. ;------------------------------------------------------------------------------
  3129. align 4
  3130. sys_getbutton:
  3131.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  3132.         mov     [esp + 32], dword 1
  3133.         movzx   ecx, word [WIN_STACK + ebx * 2]
  3134.         mov     edx, [TASK_COUNT] ; less than 256 processes
  3135.         cmp     ecx, edx
  3136.         jne     .exit
  3137.         movzx   eax, byte [BTN_COUNT]
  3138.         test    eax, eax
  3139.         jz      .exit
  3140.         mov     eax, [BTN_BUFF]
  3141.         and     al, 0xFE                                    ; delete left button bit
  3142.         mov     [BTN_COUNT], byte 0
  3143.         mov     [esp + 32], eax
  3144. ;--------------------------------------
  3145. align 4
  3146. .exit:
  3147.         ret
  3148. ;------------------------------------------------------------------------------
  3149. align 4
  3150. sys_cpuusage:
  3151.  
  3152. ;  RETURN:
  3153. ;
  3154. ;  +00 dword     process cpu usage
  3155. ;  +04  word     position in windowing stack
  3156. ;  +06  word     windowing stack value at current position (cpu nro)
  3157. ;  +10 12 bytes  name
  3158. ;  +22 dword     start in mem
  3159. ;  +26 dword     used mem
  3160. ;  +30 dword     PID , process idenfification number
  3161. ;
  3162.         ; if given memory address belongs to kernel then error
  3163.         push    ebx
  3164.         mov     eax, ebx
  3165.         mov     ebx, 0x4C
  3166.         call    is_region_userspace
  3167.         pop     ebx
  3168.         test    eax, eax
  3169.         jz      .addr_error
  3170.  
  3171.         cmp     ecx, -1 ; who am I ?
  3172.         jne     .no_who_am_i
  3173.         mov     ecx, [CURRENT_TASK]
  3174.   .no_who_am_i:
  3175.         cmp     ecx, max_processes
  3176.         ja      .nofillbuf
  3177.  
  3178. ; +4: word: position of the window of thread in the window stack
  3179.         mov     ax, [WIN_STACK + ecx * 2]
  3180.         mov     [ebx+4], ax
  3181. ; +6: word: number of the thread slot, which window has in the window stack
  3182. ;           position ecx (has no relation to the specific thread)
  3183.         mov     ax, [WIN_POS + ecx * 2]
  3184.         mov     [ebx+6], ax
  3185.  
  3186.         shl     ecx, 5
  3187.  
  3188. ; +0: dword: memory usage
  3189.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  3190.         mov     [ebx], eax
  3191. ; +10: 11 bytes: name of the process
  3192.         push    ecx
  3193.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  3194.         add     ebx, 10
  3195.         mov     ecx, 11
  3196.         call    memmove
  3197.         pop     ecx
  3198.  
  3199. ; +22: address of the process in memory
  3200. ; +26: size of used memory - 1
  3201.         push    edi
  3202.         lea     edi, [ebx+12]
  3203.         xor     eax, eax
  3204.         mov     edx, 0x100000*16
  3205.         cmp     ecx, 1 shl 5
  3206.         je      .os_mem
  3207.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.process]
  3208.         mov     edx, [edx+PROC.mem_used]
  3209.         mov     eax, std_application_base_address
  3210. .os_mem:
  3211.         stosd
  3212.         lea     eax, [edx-1]
  3213.         stosd
  3214.  
  3215. ; +30: PID/TID
  3216.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  3217.         stosd
  3218.  
  3219.     ; window position and size
  3220.         push    esi
  3221.         lea     esi, [ecx + window_data + WDATA.box]
  3222.         movsd
  3223.         movsd
  3224.         movsd
  3225.         movsd
  3226.  
  3227.     ; Process state (+50)
  3228.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  3229.         stosd
  3230.  
  3231.     ; Window client area box
  3232.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  3233.         movsd
  3234.         movsd
  3235.         movsd
  3236.         movsd
  3237.  
  3238.     ; Window state
  3239.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  3240.         stosb
  3241.  
  3242.     ; Event mask (+71)
  3243.         mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
  3244.         stosd
  3245.  
  3246.     ; Keyboard mode (+75)
  3247.         mov     al, byte [ecx*8 + SLOT_BASE + APPDATA.keyboard_mode]
  3248.         stosb
  3249.  
  3250.         pop     esi
  3251.         pop     edi
  3252.  
  3253. .nofillbuf:
  3254.     ; return number of processes
  3255.  
  3256.         mov     eax, [TASK_COUNT]
  3257.         mov     [esp+32], eax
  3258.         ret
  3259.  
  3260. .addr_error:    ; if given memory address is illegal
  3261.         mov     eax, -1
  3262.         ret  
  3263.  
  3264. align 4
  3265. sys_clock:
  3266.         cli
  3267.   ; Mikhail Lisovin  xx Jan 2005
  3268.   @@:
  3269.         mov     al, 10
  3270.         out     0x70, al
  3271.         in      al, 0x71
  3272.         test    al, al
  3273.         jns     @f
  3274.         mov     esi, 1
  3275.         call    delay_ms
  3276.         jmp     @b
  3277.   @@:
  3278.   ; end Lisovin's fix
  3279.  
  3280.         xor     al, al        ; seconds
  3281.         out     0x70, al
  3282.         in      al, 0x71
  3283.         movzx   ecx, al
  3284.         mov     al, 02        ; minutes
  3285.         shl     ecx, 16
  3286.         out     0x70, al
  3287.         in      al, 0x71
  3288.         movzx   edx, al
  3289.         mov     al, 04        ; hours
  3290.         shl     edx, 8
  3291.         out     0x70, al
  3292.         in      al, 0x71
  3293.         add     ecx, edx
  3294.         movzx   edx, al
  3295.         add     ecx, edx
  3296.         sti
  3297.         mov     [esp + 32], ecx
  3298.         ret
  3299.  
  3300.  
  3301. align 4
  3302.  
  3303. sys_date:
  3304.  
  3305.         cli
  3306.   @@:
  3307.         mov     al, 10
  3308.         out     0x70, al
  3309.         in      al, 0x71
  3310.         test    al, al
  3311.         jns     @f
  3312.         mov     esi, 1
  3313.         call    delay_ms
  3314.         jmp     @b
  3315.   @@:
  3316.  
  3317.         mov     ch, 0
  3318.         mov     al, 7           ; date
  3319.         out     0x70, al
  3320.         in      al, 0x71
  3321.         mov     cl, al
  3322.         mov     al, 8           ; month
  3323.         shl     ecx, 16
  3324.         out     0x70, al
  3325.         in      al, 0x71
  3326.         mov     ch, al
  3327.         mov     al, 9           ; year
  3328.         out     0x70, al
  3329.         in      al, 0x71
  3330.         mov     cl, al
  3331.         sti
  3332.         mov     [esp+32], ecx
  3333.         ret
  3334.  
  3335.  
  3336. ; redraw status
  3337.  
  3338. sys_redrawstat:
  3339.         cmp     ebx, 1
  3340.         jne     no_widgets_away
  3341.         ; buttons away
  3342.         mov     ecx, [CURRENT_TASK]
  3343.   sys_newba2:
  3344.         mov     edi, [BTN_ADDR]
  3345.         cmp     [edi], dword 0  ; empty button list ?
  3346.         je      end_of_buttons_away
  3347.         movzx   ebx, word [edi]
  3348.         inc     ebx
  3349.         mov     eax, edi
  3350.   sys_newba:
  3351.         dec     ebx
  3352.         jz      end_of_buttons_away
  3353.  
  3354.         add     eax, 0x10
  3355.         cmp     cx, [eax]
  3356.         jnz     sys_newba
  3357.  
  3358.         push    eax ebx ecx
  3359.         mov     ecx, ebx
  3360.         inc     ecx
  3361.         shl     ecx, 4
  3362.         mov     ebx, eax
  3363.         add     eax, 0x10
  3364.         call    memmove
  3365.         dec     dword [edi]
  3366.         pop     ecx ebx eax
  3367.  
  3368.         jmp     sys_newba2
  3369.  
  3370.   end_of_buttons_away:
  3371.  
  3372.         ret
  3373.  
  3374.   no_widgets_away:
  3375.  
  3376.         cmp     ebx, 2
  3377.         jnz     srl1
  3378.  
  3379.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  3380.         add     edx, draw_data - CURRENT_TASK
  3381.         mov     [edx + RECT.left], 0
  3382.         mov     [edx + RECT.top], 0
  3383.         mov     eax, [_display.width]
  3384.         dec     eax
  3385.         mov     [edx + RECT.right], eax
  3386.         mov     eax, [_display.height]
  3387.         dec     eax
  3388.         mov     [edx + RECT.bottom], eax
  3389.  
  3390.   srl1:
  3391.         ret
  3392.  
  3393. ;ok - 100% work
  3394. ;nt - not tested
  3395. ;---------------------------------------------------------------------------------------------
  3396. ;eax
  3397. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3398. ;1 - change task. Ret nothing. Block. ok.
  3399. ;2 - performance control
  3400. ; ebx
  3401. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3402. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3403. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3404. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3405. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3406. ;eax
  3407. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3408. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3409. ;---------------------------------------------------------------------------------------------
  3410. iglobal
  3411. align 4
  3412. sheduler:
  3413.         dd      sys_sheduler.00
  3414.         dd      change_task
  3415.         dd      sys_sheduler.02
  3416.         dd      sys_sheduler.03
  3417.         dd      sys_sheduler.04
  3418. endg
  3419. sys_sheduler:
  3420. ;rewritten by <Lrz>  29.12.2009
  3421.         jmp     dword [sheduler+ebx*4]
  3422. ;.shed_counter:
  3423. .00:
  3424.         mov     eax, [context_counter]
  3425.         mov     [esp+32], eax
  3426.         ret
  3427.  
  3428. .02:
  3429. ;.perf_control:
  3430.         inc     ebx                     ;before ebx=2, ebx=3
  3431.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  3432.         jz      cache_disable
  3433.  
  3434.         dec     ebx                     ;ebx=2
  3435.         cmp     ebx, ecx                ;
  3436.         jz      cache_enable            ;if ecx=2 and ebx=2
  3437.  
  3438.         dec     ebx                     ;ebx=1
  3439.         cmp     ebx, ecx
  3440.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  3441.  
  3442.         dec     ebx
  3443.         test    ebx, ecx                ;ebx=0 and ecx=0
  3444.         jz      modify_pce              ;if ecx=0
  3445.  
  3446.         ret
  3447.  
  3448. .03:
  3449. ;.rdmsr_instr:
  3450. ;now counter in ecx
  3451. ;(edx:eax) esi:edi => edx:esi
  3452.         mov     eax, esi
  3453.         mov     ecx, edx
  3454.         rdmsr
  3455.         mov     [esp+32], eax
  3456.         mov     [esp+20], edx           ;ret in ebx?
  3457.         ret
  3458.  
  3459. .04:
  3460. ;.wrmsr_instr:
  3461. ;now counter in ecx
  3462. ;(edx:eax) esi:edi => edx:esi
  3463.         ; Fast Call MSR can't be destroy
  3464.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  3465.         ; включаются/выключаются расширенные возможности
  3466.         cmp     edx, MSR_SYSENTER_CS
  3467.         je      @f
  3468.         cmp     edx, MSR_SYSENTER_ESP
  3469.         je      @f
  3470.         cmp     edx, MSR_SYSENTER_EIP
  3471.         je      @f
  3472.         cmp     edx, MSR_AMD_STAR
  3473.         je      @f
  3474.  
  3475.         mov     eax, esi
  3476.         mov     ecx, edx
  3477.         wrmsr
  3478.         ; mov   [esp + 32], eax
  3479.         ; mov   [esp + 20], edx ;ret in ebx?
  3480. @@:
  3481.         ret
  3482.  
  3483. cache_disable:
  3484.         mov     eax, cr0
  3485.         or      eax, 01100000000000000000000000000000b
  3486.         mov     cr0, eax
  3487.         wbinvd  ;set MESI
  3488.         ret
  3489.  
  3490. cache_enable:
  3491.         mov     eax, cr0
  3492.         and     eax, 10011111111111111111111111111111b
  3493.         mov     cr0, eax
  3494.         ret
  3495.  
  3496. is_cache_enabled:
  3497.         mov     eax, cr0
  3498.         mov     ebx, eax
  3499.         and     eax, 01100000000000000000000000000000b
  3500.         jz      cache_disabled
  3501.         mov     [esp+32], ebx
  3502. cache_disabled:
  3503.         mov     dword [esp+32], eax;0
  3504.         ret
  3505.  
  3506. modify_pce:
  3507.         mov     eax, cr4
  3508. ;       mov ebx,0
  3509. ;       or  bx,100000000b ;pce
  3510. ;       xor eax,ebx ;invert pce
  3511.         bts     eax, 8;pce=cr4[8]
  3512.         mov     cr4, eax
  3513.         mov     [esp+32], eax
  3514.         ret
  3515. ;---------------------------------------------------------------------------------------------
  3516.  
  3517.  
  3518. iglobal
  3519.   cpustring db 'CPU',0
  3520. endg
  3521.  
  3522. uglobal
  3523. background_defined    db    0    ; diamond, 11.04.2006
  3524. endg
  3525. ;-----------------------------------------------------------------------------
  3526. align 4
  3527. checkmisc:
  3528.         cmp     [ctrl_alt_del], 1
  3529.         jne     nocpustart
  3530.  
  3531.         mov     ebp, cpustring
  3532.         call    fs_execute_from_sysdir
  3533.  
  3534.         mov     [ctrl_alt_del], 0
  3535. ;--------------------------------------
  3536. align 4
  3537. nocpustart:
  3538.         cmp     [mouse_active], 1
  3539.         jne     mouse_not_active
  3540.         mov     [mouse_active], 0
  3541.  
  3542.         xor     edi, edi
  3543.         mov     ebx, CURRENT_TASK
  3544.  
  3545.         mov     ecx, [TASK_COUNT]
  3546.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  3547.         shl     eax, 8
  3548.         push    eax
  3549.  
  3550.         movzx   eax, word [MOUSE_X]
  3551.         movzx   edx, word [MOUSE_Y]
  3552. ;--------------------------------------
  3553. align 4
  3554. .set_mouse_event:
  3555.         add     edi, sizeof.APPDATA
  3556.         add     ebx, sizeof.TASKDATA
  3557.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3558.         jz      .pos_filter
  3559.  
  3560.         cmp     edi, [esp]                      ; skip if filtration active
  3561.         jne     .skip
  3562. ;--------------------------------------
  3563. align 4
  3564. .pos_filter:
  3565.         test    [ebx+TASKDATA.event_mask], 0x40000000
  3566.         jz      .set
  3567.  
  3568.         mov     esi, [ebx-twdw+WDATA.box.left]
  3569.         cmp     eax, esi
  3570.         jb      .skip
  3571.         add     esi, [ebx-twdw+WDATA.box.width]
  3572.         cmp     eax, esi
  3573.         ja      .skip
  3574.  
  3575.         mov     esi, [ebx-twdw+WDATA.box.top]
  3576.         cmp     edx, esi
  3577.         jb      .skip
  3578.         add     esi, [ebx-twdw+WDATA.box.height]
  3579.         cmp     edx, esi
  3580.         ja      .skip
  3581. ;--------------------------------------
  3582. align 4
  3583. .set:
  3584.         or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b  ; set event 6
  3585. ;--------------------------------------
  3586. align 4
  3587. .skip:
  3588.         loop    .set_mouse_event
  3589.  
  3590.         pop     eax
  3591. ;--------------------------------------
  3592. align 4
  3593. mouse_not_active:
  3594.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  3595.         jz      nobackgr
  3596.  
  3597.         cmp     [background_defined], 0
  3598.         jz      nobackgr
  3599. ;--------------------------------------
  3600. align 4
  3601. backgr:
  3602.         mov     eax, [draw_data+32 + RECT.left]
  3603.         shl     eax, 16
  3604.         add     eax, [draw_data+32 + RECT.right]
  3605.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  3606.  
  3607.         mov     eax, [draw_data+32 + RECT.top]
  3608.         shl     eax, 16
  3609.         add     eax, [draw_data+32 + RECT.bottom]
  3610.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  3611.  
  3612.         call    drawbackground
  3613. ;        DEBUGF  1, "K : drawbackground\n"
  3614. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  3615. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  3616. ;--------- set event 5 start ----------
  3617.         push    ecx edi
  3618.         xor     edi, edi
  3619.         mov     ecx, [TASK_COUNT]
  3620. ;--------------------------------------
  3621. align 4
  3622. set_bgr_event:
  3623.         add     edi, sizeof.APPDATA
  3624.         mov     eax, [BG_Rect_X_left_right]
  3625.         mov     edx, [BG_Rect_Y_top_bottom]
  3626.         cmp     [edi+SLOT_BASE+APPDATA.draw_bgr_x], 0
  3627.         jz      .set
  3628. .join:
  3629.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3630.         jae     @f
  3631.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3632. @@:
  3633.         shr     eax, 16
  3634.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3635.         jbe     @f
  3636.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3637. @@:
  3638.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3639.         jae     @f
  3640.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3641. @@:
  3642.         shr     edx, 16
  3643.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3644.         jbe     @f
  3645.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3646. @@:
  3647.         jmp     .common
  3648. .set:
  3649.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_x], eax
  3650.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_y], edx
  3651. .common:
  3652.         or      [edi+SLOT_BASE+APPDATA.event_mask], 10000b  ; set event 5
  3653.         loop    set_bgr_event
  3654.         pop     edi ecx
  3655. ;--------- set event 5 stop -----------
  3656.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  3657.         jnz     backgr
  3658.  
  3659.         xor     eax, eax
  3660.         mov     [draw_data+32 + RECT.left], eax
  3661.         mov     [draw_data+32 + RECT.top], eax
  3662.         mov     [draw_data+32 + RECT.right], eax
  3663.         mov     [draw_data+32 + RECT.bottom], eax
  3664. ;--------------------------------------
  3665. align 4
  3666. nobackgr:
  3667. ; system shutdown request
  3668.         cmp     [SYS_SHUTDOWN], byte 0
  3669.         je      noshutdown
  3670.  
  3671.         mov     edx, [shutdown_processes]
  3672.  
  3673.         cmp     [SYS_SHUTDOWN], dl
  3674.         jne     noshutdown
  3675.  
  3676.         lea     ecx, [edx-1]
  3677.         mov     edx, OS_BASE+0x3040
  3678.         jecxz   no_mark_system_shutdown
  3679. ;--------------------------------------
  3680. align 4
  3681. markz:
  3682.         push    ecx edx
  3683.         cmp     [edx+TASKDATA.state], TSTATE_FREE
  3684.         jz      .nokill
  3685.         lea     edx, [(edx-(CURRENT_TASK and 1FFFFFFFh))*8+SLOT_BASE]
  3686.         cmp     [edx+APPDATA.process], sys_proc
  3687.         jz      .nokill
  3688.         call    request_terminate
  3689.         jmp     .common
  3690. .nokill:
  3691.         dec     byte [SYS_SHUTDOWN]
  3692.         xor     eax, eax
  3693. .common:
  3694.         pop     edx ecx
  3695.         test    eax, eax
  3696.         jz      @f
  3697.         mov     [edx+TASKDATA.state], TSTATE_ZOMBIE
  3698. @@:
  3699.         add     edx, 0x20
  3700.         loop    markz
  3701.         call    wakeup_osloop
  3702. ;--------------------------------------
  3703. align 4
  3704. @@:
  3705. no_mark_system_shutdown:
  3706.         dec     byte [SYS_SHUTDOWN]
  3707.         je      system_shutdown
  3708. ;--------------------------------------
  3709. align 4
  3710. noshutdown:
  3711.         mov     eax, [TASK_COUNT]           ; termination
  3712.         mov     ebx, TASK_DATA+TASKDATA.state
  3713.         mov     esi, 1
  3714. ;--------------------------------------
  3715. align 4
  3716. newct:
  3717.         mov     cl, [ebx]
  3718.         cmp     cl, byte 3
  3719.         jz      .terminate
  3720.  
  3721.         cmp     cl, byte 4
  3722.         jnz     .noterminate
  3723. .terminate:
  3724.         pushad
  3725.         mov     ecx, eax
  3726.         shl     ecx, 8
  3727.         add     ecx, SLOT_BASE
  3728.         call    restore_default_cursor_before_killing
  3729.         popad
  3730.  
  3731.         pushad
  3732.         call    terminate
  3733.         popad
  3734.         cmp     byte [SYS_SHUTDOWN], 0
  3735.         jz      .noterminate
  3736.         dec     byte [SYS_SHUTDOWN]
  3737.         je      system_shutdown
  3738.  
  3739. .noterminate:
  3740.         add     ebx, 0x20
  3741.         inc     esi
  3742.         dec     eax
  3743.         jnz     newct
  3744.         ret
  3745. ;-----------------------------------------------------------------------------
  3746. align 4
  3747. redrawscreen:
  3748. ; eax , if process window_data base is eax, do not set flag/limits
  3749.  
  3750.         pushad
  3751.         push    eax
  3752.  
  3753. ;;;         mov   ebx,2
  3754. ;;;         call  delay_hs
  3755.  
  3756.          ;mov   ecx,0               ; redraw flags for apps
  3757.         xor     ecx, ecx
  3758. ;--------------------------------------
  3759. align 4
  3760. newdw2:
  3761.         inc     ecx
  3762.         push    ecx
  3763.  
  3764.         mov     eax, ecx
  3765.         shl     eax, 5
  3766.         add     eax, window_data
  3767.  
  3768.         cmp     eax, [esp+4]
  3769.         je      not_this_task
  3770.                                    ; check if window in redraw area
  3771.         mov     edi, eax
  3772.  
  3773.         cmp     ecx, 1             ; limit for background
  3774.         jz      bgli
  3775.  
  3776.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3777.         test    eax, eax
  3778.         jz      @f
  3779.         mov     al, [eax + WDATA.z_modif]
  3780.         cmp     [edi + WDATA.z_modif], al
  3781.         jg      ricino
  3782.       @@:
  3783.  
  3784.         mov     eax, [edi + WDATA.box.left]
  3785.         mov     ebx, [edi + WDATA.box.top]
  3786.  
  3787.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3788.         cmp     ecx, ebx
  3789.         jb      ricino
  3790.  
  3791.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3792.         cmp     ecx, eax
  3793.         jb      ricino
  3794.  
  3795.         mov     eax, [edi + WDATA.box.left]
  3796.         mov     ebx, [edi + WDATA.box.top]
  3797.         mov     ecx, [edi + WDATA.box.width]
  3798.         mov     edx, [edi + WDATA.box.height]
  3799.         add     ecx, eax
  3800.         add     edx, ebx
  3801.  
  3802.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3803.         cmp     edx, eax
  3804.         jb      ricino
  3805.  
  3806.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3807.         cmp     ecx, eax
  3808.         jb      ricino
  3809. ;--------------------------------------
  3810. align 4
  3811. bgli:
  3812.         cmp     dword[esp], 1
  3813.         jnz     .az
  3814.  
  3815.         cmp     [REDRAW_BACKGROUND], 0
  3816.         jz      .az
  3817.  
  3818.         mov     dl, 0
  3819.         lea     eax, [edi+draw_data-window_data]
  3820.         mov     ebx, [draw_limits.left]
  3821.         cmp     ebx, [eax+RECT.left]
  3822.         jae     @f
  3823.  
  3824.         mov     [eax+RECT.left], ebx
  3825.         mov     dl, 1
  3826. ;--------------------------------------
  3827. align 4
  3828. @@:
  3829.         mov     ebx, [draw_limits.top]
  3830.         cmp     ebx, [eax+RECT.top]
  3831.         jae     @f
  3832.  
  3833.         mov     [eax+RECT.top], ebx
  3834.         mov     dl, 1
  3835. ;--------------------------------------
  3836. align 4
  3837. @@:
  3838.         mov     ebx, [draw_limits.right]
  3839.         cmp     ebx, [eax+RECT.right]
  3840.         jbe     @f
  3841.  
  3842.         mov     [eax+RECT.right], ebx
  3843.         mov     dl, 1
  3844. ;--------------------------------------
  3845. align 4
  3846. @@:
  3847.         mov     ebx, [draw_limits.bottom]
  3848.         cmp     ebx, [eax+RECT.bottom]
  3849.         jbe     @f
  3850.  
  3851.         mov     [eax+RECT.bottom], ebx
  3852.         mov     dl, 1
  3853. ;--------------------------------------
  3854. align 4
  3855. @@:
  3856.         add     [REDRAW_BACKGROUND], dl
  3857.         call    wakeup_osloop
  3858.         jmp     newdw8
  3859. ;--------------------------------------
  3860. align 4
  3861. .az:
  3862.         mov     eax, edi
  3863.         add     eax, draw_data-window_data
  3864.  
  3865.         mov     ebx, [draw_limits.left]        ; set limits
  3866.         mov     [eax + RECT.left], ebx
  3867.         mov     ebx, [draw_limits.top]
  3868.         mov     [eax + RECT.top], ebx
  3869.         mov     ebx, [draw_limits.right]
  3870.         mov     [eax + RECT.right], ebx
  3871.         mov     ebx, [draw_limits.bottom]
  3872.         mov     [eax + RECT.bottom], ebx
  3873.  
  3874.         sub     eax, draw_data-window_data
  3875.  
  3876.         cmp     dword [esp], 1
  3877.         jne     nobgrd
  3878.         inc     [REDRAW_BACKGROUND]
  3879.         call    wakeup_osloop
  3880. ;--------------------------------------
  3881. align 4
  3882. newdw8:
  3883. nobgrd:
  3884. ;--------------------------------------
  3885.         push    eax  edi ebp
  3886.         mov     edi, [esp+12]
  3887.         cmp     edi, 1
  3888.         je      .found
  3889.  
  3890.         mov     eax, [draw_limits.left]
  3891.         mov     ebx, [draw_limits.top]
  3892.         mov     ecx, [draw_limits.right]
  3893.         sub     ecx, eax
  3894.         test    ecx, ecx
  3895.         jz      .not_found
  3896.  
  3897.         mov     edx, [draw_limits.bottom]
  3898.         sub     edx, ebx
  3899.         test    edx, edx
  3900.         jz      .not_found
  3901.  
  3902. ; eax - x, ebx - y
  3903. ; ecx - size x, edx - size y
  3904.         add     ebx, edx
  3905. ;--------------------------------------
  3906. align 4
  3907. .start_y:
  3908.         push    ecx
  3909. ;--------------------------------------
  3910. align 4
  3911. .start_x:
  3912.         add     eax, ecx
  3913.         mov     ebp, [d_width_calc_area + ebx*4]
  3914.         add     ebp, [_display.win_map]
  3915.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3916.         cmp     ebp, edi
  3917.         jne     @f
  3918.  
  3919.         pop     ecx
  3920.         jmp     .found
  3921. ;--------------------------------------
  3922. align 4
  3923. @@:
  3924.         sub     eax, ecx
  3925.  
  3926.         dec     ecx
  3927.         jnz     .start_x
  3928.  
  3929.         pop     ecx
  3930.         dec     ebx
  3931.         dec     edx
  3932.         jnz     .start_y
  3933. ;--------------------------------------
  3934. align 4
  3935. .not_found:
  3936.         pop     ebp edi eax
  3937.         jmp     ricino
  3938. ;--------------------------------------
  3939. align 4
  3940. .found:
  3941.         pop     ebp edi eax
  3942.  
  3943.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3944. ;--------------------------------------
  3945. align 4
  3946. ricino:
  3947. not_this_task:
  3948.         pop     ecx
  3949.  
  3950.         cmp     ecx, [TASK_COUNT]
  3951.         jle     newdw2
  3952.  
  3953.         pop     eax
  3954.         popad
  3955.         ret
  3956. ;-----------------------------------------------------------------------------
  3957. align 4
  3958. calculatebackground:   ; background
  3959.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3960.         mov     eax, 0x01010101
  3961.         mov     ecx, [_display.win_map_size]
  3962.         shr     ecx, 2
  3963.         rep stosd
  3964.         mov     byte[window_data+32+WDATA.z_modif], ZPOS_DESKTOP
  3965.         mov     [REDRAW_BACKGROUND], 0
  3966.         ret
  3967. ;-----------------------------------------------------------------------------
  3968. uglobal
  3969.   imax    dd 0x0
  3970. endg
  3971. ;-----------------------------------------------------------------------------
  3972. align 4
  3973. delay_ms:     ; delay in 1/1000 sec
  3974.         pushad
  3975.  
  3976.         cmp     [hpet_base], 0
  3977.         jz      .no_hpet
  3978.         mov     eax, esi
  3979.         mov     edx, 1_000_000 ; ms to ns
  3980.         mul     edx
  3981.         mov     ebx, edx
  3982.         mov     ecx, eax
  3983.  
  3984.         push    ecx
  3985.         call    get_clock_ns
  3986.         pop     ecx
  3987.         mov     edi, edx
  3988.         mov     esi, eax
  3989. .wait:
  3990.         push    ecx
  3991.         call    get_clock_ns
  3992.         pop     ecx
  3993.         sub     eax, esi
  3994.         sbb     edx, edi
  3995.         sub     eax, ecx
  3996.         sbb     edx, ebx
  3997.         jc      .wait
  3998.         jmp     .done
  3999.  
  4000. .no_hpet:
  4001.         mov     ecx, esi
  4002.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  4003.         imul    ecx, 33941
  4004.         shr     ecx, 9
  4005.         ; </CPU clock fix>
  4006.  
  4007.         in      al, 0x61
  4008.         and     al, 0x10
  4009.         mov     ah, al
  4010.         cld
  4011.  
  4012. .cnt1:
  4013.         in      al, 0x61
  4014.         and     al, 0x10
  4015.         cmp     al, ah
  4016.         jz      .cnt1
  4017.  
  4018.         mov     ah, al
  4019.         loop    .cnt1
  4020.  
  4021. .done:
  4022.         popad
  4023.         ret
  4024. ;-----------------------------------------------------------------------------
  4025. align 4
  4026. set_app_param:
  4027.         mov     edi, [TASK_BASE]
  4028.         mov     eax, ebx
  4029.         btr     eax, 3                           ; move MOUSE_FILTRATION
  4030.         mov     ebx, [current_slot]              ; bit into event_filter
  4031.         setc    byte [ebx+APPDATA.event_filter]
  4032.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  4033.         mov     [esp+32], eax                    ; return old mask value
  4034.         ret
  4035. ;-----------------------------------------------------------------------------
  4036.  
  4037. ; this is for syscall
  4038. proc delay_hs_unprotected
  4039.         call    unprotect_from_terminate
  4040.         call    delay_hs
  4041.         call    protect_from_terminate
  4042.         ret
  4043. endp
  4044.  
  4045. if 1
  4046. align 4
  4047. delay_hs:     ; delay in 1/100 secs
  4048. ; ebx = delay time
  4049.  
  4050.         pushad
  4051.         push    ebx
  4052.         xor     esi, esi
  4053.         mov     ecx, MANUAL_DESTROY
  4054.         call    create_event
  4055.         test    eax, eax
  4056.         jz      .done
  4057.  
  4058.         mov     ebx, edx
  4059.         mov     ecx, [esp]
  4060.         push    edx
  4061.         push    eax
  4062.         call    wait_event_timeout
  4063.         pop     eax
  4064.         pop     ebx
  4065.         call    destroy_event
  4066. .done:
  4067.         add     esp, 4
  4068.         popad
  4069.         ret
  4070.  
  4071. else
  4072.  
  4073. align 4
  4074. delay_hs:     ; delay in 1/100 secs
  4075. ; ebx = delay time
  4076.         push    ecx
  4077.         push    edx
  4078.  
  4079.         mov     edx, [timer_ticks]
  4080. ;--------------------------------------
  4081. align 4
  4082. newtic:
  4083.         mov     ecx, [timer_ticks]
  4084.         sub     ecx, edx
  4085.         cmp     ecx, ebx
  4086.         jae     zerodelay
  4087.  
  4088.         call    change_task
  4089.  
  4090.         jmp     newtic
  4091. ;--------------------------------------
  4092. align 4
  4093. zerodelay:
  4094.         pop     edx
  4095.         pop     ecx
  4096.         ret
  4097. end if
  4098.  
  4099. ;-----------------------------------------------------------------------------
  4100. align 16        ;very often call this subrutine
  4101. memmove:       ; memory move in bytes
  4102. ; eax = from
  4103. ; ebx = to
  4104. ; ecx = no of bytes
  4105.         test    ecx, ecx
  4106.         jle     .ret
  4107.  
  4108.         push    esi edi ecx
  4109.  
  4110.         mov     edi, ebx
  4111.         mov     esi, eax
  4112.  
  4113.         test    ecx, not 11b
  4114.         jz      @f
  4115.  
  4116.         push    ecx
  4117.         shr     ecx, 2
  4118.         rep movsd
  4119.         pop     ecx
  4120.         and     ecx, 11b
  4121.         jz      .finish
  4122. ;--------------------------------------
  4123. align 4
  4124. @@:
  4125.         rep movsb
  4126. ;--------------------------------------
  4127. align 4
  4128. .finish:
  4129.         pop     ecx edi esi
  4130. ;--------------------------------------
  4131. align 4
  4132. .ret:
  4133.         ret
  4134. ;-----------------------------------------------------------------------------
  4135. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  4136. ;align 4
  4137. ;
  4138. ;read_floppy_file:
  4139. ;
  4140. ;; as input
  4141. ;;
  4142. ;; eax pointer to file
  4143. ;; ebx file lenght
  4144. ;; ecx start 512 byte block number
  4145. ;; edx number of blocks to read
  4146. ;; esi pointer to return/work area (atleast 20 000 bytes)
  4147. ;;
  4148. ;;
  4149. ;; on return
  4150. ;;
  4151. ;; eax = 0 command succesful
  4152. ;;       1 no fd base and/or partition defined
  4153. ;;       2 yet unsupported FS
  4154. ;;       3 unknown FS
  4155. ;;       4 partition not defined at hd
  4156. ;;       5 file not found
  4157. ;; ebx = size of file
  4158. ;
  4159. ;     mov   edi,[TASK_BASE]
  4160. ;     add   edi,0x10
  4161. ;     add   esi,[edi]
  4162. ;     add   eax,[edi]
  4163. ;
  4164. ;     pushad
  4165. ;     mov  edi,esi
  4166. ;     add  edi,1024
  4167. ;     mov  esi,0x100000+19*512
  4168. ;     sub  ecx,1
  4169. ;     shl  ecx,9
  4170. ;     add  esi,ecx
  4171. ;     shl  edx,9
  4172. ;     mov  ecx,edx
  4173. ;     cld
  4174. ;     rep  movsb
  4175. ;     popad
  4176. ;
  4177. ;     mov   [esp+36],eax
  4178. ;     mov   [esp+24],ebx
  4179. ;     ret
  4180.  
  4181.  
  4182.  
  4183. align 4
  4184. set_io_access_rights:
  4185.         push    edi eax
  4186.         mov     edi, tss._io_map_0
  4187. ;     mov   ecx,eax
  4188. ;     and   ecx,7    ; offset in byte
  4189. ;     shr   eax,3    ; number of byte
  4190. ;     add   edi,eax
  4191. ;     mov   ebx,1
  4192. ;     shl   ebx,cl
  4193.         test    ebp, ebp
  4194. ;     cmp   ebp,0                ; enable access - ebp = 0
  4195.         jnz     .siar1
  4196. ;     not   ebx
  4197. ;     and   [edi],byte bl
  4198.         btr     [edi], eax
  4199.         pop     eax edi
  4200.         ret
  4201. .siar1:
  4202.         bts     [edi], eax
  4203.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4204.         pop     eax edi
  4205.         ret
  4206. ;reserve/free group of ports
  4207. ;  * eax = 46 - number function
  4208. ;  * ebx = 0 - reserve, 1 - free
  4209. ;  * ecx = number start arrea of ports
  4210. ;  * edx = number end arrea of ports (include last number of port)
  4211. ;Return value:
  4212. ;  * eax = 0 - succesful
  4213. ;  * eax = 1 - error
  4214. ;  * The system has reserve this ports:
  4215. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  4216. ;destroys eax,ebx, ebp
  4217. r_f_port_area:
  4218.  
  4219.         test    ebx, ebx
  4220.         jnz     free_port_area
  4221. ;     je    r_port_area
  4222. ;     jmp   free_port_area
  4223.  
  4224. ;   r_port_area:
  4225.  
  4226. ;     pushad
  4227.  
  4228.         cmp     ecx, edx      ; beginning > end ?
  4229.         ja      rpal1
  4230.         cmp     edx, 65536
  4231.         jae     rpal1
  4232.         mov     eax, [RESERVED_PORTS]
  4233.         test    eax, eax      ; no reserved areas ?
  4234.         je      rpal2
  4235.         cmp     eax, 255      ; max reserved
  4236.         jae     rpal1
  4237.  rpal3:
  4238.         mov     ebx, eax
  4239.         shl     ebx, 4
  4240.         add     ebx, RESERVED_PORTS
  4241.         cmp     ecx, [ebx+8]
  4242.         ja      rpal4
  4243.         cmp     edx, [ebx+4]
  4244.         jae     rpal1
  4245. ;     jb    rpal4
  4246. ;     jmp   rpal1
  4247.  rpal4:
  4248.         dec     eax
  4249.         jnz     rpal3
  4250.         jmp     rpal2
  4251.    rpal1:
  4252. ;     popad
  4253. ;     mov   eax,1
  4254.         xor     eax, eax
  4255.         inc     eax
  4256.         ret
  4257.    rpal2:
  4258. ;     popad
  4259.      ; enable port access at port IO map
  4260.         cli
  4261.         pushad                        ; start enable io map
  4262.  
  4263.         cmp     edx, 65536;16384
  4264.         jae     no_unmask_io; jge
  4265.         mov     eax, ecx
  4266. ;       push    ebp
  4267.         xor     ebp, ebp               ; enable - eax = port
  4268. new_port_access:
  4269. ;     pushad
  4270.         call    set_io_access_rights
  4271. ;     popad
  4272.         inc     eax
  4273.         cmp     eax, edx
  4274.         jbe     new_port_access
  4275. ;       pop     ebp
  4276. no_unmask_io:
  4277.         popad                         ; end enable io map
  4278.         sti
  4279.  
  4280.         mov     eax, [RESERVED_PORTS]
  4281.         add     eax, 1
  4282.         mov     [RESERVED_PORTS], eax
  4283.         shl     eax, 4
  4284.         add     eax, RESERVED_PORTS
  4285.         mov     ebx, [TASK_BASE]
  4286.         mov     ebx, [ebx+TASKDATA.pid]
  4287.         mov     [eax], ebx
  4288.         mov     [eax+4], ecx
  4289.         mov     [eax+8], edx
  4290.  
  4291.         xor     eax, eax
  4292.         ret
  4293.  
  4294. free_port_area:
  4295.  
  4296. ;     pushad
  4297.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  4298.         test    eax, eax
  4299.         jz      frpal2
  4300.         mov     ebx, [TASK_BASE]
  4301.         mov     ebx, [ebx+TASKDATA.pid]
  4302.    frpal3:
  4303.         mov     edi, eax
  4304.         shl     edi, 4
  4305.         add     edi, RESERVED_PORTS
  4306.         cmp     ebx, [edi]
  4307.         jne     frpal4
  4308.         cmp     ecx, [edi+4]
  4309.         jne     frpal4
  4310.         cmp     edx, [edi+8]
  4311.         jne     frpal4
  4312.         jmp     frpal1
  4313.    frpal4:
  4314.         dec     eax
  4315.         jnz     frpal3
  4316.    frpal2:
  4317. ;     popad
  4318.         inc     eax
  4319.         ret
  4320.    frpal1:
  4321.         push    ecx
  4322.         mov     ecx, 256
  4323.         sub     ecx, eax
  4324.         shl     ecx, 4
  4325.         mov     esi, edi
  4326.         add     esi, 16
  4327.         cld
  4328.         rep movsb
  4329.  
  4330.         dec     dword [RESERVED_PORTS]
  4331. ;popad
  4332. ;disable port access at port IO map
  4333.  
  4334. ;     pushad                        ; start disable io map
  4335.         pop     eax     ;start port
  4336.         cmp     edx, 65536;16384
  4337.         jge     no_mask_io
  4338.  
  4339. ;     mov   eax,ecx
  4340.         xor     ebp, ebp
  4341.         inc     ebp
  4342. new_port_access_disable:
  4343. ;     pushad
  4344. ;     mov   ebp,1                  ; disable - eax = port
  4345.         call    set_io_access_rights
  4346. ;     popad
  4347.         inc     eax
  4348.         cmp     eax, edx
  4349.         jbe     new_port_access_disable
  4350. no_mask_io:
  4351. ;     popad                         ; end disable io map
  4352.         xor     eax, eax
  4353.         ret
  4354. ;-----------------------------------------------------------------------------
  4355. align 4
  4356. drawbackground:
  4357. dbrv20:
  4358.         cmp     [BgrDrawMode], dword 1
  4359.         jne     bgrstr
  4360.         call    vesa20_drawbackground_tiled
  4361. ;        call    [draw_pointer]
  4362.         call    __sys_draw_pointer
  4363.         ret
  4364. ;--------------------------------------
  4365. align 4
  4366. bgrstr:
  4367.         call    vesa20_drawbackground_stretch
  4368. ;        call    [draw_pointer]
  4369.         call    __sys_draw_pointer
  4370.         ret
  4371. ;-----------------------------------------------------------------------------
  4372. align 4
  4373. syscall_putimage:                       ; PutImage
  4374. sys_putimage:
  4375.         test    ecx, 0x80008000
  4376.         jnz     .exit
  4377.         test    ecx, 0x0000FFFF
  4378.         jz      .exit
  4379.         test    ecx, 0xFFFF0000
  4380.         jnz     @f
  4381. ;--------------------------------------
  4382. align 4
  4383. .exit:
  4384.         ret
  4385. ;--------------------------------------
  4386. align 4
  4387. @@:
  4388.         mov     edi, [current_slot]
  4389.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  4390.         rol     edx, 16
  4391.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  4392.         rol     edx, 16
  4393. ;--------------------------------------
  4394. align 4
  4395. .forced:
  4396.         push    ebp esi 0
  4397.         mov     ebp, putimage_get24bpp
  4398.         mov     esi, putimage_init24bpp
  4399. ;--------------------------------------
  4400. align 4
  4401. sys_putimage_bpp:
  4402.         call    vesa20_putimage
  4403.         pop     ebp esi ebp
  4404.         ret
  4405. ;        jmp     [draw_pointer]
  4406. ;-----------------------------------------------------------------------------
  4407. align 4
  4408. sys_putimage_palette:
  4409. ; ebx = pointer to image
  4410. ; ecx = [xsize]*65536 + [ysize]
  4411. ; edx = [xstart]*65536 + [ystart]
  4412. ; esi = number of bits per pixel, must be 8, 24 or 32
  4413. ; edi = pointer to palette
  4414. ; ebp = row delta
  4415.         mov     eax, [CURRENT_TASK]
  4416.         shl     eax, 8
  4417.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4418.         rol     edx, 16
  4419.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4420.         rol     edx, 16
  4421. ;--------------------------------------
  4422. align 4
  4423. .forced:
  4424.         cmp     esi, 1
  4425.         jnz     @f
  4426.         push    edi
  4427.         mov     eax, [edi+4]
  4428.         sub     eax, [edi]
  4429.         push    eax
  4430.         push    dword [edi]
  4431.         push    0ffffff80h
  4432.         mov     edi, esp
  4433.         call    put_mono_image
  4434.         add     esp, 12
  4435.         pop     edi
  4436.         ret
  4437. ;--------------------------------------
  4438. align 4
  4439. @@:
  4440.         cmp     esi, 2
  4441.         jnz     @f
  4442.         push    edi
  4443.         push    0ffffff80h
  4444.         mov     edi, esp
  4445.         call    put_2bit_image
  4446.         pop     eax
  4447.         pop     edi
  4448.         ret
  4449. ;--------------------------------------
  4450. align 4
  4451. @@:
  4452.         cmp     esi, 4
  4453.         jnz     @f
  4454.         push    edi
  4455.         push    0ffffff80h
  4456.         mov     edi, esp
  4457.         call    put_4bit_image
  4458.         pop     eax
  4459.         pop     edi
  4460.         ret
  4461. ;--------------------------------------
  4462. align 4
  4463. @@:
  4464.         push    ebp esi ebp
  4465.         cmp     esi, 8
  4466.         jnz     @f
  4467.         mov     ebp, putimage_get8bpp
  4468.         mov     esi, putimage_init8bpp
  4469.         jmp     sys_putimage_bpp
  4470. ;--------------------------------------
  4471. align 4
  4472. @@:
  4473.         cmp     esi, 9
  4474.         jnz     @f
  4475.         mov     ebp, putimage_get9bpp
  4476.         mov     esi, putimage_init9bpp
  4477.         jmp     sys_putimage_bpp
  4478. ;--------------------------------------
  4479. align 4
  4480. @@:
  4481.         cmp     esi, 15
  4482.         jnz     @f
  4483.         mov     ebp, putimage_get15bpp
  4484.         mov     esi, putimage_init15bpp
  4485.         jmp     sys_putimage_bpp
  4486. ;--------------------------------------
  4487. align 4
  4488. @@:
  4489.         cmp     esi, 16
  4490.         jnz     @f
  4491.         mov     ebp, putimage_get16bpp
  4492.         mov     esi, putimage_init16bpp
  4493.         jmp     sys_putimage_bpp
  4494. ;--------------------------------------
  4495. align 4
  4496. @@:
  4497.         cmp     esi, 24
  4498.         jnz     @f
  4499.         mov     ebp, putimage_get24bpp
  4500.         mov     esi, putimage_init24bpp
  4501.         jmp     sys_putimage_bpp
  4502. ;--------------------------------------
  4503. align 4
  4504. @@:
  4505.         cmp     esi, 32
  4506.         jnz     @f
  4507.         mov     ebp, putimage_get32bpp
  4508.         mov     esi, putimage_init32bpp
  4509.         jmp     sys_putimage_bpp
  4510. ;--------------------------------------
  4511. align 4
  4512. @@:
  4513.         pop     ebp esi ebp
  4514.         ret
  4515. ;-----------------------------------------------------------------------------
  4516. align 4
  4517. put_mono_image:
  4518.         push    ebp esi ebp
  4519.         mov     ebp, putimage_get1bpp
  4520.         mov     esi, putimage_init1bpp
  4521.         jmp     sys_putimage_bpp
  4522. ;-----------------------------------------------------------------------------
  4523. align 4
  4524. put_2bit_image:
  4525.         push    ebp esi ebp
  4526.         mov     ebp, putimage_get2bpp
  4527.         mov     esi, putimage_init2bpp
  4528.         jmp     sys_putimage_bpp
  4529. ;-----------------------------------------------------------------------------
  4530. align 4
  4531. put_4bit_image:
  4532.         push    ebp esi ebp
  4533.         mov     ebp, putimage_get4bpp
  4534.         mov     esi, putimage_init4bpp
  4535.         jmp     sys_putimage_bpp
  4536. ;-----------------------------------------------------------------------------
  4537. align 4
  4538. putimage_init24bpp:
  4539.         lea     eax, [eax*3]
  4540. putimage_init8bpp:
  4541. putimage_init9bpp:
  4542.         ret
  4543. ;-----------------------------------------------------------------------------
  4544. align 16
  4545. putimage_get24bpp:
  4546.         movzx   eax, byte [esi+2]
  4547.         shl     eax, 16
  4548.         mov     ax, [esi]
  4549.         add     esi, 3
  4550.         ret     4
  4551. ;-----------------------------------------------------------------------------
  4552. align 16
  4553. putimage_get8bpp:
  4554.         movzx   eax, byte [esi]
  4555.         push    edx
  4556.         mov     edx, [esp+8]
  4557.         mov     eax, [edx+eax*4]
  4558.         pop     edx
  4559.         inc     esi
  4560.         ret     4
  4561. ;-----------------------------------------------------------------------------
  4562. align 16
  4563. putimage_get9bpp:
  4564.         lodsb
  4565.         mov     ah, al
  4566.         shl     eax, 8
  4567.         mov     al, ah
  4568.         ret     4
  4569. ;-----------------------------------------------------------------------------
  4570. align 4
  4571. putimage_init1bpp:
  4572.         add     eax, ecx
  4573.         push    ecx
  4574.         add     eax, 7
  4575.         add     ecx, 7
  4576.         shr     eax, 3
  4577.         shr     ecx, 3
  4578.         sub     eax, ecx
  4579.         pop     ecx
  4580.         ret
  4581. ;-----------------------------------------------------------------------------
  4582. align 16
  4583. putimage_get1bpp:
  4584.         push    edx
  4585.         mov     edx, [esp+8]
  4586.         mov     al, [edx]
  4587.         add     al, al
  4588.         jnz     @f
  4589.         lodsb
  4590.         adc     al, al
  4591. @@:
  4592.         mov     [edx], al
  4593.         sbb     eax, eax
  4594.         and     eax, [edx+8]
  4595.         add     eax, [edx+4]
  4596.         pop     edx
  4597.         ret     4
  4598. ;-----------------------------------------------------------------------------
  4599. align 4
  4600. putimage_init2bpp:
  4601.         add     eax, ecx
  4602.         push    ecx
  4603.         add     ecx, 3
  4604.         add     eax, 3
  4605.         shr     ecx, 2
  4606.         shr     eax, 2
  4607.         sub     eax, ecx
  4608.         pop     ecx
  4609.         ret
  4610. ;-----------------------------------------------------------------------------
  4611. align 16
  4612. putimage_get2bpp:
  4613.         push    edx
  4614.         mov     edx, [esp+8]
  4615.         mov     al, [edx]
  4616.         mov     ah, al
  4617.         shr     al, 6
  4618.         shl     ah, 2
  4619.         jnz     .nonewbyte
  4620.         lodsb
  4621.         mov     ah, al
  4622.         shr     al, 6
  4623.         shl     ah, 2
  4624.         add     ah, 1
  4625. .nonewbyte:
  4626.         mov     [edx], ah
  4627.         mov     edx, [edx+4]
  4628.         movzx   eax, al
  4629.         mov     eax, [edx+eax*4]
  4630.         pop     edx
  4631.         ret     4
  4632. ;-----------------------------------------------------------------------------
  4633. align 4
  4634. putimage_init4bpp:
  4635.         add     eax, ecx
  4636.         push    ecx
  4637.         add     ecx, 1
  4638.         add     eax, 1
  4639.         shr     ecx, 1
  4640.         shr     eax, 1
  4641.         sub     eax, ecx
  4642.         pop     ecx
  4643.         ret
  4644. ;-----------------------------------------------------------------------------
  4645. align 16
  4646. putimage_get4bpp:
  4647.         push    edx
  4648.         mov     edx, [esp+8]
  4649.         add     byte [edx], 80h
  4650.         jc      @f
  4651.         movzx   eax, byte [edx+1]
  4652.         mov     edx, [edx+4]
  4653.         and     eax, 0x0F
  4654.         mov     eax, [edx+eax*4]
  4655.         pop     edx
  4656.         ret     4
  4657. @@:
  4658.         movzx   eax, byte [esi]
  4659.         add     esi, 1
  4660.         mov     [edx+1], al
  4661.         shr     eax, 4
  4662.         mov     edx, [edx+4]
  4663.         mov     eax, [edx+eax*4]
  4664.         pop     edx
  4665.         ret     4
  4666. ;-----------------------------------------------------------------------------
  4667. align 4
  4668. putimage_init32bpp:
  4669.         shl     eax, 2
  4670.         ret
  4671. ;-----------------------------------------------------------------------------
  4672. align 16
  4673. putimage_get32bpp:
  4674.         lodsd
  4675.         ret     4
  4676. ;-----------------------------------------------------------------------------
  4677. align 4
  4678. putimage_init15bpp:
  4679. putimage_init16bpp:
  4680.         add     eax, eax
  4681.         ret
  4682. ;-----------------------------------------------------------------------------
  4683. align 16
  4684. putimage_get15bpp:
  4685. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4686.         push    ecx edx
  4687.         movzx   eax, word [esi]
  4688.         add     esi, 2
  4689.         mov     ecx, eax
  4690.         mov     edx, eax
  4691.         and     eax, 0x1F
  4692.         and     ecx, 0x1F shl 5
  4693.         and     edx, 0x1F shl 10
  4694.         shl     eax, 3
  4695.         shl     ecx, 6
  4696.         shl     edx, 9
  4697.         or      eax, ecx
  4698.         or      eax, edx
  4699.         pop     edx ecx
  4700.         ret     4
  4701. ;-----------------------------------------------------------------------------
  4702. align 16
  4703. putimage_get16bpp:
  4704. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4705.         push    ecx edx
  4706.         movzx   eax, word [esi]
  4707.         add     esi, 2
  4708.         mov     ecx, eax
  4709.         mov     edx, eax
  4710.         and     eax, 0x1F
  4711.         and     ecx, 0x3F shl 5
  4712.         and     edx, 0x1F shl 11
  4713.         shl     eax, 3
  4714.         shl     ecx, 5
  4715.         shl     edx, 8
  4716.         or      eax, ecx
  4717.         or      eax, edx
  4718.         pop     edx ecx
  4719.         ret     4
  4720. ;-----------------------------------------------------------------------------
  4721. ;align 4
  4722. ; eax x beginning
  4723. ; ebx y beginning
  4724. ; ecx x end
  4725.         ; edx y end
  4726. ; edi color
  4727. ;__sys_drawbar:
  4728. ;        mov     esi, [current_slot]
  4729. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4730. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4731. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4732. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4733. ;--------------------------------------
  4734. ;align 4
  4735. ;.forced:
  4736. ;        call    vesa20_drawbar
  4737. ;        call    [draw_pointer]
  4738. ;        ret
  4739. ;-----------------------------------------------------------------------------
  4740. align 4
  4741. kb_write_wait_ack:
  4742.  
  4743.         push    ecx edx
  4744.  
  4745.         mov     dl, al
  4746.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4747. .wait_output_ready:
  4748.         in      al, 0x64
  4749.         test    al, 2
  4750.         jz      @f
  4751.         loop    .wait_output_ready
  4752.         mov     ah, 1
  4753.         jmp     .nothing
  4754. @@:
  4755.         mov     al, dl
  4756.         out     0x60, al
  4757.         mov     ecx, 0xfffff; last 0xffff, new value in view of fast CPU's
  4758. .wait_ack:
  4759.         in      al, 0x64
  4760.         test    al, 1
  4761.         jnz     @f
  4762.         loop    .wait_ack
  4763.         mov     ah, 1
  4764.         jmp     .nothing
  4765. @@:
  4766.         in      al, 0x60
  4767.         xor     ah, ah
  4768.  
  4769. .nothing:
  4770.         pop     edx ecx
  4771.  
  4772.         ret
  4773. ;-----------------------------------------------------------------------------
  4774.  
  4775. setmouse:  ; set mousepicture -pointer
  4776.            ; ps2 mouse enable
  4777.  
  4778. ;        mov     [MOUSE_PICTURE], dword mousepointer
  4779.  
  4780.         cli
  4781.  
  4782.         ret
  4783.  
  4784. if used _rdtsc
  4785. _rdtsc:
  4786.         bt      [cpu_caps], CAPS_TSC
  4787.         jnc     ret_rdtsc
  4788.         rdtsc
  4789.         ret
  4790.    ret_rdtsc:
  4791.         mov     edx, 0xffffffff
  4792.         mov     eax, 0xffffffff
  4793.         ret
  4794. end if
  4795.  
  4796. sys_msg_board_str:
  4797.  
  4798.         pushad
  4799.    @@:
  4800.         cmp     [esi], byte 0
  4801.         je      @f
  4802.         mov     ebx, 1
  4803.         movzx   ecx, byte [esi]
  4804.         call    sys_msg_board
  4805.         inc     esi
  4806.         jmp     @b
  4807.    @@:
  4808.         popad
  4809.         ret
  4810.  
  4811. sys_msg_board_byte:
  4812. ; in: al = byte to display
  4813. ; out: nothing
  4814. ; destroys: nothing
  4815.         pushad
  4816.         mov     ecx, 2
  4817.         shl     eax, 24
  4818.         jmp     @f
  4819.  
  4820. sys_msg_board_word:
  4821. ; in: ax = word to display
  4822. ; out: nothing
  4823. ; destroys: nothing
  4824.         pushad
  4825.         mov     ecx, 4
  4826.         shl     eax, 16
  4827.         jmp     @f
  4828.  
  4829. sys_msg_board_dword:
  4830. ; in: eax = dword to display
  4831. ; out: nothing
  4832. ; destroys: nothing
  4833.         pushad
  4834.         mov     ecx, 8
  4835. @@:
  4836.         push    ecx
  4837.         rol     eax, 4
  4838.         push    eax
  4839.         and     al, 0xF
  4840.         cmp     al, 10
  4841.         sbb     al, 69h
  4842.         das
  4843.         mov     cl, al
  4844.         xor     ebx, ebx
  4845.         inc     ebx
  4846.         call    sys_msg_board
  4847.         pop     eax
  4848.         pop     ecx
  4849.         loop    @b
  4850.         popad
  4851.         ret
  4852.  
  4853. msg_board_data_size = 65536 ; Must be power of two
  4854.  
  4855. uglobal
  4856. msg_board_data  rb  msg_board_data_size
  4857. msg_board_count dd  ?
  4858. endg
  4859.  
  4860. iglobal
  4861. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4862. endg
  4863.  
  4864. sys_msg_board:
  4865. ; ebx=1 -> write, cl = byte to write
  4866. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4867.         push    eax ebx
  4868.         mov     eax, ebx
  4869.         mov     ebx, ecx
  4870.         mov     ecx, [msg_board_count]
  4871.         cmp     eax, 1
  4872.         jne     .read
  4873.  
  4874. if defined debug_com_base
  4875.         push    dx ax
  4876. @@: ; wait for empty transmit register
  4877.         mov     dx, debug_com_base+5
  4878.         in      al, dx
  4879.         test    al, 1 shl 5
  4880.         jz      @r
  4881.         mov     dx, debug_com_base      ; Output the byte
  4882.         mov     al, bl
  4883.         out     dx, al
  4884.         pop     ax dx
  4885. end if
  4886.  
  4887.         mov     [msg_board_data+ecx], bl
  4888.         cmp     byte [debug_direct_print], 1
  4889.         jnz     .end
  4890.         pusha
  4891.         lea     edx, [msg_board_data+ecx]
  4892.         mov     ecx, 0x40FFFFFF
  4893.         mov     ebx, [msg_board_pos]
  4894.         mov     edi, 1
  4895.         mov     esi, 1
  4896.         call    dtext
  4897.         popa
  4898.         add     word [msg_board_pos+2], 6
  4899.         cmp     word [msg_board_pos+2], 105*6
  4900.         jnc     @f
  4901.         cmp     bl, 10
  4902.         jnz     .end
  4903. @@:
  4904.         mov     word [msg_board_pos+2], 42*6
  4905.         add     word [msg_board_pos], 10
  4906.         mov     eax, [_display.height]
  4907.         sub     eax, 10
  4908.         cmp     ax, word [msg_board_pos]
  4909.         jnc     @f
  4910.         mov     word [msg_board_pos], 10
  4911. @@:
  4912.         pusha
  4913.         mov     eax, [msg_board_pos]
  4914.         movzx   ebx, ax
  4915.         shr     eax, 16
  4916.         mov     edx, 105*6
  4917.         xor     ecx, ecx
  4918.         mov     edi, 1
  4919.         mov     esi, 9
  4920. @@:
  4921.         call    hline
  4922.         inc     ebx
  4923.         dec     esi
  4924.         jnz     @b
  4925.         popa
  4926. .end:
  4927.         inc     ecx
  4928.         and     ecx, msg_board_data_size - 1
  4929.         mov     [msg_board_count], ecx
  4930. .ret:
  4931.         pop     ebx eax
  4932.         ret
  4933.  
  4934. @@:
  4935.         mov     [esp+32], ecx
  4936.         mov     [esp+20], ecx
  4937.         jmp     .ret
  4938.  
  4939. .read:
  4940.         cmp     eax, 2
  4941.         jne     .ret
  4942.         test    ecx, ecx
  4943.         jz      @b
  4944.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4945.         mov     eax, msg_board_data+1
  4946.         mov     ebx, msg_board_data
  4947.         movzx   edx, byte [ebx]
  4948.         call    memmove
  4949.         dec     [msg_board_count]
  4950.         mov     [esp + 32], edx ;eax
  4951.         mov     [esp + 20], dword 1
  4952.         ret
  4953.  
  4954. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4955. ;; 66 sys function.                                                ;;
  4956. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4957. ;; out eax                                                         ;;
  4958. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4959. iglobal
  4960. align 4
  4961. f66call:
  4962.            dd sys_process_def.1   ; 1 = set keyboard mode
  4963.            dd sys_process_def.2   ; 2 = get keyboard mode
  4964.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4965.            dd sys_process_def.4   ; 4 = set system-wide hotkey
  4966.            dd sys_process_def.5   ; 5 = delete installed hotkey
  4967.            dd sys_process_def.6   ; 6 = disable input, work only hotkeys
  4968.            dd sys_process_def.7   ; 7 = enable input, opposition to f.66.6
  4969. endg
  4970. ;-----------------------------------------------------------------------------
  4971. align 4
  4972. sys_process_def:
  4973.         dec     ebx
  4974.         cmp     ebx, 7
  4975.         jae     .not_support    ;if >=8 then or eax,-1
  4976.  
  4977.         mov     edi, [CURRENT_TASK]
  4978.         jmp     dword [f66call+ebx*4]
  4979.  
  4980. .not_support:
  4981.         or      eax, -1
  4982.         ret
  4983. ;-----------------------------------------------------------------------------
  4984. align 4
  4985. .1:
  4986.         shl     edi, 8
  4987.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4988.  
  4989.         ret
  4990. ;-----------------------------------------------------------------------------
  4991. align 4
  4992. .2:                             ; 2 = get keyboard mode
  4993.         shl     edi, 8
  4994.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4995.         mov     [esp+32], eax
  4996.         ret
  4997. ;-----------------------------------------------------------------------------
  4998. align 4
  4999. .3:                             ;3 = get keyboard ctrl, alt, shift
  5000.         mov     eax, [kb_state]
  5001.         mov     [esp+32], eax
  5002.         ret
  5003. ;-----------------------------------------------------------------------------
  5004. align 4
  5005. .4:
  5006.         mov     eax, hotkey_list
  5007. @@:
  5008.         cmp     dword [eax+8], 0
  5009.         jz      .found_free
  5010.         add     eax, 16
  5011.         cmp     eax, hotkey_list+16*256
  5012.         jb      @b
  5013.         mov     dword [esp+32], 1
  5014.         ret
  5015. .found_free:
  5016.         mov     [eax+8], edi
  5017.         mov     [eax+4], edx
  5018.         movzx   ecx, cl
  5019.         lea     ecx, [hotkey_scancodes+ecx*4]
  5020.         mov     edx, [ecx]
  5021.         mov     [eax], edx
  5022.         mov     [ecx], eax
  5023.         mov     [eax+12], ecx
  5024.         test    edx, edx
  5025.         jz      @f
  5026.         mov     [edx+12], eax
  5027. @@:
  5028.         and     dword [esp+32], 0
  5029.         ret
  5030. ;-----------------------------------------------------------------------------
  5031. align 4
  5032. .5:
  5033.         movzx   ebx, cl
  5034.         lea     ebx, [hotkey_scancodes+ebx*4]
  5035.         mov     eax, [ebx]
  5036. .scan:
  5037.         test    eax, eax
  5038.         jz      .notfound
  5039.         cmp     [eax+8], edi
  5040.         jnz     .next
  5041.         cmp     [eax+4], edx
  5042.         jz      .found
  5043. .next:
  5044.         mov     eax, [eax]
  5045.         jmp     .scan
  5046. .notfound:
  5047.         mov     dword [esp+32], 1
  5048.         ret
  5049. .found:
  5050.         mov     ecx, [eax]
  5051.         jecxz   @f
  5052.         mov     edx, [eax+12]
  5053.         mov     [ecx+12], edx
  5054. @@:
  5055.         mov     ecx, [eax+12]
  5056.         mov     edx, [eax]
  5057.         mov     [ecx], edx
  5058.         xor     edx, edx
  5059.         mov     [eax+4], edx
  5060.         mov     [eax+8], edx
  5061.         mov     [eax+12], edx
  5062.         mov     [eax], edx
  5063.         mov     [esp+32], edx
  5064.         ret
  5065. ;-----------------------------------------------------------------------------
  5066. align 4
  5067. .6:
  5068.         pushfd
  5069.         cli
  5070.         mov     eax, [PID_lock_input]
  5071.         test    eax, eax
  5072.         jnz     @f
  5073. ; get current PID
  5074.         mov     eax, [CURRENT_TASK]
  5075.         shl     eax, 5
  5076.         mov     eax, [eax+CURRENT_TASK+TASKDATA.pid]
  5077. ; set current PID for lock input
  5078.         mov     [PID_lock_input], eax
  5079. @@:
  5080.         popfd
  5081.         ret
  5082. ;-----------------------------------------------------------------------------
  5083. align 4
  5084. .7:
  5085.         mov     eax, [PID_lock_input]
  5086.         test    eax, eax
  5087.         jz      @f
  5088. ; get current PID
  5089.         mov     ebx, [CURRENT_TASK]
  5090.         shl     ebx, 5
  5091.         mov     ebx, [ebx+CURRENT_TASK+TASKDATA.pid]
  5092. ; compare current lock input with current PID
  5093.         cmp     ebx, eax
  5094.         jne     @f
  5095.  
  5096.         xor     eax, eax
  5097.         mov     [PID_lock_input], eax
  5098. @@:
  5099.         ret
  5100. ;-----------------------------------------------------------------------------
  5101. uglobal
  5102.   PID_lock_input dd 0x0
  5103. endg
  5104. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5105. ;; 61 sys function.                                                ;;
  5106. ;; in eax=61,ebx in [1..3]                                         ;;
  5107. ;; out eax                                                         ;;
  5108. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5109. iglobal
  5110. align 4
  5111. f61call:
  5112.            dd sys_gs.1   ; resolution
  5113.            dd sys_gs.2   ; bits per pixel
  5114.            dd sys_gs.3   ; bytes per scanline
  5115. endg
  5116.  
  5117.  
  5118. align 4
  5119.  
  5120. sys_gs:                         ; direct screen access
  5121.         dec     ebx
  5122.         cmp     ebx, 2
  5123.         ja      .not_support
  5124.         jmp     dword [f61call+ebx*4]
  5125. .not_support:
  5126.         or      [esp+32], dword -1
  5127.         ret
  5128.  
  5129.  
  5130. .1:                             ; resolution
  5131.         mov     eax, [_display.width]
  5132.         shl     eax, 16
  5133.         mov     ax, word [_display.height]
  5134.         mov     [esp+32], eax
  5135.         ret
  5136. .2:                             ; bits per pixel
  5137.         mov     eax, [_display.bits_per_pixel]
  5138.         mov     [esp+32], eax
  5139.         ret
  5140. .3:                             ; bytes per scanline
  5141.         mov     eax, [_display.lfb_pitch]
  5142.         mov     [esp+32], eax
  5143.         ret
  5144.  
  5145. align 4  ;  system functions
  5146.  
  5147. syscall_setpixel:                       ; SetPixel
  5148.  
  5149.         mov     eax, ebx
  5150.         mov     ebx, ecx
  5151.         mov     ecx, edx
  5152.         mov     edx, [TASK_BASE]
  5153.         add     eax, [edx-twdw+WDATA.box.left]
  5154.         add     ebx, [edx-twdw+WDATA.box.top]
  5155.         mov     edi, [current_slot]
  5156.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  5157.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  5158.         xor     edi, edi ; no force
  5159.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
  5160. ;        jmp     [putpixel]
  5161.         jmp     __sys_putpixel
  5162.  
  5163. align 4
  5164.  
  5165. syscall_writetext:                      ; WriteText
  5166.  
  5167.         mov     eax, [TASK_BASE]
  5168.         mov     ebp, [eax-twdw+WDATA.box.left]
  5169.         push    esi
  5170.         mov     esi, [current_slot]
  5171.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5172.         shl     ebp, 16
  5173.         add     ebp, [eax-twdw+WDATA.box.top]
  5174.         add     bp, word[esi+APPDATA.wnd_clientbox.top]
  5175.         pop     esi
  5176.         test    ecx, 0x08000000  ; redirect the output to the user area
  5177.         jnz     @f
  5178.         add     ebx, ebp
  5179. align 4
  5180. @@:
  5181.         mov     eax, edi
  5182.         test    ecx, 0x08000000  ; redirect the output to the user area
  5183.         jnz     dtext
  5184.         xor     edi, edi
  5185.         jmp     dtext
  5186.  
  5187. align 4
  5188.  
  5189. syscall_drawrect:                       ; DrawRect
  5190.  
  5191.         mov     edi, edx ; color + gradient
  5192.         and     edi, 0x80FFFFFF
  5193.         test    bx, bx  ; x.size
  5194.         je      .drectr
  5195.         test    cx, cx ; y.size
  5196.         je      .drectr
  5197.  
  5198.         mov     eax, ebx ; bad idea
  5199.         mov     ebx, ecx
  5200.  
  5201.         movzx   ecx, ax ; ecx - x.size
  5202.         shr     eax, 16 ; eax - x.coord
  5203.         movzx   edx, bx ; edx - y.size
  5204.         shr     ebx, 16 ; ebx - y.coord
  5205.         mov     esi, [current_slot]
  5206.  
  5207.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  5208.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  5209.         add     ecx, eax
  5210.         add     edx, ebx
  5211. ;        jmp     [drawbar]
  5212.         jmp     vesa20_drawbar
  5213. .drectr:
  5214.         ret
  5215.  
  5216. align 4
  5217. syscall_getscreensize:                  ; GetScreenSize
  5218.         mov     ax, word [_display.width]
  5219.         dec     ax
  5220.         shl     eax, 16
  5221.         mov     ax, word [_display.height]
  5222.         dec     ax
  5223.         mov     [esp + 32], eax
  5224.         ret
  5225. ;-----------------------------------------------------------------------------
  5226. align 4
  5227. syscall_cdaudio:
  5228. ; ECX - position of CD/DVD-drive
  5229. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  5230. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  5231. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  5232.         cmp     ecx, 11
  5233.         ja      .exit
  5234.  
  5235.         mov     eax, ecx
  5236.         shr     eax, 2
  5237.         lea     eax, [eax*5]
  5238.         mov     al, [eax+DRIVE_DATA+1]
  5239.  
  5240.         push    ecx ebx
  5241.         mov     ebx, ecx
  5242.         and     ebx, 11b
  5243.         shl     ebx, 1
  5244.         mov     cl, 6
  5245.         sub     cl, bl
  5246.         shr     al, cl
  5247.         test    al, 2 ; it's not an ATAPI device
  5248.         pop     ebx ecx
  5249.  
  5250.         jz      .exit
  5251.  
  5252.         cmp     ebx, 4
  5253.         je      .eject
  5254.  
  5255.         cmp     ebx, 5
  5256.         je      .load
  5257. ;--------------------------------------
  5258. .exit:
  5259.         ret
  5260. ;--------------------------------------
  5261. .load:
  5262.         call    .reserve
  5263.         call    LoadMedium
  5264.         jmp     .free
  5265. ;--------------------------------------
  5266. .eject:
  5267.         call    .reserve
  5268.         call    clear_CD_cache
  5269.         call    allow_medium_removal
  5270.         call    EjectMedium
  5271.         jmp     .free
  5272. ;--------------------------------------
  5273. .reserve:
  5274.         call    reserve_cd
  5275.  
  5276.         mov     ebx, ecx
  5277.         inc     ebx
  5278.         mov     [cdpos], ebx
  5279.  
  5280.         mov     eax, ecx
  5281.         shr     eax, 1
  5282.         and     eax, 1
  5283.         inc     eax
  5284.         mov     [ChannelNumber], al
  5285.         mov     eax, ecx
  5286.         and     eax, 1
  5287.         mov     [DiskNumber], al
  5288.         call    reserve_cd_channel
  5289.         ret
  5290. ;--------------------------------------
  5291. .free:
  5292.         call    free_cd_channel
  5293.         and     [cd_status], 0
  5294.         ret
  5295. ;-----------------------------------------------------------------------------
  5296. align 4
  5297. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  5298.         cmp     ebx, [_display.width]
  5299.         jb      @f
  5300.         cmp     ecx, [_display.height]
  5301.         jb      @f
  5302.         xor     eax, eax
  5303.         jmp     .store
  5304. ;--------------------------------------
  5305. align 4
  5306. @@:
  5307.         mov     eax, [d_width_calc_area + ecx*4]
  5308.         add     eax, [_display.win_map]
  5309.         movzx   eax, byte[eax+ebx]        ; get value for current point
  5310. ;--------------------------------------
  5311. align 4
  5312. .store:
  5313.         mov     [esp + 32], eax
  5314.         ret
  5315. ;-----------------------------------------------------------------------------
  5316. align 4
  5317. syscall_getpixel:                       ; GetPixel
  5318.         mov     ecx, [_display.width]
  5319.         xor     edx, edx
  5320.         mov     eax, ebx
  5321.         div     ecx
  5322.         mov     ebx, edx
  5323.         xchg    eax, ebx
  5324.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5325.         call    dword [GETPIXEL]; eax - x, ebx - y
  5326.         mov     [esp + 32], ecx
  5327.         ret
  5328. ;-----------------------------------------------------------------------------
  5329. align 4
  5330. syscall_getarea:
  5331. ;eax = 36
  5332. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5333. ;ecx = [size x]*65536 + [size y]
  5334. ;edx = [start x]*65536 + [start y]
  5335.         pushad
  5336.         mov     edi, ebx
  5337.         mov     eax, edx
  5338.         shr     eax, 16
  5339.         mov     ebx, edx
  5340.         and     ebx, 0xffff
  5341.         dec     eax
  5342.         dec     ebx
  5343.      ; eax - x, ebx - y
  5344.         mov     edx, ecx
  5345.  
  5346.         shr     ecx, 16
  5347.         and     edx, 0xffff
  5348.         mov     esi, ecx
  5349.      ; ecx - size x, edx - size y
  5350.  
  5351.         mov     ebp, edx
  5352.         dec     ebp
  5353.         lea     ebp, [ebp*3]
  5354.  
  5355.         imul    ebp, esi
  5356.  
  5357.         mov     esi, ecx
  5358.         dec     esi
  5359.         lea     esi, [esi*3]
  5360.  
  5361.         add     ebp, esi
  5362.         add     ebp, edi
  5363.  
  5364.         add     ebx, edx
  5365. ;--------------------------------------
  5366. align 4
  5367. .start_y:
  5368.         push    ecx edx
  5369. ;--------------------------------------
  5370. align 4
  5371. .start_x:
  5372.         push    eax ebx ecx
  5373.         add     eax, ecx
  5374.  
  5375.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5376.         call    dword [GETPIXEL]; eax - x, ebx - y
  5377.  
  5378.         mov     [ebp], cx
  5379.         shr     ecx, 16
  5380.         mov     [ebp+2], cl
  5381.  
  5382.         pop     ecx ebx eax
  5383.         sub     ebp, 3
  5384.         dec     ecx
  5385.         jnz     .start_x
  5386.         pop     edx ecx
  5387.         dec     ebx
  5388.         dec     edx
  5389.         jnz     .start_y
  5390.         popad
  5391.         ret
  5392. ;-----------------------------------------------------------------------------
  5393. align 4
  5394. syscall_putarea_backgr:
  5395. ;eax = 25
  5396. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5397. ;ecx = [size x]*65536 + [size y]
  5398. ;edx = [start x]*65536 + [start y]
  5399.         pushad
  5400.         mov     edi, ebx
  5401.         mov     eax, edx
  5402.         shr     eax, 16
  5403.         mov     ebx, edx
  5404.         and     ebx, 0xffff
  5405.         dec     eax
  5406.         dec     ebx
  5407. ; eax - x, ebx - y
  5408.         mov     edx, ecx
  5409.         shr     ecx, 16
  5410.         and     edx, 0xffff
  5411.         mov     esi, ecx
  5412. ; ecx - size x, edx - size y
  5413.         mov     ebp, edx
  5414.         dec     ebp
  5415.         shl     ebp, 2
  5416.  
  5417.         imul    ebp, esi
  5418.  
  5419.         mov     esi, ecx
  5420.         dec     esi
  5421.         shl     esi, 2
  5422.  
  5423.         add     ebp, esi
  5424.         add     ebp, edi
  5425.  
  5426.         add     ebx, edx
  5427. ;--------------------------------------
  5428. align 4
  5429. .start_y:
  5430.         push    ecx edx
  5431. ;--------------------------------------
  5432. align 4
  5433. .start_x:
  5434.         push    eax ecx
  5435.         add     eax, ecx
  5436.  
  5437.         mov     ecx, [ebp]
  5438.         rol     ecx, 8
  5439.         test    cl, cl        ; transparensy = 0
  5440.         jz      .no_put
  5441.  
  5442.         xor     cl, cl
  5443.         ror     ecx, 8
  5444.  
  5445.         pushad
  5446.         mov     edx, [d_width_calc_area + ebx*4]
  5447.         add     edx, [_display.win_map]
  5448.         movzx   edx, byte [eax+edx]
  5449.         cmp     dl, byte 1
  5450.         jne     @f
  5451.  
  5452.         call    dword [PUTPIXEL]; eax - x, ebx - y
  5453. ;--------------------------------------
  5454. align 4
  5455. @@:
  5456.         popad
  5457. ;--------------------------------------
  5458. align 4
  5459. .no_put:
  5460.         pop     ecx eax
  5461.  
  5462.         sub     ebp, 4
  5463.         dec     ecx
  5464.         jnz     .start_x
  5465.  
  5466.         pop     edx ecx
  5467.         dec     ebx
  5468.         dec     edx
  5469.         jnz     .start_y
  5470.  
  5471.         popad
  5472.         ret
  5473. ;-----------------------------------------------------------------------------
  5474. align 4
  5475. syscall_drawline:                       ; DrawLine
  5476.  
  5477.         mov     edi, [TASK_BASE]
  5478.         movzx   eax, word[edi-twdw+WDATA.box.left]
  5479.         mov     ebp, eax
  5480.         mov     esi, [current_slot]
  5481.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5482.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  5483.         add     ebp, ebx
  5484.         shl     eax, 16
  5485.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  5486.         add     eax, ebp
  5487.         mov     ebp, ebx
  5488.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  5489.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  5490.         add     ebp, ecx
  5491.         shl     ebx, 16
  5492.         xor     edi, edi
  5493.         add     ebx, ebp
  5494.         mov     ecx, edx
  5495. ;        jmp     [draw_line]
  5496.         jmp     __sys_draw_line
  5497.  
  5498.  
  5499. align 4
  5500. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5501.  
  5502.         call    r_f_port_area
  5503.         mov     [esp+32], eax
  5504.         ret
  5505.  
  5506. align 4
  5507. syscall_threads:                        ; CreateThreads
  5508. ;
  5509. ;   ecx=thread entry point
  5510. ;   edx=thread stack pointer
  5511. ;
  5512. ; on return : eax = pid
  5513.  
  5514.         xor     ebx, ebx
  5515.         call    new_sys_threads
  5516.  
  5517.         mov     [esp+32], eax
  5518.         ret
  5519.  
  5520. align 4
  5521.  
  5522. paleholder:
  5523.         ret
  5524. ;------------------------------------------------------------------------------
  5525. align 4
  5526. calculate_fast_getting_offset_for_WinMapAddress:
  5527. ; calculate data area for fast getting offset to _WinMapAddress
  5528.         xor     eax, eax
  5529.         mov     ecx, [_display.height]
  5530.         mov     edi, d_width_calc_area
  5531.         cld
  5532. @@:
  5533.         stosd
  5534.         add     eax, [_display.width]
  5535.         dec     ecx
  5536.         jnz     @r
  5537.         ret
  5538. ;------------------------------------------------------------------------------
  5539. align 4
  5540. calculate_fast_getting_offset_for_LFB:
  5541. ; calculate data area for fast getting offset to LFB
  5542.         xor     eax, eax
  5543.         mov     ecx, [_display.height]
  5544.         mov     edi, BPSLine_calc_area
  5545.         cld
  5546. @@:
  5547.         stosd
  5548.         add     eax, [_display.lfb_pitch]
  5549.         dec     ecx
  5550.         jnz     @r
  5551.         ret
  5552. ;------------------------------------------------------------------------------
  5553. align 4
  5554. set_screen:
  5555. ; in:
  5556. ; eax - new Screen_Max_X
  5557. ; ecx - new BytesPerScanLine
  5558. ; edx - new Screen_Max_Y
  5559.  
  5560.         pushfd
  5561.         cli
  5562.  
  5563.         mov     [_display.lfb_pitch], ecx
  5564.  
  5565.         mov     [screen_workarea.right], eax
  5566.         mov     [screen_workarea.bottom], edx
  5567.  
  5568.         push    ebx
  5569.         push    esi
  5570.         push    edi
  5571.  
  5572.         pushad
  5573.  
  5574.         cmp     [do_not_touch_winmap], 1
  5575.         je      @f
  5576.  
  5577.         stdcall kernel_free, [_display.win_map]
  5578.  
  5579.         mov     eax, [_display.width]
  5580.         mul     [_display.height]
  5581.         mov     [_display.win_map_size], eax
  5582.  
  5583.         stdcall kernel_alloc, eax
  5584.         mov     [_display.win_map], eax
  5585.         test    eax, eax
  5586.         jz      .epic_fail
  5587. ; store for f.18.24
  5588.         mov     eax, [_display.width]
  5589.         mov     [display_width_standard], eax
  5590.  
  5591.         mov     eax, [_display.height]
  5592.         mov     [display_height_standard], eax
  5593. @@:
  5594.         call    calculate_fast_getting_offset_for_WinMapAddress
  5595. ; for Qemu or non standart video cards
  5596. ; Unfortunately [BytesPerScanLine] does not always
  5597. ;                             equal to [_display.width] * [ScreenBPP] / 8
  5598.         call    calculate_fast_getting_offset_for_LFB
  5599.         popad
  5600.  
  5601.         call    repos_windows
  5602.         xor     eax, eax
  5603.         xor     ebx, ebx
  5604.         mov     ecx, [_display.width]
  5605.         mov     edx, [_display.height]
  5606.         dec     ecx
  5607.         dec     edx
  5608.         call    calculatescreen
  5609.         pop     edi
  5610.         pop     esi
  5611.         pop     ebx
  5612.  
  5613.         popfd
  5614.         ret
  5615.  
  5616. .epic_fail:
  5617.         hlt                     ; Houston, we've had a problem
  5618.  
  5619. ; --------------- APM ---------------------
  5620. uglobal
  5621. apm_entry       dp      0
  5622. apm_vf          dd      0
  5623. endg
  5624.  
  5625. align 4
  5626. sys_apm:
  5627.         xor     eax, eax
  5628.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  5629.         jne     @f
  5630.         inc     eax
  5631.         or      dword [esp + 44], eax   ; error
  5632.         add     eax, 7
  5633.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  5634.         ret
  5635.  
  5636. @@:
  5637. ;       xchg    eax, ecx
  5638. ;       xchg    ebx, ecx
  5639.  
  5640.         cmp     dx, 3
  5641.         ja      @f
  5642.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  5643.         mov     eax, [apm_vf]
  5644.         mov     [esp + 32], eax
  5645.         shr     eax, 16
  5646.         mov     [esp + 28], eax
  5647.         ret
  5648.  
  5649. @@:
  5650.  
  5651.         mov     esi, [master_tab+(OS_BASE shr 20)]
  5652.         xchg    [master_tab], esi
  5653.         push    esi
  5654.         mov     edi, cr3
  5655.         mov     cr3, edi                ;flush TLB
  5656.  
  5657.         call    pword [apm_entry]       ;call APM BIOS
  5658.  
  5659.         xchg    eax, [esp]
  5660.         mov     [master_tab], eax
  5661.         mov     eax, cr3
  5662.         mov     cr3, eax
  5663.         pop     eax
  5664.  
  5665.         mov     [esp + 4 ], edi
  5666.         mov     [esp + 8], esi
  5667.         mov     [esp + 20], ebx
  5668.         mov     [esp + 24], edx
  5669.         mov     [esp + 28], ecx
  5670.         mov     [esp + 32], eax
  5671.         setc    al
  5672.         and     [esp + 44], byte 0xfe
  5673.         or      [esp + 44], al
  5674.         ret
  5675. ; -----------------------------------------
  5676.  
  5677. align 4
  5678. undefined_syscall:                      ; Undefined system call
  5679.         mov     [esp + 32], dword -1
  5680.         ret
  5681.  
  5682. align 4
  5683. ; check if given memory region lays in lower 2gb (userspace memory) or not
  5684. is_region_userspace:
  5685. ; in:  eax = base
  5686. ;      ebx = len
  5687. ; out: eax = 1 if region in userspace memory, 0 if not
  5688.         push    esi edi ecx
  5689.  
  5690.         cmp     eax, OS_BASE
  5691.         ja      @f
  5692.  
  5693.         add     eax, ebx
  5694.         cmp     eax, OS_BASE
  5695.         ja      @f
  5696.  
  5697.         mov     eax, 1
  5698.         jmp     .ret
  5699. @@:
  5700.         xor     eax, eax
  5701. .ret:
  5702.         pop     ecx edi esi
  5703.         ret
  5704.  
  5705. if ~ lang eq sp
  5706. diff16 "end of .text segment",0,$
  5707. end if
  5708.  
  5709. include "data32.inc"
  5710.  
  5711. __REV__ = __REV
  5712.  
  5713. if ~ lang eq sp
  5714. diff16 "end of kernel code",0,$
  5715. end if
  5716.