Subversion Repositories Kolibri OS

Rev

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

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