Subversion Repositories Kolibri OS

Rev

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