Subversion Repositories Kolibri OS

Rev

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