Subversion Repositories Kolibri OS

Rev

Rev 9592 | Rev 9608 | 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: 9605 $
  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.         mov     esi, boot_setmouse
  1042.         call    boot_log
  1043.         stdcall load_pe_driver, szPS2MDriver, 0
  1044.  
  1045. ; LOAD FIRST APPLICATION
  1046.         cmp     byte [launcher_start], 1        ; Check if starting LAUNCHER is selected on blue screen (1 = yes)
  1047.         jnz     first_app_found
  1048.  
  1049.         cli
  1050.         mov     ebp, firstapp
  1051.         call    fs_execute_from_sysdir
  1052.         test    eax, eax
  1053.         jns     first_app_found
  1054.  
  1055.         mov     esi, boot_failed
  1056.         call    boot_log
  1057.  
  1058.         mov     eax, 0xDEADBEEF      ; otherwise halt
  1059.         hlt
  1060.  
  1061. first_app_found:
  1062.  
  1063. ; START MULTITASKING
  1064. preboot_blogesc = 0       ; start immediately after bootlog
  1065.  
  1066. ; A 'All set - press ESC to start' messages if need
  1067. if preboot_blogesc
  1068.         mov     esi, boot_tasking
  1069.         call    boot_log
  1070. .bll1:
  1071.         in      al, 0x60        ; wait for ESC key press
  1072.         cmp     al, 129
  1073.         jne     .bll1
  1074. end if
  1075.  
  1076.         mov     [timer_ticks_enable], 1         ; for cd driver
  1077.  
  1078.         sti
  1079.  
  1080.         call    mtrr_validate
  1081.  
  1082.         jmp     osloop
  1083.  
  1084.  
  1085.         ; Fly :)
  1086.  
  1087. uglobal
  1088. align 4
  1089. ap_initialized  dd      0
  1090. endg
  1091.  
  1092. ap_init_high:
  1093.         mov     ax, os_stack
  1094.         mov     bx, app_data
  1095.         mov     cx, app_tls
  1096.         mov     ss, ax
  1097.         mov     ds, bx
  1098.         mov     es, bx
  1099.         mov     fs, cx
  1100.         mov     gs, bx
  1101.         xor     esp, esp
  1102.         mov     eax, sys_proc-OS_BASE+PROC.pdt_0
  1103.         mov     cr3, eax
  1104.         lock inc [ap_initialized]
  1105.         jmp     idle_loop
  1106.  
  1107.  
  1108. include 'unpacker.inc'
  1109.  
  1110. align 4
  1111. boot_log:
  1112.         pushad
  1113.  
  1114.         mov     ebx, 10*65536
  1115.         mov     bx, word [boot_y]
  1116.         add     [boot_y], dword 10
  1117.         mov     ecx, 0x80ffffff; ASCIIZ string with white color
  1118.         xor     edi, edi
  1119.         mov     edx, esi
  1120.         inc     edi
  1121.         call    dtext
  1122.  
  1123.         mov     [novesachecksum], 1000
  1124.         call    checkVga_N13
  1125.  
  1126.         popad
  1127.  
  1128.         ret
  1129.  
  1130. ;-----------------------------------------------------------------------------
  1131. ; Register ramdisk file system
  1132. register_ramdisk:
  1133.         mov     esi, boot_initramdisk
  1134.         call    boot_log
  1135.         call    ramdisk_init
  1136.         ret
  1137.  
  1138. ; in: edx -> APPDATA for OS/IDLE slot
  1139. ; in: ebx = stack base
  1140. proc setup_os_slot
  1141.         xor     eax, eax
  1142.         mov     ecx, sizeof.APPDATA/4
  1143.         mov     edi, edx
  1144.         rep stosd
  1145.  
  1146.         mov     eax, tss+0x80
  1147.         call    get_pg_addr
  1148.         inc     eax
  1149.         mov     [edx+APPDATA.io_map], eax
  1150.         mov     eax, tss+0x1080
  1151.         call    get_pg_addr
  1152.         inc     eax
  1153.         mov     [edx+APPDATA.io_map+4], eax
  1154.  
  1155.         mov     dword [edx+APPDATA.pl0_stack], ebx
  1156.         lea     edi, [ebx+RING0_STACK_SIZE]
  1157.         mov     dword [edx+APPDATA.fpu_state], edi
  1158.         mov     dword [edx+APPDATA.saved_esp0], edi
  1159.         mov     dword [edx+APPDATA.saved_esp], edi
  1160.         mov     dword [edx+APPDATA.terminate_protection], 1 ; make unkillable
  1161.  
  1162.         mov     esi, fpu_data
  1163.         mov     ecx, [xsave_area_size]
  1164.         add     ecx, 3
  1165.         shr     ecx, 2
  1166.         rep movsd
  1167.  
  1168.         lea     eax, [edx+APP_EV_OFFSET]
  1169.         mov     dword [edx+APPDATA.fd_ev], eax
  1170.         mov     dword [edx+APPDATA.bk_ev], eax
  1171.  
  1172.         lea     eax, [edx+APP_OBJ_OFFSET]
  1173.         mov     dword [edx+APPDATA.fd_obj], eax
  1174.         mov     dword [edx+APPDATA.bk_obj], eax
  1175.  
  1176.         mov     dword [edx+APPDATA.cur_dir], sysdir_path-2
  1177.  
  1178.         mov     [edx + APPDATA.process], sys_proc
  1179.  
  1180.         lea     ebx, [edx+APPDATA.list]
  1181.         lea     ecx, [sys_proc+PROC.thr_list]
  1182.         list_add_tail ebx, ecx
  1183.  
  1184.         mov     eax, edx
  1185.         shr     eax, 3
  1186.         add     eax, TASK_TABLE - (SLOT_BASE shr 3)
  1187.         mov     [eax+TASKDATA.wnd_number], dh
  1188.         mov     byte [eax+TASKDATA.pid], dh
  1189.  
  1190.         ret
  1191. endp
  1192.  
  1193. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1194. ;                                                                    ;
  1195. ;                    MAIN OS LOOP START                              ;
  1196. ;                                                                    ;
  1197. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1198. align 32
  1199. osloop:
  1200.         mov     edx, osloop_has_work?
  1201.         xor     ecx, ecx
  1202.         call    Wait_events
  1203.         xor     eax, eax
  1204.         xchg    eax, [osloop_nonperiodic_work]
  1205.         test    eax, eax
  1206.         jz      .no_periodic
  1207.  
  1208.         call    __sys_draw_pointer
  1209.         call    window_check_events
  1210.         call    mouse_check_events
  1211.         call    checkmisc
  1212.         call    checkVga_N13
  1213. ;--------------------------------------
  1214. .no_periodic:
  1215.         call    stack_handler
  1216.         call    check_fdd_motor_status
  1217.         call    check_ATAPI_device_event
  1218.         call    check_lights_state
  1219.         call    check_timers
  1220.  
  1221.         jmp     osloop
  1222. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1223. ;                                                                    ;
  1224. ;                      MAIN OS LOOP END                              ;
  1225. ;                                                                    ;
  1226. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1227. proc osloop_has_work?
  1228.         cmp     [osloop_nonperiodic_work], 0
  1229.         jnz     .yes
  1230.         call    stack_handler_has_work?
  1231.         jnz     .yes
  1232.         call    check_fdd_motor_status_has_work?
  1233.         jnz     .yes
  1234.         call    check_ATAPI_device_event_has_work?
  1235.         jnz     .yes
  1236.         call    check_lights_state_has_work?
  1237.         jnz     .yes
  1238.         call    check_timers_has_work?
  1239.         jnz     .yes
  1240. .no:
  1241.         xor     eax, eax
  1242.         ret
  1243. .yes:
  1244.         xor     eax, eax
  1245.         inc     eax
  1246.         ret
  1247. endp
  1248.  
  1249. proc wakeup_osloop
  1250.         mov     [osloop_nonperiodic_work], 1
  1251.         ret
  1252. endp
  1253.  
  1254. uglobal
  1255. align 4
  1256. osloop_nonperiodic_work dd      ?
  1257. endg
  1258.  
  1259. uglobal
  1260. align 64
  1261. idle_addr       rb      64
  1262. endg
  1263.  
  1264. idle_thread:
  1265.         sti
  1266.  
  1267. ; The following code can be executed by all CPUs in the system.
  1268. ; All other parts of the kernel do not expect multi-CPU.
  1269. ; Also, APs don't even have a stack here.
  1270. ; Beware. Don't do anything here. Anything at all.
  1271. idle_loop:
  1272.         cmp     [use_mwait_for_idle], 0
  1273.         jnz     idle_loop_mwait
  1274.  
  1275. idle_loop_hlt:
  1276.         hlt
  1277.         jmp     idle_loop_hlt
  1278.  
  1279. idle_loop_mwait:
  1280.         mov     eax, idle_addr
  1281.         xor     ecx, ecx
  1282.         xor     edx, edx
  1283.         monitor
  1284.         xor     ecx, ecx
  1285.         mov     eax, 20h        ; or 10h
  1286.         mwait
  1287.         jmp     idle_loop_mwait
  1288.  
  1289.  
  1290. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1291. ;                                                                      ;
  1292. ;                   INCLUDED SYSTEM FILES                              ;
  1293. ;                                                                      ;
  1294. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1295.  
  1296.  
  1297. include "kernel32.inc"
  1298.  
  1299.  
  1300. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1301. ;                                                                      ;
  1302. ;                       KERNEL FUNCTIONS                               ;
  1303. ;                                                                      ;
  1304. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1305.  
  1306. reserve_irqs_ports:
  1307.  
  1308.  
  1309. ; RESERVE PORTS
  1310.         mov     eax, RESERVED_PORTS
  1311.         mov     ecx, 1
  1312.  
  1313.         mov     [eax], dword 4
  1314.  
  1315.         mov     [eax+16], ecx
  1316.         mov     [eax+16+4], dword 0
  1317.         mov     [eax+16+8], dword 0x2D
  1318.  
  1319.         mov     [eax+32], ecx
  1320.         mov     [eax+32+4], dword 0x30
  1321.         mov     [eax+32+8], dword 0x4D
  1322.  
  1323.         mov     [eax+48], ecx
  1324.         mov     [eax+48+4], dword 0x50
  1325.         mov     [eax+48+8], dword 0xDF
  1326.  
  1327.         mov     [eax+64], ecx
  1328.         mov     [eax+64+4], dword 0xE5
  1329.         mov     [eax+64+8], dword 0xFF
  1330.  
  1331.         ret
  1332.  
  1333.  
  1334. iglobal
  1335.   process_number dd 0x2
  1336. endg
  1337.  
  1338. set_variables:
  1339.  
  1340.         mov     ecx, 0x16                    ; flush port 0x60
  1341. .fl60:
  1342.         in      al, 0x60
  1343.         loop    .fl60
  1344.         push    eax
  1345.  
  1346.         mov     ax, [BOOT.y_res]
  1347.         shr     ax, 1
  1348.         shl     eax, 16
  1349.         mov     ax, [BOOT.x_res]
  1350.         shr     ax, 1
  1351.         mov     [MOUSE_X], eax
  1352.         call    wakeup_osloop
  1353.  
  1354.         xor     eax, eax
  1355.         mov     [BTN_ADDR], dword BUTTON_INFO ; address of button list
  1356.  
  1357.         mov     byte [KEY_COUNT], al              ; keyboard buffer
  1358.         mov     byte [BTN_COUNT], al              ; button buffer
  1359. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  1360.  
  1361.         pop     eax
  1362.         ret
  1363. ;-----------------------------------------------------------------------------
  1364.  
  1365. align 4
  1366. display_number:
  1367. ; add check pointers
  1368.         test    bl, bl
  1369.         jz      @f
  1370.         stdcall is_region_userspace, ecx, 1
  1371.         jz      @f
  1372.         ret
  1373. @@:
  1374.         test    esi, 0x08000000
  1375.         jz      @f
  1376.         stdcall is_region_userspace, edi, 1
  1377.         jz      @f
  1378.         ret
  1379. @@:
  1380. ;It is not optimization
  1381.         mov     eax, ebx
  1382.         mov     ebx, ecx
  1383.         mov     ecx, edx
  1384.         mov     edx, esi
  1385.         mov     esi, edi
  1386. ; eax = print type, al=0 -> ebx is number
  1387. ;                   al=1 -> ebx is pointer
  1388. ;                   ah=0 -> display decimal
  1389. ;                   ah=1 -> display hexadecimal
  1390. ;                   ah=2 -> display binary
  1391. ;                   eax bits 16-21 = number of digits to display (0-32)
  1392. ;                   eax bits 22-31 = reserved
  1393. ;
  1394. ; ebx = number or pointer
  1395. ; ecx = x shl 16 + y
  1396. ; edx = color
  1397.         xor     edi, edi
  1398. display_number_force:
  1399.         push    eax
  1400.         and     eax, 0x3fffffff
  1401.         cmp     eax, 0xffff     ; length > 0 ?
  1402.         pop     eax
  1403.         jge     cont_displ
  1404.         ret
  1405.    cont_displ:
  1406.         push    eax
  1407.         and     eax, 0x3fffffff
  1408.         cmp     eax, 61*0x10000  ; length <= 60 ?
  1409.         pop     eax
  1410.         jb      cont_displ2
  1411.         ret
  1412.    cont_displ2:
  1413.  
  1414.         pushad
  1415.  
  1416.         cmp     al, 1            ; ecx is a pointer ?
  1417.         jne     displnl1
  1418.         mov     ebp, ebx
  1419.         add     ebp, 4
  1420.         mov     ebp, [ebp+std_application_base_address]
  1421.         mov     ebx, [ebx+std_application_base_address]
  1422.  displnl1:
  1423.         sub     esp, 64
  1424.  
  1425.         test    ah, ah            ; DECIMAL
  1426.         jnz     no_display_desnum
  1427.         shr     eax, 16
  1428.         and     eax, 0xC03f
  1429. ;     and   eax,0x3f
  1430.         push    eax
  1431.         and     eax, 0x3f
  1432.         mov     edi, esp
  1433.         add     edi, 4+64-1
  1434.         mov     ecx, eax
  1435.         mov     eax, ebx
  1436.         mov     ebx, 10
  1437.  d_desnum:
  1438.         xor     edx, edx
  1439.         call    division_64_bits
  1440.         div     ebx
  1441.         add     dl, 48
  1442.         mov     [edi], dl
  1443.         dec     edi
  1444.         loop    d_desnum
  1445.         pop     eax
  1446.         call    normalize_number
  1447.         call    draw_num_text
  1448.         add     esp, 64
  1449.         popad
  1450.         ret
  1451.    no_display_desnum:
  1452.  
  1453.         cmp     ah, 0x01         ; HEXADECIMAL
  1454.         jne     no_display_hexnum
  1455.         shr     eax, 16
  1456.         and     eax, 0xC03f
  1457. ;     and   eax,0x3f
  1458.         push    eax
  1459.         and     eax, 0x3f
  1460.         mov     edi, esp
  1461.         add     edi, 4+64-1
  1462.         mov     ecx, eax
  1463.         mov     eax, ebx
  1464.         mov     ebx, 16
  1465.    d_hexnum:
  1466.         xor     edx, edx
  1467.         call    division_64_bits
  1468.         div     ebx
  1469.    hexletters = __fdo_hexdigits
  1470.         add     edx, hexletters
  1471.         mov     dl, [edx]
  1472.         mov     [edi], dl
  1473.         dec     edi
  1474.         loop    d_hexnum
  1475.         pop     eax
  1476.         call    normalize_number
  1477.         call    draw_num_text
  1478.         add     esp, 64
  1479.         popad
  1480.         ret
  1481.    no_display_hexnum:
  1482.  
  1483.         cmp     ah, 0x02         ; BINARY
  1484.         jne     no_display_binnum
  1485.         shr     eax, 16
  1486.         and     eax, 0xC03f
  1487. ;     and   eax,0x3f
  1488.         push    eax
  1489.         and     eax, 0x3f
  1490.         mov     edi, esp
  1491.         add     edi, 4+64-1
  1492.         mov     ecx, eax
  1493.         mov     eax, ebx
  1494.         mov     ebx, 2
  1495.    d_binnum:
  1496.         xor     edx, edx
  1497.         call    division_64_bits
  1498.         div     ebx
  1499.         add     dl, 48
  1500.         mov     [edi], dl
  1501.         dec     edi
  1502.         loop    d_binnum
  1503.         pop     eax
  1504.         call    normalize_number
  1505.         call    draw_num_text
  1506.         add     esp, 64
  1507.         popad
  1508.         ret
  1509.    no_display_binnum:
  1510.  
  1511.         add     esp, 64
  1512.         popad
  1513.         ret
  1514.  
  1515. normalize_number:
  1516.         test    ah, 0x80
  1517.         jz      .continue
  1518.         mov     ecx, 48
  1519.         and     eax, 0x3f
  1520. @@:
  1521.         inc     edi
  1522.         cmp     [edi], cl
  1523.         jne     .continue
  1524.         dec     eax
  1525.         cmp     eax, 1
  1526.         ja      @r
  1527.         mov     al, 1
  1528. .continue:
  1529.         and     eax, 0x3f
  1530.         ret
  1531.  
  1532. division_64_bits:
  1533.         test    [esp+1+4], byte 0x40
  1534.         jz      .continue
  1535.         push    eax
  1536.         mov     eax, ebp
  1537.         div     ebx
  1538.         mov     ebp, eax
  1539.         pop     eax
  1540. .continue:
  1541.         ret
  1542.  
  1543. draw_num_text:
  1544.         mov     esi, eax
  1545.         mov     edx, 64+4
  1546.         sub     edx, eax
  1547.         add     edx, esp
  1548.         mov     ebx, [esp+64+32-8+4]
  1549. ; add window start x & y
  1550.         ;mov     ecx, [TASK_BASE]
  1551.  
  1552.         mov     edi, [current_slot_idx]
  1553.         mov     ecx, edi
  1554.         shl     edi, 8
  1555.         shl     ecx, 5
  1556.  
  1557.         mov     eax, [ecx+window_data+WDATA.box.left]
  1558.         add     eax, [edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1559.         shl     eax, 16
  1560.         add     eax, [ecx+window_data+WDATA.box.top]
  1561.         add     eax, [edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1562.         add     ebx, eax
  1563.         mov     ecx, [esp+64+32-12+4]
  1564.         mov     eax, [esp+64+8]         ; background color (if given)
  1565.         mov     edi, [esp+64+4]
  1566.         and     ecx, 5FFFFFFFh
  1567.         bt      ecx, 27
  1568.         jnc     @f
  1569.         mov     edi, eax
  1570. @@:
  1571.         jmp     dtext
  1572. ;-----------------------------------------------------------------------------
  1573. iglobal
  1574. midi_base dw 0
  1575. endg
  1576. ;-----------------------------------------------------------------------------
  1577. align 4
  1578. sys_setup:
  1579. ;  1 = roland mpu midi base , base io address
  1580. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1581. ;  3 = not used
  1582. ;  4 = not used
  1583. ;  5 = system language, 1eng 2fi 3ger 4rus
  1584. ;  6 = not used
  1585. ;  7 = not used
  1586. ;  8 = not used
  1587. ;  9 = not used
  1588. ; 10 = not used
  1589. ; 11 = enable lba read
  1590. ; 12 = enable pci access
  1591. ;-----------------------------------------------------------------------------
  1592.         and     [esp+32], dword 0
  1593. ; F.21.1 - set MPU MIDI base port
  1594.         dec     ebx
  1595.         jnz     @f
  1596.  
  1597.         cmp     ecx, 0x100
  1598.         jb      @f
  1599.  
  1600.         mov     esi, 65535
  1601.         cmp     esi, ecx
  1602.         jb      @f
  1603.  
  1604.         mov     [midi_base], cx
  1605.         mov     word [mididp], cx
  1606.         inc     cx
  1607.         mov     word [midisp], cx
  1608.         ret
  1609. ;--------------------------------------
  1610. @@:
  1611. ; F.21.2 - set keyboard layout
  1612.         dec     ebx
  1613.         jnz     @f
  1614.  
  1615.         ;mov     edi, [TASK_BASE]
  1616.         ;mov     eax, [edi+TASKDATA.mem_start]
  1617.         mov     edi, [current_slot]
  1618.         mov     eax, [edi+APPDATA.mem_start]
  1619.         add     eax, edx
  1620. ; 1 = normal layout
  1621.         dec     ecx
  1622.         jnz     .shift
  1623.  
  1624.         mov     ebx, keymap
  1625.         mov     ecx, 128
  1626.         call    memmove
  1627.         ret
  1628. ;--------------------------------------
  1629. .shift:
  1630. ; 2 = layout at pressed Shift
  1631.         dec     ecx
  1632.         jnz     .alt
  1633.  
  1634.         mov     ebx, keymap_shift
  1635.         mov     ecx, 128
  1636.         call    memmove
  1637.         ret
  1638. ;--------------------------------------
  1639. .alt:
  1640. ; 3 = layout at pressed Alt
  1641.         dec     ecx
  1642.         jnz     .country
  1643.  
  1644.         mov     ebx, keymap_alt
  1645.         mov     ecx, 128
  1646.         call    memmove
  1647.         ret
  1648. ;--------------------------------------
  1649. .country:
  1650. ; country identifier
  1651.         sub     ecx, 6
  1652.         jnz     .error
  1653.  
  1654.         mov     word [keyboard], dx
  1655.         ret
  1656. ;--------------------------------------
  1657. @@:
  1658. ; F.21.5 - set system language
  1659.         sub     ebx, 3
  1660.         jnz     @f
  1661.  
  1662.         mov     [syslang], ecx
  1663.         ret
  1664. ;--------------------------------------
  1665. @@:
  1666. ; F.21.11 - enable/disable low-level access to HD
  1667.         and     ecx, 1
  1668.         sub     ebx, 6
  1669.         jnz     @f
  1670.  
  1671.         mov     [lba_read_enabled], ecx
  1672.         ret
  1673. ;--------------------------------------
  1674. @@:
  1675. ; F.21.12 - enable/disable low-level access to PCI
  1676.         dec     ebx
  1677.         jnz     .error
  1678.  
  1679.         mov     [pci_access_enabled], ecx
  1680.         ret
  1681. ;--------------------------------------
  1682. .error:
  1683.         or      [esp+32], dword -1
  1684.         ret
  1685. ;-----------------------------------------------------------------------------
  1686. align 4
  1687. sys_getsetup:
  1688. ;  1 = roland mpu midi base , base io address
  1689. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1690. ;  3 = not used
  1691. ;  4 = not used
  1692. ;  5 = system language, 1eng 2fi 3ger 4rus
  1693. ;  6 = not used
  1694. ;  7 = not used
  1695. ;  8 = not used
  1696. ;  9 = get hs timer tic
  1697. ; 10 = not used
  1698. ; 11 = get the state "lba read"
  1699. ; 12 = get the state "pci access"
  1700. ;-----------------------------------------------------------------------------
  1701. ; F.26.1 - get MPU MIDI base port
  1702.         dec     ebx
  1703.         jnz     @f
  1704.  
  1705.         movzx   eax, [midi_base]
  1706.         mov     [esp+32], eax
  1707.         ret
  1708. ;--------------------------------------
  1709. @@:
  1710. ; F.26.2 - get keyboard layout
  1711.         dec     ebx
  1712.         jnz     @f
  1713.  
  1714.         ;mov     edi, [TASK_BASE]
  1715.         ;mov     ebx, [edi+TASKDATA.mem_start]
  1716.         mov     edi, [current_slot]
  1717.         mov     ebx, [edi+APPDATA.mem_start]
  1718.         add     ebx, edx
  1719. ; 1 = normal layout
  1720.         dec     ecx
  1721.         jnz     .shift
  1722.  
  1723.         ; if given memory address belongs to kernel then error
  1724.         stdcall is_region_userspace, ebx, 128
  1725.         jnz     .addr_error
  1726.  
  1727.         mov     eax, keymap
  1728.         mov     ecx, 128
  1729.         call    memmove
  1730.         ret
  1731. ;--------------------------------------
  1732. .shift:
  1733. ; 2 = layout with pressed Shift
  1734.         dec     ecx
  1735.         jnz     .alt
  1736.  
  1737.         stdcall is_region_userspace, ebx, 128
  1738.         jnz     .addr_error
  1739.  
  1740.         mov     eax, keymap_shift
  1741.         mov     ecx, 128
  1742.         call    memmove
  1743.         ret
  1744. ;--------------------------------------
  1745. .alt:
  1746. ; 3 = layout with pressed Alt
  1747.         dec     ecx
  1748.         jne     .country
  1749.  
  1750.         stdcall is_region_userspace, ebx, 128
  1751.         jnz     .addr_error
  1752.  
  1753.         mov     eax, keymap_alt
  1754.         mov     ecx, 128
  1755.         call    memmove
  1756.         ret
  1757. ;--------------------------------------
  1758. .country:
  1759. ; 9 = country identifier
  1760.         sub     ecx, 6
  1761.         jnz     .error
  1762.  
  1763.         movzx   eax, word [keyboard]
  1764.         mov     [esp+32], eax
  1765.         ret
  1766.  
  1767. .addr_error:    ; if given memory address is illegal
  1768.         mov     dword [esp+32], -1
  1769.         ret        
  1770. ;--------------------------------------
  1771. @@:
  1772. ; F.26.5 - get system language
  1773.         sub     ebx, 3
  1774.         jnz     @f
  1775.  
  1776.         mov     eax, [syslang]
  1777.         mov     [esp+32], eax
  1778.         ret
  1779. ;--------------------------------------
  1780. @@:
  1781. ; F.26.9 - get the value of the time counter
  1782.         sub     ebx, 4
  1783.         jnz     @f
  1784.  
  1785.         mov     eax, [timer_ticks]
  1786.         mov     [esp+32], eax
  1787.         ret
  1788. ;--------------------------------------
  1789. @@:
  1790. ; F.26.10 - get the time from kernel launch in nanoseconds
  1791.         dec     ebx
  1792.         jnz     @f
  1793.  
  1794.         call    get_clock_ns
  1795.         mov     [esp+24], edx
  1796.         mov     [esp+32], eax
  1797.         ret
  1798. ;--------------------------------------
  1799. @@:
  1800. ; F.26.11 - Find out whether low-level HD access is enabled
  1801.         dec     ebx
  1802.         jnz     @f
  1803.  
  1804.         mov     eax, [lba_read_enabled]
  1805.         mov     [esp+32], eax
  1806.         ret
  1807. ;--------------------------------------
  1808. @@:
  1809. ; F.26.12 - Find out whether low-level PCI access is enabled
  1810.         dec     ebx
  1811.         jnz     .error
  1812.  
  1813.         mov     eax, [pci_access_enabled]
  1814.         mov     [esp+32], eax
  1815.         ret
  1816. ;--------------------------------------
  1817. .error:
  1818.         or      [esp+32], dword -1
  1819.         ret
  1820. ;-----------------------------------------------------------------------------
  1821. get_timer_ticks:
  1822.         mov     eax, [timer_ticks]
  1823.         ret
  1824. ;-----------------------------------------------------------------------------
  1825.  
  1826. is_input:
  1827.         push    edx
  1828.         mov     dx, word [midisp]
  1829.         in      al, dx
  1830.         and     al, 0x80
  1831.         pop     edx
  1832.         ret
  1833.  
  1834. is_output:
  1835.         push    edx
  1836.         mov     dx, word [midisp]
  1837.         in      al, dx
  1838.         and     al, 0x40
  1839.         pop     edx
  1840.         ret
  1841.  
  1842. get_mpu_in:
  1843.         push    edx
  1844.         mov     dx, word [mididp]
  1845.         in      al, dx
  1846.         pop     edx
  1847.         ret
  1848.  
  1849. put_mpu_out:
  1850.         push    edx
  1851.         mov     dx, word [mididp]
  1852.         out     dx, al
  1853.         pop     edx
  1854.         ret
  1855.  
  1856. align 4
  1857. sys_midi:
  1858.         cmp     [mididp], 0
  1859.         jnz     sm0
  1860.         mov     [esp+36], dword 1
  1861.         ret
  1862. sm0:
  1863.         and     [esp+36], dword 0
  1864.         dec     ebx
  1865.         jnz     smn1
  1866.  ;    call setuart
  1867. su1:
  1868.         call    is_output
  1869.         test    al, al
  1870.         jnz     su1
  1871.         mov     dx, word [midisp]
  1872.         mov     al, 0xff
  1873.         out     dx, al
  1874. su2:
  1875.         mov     dx, word [midisp]
  1876.         mov     al, 0xff
  1877.         out     dx, al
  1878.         call    is_input
  1879.         test    al, al
  1880.         jnz     su2
  1881.         call    get_mpu_in
  1882.         cmp     al, 0xfe
  1883.         jnz     su2
  1884. su3:
  1885.         call    is_output
  1886.         test    al, al
  1887.         jnz     su3
  1888.         mov     dx, word [midisp]
  1889.         mov     al, 0x3f
  1890.         out     dx, al
  1891.         ret
  1892. smn1:
  1893.         dec     ebx
  1894.         jnz     smn2
  1895. sm10:
  1896.         call    get_mpu_in
  1897.         call    is_output
  1898.         test    al, al
  1899.         jnz     sm10
  1900.         mov     al, bl
  1901.         call    put_mpu_out
  1902.         smn2:
  1903.         ret
  1904.  
  1905. detect_devices:
  1906. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1907. ;include 'detect/commouse.inc'
  1908. ;include 'detect/ps2mouse.inc'
  1909. ;include 'detect/dev_fd.inc'
  1910. ;include 'detect/dev_hdcd.inc'
  1911. ;include 'detect/sear_par.inc'
  1912. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1913.         ret
  1914.  
  1915. sys_end:
  1916. ;--------------------------------------
  1917.         cmp     [_display.select_cursor], 0
  1918.         je      @f
  1919. ; restore default cursor before killing
  1920.         pusha
  1921.         mov     ecx, [current_slot]
  1922.         call    restore_default_cursor_before_killing
  1923.         popa
  1924. @@:
  1925. ;--------------------------------------
  1926. ; kill all sockets this process owns
  1927.         pusha
  1928.         mov     edx, [TASK_BASE]
  1929.         mov     edx, [edx+TASKDATA.pid]
  1930.         call    socket_process_end
  1931.         popa
  1932. ;--------------------------------------
  1933.         mov     ecx, [current_slot]
  1934.         mov     eax, [ecx+APPDATA.tls_base]
  1935.         test    eax, eax
  1936.         jz      @F
  1937.  
  1938.         stdcall user_free, eax
  1939. @@:
  1940.  
  1941.         mov     eax, [TASK_BASE]
  1942.         mov     [eax+TASKDATA.state], TSTATE_ZOMBIE
  1943.         call    wakeup_osloop
  1944.  
  1945. .waitterm:            ; wait here for termination
  1946.         call    change_task
  1947.         jmp     .waitterm
  1948. ;------------------------------------------------------------------------------
  1949. align 4
  1950. restore_default_cursor_before_killing:
  1951.         pushfd
  1952.         cli
  1953.         mov     eax, [def_cursor]
  1954.         mov     [ecx+APPDATA.cursor], eax
  1955.  
  1956.         movzx   eax, word [MOUSE_Y]
  1957.         movzx   ebx, word [MOUSE_X]
  1958.         mov     eax, [d_width_calc_area + eax*4]
  1959.  
  1960.         add     eax, [_display.win_map]
  1961.         movzx   edx, byte [ebx+eax]
  1962.         shl     edx, 8
  1963.         mov     esi, [edx+SLOT_BASE+APPDATA.cursor]
  1964.  
  1965.         cmp     esi, [current_cursor]
  1966.         je      @f
  1967.  
  1968.         push    esi
  1969.         call    [_display.select_cursor]
  1970.         mov     [current_cursor], esi
  1971. @@:
  1972.         mov     [redrawmouse_unconditional], 1
  1973.         call    wakeup_osloop
  1974.         popfd
  1975.         ret
  1976. ;------------------------------------------------------------------------------
  1977. iglobal
  1978. align 4
  1979. sys_system_table:
  1980.         dd      sysfn_deactivate        ; 1 = deactivate window
  1981.         dd      sysfn_terminate         ; 2 = terminate thread
  1982.         dd      sysfn_activate          ; 3 = activate window
  1983.         dd      sysfn_getidletime       ; 4 = get idle time
  1984.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1985.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1986.         dd      sysfn_getactive         ; 7 = get active window
  1987.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1988.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1989.         dd      sysfn_minimize          ; 10 = minimize window
  1990.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1991.         dd      undefined_syscall       ; 12 = get last pressed key. function removed. sysfn_lastkey
  1992.         dd      sysfn_getversion        ; 13 = get kernel version
  1993.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1994.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1995.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1996.         dd      sysfn_getallmem         ; 17 = get total memory size
  1997.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1998.                                         ;                 instead of slot
  1999.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  2000.         dd      sysfn_meminfo           ; 20 = get extended memory info
  2001.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  2002.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  2003.         dd      sysfn_min_windows       ; 23 = minimize all windows
  2004.         dd      sysfn_set_screen_sizes  ; 24 = set screen sizes for Vesa
  2005.  
  2006.         dd      sysfn_zmodif            ; 25 = get/set window z modifier  ;Fantomer
  2007. sysfn_num = ($ - sys_system_table)/4
  2008. endg
  2009. ;------------------------------------------------------------------------------
  2010. sys_system:
  2011.         dec     ebx
  2012.         cmp     ebx, sysfn_num
  2013.         jae     @f
  2014.         jmp     dword [sys_system_table + ebx*4]
  2015. @@:
  2016.         ret
  2017. ;------------------------------------------------------------------------------
  2018. sysfn_shutdown:          ; 18.9 = system shutdown
  2019.         cmp     ecx, SYSTEM_SHUTDOWN
  2020.         jl      exit_for_anyone
  2021.         cmp     ecx, SYSTEM_RESTART
  2022.         jg      exit_for_anyone
  2023.         mov     [BOOT.shutdown_type], cl
  2024.  
  2025.         mov     eax, [thread_count]
  2026.         mov     [SYS_SHUTDOWN], al
  2027.         mov     [shutdown_processes], eax
  2028.         call    wakeup_osloop
  2029.         and     dword [esp+32], 0
  2030.  exit_for_anyone:
  2031.         ret
  2032.   uglobal
  2033.    shutdown_processes:
  2034.                        dd 0x0
  2035.   endg
  2036. ;------------------------------------------------------------------------------
  2037. ; in: eax -- APPDATA ptr
  2038. ; out: Z/z -- is/not kernel thread
  2039. is_kernel_thread:
  2040.         mov     eax, [eax+APPDATA.process]
  2041.         cmp     eax, [SLOT_BASE+2*sizeof.APPDATA+APPDATA.process]       ; OS
  2042.         ret
  2043. ;------------------------------------------------------------------------------
  2044. sysfn_terminate:        ; 18.2 = TERMINATE
  2045.         push    ecx
  2046.         cmp     ecx, 2
  2047.         jb      noprocessterminate
  2048.         mov     edx, [thread_count]
  2049.         cmp     ecx, edx
  2050.         ja      noprocessterminate
  2051.         mov     eax, [thread_count]
  2052.         shl     ecx, BSF sizeof.TASKDATA
  2053.         mov     edx, [ecx+TASK_TABLE+TASKDATA.pid]
  2054.         add     ecx, TASK_TABLE+TASKDATA.state
  2055.         cmp     byte [ecx], TSTATE_FREE
  2056.         jz      noprocessterminate
  2057.         push    eax
  2058.         lea     eax, [(ecx-(TASK_TABLE and 1FFFFFFFh)-TASKDATA.state)*8+SLOT_BASE]
  2059.         call    is_kernel_thread
  2060.         pop     eax
  2061.         jz      noprocessterminate
  2062.         push    ecx edx
  2063.         lea     edx, [(ecx-(TASK_TABLE and 1FFFFFFFh)-TASKDATA.state)*8+SLOT_BASE]
  2064.         call    request_terminate
  2065.         pop     edx ecx
  2066.         test    eax, eax
  2067.         jz      noprocessterminate
  2068. ;--------------------------------------
  2069. ; terminate all network sockets it used
  2070.         pusha
  2071.         mov     eax, edx
  2072.         call    socket_process_end
  2073.         popa
  2074. ;--------------------------------------
  2075.         cmp     [_display.select_cursor], 0
  2076.         je      .restore_end
  2077. ; restore default cursor before killing
  2078.         pusha
  2079.         mov     ecx, [esp+32]
  2080.         shl     ecx, 8
  2081.         add     ecx, SLOT_BASE
  2082.         mov     eax, [def_cursor]
  2083.         cmp     [ecx+APPDATA.cursor], eax
  2084.         je      @f
  2085.         call    restore_default_cursor_before_killing
  2086. @@:
  2087.         popa
  2088. .restore_end:
  2089. ;--------------------------------------
  2090.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2091.         mov     [ecx], byte 3; clear possible i40's
  2092.         call    wakeup_osloop
  2093.      ;call MEM_Heap_UnLock
  2094.  
  2095.         cmp     edx, [application_table_owner]; clear app table stat
  2096.         jne     noatsc
  2097.         call    unlock_application_table
  2098. noatsc:
  2099. noprocessterminate:
  2100.         add     esp, 4
  2101.         ret
  2102. ;------------------------------------------------------------------------------
  2103. sysfn_terminate2:
  2104. ;lock application_table_status mutex
  2105. .table_status:
  2106.         call    lock_application_table
  2107.         mov     eax, ecx
  2108.         call    pid_to_slot
  2109.         test    eax, eax
  2110.         jz      .not_found
  2111.         mov     ecx, eax
  2112.         cli
  2113.         call    sysfn_terminate
  2114.         call    unlock_application_table
  2115.         sti
  2116.         and     dword [esp+32], 0
  2117.         ret
  2118. .not_found:
  2119.         call    unlock_application_table
  2120.         or      dword [esp+32], -1
  2121.         ret
  2122. ;------------------------------------------------------------------------------
  2123. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2124.         cmp     ecx, 2
  2125.         jb      .nowindowdeactivate
  2126.         cmp     ecx, [thread_count]
  2127.         ja      .nowindowdeactivate
  2128.  
  2129.         movzx   esi, word [WIN_STACK + ecx*2]
  2130.         cmp     esi, 1
  2131.         je      .nowindowdeactivate ; already deactive
  2132.  
  2133.         mov     edi, ecx
  2134.         shl     edi, 5
  2135.         add     edi, window_data
  2136.         movzx   esi, word [WIN_STACK + ecx * 2]
  2137.         lea     esi, [WIN_POS + esi * 2]
  2138.         call    window._.window_deactivate
  2139.         call    syscall_display_settings.calculateScreen
  2140.         call    syscall_display_settings.redrawScreen
  2141. .nowindowdeactivate:
  2142.         ret
  2143. ;------------------------------------------------------------------------------
  2144. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2145.         cmp     ecx, 2
  2146.         jb      .nowindowactivate
  2147.         cmp     ecx, [thread_count]
  2148.         ja      .nowindowactivate
  2149. ;-------------------------------------
  2150. @@:
  2151. ; If the window is captured and moved by the user,
  2152. ; then you can't change the position in window stack!!!
  2153.         mov     al, [mouse.active_sys_window.action]
  2154.         and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
  2155.         test    al, al
  2156.         jz      @f
  2157.         call    change_task
  2158.         jmp     @b
  2159. @@:
  2160. ;-------------------------------------
  2161.         mov     [window_minimize], 2; restore window if minimized
  2162.         call    wakeup_osloop
  2163.  
  2164.         movzx   esi, word [WIN_STACK + ecx*2]
  2165.         cmp     esi, [thread_count]
  2166.         je      .nowindowactivate; already active
  2167.  
  2168.         mov     edi, ecx
  2169.         shl     edi, 5
  2170.         add     edi, window_data
  2171.         movzx   esi, word [WIN_STACK + ecx * 2]
  2172.         lea     esi, [WIN_POS + esi * 2]
  2173.         call    waredraw
  2174. .nowindowactivate:
  2175.         ret
  2176. ;------------------------------------------------------------------------------
  2177. align 4
  2178. sysfn_zmodif:
  2179. ;18,25,1 - get z_modif
  2180. ;18,25,2 - set z_modif
  2181. ;edx = -1(for current task) or TID
  2182. ;esi(for 2) = new value z_modif
  2183. ;return:
  2184. ;1:   eax = z_modif
  2185. ;2: eax=0(fail),1(success) for set z_modif
  2186.  
  2187.         cmp     edx, -1
  2188.         jne     @f
  2189.         mov     edx, [current_slot_idx]
  2190.      @@:
  2191.         cmp     edx, [thread_count]
  2192.         ja      .fail
  2193.         cmp     edx, 1
  2194.         je      .fail
  2195.  
  2196.         mov     eax, edx
  2197.         shl     edx, 5
  2198.  
  2199.         cmp     [edx + TASK_TABLE + TASKDATA.state], TSTATE_FREE
  2200.         je      .fail
  2201.  
  2202.         cmp     ecx, 1
  2203.         jnz     .set_zmod
  2204.  
  2205.         mov     al, [edx + window_data + WDATA.z_modif]
  2206.         jmp     .exit
  2207.  
  2208. .set_zmod:
  2209.         cmp     ecx, 2
  2210.         jnz     .fail
  2211.  
  2212.         mov     ebx, esi
  2213.         mov     esi, eax
  2214.  
  2215.         cmp     bl, ZPOS_ALWAYS_TOP
  2216.         jg      .fail
  2217.  
  2218.         mov     [edx + window_data + WDATA.z_modif], bl
  2219.  
  2220.         mov     eax, [edx + window_data + WDATA.box.left]
  2221.         mov     ebx, [edx + window_data + WDATA.box.top]
  2222.         mov     ecx, [edx + window_data + WDATA.box.width]
  2223.         mov     edx, [edx + window_data + WDATA.box.height]
  2224.         add     ecx, eax
  2225.         add     edx, ebx
  2226.         call    window._.set_screen
  2227.         call    window._.set_top_wnd
  2228.         call    window._.redraw_top_wnd
  2229.  
  2230.         shl     esi, 5
  2231.         mov     [esi + window_data + WDATA.fl_redraw], 1
  2232.  
  2233.  
  2234.         mov     eax, 1
  2235.         jmp     .exit
  2236. .fail:
  2237.         xor     eax, eax
  2238. .exit:
  2239.         mov     [esp+32], eax
  2240.         ret
  2241.  
  2242. ;------------------------------------------------------------------------------
  2243. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2244.         ;mov     eax, [TASK_TABLE+32+TASKDATA.cpu_usage]
  2245.         mov     eax, [SLOT_BASE+APPDATA.cpu_usage]
  2246.         mov     [esp+32], eax
  2247.         ret
  2248. ;------------------------------------------------------------------------------
  2249. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2250.         mov     eax, dword [cpu_freq]
  2251.         mov     [esp+32], eax
  2252.         ret
  2253. ;------------------------------------------------------------------------------
  2254. get_cpu_freq:
  2255.         mov     eax, dword [cpu_freq]
  2256.         mov     edx, dword [cpu_freq+4]
  2257.         ret
  2258. ;  SAVE ramdisk to /hd/1/kolibri.img
  2259. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2260.    include 'blkdev/rdsave.inc'
  2261. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2262. ;------------------------------------------------------------------------------
  2263. align 4
  2264. sysfn_getactive:        ; 18.7 = get active window
  2265.         mov     eax, [thread_count]
  2266.         movzx   eax, word [WIN_POS + eax*2]
  2267.         mov     [esp+32], eax
  2268.         ret
  2269. ;------------------------------------------------------------------------------
  2270. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2271. ;     cmp  ecx,1
  2272.         dec     ecx
  2273.         jnz     nogetsoundflag
  2274.         movzx   eax, byte [sound_flag]; get sound_flag
  2275.         mov     [esp+32], eax
  2276.         ret
  2277.  nogetsoundflag:
  2278. ;     cmp  ecx,2
  2279.         dec     ecx
  2280.         jnz     nosoundflag
  2281.         xor     byte [sound_flag], 1
  2282.  nosoundflag:
  2283.         ret
  2284. ;------------------------------------------------------------------------------
  2285. sysfn_minimize:         ; 18.10 = minimize window
  2286.         mov     [window_minimize], 1
  2287.         call    wakeup_osloop
  2288.         ret
  2289. ;------------------------------------------------------------------------------
  2290. align 4
  2291. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2292.         dec     ecx
  2293.         jnz     .exit
  2294. .small_table:
  2295.         stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
  2296.         jnz     .exit
  2297.         mov     edi, edx
  2298.         mov     esi, DRIVE_DATA
  2299.         mov     ecx, DRIVE_DATA_SIZE ;10
  2300.         cld
  2301.         rep movsb
  2302. .exit:
  2303.         ret
  2304. ;------------------------------------------------------------------------------
  2305. ;sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2306. ;        and     dword [esp+32], 0
  2307. ;        ret
  2308. ;------------------------------------------------------------------------------
  2309. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2310.         ; if given memory address belongs to kernel then error
  2311.         stdcall is_region_userspace, ecx, version_end-version_inf
  2312.         jnz     .addr_error
  2313.  
  2314.         mov     edi, ecx
  2315.         mov     esi, version_inf
  2316.         mov     ecx, version_end-version_inf
  2317.         rep movsb
  2318.         ret
  2319. .addr_error:    ; if given memory address is illegal
  2320.         mov     dword [esp+32], -1
  2321.         ret  
  2322. ;------------------------------------------------------------------------------
  2323. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2324.      ;wait retrace functions
  2325.  sys_wait_retrace:
  2326.         mov     edx, 0x3da
  2327.  WaitRetrace_loop:
  2328.         in      al, dx
  2329.         test    al, 1000b
  2330.         jz      WaitRetrace_loop
  2331.         and     [esp+32], dword 0
  2332.         ret
  2333. ;------------------------------------------------------------------------------
  2334. align 4
  2335. sysfn_centermouse:      ; 18.15 = mouse centered
  2336.         mov     eax, [_display.width]
  2337.         shr     eax, 1
  2338.         mov     [MOUSE_X], ax
  2339.         mov     eax, [_display.height]
  2340.         shr     eax, 1
  2341.         mov     [MOUSE_Y], ax
  2342.         call    wakeup_osloop
  2343.         xor     eax, eax
  2344.         and     [esp+32], eax
  2345.         ret
  2346. ;------------------------------------------------------------------------------
  2347. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2348.         cmp     ecx, 8
  2349.         jnc     @f
  2350.         jmp     dword [.table+ecx*4]
  2351. .get_mouse_acceleration:
  2352.         xor     eax, eax
  2353.         mov     ax, [mouse_speed_factor]
  2354.         mov     [esp+32], eax
  2355.         ret
  2356. .set_mouse_acceleration:
  2357.         mov     [mouse_speed_factor], dx
  2358.         ret
  2359. .get_mouse_delay:
  2360.         xor     eax, eax
  2361.         mov     al, [mouse_delay]
  2362.         mov     [esp+32], eax
  2363.         ret
  2364. .set_mouse_delay:
  2365.         mov     [mouse_delay], dl
  2366. @@:
  2367.         ret
  2368. .set_pointer_position:
  2369.         cmp     dx, word[_display.height]
  2370.         jae     @b
  2371.         rol     edx, 16
  2372.         cmp     dx, word[_display.width]
  2373.         jae     @b
  2374.         mov     [MOUSE_X], edx
  2375.         mov     [mouse_active], 1
  2376.         jmp     wakeup_osloop
  2377. .set_mouse_button:
  2378.         mov     [BTN_DOWN], edx
  2379.         mov     [mouse_active], 1
  2380.         jmp     wakeup_osloop
  2381. .get_doubleclick_delay:
  2382.         xor     eax, eax
  2383.         mov     al, [mouse_doubleclick_delay]
  2384.         mov     [esp+32], eax
  2385.         ret
  2386. .set_doubleclick_delay:
  2387.         mov     [mouse_doubleclick_delay], dl
  2388.         ret
  2389. align 4
  2390. .table:
  2391. dd      .get_mouse_acceleration
  2392. dd      .set_mouse_acceleration
  2393. dd      .get_mouse_delay
  2394. dd      .set_mouse_delay
  2395. dd      .set_pointer_position
  2396. dd      .set_mouse_button
  2397. dd      .get_doubleclick_delay
  2398. dd      .set_doubleclick_delay
  2399. ;------------------------------------------------------------------------------
  2400. sysfn_getfreemem:
  2401.         mov     eax, [pg_data.pages_free]
  2402.         shl     eax, 2
  2403.         mov     [esp+32], eax
  2404.         ret
  2405. ;------------------------------------------------------------------------------
  2406. sysfn_getallmem:
  2407.         mov     eax, [MEM_AMOUNT]
  2408.         shr     eax, 10
  2409.         mov     [esp+32], eax
  2410.         ret
  2411. ;------------------------------------------------------------------------------
  2412. sysfn_pid_to_slot:
  2413.         mov     eax, ecx
  2414.         call    pid_to_slot
  2415.         mov     [esp+32], eax
  2416.         ret
  2417. ;------------------------------------------------------------------------------
  2418. sysfn_min_rest_window:
  2419.         pushad
  2420.         mov     eax, edx ; ebx - operating
  2421.         shr     ecx, 1
  2422.         jnc     @f
  2423.         call    pid_to_slot
  2424. @@:
  2425.         or      eax, eax ; eax - number of slot
  2426.         jz      .error
  2427.         cmp     eax, 255    ; varify maximal slot number
  2428.         ja      .error
  2429.         movzx   eax, word [WIN_STACK + eax*2]
  2430.         shr     ecx, 1
  2431.         jc      .restore
  2432.  ; .minimize:
  2433.         call    minimize_window
  2434.         jmp     .exit
  2435. .restore:
  2436.         call    restore_minimized_window
  2437. .exit:
  2438.         popad
  2439.         xor     eax, eax
  2440.         mov     [esp+32], eax
  2441.         ret
  2442. .error:
  2443.         popad
  2444.         xor     eax, eax
  2445.         dec     eax
  2446.         mov     [esp+32], eax
  2447.         ret
  2448. ;------------------------------------------------------------------------------
  2449. sysfn_min_windows:
  2450.         call    minimize_all_window
  2451.         mov     [esp+32], eax
  2452.         call    change_task
  2453.         ret
  2454. ;------------------------------------------------------------------------------
  2455. sysfn_set_screen_sizes:
  2456.         cmp     [SCR_MODE], word 0x13
  2457.         jbe     .exit
  2458.  
  2459.         cmp     [_display.select_cursor], select_cursor
  2460.         jne     .exit
  2461.  
  2462.         cmp     ecx, [display_width_standard]
  2463.         ja      .exit
  2464.  
  2465.         cmp     edx, [display_height_standard]
  2466.         ja      .exit
  2467.  
  2468.         pushfd
  2469.         cli
  2470.         mov     eax, ecx
  2471.         mov     ecx, [_display.lfb_pitch]
  2472.         mov     [_display.width], eax
  2473.         dec     eax
  2474.         mov     [_display.height], edx
  2475.         dec     edx
  2476. ; eax - new Screen_Max_X
  2477. ; edx - new Screen_Max_Y
  2478.         mov     [do_not_touch_winmap], 1
  2479.         call    set_screen
  2480.         mov     [do_not_touch_winmap], 0
  2481.         popfd
  2482.         call    change_task
  2483. .exit:
  2484.         ret
  2485. ;------------------------------------------------------------------------------
  2486. uglobal
  2487. screen_workarea RECT
  2488. display_width_standard dd 0
  2489. display_height_standard dd 0
  2490. do_not_touch_winmap db 0
  2491. window_minimize db 0
  2492. sound_flag      db 0
  2493.  
  2494. endg
  2495.  
  2496. ;UID_NONE=0
  2497. ;UID_KOLIBRI=2    ;russian
  2498.  
  2499. iglobal
  2500. version_inf:
  2501.         db 0,7,7,0  ; version 0.7.7.0
  2502.         db 0
  2503. .rev    dd __REV__
  2504. version_end:
  2505. endg
  2506. ;------------------------------------------------------------------------------
  2507. align 4
  2508. sys_cachetodiskette:
  2509.         cmp     ebx, 1
  2510.         jb      .no_floppy_save
  2511.         cmp     ebx, 2
  2512.         ja      .no_floppy_save
  2513.         call    save_image
  2514.         mov     [esp + 32], eax
  2515.         ret
  2516. .no_floppy_save:
  2517.         mov     [esp + 32], dword 1
  2518.         ret
  2519. ;------------------------------------------------------------------------------
  2520.  
  2521. align 4
  2522. sys_getkey:
  2523.         mov     [esp + 32], dword 1
  2524.         ; test main buffer
  2525.         mov     ebx, [current_slot_idx]                          ; TOP OF WINDOW STACK
  2526.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2527.         mov     edx, [thread_count]
  2528.         cmp     ecx, edx
  2529.         jne     .finish
  2530.         cmp     [KEY_COUNT], byte 0
  2531.         je      .finish
  2532.         movzx   ax, byte [KEY_BUFF + 120 + 2]
  2533.         shl     eax, 8
  2534.         mov     al, byte [KEY_BUFF]
  2535.         shl     eax, 8
  2536.         push    eax
  2537.         dec     byte [KEY_COUNT]
  2538.         and     byte [KEY_COUNT], 127
  2539.         movzx   ecx, byte [KEY_COUNT]
  2540.         add     ecx, 2
  2541.         mov     eax, KEY_BUFF + 1
  2542.         mov     ebx, KEY_BUFF
  2543.         call    memmove
  2544.         add     eax, 120 + 2
  2545.         add     ebx, 120 + 2
  2546.         call    memmove
  2547.         pop     eax
  2548. ;--------------------------------------
  2549. align 4
  2550. .ret_eax:
  2551.         mov     [esp + 32], eax
  2552.         ret
  2553. ;--------------------------------------
  2554. align 4
  2555. .finish:
  2556. ; test hotkeys buffer
  2557.         mov     ecx, hotkey_buffer
  2558. ;--------------------------------------
  2559. align 4
  2560. @@:
  2561.         cmp     [ecx], ebx
  2562.         jz      .found
  2563.         add     ecx, 8
  2564.         cmp     ecx, hotkey_buffer + 120 * 8
  2565.         jb      @b
  2566.         ret
  2567. ;--------------------------------------
  2568. align 4
  2569. .found:
  2570.         mov     ax, [ecx + 6]
  2571.         shl     eax, 16
  2572.         mov     ah, [ecx + 4]
  2573.         mov     al, 2
  2574.         and     dword [ecx + 4], 0
  2575.         and     dword [ecx], 0
  2576.         jmp     .ret_eax
  2577. ;------------------------------------------------------------------------------
  2578. align 4
  2579. sys_getbutton:
  2580.         mov     ebx, [current_slot_idx]                         ; TOP OF WINDOW STACK
  2581.         mov     [esp + 32], dword 1
  2582.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2583.         mov     edx, [thread_count] ; less than 256 processes
  2584.         cmp     ecx, edx
  2585.         jne     .exit
  2586.         movzx   eax, byte [BTN_COUNT]
  2587.         test    eax, eax
  2588.         jz      .exit
  2589.         mov     eax, [BTN_BUFF]
  2590.         and     al, 0xFE                                    ; delete left button bit
  2591.         mov     [BTN_COUNT], byte 0
  2592.         mov     [esp + 32], eax
  2593. ;--------------------------------------
  2594. align 4
  2595. .exit:
  2596.         ret
  2597. ;------------------------------------------------------------------------------
  2598. align 4
  2599. sys_cpuusage:
  2600.  
  2601. ;  RETURN:
  2602. ;
  2603. ;  +00 dword     process cpu usage
  2604. ;  +04  word     position in windowing stack
  2605. ;  +06  word     windowing stack value at current position (cpu nro)
  2606. ;  +10 12 bytes  name
  2607. ;  +22 dword     start in mem
  2608. ;  +26 dword     used mem
  2609. ;  +30 dword     PID , process idenfification number
  2610. ;
  2611.         ; if given memory address belongs to kernel then error
  2612.         stdcall is_region_userspace, ebx, 0x4C
  2613.         jnz     .addr_error
  2614.  
  2615.         cmp     ecx, -1 ; who am I ?
  2616.         jne     .no_who_am_i
  2617.         mov     ecx, [current_slot_idx]
  2618.   .no_who_am_i:
  2619.         cmp     ecx, max_processes
  2620.         ja      .nofillbuf
  2621.  
  2622. ; +4: word: position of the window of thread in the window stack
  2623.         mov     ax, [WIN_STACK + ecx * 2]
  2624.         mov     [ebx+4], ax
  2625. ; +6: word: number of the thread slot, which window has in the window stack
  2626. ;           position ecx (has no relation to the specific thread)
  2627.         mov     ax, [WIN_POS + ecx * 2]
  2628.         mov     [ebx+6], ax
  2629.  
  2630.         shl     ecx, 8   ;5=32 8=256
  2631.  
  2632. ; +0: dword: memory usage
  2633.         ;mov     eax, [ecx+TASK_TABLE+TASKDATA.cpu_usage]
  2634.         mov     eax, [ecx-sizeof.APPDATA+SLOT_BASE+APPDATA.cpu_usage]
  2635.         mov     [ebx], eax
  2636. ; +10: 11 bytes: name of the process
  2637.         push    ecx
  2638.         lea     eax, [ecx+SLOT_BASE+APPDATA.app_name]
  2639.         add     ebx, 10
  2640.         mov     ecx, 11
  2641.         call    memmove
  2642.         pop     ecx
  2643.  
  2644.         shr     ecx, 3
  2645. ; +22: address of the process in memory
  2646. ; +26: size of used memory - 1
  2647.         push    edi
  2648.         lea     edi, [ebx+12]
  2649.         xor     eax, eax
  2650.         mov     edx, 0x100000*16
  2651.         cmp     ecx, 1 shl 5
  2652.         je      .os_mem
  2653.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.process]
  2654.         mov     edx, [edx+PROC.mem_used]
  2655.         mov     eax, std_application_base_address
  2656. .os_mem:
  2657.         stosd
  2658.         lea     eax, [edx-1]
  2659.         stosd
  2660.  
  2661. ; +30: PID/TID
  2662.         mov     eax, [ecx+TASK_TABLE+TASKDATA.pid]
  2663.         stosd
  2664.  
  2665.     ; window position and size
  2666.         push    esi
  2667.         lea     esi, [ecx + window_data + WDATA.box]
  2668.         movsd
  2669.         movsd
  2670.         movsd
  2671.         movsd
  2672.  
  2673.     ; Process state (+50)
  2674.         movzx   eax, byte [ecx+TASK_TABLE+TASKDATA.state]
  2675.         stosd
  2676.  
  2677.     ; Window client area box
  2678.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2679.         movsd
  2680.         movsd
  2681.         movsd
  2682.         movsd
  2683.  
  2684.     ; Window state
  2685.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2686.         stosb
  2687.  
  2688.     ; Event mask (+71)
  2689.         mov     EAX, dword [ECX+TASK_TABLE+TASKDATA.event_mask]
  2690.         stosd
  2691.  
  2692.     ; Keyboard mode (+75)
  2693.         mov     al, byte [ecx*8 + SLOT_BASE + APPDATA.keyboard_mode]
  2694.         stosb
  2695.  
  2696.         pop     esi
  2697.         pop     edi
  2698.  
  2699. .nofillbuf:
  2700.     ; return number of processes
  2701.  
  2702.         mov     eax, [thread_count]
  2703.         mov     [esp+32], eax
  2704.         ret
  2705.  
  2706. .addr_error:    ; if given memory address is illegal
  2707.         mov     dword [esp+32], -1
  2708.         ret  
  2709.  
  2710. align 4
  2711. sys_clock:
  2712.         cli
  2713.   ; Mikhail Lisovin  xx Jan 2005
  2714.   @@:
  2715.         mov     al, 10
  2716.         out     0x70, al
  2717.         in      al, 0x71
  2718.         test    al, al
  2719.         jns     @f
  2720.         mov     esi, 1
  2721.         call    delay_ms
  2722.         jmp     @b
  2723.   @@:
  2724.   ; end Lisovin's fix
  2725.  
  2726.         xor     al, al        ; seconds
  2727.         out     0x70, al
  2728.         in      al, 0x71
  2729.         movzx   ecx, al
  2730.         mov     al, 02        ; minutes
  2731.         shl     ecx, 16
  2732.         out     0x70, al
  2733.         in      al, 0x71
  2734.         movzx   edx, al
  2735.         mov     al, 04        ; hours
  2736.         shl     edx, 8
  2737.         out     0x70, al
  2738.         in      al, 0x71
  2739.         add     ecx, edx
  2740.         movzx   edx, al
  2741.         add     ecx, edx
  2742.         sti
  2743.         mov     [esp + 32], ecx
  2744.         ret
  2745.  
  2746.  
  2747. align 4
  2748.  
  2749. sys_date:
  2750.  
  2751.         cli
  2752.   @@:
  2753.         mov     al, 10
  2754.         out     0x70, al
  2755.         in      al, 0x71
  2756.         test    al, al
  2757.         jns     @f
  2758.         mov     esi, 1
  2759.         call    delay_ms
  2760.         jmp     @b
  2761.   @@:
  2762.  
  2763.         mov     ch, 0
  2764.         mov     al, 7           ; date
  2765.         out     0x70, al
  2766.         in      al, 0x71
  2767.         mov     cl, al
  2768.         mov     al, 8           ; month
  2769.         shl     ecx, 16
  2770.         out     0x70, al
  2771.         in      al, 0x71
  2772.         mov     ch, al
  2773.         mov     al, 9           ; year
  2774.         out     0x70, al
  2775.         in      al, 0x71
  2776.         mov     cl, al
  2777.         sti
  2778.         mov     [esp+32], ecx
  2779.         ret
  2780.  
  2781.  
  2782. ; redraw status
  2783.  
  2784. sys_redrawstat:
  2785.         cmp     ebx, 1
  2786.         jne     no_widgets_away
  2787.         ; buttons away
  2788.         mov     ecx, [current_slot_idx]
  2789.   sys_newba2:
  2790.         mov     edi, [BTN_ADDR]
  2791.         cmp     [edi], dword 0  ; empty button list ?
  2792.         je      end_of_buttons_away
  2793.         movzx   ebx, word [edi]
  2794.         inc     ebx
  2795.         mov     eax, edi
  2796.   sys_newba:
  2797.         dec     ebx
  2798.         jz      end_of_buttons_away
  2799.  
  2800.         add     eax, 0x10
  2801.         cmp     cx, [eax]
  2802.         jnz     sys_newba
  2803.  
  2804.         push    eax ebx ecx
  2805.         mov     ecx, ebx
  2806.         inc     ecx
  2807.         shl     ecx, 4
  2808.         mov     ebx, eax
  2809.         add     eax, 0x10
  2810.         call    memmove
  2811.         dec     dword [edi]
  2812.         pop     ecx ebx eax
  2813.  
  2814.         jmp     sys_newba2
  2815.  
  2816.   end_of_buttons_away:
  2817.  
  2818.         ret
  2819.  
  2820.   no_widgets_away:
  2821.  
  2822.         cmp     ebx, 2
  2823.         jnz     srl1
  2824.  
  2825.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2826.         add     edx, draw_data - TASK_TABLE
  2827.         mov     [edx + RECT.left], 0
  2828.         mov     [edx + RECT.top], 0
  2829.         mov     eax, [_display.width]
  2830.         dec     eax
  2831.         mov     [edx + RECT.right], eax
  2832.         mov     eax, [_display.height]
  2833.         dec     eax
  2834.         mov     [edx + RECT.bottom], eax
  2835.  
  2836.   srl1:
  2837.         ret
  2838.  
  2839. ;ok - 100% work
  2840. ;nt - not tested
  2841. ;---------------------------------------------------------------------------------------------
  2842. ;eax
  2843. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2844. ;1 - change task. Ret nothing. Block. ok.
  2845. ;2 - performance control
  2846. ; ebx
  2847. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2848. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2849. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2850. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2851. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2852. ;eax
  2853. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2854. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2855. ;---------------------------------------------------------------------------------------------
  2856. iglobal
  2857. align 4
  2858. sheduler:
  2859.         dd      sys_sheduler.00
  2860.         dd      change_task
  2861.         dd      sys_sheduler.02
  2862.         dd      sys_sheduler.03
  2863.         dd      sys_sheduler.04
  2864. endg
  2865. sys_sheduler:
  2866. ;rewritten by <Lrz>  29.12.2009
  2867.         jmp     dword [sheduler+ebx*4]
  2868. ;.shed_counter:
  2869. .00:
  2870.         mov     eax, [context_counter]
  2871.         mov     [esp+32], eax
  2872.         ret
  2873.  
  2874. .02:
  2875. ;.perf_control:
  2876.         inc     ebx                     ;before ebx=2, ebx=3
  2877.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  2878.         jz      cache_disable
  2879.  
  2880.         dec     ebx                     ;ebx=2
  2881.         cmp     ebx, ecx                ;
  2882.         jz      cache_enable            ;if ecx=2 and ebx=2
  2883.  
  2884.         dec     ebx                     ;ebx=1
  2885.         cmp     ebx, ecx
  2886.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  2887.  
  2888.         dec     ebx
  2889.         test    ebx, ecx                ;ebx=0 and ecx=0
  2890.         jz      modify_pce              ;if ecx=0
  2891.  
  2892.         ret
  2893.  
  2894. .03:
  2895. ;.rdmsr_instr:
  2896. ;now counter in ecx
  2897. ;(edx:eax) esi:edi => edx:esi
  2898.         mov     eax, esi
  2899.         mov     ecx, edx
  2900.         rdmsr
  2901.         mov     [esp+32], eax
  2902.         mov     [esp+20], edx           ;ret in ebx?
  2903.         ret
  2904.  
  2905. .04:
  2906. ;.wrmsr_instr:
  2907. ;now counter in ecx
  2908. ;(edx:eax) esi:edi => edx:esi
  2909.         ; Fast Call MSR can't be destroy
  2910.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  2911.         ; включаются/выключаются расширенные возможности
  2912.         cmp     edx, MSR_SYSENTER_CS
  2913.         je      @f
  2914.         cmp     edx, MSR_SYSENTER_ESP
  2915.         je      @f
  2916.         cmp     edx, MSR_SYSENTER_EIP
  2917.         je      @f
  2918.         cmp     edx, MSR_AMD_STAR
  2919.         je      @f
  2920.  
  2921.         mov     eax, esi
  2922.         mov     ecx, edx
  2923.         wrmsr
  2924.         ; mov   [esp + 32], eax
  2925.         ; mov   [esp + 20], edx ;ret in ebx?
  2926. @@:
  2927.         ret
  2928.  
  2929. cache_disable:
  2930.         mov     eax, cr0
  2931.         or      eax, 01100000000000000000000000000000b
  2932.         mov     cr0, eax
  2933.         wbinvd  ;set MESI
  2934.         ret
  2935.  
  2936. cache_enable:
  2937.         mov     eax, cr0
  2938.         and     eax, 10011111111111111111111111111111b
  2939.         mov     cr0, eax
  2940.         ret
  2941.  
  2942. is_cache_enabled:
  2943.         mov     eax, cr0
  2944.         mov     ebx, eax
  2945.         and     eax, 01100000000000000000000000000000b
  2946.         jz      cache_disabled
  2947.         mov     [esp+32], ebx
  2948. cache_disabled:
  2949.         mov     dword [esp+32], eax;0
  2950.         ret
  2951.  
  2952. modify_pce:
  2953.         mov     eax, cr4
  2954. ;       mov ebx,0
  2955. ;       or  bx,100000000b ;pce
  2956. ;       xor eax,ebx ;invert pce
  2957.         bts     eax, 8;pce=cr4[8]
  2958.         mov     cr4, eax
  2959.         mov     [esp+32], eax
  2960.         ret
  2961. ;---------------------------------------------------------------------------------------------
  2962.  
  2963.  
  2964. iglobal
  2965.   cpustring db 'CPU',0
  2966. endg
  2967.  
  2968. uglobal
  2969. background_defined    db    0    ; diamond, 11.04.2006
  2970. endg
  2971. ;-----------------------------------------------------------------------------
  2972. align 4
  2973. checkmisc:
  2974.         cmp     [ctrl_alt_del], 1
  2975.         jne     nocpustart
  2976.  
  2977.         mov     ebp, cpustring
  2978.         call    fs_execute_from_sysdir
  2979.  
  2980.         mov     [ctrl_alt_del], 0
  2981. ;--------------------------------------
  2982. align 4
  2983. nocpustart:
  2984.         cmp     [mouse_active], 1
  2985.         jne     mouse_not_active
  2986.         mov     [mouse_active], 0
  2987.  
  2988.         xor     edi, edi
  2989.         mov     ebx, TASK_TABLE
  2990.  
  2991.         mov     ecx, [thread_count]
  2992.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  2993.         shl     eax, 8
  2994.         push    eax
  2995.  
  2996.         movzx   eax, word [MOUSE_X]
  2997.         movzx   edx, word [MOUSE_Y]
  2998. ;--------------------------------------
  2999. align 4
  3000. .set_mouse_event:
  3001.         add     edi, sizeof.APPDATA
  3002.         add     ebx, sizeof.TASKDATA
  3003.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3004.         jz      .pos_filter
  3005.  
  3006.         cmp     edi, [esp]                      ; skip if filtration active
  3007.         jne     .skip
  3008. ;--------------------------------------
  3009. align 4
  3010. .pos_filter:
  3011.         test    [ebx+TASKDATA.event_mask], 0x40000000
  3012.         jz      .set
  3013.  
  3014.         mov     esi, [ebx-twdw+WDATA.box.left]
  3015.         cmp     eax, esi
  3016.         jb      .skip
  3017.         add     esi, [ebx-twdw+WDATA.box.width]
  3018.         cmp     eax, esi
  3019.         ja      .skip
  3020.  
  3021.         mov     esi, [ebx-twdw+WDATA.box.top]
  3022.         cmp     edx, esi
  3023.         jb      .skip
  3024.         add     esi, [ebx-twdw+WDATA.box.height]
  3025.         cmp     edx, esi
  3026.         ja      .skip
  3027. ;--------------------------------------
  3028. align 4
  3029. .set:
  3030.         or      [edi+SLOT_BASE+APPDATA.occurred_events], EVENT_MOUSE
  3031. ;--------------------------------------
  3032. align 4
  3033. .skip:
  3034.         loop    .set_mouse_event
  3035.  
  3036.         pop     eax
  3037. ;--------------------------------------
  3038. align 4
  3039. mouse_not_active:
  3040.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  3041.         jz      nobackgr
  3042.  
  3043.         cmp     [background_defined], 0
  3044.         jz      nobackgr
  3045. ;--------------------------------------
  3046. align 4
  3047. backgr:
  3048.         mov     eax, [draw_data+32 + RECT.left]
  3049.         shl     eax, 16
  3050.         add     eax, [draw_data+32 + RECT.right]
  3051.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  3052.  
  3053.         mov     eax, [draw_data+32 + RECT.top]
  3054.         shl     eax, 16
  3055.         add     eax, [draw_data+32 + RECT.bottom]
  3056.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  3057.  
  3058.         call    drawbackground
  3059. ;        DEBUGF  1, "K : drawbackground\n"
  3060. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  3061. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  3062. ;--------- set event 5 start ----------
  3063.         push    ecx edi
  3064.         xor     edi, edi
  3065.         mov     ecx, [thread_count]
  3066. ;--------------------------------------
  3067. align 4
  3068. set_bgr_event:
  3069.         add     edi, sizeof.APPDATA
  3070.         mov     eax, [BG_Rect_X_left_right]
  3071.         mov     edx, [BG_Rect_Y_top_bottom]
  3072.         cmp     [edi+SLOT_BASE+APPDATA.draw_bgr_x], 0
  3073.         jz      .set
  3074. .join:
  3075.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3076.         jae     @f
  3077.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3078. @@:
  3079.         shr     eax, 16
  3080.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3081.         jbe     @f
  3082.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3083. @@:
  3084.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3085.         jae     @f
  3086.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3087. @@:
  3088.         shr     edx, 16
  3089.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3090.         jbe     @f
  3091.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3092. @@:
  3093.         jmp     .common
  3094. .set:
  3095.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_x], eax
  3096.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_y], edx
  3097. .common:
  3098.         or      [edi+SLOT_BASE+APPDATA.occurred_events], EVENT_BACKGROUND
  3099.         loop    set_bgr_event
  3100.         pop     edi ecx
  3101. ;--------- set event 5 stop -----------
  3102.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  3103.         jnz     backgr
  3104.  
  3105.         xor     eax, eax
  3106.         mov     [draw_data+32 + RECT.left], eax
  3107.         mov     [draw_data+32 + RECT.top], eax
  3108.         mov     [draw_data+32 + RECT.right], eax
  3109.         mov     [draw_data+32 + RECT.bottom], eax
  3110. ;--------------------------------------
  3111. align 4
  3112. nobackgr:
  3113. ; system shutdown request
  3114.         cmp     [SYS_SHUTDOWN], byte 0
  3115.         je      noshutdown
  3116.  
  3117.         mov     edx, [shutdown_processes]
  3118.  
  3119.         cmp     [SYS_SHUTDOWN], dl
  3120.         jne     noshutdown
  3121.  
  3122.         lea     ecx, [edx-1]
  3123.         mov     edx, OS_BASE+0x3040
  3124.         jecxz   no_mark_system_shutdown
  3125. ;--------------------------------------
  3126. align 4
  3127. markz:
  3128.         push    ecx edx
  3129.         cmp     [edx+TASKDATA.state], TSTATE_FREE
  3130.         jz      .nokill
  3131.         lea     edx, [(edx-(TASK_TABLE and 1FFFFFFFh))*8+SLOT_BASE]
  3132.         cmp     [edx+APPDATA.process], sys_proc
  3133.         jz      .nokill
  3134.         call    request_terminate
  3135.         jmp     .common
  3136. .nokill:
  3137.         dec     byte [SYS_SHUTDOWN]
  3138.         xor     eax, eax
  3139. .common:
  3140.         pop     edx ecx
  3141.         test    eax, eax
  3142.         jz      @f
  3143.         mov     [edx+TASKDATA.state], TSTATE_ZOMBIE
  3144. @@:
  3145.         add     edx, 0x20
  3146.         loop    markz
  3147.         call    wakeup_osloop
  3148. ;--------------------------------------
  3149. align 4
  3150. @@:
  3151. no_mark_system_shutdown:
  3152.         dec     byte [SYS_SHUTDOWN]
  3153.         je      system_shutdown
  3154. ;--------------------------------------
  3155. align 4
  3156. noshutdown:
  3157.         mov     eax, [thread_count]           ; termination
  3158.         mov     ebx, TASK_DATA+TASKDATA.state
  3159.         mov     esi, 1
  3160. ;--------------------------------------
  3161. align 4
  3162. newct:
  3163.         mov     cl, [ebx]
  3164.         cmp     cl, byte 3
  3165.         jz      .terminate
  3166.  
  3167.         cmp     cl, byte 4
  3168.         jnz     .noterminate
  3169. .terminate:
  3170.         pushad
  3171.         mov     ecx, eax
  3172.         shl     ecx, 8
  3173.         add     ecx, SLOT_BASE
  3174.         call    restore_default_cursor_before_killing
  3175.         popad
  3176.  
  3177.         pushad
  3178.         call    terminate
  3179.         popad
  3180.         cmp     byte [SYS_SHUTDOWN], 0
  3181.         jz      .noterminate
  3182.         dec     byte [SYS_SHUTDOWN]
  3183.         je      system_shutdown
  3184.  
  3185. .noterminate:
  3186.         add     ebx, 0x20
  3187.         inc     esi
  3188.         dec     eax
  3189.         jnz     newct
  3190.         ret
  3191. ;-----------------------------------------------------------------------------
  3192. align 4
  3193. redrawscreen:
  3194. ; eax , if process window_data base is eax, do not set flag/limits
  3195.  
  3196.         pushad
  3197.         push    eax
  3198.  
  3199. ;;;         mov   ebx,2
  3200. ;;;         call  delay_hs
  3201.  
  3202.          ;mov   ecx,0               ; redraw flags for apps
  3203.         xor     ecx, ecx
  3204. ;--------------------------------------
  3205. align 4
  3206. newdw2:
  3207.         inc     ecx
  3208.         push    ecx
  3209.  
  3210.         mov     eax, ecx
  3211.         shl     eax, 5
  3212.         add     eax, window_data
  3213.  
  3214.         cmp     eax, [esp+4]
  3215.         je      not_this_task
  3216.                                    ; check if window in redraw area
  3217.         mov     edi, eax
  3218.  
  3219.         cmp     ecx, 1             ; limit for background
  3220.         jz      bgli
  3221.  
  3222.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3223.         test    eax, eax
  3224.         jz      @f
  3225.         mov     al, [eax + WDATA.z_modif]
  3226.         cmp     [edi + WDATA.z_modif], al
  3227.         jg      ricino
  3228.       @@:
  3229.  
  3230.         mov     eax, [edi + WDATA.box.left]
  3231.         mov     ebx, [edi + WDATA.box.top]
  3232.  
  3233.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3234.         cmp     ecx, ebx
  3235.         jb      ricino
  3236.  
  3237.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3238.         cmp     ecx, eax
  3239.         jb      ricino
  3240.  
  3241.         mov     eax, [edi + WDATA.box.left]
  3242.         mov     ebx, [edi + WDATA.box.top]
  3243.         mov     ecx, [edi + WDATA.box.width]
  3244.         mov     edx, [edi + WDATA.box.height]
  3245.         add     ecx, eax
  3246.         add     edx, ebx
  3247.  
  3248.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3249.         cmp     edx, eax
  3250.         jb      ricino
  3251.  
  3252.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3253.         cmp     ecx, eax
  3254.         jb      ricino
  3255. ;--------------------------------------
  3256. align 4
  3257. bgli:
  3258.         cmp     dword[esp], 1
  3259.         jnz     .az
  3260.  
  3261.         cmp     [REDRAW_BACKGROUND], 0
  3262.         jz      .az
  3263.  
  3264.         mov     dl, 0
  3265.         lea     eax, [edi+draw_data-window_data]
  3266.         mov     ebx, [draw_limits.left]
  3267.         cmp     ebx, [eax+RECT.left]
  3268.         jae     @f
  3269.  
  3270.         mov     [eax+RECT.left], ebx
  3271.         mov     dl, 1
  3272. ;--------------------------------------
  3273. align 4
  3274. @@:
  3275.         mov     ebx, [draw_limits.top]
  3276.         cmp     ebx, [eax+RECT.top]
  3277.         jae     @f
  3278.  
  3279.         mov     [eax+RECT.top], ebx
  3280.         mov     dl, 1
  3281. ;--------------------------------------
  3282. align 4
  3283. @@:
  3284.         mov     ebx, [draw_limits.right]
  3285.         cmp     ebx, [eax+RECT.right]
  3286.         jbe     @f
  3287.  
  3288.         mov     [eax+RECT.right], ebx
  3289.         mov     dl, 1
  3290. ;--------------------------------------
  3291. align 4
  3292. @@:
  3293.         mov     ebx, [draw_limits.bottom]
  3294.         cmp     ebx, [eax+RECT.bottom]
  3295.         jbe     @f
  3296.  
  3297.         mov     [eax+RECT.bottom], ebx
  3298.         mov     dl, 1
  3299. ;--------------------------------------
  3300. align 4
  3301. @@:
  3302.         add     [REDRAW_BACKGROUND], dl
  3303.         call    wakeup_osloop
  3304.         jmp     newdw8
  3305. ;--------------------------------------
  3306. align 4
  3307. .az:
  3308.         mov     eax, edi
  3309.         add     eax, draw_data-window_data
  3310.  
  3311.         mov     ebx, [draw_limits.left]        ; set limits
  3312.         mov     [eax + RECT.left], ebx
  3313.         mov     ebx, [draw_limits.top]
  3314.         mov     [eax + RECT.top], ebx
  3315.         mov     ebx, [draw_limits.right]
  3316.         mov     [eax + RECT.right], ebx
  3317.         mov     ebx, [draw_limits.bottom]
  3318.         mov     [eax + RECT.bottom], ebx
  3319.  
  3320.         sub     eax, draw_data-window_data
  3321.  
  3322.         cmp     dword [esp], 1
  3323.         jne     nobgrd
  3324.         inc     [REDRAW_BACKGROUND]
  3325.         call    wakeup_osloop
  3326. ;--------------------------------------
  3327. align 4
  3328. newdw8:
  3329. nobgrd:
  3330. ;--------------------------------------
  3331.         push    eax  edi ebp
  3332.         mov     edi, [esp+12]
  3333.         cmp     edi, 1
  3334.         je      .found
  3335.  
  3336.         mov     eax, [draw_limits.left]
  3337.         mov     ebx, [draw_limits.top]
  3338.         mov     ecx, [draw_limits.right]
  3339.         sub     ecx, eax
  3340.         test    ecx, ecx
  3341.         jz      .not_found
  3342.  
  3343.         mov     edx, [draw_limits.bottom]
  3344.         sub     edx, ebx
  3345.         test    edx, edx
  3346.         jz      .not_found
  3347.  
  3348. ; eax - x, ebx - y
  3349. ; ecx - size x, edx - size y
  3350.         add     ebx, edx
  3351. ;--------------------------------------
  3352. align 4
  3353. .start_y:
  3354.         push    ecx
  3355. ;--------------------------------------
  3356. align 4
  3357. .start_x:
  3358.         add     eax, ecx
  3359.         mov     ebp, [d_width_calc_area + ebx*4]
  3360.         add     ebp, [_display.win_map]
  3361.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3362.         cmp     ebp, edi
  3363.         jne     @f
  3364.  
  3365.         pop     ecx
  3366.         jmp     .found
  3367. ;--------------------------------------
  3368. align 4
  3369. @@:
  3370.         sub     eax, ecx
  3371.  
  3372.         dec     ecx
  3373.         jns     .start_x
  3374.  
  3375.         pop     ecx
  3376.         dec     ebx
  3377.         dec     edx
  3378.         jns     .start_y
  3379. ;--------------------------------------
  3380. align 4
  3381. .not_found:
  3382.         pop     ebp edi eax
  3383.         jmp     ricino
  3384. ;--------------------------------------
  3385. align 4
  3386. .found:
  3387.         pop     ebp edi eax
  3388.  
  3389.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3390. ;--------------------------------------
  3391. align 4
  3392. ricino:
  3393. not_this_task:
  3394.         pop     ecx
  3395.  
  3396.         cmp     ecx, [thread_count]
  3397.         jle     newdw2
  3398.  
  3399.         pop     eax
  3400.         popad
  3401.         ret
  3402. ;-----------------------------------------------------------------------------
  3403. align 4
  3404. calculatebackground:   ; background
  3405.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3406.         mov     eax, 0x01010101
  3407.         mov     ecx, [_display.win_map_size]
  3408.         shr     ecx, 2
  3409.         rep stosd
  3410.         mov     byte[window_data+32+WDATA.z_modif], ZPOS_DESKTOP
  3411.         mov     [REDRAW_BACKGROUND], 0
  3412.         ret
  3413. ;-----------------------------------------------------------------------------
  3414. uglobal
  3415.   imax    dd 0x0
  3416. endg
  3417. ;-----------------------------------------------------------------------------
  3418. align 4
  3419. delay_ms:     ; delay in 1/1000 sec
  3420.         pushad
  3421.  
  3422.         cmp     [hpet_base], 0
  3423.         jz      .no_hpet
  3424.         mov     eax, esi
  3425.         mov     edx, 1_000_000 ; ms to ns
  3426.         mul     edx
  3427.         mov     ebx, edx
  3428.         mov     ecx, eax
  3429.  
  3430.         push    ecx
  3431.         call    get_clock_ns
  3432.         pop     ecx
  3433.         mov     edi, edx
  3434.         mov     esi, eax
  3435. .wait:
  3436.         push    ecx
  3437.         call    get_clock_ns
  3438.         pop     ecx
  3439.         sub     eax, esi
  3440.         sbb     edx, edi
  3441.         sub     eax, ecx
  3442.         sbb     edx, ebx
  3443.         jc      .wait
  3444.         jmp     .done
  3445.  
  3446. .no_hpet:
  3447.         mov     ecx, esi
  3448.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3449.         imul    ecx, 33941
  3450.         shr     ecx, 9
  3451.         ; </CPU clock fix>
  3452.  
  3453.         in      al, 0x61
  3454.         and     al, 0x10
  3455.         mov     ah, al
  3456.         cld
  3457.  
  3458. .cnt1:
  3459.         in      al, 0x61
  3460.         and     al, 0x10
  3461.         cmp     al, ah
  3462.         jz      .cnt1
  3463.  
  3464.         mov     ah, al
  3465.         loop    .cnt1
  3466.  
  3467. .done:
  3468.         popad
  3469.         ret
  3470. ;-----------------------------------------------------------------------------
  3471. align 4
  3472. set_app_param:
  3473.         mov     edi, [TASK_BASE]
  3474.         mov     eax, ebx
  3475.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  3476.         mov     [esp+32], eax                    ; return old mask value
  3477.         ret
  3478. ;-----------------------------------------------------------------------------
  3479.  
  3480. ; this is for syscall
  3481. proc delay_hs_unprotected
  3482.         call    unprotect_from_terminate
  3483.         call    delay_hs
  3484.         call    protect_from_terminate
  3485.         ret
  3486. endp
  3487.  
  3488. if 1
  3489. align 4
  3490. delay_hs:     ; delay in 1/100 secs
  3491. ; ebx = delay time
  3492.  
  3493.         pushad
  3494.         push    ebx
  3495.         xor     esi, esi
  3496.         mov     ecx, MANUAL_DESTROY
  3497.         call    create_event
  3498.         test    eax, eax
  3499.         jz      .done
  3500.  
  3501.         mov     ebx, edx
  3502.         mov     ecx, [esp]
  3503.         push    edx
  3504.         push    eax
  3505.         call    wait_event_timeout
  3506.         pop     eax
  3507.         pop     ebx
  3508.         call    destroy_event
  3509. .done:
  3510.         add     esp, 4
  3511.         popad
  3512.         ret
  3513.  
  3514. else
  3515.  
  3516. align 4
  3517. delay_hs:     ; delay in 1/100 secs
  3518. ; ebx = delay time
  3519.         push    ecx
  3520.         push    edx
  3521.  
  3522.         mov     edx, [timer_ticks]
  3523. ;--------------------------------------
  3524. align 4
  3525. newtic:
  3526.         mov     ecx, [timer_ticks]
  3527.         sub     ecx, edx
  3528.         cmp     ecx, ebx
  3529.         jae     zerodelay
  3530.  
  3531.         call    change_task
  3532.  
  3533.         jmp     newtic
  3534. ;--------------------------------------
  3535. align 4
  3536. zerodelay:
  3537.         pop     edx
  3538.         pop     ecx
  3539.         ret
  3540. end if
  3541.  
  3542. ;-----------------------------------------------------------------------------
  3543. align 16        ;very often call this subrutine
  3544. memmove:       ; memory move in bytes
  3545. ; eax = from
  3546. ; ebx = to
  3547. ; ecx = no of bytes
  3548.         test    ecx, ecx
  3549.         jle     .ret
  3550.  
  3551.         push    esi edi ecx
  3552.  
  3553.         mov     edi, ebx
  3554.         mov     esi, eax
  3555.  
  3556.         test    ecx, not 11b
  3557.         jz      @f
  3558.  
  3559.         push    ecx
  3560.         shr     ecx, 2
  3561.         rep movsd
  3562.         pop     ecx
  3563.         and     ecx, 11b
  3564.         jz      .finish
  3565. ;--------------------------------------
  3566. align 4
  3567. @@:
  3568.         rep movsb
  3569. ;--------------------------------------
  3570. align 4
  3571. .finish:
  3572.         pop     ecx edi esi
  3573. ;--------------------------------------
  3574. align 4
  3575. .ret:
  3576.         ret
  3577. ;-----------------------------------------------------------------------------
  3578.  
  3579. align 4
  3580. set_io_access_rights:
  3581.         push    edi eax
  3582.         mov     edi, tss._io_map_0
  3583. ;     mov   ecx,eax
  3584. ;     and   ecx,7    ; offset in byte
  3585. ;     shr   eax,3    ; number of byte
  3586. ;     add   edi,eax
  3587. ;     mov   ebx,1
  3588. ;     shl   ebx,cl
  3589.         test    ebp, ebp
  3590. ;     cmp   ebp,0                ; enable access - ebp = 0
  3591.         jnz     .siar1
  3592. ;     not   ebx
  3593. ;     and   [edi],byte bl
  3594.         btr     [edi], eax
  3595.         pop     eax edi
  3596.         ret
  3597. .siar1:
  3598.         bts     [edi], eax
  3599.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3600.         pop     eax edi
  3601.         ret
  3602.  
  3603. align 4
  3604. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  3605.  
  3606.         call    r_f_port_area
  3607.         mov     [esp+32], eax
  3608.         ret
  3609.  
  3610. ;reserve/free group of ports
  3611. ;  * eax = 46 - number function
  3612. ;  * ebx = 0 - reserve, 1 - free
  3613. ;  * ecx = number start arrea of ports
  3614. ;  * edx = number end arrea of ports (include last number of port)
  3615. ;Return value:
  3616. ;  * eax = 0 - succesful
  3617. ;  * eax = 1 - error
  3618. ;  * The system has reserve this ports:
  3619. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3620. ;destroys eax,ebx, ebp
  3621. r_f_port_area:
  3622.  
  3623.         test    ebx, ebx
  3624.         jnz     free_port_area
  3625. ;     je    r_port_area
  3626. ;     jmp   free_port_area
  3627.  
  3628. ;   r_port_area:
  3629.  
  3630. ;     pushad
  3631.  
  3632.         cmp     ecx, edx      ; beginning > end ?
  3633.         ja      rpal1
  3634.         cmp     edx, 65536
  3635.         jae     rpal1
  3636.         mov     eax, [RESERVED_PORTS]
  3637.         test    eax, eax      ; no reserved areas ?
  3638.         je      rpal2
  3639.         cmp     eax, 255      ; max reserved
  3640.         jae     rpal1
  3641.  rpal3:
  3642.         mov     ebx, eax
  3643.         shl     ebx, 4
  3644.         add     ebx, RESERVED_PORTS
  3645.         cmp     ecx, [ebx+8]
  3646.         ja      rpal4
  3647.         cmp     edx, [ebx+4]
  3648.         jae     rpal1
  3649. ;     jb    rpal4
  3650. ;     jmp   rpal1
  3651.  rpal4:
  3652.         dec     eax
  3653.         jnz     rpal3
  3654.         jmp     rpal2
  3655.    rpal1:
  3656. ;     popad
  3657. ;     mov   eax,1
  3658.         xor     eax, eax
  3659.         inc     eax
  3660.         ret
  3661.    rpal2:
  3662. ;     popad
  3663.      ; enable port access at port IO map
  3664.         cli
  3665.         pushad                        ; start enable io map
  3666.  
  3667.         cmp     edx, 65536;16384
  3668.         jae     no_unmask_io; jge
  3669.         mov     eax, ecx
  3670. ;       push    ebp
  3671.         xor     ebp, ebp               ; enable - eax = port
  3672. new_port_access:
  3673. ;     pushad
  3674.         call    set_io_access_rights
  3675. ;     popad
  3676.         inc     eax
  3677.         cmp     eax, edx
  3678.         jbe     new_port_access
  3679. ;       pop     ebp
  3680. no_unmask_io:
  3681.         popad                         ; end enable io map
  3682.         sti
  3683.  
  3684.         mov     eax, [RESERVED_PORTS]
  3685.         add     eax, 1
  3686.         mov     [RESERVED_PORTS], eax
  3687.         shl     eax, 4
  3688.         add     eax, RESERVED_PORTS
  3689.         mov     ebx, [TASK_BASE]
  3690.         mov     ebx, [ebx+TASKDATA.pid]
  3691.         mov     [eax], ebx
  3692.         mov     [eax+4], ecx
  3693.         mov     [eax+8], edx
  3694.  
  3695.         xor     eax, eax
  3696.         ret
  3697.  
  3698. free_port_area:
  3699.  
  3700. ;     pushad
  3701.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3702.         test    eax, eax
  3703.         jz      frpal2
  3704.         mov     ebx, [TASK_BASE]
  3705.         mov     ebx, [ebx+TASKDATA.pid]
  3706.    frpal3:
  3707.         mov     edi, eax
  3708.         shl     edi, 4
  3709.         add     edi, RESERVED_PORTS
  3710.         cmp     ebx, [edi]
  3711.         jne     frpal4
  3712.         cmp     ecx, [edi+4]
  3713.         jne     frpal4
  3714.         cmp     edx, [edi+8]
  3715.         jne     frpal4
  3716.         jmp     frpal1
  3717.    frpal4:
  3718.         dec     eax
  3719.         jnz     frpal3
  3720.    frpal2:
  3721. ;     popad
  3722.         inc     eax
  3723.         ret
  3724.    frpal1:
  3725.         push    ecx
  3726.         mov     ecx, 256
  3727.         sub     ecx, eax
  3728.         shl     ecx, 4
  3729.         mov     esi, edi
  3730.         add     esi, 16
  3731.         cld
  3732.         rep movsb
  3733.  
  3734.         dec     dword [RESERVED_PORTS]
  3735. ;popad
  3736. ;disable port access at port IO map
  3737.  
  3738. ;     pushad                        ; start disable io map
  3739.         pop     eax     ;start port
  3740.         cmp     edx, 65536;16384
  3741.         jge     no_mask_io
  3742.  
  3743. ;     mov   eax,ecx
  3744.         xor     ebp, ebp
  3745.         inc     ebp
  3746. new_port_access_disable:
  3747. ;     pushad
  3748. ;     mov   ebp,1                  ; disable - eax = port
  3749.         call    set_io_access_rights
  3750. ;     popad
  3751.         inc     eax
  3752.         cmp     eax, edx
  3753.         jbe     new_port_access_disable
  3754. no_mask_io:
  3755. ;     popad                         ; end disable io map
  3756.         xor     eax, eax
  3757.         ret
  3758. ;-----------------------------------------------------------------------------
  3759. align 4
  3760. drawbackground:
  3761. dbrv20:
  3762.         cmp     [BgrDrawMode], dword 1
  3763.         jne     bgrstr
  3764.         call    vesa20_drawbackground_tiled
  3765. ;        call    [draw_pointer]
  3766.         call    __sys_draw_pointer
  3767.         ret
  3768. ;--------------------------------------
  3769. align 4
  3770. bgrstr:
  3771.         call    vesa20_drawbackground_stretch
  3772. ;        call    [draw_pointer]
  3773.         call    __sys_draw_pointer
  3774.         ret
  3775. ;-----------------------------------------------------------------------------
  3776. align 4
  3777. syscall_putimage:                       ; PutImage
  3778. ; add check pointer
  3779.         push    ecx
  3780.         mov     ax, cx
  3781.         shr     ecx, 16
  3782.         imul    eax, ecx
  3783.         lea     eax, [eax*3]
  3784.         stdcall is_region_userspace, ebx, eax
  3785.         pop     ecx
  3786.         jnz     sys_putimage.exit
  3787.  
  3788. sys_putimage:
  3789.         test    ecx, 0x80008000
  3790.         jnz     .exit
  3791.         test    ecx, 0x0000FFFF
  3792.         jz      .exit
  3793.         test    ecx, 0xFFFF0000
  3794.         jnz     @f
  3795. ;--------------------------------------
  3796. align 4
  3797. .exit:
  3798.         ret
  3799. ;--------------------------------------
  3800. align 4
  3801. @@:
  3802.         mov     edi, [current_slot]
  3803.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  3804.         rol     edx, 16
  3805.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  3806.         rol     edx, 16
  3807. ;--------------------------------------
  3808. align 4
  3809. .forced:
  3810.         push    ebp esi 0
  3811.         mov     ebp, putimage_get24bpp
  3812.         mov     esi, putimage_init24bpp
  3813. ;--------------------------------------
  3814. align 4
  3815. sys_putimage_bpp:
  3816.         call    vesa20_putimage
  3817.         pop     ebp esi ebp
  3818.         ret
  3819. ;        jmp     [draw_pointer]
  3820. ;-----------------------------------------------------------------------------
  3821. align 4
  3822. sys_putimage_palette:
  3823. ; ebx = pointer to image
  3824. ; ecx = [xsize]*65536 + [ysize]
  3825. ; edx = [xstart]*65536 + [ystart]
  3826. ; esi = number of bits per pixel, must be 8, 24 or 32
  3827. ; edi = pointer to palette
  3828. ; ebp = row delta
  3829. ; check pointer
  3830.         push    ecx
  3831.         mov     ax, cx
  3832.         shr     ecx, 16
  3833.         imul    eax, ecx
  3834.         stdcall is_region_userspace, ebx, eax
  3835.         pop     ecx
  3836.         jnz     sys_putimage.exit
  3837.  
  3838.         mov     eax, [current_slot_idx]
  3839.         shl     eax, 8
  3840.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3841.         rol     edx, 16
  3842.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3843.         rol     edx, 16
  3844. ;--------------------------------------
  3845. align 4
  3846. .forced:
  3847.         cmp     esi, 1
  3848.         jnz     @f
  3849.         push    edi
  3850.         mov     eax, [edi+4]
  3851.         sub     eax, [edi]
  3852.         push    eax
  3853.         push    dword [edi]
  3854.         push    0ffffff80h
  3855.         mov     edi, esp
  3856.         call    put_mono_image
  3857.         add     esp, 12
  3858.         pop     edi
  3859.         ret
  3860. ;--------------------------------------
  3861. align 4
  3862. @@:
  3863.         cmp     esi, 2
  3864.         jnz     @f
  3865.         push    edi
  3866.         push    0ffffff80h
  3867.         mov     edi, esp
  3868.         call    put_2bit_image
  3869.         pop     eax
  3870.         pop     edi
  3871.         ret
  3872. ;--------------------------------------
  3873. align 4
  3874. @@:
  3875.         cmp     esi, 4
  3876.         jnz     @f
  3877.         push    edi
  3878.         push    0ffffff80h
  3879.         mov     edi, esp
  3880.         call    put_4bit_image
  3881.         pop     eax
  3882.         pop     edi
  3883.         ret
  3884. ;--------------------------------------
  3885. align 4
  3886. @@:
  3887.         push    ebp esi ebp
  3888.         cmp     esi, 8
  3889.         jnz     @f
  3890.         mov     ebp, putimage_get8bpp
  3891.         mov     esi, putimage_init8bpp
  3892.         jmp     sys_putimage_bpp
  3893. ;--------------------------------------
  3894. align 4
  3895. @@:
  3896.         cmp     esi, 9
  3897.         jnz     @f
  3898.         mov     ebp, putimage_get9bpp
  3899.         mov     esi, putimage_init9bpp
  3900.         jmp     sys_putimage_bpp
  3901. ;--------------------------------------
  3902. align 4
  3903. @@:
  3904.         cmp     esi, 15
  3905.         jnz     @f
  3906.         mov     ebp, putimage_get15bpp
  3907.         mov     esi, putimage_init15bpp
  3908.         jmp     sys_putimage_bpp
  3909. ;--------------------------------------
  3910. align 4
  3911. @@:
  3912.         cmp     esi, 16
  3913.         jnz     @f
  3914.         mov     ebp, putimage_get16bpp
  3915.         mov     esi, putimage_init16bpp
  3916.         jmp     sys_putimage_bpp
  3917. ;--------------------------------------
  3918. align 4
  3919. @@:
  3920.         cmp     esi, 24
  3921.         jnz     @f
  3922.         mov     ebp, putimage_get24bpp
  3923.         mov     esi, putimage_init24bpp
  3924.         jmp     sys_putimage_bpp
  3925. ;--------------------------------------
  3926. align 4
  3927. @@:
  3928.         cmp     esi, 32
  3929.         jnz     @f
  3930.         mov     ebp, putimage_get32bpp
  3931.         mov     esi, putimage_init32bpp
  3932.         jmp     sys_putimage_bpp
  3933. ;--------------------------------------
  3934. align 4
  3935. @@:
  3936.         pop     ebp esi ebp
  3937.         ret
  3938. ;-----------------------------------------------------------------------------
  3939. align 4
  3940. put_mono_image:
  3941.         push    ebp esi ebp
  3942.         mov     ebp, putimage_get1bpp
  3943.         mov     esi, putimage_init1bpp
  3944.         jmp     sys_putimage_bpp
  3945. ;-----------------------------------------------------------------------------
  3946. align 4
  3947. put_2bit_image:
  3948.         push    ebp esi ebp
  3949.         mov     ebp, putimage_get2bpp
  3950.         mov     esi, putimage_init2bpp
  3951.         jmp     sys_putimage_bpp
  3952. ;-----------------------------------------------------------------------------
  3953. align 4
  3954. put_4bit_image:
  3955.         push    ebp esi ebp
  3956.         mov     ebp, putimage_get4bpp
  3957.         mov     esi, putimage_init4bpp
  3958.         jmp     sys_putimage_bpp
  3959. ;-----------------------------------------------------------------------------
  3960. align 4
  3961. putimage_init24bpp:
  3962.         lea     eax, [eax*3]
  3963. putimage_init8bpp:
  3964. putimage_init9bpp:
  3965.         ret
  3966. ;-----------------------------------------------------------------------------
  3967. align 16
  3968. putimage_get24bpp:
  3969.         movzx   eax, byte [esi+2]
  3970.         shl     eax, 16
  3971.         mov     ax, [esi]
  3972.         add     esi, 3
  3973.         ret     4
  3974. ;-----------------------------------------------------------------------------
  3975. align 16
  3976. putimage_get8bpp:
  3977.         movzx   eax, byte [esi]
  3978.         push    edx
  3979.         mov     edx, [esp+8]
  3980.         mov     eax, [edx+eax*4]
  3981.         pop     edx
  3982.         inc     esi
  3983.         ret     4
  3984. ;-----------------------------------------------------------------------------
  3985. align 16
  3986. putimage_get9bpp:
  3987.         lodsb
  3988.         mov     ah, al
  3989.         shl     eax, 8
  3990.         mov     al, ah
  3991.         ret     4
  3992. ;-----------------------------------------------------------------------------
  3993. align 4
  3994. putimage_init1bpp:
  3995.         add     eax, ecx
  3996.         push    ecx
  3997.         add     eax, 7
  3998.         add     ecx, 7
  3999.         shr     eax, 3
  4000.         shr     ecx, 3
  4001.         sub     eax, ecx
  4002.         pop     ecx
  4003.         ret
  4004. ;-----------------------------------------------------------------------------
  4005. align 16
  4006. putimage_get1bpp:
  4007.         push    edx
  4008.         mov     edx, [esp+8]
  4009.         mov     al, [edx]
  4010.         add     al, al
  4011.         jnz     @f
  4012.         lodsb
  4013.         adc     al, al
  4014. @@:
  4015.         mov     [edx], al
  4016.         sbb     eax, eax
  4017.         and     eax, [edx+8]
  4018.         add     eax, [edx+4]
  4019.         pop     edx
  4020.         ret     4
  4021. ;-----------------------------------------------------------------------------
  4022. align 4
  4023. putimage_init2bpp:
  4024.         add     eax, ecx
  4025.         push    ecx
  4026.         add     ecx, 3
  4027.         add     eax, 3
  4028.         shr     ecx, 2
  4029.         shr     eax, 2
  4030.         sub     eax, ecx
  4031.         pop     ecx
  4032.         ret
  4033. ;-----------------------------------------------------------------------------
  4034. align 16
  4035. putimage_get2bpp:
  4036.         push    edx
  4037.         mov     edx, [esp+8]
  4038.         mov     al, [edx]
  4039.         mov     ah, al
  4040.         shr     al, 6
  4041.         shl     ah, 2
  4042.         jnz     .nonewbyte
  4043.         lodsb
  4044.         mov     ah, al
  4045.         shr     al, 6
  4046.         shl     ah, 2
  4047.         add     ah, 1
  4048. .nonewbyte:
  4049.         mov     [edx], ah
  4050.         mov     edx, [edx+4]
  4051.         movzx   eax, al
  4052.         mov     eax, [edx+eax*4]
  4053.         pop     edx
  4054.         ret     4
  4055. ;-----------------------------------------------------------------------------
  4056. align 4
  4057. putimage_init4bpp:
  4058.         add     eax, ecx
  4059.         push    ecx
  4060.         add     ecx, 1
  4061.         inc     eax      ;add   eax, 1
  4062.         shr     ecx, 1
  4063.         shr     eax, 1
  4064.         sub     eax, ecx
  4065.         pop     ecx
  4066.         ret
  4067. ;-----------------------------------------------------------------------------
  4068. align 16
  4069. putimage_get4bpp:
  4070.         push    edx
  4071.         mov     edx, [esp+8]
  4072.         add     byte [edx], 80h
  4073.         jc      @f
  4074.         movzx   eax, byte [edx+1]
  4075.         mov     edx, [edx+4]
  4076.         and     eax, 0x0F
  4077.         mov     eax, [edx+eax*4]
  4078.         pop     edx
  4079.         ret     4
  4080. @@:
  4081.         movzx   eax, byte [esi]
  4082.         add     esi, 1
  4083.         mov     [edx+1], al
  4084.         shr     eax, 4
  4085.         mov     edx, [edx+4]
  4086.         mov     eax, [edx+eax*4]
  4087.         pop     edx
  4088.         ret     4
  4089. ;-----------------------------------------------------------------------------
  4090. align 4
  4091. putimage_init32bpp:
  4092.         shl     eax, 2
  4093.         ret
  4094. ;-----------------------------------------------------------------------------
  4095. align 16
  4096. putimage_get32bpp:
  4097.         lodsd
  4098.         ret     4
  4099. ;-----------------------------------------------------------------------------
  4100. align 4
  4101. putimage_init15bpp:
  4102. putimage_init16bpp:
  4103.         add     eax, eax
  4104.         ret
  4105. ;-----------------------------------------------------------------------------
  4106. align 16
  4107. putimage_get15bpp:
  4108. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4109.         push    ecx edx
  4110.         movzx   eax, word [esi]
  4111.         add     esi, 2
  4112.         mov     ecx, eax
  4113.         mov     edx, eax
  4114.         and     eax, 0x1F
  4115.         and     ecx, 0x1F shl 5
  4116.         and     edx, 0x1F shl 10
  4117.         shl     eax, 3
  4118.         shl     ecx, 6
  4119.         shl     edx, 9
  4120.         or      eax, ecx
  4121.         or      eax, edx
  4122.         pop     edx ecx
  4123.         ret     4
  4124. ;-----------------------------------------------------------------------------
  4125. align 16
  4126. putimage_get16bpp:
  4127. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4128.         push    ecx edx
  4129.         movzx   eax, word [esi]
  4130.         add     esi, 2
  4131.         mov     ecx, eax
  4132.         mov     edx, eax
  4133.         and     eax, 0x1F
  4134.         and     ecx, 0x3F shl 5
  4135.         and     edx, 0x1F shl 11
  4136.         shl     eax, 3
  4137.         shl     ecx, 5
  4138.         shl     edx, 8
  4139.         or      eax, ecx
  4140.         or      eax, edx
  4141.         pop     edx ecx
  4142.         ret     4
  4143. ;-----------------------------------------------------------------------------
  4144. ;align 4
  4145. ; eax x beginning
  4146. ; ebx y beginning
  4147. ; ecx x end
  4148.         ; edx y end
  4149. ; edi color
  4150. ;__sys_drawbar:
  4151. ;        mov     esi, [current_slot]
  4152. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4153. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4154. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4155. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4156. ;--------------------------------------
  4157. ;align 4
  4158. ;.forced:
  4159. ;        call    vesa20_drawbar
  4160. ;        call    [draw_pointer]
  4161. ;        ret
  4162. ;-----------------------------------------------------------------------------
  4163. align 4
  4164. kb_write_wait_ack:
  4165.  
  4166.         push    ecx edx
  4167.  
  4168.         mov     dl, al
  4169.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4170. .wait_output_ready:
  4171.         in      al, 0x64
  4172.         test    al, 2
  4173.         jz      @f
  4174.         loop    .wait_output_ready
  4175.         mov     ah, 1
  4176.         jmp     .nothing
  4177. @@:
  4178.         mov     al, dl
  4179.         out     0x60, al
  4180.         mov     ecx, 0xfffff; last 0xffff, new value in view of fast CPU's
  4181. .wait_ack:
  4182.         in      al, 0x64
  4183.         test    al, 1
  4184.         jnz     @f
  4185.         loop    .wait_ack
  4186.         mov     ah, 1
  4187.         jmp     .nothing
  4188. @@:
  4189.         in      al, 0x60
  4190.         xor     ah, ah
  4191.  
  4192. .nothing:
  4193.         pop     edx ecx
  4194.  
  4195.         ret
  4196. ;-----------------------------------------------------------------------------
  4197.  
  4198. if used _rdtsc
  4199. _rdtsc:
  4200.         bt      [cpu_caps], CAPS_TSC
  4201.         jnc     ret_rdtsc
  4202.         rdtsc
  4203.         ret
  4204.    ret_rdtsc:
  4205.         mov     edx, 0xffffffff
  4206.         mov     eax, 0xffffffff
  4207.         ret
  4208. end if
  4209.  
  4210. sys_msg_board_str:
  4211.  
  4212.         pushad
  4213.    @@:
  4214.         cmp     [esi], byte 0
  4215.         je      @f
  4216.         mov     ebx, 1
  4217.         movzx   ecx, byte [esi]
  4218.         call    sys_msg_board
  4219.         inc     esi
  4220.         jmp     @b
  4221.    @@:
  4222.         popad
  4223.         ret
  4224.  
  4225. sys_msg_board_byte:
  4226. ; in: al = byte to display
  4227. ; out: nothing
  4228. ; destroys: nothing
  4229.         pushad
  4230.         mov     ecx, 2
  4231.         shl     eax, 24
  4232.         jmp     @f
  4233.  
  4234. sys_msg_board_word:
  4235. ; in: ax = word to display
  4236. ; out: nothing
  4237. ; destroys: nothing
  4238.         pushad
  4239.         mov     ecx, 4
  4240.         shl     eax, 16
  4241.         jmp     @f
  4242.  
  4243. sys_msg_board_dword:
  4244. ; in: eax = dword to display
  4245. ; out: nothing
  4246. ; destroys: nothing
  4247.         pushad
  4248.         mov     ecx, 8
  4249. @@:
  4250.         push    ecx
  4251.         rol     eax, 4
  4252.         push    eax
  4253.         and     al, 0xF
  4254.         cmp     al, 10
  4255.         sbb     al, 69h
  4256.         das
  4257.         mov     cl, al
  4258.         xor     ebx, ebx
  4259.         inc     ebx
  4260.         call    sys_msg_board
  4261.         pop     eax
  4262.         pop     ecx
  4263.         loop    @b
  4264.         popad
  4265.         ret
  4266.  
  4267. msg_board_data_size = 65536 ; Must be power of two
  4268.  
  4269. uglobal
  4270. msg_board_data  rb  msg_board_data_size
  4271. msg_board_count dd  ?
  4272. endg
  4273.  
  4274. iglobal
  4275. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4276. endg
  4277.  
  4278. sys_msg_board:
  4279. ; ebx=1 -> write, cl = byte to write
  4280. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4281.         push    eax ebx
  4282.         mov     eax, ebx
  4283.         mov     ebx, ecx
  4284.         mov     ecx, [msg_board_count]
  4285.         cmp     eax, 1
  4286.         jne     .read
  4287.  
  4288. if defined debug_com_base
  4289.         push    dx ax
  4290. @@: ; wait for empty transmit register
  4291.         mov     dx, debug_com_base+5
  4292.         in      al, dx
  4293.         test    al, 1 shl 5
  4294.         jz      @r
  4295.         mov     dx, debug_com_base      ; Output the byte
  4296.         mov     al, bl
  4297.         out     dx, al
  4298.         pop     ax dx
  4299. end if
  4300.  
  4301.         mov     [msg_board_data+ecx], bl
  4302.         cmp     byte [debug_direct_print], 1
  4303.         jnz     .end
  4304.         pusha
  4305.         lea     edx, [msg_board_data+ecx]
  4306.         mov     ecx, 0x40FFFFFF
  4307.         mov     ebx, [msg_board_pos]
  4308.         mov     edi, 1
  4309.         mov     esi, edi ;1
  4310.         call    dtext
  4311.         popa
  4312.         add     word [msg_board_pos+2], 6
  4313.         cmp     word [msg_board_pos+2], 105*6
  4314.         jnc     @f
  4315.         cmp     bl, 10
  4316.         jnz     .end
  4317. @@:
  4318.         mov     word [msg_board_pos+2], 42*6
  4319.         add     word [msg_board_pos], 10
  4320.         mov     eax, [_display.height]
  4321.         sub     eax, 10
  4322.         cmp     ax, word [msg_board_pos]
  4323.         jnc     @f
  4324.         mov     word [msg_board_pos], 10
  4325. @@:
  4326.         pusha
  4327.         mov     eax, [msg_board_pos]
  4328.         movzx   ebx, ax
  4329.         shr     eax, 16
  4330.         mov     edx, 105*6
  4331.         xor     ecx, ecx
  4332.         mov     edi, 1
  4333.         mov     esi, 9
  4334. @@:
  4335.         call    hline
  4336.         inc     ebx
  4337.         dec     esi
  4338.         jnz     @b
  4339.         popa
  4340. .end:
  4341.         inc     ecx
  4342.         and     ecx, msg_board_data_size - 1
  4343.         mov     [msg_board_count], ecx
  4344. .ret:
  4345.         pop     ebx eax
  4346.         ret
  4347.  
  4348. @@:
  4349.         mov     [esp+32], ecx
  4350.         mov     [esp+20], ecx
  4351.         jmp     .ret
  4352.  
  4353. .read:
  4354.         cmp     eax, 2
  4355.         jne     .ret
  4356.         test    ecx, ecx
  4357.         jz      @b
  4358.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4359.         mov     eax, msg_board_data+1
  4360.         mov     ebx, msg_board_data
  4361.         movzx   edx, byte [ebx]
  4362.         call    memmove
  4363.         dec     [msg_board_count]
  4364.         mov     [esp + 32], edx ;eax
  4365.         mov     [esp + 20], dword 1
  4366.         ret
  4367.  
  4368. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4369. ;; 66 sys function.                                                ;;
  4370. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4371. ;; out eax                                                         ;;
  4372. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4373. iglobal
  4374. align 4
  4375. f66call:
  4376.            dd sys_process_def.1   ; 1 = set keyboard mode
  4377.            dd sys_process_def.2   ; 2 = get keyboard mode
  4378.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4379.            dd sys_process_def.4   ; 4 = set system-wide hotkey
  4380.            dd sys_process_def.5   ; 5 = delete installed hotkey
  4381.            dd sys_process_def.6   ; 6 = disable input, work only hotkeys
  4382.            dd sys_process_def.7   ; 7 = enable input, opposition to f.66.6
  4383. endg
  4384. ;-----------------------------------------------------------------------------
  4385. align 4
  4386. sys_process_def:
  4387.         dec     ebx
  4388.         cmp     ebx, 7
  4389.         jae     .not_support    ;if >=8 then or eax,-1
  4390.  
  4391.         mov     edi, [current_slot_idx]
  4392.         jmp     dword [f66call+ebx*4]
  4393.  
  4394. .not_support:
  4395.         or      eax, -1
  4396.         ret
  4397. ;-----------------------------------------------------------------------------
  4398. align 4
  4399. .1:
  4400.         shl     edi, 8
  4401.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4402.  
  4403.         ret
  4404. ;-----------------------------------------------------------------------------
  4405. align 4
  4406. .2:                             ; 2 = get keyboard mode
  4407.         shl     edi, 8
  4408.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4409.         mov     [esp+32], eax
  4410.         ret
  4411. ;-----------------------------------------------------------------------------
  4412. align 4
  4413. .3:                             ;3 = get keyboard ctrl, alt, shift
  4414.         mov     eax, [kb_state]
  4415.         mov     [esp+32], eax
  4416.         ret
  4417. ;-----------------------------------------------------------------------------
  4418. align 4
  4419. .4:
  4420.         mov     eax, hotkey_list
  4421. @@:
  4422.         cmp     dword [eax+8], 0
  4423.         jz      .found_free
  4424.         add     eax, 16
  4425.         cmp     eax, hotkey_list+16*256
  4426.         jb      @b
  4427.         mov     dword [esp+32], 1
  4428.         ret
  4429. .found_free:
  4430.         mov     [eax+8], edi
  4431.         mov     [eax+4], edx
  4432.         movzx   ecx, cl
  4433.         lea     ecx, [hotkey_scancodes+ecx*4]
  4434.         mov     edx, [ecx]
  4435.         mov     [eax], edx
  4436.         mov     [ecx], eax
  4437.         mov     [eax+12], ecx
  4438.         test    edx, edx
  4439.         jz      @f
  4440.         mov     [edx+12], eax
  4441. @@:
  4442.         and     dword [esp+32], 0
  4443.         ret
  4444. ;-----------------------------------------------------------------------------
  4445. align 4
  4446. .5:
  4447.         movzx   ebx, cl
  4448.         lea     ebx, [hotkey_scancodes+ebx*4]
  4449.         mov     eax, [ebx]
  4450. .scan:
  4451.         test    eax, eax
  4452.         jz      .notfound
  4453.         cmp     [eax+8], edi
  4454.         jnz     .next
  4455.         cmp     [eax+4], edx
  4456.         jz      .found
  4457. .next:
  4458.         mov     eax, [eax]
  4459.         jmp     .scan
  4460. .notfound:
  4461.         mov     dword [esp+32], 1
  4462.         ret
  4463. .found:
  4464.         mov     ecx, [eax]
  4465.         jecxz   @f
  4466.         mov     edx, [eax+12]
  4467.         mov     [ecx+12], edx
  4468. @@:
  4469.         mov     ecx, [eax+12]
  4470.         mov     edx, [eax]
  4471.         mov     [ecx], edx
  4472.         xor     edx, edx
  4473.         mov     [eax+4], edx
  4474.         mov     [eax+8], edx
  4475.         mov     [eax+12], edx
  4476.         mov     [eax], edx
  4477.         mov     [esp+32], edx
  4478.         ret
  4479. ;-----------------------------------------------------------------------------
  4480. align 4
  4481. .6:
  4482.         pushfd
  4483.         cli
  4484.         mov     eax, [PID_lock_input]
  4485.         test    eax, eax
  4486.         jnz     @f
  4487. ; get current PID
  4488.         mov     eax, [current_slot_idx]
  4489.         shl     eax, 5
  4490.         mov     eax, [eax+TASK_TABLE+TASKDATA.pid]
  4491. ; set current PID for lock input
  4492.         mov     [PID_lock_input], eax
  4493. @@:
  4494.         popfd
  4495.         ret
  4496. ;-----------------------------------------------------------------------------
  4497. align 4
  4498. .7:
  4499.         mov     eax, [PID_lock_input]
  4500.         test    eax, eax
  4501.         jz      @f
  4502. ; get current PID
  4503.         mov     ebx, [current_slot_idx]
  4504.         shl     ebx, 5
  4505.         mov     ebx, [ebx+TASK_TABLE+TASKDATA.pid]
  4506. ; compare current lock input with current PID
  4507.         cmp     ebx, eax
  4508.         jne     @f
  4509.  
  4510.         xor     eax, eax
  4511.         mov     [PID_lock_input], eax
  4512. @@:
  4513.         ret
  4514. ;-----------------------------------------------------------------------------
  4515. uglobal
  4516.   PID_lock_input dd 0x0
  4517. endg
  4518. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4519. ;; 61 sys function.                                                ;;
  4520. ;; in eax=61,ebx in [1..3]                                         ;;
  4521. ;; out eax                                                         ;;
  4522. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4523. iglobal
  4524. align 4
  4525. f61call:
  4526.            dd sys_gs.1   ; resolution
  4527.            dd sys_gs.2   ; bits per pixel
  4528.            dd sys_gs.3   ; bytes per scanline
  4529. endg
  4530.  
  4531.  
  4532. align 4
  4533.  
  4534. sys_gs:                         ; direct screen access
  4535.         dec     ebx
  4536.         cmp     ebx, 2
  4537.         ja      .not_support
  4538.         jmp     dword [f61call+ebx*4]
  4539. .not_support:
  4540.         or      [esp+32], dword -1
  4541.         ret
  4542.  
  4543.  
  4544. .1:                             ; resolution
  4545.         mov     eax, [_display.width]
  4546.         shl     eax, 16
  4547.         mov     ax, word [_display.height]
  4548.         mov     [esp+32], eax
  4549.         ret
  4550. .2:                             ; bits per pixel
  4551.         mov     eax, [_display.bits_per_pixel]
  4552.         mov     [esp+32], eax
  4553.         ret
  4554. .3:                             ; bytes per scanline
  4555.         mov     eax, [_display.lfb_pitch]
  4556.         mov     [esp+32], eax
  4557.         ret
  4558.  
  4559. align 4
  4560. syscall_getscreensize:                  ; GetScreenSize
  4561.         mov     ax, word [_display.width]
  4562.         dec     ax
  4563.         shl     eax, 16
  4564.         mov     ax, word [_display.height]
  4565.         dec     ax
  4566.         mov     [esp + 32], eax
  4567.         ret
  4568. ;-----------------------------------------------------------------------------
  4569. align 4
  4570. syscall_cdaudio:
  4571. ; ECX - position of CD/DVD-drive
  4572. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  4573. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  4574. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  4575.         cmp     ecx, 11
  4576.         ja      .exit
  4577.  
  4578.         mov     eax, ecx
  4579.         shr     eax, 2
  4580.         lea     eax, [eax*5]
  4581.         mov     al, [eax+DRIVE_DATA+1]
  4582.  
  4583.         push    ecx ebx
  4584.         mov     ebx, ecx
  4585.         and     ebx, 11b
  4586.         shl     ebx, 1
  4587.         mov     cl, 6
  4588.         sub     cl, bl
  4589.         shr     al, cl
  4590.         test    al, 2 ; it's not an ATAPI device
  4591.         pop     ebx ecx
  4592.  
  4593.         jz      .exit
  4594.  
  4595.         cmp     ebx, 4
  4596.         je      .eject
  4597.  
  4598.         cmp     ebx, 5
  4599.         je      .load
  4600. ;--------------------------------------
  4601. .exit:
  4602.         ret
  4603. ;--------------------------------------
  4604. .load:
  4605.         call    .reserve
  4606.         call    LoadMedium
  4607.         jmp     .free
  4608. ;--------------------------------------
  4609. .eject:
  4610.         call    .reserve
  4611.         call    clear_CD_cache
  4612.         call    allow_medium_removal
  4613.         call    EjectMedium
  4614.         jmp     .free
  4615. ;--------------------------------------
  4616. .reserve:
  4617.         call    reserve_cd
  4618.  
  4619.         mov     ebx, ecx
  4620.         inc     ebx
  4621.         mov     [cdpos], ebx
  4622.  
  4623.         mov     eax, ecx
  4624.         shr     eax, 1
  4625.         and     eax, 1
  4626.         inc     eax
  4627.         mov     [ChannelNumber], al
  4628.         mov     eax, ecx
  4629.         and     eax, 1
  4630.         mov     [DiskNumber], al
  4631.         call    reserve_cd_channel
  4632.         ret
  4633. ;--------------------------------------
  4634. .free:
  4635.         call    free_cd_channel
  4636.         and     [cd_status], 0
  4637.         ret
  4638. ;-----------------------------------------------------------------------------
  4639. align 4
  4640. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  4641.         cmp     ebx, [_display.width]
  4642.         jb      @f
  4643.         cmp     ecx, [_display.height]
  4644.         jb      @f
  4645.         xor     eax, eax
  4646.         jmp     .store
  4647. ;--------------------------------------
  4648. align 4
  4649. @@:
  4650.         mov     eax, [d_width_calc_area + ecx*4]
  4651.         add     eax, [_display.win_map]
  4652.         movzx   eax, byte[eax+ebx]        ; get value for current point
  4653. ;--------------------------------------
  4654. align 4
  4655. .store:
  4656.         mov     [esp + 32], eax
  4657.         ret
  4658. ;-----------------------------------------------------------------------------
  4659. align 4
  4660. syscall_getpixel:                       ; GetPixel
  4661.         mov     ecx, [_display.width]
  4662.         xor     edx, edx
  4663.         mov     eax, ebx
  4664.         div     ecx
  4665.         mov     ebx, edx
  4666.         xchg    eax, ebx
  4667.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4668.         call    dword [GETPIXEL]; eax - x, ebx - y
  4669.         mov     [esp + 32], ecx
  4670.         ret
  4671. ;-----------------------------------------------------------------------------
  4672. align 4
  4673. syscall_getarea:
  4674. ;eax = 36
  4675. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4676. ;ecx = [size x]*65536 + [size y]
  4677. ;edx = [start x]*65536 + [start y]
  4678.         pushad
  4679.         mov     edi, ebx
  4680.         mov     eax, edx
  4681.         shr     eax, 16
  4682.         mov     ebx, edx
  4683.         and     ebx, 0xffff
  4684.         dec     eax
  4685.         dec     ebx
  4686.      ; eax - x, ebx - y
  4687.         mov     edx, ecx
  4688.  
  4689.         shr     ecx, 16
  4690.         and     edx, 0xffff
  4691.         mov     esi, ecx
  4692.      ; ecx - size x, edx - size y
  4693.  
  4694.         mov     ebp, edx
  4695.         lea     ebp, [ebp*3]
  4696.         imul    ebp, esi
  4697.         stdcall is_region_userspace, edi, ebp
  4698.         jnz     .exit
  4699.  
  4700.         mov     ebp, edx
  4701.         dec     ebp
  4702.         lea     ebp, [ebp*3]
  4703.  
  4704.         imul    ebp, esi
  4705.  
  4706.         mov     esi, ecx
  4707.         dec     esi
  4708.         lea     esi, [esi*3]
  4709.  
  4710.         add     ebp, esi
  4711.         add     ebp, edi
  4712.  
  4713.         add     ebx, edx
  4714. ;--------------------------------------
  4715. align 4
  4716. .start_y:
  4717.         push    ecx edx
  4718. ;--------------------------------------
  4719. align 4
  4720. .start_x:
  4721.         push    eax ebx ecx
  4722.         add     eax, ecx
  4723.  
  4724.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4725.         call    dword [GETPIXEL]; eax - x, ebx - y
  4726.  
  4727.         mov     [ebp], cx
  4728.         shr     ecx, 16
  4729.         mov     [ebp+2], cl
  4730.  
  4731.         pop     ecx ebx eax
  4732.         sub     ebp, 3
  4733.         dec     ecx
  4734.         jnz     .start_x
  4735.         pop     edx ecx
  4736.         dec     ebx
  4737.         dec     edx
  4738.         jnz     .start_y
  4739.        
  4740. .exit:
  4741.         popad
  4742.         ret
  4743. ;-----------------------------------------------------------------------------
  4744.  
  4745. align 4
  4746. syscall_threads:                        ; CreateThreads
  4747. ;
  4748. ;   ecx=thread entry point
  4749. ;   edx=thread stack pointer
  4750. ;
  4751. ; on return : eax = pid
  4752.  
  4753.         xor     ebx, ebx
  4754.         call    new_sys_threads
  4755.  
  4756.         mov     [esp+32], eax
  4757.         ret
  4758.  
  4759. ;------------------------------------------------------------------------------
  4760. align 4
  4761. calculate_fast_getting_offset_for_WinMapAddress:
  4762. ; calculate data area for fast getting offset to _WinMapAddress
  4763.         xor     eax, eax
  4764.         mov     ecx, [_display.height]
  4765.         mov     edi, d_width_calc_area
  4766.         cld
  4767. @@:
  4768.         stosd
  4769.         add     eax, [_display.width]
  4770.         dec     ecx
  4771.         jnz     @r
  4772.         ret
  4773. ;------------------------------------------------------------------------------
  4774. align 4
  4775. calculate_fast_getting_offset_for_LFB:
  4776. ; calculate data area for fast getting offset to LFB
  4777.         xor     eax, eax
  4778.         mov     ecx, [_display.height]
  4779.         mov     edi, BPSLine_calc_area
  4780.         cld
  4781. @@:
  4782.         stosd
  4783.         add     eax, [_display.lfb_pitch]
  4784.         dec     ecx
  4785.         jnz     @r
  4786.         ret
  4787. ;------------------------------------------------------------------------------
  4788. align 4
  4789. set_screen:
  4790. ; in:
  4791. ; eax - new Screen_Max_X
  4792. ; ecx - new BytesPerScanLine
  4793. ; edx - new Screen_Max_Y
  4794.  
  4795.         pushfd
  4796.         cli
  4797.  
  4798.         mov     [_display.lfb_pitch], ecx
  4799.  
  4800.         mov     [screen_workarea.right], eax
  4801.         mov     [screen_workarea.bottom], edx
  4802.  
  4803.         push    ebx
  4804.         push    esi
  4805.         push    edi
  4806.  
  4807.         pushad
  4808.  
  4809.         cmp     [do_not_touch_winmap], 1
  4810.         je      @f
  4811.  
  4812.         stdcall kernel_free, [_display.win_map]
  4813.  
  4814.         mov     eax, [_display.width]
  4815.         mul     [_display.height]
  4816.         mov     [_display.win_map_size], eax
  4817.  
  4818.         stdcall kernel_alloc, eax
  4819.         mov     [_display.win_map], eax
  4820.         test    eax, eax
  4821.         jz      .epic_fail
  4822. ; store for f.18.24
  4823.         mov     eax, [_display.width]
  4824.         mov     [display_width_standard], eax
  4825.  
  4826.         mov     eax, [_display.height]
  4827.         mov     [display_height_standard], eax
  4828. @@:
  4829.         call    calculate_fast_getting_offset_for_WinMapAddress
  4830. ; for Qemu or non standart video cards
  4831. ; Unfortunately [BytesPerScanLine] does not always
  4832. ;                             equal to [_display.width] * [ScreenBPP] / 8
  4833.         call    calculate_fast_getting_offset_for_LFB
  4834.         popad
  4835.  
  4836.         call    repos_windows
  4837.         xor     eax, eax
  4838.         xor     ebx, ebx
  4839.         mov     ecx, [_display.width]
  4840.         mov     edx, [_display.height]
  4841.         dec     ecx
  4842.         dec     edx
  4843.         call    calculatescreen
  4844.         pop     edi
  4845.         pop     esi
  4846.         pop     ebx
  4847.  
  4848.         popfd
  4849.         ret
  4850.  
  4851. .epic_fail:
  4852.         hlt                     ; Houston, we've had a problem
  4853.  
  4854. ; --------------- APM ---------------------
  4855. uglobal
  4856. apm_entry       dp      0
  4857. apm_vf          dd      0
  4858. endg
  4859.  
  4860. align 4
  4861. sys_apm:
  4862.         xor     eax, eax
  4863.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4864.         jne     @f
  4865.         inc     eax
  4866.         or      dword [esp + 44], eax   ; error
  4867.         add     eax, 7
  4868.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  4869.         ret
  4870.  
  4871. @@:
  4872. ;       xchg    eax, ecx
  4873. ;       xchg    ebx, ecx
  4874.  
  4875.         cmp     dx, 3
  4876.         ja      @f
  4877.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4878.         mov     eax, [apm_vf]
  4879.         mov     [esp + 32], eax
  4880.         shr     eax, 16
  4881.         mov     [esp + 28], eax
  4882.         ret
  4883.  
  4884. @@:
  4885.  
  4886.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4887.         xchg    [master_tab], esi
  4888.         push    esi
  4889.         mov     edi, cr3
  4890.         mov     cr3, edi                ;flush TLB
  4891.  
  4892.         call    pword [apm_entry]       ;call APM BIOS
  4893.  
  4894.         xchg    eax, [esp]
  4895.         mov     [master_tab], eax
  4896.         mov     eax, cr3
  4897.         mov     cr3, eax
  4898.         pop     eax
  4899.  
  4900.         mov     [esp + 4 ], edi
  4901.         mov     [esp + 8], esi
  4902.         mov     [esp + 20], ebx
  4903.         mov     [esp + 24], edx
  4904.         mov     [esp + 28], ecx
  4905.         mov     [esp + 32], eax
  4906.         setc    al
  4907.         and     [esp + 44], byte 0xfe
  4908.         or      [esp + 44], al
  4909.         ret
  4910. ; -----------------------------------------
  4911.  
  4912. align 4
  4913. undefined_syscall:                      ; Undefined system call
  4914.         mov     [esp + 32], dword -1
  4915.         ret
  4916.  
  4917. align 4
  4918. ; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
  4919. ; @param base Base address of region
  4920. ; @param len Lenght of region
  4921. ; @return ZF = 1 if region in userspace memory,
  4922. ;         ZF = 0 otherwise
  4923. proc is_region_userspace stdcall, base:dword, len:dword
  4924.         push    eax
  4925.         mov     eax, [base]
  4926.  
  4927.         cmp     eax, OS_BASE-1
  4928.         ja      @f              ; zf
  4929.  
  4930.         add     eax, [len]
  4931.         jc      @f              ; zf
  4932.         cmp     eax, OS_BASE
  4933.         ja      @f              ; zf
  4934.  
  4935.         cmp     eax, eax        ; ZF
  4936. @@:
  4937.         pop     eax
  4938.         ret
  4939. endp
  4940.  
  4941. if ~ lang eq sp
  4942. diff16 "end of .text segment",0,$
  4943. end if
  4944.  
  4945. include "data32.inc"
  4946.  
  4947. __REV__ = __REV
  4948.  
  4949. if ~ lang eq sp
  4950. diff16 "end of kernel code",0,$
  4951. end if
  4952.