Subversion Repositories Kolibri OS

Rev

Rev 8176 | Rev 8246 | 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: 8216 $
  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.  
  3163.         cmp     ecx, -1 ; who am I ?
  3164.         jne     .no_who_am_i
  3165.         mov     ecx, [CURRENT_TASK]
  3166.   .no_who_am_i:
  3167.         cmp     ecx, max_processes
  3168.         ja      .nofillbuf
  3169.  
  3170. ; +4: word: position of the window of thread in the window stack
  3171.         mov     ax, [WIN_STACK + ecx * 2]
  3172.         mov     [ebx+4], ax
  3173. ; +6: word: number of the thread slot, which window has in the window stack
  3174. ;           position ecx (has no relation to the specific thread)
  3175.         mov     ax, [WIN_POS + ecx * 2]
  3176.         mov     [ebx+6], ax
  3177.  
  3178.         shl     ecx, 5
  3179.  
  3180. ; +0: dword: memory usage
  3181.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  3182.         mov     [ebx], eax
  3183. ; +10: 11 bytes: name of the process
  3184.         push    ecx
  3185.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  3186.         add     ebx, 10
  3187.         mov     ecx, 11
  3188.         call    memmove
  3189.         pop     ecx
  3190.  
  3191. ; +22: address of the process in memory
  3192. ; +26: size of used memory - 1
  3193.         push    edi
  3194.         lea     edi, [ebx+12]
  3195.         xor     eax, eax
  3196.         mov     edx, 0x100000*16
  3197.         cmp     ecx, 1 shl 5
  3198.         je      .os_mem
  3199.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.process]
  3200.         mov     edx, [edx+PROC.mem_used]
  3201.         mov     eax, std_application_base_address
  3202. .os_mem:
  3203.         stosd
  3204.         lea     eax, [edx-1]
  3205.         stosd
  3206.  
  3207. ; +30: PID/TID
  3208.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  3209.         stosd
  3210.  
  3211.     ; window position and size
  3212.         push    esi
  3213.         lea     esi, [ecx + window_data + WDATA.box]
  3214.         movsd
  3215.         movsd
  3216.         movsd
  3217.         movsd
  3218.  
  3219.     ; Process state (+50)
  3220.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  3221.         stosd
  3222.  
  3223.     ; Window client area box
  3224.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  3225.         movsd
  3226.         movsd
  3227.         movsd
  3228.         movsd
  3229.  
  3230.     ; Window state
  3231.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  3232.         stosb
  3233.  
  3234.     ; Event mask (+71)
  3235.         mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
  3236.         stosd
  3237.  
  3238.     ; Keyboard mode (+75)
  3239.         mov     al, byte [ecx*8 + SLOT_BASE + APPDATA.keyboard_mode]
  3240.         stosb
  3241.  
  3242.         pop     esi
  3243.         pop     edi
  3244.  
  3245. .nofillbuf:
  3246.     ; return number of processes
  3247.  
  3248.         mov     eax, [TASK_COUNT]
  3249.         mov     [esp+32], eax
  3250.         ret
  3251.  
  3252. align 4
  3253. sys_clock:
  3254.         cli
  3255.   ; Mikhail Lisovin  xx Jan 2005
  3256.   @@:
  3257.         mov     al, 10
  3258.         out     0x70, al
  3259.         in      al, 0x71
  3260.         test    al, al
  3261.         jns     @f
  3262.         mov     esi, 1
  3263.         call    delay_ms
  3264.         jmp     @b
  3265.   @@:
  3266.   ; end Lisovin's fix
  3267.  
  3268.         xor     al, al        ; seconds
  3269.         out     0x70, al
  3270.         in      al, 0x71
  3271.         movzx   ecx, al
  3272.         mov     al, 02        ; minutes
  3273.         shl     ecx, 16
  3274.         out     0x70, al
  3275.         in      al, 0x71
  3276.         movzx   edx, al
  3277.         mov     al, 04        ; hours
  3278.         shl     edx, 8
  3279.         out     0x70, al
  3280.         in      al, 0x71
  3281.         add     ecx, edx
  3282.         movzx   edx, al
  3283.         add     ecx, edx
  3284.         sti
  3285.         mov     [esp + 32], ecx
  3286.         ret
  3287.  
  3288.  
  3289. align 4
  3290.  
  3291. sys_date:
  3292.  
  3293.         cli
  3294.   @@:
  3295.         mov     al, 10
  3296.         out     0x70, al
  3297.         in      al, 0x71
  3298.         test    al, al
  3299.         jns     @f
  3300.         mov     esi, 1
  3301.         call    delay_ms
  3302.         jmp     @b
  3303.   @@:
  3304.  
  3305.         mov     ch, 0
  3306.         mov     al, 7           ; date
  3307.         out     0x70, al
  3308.         in      al, 0x71
  3309.         mov     cl, al
  3310.         mov     al, 8           ; month
  3311.         shl     ecx, 16
  3312.         out     0x70, al
  3313.         in      al, 0x71
  3314.         mov     ch, al
  3315.         mov     al, 9           ; year
  3316.         out     0x70, al
  3317.         in      al, 0x71
  3318.         mov     cl, al
  3319.         sti
  3320.         mov     [esp+32], ecx
  3321.         ret
  3322.  
  3323.  
  3324. ; redraw status
  3325.  
  3326. sys_redrawstat:
  3327.         cmp     ebx, 1
  3328.         jne     no_widgets_away
  3329.         ; buttons away
  3330.         mov     ecx, [CURRENT_TASK]
  3331.   sys_newba2:
  3332.         mov     edi, [BTN_ADDR]
  3333.         cmp     [edi], dword 0  ; empty button list ?
  3334.         je      end_of_buttons_away
  3335.         movzx   ebx, word [edi]
  3336.         inc     ebx
  3337.         mov     eax, edi
  3338.   sys_newba:
  3339.         dec     ebx
  3340.         jz      end_of_buttons_away
  3341.  
  3342.         add     eax, 0x10
  3343.         cmp     cx, [eax]
  3344.         jnz     sys_newba
  3345.  
  3346.         push    eax ebx ecx
  3347.         mov     ecx, ebx
  3348.         inc     ecx
  3349.         shl     ecx, 4
  3350.         mov     ebx, eax
  3351.         add     eax, 0x10
  3352.         call    memmove
  3353.         dec     dword [edi]
  3354.         pop     ecx ebx eax
  3355.  
  3356.         jmp     sys_newba2
  3357.  
  3358.   end_of_buttons_away:
  3359.  
  3360.         ret
  3361.  
  3362.   no_widgets_away:
  3363.  
  3364.         cmp     ebx, 2
  3365.         jnz     srl1
  3366.  
  3367.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  3368.         add     edx, draw_data - CURRENT_TASK
  3369.         mov     [edx + RECT.left], 0
  3370.         mov     [edx + RECT.top], 0
  3371.         mov     eax, [_display.width]
  3372.         dec     eax
  3373.         mov     [edx + RECT.right], eax
  3374.         mov     eax, [_display.height]
  3375.         dec     eax
  3376.         mov     [edx + RECT.bottom], eax
  3377.  
  3378.   srl1:
  3379.         ret
  3380.  
  3381. ;ok - 100% work
  3382. ;nt - not tested
  3383. ;---------------------------------------------------------------------------------------------
  3384. ;eax
  3385. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3386. ;1 - change task. Ret nothing. Block. ok.
  3387. ;2 - performance control
  3388. ; ebx
  3389. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3390. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3391. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3392. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3393. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3394. ;eax
  3395. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3396. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3397. ;---------------------------------------------------------------------------------------------
  3398. iglobal
  3399. align 4
  3400. sheduler:
  3401.         dd      sys_sheduler.00
  3402.         dd      change_task
  3403.         dd      sys_sheduler.02
  3404.         dd      sys_sheduler.03
  3405.         dd      sys_sheduler.04
  3406. endg
  3407. sys_sheduler:
  3408. ;rewritten by <Lrz>  29.12.2009
  3409.         jmp     dword [sheduler+ebx*4]
  3410. ;.shed_counter:
  3411. .00:
  3412.         mov     eax, [context_counter]
  3413.         mov     [esp+32], eax
  3414.         ret
  3415.  
  3416. .02:
  3417. ;.perf_control:
  3418.         inc     ebx                     ;before ebx=2, ebx=3
  3419.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  3420.         jz      cache_disable
  3421.  
  3422.         dec     ebx                     ;ebx=2
  3423.         cmp     ebx, ecx                ;
  3424.         jz      cache_enable            ;if ecx=2 and ebx=2
  3425.  
  3426.         dec     ebx                     ;ebx=1
  3427.         cmp     ebx, ecx
  3428.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  3429.  
  3430.         dec     ebx
  3431.         test    ebx, ecx                ;ebx=0 and ecx=0
  3432.         jz      modify_pce              ;if ecx=0
  3433.  
  3434.         ret
  3435.  
  3436. .03:
  3437. ;.rdmsr_instr:
  3438. ;now counter in ecx
  3439. ;(edx:eax) esi:edi => edx:esi
  3440.         mov     eax, esi
  3441.         mov     ecx, edx
  3442.         rdmsr
  3443.         mov     [esp+32], eax
  3444.         mov     [esp+20], edx           ;ret in ebx?
  3445.         ret
  3446.  
  3447. .04:
  3448. ;.wrmsr_instr:
  3449. ;now counter in ecx
  3450. ;(edx:eax) esi:edi => edx:esi
  3451.         ; Fast Call MSR can't be destroy
  3452.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  3453.         ; включаются/выключаются расширенные возможности
  3454.         cmp     edx, MSR_SYSENTER_CS
  3455.         je      @f
  3456.         cmp     edx, MSR_SYSENTER_ESP
  3457.         je      @f
  3458.         cmp     edx, MSR_SYSENTER_EIP
  3459.         je      @f
  3460.         cmp     edx, MSR_AMD_STAR
  3461.         je      @f
  3462.  
  3463.         mov     eax, esi
  3464.         mov     ecx, edx
  3465.         wrmsr
  3466.         ; mov   [esp + 32], eax
  3467.         ; mov   [esp + 20], edx ;ret in ebx?
  3468. @@:
  3469.         ret
  3470.  
  3471. cache_disable:
  3472.         mov     eax, cr0
  3473.         or      eax, 01100000000000000000000000000000b
  3474.         mov     cr0, eax
  3475.         wbinvd  ;set MESI
  3476.         ret
  3477.  
  3478. cache_enable:
  3479.         mov     eax, cr0
  3480.         and     eax, 10011111111111111111111111111111b
  3481.         mov     cr0, eax
  3482.         ret
  3483.  
  3484. is_cache_enabled:
  3485.         mov     eax, cr0
  3486.         mov     ebx, eax
  3487.         and     eax, 01100000000000000000000000000000b
  3488.         jz      cache_disabled
  3489.         mov     [esp+32], ebx
  3490. cache_disabled:
  3491.         mov     dword [esp+32], eax;0
  3492.         ret
  3493.  
  3494. modify_pce:
  3495.         mov     eax, cr4
  3496. ;       mov ebx,0
  3497. ;       or  bx,100000000b ;pce
  3498. ;       xor eax,ebx ;invert pce
  3499.         bts     eax, 8;pce=cr4[8]
  3500.         mov     cr4, eax
  3501.         mov     [esp+32], eax
  3502.         ret
  3503. ;---------------------------------------------------------------------------------------------
  3504.  
  3505.  
  3506. iglobal
  3507.   cpustring db 'CPU',0
  3508. endg
  3509.  
  3510. uglobal
  3511. background_defined    db    0    ; diamond, 11.04.2006
  3512. endg
  3513. ;-----------------------------------------------------------------------------
  3514. align 4
  3515. checkmisc:
  3516.         cmp     [ctrl_alt_del], 1
  3517.         jne     nocpustart
  3518.  
  3519.         mov     ebp, cpustring
  3520.         call    fs_execute_from_sysdir
  3521.  
  3522.         mov     [ctrl_alt_del], 0
  3523. ;--------------------------------------
  3524. align 4
  3525. nocpustart:
  3526.         cmp     [mouse_active], 1
  3527.         jne     mouse_not_active
  3528.         mov     [mouse_active], 0
  3529.  
  3530.         xor     edi, edi
  3531.         mov     ebx, CURRENT_TASK
  3532.  
  3533.         mov     ecx, [TASK_COUNT]
  3534.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  3535.         shl     eax, 8
  3536.         push    eax
  3537.  
  3538.         movzx   eax, word [MOUSE_X]
  3539.         movzx   edx, word [MOUSE_Y]
  3540. ;--------------------------------------
  3541. align 4
  3542. .set_mouse_event:
  3543.         add     edi, sizeof.APPDATA
  3544.         add     ebx, sizeof.TASKDATA
  3545.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3546.         jz      .pos_filter
  3547.  
  3548.         cmp     edi, [esp]                      ; skip if filtration active
  3549.         jne     .skip
  3550. ;--------------------------------------
  3551. align 4
  3552. .pos_filter:
  3553.         test    [ebx+TASKDATA.event_mask], 0x40000000
  3554.         jz      .set
  3555.  
  3556.         mov     esi, [ebx-twdw+WDATA.box.left]
  3557.         cmp     eax, esi
  3558.         jb      .skip
  3559.         add     esi, [ebx-twdw+WDATA.box.width]
  3560.         cmp     eax, esi
  3561.         ja      .skip
  3562.  
  3563.         mov     esi, [ebx-twdw+WDATA.box.top]
  3564.         cmp     edx, esi
  3565.         jb      .skip
  3566.         add     esi, [ebx-twdw+WDATA.box.height]
  3567.         cmp     edx, esi
  3568.         ja      .skip
  3569. ;--------------------------------------
  3570. align 4
  3571. .set:
  3572.         or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b  ; set event 6
  3573. ;--------------------------------------
  3574. align 4
  3575. .skip:
  3576.         loop    .set_mouse_event
  3577.  
  3578.         pop     eax
  3579. ;--------------------------------------
  3580. align 4
  3581. mouse_not_active:
  3582.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  3583.         jz      nobackgr
  3584.  
  3585.         cmp     [background_defined], 0
  3586.         jz      nobackgr
  3587. ;--------------------------------------
  3588. align 4
  3589. backgr:
  3590.         mov     eax, [draw_data+32 + RECT.left]
  3591.         shl     eax, 16
  3592.         add     eax, [draw_data+32 + RECT.right]
  3593.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  3594.  
  3595.         mov     eax, [draw_data+32 + RECT.top]
  3596.         shl     eax, 16
  3597.         add     eax, [draw_data+32 + RECT.bottom]
  3598.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  3599.  
  3600.         call    drawbackground
  3601. ;        DEBUGF  1, "K : drawbackground\n"
  3602. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  3603. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  3604. ;--------- set event 5 start ----------
  3605.         push    ecx edi
  3606.         xor     edi, edi
  3607.         mov     ecx, [TASK_COUNT]
  3608. ;--------------------------------------
  3609. align 4
  3610. set_bgr_event:
  3611.         add     edi, sizeof.APPDATA
  3612.         mov     eax, [BG_Rect_X_left_right]
  3613.         mov     edx, [BG_Rect_Y_top_bottom]
  3614.         cmp     [edi+SLOT_BASE+APPDATA.draw_bgr_x], 0
  3615.         jz      .set
  3616. .join:
  3617.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3618.         jae     @f
  3619.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3620. @@:
  3621.         shr     eax, 16
  3622.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3623.         jbe     @f
  3624.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3625. @@:
  3626.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3627.         jae     @f
  3628.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3629. @@:
  3630.         shr     edx, 16
  3631.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3632.         jbe     @f
  3633.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3634. @@:
  3635.         jmp     .common
  3636. .set:
  3637.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_x], eax
  3638.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_y], edx
  3639. .common:
  3640.         or      [edi+SLOT_BASE+APPDATA.event_mask], 10000b  ; set event 5
  3641.         loop    set_bgr_event
  3642.         pop     edi ecx
  3643. ;--------- set event 5 stop -----------
  3644.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  3645.         jnz     backgr
  3646.  
  3647.         xor     eax, eax
  3648.         mov     [draw_data+32 + RECT.left], eax
  3649.         mov     [draw_data+32 + RECT.top], eax
  3650.         mov     [draw_data+32 + RECT.right], eax
  3651.         mov     [draw_data+32 + RECT.bottom], eax
  3652. ;--------------------------------------
  3653. align 4
  3654. nobackgr:
  3655. ; system shutdown request
  3656.         cmp     [SYS_SHUTDOWN], byte 0
  3657.         je      noshutdown
  3658.  
  3659.         mov     edx, [shutdown_processes]
  3660.  
  3661.         cmp     [SYS_SHUTDOWN], dl
  3662.         jne     noshutdown
  3663.  
  3664.         lea     ecx, [edx-1]
  3665.         mov     edx, OS_BASE+0x3040
  3666.         jecxz   no_mark_system_shutdown
  3667. ;--------------------------------------
  3668. align 4
  3669. markz:
  3670.         push    ecx edx
  3671.         cmp     [edx+TASKDATA.state], TSTATE_FREE
  3672.         jz      .nokill
  3673.         lea     edx, [(edx-(CURRENT_TASK and 1FFFFFFFh))*8+SLOT_BASE]
  3674.         cmp     [edx+APPDATA.process], sys_proc
  3675.         jz      .nokill
  3676.         call    request_terminate
  3677.         jmp     .common
  3678. .nokill:
  3679.         dec     byte [SYS_SHUTDOWN]
  3680.         xor     eax, eax
  3681. .common:
  3682.         pop     edx ecx
  3683.         test    eax, eax
  3684.         jz      @f
  3685.         mov     [edx+TASKDATA.state], TSTATE_ZOMBIE
  3686. @@:
  3687.         add     edx, 0x20
  3688.         loop    markz
  3689.         call    wakeup_osloop
  3690. ;--------------------------------------
  3691. align 4
  3692. @@:
  3693. no_mark_system_shutdown:
  3694.         dec     byte [SYS_SHUTDOWN]
  3695.         je      system_shutdown
  3696. ;--------------------------------------
  3697. align 4
  3698. noshutdown:
  3699.         mov     eax, [TASK_COUNT]           ; termination
  3700.         mov     ebx, TASK_DATA+TASKDATA.state
  3701.         mov     esi, 1
  3702. ;--------------------------------------
  3703. align 4
  3704. newct:
  3705.         mov     cl, [ebx]
  3706.         cmp     cl, byte 3
  3707.         jz      .terminate
  3708.  
  3709.         cmp     cl, byte 4
  3710.         jnz     .noterminate
  3711. .terminate:
  3712.         pushad
  3713.         mov     ecx, eax
  3714.         shl     ecx, 8
  3715.         add     ecx, SLOT_BASE
  3716.         call    restore_default_cursor_before_killing
  3717.         popad
  3718.  
  3719.         pushad
  3720.         call    terminate
  3721.         popad
  3722.         cmp     byte [SYS_SHUTDOWN], 0
  3723.         jz      .noterminate
  3724.         dec     byte [SYS_SHUTDOWN]
  3725.         je      system_shutdown
  3726.  
  3727. .noterminate:
  3728.         add     ebx, 0x20
  3729.         inc     esi
  3730.         dec     eax
  3731.         jnz     newct
  3732.         ret
  3733. ;-----------------------------------------------------------------------------
  3734. align 4
  3735. redrawscreen:
  3736. ; eax , if process window_data base is eax, do not set flag/limits
  3737.  
  3738.         pushad
  3739.         push    eax
  3740.  
  3741. ;;;         mov   ebx,2
  3742. ;;;         call  delay_hs
  3743.  
  3744.          ;mov   ecx,0               ; redraw flags for apps
  3745.         xor     ecx, ecx
  3746. ;--------------------------------------
  3747. align 4
  3748. newdw2:
  3749.         inc     ecx
  3750.         push    ecx
  3751.  
  3752.         mov     eax, ecx
  3753.         shl     eax, 5
  3754.         add     eax, window_data
  3755.  
  3756.         cmp     eax, [esp+4]
  3757.         je      not_this_task
  3758.                                    ; check if window in redraw area
  3759.         mov     edi, eax
  3760.  
  3761.         cmp     ecx, 1             ; limit for background
  3762.         jz      bgli
  3763.  
  3764.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3765.         test    eax, eax
  3766.         jz      @f
  3767.         mov     al, [eax + WDATA.z_modif]
  3768.         cmp     [edi + WDATA.z_modif], al
  3769.         jg      ricino
  3770.       @@:
  3771.  
  3772.         mov     eax, [edi + WDATA.box.left]
  3773.         mov     ebx, [edi + WDATA.box.top]
  3774.  
  3775.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3776.         cmp     ecx, ebx
  3777.         jb      ricino
  3778.  
  3779.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3780.         cmp     ecx, eax
  3781.         jb      ricino
  3782.  
  3783.         mov     eax, [edi + WDATA.box.left]
  3784.         mov     ebx, [edi + WDATA.box.top]
  3785.         mov     ecx, [edi + WDATA.box.width]
  3786.         mov     edx, [edi + WDATA.box.height]
  3787.         add     ecx, eax
  3788.         add     edx, ebx
  3789.  
  3790.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3791.         cmp     edx, eax
  3792.         jb      ricino
  3793.  
  3794.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3795.         cmp     ecx, eax
  3796.         jb      ricino
  3797. ;--------------------------------------
  3798. align 4
  3799. bgli:
  3800.         cmp     dword[esp], 1
  3801.         jnz     .az
  3802.  
  3803.         cmp     [REDRAW_BACKGROUND], 0
  3804.         jz      .az
  3805.  
  3806.         mov     dl, 0
  3807.         lea     eax, [edi+draw_data-window_data]
  3808.         mov     ebx, [draw_limits.left]
  3809.         cmp     ebx, [eax+RECT.left]
  3810.         jae     @f
  3811.  
  3812.         mov     [eax+RECT.left], ebx
  3813.         mov     dl, 1
  3814. ;--------------------------------------
  3815. align 4
  3816. @@:
  3817.         mov     ebx, [draw_limits.top]
  3818.         cmp     ebx, [eax+RECT.top]
  3819.         jae     @f
  3820.  
  3821.         mov     [eax+RECT.top], ebx
  3822.         mov     dl, 1
  3823. ;--------------------------------------
  3824. align 4
  3825. @@:
  3826.         mov     ebx, [draw_limits.right]
  3827.         cmp     ebx, [eax+RECT.right]
  3828.         jbe     @f
  3829.  
  3830.         mov     [eax+RECT.right], ebx
  3831.         mov     dl, 1
  3832. ;--------------------------------------
  3833. align 4
  3834. @@:
  3835.         mov     ebx, [draw_limits.bottom]
  3836.         cmp     ebx, [eax+RECT.bottom]
  3837.         jbe     @f
  3838.  
  3839.         mov     [eax+RECT.bottom], ebx
  3840.         mov     dl, 1
  3841. ;--------------------------------------
  3842. align 4
  3843. @@:
  3844.         add     [REDRAW_BACKGROUND], dl
  3845.         call    wakeup_osloop
  3846.         jmp     newdw8
  3847. ;--------------------------------------
  3848. align 4
  3849. .az:
  3850.         mov     eax, edi
  3851.         add     eax, draw_data-window_data
  3852.  
  3853.         mov     ebx, [draw_limits.left]        ; set limits
  3854.         mov     [eax + RECT.left], ebx
  3855.         mov     ebx, [draw_limits.top]
  3856.         mov     [eax + RECT.top], ebx
  3857.         mov     ebx, [draw_limits.right]
  3858.         mov     [eax + RECT.right], ebx
  3859.         mov     ebx, [draw_limits.bottom]
  3860.         mov     [eax + RECT.bottom], ebx
  3861.  
  3862.         sub     eax, draw_data-window_data
  3863.  
  3864.         cmp     dword [esp], 1
  3865.         jne     nobgrd
  3866.         inc     [REDRAW_BACKGROUND]
  3867.         call    wakeup_osloop
  3868. ;--------------------------------------
  3869. align 4
  3870. newdw8:
  3871. nobgrd:
  3872. ;--------------------------------------
  3873.         push    eax  edi ebp
  3874.         mov     edi, [esp+12]
  3875.         cmp     edi, 1
  3876.         je      .found
  3877.  
  3878.         mov     eax, [draw_limits.left]
  3879.         mov     ebx, [draw_limits.top]
  3880.         mov     ecx, [draw_limits.right]
  3881.         sub     ecx, eax
  3882.         test    ecx, ecx
  3883.         jz      .not_found
  3884.  
  3885.         mov     edx, [draw_limits.bottom]
  3886.         sub     edx, ebx
  3887.         test    edx, edx
  3888.         jz      .not_found
  3889.  
  3890. ; eax - x, ebx - y
  3891. ; ecx - size x, edx - size y
  3892.         add     ebx, edx
  3893. ;--------------------------------------
  3894. align 4
  3895. .start_y:
  3896.         push    ecx
  3897. ;--------------------------------------
  3898. align 4
  3899. .start_x:
  3900.         add     eax, ecx
  3901.         mov     ebp, [d_width_calc_area + ebx*4]
  3902.         add     ebp, [_display.win_map]
  3903.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3904.         cmp     ebp, edi
  3905.         jne     @f
  3906.  
  3907.         pop     ecx
  3908.         jmp     .found
  3909. ;--------------------------------------
  3910. align 4
  3911. @@:
  3912.         sub     eax, ecx
  3913.  
  3914.         dec     ecx
  3915.         jnz     .start_x
  3916.  
  3917.         pop     ecx
  3918.         dec     ebx
  3919.         dec     edx
  3920.         jnz     .start_y
  3921. ;--------------------------------------
  3922. align 4
  3923. .not_found:
  3924.         pop     ebp edi eax
  3925.         jmp     ricino
  3926. ;--------------------------------------
  3927. align 4
  3928. .found:
  3929.         pop     ebp edi eax
  3930.  
  3931.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3932. ;--------------------------------------
  3933. align 4
  3934. ricino:
  3935. not_this_task:
  3936.         pop     ecx
  3937.  
  3938.         cmp     ecx, [TASK_COUNT]
  3939.         jle     newdw2
  3940.  
  3941.         pop     eax
  3942.         popad
  3943.         ret
  3944. ;-----------------------------------------------------------------------------
  3945. align 4
  3946. calculatebackground:   ; background
  3947.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3948.         mov     eax, 0x01010101
  3949.         mov     ecx, [_display.win_map_size]
  3950.         shr     ecx, 2
  3951.         rep stosd
  3952.         mov     byte[window_data+32+WDATA.z_modif], ZPOS_DESKTOP
  3953.         mov     [REDRAW_BACKGROUND], 0
  3954.         ret
  3955. ;-----------------------------------------------------------------------------
  3956. uglobal
  3957.   imax    dd 0x0
  3958. endg
  3959. ;-----------------------------------------------------------------------------
  3960. align 4
  3961. delay_ms:     ; delay in 1/1000 sec
  3962.         pushad
  3963.  
  3964.         cmp     [hpet_base], 0
  3965.         jz      .no_hpet
  3966.         mov     eax, esi
  3967.         mov     edx, 1_000_000 ; ms to ns
  3968.         mul     edx
  3969.         mov     ebx, edx
  3970.         mov     ecx, eax
  3971.  
  3972.         push    ecx
  3973.         call    get_clock_ns
  3974.         pop     ecx
  3975.         mov     edi, edx
  3976.         mov     esi, eax
  3977. .wait:
  3978.         push    ecx
  3979.         call    get_clock_ns
  3980.         pop     ecx
  3981.         sub     eax, esi
  3982.         sbb     edx, edi
  3983.         sub     eax, ecx
  3984.         sbb     edx, ebx
  3985.         jc      .wait
  3986.         jmp     .done
  3987.  
  3988. .no_hpet:
  3989.         mov     ecx, esi
  3990.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3991.         imul    ecx, 33941
  3992.         shr     ecx, 9
  3993.         ; </CPU clock fix>
  3994.  
  3995.         in      al, 0x61
  3996.         and     al, 0x10
  3997.         mov     ah, al
  3998.         cld
  3999.  
  4000. .cnt1:
  4001.         in      al, 0x61
  4002.         and     al, 0x10
  4003.         cmp     al, ah
  4004.         jz      .cnt1
  4005.  
  4006.         mov     ah, al
  4007.         loop    .cnt1
  4008.  
  4009. .done:
  4010.         popad
  4011.         ret
  4012. ;-----------------------------------------------------------------------------
  4013. align 4
  4014. set_app_param:
  4015.         mov     edi, [TASK_BASE]
  4016.         mov     eax, ebx
  4017.         btr     eax, 3                           ; move MOUSE_FILTRATION
  4018.         mov     ebx, [current_slot]              ; bit into event_filter
  4019.         setc    byte [ebx+APPDATA.event_filter]
  4020.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  4021.         mov     [esp+32], eax                    ; return old mask value
  4022.         ret
  4023. ;-----------------------------------------------------------------------------
  4024.  
  4025. ; this is for syscall
  4026. proc delay_hs_unprotected
  4027.         call    unprotect_from_terminate
  4028.         call    delay_hs
  4029.         call    protect_from_terminate
  4030.         ret
  4031. endp
  4032.  
  4033. if 1
  4034. align 4
  4035. delay_hs:     ; delay in 1/100 secs
  4036. ; ebx = delay time
  4037.  
  4038.         pushad
  4039.         push    ebx
  4040.         xor     esi, esi
  4041.         mov     ecx, MANUAL_DESTROY
  4042.         call    create_event
  4043.         test    eax, eax
  4044.         jz      .done
  4045.  
  4046.         mov     ebx, edx
  4047.         mov     ecx, [esp]
  4048.         push    edx
  4049.         push    eax
  4050.         call    wait_event_timeout
  4051.         pop     eax
  4052.         pop     ebx
  4053.         call    destroy_event
  4054. .done:
  4055.         add     esp, 4
  4056.         popad
  4057.         ret
  4058.  
  4059. else
  4060.  
  4061. align 4
  4062. delay_hs:     ; delay in 1/100 secs
  4063. ; ebx = delay time
  4064.         push    ecx
  4065.         push    edx
  4066.  
  4067.         mov     edx, [timer_ticks]
  4068. ;--------------------------------------
  4069. align 4
  4070. newtic:
  4071.         mov     ecx, [timer_ticks]
  4072.         sub     ecx, edx
  4073.         cmp     ecx, ebx
  4074.         jae     zerodelay
  4075.  
  4076.         call    change_task
  4077.  
  4078.         jmp     newtic
  4079. ;--------------------------------------
  4080. align 4
  4081. zerodelay:
  4082.         pop     edx
  4083.         pop     ecx
  4084.         ret
  4085. end if
  4086.  
  4087. ;-----------------------------------------------------------------------------
  4088. align 16        ;very often call this subrutine
  4089. memmove:       ; memory move in bytes
  4090. ; eax = from
  4091. ; ebx = to
  4092. ; ecx = no of bytes
  4093.         test    ecx, ecx
  4094.         jle     .ret
  4095.  
  4096.         push    esi edi ecx
  4097.  
  4098.         mov     edi, ebx
  4099.         mov     esi, eax
  4100.  
  4101.         test    ecx, not 11b
  4102.         jz      @f
  4103.  
  4104.         push    ecx
  4105.         shr     ecx, 2
  4106.         rep movsd
  4107.         pop     ecx
  4108.         and     ecx, 11b
  4109.         jz      .finish
  4110. ;--------------------------------------
  4111. align 4
  4112. @@:
  4113.         rep movsb
  4114. ;--------------------------------------
  4115. align 4
  4116. .finish:
  4117.         pop     ecx edi esi
  4118. ;--------------------------------------
  4119. align 4
  4120. .ret:
  4121.         ret
  4122. ;-----------------------------------------------------------------------------
  4123. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  4124. ;align 4
  4125. ;
  4126. ;read_floppy_file:
  4127. ;
  4128. ;; as input
  4129. ;;
  4130. ;; eax pointer to file
  4131. ;; ebx file lenght
  4132. ;; ecx start 512 byte block number
  4133. ;; edx number of blocks to read
  4134. ;; esi pointer to return/work area (atleast 20 000 bytes)
  4135. ;;
  4136. ;;
  4137. ;; on return
  4138. ;;
  4139. ;; eax = 0 command succesful
  4140. ;;       1 no fd base and/or partition defined
  4141. ;;       2 yet unsupported FS
  4142. ;;       3 unknown FS
  4143. ;;       4 partition not defined at hd
  4144. ;;       5 file not found
  4145. ;; ebx = size of file
  4146. ;
  4147. ;     mov   edi,[TASK_BASE]
  4148. ;     add   edi,0x10
  4149. ;     add   esi,[edi]
  4150. ;     add   eax,[edi]
  4151. ;
  4152. ;     pushad
  4153. ;     mov  edi,esi
  4154. ;     add  edi,1024
  4155. ;     mov  esi,0x100000+19*512
  4156. ;     sub  ecx,1
  4157. ;     shl  ecx,9
  4158. ;     add  esi,ecx
  4159. ;     shl  edx,9
  4160. ;     mov  ecx,edx
  4161. ;     cld
  4162. ;     rep  movsb
  4163. ;     popad
  4164. ;
  4165. ;     mov   [esp+36],eax
  4166. ;     mov   [esp+24],ebx
  4167. ;     ret
  4168.  
  4169.  
  4170.  
  4171. align 4
  4172. set_io_access_rights:
  4173.         push    edi eax
  4174.         mov     edi, tss._io_map_0
  4175. ;     mov   ecx,eax
  4176. ;     and   ecx,7    ; offset in byte
  4177. ;     shr   eax,3    ; number of byte
  4178. ;     add   edi,eax
  4179. ;     mov   ebx,1
  4180. ;     shl   ebx,cl
  4181.         test    ebp, ebp
  4182. ;     cmp   ebp,0                ; enable access - ebp = 0
  4183.         jnz     .siar1
  4184. ;     not   ebx
  4185. ;     and   [edi],byte bl
  4186.         btr     [edi], eax
  4187.         pop     eax edi
  4188.         ret
  4189. .siar1:
  4190.         bts     [edi], eax
  4191.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4192.         pop     eax edi
  4193.         ret
  4194. ;reserve/free group of ports
  4195. ;  * eax = 46 - number function
  4196. ;  * ebx = 0 - reserve, 1 - free
  4197. ;  * ecx = number start arrea of ports
  4198. ;  * edx = number end arrea of ports (include last number of port)
  4199. ;Return value:
  4200. ;  * eax = 0 - succesful
  4201. ;  * eax = 1 - error
  4202. ;  * The system has reserve this ports:
  4203. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  4204. ;destroys eax,ebx, ebp
  4205. r_f_port_area:
  4206.  
  4207.         test    ebx, ebx
  4208.         jnz     free_port_area
  4209. ;     je    r_port_area
  4210. ;     jmp   free_port_area
  4211.  
  4212. ;   r_port_area:
  4213.  
  4214. ;     pushad
  4215.  
  4216.         cmp     ecx, edx      ; beginning > end ?
  4217.         ja      rpal1
  4218.         cmp     edx, 65536
  4219.         jae     rpal1
  4220.         mov     eax, [RESERVED_PORTS]
  4221.         test    eax, eax      ; no reserved areas ?
  4222.         je      rpal2
  4223.         cmp     eax, 255      ; max reserved
  4224.         jae     rpal1
  4225.  rpal3:
  4226.         mov     ebx, eax
  4227.         shl     ebx, 4
  4228.         add     ebx, RESERVED_PORTS
  4229.         cmp     ecx, [ebx+8]
  4230.         ja      rpal4
  4231.         cmp     edx, [ebx+4]
  4232.         jae     rpal1
  4233. ;     jb    rpal4
  4234. ;     jmp   rpal1
  4235.  rpal4:
  4236.         dec     eax
  4237.         jnz     rpal3
  4238.         jmp     rpal2
  4239.    rpal1:
  4240. ;     popad
  4241. ;     mov   eax,1
  4242.         xor     eax, eax
  4243.         inc     eax
  4244.         ret
  4245.    rpal2:
  4246. ;     popad
  4247.      ; enable port access at port IO map
  4248.         cli
  4249.         pushad                        ; start enable io map
  4250.  
  4251.         cmp     edx, 65536;16384
  4252.         jae     no_unmask_io; jge
  4253.         mov     eax, ecx
  4254. ;       push    ebp
  4255.         xor     ebp, ebp               ; enable - eax = port
  4256. new_port_access:
  4257. ;     pushad
  4258.         call    set_io_access_rights
  4259. ;     popad
  4260.         inc     eax
  4261.         cmp     eax, edx
  4262.         jbe     new_port_access
  4263. ;       pop     ebp
  4264. no_unmask_io:
  4265.         popad                         ; end enable io map
  4266.         sti
  4267.  
  4268.         mov     eax, [RESERVED_PORTS]
  4269.         add     eax, 1
  4270.         mov     [RESERVED_PORTS], eax
  4271.         shl     eax, 4
  4272.         add     eax, RESERVED_PORTS
  4273.         mov     ebx, [TASK_BASE]
  4274.         mov     ebx, [ebx+TASKDATA.pid]
  4275.         mov     [eax], ebx
  4276.         mov     [eax+4], ecx
  4277.         mov     [eax+8], edx
  4278.  
  4279.         xor     eax, eax
  4280.         ret
  4281.  
  4282. free_port_area:
  4283.  
  4284. ;     pushad
  4285.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  4286.         test    eax, eax
  4287.         jz      frpal2
  4288.         mov     ebx, [TASK_BASE]
  4289.         mov     ebx, [ebx+TASKDATA.pid]
  4290.    frpal3:
  4291.         mov     edi, eax
  4292.         shl     edi, 4
  4293.         add     edi, RESERVED_PORTS
  4294.         cmp     ebx, [edi]
  4295.         jne     frpal4
  4296.         cmp     ecx, [edi+4]
  4297.         jne     frpal4
  4298.         cmp     edx, [edi+8]
  4299.         jne     frpal4
  4300.         jmp     frpal1
  4301.    frpal4:
  4302.         dec     eax
  4303.         jnz     frpal3
  4304.    frpal2:
  4305. ;     popad
  4306.         inc     eax
  4307.         ret
  4308.    frpal1:
  4309.         push    ecx
  4310.         mov     ecx, 256
  4311.         sub     ecx, eax
  4312.         shl     ecx, 4
  4313.         mov     esi, edi
  4314.         add     esi, 16
  4315.         cld
  4316.         rep movsb
  4317.  
  4318.         dec     dword [RESERVED_PORTS]
  4319. ;popad
  4320. ;disable port access at port IO map
  4321.  
  4322. ;     pushad                        ; start disable io map
  4323.         pop     eax     ;start port
  4324.         cmp     edx, 65536;16384
  4325.         jge     no_mask_io
  4326.  
  4327. ;     mov   eax,ecx
  4328.         xor     ebp, ebp
  4329.         inc     ebp
  4330. new_port_access_disable:
  4331. ;     pushad
  4332. ;     mov   ebp,1                  ; disable - eax = port
  4333.         call    set_io_access_rights
  4334. ;     popad
  4335.         inc     eax
  4336.         cmp     eax, edx
  4337.         jbe     new_port_access_disable
  4338. no_mask_io:
  4339. ;     popad                         ; end disable io map
  4340.         xor     eax, eax
  4341.         ret
  4342. ;-----------------------------------------------------------------------------
  4343. align 4
  4344. drawbackground:
  4345. dbrv20:
  4346.         cmp     [BgrDrawMode], dword 1
  4347.         jne     bgrstr
  4348.         call    vesa20_drawbackground_tiled
  4349. ;        call    [draw_pointer]
  4350.         call    __sys_draw_pointer
  4351.         ret
  4352. ;--------------------------------------
  4353. align 4
  4354. bgrstr:
  4355.         call    vesa20_drawbackground_stretch
  4356. ;        call    [draw_pointer]
  4357.         call    __sys_draw_pointer
  4358.         ret
  4359. ;-----------------------------------------------------------------------------
  4360. align 4
  4361. syscall_putimage:                       ; PutImage
  4362. sys_putimage:
  4363.         test    ecx, 0x80008000
  4364.         jnz     .exit
  4365.         test    ecx, 0x0000FFFF
  4366.         jz      .exit
  4367.         test    ecx, 0xFFFF0000
  4368.         jnz     @f
  4369. ;--------------------------------------
  4370. align 4
  4371. .exit:
  4372.         ret
  4373. ;--------------------------------------
  4374. align 4
  4375. @@:
  4376.         mov     edi, [current_slot]
  4377.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  4378.         rol     edx, 16
  4379.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  4380.         rol     edx, 16
  4381. ;--------------------------------------
  4382. align 4
  4383. .forced:
  4384.         push    ebp esi 0
  4385.         mov     ebp, putimage_get24bpp
  4386.         mov     esi, putimage_init24bpp
  4387. ;--------------------------------------
  4388. align 4
  4389. sys_putimage_bpp:
  4390.         call    vesa20_putimage
  4391.         pop     ebp esi ebp
  4392.         ret
  4393. ;        jmp     [draw_pointer]
  4394. ;-----------------------------------------------------------------------------
  4395. align 4
  4396. sys_putimage_palette:
  4397. ; ebx = pointer to image
  4398. ; ecx = [xsize]*65536 + [ysize]
  4399. ; edx = [xstart]*65536 + [ystart]
  4400. ; esi = number of bits per pixel, must be 8, 24 or 32
  4401. ; edi = pointer to palette
  4402. ; ebp = row delta
  4403.         mov     eax, [CURRENT_TASK]
  4404.         shl     eax, 8
  4405.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4406.         rol     edx, 16
  4407.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4408.         rol     edx, 16
  4409. ;--------------------------------------
  4410. align 4
  4411. .forced:
  4412.         cmp     esi, 1
  4413.         jnz     @f
  4414.         push    edi
  4415.         mov     eax, [edi+4]
  4416.         sub     eax, [edi]
  4417.         push    eax
  4418.         push    dword [edi]
  4419.         push    0ffffff80h
  4420.         mov     edi, esp
  4421.         call    put_mono_image
  4422.         add     esp, 12
  4423.         pop     edi
  4424.         ret
  4425. ;--------------------------------------
  4426. align 4
  4427. @@:
  4428.         cmp     esi, 2
  4429.         jnz     @f
  4430.         push    edi
  4431.         push    0ffffff80h
  4432.         mov     edi, esp
  4433.         call    put_2bit_image
  4434.         pop     eax
  4435.         pop     edi
  4436.         ret
  4437. ;--------------------------------------
  4438. align 4
  4439. @@:
  4440.         cmp     esi, 4
  4441.         jnz     @f
  4442.         push    edi
  4443.         push    0ffffff80h
  4444.         mov     edi, esp
  4445.         call    put_4bit_image
  4446.         pop     eax
  4447.         pop     edi
  4448.         ret
  4449. ;--------------------------------------
  4450. align 4
  4451. @@:
  4452.         push    ebp esi ebp
  4453.         cmp     esi, 8
  4454.         jnz     @f
  4455.         mov     ebp, putimage_get8bpp
  4456.         mov     esi, putimage_init8bpp
  4457.         jmp     sys_putimage_bpp
  4458. ;--------------------------------------
  4459. align 4
  4460. @@:
  4461.         cmp     esi, 9
  4462.         jnz     @f
  4463.         mov     ebp, putimage_get9bpp
  4464.         mov     esi, putimage_init9bpp
  4465.         jmp     sys_putimage_bpp
  4466. ;--------------------------------------
  4467. align 4
  4468. @@:
  4469.         cmp     esi, 15
  4470.         jnz     @f
  4471.         mov     ebp, putimage_get15bpp
  4472.         mov     esi, putimage_init15bpp
  4473.         jmp     sys_putimage_bpp
  4474. ;--------------------------------------
  4475. align 4
  4476. @@:
  4477.         cmp     esi, 16
  4478.         jnz     @f
  4479.         mov     ebp, putimage_get16bpp
  4480.         mov     esi, putimage_init16bpp
  4481.         jmp     sys_putimage_bpp
  4482. ;--------------------------------------
  4483. align 4
  4484. @@:
  4485.         cmp     esi, 24
  4486.         jnz     @f
  4487.         mov     ebp, putimage_get24bpp
  4488.         mov     esi, putimage_init24bpp
  4489.         jmp     sys_putimage_bpp
  4490. ;--------------------------------------
  4491. align 4
  4492. @@:
  4493.         cmp     esi, 32
  4494.         jnz     @f
  4495.         mov     ebp, putimage_get32bpp
  4496.         mov     esi, putimage_init32bpp
  4497.         jmp     sys_putimage_bpp
  4498. ;--------------------------------------
  4499. align 4
  4500. @@:
  4501.         pop     ebp esi ebp
  4502.         ret
  4503. ;-----------------------------------------------------------------------------
  4504. align 4
  4505. put_mono_image:
  4506.         push    ebp esi ebp
  4507.         mov     ebp, putimage_get1bpp
  4508.         mov     esi, putimage_init1bpp
  4509.         jmp     sys_putimage_bpp
  4510. ;-----------------------------------------------------------------------------
  4511. align 4
  4512. put_2bit_image:
  4513.         push    ebp esi ebp
  4514.         mov     ebp, putimage_get2bpp
  4515.         mov     esi, putimage_init2bpp
  4516.         jmp     sys_putimage_bpp
  4517. ;-----------------------------------------------------------------------------
  4518. align 4
  4519. put_4bit_image:
  4520.         push    ebp esi ebp
  4521.         mov     ebp, putimage_get4bpp
  4522.         mov     esi, putimage_init4bpp
  4523.         jmp     sys_putimage_bpp
  4524. ;-----------------------------------------------------------------------------
  4525. align 4
  4526. putimage_init24bpp:
  4527.         lea     eax, [eax*3]
  4528. putimage_init8bpp:
  4529. putimage_init9bpp:
  4530.         ret
  4531. ;-----------------------------------------------------------------------------
  4532. align 16
  4533. putimage_get24bpp:
  4534.         movzx   eax, byte [esi+2]
  4535.         shl     eax, 16
  4536.         mov     ax, [esi]
  4537.         add     esi, 3
  4538.         ret     4
  4539. ;-----------------------------------------------------------------------------
  4540. align 16
  4541. putimage_get8bpp:
  4542.         movzx   eax, byte [esi]
  4543.         push    edx
  4544.         mov     edx, [esp+8]
  4545.         mov     eax, [edx+eax*4]
  4546.         pop     edx
  4547.         inc     esi
  4548.         ret     4
  4549. ;-----------------------------------------------------------------------------
  4550. align 16
  4551. putimage_get9bpp:
  4552.         lodsb
  4553.         mov     ah, al
  4554.         shl     eax, 8
  4555.         mov     al, ah
  4556.         ret     4
  4557. ;-----------------------------------------------------------------------------
  4558. align 4
  4559. putimage_init1bpp:
  4560.         add     eax, ecx
  4561.         push    ecx
  4562.         add     eax, 7
  4563.         add     ecx, 7
  4564.         shr     eax, 3
  4565.         shr     ecx, 3
  4566.         sub     eax, ecx
  4567.         pop     ecx
  4568.         ret
  4569. ;-----------------------------------------------------------------------------
  4570. align 16
  4571. putimage_get1bpp:
  4572.         push    edx
  4573.         mov     edx, [esp+8]
  4574.         mov     al, [edx]
  4575.         add     al, al
  4576.         jnz     @f
  4577.         lodsb
  4578.         adc     al, al
  4579. @@:
  4580.         mov     [edx], al
  4581.         sbb     eax, eax
  4582.         and     eax, [edx+8]
  4583.         add     eax, [edx+4]
  4584.         pop     edx
  4585.         ret     4
  4586. ;-----------------------------------------------------------------------------
  4587. align 4
  4588. putimage_init2bpp:
  4589.         add     eax, ecx
  4590.         push    ecx
  4591.         add     ecx, 3
  4592.         add     eax, 3
  4593.         shr     ecx, 2
  4594.         shr     eax, 2
  4595.         sub     eax, ecx
  4596.         pop     ecx
  4597.         ret
  4598. ;-----------------------------------------------------------------------------
  4599. align 16
  4600. putimage_get2bpp:
  4601.         push    edx
  4602.         mov     edx, [esp+8]
  4603.         mov     al, [edx]
  4604.         mov     ah, al
  4605.         shr     al, 6
  4606.         shl     ah, 2
  4607.         jnz     .nonewbyte
  4608.         lodsb
  4609.         mov     ah, al
  4610.         shr     al, 6
  4611.         shl     ah, 2
  4612.         add     ah, 1
  4613. .nonewbyte:
  4614.         mov     [edx], ah
  4615.         mov     edx, [edx+4]
  4616.         movzx   eax, al
  4617.         mov     eax, [edx+eax*4]
  4618.         pop     edx
  4619.         ret     4
  4620. ;-----------------------------------------------------------------------------
  4621. align 4
  4622. putimage_init4bpp:
  4623.         add     eax, ecx
  4624.         push    ecx
  4625.         add     ecx, 1
  4626.         add     eax, 1
  4627.         shr     ecx, 1
  4628.         shr     eax, 1
  4629.         sub     eax, ecx
  4630.         pop     ecx
  4631.         ret
  4632. ;-----------------------------------------------------------------------------
  4633. align 16
  4634. putimage_get4bpp:
  4635.         push    edx
  4636.         mov     edx, [esp+8]
  4637.         add     byte [edx], 80h
  4638.         jc      @f
  4639.         movzx   eax, byte [edx+1]
  4640.         mov     edx, [edx+4]
  4641.         and     eax, 0x0F
  4642.         mov     eax, [edx+eax*4]
  4643.         pop     edx
  4644.         ret     4
  4645. @@:
  4646.         movzx   eax, byte [esi]
  4647.         add     esi, 1
  4648.         mov     [edx+1], al
  4649.         shr     eax, 4
  4650.         mov     edx, [edx+4]
  4651.         mov     eax, [edx+eax*4]
  4652.         pop     edx
  4653.         ret     4
  4654. ;-----------------------------------------------------------------------------
  4655. align 4
  4656. putimage_init32bpp:
  4657.         shl     eax, 2
  4658.         ret
  4659. ;-----------------------------------------------------------------------------
  4660. align 16
  4661. putimage_get32bpp:
  4662.         lodsd
  4663.         ret     4
  4664. ;-----------------------------------------------------------------------------
  4665. align 4
  4666. putimage_init15bpp:
  4667. putimage_init16bpp:
  4668.         add     eax, eax
  4669.         ret
  4670. ;-----------------------------------------------------------------------------
  4671. align 16
  4672. putimage_get15bpp:
  4673. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4674.         push    ecx edx
  4675.         movzx   eax, word [esi]
  4676.         add     esi, 2
  4677.         mov     ecx, eax
  4678.         mov     edx, eax
  4679.         and     eax, 0x1F
  4680.         and     ecx, 0x1F shl 5
  4681.         and     edx, 0x1F shl 10
  4682.         shl     eax, 3
  4683.         shl     ecx, 6
  4684.         shl     edx, 9
  4685.         or      eax, ecx
  4686.         or      eax, edx
  4687.         pop     edx ecx
  4688.         ret     4
  4689. ;-----------------------------------------------------------------------------
  4690. align 16
  4691. putimage_get16bpp:
  4692. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4693.         push    ecx edx
  4694.         movzx   eax, word [esi]
  4695.         add     esi, 2
  4696.         mov     ecx, eax
  4697.         mov     edx, eax
  4698.         and     eax, 0x1F
  4699.         and     ecx, 0x3F shl 5
  4700.         and     edx, 0x1F shl 11
  4701.         shl     eax, 3
  4702.         shl     ecx, 5
  4703.         shl     edx, 8
  4704.         or      eax, ecx
  4705.         or      eax, edx
  4706.         pop     edx ecx
  4707.         ret     4
  4708. ;-----------------------------------------------------------------------------
  4709. ;align 4
  4710. ; eax x beginning
  4711. ; ebx y beginning
  4712. ; ecx x end
  4713.         ; edx y end
  4714. ; edi color
  4715. ;__sys_drawbar:
  4716. ;        mov     esi, [current_slot]
  4717. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4718. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4719. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4720. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4721. ;--------------------------------------
  4722. ;align 4
  4723. ;.forced:
  4724. ;        call    vesa20_drawbar
  4725. ;        call    [draw_pointer]
  4726. ;        ret
  4727. ;-----------------------------------------------------------------------------
  4728. align 4
  4729. kb_write_wait_ack:
  4730.  
  4731.         push    ecx edx
  4732.  
  4733.         mov     dl, al
  4734.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4735. .wait_output_ready:
  4736.         in      al, 0x64
  4737.         test    al, 2
  4738.         jz      @f
  4739.         loop    .wait_output_ready
  4740.         mov     ah, 1
  4741.         jmp     .nothing
  4742. @@:
  4743.         mov     al, dl
  4744.         out     0x60, al
  4745.         mov     ecx, 0xfffff; last 0xffff, new value in view of fast CPU's
  4746. .wait_ack:
  4747.         in      al, 0x64
  4748.         test    al, 1
  4749.         jnz     @f
  4750.         loop    .wait_ack
  4751.         mov     ah, 1
  4752.         jmp     .nothing
  4753. @@:
  4754.         in      al, 0x60
  4755.         xor     ah, ah
  4756.  
  4757. .nothing:
  4758.         pop     edx ecx
  4759.  
  4760.         ret
  4761. ;-----------------------------------------------------------------------------
  4762.  
  4763. setmouse:  ; set mousepicture -pointer
  4764.            ; ps2 mouse enable
  4765.  
  4766. ;        mov     [MOUSE_PICTURE], dword mousepointer
  4767.  
  4768.         cli
  4769.  
  4770.         ret
  4771.  
  4772. if used _rdtsc
  4773. _rdtsc:
  4774.         bt      [cpu_caps], CAPS_TSC
  4775.         jnc     ret_rdtsc
  4776.         rdtsc
  4777.         ret
  4778.    ret_rdtsc:
  4779.         mov     edx, 0xffffffff
  4780.         mov     eax, 0xffffffff
  4781.         ret
  4782. end if
  4783.  
  4784. sys_msg_board_str:
  4785.  
  4786.         pushad
  4787.    @@:
  4788.         cmp     [esi], byte 0
  4789.         je      @f
  4790.         mov     ebx, 1
  4791.         movzx   ecx, byte [esi]
  4792.         call    sys_msg_board
  4793.         inc     esi
  4794.         jmp     @b
  4795.    @@:
  4796.         popad
  4797.         ret
  4798.  
  4799. sys_msg_board_byte:
  4800. ; in: al = byte to display
  4801. ; out: nothing
  4802. ; destroys: nothing
  4803.         pushad
  4804.         mov     ecx, 2
  4805.         shl     eax, 24
  4806.         jmp     @f
  4807.  
  4808. sys_msg_board_word:
  4809. ; in: ax = word to display
  4810. ; out: nothing
  4811. ; destroys: nothing
  4812.         pushad
  4813.         mov     ecx, 4
  4814.         shl     eax, 16
  4815.         jmp     @f
  4816.  
  4817. sys_msg_board_dword:
  4818. ; in: eax = dword to display
  4819. ; out: nothing
  4820. ; destroys: nothing
  4821.         pushad
  4822.         mov     ecx, 8
  4823. @@:
  4824.         push    ecx
  4825.         rol     eax, 4
  4826.         push    eax
  4827.         and     al, 0xF
  4828.         cmp     al, 10
  4829.         sbb     al, 69h
  4830.         das
  4831.         mov     cl, al
  4832.         xor     ebx, ebx
  4833.         inc     ebx
  4834.         call    sys_msg_board
  4835.         pop     eax
  4836.         pop     ecx
  4837.         loop    @b
  4838.         popad
  4839.         ret
  4840.  
  4841. msg_board_data_size = 65536 ; Must be power of two
  4842.  
  4843. uglobal
  4844. msg_board_data  rb  msg_board_data_size
  4845. msg_board_count dd  ?
  4846. endg
  4847.  
  4848. iglobal
  4849. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4850. endg
  4851.  
  4852. sys_msg_board:
  4853. ; ebx=1 -> write, cl = byte to write
  4854. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4855.         push    eax ebx
  4856.         mov     eax, ebx
  4857.         mov     ebx, ecx
  4858.         mov     ecx, [msg_board_count]
  4859.         cmp     eax, 1
  4860.         jne     .read
  4861.  
  4862. if defined debug_com_base
  4863.         push    dx ax
  4864. @@: ; wait for empty transmit register
  4865.         mov     dx, debug_com_base+5
  4866.         in      al, dx
  4867.         test    al, 1 shl 5
  4868.         jz      @r
  4869.         mov     dx, debug_com_base      ; Output the byte
  4870.         mov     al, bl
  4871.         out     dx, al
  4872.         pop     ax dx
  4873. end if
  4874.  
  4875.         mov     [msg_board_data+ecx], bl
  4876.         cmp     byte [debug_direct_print], 1
  4877.         jnz     .end
  4878.         pusha
  4879.         lea     edx, [msg_board_data+ecx]
  4880.         mov     ecx, 0x40FFFFFF
  4881.         mov     ebx, [msg_board_pos]
  4882.         mov     edi, 1
  4883.         mov     esi, 1
  4884.         call    dtext
  4885.         popa
  4886.         add     word [msg_board_pos+2], 6
  4887.         cmp     word [msg_board_pos+2], 105*6
  4888.         jnc     @f
  4889.         cmp     bl, 10
  4890.         jnz     .end
  4891. @@:
  4892.         mov     word [msg_board_pos+2], 42*6
  4893.         add     word [msg_board_pos], 10
  4894.         mov     eax, [_display.height]
  4895.         sub     eax, 10
  4896.         cmp     ax, word [msg_board_pos]
  4897.         jnc     @f
  4898.         mov     word [msg_board_pos], 10
  4899. @@:
  4900.         pusha
  4901.         mov     eax, [msg_board_pos]
  4902.         movzx   ebx, ax
  4903.         shr     eax, 16
  4904.         mov     edx, 105*6
  4905.         xor     ecx, ecx
  4906.         mov     edi, 1
  4907.         mov     esi, 9
  4908. @@:
  4909.         call    hline
  4910.         inc     ebx
  4911.         dec     esi
  4912.         jnz     @b
  4913.         popa
  4914. .end:
  4915.         inc     ecx
  4916.         and     ecx, msg_board_data_size - 1
  4917.         mov     [msg_board_count], ecx
  4918. .ret:
  4919.         pop     ebx eax
  4920.         ret
  4921.  
  4922. @@:
  4923.         mov     [esp+32], ecx
  4924.         mov     [esp+20], ecx
  4925.         jmp     .ret
  4926.  
  4927. .read:
  4928.         cmp     eax, 2
  4929.         jne     .ret
  4930.         test    ecx, ecx
  4931.         jz      @b
  4932.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4933.         mov     eax, msg_board_data+1
  4934.         mov     ebx, msg_board_data
  4935.         movzx   edx, byte [ebx]
  4936.         call    memmove
  4937.         dec     [msg_board_count]
  4938.         mov     [esp + 32], edx ;eax
  4939.         mov     [esp + 20], dword 1
  4940.         ret
  4941.  
  4942. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4943. ;; 66 sys function.                                                ;;
  4944. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4945. ;; out eax                                                         ;;
  4946. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4947. iglobal
  4948. align 4
  4949. f66call:
  4950.            dd sys_process_def.1   ; 1 = set keyboard mode
  4951.            dd sys_process_def.2   ; 2 = get keyboard mode
  4952.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4953.            dd sys_process_def.4   ; 4 = set system-wide hotkey
  4954.            dd sys_process_def.5   ; 5 = delete installed hotkey
  4955.            dd sys_process_def.6   ; 6 = disable input, work only hotkeys
  4956.            dd sys_process_def.7   ; 7 = enable input, opposition to f.66.6
  4957. endg
  4958. ;-----------------------------------------------------------------------------
  4959. align 4
  4960. sys_process_def:
  4961.         dec     ebx
  4962.         cmp     ebx, 7
  4963.         jae     .not_support    ;if >=8 then or eax,-1
  4964.  
  4965.         mov     edi, [CURRENT_TASK]
  4966.         jmp     dword [f66call+ebx*4]
  4967.  
  4968. .not_support:
  4969.         or      eax, -1
  4970.         ret
  4971. ;-----------------------------------------------------------------------------
  4972. align 4
  4973. .1:
  4974.         shl     edi, 8
  4975.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4976.  
  4977.         ret
  4978. ;-----------------------------------------------------------------------------
  4979. align 4
  4980. .2:                             ; 2 = get keyboard mode
  4981.         shl     edi, 8
  4982.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4983.         mov     [esp+32], eax
  4984.         ret
  4985. ;-----------------------------------------------------------------------------
  4986. align 4
  4987. .3:                             ;3 = get keyboard ctrl, alt, shift
  4988.         mov     eax, [kb_state]
  4989.         mov     [esp+32], eax
  4990.         ret
  4991. ;-----------------------------------------------------------------------------
  4992. align 4
  4993. .4:
  4994.         mov     eax, hotkey_list
  4995. @@:
  4996.         cmp     dword [eax+8], 0
  4997.         jz      .found_free
  4998.         add     eax, 16
  4999.         cmp     eax, hotkey_list+16*256
  5000.         jb      @b
  5001.         mov     dword [esp+32], 1
  5002.         ret
  5003. .found_free:
  5004.         mov     [eax+8], edi
  5005.         mov     [eax+4], edx
  5006.         movzx   ecx, cl
  5007.         lea     ecx, [hotkey_scancodes+ecx*4]
  5008.         mov     edx, [ecx]
  5009.         mov     [eax], edx
  5010.         mov     [ecx], eax
  5011.         mov     [eax+12], ecx
  5012.         test    edx, edx
  5013.         jz      @f
  5014.         mov     [edx+12], eax
  5015. @@:
  5016.         and     dword [esp+32], 0
  5017.         ret
  5018. ;-----------------------------------------------------------------------------
  5019. align 4
  5020. .5:
  5021.         movzx   ebx, cl
  5022.         lea     ebx, [hotkey_scancodes+ebx*4]
  5023.         mov     eax, [ebx]
  5024. .scan:
  5025.         test    eax, eax
  5026.         jz      .notfound
  5027.         cmp     [eax+8], edi
  5028.         jnz     .next
  5029.         cmp     [eax+4], edx
  5030.         jz      .found
  5031. .next:
  5032.         mov     eax, [eax]
  5033.         jmp     .scan
  5034. .notfound:
  5035.         mov     dword [esp+32], 1
  5036.         ret
  5037. .found:
  5038.         mov     ecx, [eax]
  5039.         jecxz   @f
  5040.         mov     edx, [eax+12]
  5041.         mov     [ecx+12], edx
  5042. @@:
  5043.         mov     ecx, [eax+12]
  5044.         mov     edx, [eax]
  5045.         mov     [ecx], edx
  5046.         xor     edx, edx
  5047.         mov     [eax+4], edx
  5048.         mov     [eax+8], edx
  5049.         mov     [eax+12], edx
  5050.         mov     [eax], edx
  5051.         mov     [esp+32], edx
  5052.         ret
  5053. ;-----------------------------------------------------------------------------
  5054. align 4
  5055. .6:
  5056.         pushfd
  5057.         cli
  5058.         mov     eax, [PID_lock_input]
  5059.         test    eax, eax
  5060.         jnz     @f
  5061. ; get current PID
  5062.         mov     eax, [CURRENT_TASK]
  5063.         shl     eax, 5
  5064.         mov     eax, [eax+CURRENT_TASK+TASKDATA.pid]
  5065. ; set current PID for lock input
  5066.         mov     [PID_lock_input], eax
  5067. @@:
  5068.         popfd
  5069.         ret
  5070. ;-----------------------------------------------------------------------------
  5071. align 4
  5072. .7:
  5073.         mov     eax, [PID_lock_input]
  5074.         test    eax, eax
  5075.         jz      @f
  5076. ; get current PID
  5077.         mov     ebx, [CURRENT_TASK]
  5078.         shl     ebx, 5
  5079.         mov     ebx, [ebx+CURRENT_TASK+TASKDATA.pid]
  5080. ; compare current lock input with current PID
  5081.         cmp     ebx, eax
  5082.         jne     @f
  5083.  
  5084.         xor     eax, eax
  5085.         mov     [PID_lock_input], eax
  5086. @@:
  5087.         ret
  5088. ;-----------------------------------------------------------------------------
  5089. uglobal
  5090.   PID_lock_input dd 0x0
  5091. endg
  5092. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5093. ;; 61 sys function.                                                ;;
  5094. ;; in eax=61,ebx in [1..3]                                         ;;
  5095. ;; out eax                                                         ;;
  5096. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5097. iglobal
  5098. align 4
  5099. f61call:
  5100.            dd sys_gs.1   ; resolution
  5101.            dd sys_gs.2   ; bits per pixel
  5102.            dd sys_gs.3   ; bytes per scanline
  5103. endg
  5104.  
  5105.  
  5106. align 4
  5107.  
  5108. sys_gs:                         ; direct screen access
  5109.         dec     ebx
  5110.         cmp     ebx, 2
  5111.         ja      .not_support
  5112.         jmp     dword [f61call+ebx*4]
  5113. .not_support:
  5114.         or      [esp+32], dword -1
  5115.         ret
  5116.  
  5117.  
  5118. .1:                             ; resolution
  5119.         mov     eax, [_display.width]
  5120.         shl     eax, 16
  5121.         mov     ax, word [_display.height]
  5122.         mov     [esp+32], eax
  5123.         ret
  5124. .2:                             ; bits per pixel
  5125.         mov     eax, [_display.bits_per_pixel]
  5126.         mov     [esp+32], eax
  5127.         ret
  5128. .3:                             ; bytes per scanline
  5129.         mov     eax, [_display.lfb_pitch]
  5130.         mov     [esp+32], eax
  5131.         ret
  5132.  
  5133. align 4  ;  system functions
  5134.  
  5135. syscall_setpixel:                       ; SetPixel
  5136.  
  5137.         mov     eax, ebx
  5138.         mov     ebx, ecx
  5139.         mov     ecx, edx
  5140.         mov     edx, [TASK_BASE]
  5141.         add     eax, [edx-twdw+WDATA.box.left]
  5142.         add     ebx, [edx-twdw+WDATA.box.top]
  5143.         mov     edi, [current_slot]
  5144.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  5145.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  5146.         xor     edi, edi ; no force
  5147.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
  5148. ;        jmp     [putpixel]
  5149.         jmp     __sys_putpixel
  5150.  
  5151. align 4
  5152.  
  5153. syscall_writetext:                      ; WriteText
  5154.  
  5155.         mov     eax, [TASK_BASE]
  5156.         mov     ebp, [eax-twdw+WDATA.box.left]
  5157.         push    esi
  5158.         mov     esi, [current_slot]
  5159.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5160.         shl     ebp, 16
  5161.         add     ebp, [eax-twdw+WDATA.box.top]
  5162.         add     bp, word[esi+APPDATA.wnd_clientbox.top]
  5163.         pop     esi
  5164.         test    ecx, 0x08000000  ; redirect the output to the user area
  5165.         jnz     @f
  5166.         add     ebx, ebp
  5167. align 4
  5168. @@:
  5169.         mov     eax, edi
  5170.         test    ecx, 0x08000000  ; redirect the output to the user area
  5171.         jnz     dtext
  5172.         xor     edi, edi
  5173.         jmp     dtext
  5174.  
  5175. align 4
  5176.  
  5177. syscall_drawrect:                       ; DrawRect
  5178.  
  5179.         mov     edi, edx ; color + gradient
  5180.         and     edi, 0x80FFFFFF
  5181.         test    bx, bx  ; x.size
  5182.         je      .drectr
  5183.         test    cx, cx ; y.size
  5184.         je      .drectr
  5185.  
  5186.         mov     eax, ebx ; bad idea
  5187.         mov     ebx, ecx
  5188.  
  5189.         movzx   ecx, ax ; ecx - x.size
  5190.         shr     eax, 16 ; eax - x.coord
  5191.         movzx   edx, bx ; edx - y.size
  5192.         shr     ebx, 16 ; ebx - y.coord
  5193.         mov     esi, [current_slot]
  5194.  
  5195.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  5196.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  5197.         add     ecx, eax
  5198.         add     edx, ebx
  5199. ;        jmp     [drawbar]
  5200.         jmp     vesa20_drawbar
  5201. .drectr:
  5202.         ret
  5203.  
  5204. align 4
  5205. syscall_getscreensize:                  ; GetScreenSize
  5206.         mov     ax, word [_display.width]
  5207.         dec     ax
  5208.         shl     eax, 16
  5209.         mov     ax, word [_display.height]
  5210.         dec     ax
  5211.         mov     [esp + 32], eax
  5212.         ret
  5213. ;-----------------------------------------------------------------------------
  5214. align 4
  5215. syscall_cdaudio:
  5216. ; ECX - position of CD/DVD-drive
  5217. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  5218. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  5219. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  5220.         cmp     ecx, 11
  5221.         ja      .exit
  5222.  
  5223.         mov     eax, ecx
  5224.         shr     eax, 2
  5225.         lea     eax, [eax*5]
  5226.         mov     al, [eax+DRIVE_DATA+1]
  5227.  
  5228.         push    ecx ebx
  5229.         mov     ebx, ecx
  5230.         and     ebx, 11b
  5231.         shl     ebx, 1
  5232.         mov     cl, 6
  5233.         sub     cl, bl
  5234.         shr     al, cl
  5235.         test    al, 2 ; it's not an ATAPI device
  5236.         pop     ebx ecx
  5237.  
  5238.         jz      .exit
  5239.  
  5240.         cmp     ebx, 4
  5241.         je      .eject
  5242.  
  5243.         cmp     ebx, 5
  5244.         je      .load
  5245. ;--------------------------------------
  5246. .exit:
  5247.         ret
  5248. ;--------------------------------------
  5249. .load:
  5250.         call    .reserve
  5251.         call    LoadMedium
  5252.         jmp     .free
  5253. ;--------------------------------------
  5254. .eject:
  5255.         call    .reserve
  5256.         call    clear_CD_cache
  5257.         call    allow_medium_removal
  5258.         call    EjectMedium
  5259.         jmp     .free
  5260. ;--------------------------------------
  5261. .reserve:
  5262.         call    reserve_cd
  5263.  
  5264.         mov     ebx, ecx
  5265.         inc     ebx
  5266.         mov     [cdpos], ebx
  5267.  
  5268.         mov     eax, ecx
  5269.         shr     eax, 1
  5270.         and     eax, 1
  5271.         inc     eax
  5272.         mov     [ChannelNumber], al
  5273.         mov     eax, ecx
  5274.         and     eax, 1
  5275.         mov     [DiskNumber], al
  5276.         call    reserve_cd_channel
  5277.         ret
  5278. ;--------------------------------------
  5279. .free:
  5280.         call    free_cd_channel
  5281.         and     [cd_status], 0
  5282.         ret
  5283. ;-----------------------------------------------------------------------------
  5284. align 4
  5285. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  5286.         cmp     ebx, [_display.width]
  5287.         jb      @f
  5288.         cmp     ecx, [_display.height]
  5289.         jb      @f
  5290.         xor     eax, eax
  5291.         jmp     .store
  5292. ;--------------------------------------
  5293. align 4
  5294. @@:
  5295.         mov     eax, [d_width_calc_area + ecx*4]
  5296.         add     eax, [_display.win_map]
  5297.         movzx   eax, byte[eax+ebx]        ; get value for current point
  5298. ;--------------------------------------
  5299. align 4
  5300. .store:
  5301.         mov     [esp + 32], eax
  5302.         ret
  5303. ;-----------------------------------------------------------------------------
  5304. align 4
  5305. syscall_getpixel:                       ; GetPixel
  5306.         mov     ecx, [_display.width]
  5307.         xor     edx, edx
  5308.         mov     eax, ebx
  5309.         div     ecx
  5310.         mov     ebx, edx
  5311.         xchg    eax, ebx
  5312.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5313.         call    dword [GETPIXEL]; eax - x, ebx - y
  5314.         mov     [esp + 32], ecx
  5315.         ret
  5316. ;-----------------------------------------------------------------------------
  5317. align 4
  5318. syscall_getarea:
  5319. ;eax = 36
  5320. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5321. ;ecx = [size x]*65536 + [size y]
  5322. ;edx = [start x]*65536 + [start y]
  5323.         pushad
  5324.         mov     edi, ebx
  5325.         mov     eax, edx
  5326.         shr     eax, 16
  5327.         mov     ebx, edx
  5328.         and     ebx, 0xffff
  5329.         dec     eax
  5330.         dec     ebx
  5331.      ; eax - x, ebx - y
  5332.         mov     edx, ecx
  5333.  
  5334.         shr     ecx, 16
  5335.         and     edx, 0xffff
  5336.         mov     esi, ecx
  5337.      ; ecx - size x, edx - size y
  5338.  
  5339.         mov     ebp, edx
  5340.         dec     ebp
  5341.         lea     ebp, [ebp*3]
  5342.  
  5343.         imul    ebp, esi
  5344.  
  5345.         mov     esi, ecx
  5346.         dec     esi
  5347.         lea     esi, [esi*3]
  5348.  
  5349.         add     ebp, esi
  5350.         add     ebp, edi
  5351.  
  5352.         add     ebx, edx
  5353. ;--------------------------------------
  5354. align 4
  5355. .start_y:
  5356.         push    ecx edx
  5357. ;--------------------------------------
  5358. align 4
  5359. .start_x:
  5360.         push    eax ebx ecx
  5361.         add     eax, ecx
  5362.  
  5363.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5364.         call    dword [GETPIXEL]; eax - x, ebx - y
  5365.  
  5366.         mov     [ebp], cx
  5367.         shr     ecx, 16
  5368.         mov     [ebp+2], cl
  5369.  
  5370.         pop     ecx ebx eax
  5371.         sub     ebp, 3
  5372.         dec     ecx
  5373.         jnz     .start_x
  5374.         pop     edx ecx
  5375.         dec     ebx
  5376.         dec     edx
  5377.         jnz     .start_y
  5378.         popad
  5379.         ret
  5380. ;-----------------------------------------------------------------------------
  5381. align 4
  5382. syscall_putarea_backgr:
  5383. ;eax = 25
  5384. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5385. ;ecx = [size x]*65536 + [size y]
  5386. ;edx = [start x]*65536 + [start y]
  5387.         pushad
  5388.         mov     edi, ebx
  5389.         mov     eax, edx
  5390.         shr     eax, 16
  5391.         mov     ebx, edx
  5392.         and     ebx, 0xffff
  5393.         dec     eax
  5394.         dec     ebx
  5395. ; eax - x, ebx - y
  5396.         mov     edx, ecx
  5397.         shr     ecx, 16
  5398.         and     edx, 0xffff
  5399.         mov     esi, ecx
  5400. ; ecx - size x, edx - size y
  5401.         mov     ebp, edx
  5402.         dec     ebp
  5403.         shl     ebp, 2
  5404.  
  5405.         imul    ebp, esi
  5406.  
  5407.         mov     esi, ecx
  5408.         dec     esi
  5409.         shl     esi, 2
  5410.  
  5411.         add     ebp, esi
  5412.         add     ebp, edi
  5413.  
  5414.         add     ebx, edx
  5415. ;--------------------------------------
  5416. align 4
  5417. .start_y:
  5418.         push    ecx edx
  5419. ;--------------------------------------
  5420. align 4
  5421. .start_x:
  5422.         push    eax ecx
  5423.         add     eax, ecx
  5424.  
  5425.         mov     ecx, [ebp]
  5426.         rol     ecx, 8
  5427.         test    cl, cl        ; transparensy = 0
  5428.         jz      .no_put
  5429.  
  5430.         xor     cl, cl
  5431.         ror     ecx, 8
  5432.  
  5433.         pushad
  5434.         mov     edx, [d_width_calc_area + ebx*4]
  5435.         add     edx, [_display.win_map]
  5436.         movzx   edx, byte [eax+edx]
  5437.         cmp     dl, byte 1
  5438.         jne     @f
  5439.  
  5440.         call    dword [PUTPIXEL]; eax - x, ebx - y
  5441. ;--------------------------------------
  5442. align 4
  5443. @@:
  5444.         popad
  5445. ;--------------------------------------
  5446. align 4
  5447. .no_put:
  5448.         pop     ecx eax
  5449.  
  5450.         sub     ebp, 4
  5451.         dec     ecx
  5452.         jnz     .start_x
  5453.  
  5454.         pop     edx ecx
  5455.         dec     ebx
  5456.         dec     edx
  5457.         jnz     .start_y
  5458.  
  5459.         popad
  5460.         ret
  5461. ;-----------------------------------------------------------------------------
  5462. align 4
  5463. syscall_drawline:                       ; DrawLine
  5464.  
  5465.         mov     edi, [TASK_BASE]
  5466.         movzx   eax, word[edi-twdw+WDATA.box.left]
  5467.         mov     ebp, eax
  5468.         mov     esi, [current_slot]
  5469.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5470.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  5471.         add     ebp, ebx
  5472.         shl     eax, 16
  5473.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  5474.         add     eax, ebp
  5475.         mov     ebp, ebx
  5476.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  5477.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  5478.         add     ebp, ecx
  5479.         shl     ebx, 16
  5480.         xor     edi, edi
  5481.         add     ebx, ebp
  5482.         mov     ecx, edx
  5483. ;        jmp     [draw_line]
  5484.         jmp     __sys_draw_line
  5485.  
  5486.  
  5487. align 4
  5488. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5489.  
  5490.         call    r_f_port_area
  5491.         mov     [esp+32], eax
  5492.         ret
  5493.  
  5494. align 4
  5495. syscall_threads:                        ; CreateThreads
  5496. ;
  5497. ;   ecx=thread entry point
  5498. ;   edx=thread stack pointer
  5499. ;
  5500. ; on return : eax = pid
  5501.  
  5502.         xor     ebx, ebx
  5503.         call    new_sys_threads
  5504.  
  5505.         mov     [esp+32], eax
  5506.         ret
  5507.  
  5508. align 4
  5509.  
  5510. paleholder:
  5511.         ret
  5512. ;------------------------------------------------------------------------------
  5513. align 4
  5514. calculate_fast_getting_offset_for_WinMapAddress:
  5515. ; calculate data area for fast getting offset to _WinMapAddress
  5516.         xor     eax, eax
  5517.         mov     ecx, [_display.height]
  5518.         mov     edi, d_width_calc_area
  5519.         cld
  5520. @@:
  5521.         stosd
  5522.         add     eax, [_display.width]
  5523.         dec     ecx
  5524.         jnz     @r
  5525.         ret
  5526. ;------------------------------------------------------------------------------
  5527. align 4
  5528. calculate_fast_getting_offset_for_LFB:
  5529. ; calculate data area for fast getting offset to LFB
  5530.         xor     eax, eax
  5531.         mov     ecx, [_display.height]
  5532.         mov     edi, BPSLine_calc_area
  5533.         cld
  5534. @@:
  5535.         stosd
  5536.         add     eax, [_display.lfb_pitch]
  5537.         dec     ecx
  5538.         jnz     @r
  5539.         ret
  5540. ;------------------------------------------------------------------------------
  5541. align 4
  5542. set_screen:
  5543. ; in:
  5544. ; eax - new Screen_Max_X
  5545. ; ecx - new BytesPerScanLine
  5546. ; edx - new Screen_Max_Y
  5547.  
  5548.         pushfd
  5549.         cli
  5550.  
  5551.         mov     [_display.lfb_pitch], ecx
  5552.  
  5553.         mov     [screen_workarea.right], eax
  5554.         mov     [screen_workarea.bottom], edx
  5555.  
  5556.         push    ebx
  5557.         push    esi
  5558.         push    edi
  5559.  
  5560.         pushad
  5561.  
  5562.         cmp     [do_not_touch_winmap], 1
  5563.         je      @f
  5564.  
  5565.         stdcall kernel_free, [_display.win_map]
  5566.  
  5567.         mov     eax, [_display.width]
  5568.         mul     [_display.height]
  5569.         mov     [_display.win_map_size], eax
  5570.  
  5571.         stdcall kernel_alloc, eax
  5572.         mov     [_display.win_map], eax
  5573.         test    eax, eax
  5574.         jz      .epic_fail
  5575. ; store for f.18.24
  5576.         mov     eax, [_display.width]
  5577.         mov     [display_width_standard], eax
  5578.  
  5579.         mov     eax, [_display.height]
  5580.         mov     [display_height_standard], eax
  5581. @@:
  5582.         call    calculate_fast_getting_offset_for_WinMapAddress
  5583. ; for Qemu or non standart video cards
  5584. ; Unfortunately [BytesPerScanLine] does not always
  5585. ;                             equal to [_display.width] * [ScreenBPP] / 8
  5586.         call    calculate_fast_getting_offset_for_LFB
  5587.         popad
  5588.  
  5589.         call    repos_windows
  5590.         xor     eax, eax
  5591.         xor     ebx, ebx
  5592.         mov     ecx, [_display.width]
  5593.         mov     edx, [_display.height]
  5594.         dec     ecx
  5595.         dec     edx
  5596.         call    calculatescreen
  5597.         pop     edi
  5598.         pop     esi
  5599.         pop     ebx
  5600.  
  5601.         popfd
  5602.         ret
  5603.  
  5604. .epic_fail:
  5605.         hlt                     ; Houston, we've had a problem
  5606.  
  5607. ; --------------- APM ---------------------
  5608. uglobal
  5609. apm_entry       dp      0
  5610. apm_vf          dd      0
  5611. endg
  5612.  
  5613. align 4
  5614. sys_apm:
  5615.         xor     eax, eax
  5616.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  5617.         jne     @f
  5618.         inc     eax
  5619.         or      dword [esp + 44], eax   ; error
  5620.         add     eax, 7
  5621.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  5622.         ret
  5623.  
  5624. @@:
  5625. ;       xchg    eax, ecx
  5626. ;       xchg    ebx, ecx
  5627.  
  5628.         cmp     dx, 3
  5629.         ja      @f
  5630.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  5631.         mov     eax, [apm_vf]
  5632.         mov     [esp + 32], eax
  5633.         shr     eax, 16
  5634.         mov     [esp + 28], eax
  5635.         ret
  5636.  
  5637. @@:
  5638.  
  5639.         mov     esi, [master_tab+(OS_BASE shr 20)]
  5640.         xchg    [master_tab], esi
  5641.         push    esi
  5642.         mov     edi, cr3
  5643.         mov     cr3, edi                ;flush TLB
  5644.  
  5645.         call    pword [apm_entry]       ;call APM BIOS
  5646.  
  5647.         xchg    eax, [esp]
  5648.         mov     [master_tab], eax
  5649.         mov     eax, cr3
  5650.         mov     cr3, eax
  5651.         pop     eax
  5652.  
  5653.         mov     [esp + 4 ], edi
  5654.         mov     [esp + 8], esi
  5655.         mov     [esp + 20], ebx
  5656.         mov     [esp + 24], edx
  5657.         mov     [esp + 28], ecx
  5658.         mov     [esp + 32], eax
  5659.         setc    al
  5660.         and     [esp + 44], byte 0xfe
  5661.         or      [esp + 44], al
  5662.         ret
  5663. ; -----------------------------------------
  5664.  
  5665. align 4
  5666. undefined_syscall:                      ; Undefined system call
  5667.         mov     [esp + 32], dword -1
  5668.         ret
  5669.  
  5670. align 4
  5671. ; check if given memory region lays in lower 2gb (userspace memory) or not
  5672. is_region_userspace:
  5673. ; in:  eax = base
  5674. ;      ebx = len
  5675. ; out: eax = 1 if region in userspace memory, 0 if not
  5676.         push    esi edi ecx
  5677.  
  5678.         cmp     eax, OS_BASE
  5679.         ja      @f
  5680.  
  5681.         add     eax, ebx
  5682.         cmp     eax, OS_BASE
  5683.         ja      @f
  5684.  
  5685.         mov     eax, 1
  5686.         jmp     .ret
  5687. @@:
  5688.         xor     eax, eax
  5689. .ret:
  5690.         pop     ecx edi esi
  5691.         ret
  5692.  
  5693. if ~ lang eq sp
  5694. diff16 "end of .text segment",0,$
  5695. end if
  5696.  
  5697. include "data32.inc"
  5698.  
  5699. __REV__ = __REV
  5700.  
  5701. if ~ lang eq sp
  5702. diff16 "end of kernel code",0,$
  5703. end if
  5704.