Subversion Repositories Kolibri OS

Rev

Rev 9612 | Rev 9614 | 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: 9613 $
  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.         ;shl     edx, 8
  2205.  
  2206.         ;cmp     [edx + SLOT_BASE + APPDATA.state], TSTATE_FREE
  2207.         cmp     [edx + TASK_TABLE + TASKDATA.state], TSTATE_FREE
  2208.         je      .fail
  2209.  
  2210.         ;shr     edx, 3
  2211.         cmp     ecx, 1
  2212.         jnz     .set_zmod
  2213.  
  2214.         mov     al, [edx + window_data + WDATA.z_modif]
  2215.         jmp     .exit
  2216.  
  2217. .set_zmod:
  2218.         cmp     ecx, 2
  2219.         jnz     .fail
  2220.  
  2221.         mov     ebx, esi
  2222.         mov     esi, eax
  2223.  
  2224.         cmp     bl, ZPOS_ALWAYS_TOP
  2225.         jg      .fail
  2226.  
  2227.         mov     [edx + window_data + WDATA.z_modif], bl
  2228.  
  2229.         mov     eax, [edx + window_data + WDATA.box.left]
  2230.         mov     ebx, [edx + window_data + WDATA.box.top]
  2231.         mov     ecx, [edx + window_data + WDATA.box.width]
  2232.         mov     edx, [edx + window_data + WDATA.box.height]
  2233.         add     ecx, eax
  2234.         add     edx, ebx
  2235.         call    window._.set_screen
  2236.         call    window._.set_top_wnd
  2237.         call    window._.redraw_top_wnd
  2238.  
  2239.         shl     esi, 5
  2240.         mov     [esi + window_data + WDATA.fl_redraw], 1
  2241.  
  2242.  
  2243.         mov     eax, 1
  2244.         jmp     .exit
  2245. .fail:
  2246.         xor     eax, eax
  2247. .exit:
  2248.         mov     [esp+32], eax
  2249.         ret
  2250.  
  2251. ;------------------------------------------------------------------------------
  2252. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2253.         ;mov     eax, [TASK_TABLE+32+TASKDATA.cpu_usage]
  2254.         mov     eax, [SLOT_BASE+APPDATA.cpu_usage]
  2255.         mov     [esp+32], eax
  2256.         ret
  2257. ;------------------------------------------------------------------------------
  2258. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2259.         mov     eax, dword [cpu_freq]
  2260.         mov     [esp+32], eax
  2261.         ret
  2262. ;------------------------------------------------------------------------------
  2263. get_cpu_freq:
  2264.         mov     eax, dword [cpu_freq]
  2265.         mov     edx, dword [cpu_freq+4]
  2266.         ret
  2267. ;  SAVE ramdisk to /hd/1/kolibri.img
  2268. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2269.    include 'blkdev/rdsave.inc'
  2270. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2271. ;------------------------------------------------------------------------------
  2272. align 4
  2273. sysfn_getactive:        ; 18.7 = get active window
  2274.         mov     eax, [thread_count]
  2275.         movzx   eax, word [WIN_POS + eax*2]
  2276.         mov     [esp+32], eax
  2277.         ret
  2278. ;------------------------------------------------------------------------------
  2279. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2280. ;     cmp  ecx,1
  2281.         dec     ecx
  2282.         jnz     nogetsoundflag
  2283.         movzx   eax, byte [sound_flag]; get sound_flag
  2284.         mov     [esp+32], eax
  2285.         ret
  2286.  nogetsoundflag:
  2287. ;     cmp  ecx,2
  2288.         dec     ecx
  2289.         jnz     nosoundflag
  2290.         xor     byte [sound_flag], 1
  2291.  nosoundflag:
  2292.         ret
  2293. ;------------------------------------------------------------------------------
  2294. sysfn_minimize:         ; 18.10 = minimize window
  2295.         mov     [window_minimize], 1
  2296.         call    wakeup_osloop
  2297.         ret
  2298. ;------------------------------------------------------------------------------
  2299. align 4
  2300. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2301.         dec     ecx
  2302.         jnz     .exit
  2303. .small_table:
  2304.         stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
  2305.         jnz     .exit
  2306.         mov     edi, edx
  2307.         mov     esi, DRIVE_DATA
  2308.         mov     ecx, DRIVE_DATA_SIZE ;10
  2309.         cld
  2310.         rep movsb
  2311. .exit:
  2312.         ret
  2313. ;------------------------------------------------------------------------------
  2314. ;sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2315. ;        and     dword [esp+32], 0
  2316. ;        ret
  2317. ;------------------------------------------------------------------------------
  2318. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2319.         ; if given memory address belongs to kernel then error
  2320.         stdcall is_region_userspace, ecx, version_end-version_inf
  2321.         jnz     .addr_error
  2322.  
  2323.         mov     edi, ecx
  2324.         mov     esi, version_inf
  2325.         mov     ecx, version_end-version_inf
  2326.         rep movsb
  2327.         ret
  2328. .addr_error:    ; if given memory address is illegal
  2329.         mov     dword [esp+32], -1
  2330.         ret  
  2331. ;------------------------------------------------------------------------------
  2332. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2333.      ;wait retrace functions
  2334.  sys_wait_retrace:
  2335.         mov     edx, 0x3da
  2336.  WaitRetrace_loop:
  2337.         in      al, dx
  2338.         test    al, 1000b
  2339.         jz      WaitRetrace_loop
  2340.         and     [esp+32], dword 0
  2341.         ret
  2342. ;------------------------------------------------------------------------------
  2343. align 4
  2344. sysfn_centermouse:      ; 18.15 = mouse centered
  2345.         mov     eax, [_display.width]
  2346.         shr     eax, 1
  2347.         mov     [MOUSE_X], ax
  2348.         mov     eax, [_display.height]
  2349.         shr     eax, 1
  2350.         mov     [MOUSE_Y], ax
  2351.         call    wakeup_osloop
  2352.         xor     eax, eax
  2353.         and     [esp+32], eax
  2354.         ret
  2355. ;------------------------------------------------------------------------------
  2356. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2357.         cmp     ecx, 8
  2358.         jnc     @f
  2359.         jmp     dword [.table+ecx*4]
  2360. .get_mouse_acceleration:
  2361.         xor     eax, eax
  2362.         mov     ax, [mouse_speed_factor]
  2363.         mov     [esp+32], eax
  2364.         ret
  2365. .set_mouse_acceleration:
  2366.         mov     [mouse_speed_factor], dx
  2367.         ret
  2368. .get_mouse_delay:
  2369.         xor     eax, eax
  2370.         mov     al, [mouse_delay]
  2371.         mov     [esp+32], eax
  2372.         ret
  2373. .set_mouse_delay:
  2374.         mov     [mouse_delay], dl
  2375. @@:
  2376.         ret
  2377. .set_pointer_position:
  2378.         cmp     dx, word[_display.height]
  2379.         jae     @b
  2380.         rol     edx, 16
  2381.         cmp     dx, word[_display.width]
  2382.         jae     @b
  2383.         mov     [MOUSE_X], edx
  2384.         mov     [mouse_active], 1
  2385.         jmp     wakeup_osloop
  2386. .set_mouse_button:
  2387.         mov     [BTN_DOWN], edx
  2388.         mov     [mouse_active], 1
  2389.         jmp     wakeup_osloop
  2390. .get_doubleclick_delay:
  2391.         xor     eax, eax
  2392.         mov     al, [mouse_doubleclick_delay]
  2393.         mov     [esp+32], eax
  2394.         ret
  2395. .set_doubleclick_delay:
  2396.         mov     [mouse_doubleclick_delay], dl
  2397.         ret
  2398. align 4
  2399. .table:
  2400. dd      .get_mouse_acceleration
  2401. dd      .set_mouse_acceleration
  2402. dd      .get_mouse_delay
  2403. dd      .set_mouse_delay
  2404. dd      .set_pointer_position
  2405. dd      .set_mouse_button
  2406. dd      .get_doubleclick_delay
  2407. dd      .set_doubleclick_delay
  2408. ;------------------------------------------------------------------------------
  2409. sysfn_getfreemem:
  2410.         mov     eax, [pg_data.pages_free]
  2411.         shl     eax, 2
  2412.         mov     [esp+32], eax
  2413.         ret
  2414. ;------------------------------------------------------------------------------
  2415. sysfn_getallmem:
  2416.         mov     eax, [MEM_AMOUNT]
  2417.         shr     eax, 10
  2418.         mov     [esp+32], eax
  2419.         ret
  2420. ;------------------------------------------------------------------------------
  2421. sysfn_pid_to_slot:
  2422.         mov     eax, ecx
  2423.         call    pid_to_slot
  2424.         mov     [esp+32], eax
  2425.         ret
  2426. ;------------------------------------------------------------------------------
  2427. sysfn_min_rest_window:
  2428.         pushad
  2429.         mov     eax, edx ; ebx - operating
  2430.         shr     ecx, 1
  2431.         jnc     @f
  2432.         call    pid_to_slot
  2433. @@:
  2434.         or      eax, eax ; eax - number of slot
  2435.         jz      .error
  2436.         cmp     eax, 255    ; varify maximal slot number
  2437.         ja      .error
  2438.         movzx   eax, word [WIN_STACK + eax*2]
  2439.         shr     ecx, 1
  2440.         jc      .restore
  2441.  ; .minimize:
  2442.         call    minimize_window
  2443.         jmp     .exit
  2444. .restore:
  2445.         call    restore_minimized_window
  2446. .exit:
  2447.         popad
  2448.         xor     eax, eax
  2449.         mov     [esp+32], eax
  2450.         ret
  2451. .error:
  2452.         popad
  2453.         xor     eax, eax
  2454.         dec     eax
  2455.         mov     [esp+32], eax
  2456.         ret
  2457. ;------------------------------------------------------------------------------
  2458. sysfn_min_windows:
  2459.         call    minimize_all_window
  2460.         mov     [esp+32], eax
  2461.         call    change_task
  2462.         ret
  2463. ;------------------------------------------------------------------------------
  2464. sysfn_set_screen_sizes:
  2465.         cmp     [SCR_MODE], word 0x13
  2466.         jbe     .exit
  2467.  
  2468.         cmp     [_display.select_cursor], select_cursor
  2469.         jne     .exit
  2470.  
  2471.         cmp     ecx, [display_width_standard]
  2472.         ja      .exit
  2473.  
  2474.         cmp     edx, [display_height_standard]
  2475.         ja      .exit
  2476.  
  2477.         pushfd
  2478.         cli
  2479.         mov     eax, ecx
  2480.         mov     ecx, [_display.lfb_pitch]
  2481.         mov     [_display.width], eax
  2482.         dec     eax
  2483.         mov     [_display.height], edx
  2484.         dec     edx
  2485. ; eax - new Screen_Max_X
  2486. ; edx - new Screen_Max_Y
  2487.         mov     [do_not_touch_winmap], 1
  2488.         call    set_screen
  2489.         mov     [do_not_touch_winmap], 0
  2490.         popfd
  2491.         call    change_task
  2492. .exit:
  2493.         ret
  2494. ;------------------------------------------------------------------------------
  2495. uglobal
  2496. screen_workarea RECT
  2497. display_width_standard dd 0
  2498. display_height_standard dd 0
  2499. do_not_touch_winmap db 0
  2500. window_minimize db 0
  2501. sound_flag      db 0
  2502.  
  2503. endg
  2504.  
  2505. ;UID_NONE=0
  2506. ;UID_KOLIBRI=2    ;russian
  2507.  
  2508. iglobal
  2509. version_inf:
  2510.         db 0,7,7,0  ; version 0.7.7.0
  2511.         db 0
  2512. .rev    dd __REV__
  2513. version_end:
  2514. endg
  2515. ;------------------------------------------------------------------------------
  2516. align 4
  2517. sys_cachetodiskette:
  2518.         cmp     ebx, 1
  2519.         jb      .no_floppy_save
  2520.         cmp     ebx, 2
  2521.         ja      .no_floppy_save
  2522.         call    save_image
  2523.         mov     [esp + 32], eax
  2524.         ret
  2525. .no_floppy_save:
  2526.         mov     [esp + 32], dword 1
  2527.         ret
  2528. ;------------------------------------------------------------------------------
  2529.  
  2530. align 4
  2531. sys_getkey:
  2532.         mov     [esp + 32], dword 1
  2533.         ; test main buffer
  2534.         mov     ebx, [current_slot_idx]                          ; TOP OF WINDOW STACK
  2535.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2536.         mov     edx, [thread_count]
  2537.         cmp     ecx, edx
  2538.         jne     .finish
  2539.         cmp     [KEY_COUNT], byte 0
  2540.         je      .finish
  2541.         movzx   ax, byte [KEY_BUFF + 120 + 2]
  2542.         shl     eax, 8
  2543.         mov     al, byte [KEY_BUFF]
  2544.         shl     eax, 8
  2545.         push    eax
  2546.         dec     byte [KEY_COUNT]
  2547.         and     byte [KEY_COUNT], 127
  2548.         movzx   ecx, byte [KEY_COUNT]
  2549.         add     ecx, 2
  2550.         mov     eax, KEY_BUFF + 1
  2551.         mov     ebx, KEY_BUFF
  2552.         call    memmove
  2553.         add     eax, 120 + 2
  2554.         add     ebx, 120 + 2
  2555.         call    memmove
  2556.         pop     eax
  2557. ;--------------------------------------
  2558. align 4
  2559. .ret_eax:
  2560.         mov     [esp + 32], eax
  2561.         ret
  2562. ;--------------------------------------
  2563. align 4
  2564. .finish:
  2565. ; test hotkeys buffer
  2566.         mov     ecx, hotkey_buffer
  2567. ;--------------------------------------
  2568. align 4
  2569. @@:
  2570.         cmp     [ecx], ebx
  2571.         jz      .found
  2572.         add     ecx, 8
  2573.         cmp     ecx, hotkey_buffer + 120 * 8
  2574.         jb      @b
  2575.         ret
  2576. ;--------------------------------------
  2577. align 4
  2578. .found:
  2579.         mov     ax, [ecx + 6]
  2580.         shl     eax, 16
  2581.         mov     ah, [ecx + 4]
  2582.         mov     al, 2
  2583.         and     dword [ecx + 4], 0
  2584.         and     dword [ecx], 0
  2585.         jmp     .ret_eax
  2586. ;------------------------------------------------------------------------------
  2587. align 4
  2588. sys_getbutton:
  2589.         mov     ebx, [current_slot_idx]                         ; TOP OF WINDOW STACK
  2590.         mov     [esp + 32], dword 1
  2591.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2592.         mov     edx, [thread_count] ; less than 256 processes
  2593.         cmp     ecx, edx
  2594.         jne     .exit
  2595.         movzx   eax, byte [BTN_COUNT]
  2596.         test    eax, eax
  2597.         jz      .exit
  2598.         mov     eax, [BTN_BUFF]
  2599.         and     al, 0xFE                                    ; delete left button bit
  2600.         mov     [BTN_COUNT], byte 0
  2601.         mov     [esp + 32], eax
  2602. ;--------------------------------------
  2603. align 4
  2604. .exit:
  2605.         ret
  2606. ;------------------------------------------------------------------------------
  2607. align 4
  2608. sys_cpuusage:
  2609.  
  2610. ;  RETURN:
  2611. ;
  2612. ;  +00 dword     process cpu usage
  2613. ;  +04  word     position in windowing stack
  2614. ;  +06  word     windowing stack value at current position (cpu nro)
  2615. ;  +10 12 bytes  name
  2616. ;  +22 dword     start in mem
  2617. ;  +26 dword     used mem
  2618. ;  +30 dword     PID , process idenfification number
  2619. ;
  2620.         ; if given memory address belongs to kernel then error
  2621.         stdcall is_region_userspace, ebx, 0x4C
  2622.         jnz     .addr_error
  2623.  
  2624.         cmp     ecx, -1 ; who am I ?
  2625.         jne     .no_who_am_i
  2626.         mov     ecx, [current_slot_idx]
  2627.   .no_who_am_i:
  2628.         cmp     ecx, max_processes
  2629.         ja      .nofillbuf
  2630.  
  2631. ; +4: word: position of the window of thread in the window stack
  2632.         mov     ax, [WIN_STACK + ecx * 2]
  2633.         mov     [ebx+4], ax
  2634. ; +6: word: number of the thread slot, which window has in the window stack
  2635. ;           position ecx (has no relation to the specific thread)
  2636.         mov     ax, [WIN_POS + ecx * 2]
  2637.         mov     [ebx+6], ax
  2638.  
  2639.         shl     ecx, 8   ;5=32 8=256
  2640.  
  2641. ; +0: dword: memory usage
  2642.         ;mov     eax, [ecx+TASK_TABLE+TASKDATA.cpu_usage]
  2643.         mov     eax, [ecx-sizeof.APPDATA+SLOT_BASE+APPDATA.cpu_usage]
  2644.         mov     [ebx], eax
  2645. ; +10: 11 bytes: name of the process
  2646.         push    ecx
  2647.         lea     eax, [ecx+SLOT_BASE+APPDATA.app_name]
  2648.         add     ebx, 10
  2649.         mov     ecx, 11
  2650.         call    memmove
  2651.         pop     ecx
  2652.  
  2653.         shr     ecx, 3
  2654. ; +22: address of the process in memory
  2655. ; +26: size of used memory - 1
  2656.         push    edi
  2657.         lea     edi, [ebx+12]
  2658.         xor     eax, eax
  2659.         mov     edx, 0x100000*16
  2660.         cmp     ecx, 1 shl 5
  2661.         je      .os_mem
  2662.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.process]
  2663.         mov     edx, [edx+PROC.mem_used]
  2664.         mov     eax, std_application_base_address
  2665. .os_mem:
  2666.         stosd
  2667.         lea     eax, [edx-1]
  2668.         stosd
  2669.  
  2670. ; +30: PID/TID
  2671.         mov     eax, [ecx*8 + SLOT_BASE + APPDATA.tid]
  2672.         mov     eax, [ecx+TASK_TABLE+TASKDATA.pid]
  2673.         stosd
  2674.  
  2675.     ; window position and size
  2676.         push    esi
  2677.         lea     esi, [ecx + window_data + WDATA.box]
  2678.         movsd
  2679.         movsd
  2680.         movsd
  2681.         movsd
  2682.  
  2683.     ; Process state (+50)
  2684.         movzx   eax, byte [ecx*8 + SLOT_BASE + APPDATA.state]
  2685.         movzx   eax, byte [ecx+TASK_TABLE+TASKDATA.state]
  2686.         stosd
  2687.  
  2688.     ; Window client area box
  2689.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2690.         movsd
  2691.         movsd
  2692.         movsd
  2693.         movsd
  2694.  
  2695.     ; Window state
  2696.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2697.         stosb
  2698.  
  2699.     ; Event mask (+71)
  2700.         mov     EAX, dword [ecx*8 + SLOT_BASE + APPDATA.event_mask]
  2701.         mov     EAX, dword [ECX+TASK_TABLE+TASKDATA.event_mask]     ; delete
  2702.         stosd
  2703.  
  2704.     ; Keyboard mode (+75)
  2705.         mov     al, byte [ecx*8 + SLOT_BASE + APPDATA.keyboard_mode]
  2706.         stosb
  2707.  
  2708.         pop     esi
  2709.         pop     edi
  2710.  
  2711. .nofillbuf:
  2712.     ; return number of processes
  2713.  
  2714.         mov     eax, [thread_count]
  2715.         mov     [esp+32], eax
  2716.         ret
  2717.  
  2718. .addr_error:    ; if given memory address is illegal
  2719.         mov     dword [esp+32], -1
  2720.         ret  
  2721.  
  2722.  
  2723. ; redraw status
  2724. align 4
  2725. sys_redrawstat:
  2726.         cmp     ebx, 1
  2727.         jne     no_widgets_away
  2728.         ; buttons away
  2729.         mov     ecx, [current_slot_idx]
  2730.   sys_newba2:
  2731.         mov     edi, [BTN_ADDR]
  2732.         cmp     [edi], dword 0  ; empty button list ?
  2733.         je      end_of_buttons_away
  2734.         movzx   ebx, word [edi]
  2735.         inc     ebx
  2736.         mov     eax, edi
  2737.   sys_newba:
  2738.         dec     ebx
  2739.         jz      end_of_buttons_away
  2740.  
  2741.         add     eax, 0x10
  2742.         cmp     cx, [eax]
  2743.         jnz     sys_newba
  2744.  
  2745.         push    eax ebx ecx
  2746.         mov     ecx, ebx
  2747.         inc     ecx
  2748.         shl     ecx, 4
  2749.         mov     ebx, eax
  2750.         add     eax, 0x10
  2751.         call    memmove
  2752.         dec     dword [edi]
  2753.         pop     ecx ebx eax
  2754.  
  2755.         jmp     sys_newba2
  2756.  
  2757.   end_of_buttons_away:
  2758.  
  2759.         ret
  2760.  
  2761.   no_widgets_away:
  2762.  
  2763.         cmp     ebx, 2
  2764.         jnz     srl1
  2765.  
  2766.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2767.         add     edx, draw_data - TASK_TABLE
  2768.         mov     [edx + RECT.left], 0
  2769.         mov     [edx + RECT.top], 0
  2770.         mov     eax, [_display.width]
  2771.         dec     eax
  2772.         mov     [edx + RECT.right], eax
  2773.         mov     eax, [_display.height]
  2774.         dec     eax
  2775.         mov     [edx + RECT.bottom], eax
  2776.  
  2777.   srl1:
  2778.         ret
  2779.  
  2780. ;ok - 100% work
  2781. ;nt - not tested
  2782. ;---------------------------------------------------------------------------------------------
  2783. ;eax
  2784. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2785. ;1 - change task. Ret nothing. Block. ok.
  2786. ;2 - performance control
  2787. ; ebx
  2788. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2789. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2790. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2791. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2792. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2793. ;eax
  2794. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2795. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2796. ;---------------------------------------------------------------------------------------------
  2797. iglobal
  2798. align 4
  2799. sheduler:
  2800.         dd      sys_sheduler.00
  2801.         dd      change_task
  2802.         dd      sys_sheduler.02
  2803.         dd      sys_sheduler.03
  2804.         dd      sys_sheduler.04
  2805. endg
  2806. sys_sheduler:
  2807. ;rewritten by <Lrz>  29.12.2009
  2808.         jmp     dword [sheduler+ebx*4]
  2809. ;.shed_counter:
  2810. .00:
  2811.         mov     eax, [context_counter]
  2812.         mov     [esp+32], eax
  2813.         ret
  2814.  
  2815. .02:
  2816. ;.perf_control:
  2817.         inc     ebx                     ;before ebx=2, ebx=3
  2818.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  2819.         jz      cache_disable
  2820.  
  2821.         dec     ebx                     ;ebx=2
  2822.         cmp     ebx, ecx                ;
  2823.         jz      cache_enable            ;if ecx=2 and ebx=2
  2824.  
  2825.         dec     ebx                     ;ebx=1
  2826.         cmp     ebx, ecx
  2827.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  2828.  
  2829.         dec     ebx
  2830.         test    ebx, ecx                ;ebx=0 and ecx=0
  2831.         jz      modify_pce              ;if ecx=0
  2832.  
  2833.         ret
  2834.  
  2835. .03:
  2836. ;.rdmsr_instr:
  2837. ;now counter in ecx
  2838. ;(edx:eax) esi:edi => edx:esi
  2839.         mov     eax, esi
  2840.         mov     ecx, edx
  2841.         rdmsr
  2842.         mov     [esp+32], eax
  2843.         mov     [esp+20], edx           ;ret in ebx?
  2844.         ret
  2845.  
  2846. .04:
  2847. ;.wrmsr_instr:
  2848. ;now counter in ecx
  2849. ;(edx:eax) esi:edi => edx:esi
  2850.         ; Fast Call MSR can't be destroy
  2851.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  2852.         ; включаются/выключаются расширенные возможности
  2853.         cmp     edx, MSR_SYSENTER_CS
  2854.         je      @f
  2855.         cmp     edx, MSR_SYSENTER_ESP
  2856.         je      @f
  2857.         cmp     edx, MSR_SYSENTER_EIP
  2858.         je      @f
  2859.         cmp     edx, MSR_AMD_STAR
  2860.         je      @f
  2861.  
  2862.         mov     eax, esi
  2863.         mov     ecx, edx
  2864.         wrmsr
  2865.         ; mov   [esp + 32], eax
  2866.         ; mov   [esp + 20], edx ;ret in ebx?
  2867. @@:
  2868.         ret
  2869.  
  2870. cache_disable:
  2871.         mov     eax, cr0
  2872.         or      eax, 01100000000000000000000000000000b
  2873.         mov     cr0, eax
  2874.         wbinvd  ;set MESI
  2875.         ret
  2876.  
  2877. cache_enable:
  2878.         mov     eax, cr0
  2879.         and     eax, 10011111111111111111111111111111b
  2880.         mov     cr0, eax
  2881.         ret
  2882.  
  2883. is_cache_enabled:
  2884.         mov     eax, cr0
  2885.         mov     ebx, eax
  2886.         and     eax, 01100000000000000000000000000000b
  2887.         jz      cache_disabled
  2888.         mov     [esp+32], ebx
  2889. cache_disabled:
  2890.         mov     dword [esp+32], eax;0
  2891.         ret
  2892.  
  2893. modify_pce:
  2894.         mov     eax, cr4
  2895. ;       mov ebx,0
  2896. ;       or  bx,100000000b ;pce
  2897. ;       xor eax,ebx ;invert pce
  2898.         bts     eax, 8;pce=cr4[8]
  2899.         mov     cr4, eax
  2900.         mov     [esp+32], eax
  2901.         ret
  2902. ;---------------------------------------------------------------------------------------------
  2903.  
  2904.  
  2905. iglobal
  2906.   cpustring db 'CPU',0
  2907. endg
  2908.  
  2909. uglobal
  2910. background_defined    db    0    ; diamond, 11.04.2006
  2911. endg
  2912. ;-----------------------------------------------------------------------------
  2913. align 4
  2914. checkmisc:
  2915.         cmp     [ctrl_alt_del], 1
  2916.         jne     nocpustart
  2917.  
  2918.         mov     ebp, cpustring
  2919.         call    fs_execute_from_sysdir
  2920.  
  2921.         mov     [ctrl_alt_del], 0
  2922. ;--------------------------------------
  2923. align 4
  2924. nocpustart:
  2925.         cmp     [mouse_active], 1
  2926.         jne     mouse_not_active
  2927.         mov     [mouse_active], 0
  2928.  
  2929.         xor     edi, edi
  2930.         mov     ebx, TASK_TABLE
  2931.  
  2932.         mov     ecx, [thread_count]
  2933.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  2934.         shl     eax, 8
  2935.         push    eax
  2936.  
  2937.         movzx   eax, word [MOUSE_X]
  2938.         movzx   edx, word [MOUSE_Y]
  2939. ;--------------------------------------
  2940. align 4
  2941. .set_mouse_event:
  2942.         add     edi, sizeof.APPDATA
  2943.         add     ebx, sizeof.TASKDATA
  2944.         test    [ebx+TASKDATA.event_mask], 0x80000000
  2945.         jz      .pos_filter
  2946.  
  2947.         cmp     edi, [esp]                      ; skip if filtration active
  2948.         jne     .skip
  2949. ;--------------------------------------
  2950. align 4
  2951. .pos_filter:
  2952.         test    [ebx+TASKDATA.event_mask], 0x40000000
  2953.         jz      .set
  2954.  
  2955.         mov     esi, [ebx-twdw+WDATA.box.left]
  2956.         cmp     eax, esi
  2957.         jb      .skip
  2958.         add     esi, [ebx-twdw+WDATA.box.width]
  2959.         cmp     eax, esi
  2960.         ja      .skip
  2961.  
  2962.         mov     esi, [ebx-twdw+WDATA.box.top]
  2963.         cmp     edx, esi
  2964.         jb      .skip
  2965.         add     esi, [ebx-twdw+WDATA.box.height]
  2966.         cmp     edx, esi
  2967.         ja      .skip
  2968. ;--------------------------------------
  2969. align 4
  2970. .set:
  2971.         or      [edi+SLOT_BASE+APPDATA.occurred_events], EVENT_MOUSE
  2972. ;--------------------------------------
  2973. align 4
  2974. .skip:
  2975.         loop    .set_mouse_event
  2976.  
  2977.         pop     eax
  2978. ;--------------------------------------
  2979. align 4
  2980. mouse_not_active:
  2981.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  2982.         jz      nobackgr
  2983.  
  2984.         cmp     [background_defined], 0
  2985.         jz      nobackgr
  2986. ;--------------------------------------
  2987. align 4
  2988. backgr:
  2989.         mov     eax, [draw_data+32 + RECT.left]
  2990.         shl     eax, 16
  2991.         add     eax, [draw_data+32 + RECT.right]
  2992.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  2993.  
  2994.         mov     eax, [draw_data+32 + RECT.top]
  2995.         shl     eax, 16
  2996.         add     eax, [draw_data+32 + RECT.bottom]
  2997.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  2998.  
  2999.         call    drawbackground
  3000. ;        DEBUGF  1, "K : drawbackground\n"
  3001. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  3002. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  3003. ;--------- set event 5 start ----------
  3004.         push    ecx edi
  3005.         xor     edi, edi
  3006.         mov     ecx, [thread_count]
  3007. ;--------------------------------------
  3008. align 4
  3009. set_bgr_event:
  3010.         add     edi, sizeof.APPDATA
  3011.         mov     eax, [BG_Rect_X_left_right]
  3012.         mov     edx, [BG_Rect_Y_top_bottom]
  3013.         cmp     [edi+SLOT_BASE+APPDATA.draw_bgr_x], 0
  3014.         jz      .set
  3015. .join:
  3016.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3017.         jae     @f
  3018.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3019. @@:
  3020.         shr     eax, 16
  3021.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3022.         jbe     @f
  3023.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3024. @@:
  3025.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3026.         jae     @f
  3027.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3028. @@:
  3029.         shr     edx, 16
  3030.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3031.         jbe     @f
  3032.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3033. @@:
  3034.         jmp     .common
  3035. .set:
  3036.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_x], eax
  3037.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_y], edx
  3038. .common:
  3039.         or      [edi+SLOT_BASE+APPDATA.occurred_events], EVENT_BACKGROUND
  3040.         loop    set_bgr_event
  3041.         pop     edi ecx
  3042. ;--------- set event 5 stop -----------
  3043.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  3044.         jnz     backgr
  3045.  
  3046.         xor     eax, eax
  3047.         mov     [draw_data+32 + RECT.left], eax
  3048.         mov     [draw_data+32 + RECT.top], eax
  3049.         mov     [draw_data+32 + RECT.right], eax
  3050.         mov     [draw_data+32 + RECT.bottom], eax
  3051. ;--------------------------------------
  3052. align 4
  3053. nobackgr:
  3054. ; system shutdown request
  3055.         cmp     [SYS_SHUTDOWN], byte 0
  3056.         je      noshutdown
  3057.  
  3058.         mov     edx, [shutdown_processes]
  3059.  
  3060.         cmp     [SYS_SHUTDOWN], dl
  3061.         jne     noshutdown
  3062.  
  3063.         lea     ecx, [edx-1]
  3064.         mov     edx, OS_BASE+0x3040
  3065.         jecxz   no_mark_system_shutdown
  3066. ;--------------------------------------
  3067. align 4
  3068. markz:
  3069.         push    ecx edx
  3070.         cmp     [edx+TASKDATA.state], TSTATE_FREE
  3071.         jz      .nokill
  3072.         lea     edx, [(edx-(TASK_TABLE and 1FFFFFFFh))*8+SLOT_BASE]
  3073.         cmp     [edx+APPDATA.process], sys_proc
  3074.         jz      .nokill
  3075.         call    request_terminate
  3076.         jmp     .common
  3077. .nokill:
  3078.         dec     byte [SYS_SHUTDOWN]
  3079.         xor     eax, eax
  3080. .common:
  3081.         pop     edx ecx
  3082.         test    eax, eax
  3083.         jz      @f
  3084.         mov     [edx+TASKDATA.state], TSTATE_ZOMBIE
  3085.         ;mov     [edx+APPDATA.state], TSTATE_ZOMBIE
  3086. @@:
  3087.         add     edx, 0x20
  3088.         loop    markz
  3089.         call    wakeup_osloop
  3090. ;--------------------------------------
  3091. align 4
  3092. @@:
  3093. no_mark_system_shutdown:
  3094.         dec     byte [SYS_SHUTDOWN]
  3095.         je      system_shutdown
  3096. ;--------------------------------------
  3097. align 4
  3098. noshutdown:
  3099.         mov     eax, [thread_count]           ; termination
  3100.         mov     ebx, TASK_DATA+TASKDATA.state
  3101.         mov     esi, 1
  3102. ;--------------------------------------
  3103. align 4
  3104. newct:
  3105.         mov     cl, [ebx]
  3106.         cmp     cl, byte 3
  3107.         jz      .terminate
  3108.  
  3109.         cmp     cl, byte 4
  3110.         jnz     .noterminate
  3111. .terminate:
  3112.         pushad
  3113.         mov     ecx, eax
  3114.         shl     ecx, 8
  3115.         add     ecx, SLOT_BASE
  3116.         call    restore_default_cursor_before_killing
  3117.         popad
  3118.  
  3119.         pushad
  3120.         call    terminate
  3121.         popad
  3122.         cmp     byte [SYS_SHUTDOWN], 0
  3123.         jz      .noterminate
  3124.         dec     byte [SYS_SHUTDOWN]
  3125.         je      system_shutdown
  3126.  
  3127. .noterminate:
  3128.         add     ebx, 0x20
  3129.         inc     esi
  3130.         dec     eax
  3131.         jnz     newct
  3132.         ret
  3133. ;-----------------------------------------------------------------------------
  3134. align 4
  3135. redrawscreen:
  3136. ; eax , if process window_data base is eax, do not set flag/limits
  3137.  
  3138.         pushad
  3139.         push    eax
  3140.  
  3141. ;;;         mov   ebx,2
  3142. ;;;         call  delay_hs
  3143.  
  3144.          ;mov   ecx,0               ; redraw flags for apps
  3145.         xor     ecx, ecx
  3146. ;--------------------------------------
  3147. align 4
  3148. newdw2:
  3149.         inc     ecx
  3150.         push    ecx
  3151.  
  3152.         mov     eax, ecx
  3153.         shl     eax, 5
  3154.         add     eax, window_data
  3155.  
  3156.         cmp     eax, [esp+4]
  3157.         je      not_this_task
  3158.                                    ; check if window in redraw area
  3159.         mov     edi, eax
  3160.  
  3161.         cmp     ecx, 1             ; limit for background
  3162.         jz      bgli
  3163.  
  3164.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3165.         test    eax, eax
  3166.         jz      @f
  3167.         mov     al, [eax + WDATA.z_modif]
  3168.         cmp     [edi + WDATA.z_modif], al
  3169.         jg      ricino
  3170.       @@:
  3171.  
  3172.         mov     eax, [edi + WDATA.box.left]
  3173.         mov     ebx, [edi + WDATA.box.top]
  3174.  
  3175.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3176.         cmp     ecx, ebx
  3177.         jb      ricino
  3178.  
  3179.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3180.         cmp     ecx, eax
  3181.         jb      ricino
  3182.  
  3183.         mov     eax, [edi + WDATA.box.left]
  3184.         mov     ebx, [edi + WDATA.box.top]
  3185.         mov     ecx, [edi + WDATA.box.width]
  3186.         mov     edx, [edi + WDATA.box.height]
  3187.         add     ecx, eax
  3188.         add     edx, ebx
  3189.  
  3190.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3191.         cmp     edx, eax
  3192.         jb      ricino
  3193.  
  3194.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3195.         cmp     ecx, eax
  3196.         jb      ricino
  3197. ;--------------------------------------
  3198. align 4
  3199. bgli:
  3200.         cmp     dword[esp], 1
  3201.         jnz     .az
  3202.  
  3203.         cmp     [REDRAW_BACKGROUND], 0
  3204.         jz      .az
  3205.  
  3206.         mov     dl, 0
  3207.         lea     eax, [edi+draw_data-window_data]
  3208.         mov     ebx, [draw_limits.left]
  3209.         cmp     ebx, [eax+RECT.left]
  3210.         jae     @f
  3211.  
  3212.         mov     [eax+RECT.left], ebx
  3213.         mov     dl, 1
  3214. ;--------------------------------------
  3215. align 4
  3216. @@:
  3217.         mov     ebx, [draw_limits.top]
  3218.         cmp     ebx, [eax+RECT.top]
  3219.         jae     @f
  3220.  
  3221.         mov     [eax+RECT.top], ebx
  3222.         mov     dl, 1
  3223. ;--------------------------------------
  3224. align 4
  3225. @@:
  3226.         mov     ebx, [draw_limits.right]
  3227.         cmp     ebx, [eax+RECT.right]
  3228.         jbe     @f
  3229.  
  3230.         mov     [eax+RECT.right], ebx
  3231.         mov     dl, 1
  3232. ;--------------------------------------
  3233. align 4
  3234. @@:
  3235.         mov     ebx, [draw_limits.bottom]
  3236.         cmp     ebx, [eax+RECT.bottom]
  3237.         jbe     @f
  3238.  
  3239.         mov     [eax+RECT.bottom], ebx
  3240.         mov     dl, 1
  3241. ;--------------------------------------
  3242. align 4
  3243. @@:
  3244.         add     [REDRAW_BACKGROUND], dl
  3245.         call    wakeup_osloop
  3246.         jmp     newdw8
  3247. ;--------------------------------------
  3248. align 4
  3249. .az:
  3250.         mov     eax, edi
  3251.         add     eax, draw_data-window_data
  3252.  
  3253.         mov     ebx, [draw_limits.left]        ; set limits
  3254.         mov     [eax + RECT.left], ebx
  3255.         mov     ebx, [draw_limits.top]
  3256.         mov     [eax + RECT.top], ebx
  3257.         mov     ebx, [draw_limits.right]
  3258.         mov     [eax + RECT.right], ebx
  3259.         mov     ebx, [draw_limits.bottom]
  3260.         mov     [eax + RECT.bottom], ebx
  3261.  
  3262.         sub     eax, draw_data-window_data
  3263.  
  3264.         cmp     dword [esp], 1
  3265.         jne     nobgrd
  3266.         inc     [REDRAW_BACKGROUND]
  3267.         call    wakeup_osloop
  3268. ;--------------------------------------
  3269. align 4
  3270. newdw8:
  3271. nobgrd:
  3272. ;--------------------------------------
  3273.         push    eax  edi ebp
  3274.         mov     edi, [esp+12]
  3275.         cmp     edi, 1
  3276.         je      .found
  3277.  
  3278.         mov     eax, [draw_limits.left]
  3279.         mov     ebx, [draw_limits.top]
  3280.         mov     ecx, [draw_limits.right]
  3281.         sub     ecx, eax
  3282.         test    ecx, ecx
  3283.         jz      .not_found
  3284.  
  3285.         mov     edx, [draw_limits.bottom]
  3286.         sub     edx, ebx
  3287.         test    edx, edx
  3288.         jz      .not_found
  3289.  
  3290. ; eax - x, ebx - y
  3291. ; ecx - size x, edx - size y
  3292.         add     ebx, edx
  3293. ;--------------------------------------
  3294. align 4
  3295. .start_y:
  3296.         push    ecx
  3297. ;--------------------------------------
  3298. align 4
  3299. .start_x:
  3300.         add     eax, ecx
  3301.         mov     ebp, [d_width_calc_area + ebx*4]
  3302.         add     ebp, [_display.win_map]
  3303.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3304.         cmp     ebp, edi
  3305.         jne     @f
  3306.  
  3307.         pop     ecx
  3308.         jmp     .found
  3309. ;--------------------------------------
  3310. align 4
  3311. @@:
  3312.         sub     eax, ecx
  3313.  
  3314.         dec     ecx
  3315.         jns     .start_x
  3316.  
  3317.         pop     ecx
  3318.         dec     ebx
  3319.         dec     edx
  3320.         jns     .start_y
  3321. ;--------------------------------------
  3322. align 4
  3323. .not_found:
  3324.         pop     ebp edi eax
  3325.         jmp     ricino
  3326. ;--------------------------------------
  3327. align 4
  3328. .found:
  3329.         pop     ebp edi eax
  3330.  
  3331.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3332. ;--------------------------------------
  3333. align 4
  3334. ricino:
  3335. not_this_task:
  3336.         pop     ecx
  3337.  
  3338.         cmp     ecx, [thread_count]
  3339.         jle     newdw2
  3340.  
  3341.         pop     eax
  3342.         popad
  3343.         ret
  3344. ;-----------------------------------------------------------------------------
  3345. align 4
  3346. calculatebackground:   ; background
  3347.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3348.         mov     eax, 0x01010101
  3349.         mov     ecx, [_display.win_map_size]
  3350.         shr     ecx, 2
  3351.         rep stosd
  3352.         mov     byte[window_data+32+WDATA.z_modif], ZPOS_DESKTOP
  3353.         mov     [REDRAW_BACKGROUND], 0
  3354.         ret
  3355. ;-----------------------------------------------------------------------------
  3356. uglobal
  3357.   imax    dd 0x0
  3358. endg
  3359. ;-----------------------------------------------------------------------------
  3360. align 4
  3361. delay_ms:     ; delay in 1/1000 sec
  3362.         pushad
  3363.  
  3364.         cmp     [hpet_base], 0
  3365.         jz      .no_hpet
  3366.         mov     eax, esi
  3367.         mov     edx, 1_000_000 ; ms to ns
  3368.         mul     edx
  3369.         mov     ebx, edx
  3370.         mov     ecx, eax
  3371.  
  3372.         push    ecx
  3373.         call    get_clock_ns
  3374.         pop     ecx
  3375.         mov     edi, edx
  3376.         mov     esi, eax
  3377. .wait:
  3378.         push    ecx
  3379.         call    get_clock_ns
  3380.         pop     ecx
  3381.         sub     eax, esi
  3382.         sbb     edx, edi
  3383.         sub     eax, ecx
  3384.         sbb     edx, ebx
  3385.         jc      .wait
  3386.         jmp     .done
  3387.  
  3388. .no_hpet:
  3389.         mov     ecx, esi
  3390.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3391.         imul    ecx, 33941
  3392.         shr     ecx, 9
  3393.         ; </CPU clock fix>
  3394.  
  3395.         in      al, 0x61
  3396.         and     al, 0x10
  3397.         mov     ah, al
  3398.         cld
  3399.  
  3400. .cnt1:
  3401.         in      al, 0x61
  3402.         and     al, 0x10
  3403.         cmp     al, ah
  3404.         jz      .cnt1
  3405.  
  3406.         mov     ah, al
  3407.         loop    .cnt1
  3408.  
  3409. .done:
  3410.         popad
  3411.         ret
  3412. ;-----------------------------------------------------------------------------
  3413. align 4
  3414. set_app_param:
  3415.         mov     edi, [current_slot]
  3416.         mov     edi, [TASK_BASE]
  3417.         mov     eax, ebx
  3418.         ;xchg    eax, [edi + APPDATA.event_mask] ; set new event mask
  3419.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  3420.         mov     [esp+32], eax                    ; return old mask value
  3421.         ret
  3422. ;-----------------------------------------------------------------------------
  3423.  
  3424. ; this is for syscall
  3425. proc delay_hs_unprotected
  3426.         call    unprotect_from_terminate
  3427.         call    delay_hs
  3428.         call    protect_from_terminate
  3429.         ret
  3430. endp
  3431.  
  3432. if 1
  3433. align 4
  3434. delay_hs:     ; delay in 1/100 secs
  3435. ; ebx = delay time
  3436.  
  3437.         pushad
  3438.         push    ebx
  3439.         xor     esi, esi
  3440.         mov     ecx, MANUAL_DESTROY
  3441.         call    create_event
  3442.         test    eax, eax
  3443.         jz      .done
  3444.  
  3445.         mov     ebx, edx
  3446.         mov     ecx, [esp]
  3447.         push    edx
  3448.         push    eax
  3449.         call    wait_event_timeout
  3450.         pop     eax
  3451.         pop     ebx
  3452.         call    destroy_event
  3453. .done:
  3454.         add     esp, 4
  3455.         popad
  3456.         ret
  3457.  
  3458. else
  3459.  
  3460. align 4
  3461. delay_hs:     ; delay in 1/100 secs
  3462. ; ebx = delay time
  3463.         push    ecx
  3464.         push    edx
  3465.  
  3466.         mov     edx, [timer_ticks]
  3467. ;--------------------------------------
  3468. align 4
  3469. newtic:
  3470.         mov     ecx, [timer_ticks]
  3471.         sub     ecx, edx
  3472.         cmp     ecx, ebx
  3473.         jae     zerodelay
  3474.  
  3475.         call    change_task
  3476.  
  3477.         jmp     newtic
  3478. ;--------------------------------------
  3479. align 4
  3480. zerodelay:
  3481.         pop     edx
  3482.         pop     ecx
  3483.         ret
  3484. end if
  3485.  
  3486. ;-----------------------------------------------------------------------------
  3487. align 16        ;very often call this subrutine
  3488. memmove:       ; memory move in bytes
  3489. ; eax = from
  3490. ; ebx = to
  3491. ; ecx = no of bytes
  3492.         test    ecx, ecx
  3493.         jle     .ret
  3494.  
  3495.         push    esi edi ecx
  3496.  
  3497.         mov     edi, ebx
  3498.         mov     esi, eax
  3499.  
  3500.         test    ecx, not 11b
  3501.         jz      @f
  3502.  
  3503.         push    ecx
  3504.         shr     ecx, 2
  3505.         rep movsd
  3506.         pop     ecx
  3507.         and     ecx, 11b
  3508.         jz      .finish
  3509. ;--------------------------------------
  3510. align 4
  3511. @@:
  3512.         rep movsb
  3513. ;--------------------------------------
  3514. align 4
  3515. .finish:
  3516.         pop     ecx edi esi
  3517. ;--------------------------------------
  3518. align 4
  3519. .ret:
  3520.         ret
  3521. ;-----------------------------------------------------------------------------
  3522.  
  3523. align 4
  3524. set_io_access_rights:
  3525.         push    edi eax
  3526.         mov     edi, tss._io_map_0
  3527. ;     mov   ecx,eax
  3528. ;     and   ecx,7    ; offset in byte
  3529. ;     shr   eax,3    ; number of byte
  3530. ;     add   edi,eax
  3531. ;     mov   ebx,1
  3532. ;     shl   ebx,cl
  3533.         test    ebp, ebp
  3534. ;     cmp   ebp,0                ; enable access - ebp = 0
  3535.         jnz     .siar1
  3536. ;     not   ebx
  3537. ;     and   [edi],byte bl
  3538.         btr     [edi], eax
  3539.         pop     eax edi
  3540.         ret
  3541. .siar1:
  3542.         bts     [edi], eax
  3543.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3544.         pop     eax edi
  3545.         ret
  3546.  
  3547. align 4
  3548. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  3549.  
  3550.         call    r_f_port_area
  3551.         mov     [esp+32], eax
  3552.         ret
  3553.  
  3554. ;reserve/free group of ports
  3555. ;  * eax = 46 - number function
  3556. ;  * ebx = 0 - reserve, 1 - free
  3557. ;  * ecx = number start arrea of ports
  3558. ;  * edx = number end arrea of ports (include last number of port)
  3559. ;Return value:
  3560. ;  * eax = 0 - succesful
  3561. ;  * eax = 1 - error
  3562. ;  * The system has reserve this ports:
  3563. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3564. ;destroys eax,ebx, ebp
  3565. r_f_port_area:
  3566.  
  3567.         test    ebx, ebx
  3568.         jnz     free_port_area
  3569. ;     je    r_port_area
  3570. ;     jmp   free_port_area
  3571.  
  3572. ;   r_port_area:
  3573.  
  3574. ;     pushad
  3575.  
  3576.         cmp     ecx, edx      ; beginning > end ?
  3577.         ja      rpal1
  3578.         cmp     edx, 65536
  3579.         jae     rpal1
  3580.         mov     eax, [RESERVED_PORTS]
  3581.         test    eax, eax      ; no reserved areas ?
  3582.         je      rpal2
  3583.         cmp     eax, 255      ; max reserved
  3584.         jae     rpal1
  3585.  rpal3:
  3586.         mov     ebx, eax
  3587.         shl     ebx, 4
  3588.         add     ebx, RESERVED_PORTS
  3589.         cmp     ecx, [ebx+8]
  3590.         ja      rpal4
  3591.         cmp     edx, [ebx+4]
  3592.         jae     rpal1
  3593. ;     jb    rpal4
  3594. ;     jmp   rpal1
  3595.  rpal4:
  3596.         dec     eax
  3597.         jnz     rpal3
  3598.         jmp     rpal2
  3599.    rpal1:
  3600. ;     popad
  3601. ;     mov   eax,1
  3602.         xor     eax, eax
  3603.         inc     eax
  3604.         ret
  3605.    rpal2:
  3606. ;     popad
  3607.      ; enable port access at port IO map
  3608.         cli
  3609.         pushad                        ; start enable io map
  3610.  
  3611.         cmp     edx, 65536;16384
  3612.         jae     no_unmask_io; jge
  3613.         mov     eax, ecx
  3614. ;       push    ebp
  3615.         xor     ebp, ebp               ; enable - eax = port
  3616. new_port_access:
  3617. ;     pushad
  3618.         call    set_io_access_rights
  3619. ;     popad
  3620.         inc     eax
  3621.         cmp     eax, edx
  3622.         jbe     new_port_access
  3623. ;       pop     ebp
  3624. no_unmask_io:
  3625.         popad                         ; end enable io map
  3626.         sti
  3627.  
  3628.         mov     eax, [RESERVED_PORTS]
  3629.         add     eax, 1
  3630.         mov     [RESERVED_PORTS], eax
  3631.         shl     eax, 4
  3632.         add     eax, RESERVED_PORTS
  3633.         ;mov     ebx, [current_slot]
  3634.         ;mov     ebx, [ebx+APPDATA.tid]
  3635.         mov     ebx, [TASK_BASE]
  3636.         mov     ebx, [ebx+TASKDATA.pid]
  3637.         mov     [eax], ebx
  3638.         mov     [eax+4], ecx
  3639.         mov     [eax+8], edx
  3640.  
  3641.         xor     eax, eax
  3642.         ret
  3643.  
  3644. free_port_area:
  3645.  
  3646. ;     pushad
  3647.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3648.         test    eax, eax
  3649.         jz      frpal2
  3650.         ;mov     ebx, [current_slot]
  3651.         ;mov     ebx, [ebx+APPDATA.tid]
  3652.         mov     ebx, [TASK_BASE]
  3653.         mov     ebx, [ebx+TASKDATA.pid]
  3654.    frpal3:
  3655.         mov     edi, eax
  3656.         shl     edi, 4
  3657.         add     edi, RESERVED_PORTS
  3658.         cmp     ebx, [edi]
  3659.         jne     frpal4
  3660.         cmp     ecx, [edi+4]
  3661.         jne     frpal4
  3662.         cmp     edx, [edi+8]
  3663.         jne     frpal4
  3664.         jmp     frpal1
  3665.    frpal4:
  3666.         dec     eax
  3667.         jnz     frpal3
  3668.    frpal2:
  3669. ;     popad
  3670.         inc     eax
  3671.         ret
  3672.    frpal1:
  3673.         push    ecx
  3674.         mov     ecx, 256
  3675.         sub     ecx, eax
  3676.         shl     ecx, 4
  3677.         mov     esi, edi
  3678.         add     esi, 16
  3679.         cld
  3680.         rep movsb
  3681.  
  3682.         dec     dword [RESERVED_PORTS]
  3683. ;popad
  3684. ;disable port access at port IO map
  3685.  
  3686. ;     pushad                        ; start disable io map
  3687.         pop     eax     ;start port
  3688.         cmp     edx, 65536;16384
  3689.         jge     no_mask_io
  3690.  
  3691. ;     mov   eax,ecx
  3692.         xor     ebp, ebp
  3693.         inc     ebp
  3694. new_port_access_disable:
  3695. ;     pushad
  3696. ;     mov   ebp,1                  ; disable - eax = port
  3697.         call    set_io_access_rights
  3698. ;     popad
  3699.         inc     eax
  3700.         cmp     eax, edx
  3701.         jbe     new_port_access_disable
  3702. no_mask_io:
  3703. ;     popad                         ; end disable io map
  3704.         xor     eax, eax
  3705.         ret
  3706. ;-----------------------------------------------------------------------------
  3707. align 4
  3708. drawbackground:
  3709. dbrv20:
  3710.         cmp     [BgrDrawMode], dword 1
  3711.         jne     bgrstr
  3712.         call    vesa20_drawbackground_tiled
  3713. ;        call    [draw_pointer]
  3714.         call    __sys_draw_pointer
  3715.         ret
  3716. ;--------------------------------------
  3717. align 4
  3718. bgrstr:
  3719.         call    vesa20_drawbackground_stretch
  3720. ;        call    [draw_pointer]
  3721.         call    __sys_draw_pointer
  3722.         ret
  3723. ;-----------------------------------------------------------------------------
  3724. align 4
  3725. syscall_putimage:                       ; PutImage
  3726. ; add check pointer
  3727.         push    ecx
  3728.         mov     ax, cx
  3729.         shr     ecx, 16
  3730.         imul    eax, ecx
  3731.         lea     eax, [eax*3]
  3732.         stdcall is_region_userspace, ebx, eax
  3733.         pop     ecx
  3734.         jnz     sys_putimage.exit
  3735.  
  3736. sys_putimage:
  3737.         test    ecx, 0x80008000
  3738.         jnz     .exit
  3739.         test    ecx, 0x0000FFFF
  3740.         jz      .exit
  3741.         test    ecx, 0xFFFF0000
  3742.         jnz     @f
  3743. ;--------------------------------------
  3744. align 4
  3745. .exit:
  3746.         ret
  3747. ;--------------------------------------
  3748. align 4
  3749. @@:
  3750.         mov     edi, [current_slot]
  3751.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  3752.         rol     edx, 16
  3753.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  3754.         rol     edx, 16
  3755. ;--------------------------------------
  3756. align 4
  3757. .forced:
  3758.         push    ebp esi 0
  3759.         mov     ebp, putimage_get24bpp
  3760.         mov     esi, putimage_init24bpp
  3761. ;--------------------------------------
  3762. align 4
  3763. sys_putimage_bpp:
  3764.         call    vesa20_putimage
  3765.         pop     ebp esi ebp
  3766.         ret
  3767. ;        jmp     [draw_pointer]
  3768. ;-----------------------------------------------------------------------------
  3769. align 4
  3770. sys_putimage_palette:
  3771. ; ebx = pointer to image
  3772. ; ecx = [xsize]*65536 + [ysize]
  3773. ; edx = [xstart]*65536 + [ystart]
  3774. ; esi = number of bits per pixel, must be 8, 24 or 32
  3775. ; edi = pointer to palette
  3776. ; ebp = row delta
  3777. ; check pointer
  3778.         push    ecx
  3779.         mov     ax, cx
  3780.         shr     ecx, 16
  3781.         imul    eax, ecx
  3782.         stdcall is_region_userspace, ebx, eax
  3783.         pop     ecx
  3784.         jnz     sys_putimage.exit
  3785.  
  3786.         mov     eax, [current_slot_idx]
  3787.         shl     eax, 8
  3788.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3789.         rol     edx, 16
  3790.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3791.         rol     edx, 16
  3792. ;--------------------------------------
  3793. align 4
  3794. .forced:
  3795.         cmp     esi, 1
  3796.         jnz     @f
  3797.         push    edi
  3798.         mov     eax, [edi+4]
  3799.         sub     eax, [edi]
  3800.         push    eax
  3801.         push    dword [edi]
  3802.         push    0ffffff80h
  3803.         mov     edi, esp
  3804.         call    put_mono_image
  3805.         add     esp, 12
  3806.         pop     edi
  3807.         ret
  3808. ;--------------------------------------
  3809. align 4
  3810. @@:
  3811.         cmp     esi, 2
  3812.         jnz     @f
  3813.         push    edi
  3814.         push    0ffffff80h
  3815.         mov     edi, esp
  3816.         call    put_2bit_image
  3817.         pop     eax
  3818.         pop     edi
  3819.         ret
  3820. ;--------------------------------------
  3821. align 4
  3822. @@:
  3823.         cmp     esi, 4
  3824.         jnz     @f
  3825.         push    edi
  3826.         push    0ffffff80h
  3827.         mov     edi, esp
  3828.         call    put_4bit_image
  3829.         pop     eax
  3830.         pop     edi
  3831.         ret
  3832. ;--------------------------------------
  3833. align 4
  3834. @@:
  3835.         push    ebp esi ebp
  3836.         cmp     esi, 8
  3837.         jnz     @f
  3838.         mov     ebp, putimage_get8bpp
  3839.         mov     esi, putimage_init8bpp
  3840.         jmp     sys_putimage_bpp
  3841. ;--------------------------------------
  3842. align 4
  3843. @@:
  3844.         cmp     esi, 9
  3845.         jnz     @f
  3846.         mov     ebp, putimage_get9bpp
  3847.         mov     esi, putimage_init9bpp
  3848.         jmp     sys_putimage_bpp
  3849. ;--------------------------------------
  3850. align 4
  3851. @@:
  3852.         cmp     esi, 15
  3853.         jnz     @f
  3854.         mov     ebp, putimage_get15bpp
  3855.         mov     esi, putimage_init15bpp
  3856.         jmp     sys_putimage_bpp
  3857. ;--------------------------------------
  3858. align 4
  3859. @@:
  3860.         cmp     esi, 16
  3861.         jnz     @f
  3862.         mov     ebp, putimage_get16bpp
  3863.         mov     esi, putimage_init16bpp
  3864.         jmp     sys_putimage_bpp
  3865. ;--------------------------------------
  3866. align 4
  3867. @@:
  3868.         cmp     esi, 24
  3869.         jnz     @f
  3870.         mov     ebp, putimage_get24bpp
  3871.         mov     esi, putimage_init24bpp
  3872.         jmp     sys_putimage_bpp
  3873. ;--------------------------------------
  3874. align 4
  3875. @@:
  3876.         cmp     esi, 32
  3877.         jnz     @f
  3878.         mov     ebp, putimage_get32bpp
  3879.         mov     esi, putimage_init32bpp
  3880.         jmp     sys_putimage_bpp
  3881. ;--------------------------------------
  3882. align 4
  3883. @@:
  3884.         pop     ebp esi ebp
  3885.         ret
  3886. ;-----------------------------------------------------------------------------
  3887. align 4
  3888. put_mono_image:
  3889.         push    ebp esi ebp
  3890.         mov     ebp, putimage_get1bpp
  3891.         mov     esi, putimage_init1bpp
  3892.         jmp     sys_putimage_bpp
  3893. ;-----------------------------------------------------------------------------
  3894. align 4
  3895. put_2bit_image:
  3896.         push    ebp esi ebp
  3897.         mov     ebp, putimage_get2bpp
  3898.         mov     esi, putimage_init2bpp
  3899.         jmp     sys_putimage_bpp
  3900. ;-----------------------------------------------------------------------------
  3901. align 4
  3902. put_4bit_image:
  3903.         push    ebp esi ebp
  3904.         mov     ebp, putimage_get4bpp
  3905.         mov     esi, putimage_init4bpp
  3906.         jmp     sys_putimage_bpp
  3907. ;-----------------------------------------------------------------------------
  3908. align 4
  3909. putimage_init24bpp:
  3910.         lea     eax, [eax*3]
  3911. putimage_init8bpp:
  3912. putimage_init9bpp:
  3913.         ret
  3914. ;-----------------------------------------------------------------------------
  3915. align 16
  3916. putimage_get24bpp:
  3917.         movzx   eax, byte [esi+2]
  3918.         shl     eax, 16
  3919.         mov     ax, [esi]
  3920.         add     esi, 3
  3921.         ret     4
  3922. ;-----------------------------------------------------------------------------
  3923. align 16
  3924. putimage_get8bpp:
  3925.         movzx   eax, byte [esi]
  3926.         push    edx
  3927.         mov     edx, [esp+8]
  3928.         mov     eax, [edx+eax*4]
  3929.         pop     edx
  3930.         inc     esi
  3931.         ret     4
  3932. ;-----------------------------------------------------------------------------
  3933. align 16
  3934. putimage_get9bpp:
  3935.         lodsb
  3936.         mov     ah, al
  3937.         shl     eax, 8
  3938.         mov     al, ah
  3939.         ret     4
  3940. ;-----------------------------------------------------------------------------
  3941. align 4
  3942. putimage_init1bpp:
  3943.         add     eax, ecx
  3944.         push    ecx
  3945.         add     eax, 7
  3946.         add     ecx, 7
  3947.         shr     eax, 3
  3948.         shr     ecx, 3
  3949.         sub     eax, ecx
  3950.         pop     ecx
  3951.         ret
  3952. ;-----------------------------------------------------------------------------
  3953. align 16
  3954. putimage_get1bpp:
  3955.         push    edx
  3956.         mov     edx, [esp+8]
  3957.         mov     al, [edx]
  3958.         add     al, al
  3959.         jnz     @f
  3960.         lodsb
  3961.         adc     al, al
  3962. @@:
  3963.         mov     [edx], al
  3964.         sbb     eax, eax
  3965.         and     eax, [edx+8]
  3966.         add     eax, [edx+4]
  3967.         pop     edx
  3968.         ret     4
  3969. ;-----------------------------------------------------------------------------
  3970. align 4
  3971. putimage_init2bpp:
  3972.         add     eax, ecx
  3973.         push    ecx
  3974.         add     ecx, 3
  3975.         add     eax, 3
  3976.         shr     ecx, 2
  3977.         shr     eax, 2
  3978.         sub     eax, ecx
  3979.         pop     ecx
  3980.         ret
  3981. ;-----------------------------------------------------------------------------
  3982. align 16
  3983. putimage_get2bpp:
  3984.         push    edx
  3985.         mov     edx, [esp+8]
  3986.         mov     al, [edx]
  3987.         mov     ah, al
  3988.         shr     al, 6
  3989.         shl     ah, 2
  3990.         jnz     .nonewbyte
  3991.         lodsb
  3992.         mov     ah, al
  3993.         shr     al, 6
  3994.         shl     ah, 2
  3995.         add     ah, 1
  3996. .nonewbyte:
  3997.         mov     [edx], ah
  3998.         mov     edx, [edx+4]
  3999.         movzx   eax, al
  4000.         mov     eax, [edx+eax*4]
  4001.         pop     edx
  4002.         ret     4
  4003. ;-----------------------------------------------------------------------------
  4004. align 4
  4005. putimage_init4bpp:
  4006.         add     eax, ecx
  4007.         push    ecx
  4008.         add     ecx, 1
  4009.         inc     eax      ;add   eax, 1
  4010.         shr     ecx, 1
  4011.         shr     eax, 1
  4012.         sub     eax, ecx
  4013.         pop     ecx
  4014.         ret
  4015. ;-----------------------------------------------------------------------------
  4016. align 16
  4017. putimage_get4bpp:
  4018.         push    edx
  4019.         mov     edx, [esp+8]
  4020.         add     byte [edx], 80h
  4021.         jc      @f
  4022.         movzx   eax, byte [edx+1]
  4023.         mov     edx, [edx+4]
  4024.         and     eax, 0x0F
  4025.         mov     eax, [edx+eax*4]
  4026.         pop     edx
  4027.         ret     4
  4028. @@:
  4029.         movzx   eax, byte [esi]
  4030.         add     esi, 1
  4031.         mov     [edx+1], al
  4032.         shr     eax, 4
  4033.         mov     edx, [edx+4]
  4034.         mov     eax, [edx+eax*4]
  4035.         pop     edx
  4036.         ret     4
  4037. ;-----------------------------------------------------------------------------
  4038. align 4
  4039. putimage_init32bpp:
  4040.         shl     eax, 2
  4041.         ret
  4042. ;-----------------------------------------------------------------------------
  4043. align 16
  4044. putimage_get32bpp:
  4045.         lodsd
  4046.         ret     4
  4047. ;-----------------------------------------------------------------------------
  4048. align 4
  4049. putimage_init15bpp:
  4050. putimage_init16bpp:
  4051.         add     eax, eax
  4052.         ret
  4053. ;-----------------------------------------------------------------------------
  4054. align 16
  4055. putimage_get15bpp:
  4056. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4057.         push    ecx edx
  4058.         movzx   eax, word [esi]
  4059.         add     esi, 2
  4060.         mov     ecx, eax
  4061.         mov     edx, eax
  4062.         and     eax, 0x1F
  4063.         and     ecx, 0x1F shl 5
  4064.         and     edx, 0x1F shl 10
  4065.         shl     eax, 3
  4066.         shl     ecx, 6
  4067.         shl     edx, 9
  4068.         or      eax, ecx
  4069.         or      eax, edx
  4070.         pop     edx ecx
  4071.         ret     4
  4072. ;-----------------------------------------------------------------------------
  4073. align 16
  4074. putimage_get16bpp:
  4075. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4076.         push    ecx edx
  4077.         movzx   eax, word [esi]
  4078.         add     esi, 2
  4079.         mov     ecx, eax
  4080.         mov     edx, eax
  4081.         and     eax, 0x1F
  4082.         and     ecx, 0x3F shl 5
  4083.         and     edx, 0x1F shl 11
  4084.         shl     eax, 3
  4085.         shl     ecx, 5
  4086.         shl     edx, 8
  4087.         or      eax, ecx
  4088.         or      eax, edx
  4089.         pop     edx ecx
  4090.         ret     4
  4091. ;-----------------------------------------------------------------------------
  4092. ;align 4
  4093. ; eax x beginning
  4094. ; ebx y beginning
  4095. ; ecx x end
  4096.         ; edx y end
  4097. ; edi color
  4098. ;__sys_drawbar:
  4099. ;        mov     esi, [current_slot]
  4100. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4101. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4102. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4103. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4104. ;--------------------------------------
  4105. ;align 4
  4106. ;.forced:
  4107. ;        call    vesa20_drawbar
  4108. ;        call    [draw_pointer]
  4109. ;        ret
  4110. ;-----------------------------------------------------------------------------
  4111. align 4
  4112. kb_write_wait_ack:
  4113.  
  4114.         push    ecx edx
  4115.  
  4116.         mov     dl, al
  4117.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4118. .wait_output_ready:
  4119.         in      al, 0x64
  4120.         test    al, 2
  4121.         jz      @f
  4122.         loop    .wait_output_ready
  4123.         mov     ah, 1
  4124.         jmp     .nothing
  4125. @@:
  4126.         mov     al, dl
  4127.         out     0x60, al
  4128.         mov     ecx, 0xfffff; last 0xffff, new value in view of fast CPU's
  4129. .wait_ack:
  4130.         in      al, 0x64
  4131.         test    al, 1
  4132.         jnz     @f
  4133.         loop    .wait_ack
  4134.         mov     ah, 1
  4135.         jmp     .nothing
  4136. @@:
  4137.         in      al, 0x60
  4138.         xor     ah, ah
  4139.  
  4140. .nothing:
  4141.         pop     edx ecx
  4142.  
  4143.         ret
  4144. ;-----------------------------------------------------------------------------
  4145.  
  4146. if used _rdtsc
  4147. _rdtsc:
  4148.         bt      [cpu_caps], CAPS_TSC
  4149.         jnc     ret_rdtsc
  4150.         rdtsc
  4151.         ret
  4152.    ret_rdtsc:
  4153.         mov     edx, 0xffffffff
  4154.         mov     eax, 0xffffffff
  4155.         ret
  4156. end if
  4157.  
  4158. sys_msg_board_str:
  4159.  
  4160.         pushad
  4161.    @@:
  4162.         cmp     [esi], byte 0
  4163.         je      @f
  4164.         mov     ebx, 1
  4165.         movzx   ecx, byte [esi]
  4166.         call    sys_msg_board
  4167.         inc     esi
  4168.         jmp     @b
  4169.    @@:
  4170.         popad
  4171.         ret
  4172.  
  4173. sys_msg_board_byte:
  4174. ; in: al = byte to display
  4175. ; out: nothing
  4176. ; destroys: nothing
  4177.         pushad
  4178.         mov     ecx, 2
  4179.         shl     eax, 24
  4180.         jmp     @f
  4181.  
  4182. sys_msg_board_word:
  4183. ; in: ax = word to display
  4184. ; out: nothing
  4185. ; destroys: nothing
  4186.         pushad
  4187.         mov     ecx, 4
  4188.         shl     eax, 16
  4189.         jmp     @f
  4190.  
  4191. sys_msg_board_dword:
  4192. ; in: eax = dword to display
  4193. ; out: nothing
  4194. ; destroys: nothing
  4195.         pushad
  4196.         mov     ecx, 8
  4197. @@:
  4198.         push    ecx
  4199.         rol     eax, 4
  4200.         push    eax
  4201.         and     al, 0xF
  4202.         cmp     al, 10
  4203.         sbb     al, 69h
  4204.         das
  4205.         mov     cl, al
  4206.         xor     ebx, ebx
  4207.         inc     ebx
  4208.         call    sys_msg_board
  4209.         pop     eax
  4210.         pop     ecx
  4211.         loop    @b
  4212.         popad
  4213.         ret
  4214.  
  4215. msg_board_data_size = 65536 ; Must be power of two
  4216.  
  4217. uglobal
  4218. msg_board_data  rb  msg_board_data_size
  4219. msg_board_count dd  ?
  4220. endg
  4221.  
  4222. iglobal
  4223. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4224. endg
  4225.  
  4226. sys_msg_board:
  4227. ; ebx=1 -> write, cl = byte to write
  4228. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4229.         push    eax ebx
  4230.         mov     eax, ebx
  4231.         mov     ebx, ecx
  4232.         mov     ecx, [msg_board_count]
  4233.         cmp     eax, 1
  4234.         jne     .read
  4235.  
  4236. if defined debug_com_base
  4237.         push    dx ax
  4238. @@: ; wait for empty transmit register
  4239.         mov     dx, debug_com_base+5
  4240.         in      al, dx
  4241.         test    al, 1 shl 5
  4242.         jz      @r
  4243.         mov     dx, debug_com_base      ; Output the byte
  4244.         mov     al, bl
  4245.         out     dx, al
  4246.         pop     ax dx
  4247. end if
  4248.  
  4249.         mov     [msg_board_data+ecx], bl
  4250.         cmp     byte [debug_direct_print], 1
  4251.         jnz     .end
  4252.         pusha
  4253.         lea     edx, [msg_board_data+ecx]
  4254.         mov     ecx, 0x40FFFFFF
  4255.         mov     ebx, [msg_board_pos]
  4256.         mov     edi, 1
  4257.         mov     esi, edi ;1
  4258.         call    dtext
  4259.         popa
  4260.         add     word [msg_board_pos+2], 6
  4261.         cmp     word [msg_board_pos+2], 105*6
  4262.         jnc     @f
  4263.         cmp     bl, 10
  4264.         jnz     .end
  4265. @@:
  4266.         mov     word [msg_board_pos+2], 42*6
  4267.         add     word [msg_board_pos], 10
  4268.         mov     eax, [_display.height]
  4269.         sub     eax, 10
  4270.         cmp     ax, word [msg_board_pos]
  4271.         jnc     @f
  4272.         mov     word [msg_board_pos], 10
  4273. @@:
  4274.         pusha
  4275.         mov     eax, [msg_board_pos]
  4276.         movzx   ebx, ax
  4277.         shr     eax, 16
  4278.         mov     edx, 105*6
  4279.         xor     ecx, ecx
  4280.         mov     edi, 1
  4281.         mov     esi, 9
  4282. @@:
  4283.         call    hline
  4284.         inc     ebx
  4285.         dec     esi
  4286.         jnz     @b
  4287.         popa
  4288. .end:
  4289.         inc     ecx
  4290.         and     ecx, msg_board_data_size - 1
  4291.         mov     [msg_board_count], ecx
  4292. .ret:
  4293.         pop     ebx eax
  4294.         ret
  4295.  
  4296. @@:
  4297.         mov     [esp+32], ecx
  4298.         mov     [esp+20], ecx
  4299.         jmp     .ret
  4300.  
  4301. .read:
  4302.         cmp     eax, 2
  4303.         jne     .ret
  4304.         test    ecx, ecx
  4305.         jz      @b
  4306.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4307.         mov     eax, msg_board_data+1
  4308.         mov     ebx, msg_board_data
  4309.         movzx   edx, byte [ebx]
  4310.         call    memmove
  4311.         dec     [msg_board_count]
  4312.         mov     [esp + 32], edx ;eax
  4313.         mov     [esp + 20], dword 1
  4314.         ret
  4315.  
  4316. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4317. ;; 66 sys function.                                                ;;
  4318. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4319. ;; out eax                                                         ;;
  4320. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4321. iglobal
  4322. align 4
  4323. f66call:
  4324.            dd sys_process_def.1   ; 1 = set keyboard mode
  4325.            dd sys_process_def.2   ; 2 = get keyboard mode
  4326.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4327.            dd sys_process_def.4   ; 4 = set system-wide hotkey
  4328.            dd sys_process_def.5   ; 5 = delete installed hotkey
  4329.            dd sys_process_def.6   ; 6 = disable input, work only hotkeys
  4330.            dd sys_process_def.7   ; 7 = enable input, opposition to f.66.6
  4331. endg
  4332. ;-----------------------------------------------------------------------------
  4333. align 4
  4334. sys_process_def:
  4335.         dec     ebx
  4336.         cmp     ebx, 7
  4337.         jae     .not_support    ;if >=8 then or eax,-1
  4338.  
  4339.         mov     edi, [current_slot_idx]
  4340.         jmp     dword [f66call+ebx*4]
  4341.  
  4342. .not_support:
  4343.         or      eax, -1
  4344.         ret
  4345. ;-----------------------------------------------------------------------------
  4346. align 4
  4347. .1:
  4348.         shl     edi, 8
  4349.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4350.  
  4351.         ret
  4352. ;-----------------------------------------------------------------------------
  4353. align 4
  4354. .2:                             ; 2 = get keyboard mode
  4355.         shl     edi, 8
  4356.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4357.         mov     [esp+32], eax
  4358.         ret
  4359. ;-----------------------------------------------------------------------------
  4360. align 4
  4361. .3:                             ;3 = get keyboard ctrl, alt, shift
  4362.         mov     eax, [kb_state]
  4363.         mov     [esp+32], eax
  4364.         ret
  4365. ;-----------------------------------------------------------------------------
  4366. align 4
  4367. .4:
  4368.         mov     eax, hotkey_list
  4369. @@:
  4370.         cmp     dword [eax+8], 0
  4371.         jz      .found_free
  4372.         add     eax, 16
  4373.         cmp     eax, hotkey_list+16*256
  4374.         jb      @b
  4375.         mov     dword [esp+32], 1
  4376.         ret
  4377. .found_free:
  4378.         mov     [eax+8], edi
  4379.         mov     [eax+4], edx
  4380.         movzx   ecx, cl
  4381.         lea     ecx, [hotkey_scancodes+ecx*4]
  4382.         mov     edx, [ecx]
  4383.         mov     [eax], edx
  4384.         mov     [ecx], eax
  4385.         mov     [eax+12], ecx
  4386.         test    edx, edx
  4387.         jz      @f
  4388.         mov     [edx+12], eax
  4389. @@:
  4390.         and     dword [esp+32], 0
  4391.         ret
  4392. ;-----------------------------------------------------------------------------
  4393. align 4
  4394. .5:
  4395.         movzx   ebx, cl
  4396.         lea     ebx, [hotkey_scancodes+ebx*4]
  4397.         mov     eax, [ebx]
  4398. .scan:
  4399.         test    eax, eax
  4400.         jz      .notfound
  4401.         cmp     [eax+8], edi
  4402.         jnz     .next
  4403.         cmp     [eax+4], edx
  4404.         jz      .found
  4405. .next:
  4406.         mov     eax, [eax]
  4407.         jmp     .scan
  4408. .notfound:
  4409.         mov     dword [esp+32], 1
  4410.         ret
  4411. .found:
  4412.         mov     ecx, [eax]
  4413.         jecxz   @f
  4414.         mov     edx, [eax+12]
  4415.         mov     [ecx+12], edx
  4416. @@:
  4417.         mov     ecx, [eax+12]
  4418.         mov     edx, [eax]
  4419.         mov     [ecx], edx
  4420.         xor     edx, edx
  4421.         mov     [eax+4], edx
  4422.         mov     [eax+8], edx
  4423.         mov     [eax+12], edx
  4424.         mov     [eax], edx
  4425.         mov     [esp+32], edx
  4426.         ret
  4427. ;-----------------------------------------------------------------------------
  4428. align 4
  4429. .6:
  4430.         pushfd
  4431.         cli
  4432.         mov     eax, [PID_lock_input]
  4433.         test    eax, eax
  4434.         jnz     @f
  4435. ; get current PID
  4436.         mov     eax, [current_slot_idx]
  4437.         shl     eax, 5
  4438.         mov     eax, [eax+TASK_TABLE+TASKDATA.pid]
  4439.         ;shl     eax, 8
  4440.         ;mov     eax, [eax+SLOT_BASE+APPDATA.tid]
  4441. ; set current PID for lock input
  4442.         mov     [PID_lock_input], eax
  4443. @@:
  4444.         popfd
  4445.         ret
  4446. ;-----------------------------------------------------------------------------
  4447. align 4
  4448. .7:
  4449.         mov     eax, [PID_lock_input]
  4450.         test    eax, eax
  4451.         jz      @f
  4452. ; get current PID
  4453.         mov     ebx, [current_slot_idx]
  4454.         shl     ebx, 5
  4455.         mov     ebx, [ebx+TASK_TABLE+TASKDATA.pid]
  4456.         ;shl     ebx, 8
  4457.         ;mov     ebx, [ebx+SLOT_BASE+APPDATA.tid]
  4458. ; compare current lock input with current PID
  4459.         cmp     ebx, eax
  4460.         jne     @f
  4461.  
  4462.         xor     eax, eax
  4463.         mov     [PID_lock_input], eax
  4464. @@:
  4465.         ret
  4466. ;-----------------------------------------------------------------------------
  4467. uglobal
  4468.   PID_lock_input dd 0x0
  4469. endg
  4470. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4471. ;; 61 sys function.                                                ;;
  4472. ;; in eax=61,ebx in [1..3]                                         ;;
  4473. ;; out eax                                                         ;;
  4474. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4475. iglobal
  4476. align 4
  4477. f61call:
  4478.            dd sys_gs.1   ; resolution
  4479.            dd sys_gs.2   ; bits per pixel
  4480.            dd sys_gs.3   ; bytes per scanline
  4481. endg
  4482.  
  4483.  
  4484. align 4
  4485.  
  4486. sys_gs:                         ; direct screen access
  4487.         dec     ebx
  4488.         cmp     ebx, 2
  4489.         ja      .not_support
  4490.         jmp     dword [f61call+ebx*4]
  4491. .not_support:
  4492.         or      [esp+32], dword -1
  4493.         ret
  4494.  
  4495.  
  4496. .1:                             ; resolution
  4497.         mov     eax, [_display.width]
  4498.         shl     eax, 16
  4499.         mov     ax, word [_display.height]
  4500.         mov     [esp+32], eax
  4501.         ret
  4502. .2:                             ; bits per pixel
  4503.         mov     eax, [_display.bits_per_pixel]
  4504.         mov     [esp+32], eax
  4505.         ret
  4506. .3:                             ; bytes per scanline
  4507.         mov     eax, [_display.lfb_pitch]
  4508.         mov     [esp+32], eax
  4509.         ret
  4510.  
  4511. align 4
  4512. syscall_getscreensize:                  ; GetScreenSize
  4513.         mov     ax, word [_display.width]
  4514.         dec     ax
  4515.         shl     eax, 16
  4516.         mov     ax, word [_display.height]
  4517.         dec     ax
  4518.         mov     [esp + 32], eax
  4519.         ret
  4520. ;-----------------------------------------------------------------------------
  4521. align 4
  4522. syscall_cdaudio:
  4523. ; ECX - position of CD/DVD-drive
  4524. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  4525. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  4526. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  4527.         cmp     ecx, 11
  4528.         ja      .exit
  4529.  
  4530.         mov     eax, ecx
  4531.         shr     eax, 2
  4532.         lea     eax, [eax*5]
  4533.         mov     al, [eax+DRIVE_DATA+1]
  4534.  
  4535.         push    ecx ebx
  4536.         mov     ebx, ecx
  4537.         and     ebx, 11b
  4538.         shl     ebx, 1
  4539.         mov     cl, 6
  4540.         sub     cl, bl
  4541.         shr     al, cl
  4542.         test    al, 2 ; it's not an ATAPI device
  4543.         pop     ebx ecx
  4544.  
  4545.         jz      .exit
  4546.  
  4547.         cmp     ebx, 4
  4548.         je      .eject
  4549.  
  4550.         cmp     ebx, 5
  4551.         je      .load
  4552. ;--------------------------------------
  4553. .exit:
  4554.         ret
  4555. ;--------------------------------------
  4556. .load:
  4557.         call    .reserve
  4558.         call    LoadMedium
  4559.         jmp     .free
  4560. ;--------------------------------------
  4561. .eject:
  4562.         call    .reserve
  4563.         call    clear_CD_cache
  4564.         call    allow_medium_removal
  4565.         call    EjectMedium
  4566.         jmp     .free
  4567. ;--------------------------------------
  4568. .reserve:
  4569.         call    reserve_cd
  4570.  
  4571.         mov     ebx, ecx
  4572.         inc     ebx
  4573.         mov     [cdpos], ebx
  4574.  
  4575.         mov     eax, ecx
  4576.         shr     eax, 1
  4577.         and     eax, 1
  4578.         inc     eax
  4579.         mov     [ChannelNumber], al
  4580.         mov     eax, ecx
  4581.         and     eax, 1
  4582.         mov     [DiskNumber], al
  4583.         call    reserve_cd_channel
  4584.         ret
  4585. ;--------------------------------------
  4586. .free:
  4587.         call    free_cd_channel
  4588.         and     [cd_status], 0
  4589.         ret
  4590. ;-----------------------------------------------------------------------------
  4591. align 4
  4592. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  4593.         cmp     ebx, [_display.width]
  4594.         jb      @f
  4595.         cmp     ecx, [_display.height]
  4596.         jb      @f
  4597.         xor     eax, eax
  4598.         jmp     .store
  4599. ;--------------------------------------
  4600. align 4
  4601. @@:
  4602.         mov     eax, [d_width_calc_area + ecx*4]
  4603.         add     eax, [_display.win_map]
  4604.         movzx   eax, byte[eax+ebx]        ; get value for current point
  4605. ;--------------------------------------
  4606. align 4
  4607. .store:
  4608.         mov     [esp + 32], eax
  4609.         ret
  4610. ;-----------------------------------------------------------------------------
  4611. align 4
  4612. syscall_getpixel:                       ; GetPixel
  4613.         mov     ecx, [_display.width]
  4614.         xor     edx, edx
  4615.         mov     eax, ebx
  4616.         div     ecx
  4617.         mov     ebx, edx
  4618.         xchg    eax, ebx
  4619.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4620.         call    dword [GETPIXEL]; eax - x, ebx - y
  4621.         mov     [esp + 32], ecx
  4622.         ret
  4623. ;-----------------------------------------------------------------------------
  4624. align 4
  4625. syscall_getarea:
  4626. ;eax = 36
  4627. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4628. ;ecx = [size x]*65536 + [size y]
  4629. ;edx = [start x]*65536 + [start y]
  4630.         pushad
  4631.         mov     edi, ebx
  4632.         mov     eax, edx
  4633.         shr     eax, 16
  4634.         mov     ebx, edx
  4635.         and     ebx, 0xffff
  4636.         dec     eax
  4637.         dec     ebx
  4638.      ; eax - x, ebx - y
  4639.         mov     edx, ecx
  4640.  
  4641.         shr     ecx, 16
  4642.         and     edx, 0xffff
  4643.         mov     esi, ecx
  4644.      ; ecx - size x, edx - size y
  4645.  
  4646.         mov     ebp, edx
  4647.         lea     ebp, [ebp*3]
  4648.         imul    ebp, esi
  4649.         stdcall is_region_userspace, edi, ebp
  4650.         jnz     .exit
  4651.  
  4652.         mov     ebp, edx
  4653.         dec     ebp
  4654.         lea     ebp, [ebp*3]
  4655.  
  4656.         imul    ebp, esi
  4657.  
  4658.         mov     esi, ecx
  4659.         dec     esi
  4660.         lea     esi, [esi*3]
  4661.  
  4662.         add     ebp, esi
  4663.         add     ebp, edi
  4664.  
  4665.         add     ebx, edx
  4666. ;--------------------------------------
  4667. align 4
  4668. .start_y:
  4669.         push    ecx edx
  4670. ;--------------------------------------
  4671. align 4
  4672. .start_x:
  4673.         push    eax ebx ecx
  4674.         add     eax, ecx
  4675.  
  4676.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4677.         call    dword [GETPIXEL]; eax - x, ebx - y
  4678.  
  4679.         mov     [ebp], cx
  4680.         shr     ecx, 16
  4681.         mov     [ebp+2], cl
  4682.  
  4683.         pop     ecx ebx eax
  4684.         sub     ebp, 3
  4685.         dec     ecx
  4686.         jnz     .start_x
  4687.         pop     edx ecx
  4688.         dec     ebx
  4689.         dec     edx
  4690.         jnz     .start_y
  4691.        
  4692. .exit:
  4693.         popad
  4694.         ret
  4695. ;-----------------------------------------------------------------------------
  4696.  
  4697. align 4
  4698. syscall_threads:                        ; CreateThreads
  4699. ;
  4700. ;   ecx=thread entry point
  4701. ;   edx=thread stack pointer
  4702. ;
  4703. ; on return : eax = pid
  4704.  
  4705.         xor     ebx, ebx
  4706.         call    new_sys_threads
  4707.  
  4708.         mov     [esp+32], eax
  4709.         ret
  4710.  
  4711. ;------------------------------------------------------------------------------
  4712. align 4
  4713. calculate_fast_getting_offset_for_WinMapAddress:
  4714. ; calculate data area for fast getting offset to _WinMapAddress
  4715.         xor     eax, eax
  4716.         mov     ecx, [_display.height]
  4717.         mov     edi, d_width_calc_area
  4718.         cld
  4719. @@:
  4720.         stosd
  4721.         add     eax, [_display.width]
  4722.         dec     ecx
  4723.         jnz     @r
  4724.         ret
  4725. ;------------------------------------------------------------------------------
  4726. align 4
  4727. calculate_fast_getting_offset_for_LFB:
  4728. ; calculate data area for fast getting offset to LFB
  4729.         xor     eax, eax
  4730.         mov     ecx, [_display.height]
  4731.         mov     edi, BPSLine_calc_area
  4732.         cld
  4733. @@:
  4734.         stosd
  4735.         add     eax, [_display.lfb_pitch]
  4736.         dec     ecx
  4737.         jnz     @r
  4738.         ret
  4739. ;------------------------------------------------------------------------------
  4740. align 4
  4741. set_screen:
  4742. ; in:
  4743. ; eax - new Screen_Max_X
  4744. ; ecx - new BytesPerScanLine
  4745. ; edx - new Screen_Max_Y
  4746.  
  4747.         pushfd
  4748.         cli
  4749.  
  4750.         mov     [_display.lfb_pitch], ecx
  4751.  
  4752.         mov     [screen_workarea.right], eax
  4753.         mov     [screen_workarea.bottom], edx
  4754.  
  4755.         push    ebx
  4756.         push    esi
  4757.         push    edi
  4758.  
  4759.         pushad
  4760.  
  4761.         cmp     [do_not_touch_winmap], 1
  4762.         je      @f
  4763.  
  4764.         stdcall kernel_free, [_display.win_map]
  4765.  
  4766.         mov     eax, [_display.width]
  4767.         mul     [_display.height]
  4768.         mov     [_display.win_map_size], eax
  4769.  
  4770.         stdcall kernel_alloc, eax
  4771.         mov     [_display.win_map], eax
  4772.         test    eax, eax
  4773.         jz      .epic_fail
  4774. ; store for f.18.24
  4775.         mov     eax, [_display.width]
  4776.         mov     [display_width_standard], eax
  4777.  
  4778.         mov     eax, [_display.height]
  4779.         mov     [display_height_standard], eax
  4780. @@:
  4781.         call    calculate_fast_getting_offset_for_WinMapAddress
  4782. ; for Qemu or non standart video cards
  4783. ; Unfortunately [BytesPerScanLine] does not always
  4784. ;                             equal to [_display.width] * [ScreenBPP] / 8
  4785.         call    calculate_fast_getting_offset_for_LFB
  4786.         popad
  4787.  
  4788.         call    repos_windows
  4789.         xor     eax, eax
  4790.         xor     ebx, ebx
  4791.         mov     ecx, [_display.width]
  4792.         mov     edx, [_display.height]
  4793.         dec     ecx
  4794.         dec     edx
  4795.         call    calculatescreen
  4796.         pop     edi
  4797.         pop     esi
  4798.         pop     ebx
  4799.  
  4800.         popfd
  4801.         ret
  4802.  
  4803. .epic_fail:
  4804.         hlt                     ; Houston, we've had a problem
  4805.  
  4806. ; --------------- APM ---------------------
  4807. uglobal
  4808. apm_entry       dp      0
  4809. apm_vf          dd      0
  4810. endg
  4811.  
  4812. align 4
  4813. sys_apm:
  4814.         xor     eax, eax
  4815.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4816.         jne     @f
  4817.         inc     eax
  4818.         or      dword [esp + 44], eax   ; error
  4819.         add     eax, 7
  4820.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  4821.         ret
  4822.  
  4823. @@:
  4824. ;       xchg    eax, ecx
  4825. ;       xchg    ebx, ecx
  4826.  
  4827.         cmp     dx, 3
  4828.         ja      @f
  4829.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4830.         mov     eax, [apm_vf]
  4831.         mov     [esp + 32], eax
  4832.         shr     eax, 16
  4833.         mov     [esp + 28], eax
  4834.         ret
  4835.  
  4836. @@:
  4837.  
  4838.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4839.         xchg    [master_tab], esi
  4840.         push    esi
  4841.         mov     edi, cr3
  4842.         mov     cr3, edi                ;flush TLB
  4843.  
  4844.         call    pword [apm_entry]       ;call APM BIOS
  4845.  
  4846.         xchg    eax, [esp]
  4847.         mov     [master_tab], eax
  4848.         mov     eax, cr3
  4849.         mov     cr3, eax
  4850.         pop     eax
  4851.  
  4852.         mov     [esp + 4 ], edi
  4853.         mov     [esp + 8], esi
  4854.         mov     [esp + 20], ebx
  4855.         mov     [esp + 24], edx
  4856.         mov     [esp + 28], ecx
  4857.         mov     [esp + 32], eax
  4858.         setc    al
  4859.         and     [esp + 44], byte 0xfe
  4860.         or      [esp + 44], al
  4861.         ret
  4862. ; -----------------------------------------
  4863.  
  4864. align 4
  4865. undefined_syscall:                      ; Undefined system call
  4866.         mov     [esp + 32], dword -1
  4867.         ret
  4868.  
  4869. align 4
  4870. ; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
  4871. ; @param base Base address of region
  4872. ; @param len Lenght of region
  4873. ; @return ZF = 1 if region in userspace memory,
  4874. ;         ZF = 0 otherwise
  4875. proc is_region_userspace stdcall, base:dword, len:dword
  4876.         push    eax
  4877.         mov     eax, [base]
  4878.  
  4879.         cmp     eax, OS_BASE-1
  4880.         ja      @f              ; zf
  4881.  
  4882.         add     eax, [len]
  4883.         jc      @f              ; zf
  4884.         cmp     eax, OS_BASE
  4885.         ja      @f              ; zf
  4886.  
  4887.         cmp     eax, eax        ; ZF
  4888. @@:
  4889.         pop     eax
  4890.         ret
  4891. endp
  4892.  
  4893. if ~ lang eq sp
  4894. diff16 "end of .text segment",0,$
  4895. end if
  4896.  
  4897. include "data32.inc"
  4898.  
  4899. __REV__ = __REV
  4900.  
  4901. if ~ lang eq sp
  4902. diff16 "end of kernel code",0,$
  4903. end if
  4904.