Subversion Repositories Kolibri OS

Rev

Rev 8493 | Rev 8675 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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