Subversion Repositories Kolibri OS

Rev

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