Subversion Repositories Kolibri OS

Rev

Rev 8329 | Rev 8598 | 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: 8493 $
  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.         mov     edi, edx
  2443.         mov     esi, DRIVE_DATA
  2444.         mov     ecx, DRIVE_DATA_SIZE ;10
  2445.         cld
  2446.         rep movsb
  2447. .exit:
  2448.         ret
  2449. ;------------------------------------------------------------------------------
  2450. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2451.         and     dword [esp+32], 0
  2452.         ret
  2453. ;------------------------------------------------------------------------------
  2454. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2455.         ; if given memory address belongs to kernel then error
  2456.         stdcall is_region_userspace, ecx, version_end-version_inf
  2457.         jz      .addr_error
  2458.  
  2459.         mov     edi, ecx
  2460.         mov     esi, version_inf
  2461.         mov     ecx, version_end-version_inf
  2462.         rep movsb
  2463.         ret
  2464. .addr_error:    ; if given memory address is illegal
  2465.         mov     dword [esp+32], -1
  2466.         ret  
  2467. ;------------------------------------------------------------------------------
  2468. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2469.      ;wait retrace functions
  2470.  sys_wait_retrace:
  2471.         mov     edx, 0x3da
  2472.  WaitRetrace_loop:
  2473.         in      al, dx
  2474.         test    al, 1000b
  2475.         jz      WaitRetrace_loop
  2476.         and     [esp+32], dword 0
  2477.         ret
  2478. ;------------------------------------------------------------------------------
  2479. align 4
  2480. sysfn_centermouse:      ; 18.15 = mouse centered
  2481.         mov     eax, [_display.width]
  2482.         shr     eax, 1
  2483.         mov     [MOUSE_X], ax
  2484.         mov     eax, [_display.height]
  2485.         shr     eax, 1
  2486.         mov     [MOUSE_Y], ax
  2487.         call    wakeup_osloop
  2488.         xor     eax, eax
  2489.         and     [esp+32], eax
  2490.         ret
  2491. ;------------------------------------------------------------------------------
  2492. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2493.         cmp     ecx, 8
  2494.         jnc     @f
  2495.         jmp     dword [.table+ecx*4]
  2496. .get_mouse_acceleration:
  2497.         xor     eax, eax
  2498.         mov     ax, [mouse_speed_factor]
  2499.         mov     [esp+32], eax
  2500.         ret
  2501. .set_mouse_acceleration:
  2502.         mov     [mouse_speed_factor], dx
  2503.         ret
  2504. .get_mouse_delay:
  2505.         xor     eax, eax
  2506.         mov     al, [mouse_delay]
  2507.         mov     [esp+32], eax
  2508.         ret
  2509. .set_mouse_delay:
  2510.         mov     [mouse_delay], dl
  2511. @@:
  2512.         ret
  2513. .set_pointer_position:
  2514.         cmp     dx, word[_display.height]
  2515.         jae     @b
  2516.         rol     edx, 16
  2517.         cmp     dx, word[_display.width]
  2518.         jae     @b
  2519.         mov     [MOUSE_X], edx
  2520.         mov     [mouse_active], 1
  2521.         jmp     wakeup_osloop
  2522. .set_mouse_button:
  2523.         mov     [BTN_DOWN], edx
  2524.         mov     [mouse_active], 1
  2525.         jmp     wakeup_osloop
  2526. .get_doubleclick_delay:
  2527.         xor     eax, eax
  2528.         mov     al, [mouse_doubleclick_delay]
  2529.         mov     [esp+32], eax
  2530.         ret
  2531. .set_doubleclick_delay:
  2532.         mov     [mouse_doubleclick_delay], dl
  2533.         ret
  2534. align 4
  2535. .table:
  2536. dd      .get_mouse_acceleration
  2537. dd      .set_mouse_acceleration
  2538. dd      .get_mouse_delay
  2539. dd      .set_mouse_delay
  2540. dd      .set_pointer_position
  2541. dd      .set_mouse_button
  2542. dd      .get_doubleclick_delay
  2543. dd      .set_doubleclick_delay
  2544. ;------------------------------------------------------------------------------
  2545. sysfn_getfreemem:
  2546.         mov     eax, [pg_data.pages_free]
  2547.         shl     eax, 2
  2548.         mov     [esp+32], eax
  2549.         ret
  2550. ;------------------------------------------------------------------------------
  2551. sysfn_getallmem:
  2552.         mov     eax, [MEM_AMOUNT]
  2553.         shr     eax, 10
  2554.         mov     [esp+32], eax
  2555.         ret
  2556. ;------------------------------------------------------------------------------
  2557. sysfn_pid_to_slot:
  2558.         mov     eax, ecx
  2559.         call    pid_to_slot
  2560.         mov     [esp+32], eax
  2561.         ret
  2562. ;------------------------------------------------------------------------------
  2563. sysfn_min_rest_window:
  2564.         pushad
  2565.         mov     eax, edx ; ebx - operating
  2566.         shr     ecx, 1
  2567.         jnc     @f
  2568.         call    pid_to_slot
  2569. @@:
  2570.         or      eax, eax ; eax - number of slot
  2571.         jz      .error
  2572.         cmp     eax, 255    ; varify maximal slot number
  2573.         ja      .error
  2574.         movzx   eax, word [WIN_STACK + eax*2]
  2575.         shr     ecx, 1
  2576.         jc      .restore
  2577.  ; .minimize:
  2578.         call    minimize_window
  2579.         jmp     .exit
  2580. .restore:
  2581.         call    restore_minimized_window
  2582. .exit:
  2583.         popad
  2584.         xor     eax, eax
  2585.         mov     [esp+32], eax
  2586.         ret
  2587. .error:
  2588.         popad
  2589.         xor     eax, eax
  2590.         dec     eax
  2591.         mov     [esp+32], eax
  2592.         ret
  2593. ;------------------------------------------------------------------------------
  2594. sysfn_min_windows:
  2595.         call    minimize_all_window
  2596.         mov     [esp+32], eax
  2597.         call    change_task
  2598.         ret
  2599. ;------------------------------------------------------------------------------
  2600. sysfn_set_screen_sizes:
  2601.         cmp     [SCR_MODE], word 0x13
  2602.         jbe     .exit
  2603.  
  2604.         cmp     [_display.select_cursor], select_cursor
  2605.         jne     .exit
  2606.  
  2607.         cmp     ecx, [display_width_standard]
  2608.         ja      .exit
  2609.  
  2610.         cmp     edx, [display_height_standard]
  2611.         ja      .exit
  2612.  
  2613.         pushfd
  2614.         cli
  2615.         mov     eax, ecx
  2616.         mov     ecx, [_display.lfb_pitch]
  2617.         mov     [_display.width], eax
  2618.         dec     eax
  2619.         mov     [_display.height], edx
  2620.         dec     edx
  2621. ; eax - new Screen_Max_X
  2622. ; edx - new Screen_Max_Y
  2623.         mov     [do_not_touch_winmap], 1
  2624.         call    set_screen
  2625.         mov     [do_not_touch_winmap], 0
  2626.         popfd
  2627.         call    change_task
  2628. .exit:
  2629.         ret
  2630. ;------------------------------------------------------------------------------
  2631. uglobal
  2632. screen_workarea RECT
  2633. display_width_standard dd 0
  2634. display_height_standard dd 0
  2635. do_not_touch_winmap db 0
  2636. window_minimize db 0
  2637. sound_flag      db 0
  2638.  
  2639. endg
  2640.  
  2641. UID_NONE=0
  2642. UID_MENUETOS=1   ;official
  2643. UID_KOLIBRI=2    ;russian
  2644.  
  2645. iglobal
  2646. version_inf:
  2647.         db 0,7,7,0  ; version 0.7.7.0
  2648.         db 0
  2649. .rev    dd __REV__
  2650. version_end:
  2651. endg
  2652. ;------------------------------------------------------------------------------
  2653. align 4
  2654. sys_cachetodiskette:
  2655.         cmp     ebx, 1
  2656.         jb      .no_floppy_save
  2657.         cmp     ebx, 2
  2658.         ja      .no_floppy_save
  2659.         call    save_image
  2660.         mov     [esp + 32], eax
  2661.         ret
  2662. .no_floppy_save:
  2663.         mov     [esp + 32], dword 1
  2664.         ret
  2665. ;------------------------------------------------------------------------------
  2666. uglobal
  2667. ;  bgrchanged  dd  0x0
  2668. align 4
  2669. bgrlockpid dd 0
  2670. bgrlock db 0
  2671. endg
  2672. ;------------------------------------------------------------------------------
  2673. align 4
  2674. sys_background:
  2675.         cmp     ebx, 1                     ; BACKGROUND SIZE
  2676.         jnz     nosb1
  2677.         test    ecx, ecx
  2678.         jz      sbgrr
  2679.  
  2680.         test    edx, edx
  2681.         jz      sbgrr
  2682. ;--------------------------------------
  2683. align 4
  2684. @@:
  2685. ;;Maxis use atomic bts for mutexes  4.4.2009
  2686.         bts     dword [bgrlock], 0
  2687.         jnc     @f
  2688.         call    change_task
  2689.         jmp     @b
  2690. ;--------------------------------------
  2691. align 4
  2692. @@:
  2693.         mov     [BgrDataWidth], ecx
  2694.         mov     [BgrDataHeight], edx
  2695. ;    mov   [bgrchanged],1
  2696.  
  2697.         pushad
  2698. ; return memory for old background
  2699.         mov     eax, [img_background]
  2700.         cmp     eax, static_background_data
  2701.         jz      @f
  2702.         stdcall kernel_free, eax
  2703. ;--------------------------------------
  2704. align 4
  2705. @@:
  2706. ; calculate RAW size
  2707.         xor     eax, eax
  2708.         inc     eax
  2709.         cmp     [BgrDataWidth], eax
  2710.         jae     @f
  2711.         mov     [BgrDataWidth], eax
  2712. ;--------------------------------------
  2713. align 4
  2714. @@:
  2715.         cmp     [BgrDataHeight], eax
  2716.         jae     @f
  2717.         mov     [BgrDataHeight], eax
  2718. ;--------------------------------------
  2719. align 4
  2720. @@:
  2721.         mov     eax, [BgrDataWidth]
  2722.         imul    eax, [BgrDataHeight]
  2723.         lea     eax, [eax*3]
  2724. ; it is reserved with aligned to the boundary of 4 KB pages,
  2725. ; otherwise there may be exceptions a page fault for vesa20_drawbackground_tiled
  2726. ; because the 32 bit read is used for  high performance: "mov eax,[esi]"
  2727.         shr     eax, 12
  2728.         inc     eax
  2729.         shl     eax, 12
  2730.         mov     [mem_BACKGROUND], eax
  2731. ; get memory for new background
  2732.         stdcall kernel_alloc, eax
  2733.         test    eax, eax
  2734.         jz      .memfailed
  2735.         mov     [img_background], eax
  2736.         jmp     .exit
  2737. ;--------------------------------------
  2738. align 4
  2739. .memfailed:
  2740. ; revert to static monotone data
  2741.         mov     [img_background], static_background_data
  2742.         xor     eax, eax
  2743.         inc     eax
  2744.         mov     [BgrDataWidth], eax
  2745.         mov     [BgrDataHeight], eax
  2746.         mov     [mem_BACKGROUND], 4
  2747. ;--------------------------------------
  2748. align 4
  2749. .exit:
  2750.         popad
  2751.         mov     [bgrlock], 0
  2752. ;--------------------------------------
  2753. align 4
  2754. sbgrr:
  2755.         ret
  2756. ;------------------------------------------------------------------------------
  2757. align 4
  2758. nosb1:
  2759.         cmp     ebx, 2                     ; SET PIXEL
  2760.         jnz     nosb2
  2761.  
  2762.         mov     eax, [img_background]
  2763.         test    ecx, ecx
  2764.         jz      @f
  2765.         cmp     eax, static_background_data
  2766.         jz      .ret
  2767. ;--------------------------------------
  2768. align 4
  2769. @@:
  2770.         mov     ebx, [mem_BACKGROUND]
  2771.         add     ebx, 4095
  2772.         and     ebx, -4096
  2773.         sub     ebx, 4
  2774.         cmp     ecx, ebx
  2775.         ja      .ret
  2776.  
  2777.         mov     ebx, [eax+ecx]
  2778.         and     ebx, 0xFF000000;255*256*256*256
  2779.         and     edx, 0x00FFFFFF;255*256*256+255*256+255
  2780.         add     edx, ebx
  2781.         mov     [eax+ecx], edx
  2782. ;--------------------------------------
  2783. align 4
  2784. .ret:
  2785.         ret
  2786. ;------------------------------------------------------------------------------
  2787. align 4
  2788. nosb2:
  2789.         cmp     ebx, 3                     ; DRAW BACKGROUND
  2790.         jnz     nosb3
  2791. ;--------------------------------------
  2792. align 4
  2793. draw_background_temp:
  2794.         mov     [background_defined], 1
  2795.         call    force_redraw_background
  2796. ;--------------------------------------
  2797. align 4
  2798. nosb31:
  2799.         ret
  2800. ;------------------------------------------------------------------------------
  2801. align 4
  2802. nosb3:
  2803.         cmp     ebx, 4                     ; TILED / STRETCHED
  2804.         jnz     nosb4
  2805.         cmp     ecx, [BgrDrawMode]
  2806.         je      nosb41
  2807.         mov     [BgrDrawMode], ecx
  2808. ;--------------------------------------
  2809. align 4
  2810. nosb41:
  2811.         ret
  2812. ;------------------------------------------------------------------------------
  2813. align 4
  2814. nosb4:
  2815.         cmp     ebx, 5                     ; BLOCK MOVE TO BGR
  2816.         jnz     nosb5
  2817.         cmp     [img_background], static_background_data
  2818.         jnz     @f
  2819.         test    edx, edx
  2820.         jnz     .fin
  2821.         cmp     esi, 4
  2822.         ja      .fin
  2823. ;--------------------------------------
  2824. align 4
  2825. @@:
  2826.   ; bughere
  2827.         mov     eax, ecx
  2828.         mov     ebx, edx
  2829.         add     ebx, [img_background];IMG_BACKGROUND
  2830.         mov     ecx, esi
  2831.         call    memmove
  2832. ;--------------------------------------
  2833. align 4
  2834. .fin:
  2835.         ret
  2836. ;------------------------------------------------------------------------------
  2837. align 4
  2838. nosb5:
  2839.         cmp     ebx, 6
  2840.         jnz     nosb6
  2841. ;--------------------------------------
  2842. align 4
  2843. ;;Maxis use atomic bts for mutex 4.4.2009
  2844. @@:
  2845.         bts     dword [bgrlock], 0
  2846.         jnc     @f
  2847.         call    change_task
  2848.         jmp     @b
  2849. ;--------------------------------------
  2850. align 4
  2851. @@:
  2852.         mov     eax, [CURRENT_TASK]
  2853.         mov     [bgrlockpid], eax
  2854.         cmp     [img_background], static_background_data
  2855.         jz      .nomem
  2856.         stdcall user_alloc, [mem_BACKGROUND]
  2857.         mov     [esp+32], eax
  2858.         test    eax, eax
  2859.         jz      .nomem
  2860.         mov     ebx, eax
  2861.         shr     ebx, 12
  2862.         or      dword [page_tabs+(ebx-1)*4], MEM_BLOCK_DONT_FREE
  2863.         mov     esi, [img_background]
  2864.         shr     esi, 12
  2865.         mov     ecx, [mem_BACKGROUND]
  2866.         add     ecx, 0xFFF
  2867.         shr     ecx, 12
  2868. ;--------------------------------------
  2869. align 4
  2870. .z:
  2871.         mov     eax, [page_tabs+ebx*4]
  2872.         test    al, 1
  2873.         jz      @f
  2874.         call    free_page
  2875. ;--------------------------------------
  2876. align 4
  2877. @@:
  2878.         mov     eax, [page_tabs+esi*4]
  2879.         or      al, PG_UWR
  2880.         mov     [page_tabs+ebx*4], eax
  2881.         mov     eax, ebx
  2882.         shl     eax, 12
  2883.         invlpg  [eax]
  2884.         inc     ebx
  2885.         inc     esi
  2886.         loop    .z
  2887.         ret
  2888. ;--------------------------------------
  2889. align 4
  2890. .nomem:
  2891.         and     [bgrlockpid], 0
  2892.         mov     [bgrlock], 0
  2893. ;------------------------------------------------------------------------------
  2894. align 4
  2895. nosb6:
  2896.         cmp     ebx, 7
  2897.         jnz     nosb7
  2898.         cmp     [bgrlock], 0
  2899.         jz      .err
  2900.         mov     eax, [CURRENT_TASK]
  2901.         cmp     [bgrlockpid], eax
  2902.         jnz     .err
  2903.         mov     eax, ecx
  2904.         mov     ebx, ecx
  2905.         shr     eax, 12
  2906.         mov     ecx, [page_tabs+(eax-1)*4]
  2907.         test    cl, MEM_BLOCK_USED or MEM_BLOCK_DONT_FREE
  2908.         jz      .err
  2909.         jnp     .err
  2910.         push    eax
  2911.         shr     ecx, 12
  2912.         dec     ecx
  2913. ;--------------------------------------
  2914. align 4
  2915. @@:
  2916.         and     dword [page_tabs+eax*4], 0
  2917.         mov     edx, eax
  2918.         shl     edx, 12
  2919.         push    eax
  2920.         invlpg  [edx]
  2921.         pop     eax
  2922.         inc     eax
  2923.         loop    @b
  2924.         pop     eax
  2925.         and     dword [page_tabs+(eax-1)*4], not MEM_BLOCK_DONT_FREE
  2926.         stdcall user_free, ebx
  2927.         mov     [esp+32], eax
  2928.         and     [bgrlockpid], 0
  2929.         mov     [bgrlock], 0
  2930.         ret
  2931. ;--------------------------------------
  2932. align 4
  2933. .err:
  2934.         and     dword [esp+32], 0
  2935.         ret
  2936. ;------------------------------------------------------------------------------
  2937. align 4
  2938. nosb7:
  2939.         cmp     ebx, 8
  2940.         jnz     nosb8
  2941.  
  2942.         mov     ecx, [current_slot]
  2943.         xor     eax, eax
  2944.         xchg    eax, [ecx+APPDATA.draw_bgr_x]
  2945.         mov     [esp + 32], eax ; eax = [left]*65536 + [right]
  2946.         xor     eax, eax
  2947.         xchg    eax, [ecx+APPDATA.draw_bgr_y]
  2948.         mov     [esp + 20], eax ; ebx = [top]*65536 + [bottom]
  2949.         ret
  2950. ;------------------------------------------------------------------------------
  2951. align 4
  2952. nosb8:
  2953.         cmp     ebx, 9
  2954.         jnz     nosb9
  2955. ; ecx = [left]*65536 + [right]
  2956. ; edx = [top]*65536 + [bottom]
  2957.         mov     eax, [_display.width]
  2958.         mov     ebx, [_display.height]
  2959. ; check [right]
  2960.         cmp     cx, ax
  2961.         jae     .exit
  2962. ; check [left]
  2963.         ror     ecx, 16
  2964.         cmp     cx, ax
  2965.         jae     .exit
  2966. ; check [bottom]
  2967.         cmp     dx, bx
  2968.         jae     .exit
  2969. ; check [top]
  2970.         ror     edx, 16
  2971.         cmp     dx, bx
  2972.         jae     .exit
  2973.  
  2974.         movzx   eax, cx  ; [left]
  2975.         movzx   ebx, dx  ; [top]
  2976.  
  2977.         shr     ecx, 16 ; [right]
  2978.         shr     edx, 16 ; [bottom]
  2979.  
  2980.         mov     [background_defined], 1
  2981.  
  2982.         mov     [draw_data+32 + RECT.left], eax
  2983.         mov     [draw_data+32 + RECT.top], ebx
  2984.  
  2985.         mov     [draw_data+32 + RECT.right], ecx
  2986.         mov     [draw_data+32 + RECT.bottom], edx
  2987.  
  2988.         inc     [REDRAW_BACKGROUND]
  2989.         call    wakeup_osloop
  2990. ;--------------------------------------
  2991. align 4
  2992. .exit:
  2993.         ret
  2994. ;------------------------------------------------------------------------------
  2995. align 4
  2996. nosb9:
  2997.         ret
  2998. ;------------------------------------------------------------------------------
  2999. align 4
  3000. uglobal
  3001.   BG_Rect_X_left_right  dd   0x0
  3002.   BG_Rect_Y_top_bottom  dd   0x0
  3003. endg
  3004. ;------------------------------------------------------------------------------
  3005. align 4
  3006. force_redraw_background:
  3007.         and     [draw_data+32 + RECT.left], 0
  3008.         and     [draw_data+32 + RECT.top], 0
  3009.         push    eax ebx
  3010.         mov     eax, [_display.width]
  3011.         mov     ebx, [_display.height]
  3012.         dec     eax
  3013.         dec     ebx
  3014.         mov     [draw_data+32 + RECT.right], eax
  3015.         mov     [draw_data+32 + RECT.bottom], ebx
  3016.         pop     ebx eax
  3017.         inc     [REDRAW_BACKGROUND]
  3018.         call    wakeup_osloop
  3019.         ret
  3020. ;------------------------------------------------------------------------------
  3021. align 4
  3022. sys_getbackground:
  3023. ;    cmp   eax,1                                  ; SIZE
  3024.         dec     ebx
  3025.         jnz     nogb1
  3026.         mov     eax, [BgrDataWidth]
  3027.         shl     eax, 16
  3028.         mov     ax, word [BgrDataHeight]
  3029.         mov     [esp+32], eax
  3030.         ret
  3031. ;------------------------------------------------------------------------------
  3032. align 4
  3033. nogb1:
  3034. ;    cmp   eax,2                                  ; PIXEL
  3035.         dec     ebx
  3036.         jnz     nogb2
  3037.  
  3038.         mov     eax, [img_background]
  3039.         test    ecx, ecx
  3040.         jz      @f
  3041.         cmp     eax, static_background_data
  3042.         jz      .ret
  3043. ;--------------------------------------
  3044. align 4
  3045. @@:
  3046.         mov     ebx, [mem_BACKGROUND]
  3047.         add     ebx, 4095
  3048.         and     ebx, -4096
  3049.         sub     ebx, 4
  3050.         cmp     ecx, ebx
  3051.         ja      .ret
  3052.  
  3053.         mov     eax, [ecx+eax]
  3054.  
  3055.         and     eax, 0xFFFFFF
  3056.         mov     [esp+32], eax
  3057. ;--------------------------------------
  3058. align 4
  3059. .ret:
  3060.         ret
  3061. ;------------------------------------------------------------------------------
  3062. align 4
  3063. nogb2:
  3064.  
  3065. ;    cmp   eax,4                                  ; TILED / STRETCHED
  3066.         dec     ebx
  3067.         dec     ebx
  3068.         jnz     nogb4
  3069.         mov     eax, [BgrDrawMode]
  3070. ;--------------------------------------
  3071. align 4
  3072. nogb4:
  3073.         mov     [esp+32], eax
  3074.         ret
  3075. ;------------------------------------------------------------------------------
  3076. align 4
  3077. sys_getkey:
  3078.         mov     [esp + 32], dword 1
  3079.         ; test main buffer
  3080.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  3081.         movzx   ecx, word [WIN_STACK + ebx * 2]
  3082.         mov     edx, [TASK_COUNT]
  3083.         cmp     ecx, edx
  3084.         jne     .finish
  3085.         cmp     [KEY_COUNT], byte 0
  3086.         je      .finish
  3087.         movzx   ax, byte [KEY_BUFF + 120 + 2]
  3088.         shl     eax, 8
  3089.         mov     al, byte [KEY_BUFF]
  3090.         shl     eax, 8
  3091.         push    eax
  3092.         dec     byte [KEY_COUNT]
  3093.         and     byte [KEY_COUNT], 127
  3094.         movzx   ecx, byte [KEY_COUNT]
  3095.         add     ecx, 2
  3096.         mov     eax, KEY_BUFF + 1
  3097.         mov     ebx, KEY_BUFF
  3098.         call    memmove
  3099.         add     eax, 120 + 2
  3100.         add     ebx, 120 + 2
  3101.         call    memmove
  3102.         pop     eax
  3103. ;--------------------------------------
  3104. align 4
  3105. .ret_eax:
  3106.         mov     [esp + 32], eax
  3107.         ret
  3108. ;--------------------------------------
  3109. align 4
  3110. .finish:
  3111. ; test hotkeys buffer
  3112.         mov     ecx, hotkey_buffer
  3113. ;--------------------------------------
  3114. align 4
  3115. @@:
  3116.         cmp     [ecx], ebx
  3117.         jz      .found
  3118.         add     ecx, 8
  3119.         cmp     ecx, hotkey_buffer + 120 * 8
  3120.         jb      @b
  3121.         ret
  3122. ;--------------------------------------
  3123. align 4
  3124. .found:
  3125.         mov     ax, [ecx + 6]
  3126.         shl     eax, 16
  3127.         mov     ah, [ecx + 4]
  3128.         mov     al, 2
  3129.         and     dword [ecx + 4], 0
  3130.         and     dword [ecx], 0
  3131.         jmp     .ret_eax
  3132. ;------------------------------------------------------------------------------
  3133. align 4
  3134. sys_getbutton:
  3135.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  3136.         mov     [esp + 32], dword 1
  3137.         movzx   ecx, word [WIN_STACK + ebx * 2]
  3138.         mov     edx, [TASK_COUNT] ; less than 256 processes
  3139.         cmp     ecx, edx
  3140.         jne     .exit
  3141.         movzx   eax, byte [BTN_COUNT]
  3142.         test    eax, eax
  3143.         jz      .exit
  3144.         mov     eax, [BTN_BUFF]
  3145.         and     al, 0xFE                                    ; delete left button bit
  3146.         mov     [BTN_COUNT], byte 0
  3147.         mov     [esp + 32], eax
  3148. ;--------------------------------------
  3149. align 4
  3150. .exit:
  3151.         ret
  3152. ;------------------------------------------------------------------------------
  3153. align 4
  3154. sys_cpuusage:
  3155.  
  3156. ;  RETURN:
  3157. ;
  3158. ;  +00 dword     process cpu usage
  3159. ;  +04  word     position in windowing stack
  3160. ;  +06  word     windowing stack value at current position (cpu nro)
  3161. ;  +10 12 bytes  name
  3162. ;  +22 dword     start in mem
  3163. ;  +26 dword     used mem
  3164. ;  +30 dword     PID , process idenfification number
  3165. ;
  3166.         ; if given memory address belongs to kernel then error
  3167.         stdcall is_region_userspace, ebx, 0x4C
  3168.         jz      .addr_error
  3169.  
  3170.         cmp     ecx, -1 ; who am I ?
  3171.         jne     .no_who_am_i
  3172.         mov     ecx, [CURRENT_TASK]
  3173.   .no_who_am_i:
  3174.         cmp     ecx, max_processes
  3175.         ja      .nofillbuf
  3176.  
  3177. ; +4: word: position of the window of thread in the window stack
  3178.         mov     ax, [WIN_STACK + ecx * 2]
  3179.         mov     [ebx+4], ax
  3180. ; +6: word: number of the thread slot, which window has in the window stack
  3181. ;           position ecx (has no relation to the specific thread)
  3182.         mov     ax, [WIN_POS + ecx * 2]
  3183.         mov     [ebx+6], ax
  3184.  
  3185.         shl     ecx, 5
  3186.  
  3187. ; +0: dword: memory usage
  3188.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  3189.         mov     [ebx], eax
  3190. ; +10: 11 bytes: name of the process
  3191.         push    ecx
  3192.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  3193.         add     ebx, 10
  3194.         mov     ecx, 11
  3195.         call    memmove
  3196.         pop     ecx
  3197.  
  3198. ; +22: address of the process in memory
  3199. ; +26: size of used memory - 1
  3200.         push    edi
  3201.         lea     edi, [ebx+12]
  3202.         xor     eax, eax
  3203.         mov     edx, 0x100000*16
  3204.         cmp     ecx, 1 shl 5
  3205.         je      .os_mem
  3206.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.process]
  3207.         mov     edx, [edx+PROC.mem_used]
  3208.         mov     eax, std_application_base_address
  3209. .os_mem:
  3210.         stosd
  3211.         lea     eax, [edx-1]
  3212.         stosd
  3213.  
  3214. ; +30: PID/TID
  3215.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  3216.         stosd
  3217.  
  3218.     ; window position and size
  3219.         push    esi
  3220.         lea     esi, [ecx + window_data + WDATA.box]
  3221.         movsd
  3222.         movsd
  3223.         movsd
  3224.         movsd
  3225.  
  3226.     ; Process state (+50)
  3227.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  3228.         stosd
  3229.  
  3230.     ; Window client area box
  3231.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  3232.         movsd
  3233.         movsd
  3234.         movsd
  3235.         movsd
  3236.  
  3237.     ; Window state
  3238.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  3239.         stosb
  3240.  
  3241.     ; Event mask (+71)
  3242.         mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
  3243.         stosd
  3244.  
  3245.     ; Keyboard mode (+75)
  3246.         mov     al, byte [ecx*8 + SLOT_BASE + APPDATA.keyboard_mode]
  3247.         stosb
  3248.  
  3249.         pop     esi
  3250.         pop     edi
  3251.  
  3252. .nofillbuf:
  3253.     ; return number of processes
  3254.  
  3255.         mov     eax, [TASK_COUNT]
  3256.         mov     [esp+32], eax
  3257.         ret
  3258.  
  3259. .addr_error:    ; if given memory address is illegal
  3260.         mov     dword [esp+32], -1
  3261.         ret  
  3262.  
  3263. align 4
  3264. sys_clock:
  3265.         cli
  3266.   ; Mikhail Lisovin  xx Jan 2005
  3267.   @@:
  3268.         mov     al, 10
  3269.         out     0x70, al
  3270.         in      al, 0x71
  3271.         test    al, al
  3272.         jns     @f
  3273.         mov     esi, 1
  3274.         call    delay_ms
  3275.         jmp     @b
  3276.   @@:
  3277.   ; end Lisovin's fix
  3278.  
  3279.         xor     al, al        ; seconds
  3280.         out     0x70, al
  3281.         in      al, 0x71
  3282.         movzx   ecx, al
  3283.         mov     al, 02        ; minutes
  3284.         shl     ecx, 16
  3285.         out     0x70, al
  3286.         in      al, 0x71
  3287.         movzx   edx, al
  3288.         mov     al, 04        ; hours
  3289.         shl     edx, 8
  3290.         out     0x70, al
  3291.         in      al, 0x71
  3292.         add     ecx, edx
  3293.         movzx   edx, al
  3294.         add     ecx, edx
  3295.         sti
  3296.         mov     [esp + 32], ecx
  3297.         ret
  3298.  
  3299.  
  3300. align 4
  3301.  
  3302. sys_date:
  3303.  
  3304.         cli
  3305.   @@:
  3306.         mov     al, 10
  3307.         out     0x70, al
  3308.         in      al, 0x71
  3309.         test    al, al
  3310.         jns     @f
  3311.         mov     esi, 1
  3312.         call    delay_ms
  3313.         jmp     @b
  3314.   @@:
  3315.  
  3316.         mov     ch, 0
  3317.         mov     al, 7           ; date
  3318.         out     0x70, al
  3319.         in      al, 0x71
  3320.         mov     cl, al
  3321.         mov     al, 8           ; month
  3322.         shl     ecx, 16
  3323.         out     0x70, al
  3324.         in      al, 0x71
  3325.         mov     ch, al
  3326.         mov     al, 9           ; year
  3327.         out     0x70, al
  3328.         in      al, 0x71
  3329.         mov     cl, al
  3330.         sti
  3331.         mov     [esp+32], ecx
  3332.         ret
  3333.  
  3334.  
  3335. ; redraw status
  3336.  
  3337. sys_redrawstat:
  3338.         cmp     ebx, 1
  3339.         jne     no_widgets_away
  3340.         ; buttons away
  3341.         mov     ecx, [CURRENT_TASK]
  3342.   sys_newba2:
  3343.         mov     edi, [BTN_ADDR]
  3344.         cmp     [edi], dword 0  ; empty button list ?
  3345.         je      end_of_buttons_away
  3346.         movzx   ebx, word [edi]
  3347.         inc     ebx
  3348.         mov     eax, edi
  3349.   sys_newba:
  3350.         dec     ebx
  3351.         jz      end_of_buttons_away
  3352.  
  3353.         add     eax, 0x10
  3354.         cmp     cx, [eax]
  3355.         jnz     sys_newba
  3356.  
  3357.         push    eax ebx ecx
  3358.         mov     ecx, ebx
  3359.         inc     ecx
  3360.         shl     ecx, 4
  3361.         mov     ebx, eax
  3362.         add     eax, 0x10
  3363.         call    memmove
  3364.         dec     dword [edi]
  3365.         pop     ecx ebx eax
  3366.  
  3367.         jmp     sys_newba2
  3368.  
  3369.   end_of_buttons_away:
  3370.  
  3371.         ret
  3372.  
  3373.   no_widgets_away:
  3374.  
  3375.         cmp     ebx, 2
  3376.         jnz     srl1
  3377.  
  3378.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  3379.         add     edx, draw_data - CURRENT_TASK
  3380.         mov     [edx + RECT.left], 0
  3381.         mov     [edx + RECT.top], 0
  3382.         mov     eax, [_display.width]
  3383.         dec     eax
  3384.         mov     [edx + RECT.right], eax
  3385.         mov     eax, [_display.height]
  3386.         dec     eax
  3387.         mov     [edx + RECT.bottom], eax
  3388.  
  3389.   srl1:
  3390.         ret
  3391.  
  3392. ;ok - 100% work
  3393. ;nt - not tested
  3394. ;---------------------------------------------------------------------------------------------
  3395. ;eax
  3396. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3397. ;1 - change task. Ret nothing. Block. ok.
  3398. ;2 - performance control
  3399. ; ebx
  3400. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3401. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3402. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3403. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3404. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3405. ;eax
  3406. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3407. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3408. ;---------------------------------------------------------------------------------------------
  3409. iglobal
  3410. align 4
  3411. sheduler:
  3412.         dd      sys_sheduler.00
  3413.         dd      change_task
  3414.         dd      sys_sheduler.02
  3415.         dd      sys_sheduler.03
  3416.         dd      sys_sheduler.04
  3417. endg
  3418. sys_sheduler:
  3419. ;rewritten by <Lrz>  29.12.2009
  3420.         jmp     dword [sheduler+ebx*4]
  3421. ;.shed_counter:
  3422. .00:
  3423.         mov     eax, [context_counter]
  3424.         mov     [esp+32], eax
  3425.         ret
  3426.  
  3427. .02:
  3428. ;.perf_control:
  3429.         inc     ebx                     ;before ebx=2, ebx=3
  3430.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  3431.         jz      cache_disable
  3432.  
  3433.         dec     ebx                     ;ebx=2
  3434.         cmp     ebx, ecx                ;
  3435.         jz      cache_enable            ;if ecx=2 and ebx=2
  3436.  
  3437.         dec     ebx                     ;ebx=1
  3438.         cmp     ebx, ecx
  3439.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  3440.  
  3441.         dec     ebx
  3442.         test    ebx, ecx                ;ebx=0 and ecx=0
  3443.         jz      modify_pce              ;if ecx=0
  3444.  
  3445.         ret
  3446.  
  3447. .03:
  3448. ;.rdmsr_instr:
  3449. ;now counter in ecx
  3450. ;(edx:eax) esi:edi => edx:esi
  3451.         mov     eax, esi
  3452.         mov     ecx, edx
  3453.         rdmsr
  3454.         mov     [esp+32], eax
  3455.         mov     [esp+20], edx           ;ret in ebx?
  3456.         ret
  3457.  
  3458. .04:
  3459. ;.wrmsr_instr:
  3460. ;now counter in ecx
  3461. ;(edx:eax) esi:edi => edx:esi
  3462.         ; Fast Call MSR can't be destroy
  3463.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  3464.         ; включаются/выключаются расширенные возможности
  3465.         cmp     edx, MSR_SYSENTER_CS
  3466.         je      @f
  3467.         cmp     edx, MSR_SYSENTER_ESP
  3468.         je      @f
  3469.         cmp     edx, MSR_SYSENTER_EIP
  3470.         je      @f
  3471.         cmp     edx, MSR_AMD_STAR
  3472.         je      @f
  3473.  
  3474.         mov     eax, esi
  3475.         mov     ecx, edx
  3476.         wrmsr
  3477.         ; mov   [esp + 32], eax
  3478.         ; mov   [esp + 20], edx ;ret in ebx?
  3479. @@:
  3480.         ret
  3481.  
  3482. cache_disable:
  3483.         mov     eax, cr0
  3484.         or      eax, 01100000000000000000000000000000b
  3485.         mov     cr0, eax
  3486.         wbinvd  ;set MESI
  3487.         ret
  3488.  
  3489. cache_enable:
  3490.         mov     eax, cr0
  3491.         and     eax, 10011111111111111111111111111111b
  3492.         mov     cr0, eax
  3493.         ret
  3494.  
  3495. is_cache_enabled:
  3496.         mov     eax, cr0
  3497.         mov     ebx, eax
  3498.         and     eax, 01100000000000000000000000000000b
  3499.         jz      cache_disabled
  3500.         mov     [esp+32], ebx
  3501. cache_disabled:
  3502.         mov     dword [esp+32], eax;0
  3503.         ret
  3504.  
  3505. modify_pce:
  3506.         mov     eax, cr4
  3507. ;       mov ebx,0
  3508. ;       or  bx,100000000b ;pce
  3509. ;       xor eax,ebx ;invert pce
  3510.         bts     eax, 8;pce=cr4[8]
  3511.         mov     cr4, eax
  3512.         mov     [esp+32], eax
  3513.         ret
  3514. ;---------------------------------------------------------------------------------------------
  3515.  
  3516.  
  3517. iglobal
  3518.   cpustring db 'CPU',0
  3519. endg
  3520.  
  3521. uglobal
  3522. background_defined    db    0    ; diamond, 11.04.2006
  3523. endg
  3524. ;-----------------------------------------------------------------------------
  3525. align 4
  3526. checkmisc:
  3527.         cmp     [ctrl_alt_del], 1
  3528.         jne     nocpustart
  3529.  
  3530.         mov     ebp, cpustring
  3531.         call    fs_execute_from_sysdir
  3532.  
  3533.         mov     [ctrl_alt_del], 0
  3534. ;--------------------------------------
  3535. align 4
  3536. nocpustart:
  3537.         cmp     [mouse_active], 1
  3538.         jne     mouse_not_active
  3539.         mov     [mouse_active], 0
  3540.  
  3541.         xor     edi, edi
  3542.         mov     ebx, CURRENT_TASK
  3543.  
  3544.         mov     ecx, [TASK_COUNT]
  3545.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  3546.         shl     eax, 8
  3547.         push    eax
  3548.  
  3549.         movzx   eax, word [MOUSE_X]
  3550.         movzx   edx, word [MOUSE_Y]
  3551. ;--------------------------------------
  3552. align 4
  3553. .set_mouse_event:
  3554.         add     edi, sizeof.APPDATA
  3555.         add     ebx, sizeof.TASKDATA
  3556.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3557.         jz      .pos_filter
  3558.  
  3559.         cmp     edi, [esp]                      ; skip if filtration active
  3560.         jne     .skip
  3561. ;--------------------------------------
  3562. align 4
  3563. .pos_filter:
  3564.         test    [ebx+TASKDATA.event_mask], 0x40000000
  3565.         jz      .set
  3566.  
  3567.         mov     esi, [ebx-twdw+WDATA.box.left]
  3568.         cmp     eax, esi
  3569.         jb      .skip
  3570.         add     esi, [ebx-twdw+WDATA.box.width]
  3571.         cmp     eax, esi
  3572.         ja      .skip
  3573.  
  3574.         mov     esi, [ebx-twdw+WDATA.box.top]
  3575.         cmp     edx, esi
  3576.         jb      .skip
  3577.         add     esi, [ebx-twdw+WDATA.box.height]
  3578.         cmp     edx, esi
  3579.         ja      .skip
  3580. ;--------------------------------------
  3581. align 4
  3582. .set:
  3583.         or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b  ; set event 6
  3584. ;--------------------------------------
  3585. align 4
  3586. .skip:
  3587.         loop    .set_mouse_event
  3588.  
  3589.         pop     eax
  3590. ;--------------------------------------
  3591. align 4
  3592. mouse_not_active:
  3593.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  3594.         jz      nobackgr
  3595.  
  3596.         cmp     [background_defined], 0
  3597.         jz      nobackgr
  3598. ;--------------------------------------
  3599. align 4
  3600. backgr:
  3601.         mov     eax, [draw_data+32 + RECT.left]
  3602.         shl     eax, 16
  3603.         add     eax, [draw_data+32 + RECT.right]
  3604.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  3605.  
  3606.         mov     eax, [draw_data+32 + RECT.top]
  3607.         shl     eax, 16
  3608.         add     eax, [draw_data+32 + RECT.bottom]
  3609.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  3610.  
  3611.         call    drawbackground
  3612. ;        DEBUGF  1, "K : drawbackground\n"
  3613. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  3614. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  3615. ;--------- set event 5 start ----------
  3616.         push    ecx edi
  3617.         xor     edi, edi
  3618.         mov     ecx, [TASK_COUNT]
  3619. ;--------------------------------------
  3620. align 4
  3621. set_bgr_event:
  3622.         add     edi, sizeof.APPDATA
  3623.         mov     eax, [BG_Rect_X_left_right]
  3624.         mov     edx, [BG_Rect_Y_top_bottom]
  3625.         cmp     [edi+SLOT_BASE+APPDATA.draw_bgr_x], 0
  3626.         jz      .set
  3627. .join:
  3628.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3629.         jae     @f
  3630.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3631. @@:
  3632.         shr     eax, 16
  3633.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3634.         jbe     @f
  3635.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3636. @@:
  3637.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3638.         jae     @f
  3639.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3640. @@:
  3641.         shr     edx, 16
  3642.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3643.         jbe     @f
  3644.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3645. @@:
  3646.         jmp     .common
  3647. .set:
  3648.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_x], eax
  3649.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_y], edx
  3650. .common:
  3651.         or      [edi+SLOT_BASE+APPDATA.event_mask], 10000b  ; set event 5
  3652.         loop    set_bgr_event
  3653.         pop     edi ecx
  3654. ;--------- set event 5 stop -----------
  3655.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  3656.         jnz     backgr
  3657.  
  3658.         xor     eax, eax
  3659.         mov     [draw_data+32 + RECT.left], eax
  3660.         mov     [draw_data+32 + RECT.top], eax
  3661.         mov     [draw_data+32 + RECT.right], eax
  3662.         mov     [draw_data+32 + RECT.bottom], eax
  3663. ;--------------------------------------
  3664. align 4
  3665. nobackgr:
  3666. ; system shutdown request
  3667.         cmp     [SYS_SHUTDOWN], byte 0
  3668.         je      noshutdown
  3669.  
  3670.         mov     edx, [shutdown_processes]
  3671.  
  3672.         cmp     [SYS_SHUTDOWN], dl
  3673.         jne     noshutdown
  3674.  
  3675.         lea     ecx, [edx-1]
  3676.         mov     edx, OS_BASE+0x3040
  3677.         jecxz   no_mark_system_shutdown
  3678. ;--------------------------------------
  3679. align 4
  3680. markz:
  3681.         push    ecx edx
  3682.         cmp     [edx+TASKDATA.state], TSTATE_FREE
  3683.         jz      .nokill
  3684.         lea     edx, [(edx-(CURRENT_TASK and 1FFFFFFFh))*8+SLOT_BASE]
  3685.         cmp     [edx+APPDATA.process], sys_proc
  3686.         jz      .nokill
  3687.         call    request_terminate
  3688.         jmp     .common
  3689. .nokill:
  3690.         dec     byte [SYS_SHUTDOWN]
  3691.         xor     eax, eax
  3692. .common:
  3693.         pop     edx ecx
  3694.         test    eax, eax
  3695.         jz      @f
  3696.         mov     [edx+TASKDATA.state], TSTATE_ZOMBIE
  3697. @@:
  3698.         add     edx, 0x20
  3699.         loop    markz
  3700.         call    wakeup_osloop
  3701. ;--------------------------------------
  3702. align 4
  3703. @@:
  3704. no_mark_system_shutdown:
  3705.         dec     byte [SYS_SHUTDOWN]
  3706.         je      system_shutdown
  3707. ;--------------------------------------
  3708. align 4
  3709. noshutdown:
  3710.         mov     eax, [TASK_COUNT]           ; termination
  3711.         mov     ebx, TASK_DATA+TASKDATA.state
  3712.         mov     esi, 1
  3713. ;--------------------------------------
  3714. align 4
  3715. newct:
  3716.         mov     cl, [ebx]
  3717.         cmp     cl, byte 3
  3718.         jz      .terminate
  3719.  
  3720.         cmp     cl, byte 4
  3721.         jnz     .noterminate
  3722. .terminate:
  3723.         pushad
  3724.         mov     ecx, eax
  3725.         shl     ecx, 8
  3726.         add     ecx, SLOT_BASE
  3727.         call    restore_default_cursor_before_killing
  3728.         popad
  3729.  
  3730.         pushad
  3731.         call    terminate
  3732.         popad
  3733.         cmp     byte [SYS_SHUTDOWN], 0
  3734.         jz      .noterminate
  3735.         dec     byte [SYS_SHUTDOWN]
  3736.         je      system_shutdown
  3737.  
  3738. .noterminate:
  3739.         add     ebx, 0x20
  3740.         inc     esi
  3741.         dec     eax
  3742.         jnz     newct
  3743.         ret
  3744. ;-----------------------------------------------------------------------------
  3745. align 4
  3746. redrawscreen:
  3747. ; eax , if process window_data base is eax, do not set flag/limits
  3748.  
  3749.         pushad
  3750.         push    eax
  3751.  
  3752. ;;;         mov   ebx,2
  3753. ;;;         call  delay_hs
  3754.  
  3755.          ;mov   ecx,0               ; redraw flags for apps
  3756.         xor     ecx, ecx
  3757. ;--------------------------------------
  3758. align 4
  3759. newdw2:
  3760.         inc     ecx
  3761.         push    ecx
  3762.  
  3763.         mov     eax, ecx
  3764.         shl     eax, 5
  3765.         add     eax, window_data
  3766.  
  3767.         cmp     eax, [esp+4]
  3768.         je      not_this_task
  3769.                                    ; check if window in redraw area
  3770.         mov     edi, eax
  3771.  
  3772.         cmp     ecx, 1             ; limit for background
  3773.         jz      bgli
  3774.  
  3775.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3776.         test    eax, eax
  3777.         jz      @f
  3778.         mov     al, [eax + WDATA.z_modif]
  3779.         cmp     [edi + WDATA.z_modif], al
  3780.         jg      ricino
  3781.       @@:
  3782.  
  3783.         mov     eax, [edi + WDATA.box.left]
  3784.         mov     ebx, [edi + WDATA.box.top]
  3785.  
  3786.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3787.         cmp     ecx, ebx
  3788.         jb      ricino
  3789.  
  3790.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3791.         cmp     ecx, eax
  3792.         jb      ricino
  3793.  
  3794.         mov     eax, [edi + WDATA.box.left]
  3795.         mov     ebx, [edi + WDATA.box.top]
  3796.         mov     ecx, [edi + WDATA.box.width]
  3797.         mov     edx, [edi + WDATA.box.height]
  3798.         add     ecx, eax
  3799.         add     edx, ebx
  3800.  
  3801.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3802.         cmp     edx, eax
  3803.         jb      ricino
  3804.  
  3805.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3806.         cmp     ecx, eax
  3807.         jb      ricino
  3808. ;--------------------------------------
  3809. align 4
  3810. bgli:
  3811.         cmp     dword[esp], 1
  3812.         jnz     .az
  3813.  
  3814.         cmp     [REDRAW_BACKGROUND], 0
  3815.         jz      .az
  3816.  
  3817.         mov     dl, 0
  3818.         lea     eax, [edi+draw_data-window_data]
  3819.         mov     ebx, [draw_limits.left]
  3820.         cmp     ebx, [eax+RECT.left]
  3821.         jae     @f
  3822.  
  3823.         mov     [eax+RECT.left], ebx
  3824.         mov     dl, 1
  3825. ;--------------------------------------
  3826. align 4
  3827. @@:
  3828.         mov     ebx, [draw_limits.top]
  3829.         cmp     ebx, [eax+RECT.top]
  3830.         jae     @f
  3831.  
  3832.         mov     [eax+RECT.top], ebx
  3833.         mov     dl, 1
  3834. ;--------------------------------------
  3835. align 4
  3836. @@:
  3837.         mov     ebx, [draw_limits.right]
  3838.         cmp     ebx, [eax+RECT.right]
  3839.         jbe     @f
  3840.  
  3841.         mov     [eax+RECT.right], ebx
  3842.         mov     dl, 1
  3843. ;--------------------------------------
  3844. align 4
  3845. @@:
  3846.         mov     ebx, [draw_limits.bottom]
  3847.         cmp     ebx, [eax+RECT.bottom]
  3848.         jbe     @f
  3849.  
  3850.         mov     [eax+RECT.bottom], ebx
  3851.         mov     dl, 1
  3852. ;--------------------------------------
  3853. align 4
  3854. @@:
  3855.         add     [REDRAW_BACKGROUND], dl
  3856.         call    wakeup_osloop
  3857.         jmp     newdw8
  3858. ;--------------------------------------
  3859. align 4
  3860. .az:
  3861.         mov     eax, edi
  3862.         add     eax, draw_data-window_data
  3863.  
  3864.         mov     ebx, [draw_limits.left]        ; set limits
  3865.         mov     [eax + RECT.left], ebx
  3866.         mov     ebx, [draw_limits.top]
  3867.         mov     [eax + RECT.top], ebx
  3868.         mov     ebx, [draw_limits.right]
  3869.         mov     [eax + RECT.right], ebx
  3870.         mov     ebx, [draw_limits.bottom]
  3871.         mov     [eax + RECT.bottom], ebx
  3872.  
  3873.         sub     eax, draw_data-window_data
  3874.  
  3875.         cmp     dword [esp], 1
  3876.         jne     nobgrd
  3877.         inc     [REDRAW_BACKGROUND]
  3878.         call    wakeup_osloop
  3879. ;--------------------------------------
  3880. align 4
  3881. newdw8:
  3882. nobgrd:
  3883. ;--------------------------------------
  3884.         push    eax  edi ebp
  3885.         mov     edi, [esp+12]
  3886.         cmp     edi, 1
  3887.         je      .found
  3888.  
  3889.         mov     eax, [draw_limits.left]
  3890.         mov     ebx, [draw_limits.top]
  3891.         mov     ecx, [draw_limits.right]
  3892.         sub     ecx, eax
  3893.         test    ecx, ecx
  3894.         jz      .not_found
  3895.  
  3896.         mov     edx, [draw_limits.bottom]
  3897.         sub     edx, ebx
  3898.         test    edx, edx
  3899.         jz      .not_found
  3900.  
  3901. ; eax - x, ebx - y
  3902. ; ecx - size x, edx - size y
  3903.         add     ebx, edx
  3904. ;--------------------------------------
  3905. align 4
  3906. .start_y:
  3907.         push    ecx
  3908. ;--------------------------------------
  3909. align 4
  3910. .start_x:
  3911.         add     eax, ecx
  3912.         mov     ebp, [d_width_calc_area + ebx*4]
  3913.         add     ebp, [_display.win_map]
  3914.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3915.         cmp     ebp, edi
  3916.         jne     @f
  3917.  
  3918.         pop     ecx
  3919.         jmp     .found
  3920. ;--------------------------------------
  3921. align 4
  3922. @@:
  3923.         sub     eax, ecx
  3924.  
  3925.         dec     ecx
  3926.         jnz     .start_x
  3927.  
  3928.         pop     ecx
  3929.         dec     ebx
  3930.         dec     edx
  3931.         jnz     .start_y
  3932. ;--------------------------------------
  3933. align 4
  3934. .not_found:
  3935.         pop     ebp edi eax
  3936.         jmp     ricino
  3937. ;--------------------------------------
  3938. align 4
  3939. .found:
  3940.         pop     ebp edi eax
  3941.  
  3942.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3943. ;--------------------------------------
  3944. align 4
  3945. ricino:
  3946. not_this_task:
  3947.         pop     ecx
  3948.  
  3949.         cmp     ecx, [TASK_COUNT]
  3950.         jle     newdw2
  3951.  
  3952.         pop     eax
  3953.         popad
  3954.         ret
  3955. ;-----------------------------------------------------------------------------
  3956. align 4
  3957. calculatebackground:   ; background
  3958.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3959.         mov     eax, 0x01010101
  3960.         mov     ecx, [_display.win_map_size]
  3961.         shr     ecx, 2
  3962.         rep stosd
  3963.         mov     byte[window_data+32+WDATA.z_modif], ZPOS_DESKTOP
  3964.         mov     [REDRAW_BACKGROUND], 0
  3965.         ret
  3966. ;-----------------------------------------------------------------------------
  3967. uglobal
  3968.   imax    dd 0x0
  3969. endg
  3970. ;-----------------------------------------------------------------------------
  3971. align 4
  3972. delay_ms:     ; delay in 1/1000 sec
  3973.         pushad
  3974.  
  3975.         cmp     [hpet_base], 0
  3976.         jz      .no_hpet
  3977.         mov     eax, esi
  3978.         mov     edx, 1_000_000 ; ms to ns
  3979.         mul     edx
  3980.         mov     ebx, edx
  3981.         mov     ecx, eax
  3982.  
  3983.         push    ecx
  3984.         call    get_clock_ns
  3985.         pop     ecx
  3986.         mov     edi, edx
  3987.         mov     esi, eax
  3988. .wait:
  3989.         push    ecx
  3990.         call    get_clock_ns
  3991.         pop     ecx
  3992.         sub     eax, esi
  3993.         sbb     edx, edi
  3994.         sub     eax, ecx
  3995.         sbb     edx, ebx
  3996.         jc      .wait
  3997.         jmp     .done
  3998.  
  3999. .no_hpet:
  4000.         mov     ecx, esi
  4001.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  4002.         imul    ecx, 33941
  4003.         shr     ecx, 9
  4004.         ; </CPU clock fix>
  4005.  
  4006.         in      al, 0x61
  4007.         and     al, 0x10
  4008.         mov     ah, al
  4009.         cld
  4010.  
  4011. .cnt1:
  4012.         in      al, 0x61
  4013.         and     al, 0x10
  4014.         cmp     al, ah
  4015.         jz      .cnt1
  4016.  
  4017.         mov     ah, al
  4018.         loop    .cnt1
  4019.  
  4020. .done:
  4021.         popad
  4022.         ret
  4023. ;-----------------------------------------------------------------------------
  4024. align 4
  4025. set_app_param:
  4026.         mov     edi, [TASK_BASE]
  4027.         mov     eax, ebx
  4028.         btr     eax, 3                           ; move MOUSE_FILTRATION
  4029.         mov     ebx, [current_slot]              ; bit into event_filter
  4030.         setc    byte [ebx+APPDATA.event_filter]
  4031.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  4032.         mov     [esp+32], eax                    ; return old mask value
  4033.         ret
  4034. ;-----------------------------------------------------------------------------
  4035.  
  4036. ; this is for syscall
  4037. proc delay_hs_unprotected
  4038.         call    unprotect_from_terminate
  4039.         call    delay_hs
  4040.         call    protect_from_terminate
  4041.         ret
  4042. endp
  4043.  
  4044. if 1
  4045. align 4
  4046. delay_hs:     ; delay in 1/100 secs
  4047. ; ebx = delay time
  4048.  
  4049.         pushad
  4050.         push    ebx
  4051.         xor     esi, esi
  4052.         mov     ecx, MANUAL_DESTROY
  4053.         call    create_event
  4054.         test    eax, eax
  4055.         jz      .done
  4056.  
  4057.         mov     ebx, edx
  4058.         mov     ecx, [esp]
  4059.         push    edx
  4060.         push    eax
  4061.         call    wait_event_timeout
  4062.         pop     eax
  4063.         pop     ebx
  4064.         call    destroy_event
  4065. .done:
  4066.         add     esp, 4
  4067.         popad
  4068.         ret
  4069.  
  4070. else
  4071.  
  4072. align 4
  4073. delay_hs:     ; delay in 1/100 secs
  4074. ; ebx = delay time
  4075.         push    ecx
  4076.         push    edx
  4077.  
  4078.         mov     edx, [timer_ticks]
  4079. ;--------------------------------------
  4080. align 4
  4081. newtic:
  4082.         mov     ecx, [timer_ticks]
  4083.         sub     ecx, edx
  4084.         cmp     ecx, ebx
  4085.         jae     zerodelay
  4086.  
  4087.         call    change_task
  4088.  
  4089.         jmp     newtic
  4090. ;--------------------------------------
  4091. align 4
  4092. zerodelay:
  4093.         pop     edx
  4094.         pop     ecx
  4095.         ret
  4096. end if
  4097.  
  4098. ;-----------------------------------------------------------------------------
  4099. align 16        ;very often call this subrutine
  4100. memmove:       ; memory move in bytes
  4101. ; eax = from
  4102. ; ebx = to
  4103. ; ecx = no of bytes
  4104.         test    ecx, ecx
  4105.         jle     .ret
  4106.  
  4107.         push    esi edi ecx
  4108.  
  4109.         mov     edi, ebx
  4110.         mov     esi, eax
  4111.  
  4112.         test    ecx, not 11b
  4113.         jz      @f
  4114.  
  4115.         push    ecx
  4116.         shr     ecx, 2
  4117.         rep movsd
  4118.         pop     ecx
  4119.         and     ecx, 11b
  4120.         jz      .finish
  4121. ;--------------------------------------
  4122. align 4
  4123. @@:
  4124.         rep movsb
  4125. ;--------------------------------------
  4126. align 4
  4127. .finish:
  4128.         pop     ecx edi esi
  4129. ;--------------------------------------
  4130. align 4
  4131. .ret:
  4132.         ret
  4133. ;-----------------------------------------------------------------------------
  4134. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  4135. ;align 4
  4136. ;
  4137. ;read_floppy_file:
  4138. ;
  4139. ;; as input
  4140. ;;
  4141. ;; eax pointer to file
  4142. ;; ebx file lenght
  4143. ;; ecx start 512 byte block number
  4144. ;; edx number of blocks to read
  4145. ;; esi pointer to return/work area (atleast 20 000 bytes)
  4146. ;;
  4147. ;;
  4148. ;; on return
  4149. ;;
  4150. ;; eax = 0 command succesful
  4151. ;;       1 no fd base and/or partition defined
  4152. ;;       2 yet unsupported FS
  4153. ;;       3 unknown FS
  4154. ;;       4 partition not defined at hd
  4155. ;;       5 file not found
  4156. ;; ebx = size of file
  4157. ;
  4158. ;     mov   edi,[TASK_BASE]
  4159. ;     add   edi,0x10
  4160. ;     add   esi,[edi]
  4161. ;     add   eax,[edi]
  4162. ;
  4163. ;     pushad
  4164. ;     mov  edi,esi
  4165. ;     add  edi,1024
  4166. ;     mov  esi,0x100000+19*512
  4167. ;     sub  ecx,1
  4168. ;     shl  ecx,9
  4169. ;     add  esi,ecx
  4170. ;     shl  edx,9
  4171. ;     mov  ecx,edx
  4172. ;     cld
  4173. ;     rep  movsb
  4174. ;     popad
  4175. ;
  4176. ;     mov   [esp+36],eax
  4177. ;     mov   [esp+24],ebx
  4178. ;     ret
  4179.  
  4180.  
  4181.  
  4182. align 4
  4183. set_io_access_rights:
  4184.         push    edi eax
  4185.         mov     edi, tss._io_map_0
  4186. ;     mov   ecx,eax
  4187. ;     and   ecx,7    ; offset in byte
  4188. ;     shr   eax,3    ; number of byte
  4189. ;     add   edi,eax
  4190. ;     mov   ebx,1
  4191. ;     shl   ebx,cl
  4192.         test    ebp, ebp
  4193. ;     cmp   ebp,0                ; enable access - ebp = 0
  4194.         jnz     .siar1
  4195. ;     not   ebx
  4196. ;     and   [edi],byte bl
  4197.         btr     [edi], eax
  4198.         pop     eax edi
  4199.         ret
  4200. .siar1:
  4201.         bts     [edi], eax
  4202.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4203.         pop     eax edi
  4204.         ret
  4205. ;reserve/free group of ports
  4206. ;  * eax = 46 - number function
  4207. ;  * ebx = 0 - reserve, 1 - free
  4208. ;  * ecx = number start arrea of ports
  4209. ;  * edx = number end arrea of ports (include last number of port)
  4210. ;Return value:
  4211. ;  * eax = 0 - succesful
  4212. ;  * eax = 1 - error
  4213. ;  * The system has reserve this ports:
  4214. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  4215. ;destroys eax,ebx, ebp
  4216. r_f_port_area:
  4217.  
  4218.         test    ebx, ebx
  4219.         jnz     free_port_area
  4220. ;     je    r_port_area
  4221. ;     jmp   free_port_area
  4222.  
  4223. ;   r_port_area:
  4224.  
  4225. ;     pushad
  4226.  
  4227.         cmp     ecx, edx      ; beginning > end ?
  4228.         ja      rpal1
  4229.         cmp     edx, 65536
  4230.         jae     rpal1
  4231.         mov     eax, [RESERVED_PORTS]
  4232.         test    eax, eax      ; no reserved areas ?
  4233.         je      rpal2
  4234.         cmp     eax, 255      ; max reserved
  4235.         jae     rpal1
  4236.  rpal3:
  4237.         mov     ebx, eax
  4238.         shl     ebx, 4
  4239.         add     ebx, RESERVED_PORTS
  4240.         cmp     ecx, [ebx+8]
  4241.         ja      rpal4
  4242.         cmp     edx, [ebx+4]
  4243.         jae     rpal1
  4244. ;     jb    rpal4
  4245. ;     jmp   rpal1
  4246.  rpal4:
  4247.         dec     eax
  4248.         jnz     rpal3
  4249.         jmp     rpal2
  4250.    rpal1:
  4251. ;     popad
  4252. ;     mov   eax,1
  4253.         xor     eax, eax
  4254.         inc     eax
  4255.         ret
  4256.    rpal2:
  4257. ;     popad
  4258.      ; enable port access at port IO map
  4259.         cli
  4260.         pushad                        ; start enable io map
  4261.  
  4262.         cmp     edx, 65536;16384
  4263.         jae     no_unmask_io; jge
  4264.         mov     eax, ecx
  4265. ;       push    ebp
  4266.         xor     ebp, ebp               ; enable - eax = port
  4267. new_port_access:
  4268. ;     pushad
  4269.         call    set_io_access_rights
  4270. ;     popad
  4271.         inc     eax
  4272.         cmp     eax, edx
  4273.         jbe     new_port_access
  4274. ;       pop     ebp
  4275. no_unmask_io:
  4276.         popad                         ; end enable io map
  4277.         sti
  4278.  
  4279.         mov     eax, [RESERVED_PORTS]
  4280.         add     eax, 1
  4281.         mov     [RESERVED_PORTS], eax
  4282.         shl     eax, 4
  4283.         add     eax, RESERVED_PORTS
  4284.         mov     ebx, [TASK_BASE]
  4285.         mov     ebx, [ebx+TASKDATA.pid]
  4286.         mov     [eax], ebx
  4287.         mov     [eax+4], ecx
  4288.         mov     [eax+8], edx
  4289.  
  4290.         xor     eax, eax
  4291.         ret
  4292.  
  4293. free_port_area:
  4294.  
  4295. ;     pushad
  4296.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  4297.         test    eax, eax
  4298.         jz      frpal2
  4299.         mov     ebx, [TASK_BASE]
  4300.         mov     ebx, [ebx+TASKDATA.pid]
  4301.    frpal3:
  4302.         mov     edi, eax
  4303.         shl     edi, 4
  4304.         add     edi, RESERVED_PORTS
  4305.         cmp     ebx, [edi]
  4306.         jne     frpal4
  4307.         cmp     ecx, [edi+4]
  4308.         jne     frpal4
  4309.         cmp     edx, [edi+8]
  4310.         jne     frpal4
  4311.         jmp     frpal1
  4312.    frpal4:
  4313.         dec     eax
  4314.         jnz     frpal3
  4315.    frpal2:
  4316. ;     popad
  4317.         inc     eax
  4318.         ret
  4319.    frpal1:
  4320.         push    ecx
  4321.         mov     ecx, 256
  4322.         sub     ecx, eax
  4323.         shl     ecx, 4
  4324.         mov     esi, edi
  4325.         add     esi, 16
  4326.         cld
  4327.         rep movsb
  4328.  
  4329.         dec     dword [RESERVED_PORTS]
  4330. ;popad
  4331. ;disable port access at port IO map
  4332.  
  4333. ;     pushad                        ; start disable io map
  4334.         pop     eax     ;start port
  4335.         cmp     edx, 65536;16384
  4336.         jge     no_mask_io
  4337.  
  4338. ;     mov   eax,ecx
  4339.         xor     ebp, ebp
  4340.         inc     ebp
  4341. new_port_access_disable:
  4342. ;     pushad
  4343. ;     mov   ebp,1                  ; disable - eax = port
  4344.         call    set_io_access_rights
  4345. ;     popad
  4346.         inc     eax
  4347.         cmp     eax, edx
  4348.         jbe     new_port_access_disable
  4349. no_mask_io:
  4350. ;     popad                         ; end disable io map
  4351.         xor     eax, eax
  4352.         ret
  4353. ;-----------------------------------------------------------------------------
  4354. align 4
  4355. drawbackground:
  4356. dbrv20:
  4357.         cmp     [BgrDrawMode], dword 1
  4358.         jne     bgrstr
  4359.         call    vesa20_drawbackground_tiled
  4360. ;        call    [draw_pointer]
  4361.         call    __sys_draw_pointer
  4362.         ret
  4363. ;--------------------------------------
  4364. align 4
  4365. bgrstr:
  4366.         call    vesa20_drawbackground_stretch
  4367. ;        call    [draw_pointer]
  4368.         call    __sys_draw_pointer
  4369.         ret
  4370. ;-----------------------------------------------------------------------------
  4371. align 4
  4372. syscall_putimage:                       ; PutImage
  4373. sys_putimage:
  4374.         test    ecx, 0x80008000
  4375.         jnz     .exit
  4376.         test    ecx, 0x0000FFFF
  4377.         jz      .exit
  4378.         test    ecx, 0xFFFF0000
  4379.         jnz     @f
  4380. ;--------------------------------------
  4381. align 4
  4382. .exit:
  4383.         ret
  4384. ;--------------------------------------
  4385. align 4
  4386. @@:
  4387.         mov     edi, [current_slot]
  4388.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  4389.         rol     edx, 16
  4390.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  4391.         rol     edx, 16
  4392. ;--------------------------------------
  4393. align 4
  4394. .forced:
  4395.         push    ebp esi 0
  4396.         mov     ebp, putimage_get24bpp
  4397.         mov     esi, putimage_init24bpp
  4398. ;--------------------------------------
  4399. align 4
  4400. sys_putimage_bpp:
  4401.         call    vesa20_putimage
  4402.         pop     ebp esi ebp
  4403.         ret
  4404. ;        jmp     [draw_pointer]
  4405. ;-----------------------------------------------------------------------------
  4406. align 4
  4407. sys_putimage_palette:
  4408. ; ebx = pointer to image
  4409. ; ecx = [xsize]*65536 + [ysize]
  4410. ; edx = [xstart]*65536 + [ystart]
  4411. ; esi = number of bits per pixel, must be 8, 24 or 32
  4412. ; edi = pointer to palette
  4413. ; ebp = row delta
  4414.         mov     eax, [CURRENT_TASK]
  4415.         shl     eax, 8
  4416.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4417.         rol     edx, 16
  4418.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4419.         rol     edx, 16
  4420. ;--------------------------------------
  4421. align 4
  4422. .forced:
  4423.         cmp     esi, 1
  4424.         jnz     @f
  4425.         push    edi
  4426.         mov     eax, [edi+4]
  4427.         sub     eax, [edi]
  4428.         push    eax
  4429.         push    dword [edi]
  4430.         push    0ffffff80h
  4431.         mov     edi, esp
  4432.         call    put_mono_image
  4433.         add     esp, 12
  4434.         pop     edi
  4435.         ret
  4436. ;--------------------------------------
  4437. align 4
  4438. @@:
  4439.         cmp     esi, 2
  4440.         jnz     @f
  4441.         push    edi
  4442.         push    0ffffff80h
  4443.         mov     edi, esp
  4444.         call    put_2bit_image
  4445.         pop     eax
  4446.         pop     edi
  4447.         ret
  4448. ;--------------------------------------
  4449. align 4
  4450. @@:
  4451.         cmp     esi, 4
  4452.         jnz     @f
  4453.         push    edi
  4454.         push    0ffffff80h
  4455.         mov     edi, esp
  4456.         call    put_4bit_image
  4457.         pop     eax
  4458.         pop     edi
  4459.         ret
  4460. ;--------------------------------------
  4461. align 4
  4462. @@:
  4463.         push    ebp esi ebp
  4464.         cmp     esi, 8
  4465.         jnz     @f
  4466.         mov     ebp, putimage_get8bpp
  4467.         mov     esi, putimage_init8bpp
  4468.         jmp     sys_putimage_bpp
  4469. ;--------------------------------------
  4470. align 4
  4471. @@:
  4472.         cmp     esi, 9
  4473.         jnz     @f
  4474.         mov     ebp, putimage_get9bpp
  4475.         mov     esi, putimage_init9bpp
  4476.         jmp     sys_putimage_bpp
  4477. ;--------------------------------------
  4478. align 4
  4479. @@:
  4480.         cmp     esi, 15
  4481.         jnz     @f
  4482.         mov     ebp, putimage_get15bpp
  4483.         mov     esi, putimage_init15bpp
  4484.         jmp     sys_putimage_bpp
  4485. ;--------------------------------------
  4486. align 4
  4487. @@:
  4488.         cmp     esi, 16
  4489.         jnz     @f
  4490.         mov     ebp, putimage_get16bpp
  4491.         mov     esi, putimage_init16bpp
  4492.         jmp     sys_putimage_bpp
  4493. ;--------------------------------------
  4494. align 4
  4495. @@:
  4496.         cmp     esi, 24
  4497.         jnz     @f
  4498.         mov     ebp, putimage_get24bpp
  4499.         mov     esi, putimage_init24bpp
  4500.         jmp     sys_putimage_bpp
  4501. ;--------------------------------------
  4502. align 4
  4503. @@:
  4504.         cmp     esi, 32
  4505.         jnz     @f
  4506.         mov     ebp, putimage_get32bpp
  4507.         mov     esi, putimage_init32bpp
  4508.         jmp     sys_putimage_bpp
  4509. ;--------------------------------------
  4510. align 4
  4511. @@:
  4512.         pop     ebp esi ebp
  4513.         ret
  4514. ;-----------------------------------------------------------------------------
  4515. align 4
  4516. put_mono_image:
  4517.         push    ebp esi ebp
  4518.         mov     ebp, putimage_get1bpp
  4519.         mov     esi, putimage_init1bpp
  4520.         jmp     sys_putimage_bpp
  4521. ;-----------------------------------------------------------------------------
  4522. align 4
  4523. put_2bit_image:
  4524.         push    ebp esi ebp
  4525.         mov     ebp, putimage_get2bpp
  4526.         mov     esi, putimage_init2bpp
  4527.         jmp     sys_putimage_bpp
  4528. ;-----------------------------------------------------------------------------
  4529. align 4
  4530. put_4bit_image:
  4531.         push    ebp esi ebp
  4532.         mov     ebp, putimage_get4bpp
  4533.         mov     esi, putimage_init4bpp
  4534.         jmp     sys_putimage_bpp
  4535. ;-----------------------------------------------------------------------------
  4536. align 4
  4537. putimage_init24bpp:
  4538.         lea     eax, [eax*3]
  4539. putimage_init8bpp:
  4540. putimage_init9bpp:
  4541.         ret
  4542. ;-----------------------------------------------------------------------------
  4543. align 16
  4544. putimage_get24bpp:
  4545.         movzx   eax, byte [esi+2]
  4546.         shl     eax, 16
  4547.         mov     ax, [esi]
  4548.         add     esi, 3
  4549.         ret     4
  4550. ;-----------------------------------------------------------------------------
  4551. align 16
  4552. putimage_get8bpp:
  4553.         movzx   eax, byte [esi]
  4554.         push    edx
  4555.         mov     edx, [esp+8]
  4556.         mov     eax, [edx+eax*4]
  4557.         pop     edx
  4558.         inc     esi
  4559.         ret     4
  4560. ;-----------------------------------------------------------------------------
  4561. align 16
  4562. putimage_get9bpp:
  4563.         lodsb
  4564.         mov     ah, al
  4565.         shl     eax, 8
  4566.         mov     al, ah
  4567.         ret     4
  4568. ;-----------------------------------------------------------------------------
  4569. align 4
  4570. putimage_init1bpp:
  4571.         add     eax, ecx
  4572.         push    ecx
  4573.         add     eax, 7
  4574.         add     ecx, 7
  4575.         shr     eax, 3
  4576.         shr     ecx, 3
  4577.         sub     eax, ecx
  4578.         pop     ecx
  4579.         ret
  4580. ;-----------------------------------------------------------------------------
  4581. align 16
  4582. putimage_get1bpp:
  4583.         push    edx
  4584.         mov     edx, [esp+8]
  4585.         mov     al, [edx]
  4586.         add     al, al
  4587.         jnz     @f
  4588.         lodsb
  4589.         adc     al, al
  4590. @@:
  4591.         mov     [edx], al
  4592.         sbb     eax, eax
  4593.         and     eax, [edx+8]
  4594.         add     eax, [edx+4]
  4595.         pop     edx
  4596.         ret     4
  4597. ;-----------------------------------------------------------------------------
  4598. align 4
  4599. putimage_init2bpp:
  4600.         add     eax, ecx
  4601.         push    ecx
  4602.         add     ecx, 3
  4603.         add     eax, 3
  4604.         shr     ecx, 2
  4605.         shr     eax, 2
  4606.         sub     eax, ecx
  4607.         pop     ecx
  4608.         ret
  4609. ;-----------------------------------------------------------------------------
  4610. align 16
  4611. putimage_get2bpp:
  4612.         push    edx
  4613.         mov     edx, [esp+8]
  4614.         mov     al, [edx]
  4615.         mov     ah, al
  4616.         shr     al, 6
  4617.         shl     ah, 2
  4618.         jnz     .nonewbyte
  4619.         lodsb
  4620.         mov     ah, al
  4621.         shr     al, 6
  4622.         shl     ah, 2
  4623.         add     ah, 1
  4624. .nonewbyte:
  4625.         mov     [edx], ah
  4626.         mov     edx, [edx+4]
  4627.         movzx   eax, al
  4628.         mov     eax, [edx+eax*4]
  4629.         pop     edx
  4630.         ret     4
  4631. ;-----------------------------------------------------------------------------
  4632. align 4
  4633. putimage_init4bpp:
  4634.         add     eax, ecx
  4635.         push    ecx
  4636.         add     ecx, 1
  4637.         add     eax, 1
  4638.         shr     ecx, 1
  4639.         shr     eax, 1
  4640.         sub     eax, ecx
  4641.         pop     ecx
  4642.         ret
  4643. ;-----------------------------------------------------------------------------
  4644. align 16
  4645. putimage_get4bpp:
  4646.         push    edx
  4647.         mov     edx, [esp+8]
  4648.         add     byte [edx], 80h
  4649.         jc      @f
  4650.         movzx   eax, byte [edx+1]
  4651.         mov     edx, [edx+4]
  4652.         and     eax, 0x0F
  4653.         mov     eax, [edx+eax*4]
  4654.         pop     edx
  4655.         ret     4
  4656. @@:
  4657.         movzx   eax, byte [esi]
  4658.         add     esi, 1
  4659.         mov     [edx+1], al
  4660.         shr     eax, 4
  4661.         mov     edx, [edx+4]
  4662.         mov     eax, [edx+eax*4]
  4663.         pop     edx
  4664.         ret     4
  4665. ;-----------------------------------------------------------------------------
  4666. align 4
  4667. putimage_init32bpp:
  4668.         shl     eax, 2
  4669.         ret
  4670. ;-----------------------------------------------------------------------------
  4671. align 16
  4672. putimage_get32bpp:
  4673.         lodsd
  4674.         ret     4
  4675. ;-----------------------------------------------------------------------------
  4676. align 4
  4677. putimage_init15bpp:
  4678. putimage_init16bpp:
  4679.         add     eax, eax
  4680.         ret
  4681. ;-----------------------------------------------------------------------------
  4682. align 16
  4683. putimage_get15bpp:
  4684. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4685.         push    ecx edx
  4686.         movzx   eax, word [esi]
  4687.         add     esi, 2
  4688.         mov     ecx, eax
  4689.         mov     edx, eax
  4690.         and     eax, 0x1F
  4691.         and     ecx, 0x1F shl 5
  4692.         and     edx, 0x1F shl 10
  4693.         shl     eax, 3
  4694.         shl     ecx, 6
  4695.         shl     edx, 9
  4696.         or      eax, ecx
  4697.         or      eax, edx
  4698.         pop     edx ecx
  4699.         ret     4
  4700. ;-----------------------------------------------------------------------------
  4701. align 16
  4702. putimage_get16bpp:
  4703. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4704.         push    ecx edx
  4705.         movzx   eax, word [esi]
  4706.         add     esi, 2
  4707.         mov     ecx, eax
  4708.         mov     edx, eax
  4709.         and     eax, 0x1F
  4710.         and     ecx, 0x3F shl 5
  4711.         and     edx, 0x1F shl 11
  4712.         shl     eax, 3
  4713.         shl     ecx, 5
  4714.         shl     edx, 8
  4715.         or      eax, ecx
  4716.         or      eax, edx
  4717.         pop     edx ecx
  4718.         ret     4
  4719. ;-----------------------------------------------------------------------------
  4720. ;align 4
  4721. ; eax x beginning
  4722. ; ebx y beginning
  4723. ; ecx x end
  4724.         ; edx y end
  4725. ; edi color
  4726. ;__sys_drawbar:
  4727. ;        mov     esi, [current_slot]
  4728. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4729. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4730. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4731. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4732. ;--------------------------------------
  4733. ;align 4
  4734. ;.forced:
  4735. ;        call    vesa20_drawbar
  4736. ;        call    [draw_pointer]
  4737. ;        ret
  4738. ;-----------------------------------------------------------------------------
  4739. align 4
  4740. kb_write_wait_ack:
  4741.  
  4742.         push    ecx edx
  4743.  
  4744.         mov     dl, al
  4745.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4746. .wait_output_ready:
  4747.         in      al, 0x64
  4748.         test    al, 2
  4749.         jz      @f
  4750.         loop    .wait_output_ready
  4751.         mov     ah, 1
  4752.         jmp     .nothing
  4753. @@:
  4754.         mov     al, dl
  4755.         out     0x60, al
  4756.         mov     ecx, 0xfffff; last 0xffff, new value in view of fast CPU's
  4757. .wait_ack:
  4758.         in      al, 0x64
  4759.         test    al, 1
  4760.         jnz     @f
  4761.         loop    .wait_ack
  4762.         mov     ah, 1
  4763.         jmp     .nothing
  4764. @@:
  4765.         in      al, 0x60
  4766.         xor     ah, ah
  4767.  
  4768. .nothing:
  4769.         pop     edx ecx
  4770.  
  4771.         ret
  4772. ;-----------------------------------------------------------------------------
  4773.  
  4774. setmouse:  ; set mousepicture -pointer
  4775.            ; ps2 mouse enable
  4776.  
  4777. ;        mov     [MOUSE_PICTURE], dword mousepointer
  4778.  
  4779.         cli
  4780.  
  4781.         ret
  4782.  
  4783. if used _rdtsc
  4784. _rdtsc:
  4785.         bt      [cpu_caps], CAPS_TSC
  4786.         jnc     ret_rdtsc
  4787.         rdtsc
  4788.         ret
  4789.    ret_rdtsc:
  4790.         mov     edx, 0xffffffff
  4791.         mov     eax, 0xffffffff
  4792.         ret
  4793. end if
  4794.  
  4795. sys_msg_board_str:
  4796.  
  4797.         pushad
  4798.    @@:
  4799.         cmp     [esi], byte 0
  4800.         je      @f
  4801.         mov     ebx, 1
  4802.         movzx   ecx, byte [esi]
  4803.         call    sys_msg_board
  4804.         inc     esi
  4805.         jmp     @b
  4806.    @@:
  4807.         popad
  4808.         ret
  4809.  
  4810. sys_msg_board_byte:
  4811. ; in: al = byte to display
  4812. ; out: nothing
  4813. ; destroys: nothing
  4814.         pushad
  4815.         mov     ecx, 2
  4816.         shl     eax, 24
  4817.         jmp     @f
  4818.  
  4819. sys_msg_board_word:
  4820. ; in: ax = word to display
  4821. ; out: nothing
  4822. ; destroys: nothing
  4823.         pushad
  4824.         mov     ecx, 4
  4825.         shl     eax, 16
  4826.         jmp     @f
  4827.  
  4828. sys_msg_board_dword:
  4829. ; in: eax = dword to display
  4830. ; out: nothing
  4831. ; destroys: nothing
  4832.         pushad
  4833.         mov     ecx, 8
  4834. @@:
  4835.         push    ecx
  4836.         rol     eax, 4
  4837.         push    eax
  4838.         and     al, 0xF
  4839.         cmp     al, 10
  4840.         sbb     al, 69h
  4841.         das
  4842.         mov     cl, al
  4843.         xor     ebx, ebx
  4844.         inc     ebx
  4845.         call    sys_msg_board
  4846.         pop     eax
  4847.         pop     ecx
  4848.         loop    @b
  4849.         popad
  4850.         ret
  4851.  
  4852. msg_board_data_size = 65536 ; Must be power of two
  4853.  
  4854. uglobal
  4855. msg_board_data  rb  msg_board_data_size
  4856. msg_board_count dd  ?
  4857. endg
  4858.  
  4859. iglobal
  4860. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4861. endg
  4862.  
  4863. sys_msg_board:
  4864. ; ebx=1 -> write, cl = byte to write
  4865. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4866.         push    eax ebx
  4867.         mov     eax, ebx
  4868.         mov     ebx, ecx
  4869.         mov     ecx, [msg_board_count]
  4870.         cmp     eax, 1
  4871.         jne     .read
  4872.  
  4873. if defined debug_com_base
  4874.         push    dx ax
  4875. @@: ; wait for empty transmit register
  4876.         mov     dx, debug_com_base+5
  4877.         in      al, dx
  4878.         test    al, 1 shl 5
  4879.         jz      @r
  4880.         mov     dx, debug_com_base      ; Output the byte
  4881.         mov     al, bl
  4882.         out     dx, al
  4883.         pop     ax dx
  4884. end if
  4885.  
  4886.         mov     [msg_board_data+ecx], bl
  4887.         cmp     byte [debug_direct_print], 1
  4888.         jnz     .end
  4889.         pusha
  4890.         lea     edx, [msg_board_data+ecx]
  4891.         mov     ecx, 0x40FFFFFF
  4892.         mov     ebx, [msg_board_pos]
  4893.         mov     edi, 1
  4894.         mov     esi, 1
  4895.         call    dtext
  4896.         popa
  4897.         add     word [msg_board_pos+2], 6
  4898.         cmp     word [msg_board_pos+2], 105*6
  4899.         jnc     @f
  4900.         cmp     bl, 10
  4901.         jnz     .end
  4902. @@:
  4903.         mov     word [msg_board_pos+2], 42*6
  4904.         add     word [msg_board_pos], 10
  4905.         mov     eax, [_display.height]
  4906.         sub     eax, 10
  4907.         cmp     ax, word [msg_board_pos]
  4908.         jnc     @f
  4909.         mov     word [msg_board_pos], 10
  4910. @@:
  4911.         pusha
  4912.         mov     eax, [msg_board_pos]
  4913.         movzx   ebx, ax
  4914.         shr     eax, 16
  4915.         mov     edx, 105*6
  4916.         xor     ecx, ecx
  4917.         mov     edi, 1
  4918.         mov     esi, 9
  4919. @@:
  4920.         call    hline
  4921.         inc     ebx
  4922.         dec     esi
  4923.         jnz     @b
  4924.         popa
  4925. .end:
  4926.         inc     ecx
  4927.         and     ecx, msg_board_data_size - 1
  4928.         mov     [msg_board_count], ecx
  4929. .ret:
  4930.         pop     ebx eax
  4931.         ret
  4932.  
  4933. @@:
  4934.         mov     [esp+32], ecx
  4935.         mov     [esp+20], ecx
  4936.         jmp     .ret
  4937.  
  4938. .read:
  4939.         cmp     eax, 2
  4940.         jne     .ret
  4941.         test    ecx, ecx
  4942.         jz      @b
  4943.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4944.         mov     eax, msg_board_data+1
  4945.         mov     ebx, msg_board_data
  4946.         movzx   edx, byte [ebx]
  4947.         call    memmove
  4948.         dec     [msg_board_count]
  4949.         mov     [esp + 32], edx ;eax
  4950.         mov     [esp + 20], dword 1
  4951.         ret
  4952.  
  4953. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4954. ;; 66 sys function.                                                ;;
  4955. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4956. ;; out eax                                                         ;;
  4957. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4958. iglobal
  4959. align 4
  4960. f66call:
  4961.            dd sys_process_def.1   ; 1 = set keyboard mode
  4962.            dd sys_process_def.2   ; 2 = get keyboard mode
  4963.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4964.            dd sys_process_def.4   ; 4 = set system-wide hotkey
  4965.            dd sys_process_def.5   ; 5 = delete installed hotkey
  4966.            dd sys_process_def.6   ; 6 = disable input, work only hotkeys
  4967.            dd sys_process_def.7   ; 7 = enable input, opposition to f.66.6
  4968. endg
  4969. ;-----------------------------------------------------------------------------
  4970. align 4
  4971. sys_process_def:
  4972.         dec     ebx
  4973.         cmp     ebx, 7
  4974.         jae     .not_support    ;if >=8 then or eax,-1
  4975.  
  4976.         mov     edi, [CURRENT_TASK]
  4977.         jmp     dword [f66call+ebx*4]
  4978.  
  4979. .not_support:
  4980.         or      eax, -1
  4981.         ret
  4982. ;-----------------------------------------------------------------------------
  4983. align 4
  4984. .1:
  4985.         shl     edi, 8
  4986.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4987.  
  4988.         ret
  4989. ;-----------------------------------------------------------------------------
  4990. align 4
  4991. .2:                             ; 2 = get keyboard mode
  4992.         shl     edi, 8
  4993.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4994.         mov     [esp+32], eax
  4995.         ret
  4996. ;-----------------------------------------------------------------------------
  4997. align 4
  4998. .3:                             ;3 = get keyboard ctrl, alt, shift
  4999.         mov     eax, [kb_state]
  5000.         mov     [esp+32], eax
  5001.         ret
  5002. ;-----------------------------------------------------------------------------
  5003. align 4
  5004. .4:
  5005.         mov     eax, hotkey_list
  5006. @@:
  5007.         cmp     dword [eax+8], 0
  5008.         jz      .found_free
  5009.         add     eax, 16
  5010.         cmp     eax, hotkey_list+16*256
  5011.         jb      @b
  5012.         mov     dword [esp+32], 1
  5013.         ret
  5014. .found_free:
  5015.         mov     [eax+8], edi
  5016.         mov     [eax+4], edx
  5017.         movzx   ecx, cl
  5018.         lea     ecx, [hotkey_scancodes+ecx*4]
  5019.         mov     edx, [ecx]
  5020.         mov     [eax], edx
  5021.         mov     [ecx], eax
  5022.         mov     [eax+12], ecx
  5023.         test    edx, edx
  5024.         jz      @f
  5025.         mov     [edx+12], eax
  5026. @@:
  5027.         and     dword [esp+32], 0
  5028.         ret
  5029. ;-----------------------------------------------------------------------------
  5030. align 4
  5031. .5:
  5032.         movzx   ebx, cl
  5033.         lea     ebx, [hotkey_scancodes+ebx*4]
  5034.         mov     eax, [ebx]
  5035. .scan:
  5036.         test    eax, eax
  5037.         jz      .notfound
  5038.         cmp     [eax+8], edi
  5039.         jnz     .next
  5040.         cmp     [eax+4], edx
  5041.         jz      .found
  5042. .next:
  5043.         mov     eax, [eax]
  5044.         jmp     .scan
  5045. .notfound:
  5046.         mov     dword [esp+32], 1
  5047.         ret
  5048. .found:
  5049.         mov     ecx, [eax]
  5050.         jecxz   @f
  5051.         mov     edx, [eax+12]
  5052.         mov     [ecx+12], edx
  5053. @@:
  5054.         mov     ecx, [eax+12]
  5055.         mov     edx, [eax]
  5056.         mov     [ecx], edx
  5057.         xor     edx, edx
  5058.         mov     [eax+4], edx
  5059.         mov     [eax+8], edx
  5060.         mov     [eax+12], edx
  5061.         mov     [eax], edx
  5062.         mov     [esp+32], edx
  5063.         ret
  5064. ;-----------------------------------------------------------------------------
  5065. align 4
  5066. .6:
  5067.         pushfd
  5068.         cli
  5069.         mov     eax, [PID_lock_input]
  5070.         test    eax, eax
  5071.         jnz     @f
  5072. ; get current PID
  5073.         mov     eax, [CURRENT_TASK]
  5074.         shl     eax, 5
  5075.         mov     eax, [eax+CURRENT_TASK+TASKDATA.pid]
  5076. ; set current PID for lock input
  5077.         mov     [PID_lock_input], eax
  5078. @@:
  5079.         popfd
  5080.         ret
  5081. ;-----------------------------------------------------------------------------
  5082. align 4
  5083. .7:
  5084.         mov     eax, [PID_lock_input]
  5085.         test    eax, eax
  5086.         jz      @f
  5087. ; get current PID
  5088.         mov     ebx, [CURRENT_TASK]
  5089.         shl     ebx, 5
  5090.         mov     ebx, [ebx+CURRENT_TASK+TASKDATA.pid]
  5091. ; compare current lock input with current PID
  5092.         cmp     ebx, eax
  5093.         jne     @f
  5094.  
  5095.         xor     eax, eax
  5096.         mov     [PID_lock_input], eax
  5097. @@:
  5098.         ret
  5099. ;-----------------------------------------------------------------------------
  5100. uglobal
  5101.   PID_lock_input dd 0x0
  5102. endg
  5103. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5104. ;; 61 sys function.                                                ;;
  5105. ;; in eax=61,ebx in [1..3]                                         ;;
  5106. ;; out eax                                                         ;;
  5107. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5108. iglobal
  5109. align 4
  5110. f61call:
  5111.            dd sys_gs.1   ; resolution
  5112.            dd sys_gs.2   ; bits per pixel
  5113.            dd sys_gs.3   ; bytes per scanline
  5114. endg
  5115.  
  5116.  
  5117. align 4
  5118.  
  5119. sys_gs:                         ; direct screen access
  5120.         dec     ebx
  5121.         cmp     ebx, 2
  5122.         ja      .not_support
  5123.         jmp     dword [f61call+ebx*4]
  5124. .not_support:
  5125.         or      [esp+32], dword -1
  5126.         ret
  5127.  
  5128.  
  5129. .1:                             ; resolution
  5130.         mov     eax, [_display.width]
  5131.         shl     eax, 16
  5132.         mov     ax, word [_display.height]
  5133.         mov     [esp+32], eax
  5134.         ret
  5135. .2:                             ; bits per pixel
  5136.         mov     eax, [_display.bits_per_pixel]
  5137.         mov     [esp+32], eax
  5138.         ret
  5139. .3:                             ; bytes per scanline
  5140.         mov     eax, [_display.lfb_pitch]
  5141.         mov     [esp+32], eax
  5142.         ret
  5143.  
  5144. align 4  ;  system functions
  5145.  
  5146. syscall_setpixel:                       ; SetPixel
  5147.  
  5148.         mov     eax, ebx
  5149.         mov     ebx, ecx
  5150.         mov     ecx, edx
  5151.         mov     edx, [TASK_BASE]
  5152.         add     eax, [edx-twdw+WDATA.box.left]
  5153.         add     ebx, [edx-twdw+WDATA.box.top]
  5154.         mov     edi, [current_slot]
  5155.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  5156.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  5157.         xor     edi, edi ; no force
  5158.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
  5159. ;        jmp     [putpixel]
  5160.         jmp     __sys_putpixel
  5161.  
  5162. align 4
  5163.  
  5164. syscall_writetext:                      ; WriteText
  5165.  
  5166.         mov     eax, [TASK_BASE]
  5167.         mov     ebp, [eax-twdw+WDATA.box.left]
  5168.         push    esi
  5169.         mov     esi, [current_slot]
  5170.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5171.         shl     ebp, 16
  5172.         add     ebp, [eax-twdw+WDATA.box.top]
  5173.         add     bp, word[esi+APPDATA.wnd_clientbox.top]
  5174.         pop     esi
  5175.         test    ecx, 0x08000000  ; redirect the output to the user area
  5176.         jnz     @f
  5177.         add     ebx, ebp
  5178. align 4
  5179. @@:
  5180.         mov     eax, edi
  5181.         test    ecx, 0x08000000  ; redirect the output to the user area
  5182.         jnz     dtext
  5183.         xor     edi, edi
  5184.         jmp     dtext
  5185.  
  5186. align 4
  5187.  
  5188. syscall_drawrect:                       ; DrawRect
  5189.  
  5190.         mov     edi, edx ; color + gradient
  5191.         and     edi, 0x80FFFFFF
  5192.         test    bx, bx  ; x.size
  5193.         je      .drectr
  5194.         test    cx, cx ; y.size
  5195.         je      .drectr
  5196.  
  5197.         mov     eax, ebx ; bad idea
  5198.         mov     ebx, ecx
  5199.  
  5200.         movzx   ecx, ax ; ecx - x.size
  5201.         shr     eax, 16 ; eax - x.coord
  5202.         movzx   edx, bx ; edx - y.size
  5203.         shr     ebx, 16 ; ebx - y.coord
  5204.         mov     esi, [current_slot]
  5205.  
  5206.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  5207.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  5208.         add     ecx, eax
  5209.         add     edx, ebx
  5210. ;        jmp     [drawbar]
  5211.         jmp     vesa20_drawbar
  5212. .drectr:
  5213.         ret
  5214.  
  5215. align 4
  5216. syscall_getscreensize:                  ; GetScreenSize
  5217.         mov     ax, word [_display.width]
  5218.         dec     ax
  5219.         shl     eax, 16
  5220.         mov     ax, word [_display.height]
  5221.         dec     ax
  5222.         mov     [esp + 32], eax
  5223.         ret
  5224. ;-----------------------------------------------------------------------------
  5225. align 4
  5226. syscall_cdaudio:
  5227. ; ECX - position of CD/DVD-drive
  5228. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  5229. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  5230. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  5231.         cmp     ecx, 11
  5232.         ja      .exit
  5233.  
  5234.         mov     eax, ecx
  5235.         shr     eax, 2
  5236.         lea     eax, [eax*5]
  5237.         mov     al, [eax+DRIVE_DATA+1]
  5238.  
  5239.         push    ecx ebx
  5240.         mov     ebx, ecx
  5241.         and     ebx, 11b
  5242.         shl     ebx, 1
  5243.         mov     cl, 6
  5244.         sub     cl, bl
  5245.         shr     al, cl
  5246.         test    al, 2 ; it's not an ATAPI device
  5247.         pop     ebx ecx
  5248.  
  5249.         jz      .exit
  5250.  
  5251.         cmp     ebx, 4
  5252.         je      .eject
  5253.  
  5254.         cmp     ebx, 5
  5255.         je      .load
  5256. ;--------------------------------------
  5257. .exit:
  5258.         ret
  5259. ;--------------------------------------
  5260. .load:
  5261.         call    .reserve
  5262.         call    LoadMedium
  5263.         jmp     .free
  5264. ;--------------------------------------
  5265. .eject:
  5266.         call    .reserve
  5267.         call    clear_CD_cache
  5268.         call    allow_medium_removal
  5269.         call    EjectMedium
  5270.         jmp     .free
  5271. ;--------------------------------------
  5272. .reserve:
  5273.         call    reserve_cd
  5274.  
  5275.         mov     ebx, ecx
  5276.         inc     ebx
  5277.         mov     [cdpos], ebx
  5278.  
  5279.         mov     eax, ecx
  5280.         shr     eax, 1
  5281.         and     eax, 1
  5282.         inc     eax
  5283.         mov     [ChannelNumber], al
  5284.         mov     eax, ecx
  5285.         and     eax, 1
  5286.         mov     [DiskNumber], al
  5287.         call    reserve_cd_channel
  5288.         ret
  5289. ;--------------------------------------
  5290. .free:
  5291.         call    free_cd_channel
  5292.         and     [cd_status], 0
  5293.         ret
  5294. ;-----------------------------------------------------------------------------
  5295. align 4
  5296. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  5297.         cmp     ebx, [_display.width]
  5298.         jb      @f
  5299.         cmp     ecx, [_display.height]
  5300.         jb      @f
  5301.         xor     eax, eax
  5302.         jmp     .store
  5303. ;--------------------------------------
  5304. align 4
  5305. @@:
  5306.         mov     eax, [d_width_calc_area + ecx*4]
  5307.         add     eax, [_display.win_map]
  5308.         movzx   eax, byte[eax+ebx]        ; get value for current point
  5309. ;--------------------------------------
  5310. align 4
  5311. .store:
  5312.         mov     [esp + 32], eax
  5313.         ret
  5314. ;-----------------------------------------------------------------------------
  5315. align 4
  5316. syscall_getpixel:                       ; GetPixel
  5317.         mov     ecx, [_display.width]
  5318.         xor     edx, edx
  5319.         mov     eax, ebx
  5320.         div     ecx
  5321.         mov     ebx, edx
  5322.         xchg    eax, ebx
  5323.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5324.         call    dword [GETPIXEL]; eax - x, ebx - y
  5325.         mov     [esp + 32], ecx
  5326.         ret
  5327. ;-----------------------------------------------------------------------------
  5328. align 4
  5329. syscall_getarea:
  5330. ;eax = 36
  5331. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5332. ;ecx = [size x]*65536 + [size y]
  5333. ;edx = [start x]*65536 + [start y]
  5334.         pushad
  5335.         mov     edi, ebx
  5336.         mov     eax, edx
  5337.         shr     eax, 16
  5338.         mov     ebx, edx
  5339.         and     ebx, 0xffff
  5340.         dec     eax
  5341.         dec     ebx
  5342.      ; eax - x, ebx - y
  5343.         mov     edx, ecx
  5344.  
  5345.         shr     ecx, 16
  5346.         and     edx, 0xffff
  5347.         mov     esi, ecx
  5348.      ; ecx - size x, edx - size y
  5349.  
  5350.         mov     ebp, edx
  5351.         dec     ebp
  5352.         lea     ebp, [ebp*3]
  5353.  
  5354.         imul    ebp, esi
  5355.  
  5356.         mov     esi, ecx
  5357.         dec     esi
  5358.         lea     esi, [esi*3]
  5359.  
  5360.         add     ebp, esi
  5361.         add     ebp, edi
  5362.  
  5363.         add     ebx, edx
  5364. ;--------------------------------------
  5365. align 4
  5366. .start_y:
  5367.         push    ecx edx
  5368. ;--------------------------------------
  5369. align 4
  5370. .start_x:
  5371.         push    eax ebx ecx
  5372.         add     eax, ecx
  5373.  
  5374.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5375.         call    dword [GETPIXEL]; eax - x, ebx - y
  5376.  
  5377.         mov     [ebp], cx
  5378.         shr     ecx, 16
  5379.         mov     [ebp+2], cl
  5380.  
  5381.         pop     ecx ebx eax
  5382.         sub     ebp, 3
  5383.         dec     ecx
  5384.         jnz     .start_x
  5385.         pop     edx ecx
  5386.         dec     ebx
  5387.         dec     edx
  5388.         jnz     .start_y
  5389.         popad
  5390.         ret
  5391. ;-----------------------------------------------------------------------------
  5392. align 4
  5393. syscall_putarea_backgr:
  5394. ;eax = 25
  5395. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5396. ;ecx = [size x]*65536 + [size y]
  5397. ;edx = [start x]*65536 + [start y]
  5398.         pushad
  5399.         mov     edi, ebx
  5400.         mov     eax, edx
  5401.         shr     eax, 16
  5402.         mov     ebx, edx
  5403.         and     ebx, 0xffff
  5404.         dec     eax
  5405.         dec     ebx
  5406. ; eax - x, ebx - y
  5407.         mov     edx, ecx
  5408.         shr     ecx, 16
  5409.         and     edx, 0xffff
  5410.         mov     esi, ecx
  5411. ; ecx - size x, edx - size y
  5412.         mov     ebp, edx
  5413.         dec     ebp
  5414.         shl     ebp, 2
  5415.  
  5416.         imul    ebp, esi
  5417.  
  5418.         mov     esi, ecx
  5419.         dec     esi
  5420.         shl     esi, 2
  5421.  
  5422.         add     ebp, esi
  5423.         add     ebp, edi
  5424.  
  5425.         add     ebx, edx
  5426. ;--------------------------------------
  5427. align 4
  5428. .start_y:
  5429.         push    ecx edx
  5430. ;--------------------------------------
  5431. align 4
  5432. .start_x:
  5433.         push    eax ecx
  5434.         add     eax, ecx
  5435.  
  5436.         mov     ecx, [ebp]
  5437.         rol     ecx, 8
  5438.         test    cl, cl        ; transparensy = 0
  5439.         jz      .no_put
  5440.  
  5441.         xor     cl, cl
  5442.         ror     ecx, 8
  5443.  
  5444.         pushad
  5445.         mov     edx, [d_width_calc_area + ebx*4]
  5446.         add     edx, [_display.win_map]
  5447.         movzx   edx, byte [eax+edx]
  5448.         cmp     dl, byte 1
  5449.         jne     @f
  5450.  
  5451.         call    dword [PUTPIXEL]; eax - x, ebx - y
  5452. ;--------------------------------------
  5453. align 4
  5454. @@:
  5455.         popad
  5456. ;--------------------------------------
  5457. align 4
  5458. .no_put:
  5459.         pop     ecx eax
  5460.  
  5461.         sub     ebp, 4
  5462.         dec     ecx
  5463.         jnz     .start_x
  5464.  
  5465.         pop     edx ecx
  5466.         dec     ebx
  5467.         dec     edx
  5468.         jnz     .start_y
  5469.  
  5470.         popad
  5471.         ret
  5472. ;-----------------------------------------------------------------------------
  5473. align 4
  5474. syscall_drawline:                       ; DrawLine
  5475.  
  5476.         mov     edi, [TASK_BASE]
  5477.         movzx   eax, word[edi-twdw+WDATA.box.left]
  5478.         mov     ebp, eax
  5479.         mov     esi, [current_slot]
  5480.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5481.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  5482.         add     ebp, ebx
  5483.         shl     eax, 16
  5484.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  5485.         add     eax, ebp
  5486.         mov     ebp, ebx
  5487.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  5488.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  5489.         add     ebp, ecx
  5490.         shl     ebx, 16
  5491.         xor     edi, edi
  5492.         add     ebx, ebp
  5493.         mov     ecx, edx
  5494. ;        jmp     [draw_line]
  5495.         jmp     __sys_draw_line
  5496.  
  5497.  
  5498. align 4
  5499. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5500.  
  5501.         call    r_f_port_area
  5502.         mov     [esp+32], eax
  5503.         ret
  5504.  
  5505. align 4
  5506. syscall_threads:                        ; CreateThreads
  5507. ;
  5508. ;   ecx=thread entry point
  5509. ;   edx=thread stack pointer
  5510. ;
  5511. ; on return : eax = pid
  5512.  
  5513.         xor     ebx, ebx
  5514.         call    new_sys_threads
  5515.  
  5516.         mov     [esp+32], eax
  5517.         ret
  5518.  
  5519. align 4
  5520.  
  5521. paleholder:
  5522.         ret
  5523. ;------------------------------------------------------------------------------
  5524. align 4
  5525. calculate_fast_getting_offset_for_WinMapAddress:
  5526. ; calculate data area for fast getting offset to _WinMapAddress
  5527.         xor     eax, eax
  5528.         mov     ecx, [_display.height]
  5529.         mov     edi, d_width_calc_area
  5530.         cld
  5531. @@:
  5532.         stosd
  5533.         add     eax, [_display.width]
  5534.         dec     ecx
  5535.         jnz     @r
  5536.         ret
  5537. ;------------------------------------------------------------------------------
  5538. align 4
  5539. calculate_fast_getting_offset_for_LFB:
  5540. ; calculate data area for fast getting offset to LFB
  5541.         xor     eax, eax
  5542.         mov     ecx, [_display.height]
  5543.         mov     edi, BPSLine_calc_area
  5544.         cld
  5545. @@:
  5546.         stosd
  5547.         add     eax, [_display.lfb_pitch]
  5548.         dec     ecx
  5549.         jnz     @r
  5550.         ret
  5551. ;------------------------------------------------------------------------------
  5552. align 4
  5553. set_screen:
  5554. ; in:
  5555. ; eax - new Screen_Max_X
  5556. ; ecx - new BytesPerScanLine
  5557. ; edx - new Screen_Max_Y
  5558.  
  5559.         pushfd
  5560.         cli
  5561.  
  5562.         mov     [_display.lfb_pitch], ecx
  5563.  
  5564.         mov     [screen_workarea.right], eax
  5565.         mov     [screen_workarea.bottom], edx
  5566.  
  5567.         push    ebx
  5568.         push    esi
  5569.         push    edi
  5570.  
  5571.         pushad
  5572.  
  5573.         cmp     [do_not_touch_winmap], 1
  5574.         je      @f
  5575.  
  5576.         stdcall kernel_free, [_display.win_map]
  5577.  
  5578.         mov     eax, [_display.width]
  5579.         mul     [_display.height]
  5580.         mov     [_display.win_map_size], eax
  5581.  
  5582.         stdcall kernel_alloc, eax
  5583.         mov     [_display.win_map], eax
  5584.         test    eax, eax
  5585.         jz      .epic_fail
  5586. ; store for f.18.24
  5587.         mov     eax, [_display.width]
  5588.         mov     [display_width_standard], eax
  5589.  
  5590.         mov     eax, [_display.height]
  5591.         mov     [display_height_standard], eax
  5592. @@:
  5593.         call    calculate_fast_getting_offset_for_WinMapAddress
  5594. ; for Qemu or non standart video cards
  5595. ; Unfortunately [BytesPerScanLine] does not always
  5596. ;                             equal to [_display.width] * [ScreenBPP] / 8
  5597.         call    calculate_fast_getting_offset_for_LFB
  5598.         popad
  5599.  
  5600.         call    repos_windows
  5601.         xor     eax, eax
  5602.         xor     ebx, ebx
  5603.         mov     ecx, [_display.width]
  5604.         mov     edx, [_display.height]
  5605.         dec     ecx
  5606.         dec     edx
  5607.         call    calculatescreen
  5608.         pop     edi
  5609.         pop     esi
  5610.         pop     ebx
  5611.  
  5612.         popfd
  5613.         ret
  5614.  
  5615. .epic_fail:
  5616.         hlt                     ; Houston, we've had a problem
  5617.  
  5618. ; --------------- APM ---------------------
  5619. uglobal
  5620. apm_entry       dp      0
  5621. apm_vf          dd      0
  5622. endg
  5623.  
  5624. align 4
  5625. sys_apm:
  5626.         xor     eax, eax
  5627.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  5628.         jne     @f
  5629.         inc     eax
  5630.         or      dword [esp + 44], eax   ; error
  5631.         add     eax, 7
  5632.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  5633.         ret
  5634.  
  5635. @@:
  5636. ;       xchg    eax, ecx
  5637. ;       xchg    ebx, ecx
  5638.  
  5639.         cmp     dx, 3
  5640.         ja      @f
  5641.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  5642.         mov     eax, [apm_vf]
  5643.         mov     [esp + 32], eax
  5644.         shr     eax, 16
  5645.         mov     [esp + 28], eax
  5646.         ret
  5647.  
  5648. @@:
  5649.  
  5650.         mov     esi, [master_tab+(OS_BASE shr 20)]
  5651.         xchg    [master_tab], esi
  5652.         push    esi
  5653.         mov     edi, cr3
  5654.         mov     cr3, edi                ;flush TLB
  5655.  
  5656.         call    pword [apm_entry]       ;call APM BIOS
  5657.  
  5658.         xchg    eax, [esp]
  5659.         mov     [master_tab], eax
  5660.         mov     eax, cr3
  5661.         mov     cr3, eax
  5662.         pop     eax
  5663.  
  5664.         mov     [esp + 4 ], edi
  5665.         mov     [esp + 8], esi
  5666.         mov     [esp + 20], ebx
  5667.         mov     [esp + 24], edx
  5668.         mov     [esp + 28], ecx
  5669.         mov     [esp + 32], eax
  5670.         setc    al
  5671.         and     [esp + 44], byte 0xfe
  5672.         or      [esp + 44], al
  5673.         ret
  5674. ; -----------------------------------------
  5675.  
  5676. align 4
  5677. undefined_syscall:                      ; Undefined system call
  5678.         mov     [esp + 32], dword -1
  5679.         ret
  5680.  
  5681. ; check if given memory region lays in lower 2gb (userspace memory) or not
  5682. align 4
  5683. proc is_region_userspace stdcall, base:dword, len:dword
  5684. ; in:
  5685. ;      base = base address of region
  5686. ;      len = lenght of region
  5687. ; out: ZF = 1 if region in userspace memory
  5688. ;      ZF = 0 otherwise
  5689.         push    eax ebx
  5690.         mov     eax, [base]
  5691.  
  5692.         cmp     eax, OS_BASE
  5693.         ja      @f
  5694.  
  5695.         add     eax, [len]
  5696.         cmp     eax, OS_BASE
  5697.         ja      @f
  5698.  
  5699.         mov     eax, 1
  5700.         jmp     .ret
  5701. @@:
  5702.         xor     eax, eax
  5703. .ret:
  5704.         test    eax, eax
  5705.         pop     ebx eax
  5706.         ret
  5707. endp
  5708.  
  5709. if ~ lang eq sp
  5710. diff16 "end of .text segment",0,$
  5711. end if
  5712.  
  5713. include "data32.inc"
  5714.  
  5715. __REV__ = __REV
  5716.  
  5717. if ~ lang eq sp
  5718. diff16 "end of kernel code",0,$
  5719. end if
  5720.