Subversion Repositories Kolibri OS

Rev

Rev 8675 | Rev 8714 | 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: 8713 $
  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. ; add check pointers
  1413.         test    bl, bl
  1414.         jz      @f
  1415.         stdcall is_region_userspace, ecx, 1
  1416.         jnz     @f
  1417.         ret
  1418. @@:
  1419.         test    esi, 0x08000000
  1420.         jz      @f
  1421.         stdcall is_region_userspace, edi, 1
  1422.         jnz     @f
  1423.         ret
  1424. @@:
  1425. ;It is not optimization
  1426.         mov     eax, ebx
  1427.         mov     ebx, ecx
  1428.         mov     ecx, edx
  1429.         mov     edx, esi
  1430.         mov     esi, edi
  1431. ; eax = print type, al=0 -> ebx is number
  1432. ;                   al=1 -> ebx is pointer
  1433. ;                   ah=0 -> display decimal
  1434. ;                   ah=1 -> display hexadecimal
  1435. ;                   ah=2 -> display binary
  1436. ;                   eax bits 16-21 = number of digits to display (0-32)
  1437. ;                   eax bits 22-31 = reserved
  1438. ;
  1439. ; ebx = number or pointer
  1440. ; ecx = x shl 16 + y
  1441. ; edx = color
  1442.         xor     edi, edi
  1443. display_number_force:
  1444.         push    eax
  1445.         and     eax, 0x3fffffff
  1446.         cmp     eax, 0xffff     ; length > 0 ?
  1447.         pop     eax
  1448.         jge     cont_displ
  1449.         ret
  1450.    cont_displ:
  1451.         push    eax
  1452.         and     eax, 0x3fffffff
  1453.         cmp     eax, 61*0x10000  ; length <= 60 ?
  1454.         pop     eax
  1455.         jb      cont_displ2
  1456.         ret
  1457.    cont_displ2:
  1458.  
  1459.         pushad
  1460.  
  1461.         cmp     al, 1            ; ecx is a pointer ?
  1462.         jne     displnl1
  1463.         mov     ebp, ebx
  1464.         add     ebp, 4
  1465.         mov     ebp, [ebp+std_application_base_address]
  1466.         mov     ebx, [ebx+std_application_base_address]
  1467.  displnl1:
  1468.         sub     esp, 64
  1469.  
  1470.         test    ah, ah            ; DECIMAL
  1471.         jnz     no_display_desnum
  1472.         shr     eax, 16
  1473.         and     eax, 0xC03f
  1474. ;     and   eax,0x3f
  1475.         push    eax
  1476.         and     eax, 0x3f
  1477.         mov     edi, esp
  1478.         add     edi, 4+64-1
  1479.         mov     ecx, eax
  1480.         mov     eax, ebx
  1481.         mov     ebx, 10
  1482.  d_desnum:
  1483.         xor     edx, edx
  1484.         call    division_64_bits
  1485.         div     ebx
  1486.         add     dl, 48
  1487.         mov     [edi], dl
  1488.         dec     edi
  1489.         loop    d_desnum
  1490.         pop     eax
  1491.         call    normalize_number
  1492.         call    draw_num_text
  1493.         add     esp, 64
  1494.         popad
  1495.         ret
  1496.    no_display_desnum:
  1497.  
  1498.         cmp     ah, 0x01         ; HEXADECIMAL
  1499.         jne     no_display_hexnum
  1500.         shr     eax, 16
  1501.         and     eax, 0xC03f
  1502. ;     and   eax,0x3f
  1503.         push    eax
  1504.         and     eax, 0x3f
  1505.         mov     edi, esp
  1506.         add     edi, 4+64-1
  1507.         mov     ecx, eax
  1508.         mov     eax, ebx
  1509.         mov     ebx, 16
  1510.    d_hexnum:
  1511.         xor     edx, edx
  1512.         call    division_64_bits
  1513.         div     ebx
  1514.    hexletters = __fdo_hexdigits
  1515.         add     edx, hexletters
  1516.         mov     dl, [edx]
  1517.         mov     [edi], dl
  1518.         dec     edi
  1519.         loop    d_hexnum
  1520.         pop     eax
  1521.         call    normalize_number
  1522.         call    draw_num_text
  1523.         add     esp, 64
  1524.         popad
  1525.         ret
  1526.    no_display_hexnum:
  1527.  
  1528.         cmp     ah, 0x02         ; BINARY
  1529.         jne     no_display_binnum
  1530.         shr     eax, 16
  1531.         and     eax, 0xC03f
  1532. ;     and   eax,0x3f
  1533.         push    eax
  1534.         and     eax, 0x3f
  1535.         mov     edi, esp
  1536.         add     edi, 4+64-1
  1537.         mov     ecx, eax
  1538.         mov     eax, ebx
  1539.         mov     ebx, 2
  1540.    d_binnum:
  1541.         xor     edx, edx
  1542.         call    division_64_bits
  1543.         div     ebx
  1544.         add     dl, 48
  1545.         mov     [edi], dl
  1546.         dec     edi
  1547.         loop    d_binnum
  1548.         pop     eax
  1549.         call    normalize_number
  1550.         call    draw_num_text
  1551.         add     esp, 64
  1552.         popad
  1553.         ret
  1554.    no_display_binnum:
  1555.  
  1556.         add     esp, 64
  1557.         popad
  1558.         ret
  1559.  
  1560. normalize_number:
  1561.         test    ah, 0x80
  1562.         jz      .continue
  1563.         mov     ecx, 48
  1564.         and     eax, 0x3f
  1565. @@:
  1566.         inc     edi
  1567.         cmp     [edi], cl
  1568.         jne     .continue
  1569.         dec     eax
  1570.         cmp     eax, 1
  1571.         ja      @r
  1572.         mov     al, 1
  1573. .continue:
  1574.         and     eax, 0x3f
  1575.         ret
  1576.  
  1577. division_64_bits:
  1578.         test    [esp+1+4], byte 0x40
  1579.         jz      .continue
  1580.         push    eax
  1581.         mov     eax, ebp
  1582.         div     ebx
  1583.         mov     ebp, eax
  1584.         pop     eax
  1585. .continue:
  1586.         ret
  1587.  
  1588. draw_num_text:
  1589.         mov     esi, eax
  1590.         mov     edx, 64+4
  1591.         sub     edx, eax
  1592.         add     edx, esp
  1593.         mov     ebx, [esp+64+32-8+4]
  1594. ; add window start x & y
  1595.         mov     ecx, [TASK_BASE]
  1596.  
  1597.         mov     edi, [CURRENT_TASK]
  1598.         shl     edi, 8
  1599.  
  1600.         mov     eax, [ecx-twdw+WDATA.box.left]
  1601.         add     eax, [edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1602.         shl     eax, 16
  1603.         add     eax, [ecx-twdw+WDATA.box.top]
  1604.         add     eax, [edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1605.         add     ebx, eax
  1606.         mov     ecx, [esp+64+32-12+4]
  1607.         mov     eax, [esp+64+8]         ; background color (if given)
  1608.         mov     edi, [esp+64+4]
  1609.         and     ecx, 5FFFFFFFh
  1610.         bt      ecx, 27
  1611.         jnc     @f
  1612.         mov     edi, eax
  1613. @@:
  1614.         jmp     dtext
  1615. ;-----------------------------------------------------------------------------
  1616. iglobal
  1617. midi_base dw 0
  1618. endg
  1619. ;-----------------------------------------------------------------------------
  1620. align 4
  1621. sys_setup:
  1622. ;  1 = roland mpu midi base , base io address
  1623. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1624. ;  3 = not used
  1625. ;  4 = not used
  1626. ;  5 = system language, 1eng 2fi 3ger 4rus
  1627. ;  6 = not used
  1628. ;  7 = not used
  1629. ;  8 = not used
  1630. ;  9 = not used
  1631. ; 10 = not used
  1632. ; 11 = enable lba read
  1633. ; 12 = enable pci access
  1634. ;-----------------------------------------------------------------------------
  1635.         and     [esp+32], dword 0
  1636. ; F.21.1 - set MPU MIDI base port
  1637.         dec     ebx
  1638.         jnz     @f
  1639.  
  1640.         cmp     ecx, 0x100
  1641.         jb      @f
  1642.  
  1643.         mov     esi, 65535
  1644.         cmp     esi, ecx
  1645.         jb      @f
  1646.  
  1647.         mov     [midi_base], cx
  1648.         mov     word [mididp], cx
  1649.         inc     cx
  1650.         mov     word [midisp], cx
  1651.         ret
  1652. ;--------------------------------------
  1653. @@:
  1654. ; F.21.2 - set keyboard layout
  1655.         dec     ebx
  1656.         jnz     @f
  1657.  
  1658.         mov     edi, [TASK_BASE]
  1659.         mov     eax, [edi+TASKDATA.mem_start]
  1660.         add     eax, edx
  1661. ; 1 = normal layout
  1662.         dec     ecx
  1663.         jnz     .shift
  1664.  
  1665.         mov     ebx, keymap
  1666.         mov     ecx, 128
  1667.         call    memmove
  1668.         ret
  1669. ;--------------------------------------
  1670. .shift:
  1671. ; 2 = layout at pressed Shift
  1672.         dec     ecx
  1673.         jnz     .alt
  1674.  
  1675.         mov     ebx, keymap_shift
  1676.         mov     ecx, 128
  1677.         call    memmove
  1678.         ret
  1679. ;--------------------------------------
  1680. .alt:
  1681. ; 3 = layout at pressed Alt
  1682.         dec     ecx
  1683.         jnz     .country
  1684.  
  1685.         mov     ebx, keymap_alt
  1686.         mov     ecx, 128
  1687.         call    memmove
  1688.         ret
  1689. ;--------------------------------------
  1690. .country:
  1691. ; country identifier
  1692.         sub     ecx, 6
  1693.         jnz     .error
  1694.  
  1695.         mov     word [keyboard], dx
  1696.         ret
  1697. ;--------------------------------------
  1698. @@:
  1699. ; F.21.5 - set system language
  1700.         sub     ebx, 3
  1701.         jnz     @f
  1702.  
  1703.         mov     [syslang], ecx
  1704.         ret
  1705. ;--------------------------------------
  1706. @@:
  1707. ; F.21.11 - enable/disable low-level access to HD
  1708.         and     ecx, 1
  1709.         sub     ebx, 6
  1710.         jnz     @f
  1711.  
  1712.         mov     [lba_read_enabled], ecx
  1713.         ret
  1714. ;--------------------------------------
  1715. @@:
  1716. ; F.21.12 - enable/disable low-level access to PCI
  1717.         dec     ebx
  1718.         jnz     .error
  1719.  
  1720.         mov     [pci_access_enabled], ecx
  1721.         ret
  1722. ;--------------------------------------
  1723. .error:
  1724.         or      [esp+32], dword -1
  1725.         ret
  1726. ;-----------------------------------------------------------------------------
  1727. align 4
  1728. sys_getsetup:
  1729. ;  1 = roland mpu midi base , base io address
  1730. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1731. ;  3 = not used
  1732. ;  4 = not used
  1733. ;  5 = system language, 1eng 2fi 3ger 4rus
  1734. ;  6 = not used
  1735. ;  7 = not used
  1736. ;  8 = not used
  1737. ;  9 = get hs timer tic
  1738. ; 10 = not used
  1739. ; 11 = get the state "lba read"
  1740. ; 12 = get the state "pci access"
  1741. ;-----------------------------------------------------------------------------
  1742. ; F.26.1 - get MPU MIDI base port
  1743.         dec     ebx
  1744.         jnz     @f
  1745.  
  1746.         movzx   eax, [midi_base]
  1747.         mov     [esp+32], eax
  1748.         ret
  1749. ;--------------------------------------
  1750. @@:
  1751. ; F.26.2 - get keyboard layout
  1752.         dec     ebx
  1753.         jnz     @f
  1754.  
  1755.         mov     edi, [TASK_BASE]
  1756.         mov     ebx, [edi+TASKDATA.mem_start]
  1757.         add     ebx, edx
  1758. ; 1 = normal layout
  1759.         dec     ecx
  1760.         jnz     .shift
  1761.  
  1762.         ; if given memory address belongs to kernel then error
  1763.         stdcall is_region_userspace, ebx, 128
  1764.         jz      .addr_error
  1765.  
  1766.         mov     eax, keymap
  1767.         mov     ecx, 128
  1768.         call    memmove
  1769.         ret
  1770. ;--------------------------------------
  1771. .shift:
  1772. ; 2 = layout with pressed Shift
  1773.         dec     ecx
  1774.         jnz     .alt
  1775.  
  1776.         stdcall is_region_userspace, ebx, 128
  1777.         jz      .addr_error
  1778.  
  1779.         mov     eax, keymap_shift
  1780.         mov     ecx, 128
  1781.         call    memmove
  1782.         ret
  1783. ;--------------------------------------
  1784. .alt:
  1785. ; 3 = layout with pressed Alt
  1786.         dec     ecx
  1787.         jne     .country
  1788.  
  1789.         stdcall is_region_userspace, ebx, 128
  1790.         jz      .addr_error
  1791.  
  1792.         mov     eax, keymap_alt
  1793.         mov     ecx, 128
  1794.         call    memmove
  1795.         ret
  1796. ;--------------------------------------
  1797. .country:
  1798. ; 9 = country identifier
  1799.         sub     ecx, 6
  1800.         jnz     .error
  1801.  
  1802.         movzx   eax, word [keyboard]
  1803.         mov     [esp+32], eax
  1804.         ret
  1805.  
  1806. .addr_error:    ; if given memory address is illegal
  1807.         mov     dword [esp+32], -1
  1808.         ret        
  1809. ;--------------------------------------
  1810. @@:
  1811. ; F.26.5 - get system language
  1812.         sub     ebx, 3
  1813.         jnz     @f
  1814.  
  1815.         mov     eax, [syslang]
  1816.         mov     [esp+32], eax
  1817.         ret
  1818. ;--------------------------------------
  1819. @@:
  1820. ; F.26.9 - get the value of the time counter
  1821.         sub     ebx, 4
  1822.         jnz     @f
  1823.  
  1824.         mov     eax, [timer_ticks]
  1825.         mov     [esp+32], eax
  1826.         ret
  1827. ;--------------------------------------
  1828. @@:
  1829. ; F.26.10 - get the time from kernel launch in nanoseconds
  1830.         sub     ebx, 1
  1831.         jnz     @f
  1832.  
  1833.         call    get_clock_ns
  1834.         mov     [esp+24], edx
  1835.         mov     [esp+32], eax
  1836.         ret
  1837. ;--------------------------------------
  1838. @@:
  1839. ; F.26.11 - Find out whether low-level HD access is enabled
  1840.         sub     ebx, 1
  1841.         jnz     @f
  1842.  
  1843.         mov     eax, [lba_read_enabled]
  1844.         mov     [esp+32], eax
  1845.         ret
  1846. ;--------------------------------------
  1847. @@:
  1848. ; F.26.12 - Find out whether low-level PCI access is enabled
  1849.         dec     ebx
  1850.         jnz     .error
  1851.  
  1852.         mov     eax, [pci_access_enabled]
  1853.         mov     [esp+32], eax
  1854.         ret
  1855. ;--------------------------------------
  1856. .error:
  1857.         or      [esp+32], dword -1
  1858.         ret
  1859. ;-----------------------------------------------------------------------------
  1860. get_timer_ticks:
  1861.         mov     eax, [timer_ticks]
  1862.         ret
  1863. ;-----------------------------------------------------------------------------
  1864. readmousepos:
  1865. ; eax=0 screen relative
  1866. ; eax=1 window relative
  1867. ; eax=2 buttons pressed
  1868. ; eax=3 buttons pressed ext
  1869. ; eax=4 load cursor
  1870. ; eax=5 set cursor
  1871. ; eax=6 delete cursor
  1872. ; eax=7 get mouse_z
  1873. ; eax=8 load cursor unicode
  1874.         cmp     ebx, 8
  1875.         ja      @f
  1876.         jmp     dword[.mousefn+ebx*4]
  1877.  
  1878. align 4
  1879. .mousefn:
  1880. dd  .msscreen
  1881. dd  .mswin
  1882. dd  .msbutton
  1883. dd  .msbuttonExt
  1884. dd  .app_load_cursor
  1885. dd  .app_set_cursor
  1886. dd  .app_delete_cursor
  1887. dd  .msz
  1888. dd  .loadCursorUni
  1889.  
  1890. .msscreen:
  1891.         mov     eax, [MOUSE_X]
  1892.         shl     eax, 16
  1893.         mov     ax, [MOUSE_Y]
  1894.         mov     [esp+36-4], eax
  1895. @@:
  1896.         ret
  1897.  
  1898. .mswin:
  1899.         mov     eax, [MOUSE_X]
  1900.         shl     eax, 16
  1901.         mov     ax, [MOUSE_Y]
  1902.         mov     esi, [TASK_BASE]
  1903.         mov     bx, word [esi-twdw+WDATA.box.left]
  1904.         shl     ebx, 16
  1905.         mov     bx, word [esi-twdw+WDATA.box.top]
  1906.         sub     eax, ebx
  1907.         mov     edi, [CURRENT_TASK]
  1908.         shl     edi, 8
  1909.         sub     ax, word[edi+SLOT_BASE+APPDATA.wnd_clientbox.top]
  1910.         rol     eax, 16
  1911.         sub     ax, word[edi+SLOT_BASE+APPDATA.wnd_clientbox.left]
  1912.         rol     eax, 16
  1913.         mov     [esp+36-4], eax
  1914.         ret
  1915.  
  1916. .msbutton:
  1917.         movzx   eax, byte [BTN_DOWN]
  1918.         mov     [esp+36-4], eax
  1919.         ret
  1920.  
  1921. .msbuttonExt:
  1922.         mov     eax, [BTN_DOWN]
  1923.         mov     [esp+36-4], eax
  1924.         ret
  1925.  
  1926. .app_load_cursor:
  1927.         cmp     ecx, OS_BASE
  1928.         jae     @f
  1929.         stdcall load_cursor, ecx, edx
  1930.         mov     [esp+36-4], eax
  1931. @@:
  1932.         ret
  1933.  
  1934. .loadCursorUni:
  1935.         cmp     ecx, OS_BASE
  1936.         jae     @b
  1937.         push    ecx edx
  1938.         stdcall kernel_alloc, maxPathLength
  1939.         mov     edi, eax
  1940.         pop     eax esi
  1941.         push    edi
  1942.         call    getFullPath
  1943.         pop     ebp
  1944.         test    eax, eax
  1945.         jz      @f
  1946.         stdcall load_cursor, ebp, LOAD_FROM_FILE
  1947.         mov     [esp+32], eax
  1948. @@:
  1949.         stdcall kernel_free, ebp
  1950.         ret
  1951.  
  1952. .app_set_cursor:
  1953.         stdcall set_cursor, ecx
  1954.         mov     [esp+36-4], eax
  1955.         ret
  1956.  
  1957. .app_delete_cursor:
  1958.         stdcall delete_cursor, ecx
  1959.         mov     [esp+36-4], eax
  1960.         ret
  1961.  
  1962. .msz:
  1963.         mov     edi, [TASK_COUNT]
  1964.         movzx   edi, word [WIN_POS + edi*2]
  1965.         cmp     edi, [CURRENT_TASK]
  1966.         jne     @f
  1967.         mov     ax, [MOUSE_SCROLL_H]
  1968.         shl     eax, 16
  1969.         mov     ax, [MOUSE_SCROLL_V]
  1970.         mov     [esp+36-4], eax
  1971.         and     [MOUSE_SCROLL_H], word 0
  1972.         and     [MOUSE_SCROLL_V], word 0
  1973.         ret
  1974. @@:
  1975.         and     [esp+36-4], dword 0
  1976.         ret
  1977.  
  1978. is_input:
  1979.  
  1980.         push    edx
  1981.         mov     dx, word [midisp]
  1982.         in      al, dx
  1983.         and     al, 0x80
  1984.         pop     edx
  1985.         ret
  1986.  
  1987. is_output:
  1988.  
  1989.         push    edx
  1990.         mov     dx, word [midisp]
  1991.         in      al, dx
  1992.         and     al, 0x40
  1993.         pop     edx
  1994.         ret
  1995.  
  1996.  
  1997. get_mpu_in:
  1998.  
  1999.         push    edx
  2000.         mov     dx, word [mididp]
  2001.         in      al, dx
  2002.         pop     edx
  2003.         ret
  2004.  
  2005.  
  2006. put_mpu_out:
  2007.  
  2008.         push    edx
  2009.         mov     dx, word [mididp]
  2010.         out     dx, al
  2011.         pop     edx
  2012.         ret
  2013.  
  2014.  
  2015.  
  2016. align 4
  2017.  
  2018. sys_midi:
  2019.         cmp     [mididp], 0
  2020.         jnz     sm0
  2021.         mov     [esp+36], dword 1
  2022.         ret
  2023. sm0:
  2024.         and     [esp+36], dword 0
  2025.         dec     ebx
  2026.         jnz     smn1
  2027.  ;    call setuart
  2028. su1:
  2029.         call    is_output
  2030.         test    al, al
  2031.         jnz     su1
  2032.         mov     dx, word [midisp]
  2033.         mov     al, 0xff
  2034.         out     dx, al
  2035. su2:
  2036.         mov     dx, word [midisp]
  2037.         mov     al, 0xff
  2038.         out     dx, al
  2039.         call    is_input
  2040.         test    al, al
  2041.         jnz     su2
  2042.         call    get_mpu_in
  2043.         cmp     al, 0xfe
  2044.         jnz     su2
  2045. su3:
  2046.         call    is_output
  2047.         test    al, al
  2048.         jnz     su3
  2049.         mov     dx, word [midisp]
  2050.         mov     al, 0x3f
  2051.         out     dx, al
  2052.         ret
  2053. smn1:
  2054.         dec     ebx
  2055.         jnz     smn2
  2056. sm10:
  2057.         call    get_mpu_in
  2058.         call    is_output
  2059.         test    al, al
  2060.         jnz     sm10
  2061.         mov     al, bl
  2062.         call    put_mpu_out
  2063.         smn2:
  2064.         ret
  2065.  
  2066. detect_devices:
  2067. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2068. ;include 'detect/commouse.inc'
  2069. ;include 'detect/ps2mouse.inc'
  2070. ;include 'detect/dev_fd.inc'
  2071. ;include 'detect/dev_hdcd.inc'
  2072. ;include 'detect/sear_par.inc'
  2073. ;!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  2074.         ret
  2075.  
  2076. sys_end:
  2077. ;--------------------------------------
  2078.         cmp     [_display.select_cursor], 0
  2079.         je      @f
  2080. ; restore default cursor before killing
  2081.         pusha
  2082.         mov     ecx, [current_slot]
  2083.         call    restore_default_cursor_before_killing
  2084.         popa
  2085. @@:
  2086. ;--------------------------------------
  2087. ; kill all sockets this process owns
  2088.         pusha
  2089.         mov     edx, [TASK_BASE]
  2090.         mov     edx, [edx+TASKDATA.pid]
  2091.         call    socket_process_end
  2092.         popa
  2093. ;--------------------------------------
  2094.         mov     ecx, [current_slot]
  2095.         mov     eax, [ecx+APPDATA.tls_base]
  2096.         test    eax, eax
  2097.         jz      @F
  2098.  
  2099.         stdcall user_free, eax
  2100. @@:
  2101.  
  2102.         mov     eax, [TASK_BASE]
  2103.         mov     [eax+TASKDATA.state], TSTATE_ZOMBIE
  2104.         call    wakeup_osloop
  2105.  
  2106. .waitterm:            ; wait here for termination
  2107.         call    change_task
  2108.         jmp     .waitterm
  2109. ;------------------------------------------------------------------------------
  2110. align 4
  2111. restore_default_cursor_before_killing:
  2112.         pushfd
  2113.         cli
  2114.         mov     eax, [def_cursor]
  2115.         mov     [ecx+APPDATA.cursor], eax
  2116.  
  2117.         movzx   eax, word [MOUSE_Y]
  2118.         movzx   ebx, word [MOUSE_X]
  2119.         mov     eax, [d_width_calc_area + eax*4]
  2120.  
  2121.         add     eax, [_display.win_map]
  2122.         movzx   edx, byte [ebx+eax]
  2123.         shl     edx, 8
  2124.         mov     esi, [edx+SLOT_BASE+APPDATA.cursor]
  2125.  
  2126.         cmp     esi, [current_cursor]
  2127.         je      @f
  2128.  
  2129.         push    esi
  2130.         call    [_display.select_cursor]
  2131.         mov     [current_cursor], esi
  2132. @@:
  2133.         mov     [redrawmouse_unconditional], 1
  2134.         call    wakeup_osloop
  2135.         popfd
  2136.         ret
  2137. ;------------------------------------------------------------------------------
  2138. iglobal
  2139. align 4
  2140. sys_system_table:
  2141.         dd      sysfn_deactivate        ; 1 = deactivate window
  2142.         dd      sysfn_terminate         ; 2 = terminate thread
  2143.         dd      sysfn_activate          ; 3 = activate window
  2144.         dd      sysfn_getidletime       ; 4 = get idle time
  2145.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  2146.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  2147.         dd      sysfn_getactive         ; 7 = get active window
  2148.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  2149.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  2150.         dd      sysfn_minimize          ; 10 = minimize window
  2151.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  2152.         dd      sysfn_lastkey           ; 12 = get last pressed key
  2153.         dd      sysfn_getversion        ; 13 = get kernel version
  2154.         dd      sysfn_waitretrace       ; 14 = wait retrace
  2155.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  2156.         dd      sysfn_getfreemem        ; 16 = get free memory size
  2157.         dd      sysfn_getallmem         ; 17 = get total memory size
  2158.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  2159.                                         ;                 instead of slot
  2160.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  2161.         dd      sysfn_meminfo           ; 20 = get extended memory info
  2162.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  2163.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  2164.         dd      sysfn_min_windows       ; 23 = minimize all windows
  2165.         dd      sysfn_set_screen_sizes  ; 24 = set screen sizes for Vesa
  2166.  
  2167.         dd      sysfn_zmodif            ; 25 = get/set window z modifier  ;Fantomer
  2168. sysfn_num = ($ - sys_system_table)/4
  2169. endg
  2170. ;------------------------------------------------------------------------------
  2171. sys_system:
  2172.         dec     ebx
  2173.         cmp     ebx, sysfn_num
  2174.         jae     @f
  2175.         jmp     dword [sys_system_table + ebx*4]
  2176. @@:
  2177.         ret
  2178. ;------------------------------------------------------------------------------
  2179. sysfn_shutdown:          ; 18.9 = system shutdown
  2180.         cmp     ecx, SYSTEM_SHUTDOWN
  2181.         jl      exit_for_anyone
  2182.         cmp     ecx, SYSTEM_RESTART
  2183.         jg      exit_for_anyone
  2184.         mov     [BOOT.shutdown_type], cl
  2185.  
  2186.         mov     eax, [TASK_COUNT]
  2187.         mov     [SYS_SHUTDOWN], al
  2188.         mov     [shutdown_processes], eax
  2189.         call    wakeup_osloop
  2190.         and     dword [esp+32], 0
  2191.  exit_for_anyone:
  2192.         ret
  2193.   uglobal
  2194.    shutdown_processes:
  2195.                        dd 0x0
  2196.   endg
  2197. ;------------------------------------------------------------------------------
  2198. ; in: eax -- APPDATA ptr
  2199. ; out: Z/z -- is/not kernel thread
  2200. is_kernel_thread:
  2201.         mov     eax, [eax+APPDATA.process]
  2202.         cmp     eax, [SLOT_BASE+2*sizeof.APPDATA+APPDATA.process]       ; OS
  2203.         ret
  2204. ;------------------------------------------------------------------------------
  2205. sysfn_terminate:        ; 18.2 = TERMINATE
  2206.         push    ecx
  2207.         cmp     ecx, 2
  2208.         jb      noprocessterminate
  2209.         mov     edx, [TASK_COUNT]
  2210.         cmp     ecx, edx
  2211.         ja      noprocessterminate
  2212.         mov     eax, [TASK_COUNT]
  2213.         shl     ecx, BSF sizeof.TASKDATA
  2214.         mov     edx, [ecx+CURRENT_TASK+TASKDATA.pid]
  2215.         add     ecx, CURRENT_TASK+TASKDATA.state
  2216.         cmp     byte [ecx], TSTATE_FREE
  2217.         jz      noprocessterminate
  2218.         push    eax
  2219.         lea     eax, [(ecx-(CURRENT_TASK and 1FFFFFFFh)-TASKDATA.state)*8+SLOT_BASE]
  2220.         call    is_kernel_thread
  2221.         pop     eax
  2222.         jz      noprocessterminate
  2223.         push    ecx edx
  2224.         lea     edx, [(ecx-(CURRENT_TASK and 1FFFFFFFh)-TASKDATA.state)*8+SLOT_BASE]
  2225.         call    request_terminate
  2226.         pop     edx ecx
  2227.         test    eax, eax
  2228.         jz      noprocessterminate
  2229. ;--------------------------------------
  2230. ; terminate all network sockets it used
  2231.         pusha
  2232.         mov     eax, edx
  2233.         call    socket_process_end
  2234.         popa
  2235. ;--------------------------------------
  2236.         cmp     [_display.select_cursor], 0
  2237.         je      .restore_end
  2238. ; restore default cursor before killing
  2239.         pusha
  2240.         mov     ecx, [esp+32]
  2241.         shl     ecx, 8
  2242.         add     ecx, SLOT_BASE
  2243.         mov     eax, [def_cursor]
  2244.         cmp     [ecx+APPDATA.cursor], eax
  2245.         je      @f
  2246.         call    restore_default_cursor_before_killing
  2247. @@:
  2248.         popa
  2249. .restore_end:
  2250. ;--------------------------------------
  2251.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2252.         mov     [ecx], byte 3; clear possible i40's
  2253.         call    wakeup_osloop
  2254.      ;call MEM_Heap_UnLock
  2255.  
  2256.         cmp     edx, [application_table_owner]; clear app table stat
  2257.         jne     noatsc
  2258.         call    unlock_application_table
  2259. noatsc:
  2260. noprocessterminate:
  2261.         add     esp, 4
  2262.         ret
  2263. ;------------------------------------------------------------------------------
  2264. sysfn_terminate2:
  2265. ;lock application_table_status mutex
  2266. .table_status:
  2267.         call    lock_application_table
  2268.         mov     eax, ecx
  2269.         call    pid_to_slot
  2270.         test    eax, eax
  2271.         jz      .not_found
  2272.         mov     ecx, eax
  2273.         cli
  2274.         call    sysfn_terminate
  2275.         call    unlock_application_table
  2276.         sti
  2277.         and     dword [esp+32], 0
  2278.         ret
  2279. .not_found:
  2280.         call    unlock_application_table
  2281.         or      dword [esp+32], -1
  2282.         ret
  2283. ;------------------------------------------------------------------------------
  2284. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2285.         cmp     ecx, 2
  2286.         jb      .nowindowdeactivate
  2287.         cmp     ecx, [TASK_COUNT]
  2288.         ja      .nowindowdeactivate
  2289.  
  2290.         movzx   esi, word [WIN_STACK + ecx*2]
  2291.         cmp     esi, 1
  2292.         je      .nowindowdeactivate ; already deactive
  2293.  
  2294.         mov     edi, ecx
  2295.         shl     edi, 5
  2296.         add     edi, window_data
  2297.         movzx   esi, word [WIN_STACK + ecx * 2]
  2298.         lea     esi, [WIN_POS + esi * 2]
  2299.         call    window._.window_deactivate
  2300.         call    syscall_display_settings.calculateScreen
  2301.         call    syscall_display_settings.redrawScreen
  2302. .nowindowdeactivate:
  2303.         ret
  2304. ;------------------------------------------------------------------------------
  2305. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2306.         cmp     ecx, 2
  2307.         jb      .nowindowactivate
  2308.         cmp     ecx, [TASK_COUNT]
  2309.         ja      .nowindowactivate
  2310. ;-------------------------------------
  2311. @@:
  2312. ; If the window is captured and moved by the user,
  2313. ; then you can't change the position in window stack!!!
  2314.         mov     al, [mouse.active_sys_window.action]
  2315.         and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
  2316.         test    al, al
  2317.         jz      @f
  2318.         call    change_task
  2319.         jmp     @b
  2320. @@:
  2321. ;-------------------------------------
  2322.         mov     [window_minimize], 2; restore window if minimized
  2323.         call    wakeup_osloop
  2324.  
  2325.         movzx   esi, word [WIN_STACK + ecx*2]
  2326.         cmp     esi, [TASK_COUNT]
  2327.         je      .nowindowactivate; already active
  2328.  
  2329.         mov     edi, ecx
  2330.         shl     edi, 5
  2331.         add     edi, window_data
  2332.         movzx   esi, word [WIN_STACK + ecx * 2]
  2333.         lea     esi, [WIN_POS + esi * 2]
  2334.         call    waredraw
  2335. .nowindowactivate:
  2336.         ret
  2337. ;------------------------------------------------------------------------------
  2338. align 4
  2339. sysfn_zmodif:
  2340. ;18,25,1 - get z_modif
  2341. ;18,25,2 - set z_modif
  2342. ;edx = -1(for current task) or TID
  2343. ;esi(for 2) = new value z_modif
  2344. ;return:
  2345. ;1:   eax = z_modif
  2346. ;2: eax=0(fail),1(success) for set z_modif
  2347.  
  2348.         cmp     edx, -1
  2349.         jne     @f
  2350.         mov     edx, [CURRENT_TASK]
  2351.      @@:
  2352.         cmp     edx, [TASK_COUNT]
  2353.         ja      .fail
  2354.         cmp     edx, 1
  2355.         je      .fail
  2356.  
  2357.         mov     eax, edx
  2358.         shl     edx, 5
  2359.  
  2360.         cmp     [edx + CURRENT_TASK + TASKDATA.state], 9
  2361.         je      .fail
  2362.  
  2363.         cmp     ecx, 1
  2364.         jnz     .set_zmod
  2365.  
  2366.         mov     al, [edx + window_data + WDATA.z_modif]
  2367.         jmp     .exit
  2368.  
  2369. .set_zmod:
  2370.         cmp     ecx, 2
  2371.         jnz     .fail
  2372.  
  2373.         mov     ebx, esi
  2374.         mov     esi, eax
  2375.  
  2376.         cmp     bl, ZPOS_ALWAYS_TOP
  2377.         jg      .fail
  2378.  
  2379.         mov     [edx + window_data + WDATA.z_modif], bl
  2380.  
  2381.         mov     eax, [edx + window_data + WDATA.box.left]
  2382.         mov     ebx, [edx + window_data + WDATA.box.top]
  2383.         mov     ecx, [edx + window_data + WDATA.box.width]
  2384.         mov     edx, [edx + window_data + WDATA.box.height]
  2385.         add     ecx, eax
  2386.         add     edx, ebx
  2387.         call    window._.set_screen
  2388.         call    window._.set_top_wnd
  2389.         call    window._.redraw_top_wnd
  2390.  
  2391.         shl     esi, 5
  2392.         mov     [esi + window_data + WDATA.fl_redraw], 1
  2393.  
  2394.  
  2395.         mov     eax, 1
  2396.         jmp     .exit
  2397. .fail:
  2398.         xor     eax, eax
  2399. .exit:
  2400.         mov     [esp+32], eax
  2401.         ret
  2402.  
  2403. ;------------------------------------------------------------------------------
  2404. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2405.         mov     eax, [CURRENT_TASK+32+TASKDATA.cpu_usage]
  2406.         mov     [esp+32], eax
  2407.         ret
  2408. ;------------------------------------------------------------------------------
  2409. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2410.         mov     eax, dword [cpu_freq]
  2411.         mov     [esp+32], eax
  2412.         ret
  2413. ;------------------------------------------------------------------------------
  2414. get_cpu_freq:
  2415.         mov     eax, dword [cpu_freq]
  2416.         mov     edx, dword [cpu_freq+4]
  2417.         ret
  2418. ;  SAVE ramdisk to /hd/1/menuet.img
  2419. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2420.    include 'blkdev/rdsave.inc'
  2421. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2422. ;------------------------------------------------------------------------------
  2423. align 4
  2424. sysfn_getactive:        ; 18.7 = get active window
  2425.         mov     eax, [TASK_COUNT]
  2426.         movzx   eax, word [WIN_POS + eax*2]
  2427.         mov     [esp+32], eax
  2428.         ret
  2429. ;------------------------------------------------------------------------------
  2430. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2431. ;     cmp  ecx,1
  2432.         dec     ecx
  2433.         jnz     nogetsoundflag
  2434.         movzx   eax, byte [sound_flag]; get sound_flag
  2435.         mov     [esp+32], eax
  2436.         ret
  2437.  nogetsoundflag:
  2438. ;     cmp  ecx,2
  2439.         dec     ecx
  2440.         jnz     nosoundflag
  2441.         xor     byte [sound_flag], 1
  2442.  nosoundflag:
  2443.         ret
  2444. ;------------------------------------------------------------------------------
  2445. sysfn_minimize:         ; 18.10 = minimize window
  2446.         mov     [window_minimize], 1
  2447.         call    wakeup_osloop
  2448.         ret
  2449. ;------------------------------------------------------------------------------
  2450. align 4
  2451. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2452.         dec     ecx
  2453.         jnz     .exit
  2454. .small_table:
  2455.         stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
  2456.         jz      .exit
  2457.         mov     edi, edx
  2458.         mov     esi, DRIVE_DATA
  2459.         mov     ecx, DRIVE_DATA_SIZE ;10
  2460.         cld
  2461.         rep movsb
  2462. .exit:
  2463.         ret
  2464. ;------------------------------------------------------------------------------
  2465. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2466.         and     dword [esp+32], 0
  2467.         ret
  2468. ;------------------------------------------------------------------------------
  2469. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2470.         ; if given memory address belongs to kernel then error
  2471.         stdcall is_region_userspace, ecx, version_end-version_inf
  2472.         jz      .addr_error
  2473.  
  2474.         mov     edi, ecx
  2475.         mov     esi, version_inf
  2476.         mov     ecx, version_end-version_inf
  2477.         rep movsb
  2478.         ret
  2479. .addr_error:    ; if given memory address is illegal
  2480.         mov     dword [esp+32], -1
  2481.         ret  
  2482. ;------------------------------------------------------------------------------
  2483. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2484.      ;wait retrace functions
  2485.  sys_wait_retrace:
  2486.         mov     edx, 0x3da
  2487.  WaitRetrace_loop:
  2488.         in      al, dx
  2489.         test    al, 1000b
  2490.         jz      WaitRetrace_loop
  2491.         and     [esp+32], dword 0
  2492.         ret
  2493. ;------------------------------------------------------------------------------
  2494. align 4
  2495. sysfn_centermouse:      ; 18.15 = mouse centered
  2496.         mov     eax, [_display.width]
  2497.         shr     eax, 1
  2498.         mov     [MOUSE_X], ax
  2499.         mov     eax, [_display.height]
  2500.         shr     eax, 1
  2501.         mov     [MOUSE_Y], ax
  2502.         call    wakeup_osloop
  2503.         xor     eax, eax
  2504.         and     [esp+32], eax
  2505.         ret
  2506. ;------------------------------------------------------------------------------
  2507. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2508.         cmp     ecx, 8
  2509.         jnc     @f
  2510.         jmp     dword [.table+ecx*4]
  2511. .get_mouse_acceleration:
  2512.         xor     eax, eax
  2513.         mov     ax, [mouse_speed_factor]
  2514.         mov     [esp+32], eax
  2515.         ret
  2516. .set_mouse_acceleration:
  2517.         mov     [mouse_speed_factor], dx
  2518.         ret
  2519. .get_mouse_delay:
  2520.         xor     eax, eax
  2521.         mov     al, [mouse_delay]
  2522.         mov     [esp+32], eax
  2523.         ret
  2524. .set_mouse_delay:
  2525.         mov     [mouse_delay], dl
  2526. @@:
  2527.         ret
  2528. .set_pointer_position:
  2529.         cmp     dx, word[_display.height]
  2530.         jae     @b
  2531.         rol     edx, 16
  2532.         cmp     dx, word[_display.width]
  2533.         jae     @b
  2534.         mov     [MOUSE_X], edx
  2535.         mov     [mouse_active], 1
  2536.         jmp     wakeup_osloop
  2537. .set_mouse_button:
  2538.         mov     [BTN_DOWN], edx
  2539.         mov     [mouse_active], 1
  2540.         jmp     wakeup_osloop
  2541. .get_doubleclick_delay:
  2542.         xor     eax, eax
  2543.         mov     al, [mouse_doubleclick_delay]
  2544.         mov     [esp+32], eax
  2545.         ret
  2546. .set_doubleclick_delay:
  2547.         mov     [mouse_doubleclick_delay], dl
  2548.         ret
  2549. align 4
  2550. .table:
  2551. dd      .get_mouse_acceleration
  2552. dd      .set_mouse_acceleration
  2553. dd      .get_mouse_delay
  2554. dd      .set_mouse_delay
  2555. dd      .set_pointer_position
  2556. dd      .set_mouse_button
  2557. dd      .get_doubleclick_delay
  2558. dd      .set_doubleclick_delay
  2559. ;------------------------------------------------------------------------------
  2560. sysfn_getfreemem:
  2561.         mov     eax, [pg_data.pages_free]
  2562.         shl     eax, 2
  2563.         mov     [esp+32], eax
  2564.         ret
  2565. ;------------------------------------------------------------------------------
  2566. sysfn_getallmem:
  2567.         mov     eax, [MEM_AMOUNT]
  2568.         shr     eax, 10
  2569.         mov     [esp+32], eax
  2570.         ret
  2571. ;------------------------------------------------------------------------------
  2572. sysfn_pid_to_slot:
  2573.         mov     eax, ecx
  2574.         call    pid_to_slot
  2575.         mov     [esp+32], eax
  2576.         ret
  2577. ;------------------------------------------------------------------------------
  2578. sysfn_min_rest_window:
  2579.         pushad
  2580.         mov     eax, edx ; ebx - operating
  2581.         shr     ecx, 1
  2582.         jnc     @f
  2583.         call    pid_to_slot
  2584. @@:
  2585.         or      eax, eax ; eax - number of slot
  2586.         jz      .error
  2587.         cmp     eax, 255    ; varify maximal slot number
  2588.         ja      .error
  2589.         movzx   eax, word [WIN_STACK + eax*2]
  2590.         shr     ecx, 1
  2591.         jc      .restore
  2592.  ; .minimize:
  2593.         call    minimize_window
  2594.         jmp     .exit
  2595. .restore:
  2596.         call    restore_minimized_window
  2597. .exit:
  2598.         popad
  2599.         xor     eax, eax
  2600.         mov     [esp+32], eax
  2601.         ret
  2602. .error:
  2603.         popad
  2604.         xor     eax, eax
  2605.         dec     eax
  2606.         mov     [esp+32], eax
  2607.         ret
  2608. ;------------------------------------------------------------------------------
  2609. sysfn_min_windows:
  2610.         call    minimize_all_window
  2611.         mov     [esp+32], eax
  2612.         call    change_task
  2613.         ret
  2614. ;------------------------------------------------------------------------------
  2615. sysfn_set_screen_sizes:
  2616.         cmp     [SCR_MODE], word 0x13
  2617.         jbe     .exit
  2618.  
  2619.         cmp     [_display.select_cursor], select_cursor
  2620.         jne     .exit
  2621.  
  2622.         cmp     ecx, [display_width_standard]
  2623.         ja      .exit
  2624.  
  2625.         cmp     edx, [display_height_standard]
  2626.         ja      .exit
  2627.  
  2628.         pushfd
  2629.         cli
  2630.         mov     eax, ecx
  2631.         mov     ecx, [_display.lfb_pitch]
  2632.         mov     [_display.width], eax
  2633.         dec     eax
  2634.         mov     [_display.height], edx
  2635.         dec     edx
  2636. ; eax - new Screen_Max_X
  2637. ; edx - new Screen_Max_Y
  2638.         mov     [do_not_touch_winmap], 1
  2639.         call    set_screen
  2640.         mov     [do_not_touch_winmap], 0
  2641.         popfd
  2642.         call    change_task
  2643. .exit:
  2644.         ret
  2645. ;------------------------------------------------------------------------------
  2646. uglobal
  2647. screen_workarea RECT
  2648. display_width_standard dd 0
  2649. display_height_standard dd 0
  2650. do_not_touch_winmap db 0
  2651. window_minimize db 0
  2652. sound_flag      db 0
  2653.  
  2654. endg
  2655.  
  2656. UID_NONE=0
  2657. UID_MENUETOS=1   ;official
  2658. UID_KOLIBRI=2    ;russian
  2659.  
  2660. iglobal
  2661. version_inf:
  2662.         db 0,7,7,0  ; version 0.7.7.0
  2663.         db 0
  2664. .rev    dd __REV__
  2665. version_end:
  2666. endg
  2667. ;------------------------------------------------------------------------------
  2668. align 4
  2669. sys_cachetodiskette:
  2670.         cmp     ebx, 1
  2671.         jb      .no_floppy_save
  2672.         cmp     ebx, 2
  2673.         ja      .no_floppy_save
  2674.         call    save_image
  2675.         mov     [esp + 32], eax
  2676.         ret
  2677. .no_floppy_save:
  2678.         mov     [esp + 32], dword 1
  2679.         ret
  2680. ;------------------------------------------------------------------------------
  2681. uglobal
  2682. ;  bgrchanged  dd  0x0
  2683. align 4
  2684. bgrlockpid dd 0
  2685. bgrlock db 0
  2686. endg
  2687. ;------------------------------------------------------------------------------
  2688. align 4
  2689. sys_background:
  2690.         cmp     ebx, 1                     ; BACKGROUND SIZE
  2691.         jnz     nosb1
  2692.         test    ecx, ecx
  2693.         jz      sbgrr
  2694.  
  2695.         test    edx, edx
  2696.         jz      sbgrr
  2697. ;--------------------------------------
  2698. align 4
  2699. @@:
  2700. ;;Maxis use atomic bts for mutexes  4.4.2009
  2701.         bts     dword [bgrlock], 0
  2702.         jnc     @f
  2703.         call    change_task
  2704.         jmp     @b
  2705. ;--------------------------------------
  2706. align 4
  2707. @@:
  2708.         mov     [BgrDataWidth], ecx
  2709.         mov     [BgrDataHeight], edx
  2710. ;    mov   [bgrchanged],1
  2711.  
  2712.         pushad
  2713. ; return memory for old background
  2714.         mov     eax, [img_background]
  2715.         cmp     eax, static_background_data
  2716.         jz      @f
  2717.         stdcall kernel_free, eax
  2718. ;--------------------------------------
  2719. align 4
  2720. @@:
  2721. ; calculate RAW size
  2722.         xor     eax, eax
  2723.         inc     eax
  2724.         cmp     [BgrDataWidth], eax
  2725.         jae     @f
  2726.         mov     [BgrDataWidth], eax
  2727. ;--------------------------------------
  2728. align 4
  2729. @@:
  2730.         cmp     [BgrDataHeight], eax
  2731.         jae     @f
  2732.         mov     [BgrDataHeight], eax
  2733. ;--------------------------------------
  2734. align 4
  2735. @@:
  2736.         mov     eax, [BgrDataWidth]
  2737.         imul    eax, [BgrDataHeight]
  2738.         lea     eax, [eax*3]
  2739. ; it is reserved with aligned to the boundary of 4 KB pages,
  2740. ; otherwise there may be exceptions a page fault for vesa20_drawbackground_tiled
  2741. ; because the 32 bit read is used for  high performance: "mov eax,[esi]"
  2742.         shr     eax, 12
  2743.         inc     eax
  2744.         shl     eax, 12
  2745.         mov     [mem_BACKGROUND], eax
  2746. ; get memory for new background
  2747.         stdcall kernel_alloc, eax
  2748.         test    eax, eax
  2749.         jz      .memfailed
  2750.         mov     [img_background], eax
  2751.         jmp     .exit
  2752. ;--------------------------------------
  2753. align 4
  2754. .memfailed:
  2755. ; revert to static monotone data
  2756.         mov     [img_background], static_background_data
  2757.         xor     eax, eax
  2758.         inc     eax
  2759.         mov     [BgrDataWidth], eax
  2760.         mov     [BgrDataHeight], eax
  2761.         mov     [mem_BACKGROUND], 4
  2762. ;--------------------------------------
  2763. align 4
  2764. .exit:
  2765.         popad
  2766.         mov     [bgrlock], 0
  2767. ;--------------------------------------
  2768. align 4
  2769. sbgrr:
  2770.         ret
  2771. ;------------------------------------------------------------------------------
  2772. align 4
  2773. nosb1:
  2774.         cmp     ebx, 2                     ; SET PIXEL
  2775.         jnz     nosb2
  2776.  
  2777.         mov     eax, [img_background]
  2778.         test    ecx, ecx
  2779.         jz      @f
  2780.         cmp     eax, static_background_data
  2781.         jz      .ret
  2782. ;--------------------------------------
  2783. align 4
  2784. @@:
  2785.         mov     ebx, [mem_BACKGROUND]
  2786.         add     ebx, 4095
  2787.         and     ebx, -4096
  2788.         sub     ebx, 4
  2789.         cmp     ecx, ebx
  2790.         ja      .ret
  2791.  
  2792.         mov     ebx, [eax+ecx]
  2793.         and     ebx, 0xFF000000;255*256*256*256
  2794.         and     edx, 0x00FFFFFF;255*256*256+255*256+255
  2795.         add     edx, ebx
  2796.         mov     [eax+ecx], edx
  2797. ;--------------------------------------
  2798. align 4
  2799. .ret:
  2800.         ret
  2801. ;------------------------------------------------------------------------------
  2802. align 4
  2803. nosb2:
  2804.         cmp     ebx, 3                     ; DRAW BACKGROUND
  2805.         jnz     nosb3
  2806. ;--------------------------------------
  2807. align 4
  2808. draw_background_temp:
  2809.         mov     [background_defined], 1
  2810.         call    force_redraw_background
  2811. ;--------------------------------------
  2812. align 4
  2813. nosb31:
  2814.         ret
  2815. ;------------------------------------------------------------------------------
  2816. align 4
  2817. nosb3:
  2818.         cmp     ebx, 4                     ; TILED / STRETCHED
  2819.         jnz     nosb4
  2820.         cmp     ecx, [BgrDrawMode]
  2821.         je      nosb41
  2822.         mov     [BgrDrawMode], ecx
  2823. ;--------------------------------------
  2824. align 4
  2825. nosb41:
  2826.         ret
  2827. ;------------------------------------------------------------------------------
  2828. align 4
  2829. nosb4:
  2830.         cmp     ebx, 5                     ; BLOCK MOVE TO BGR
  2831.         jnz     nosb5
  2832.         cmp     [img_background], static_background_data
  2833.         jnz     @f
  2834.         test    edx, edx
  2835.         jnz     .fin
  2836.         cmp     esi, 4
  2837.         ja      .fin
  2838. ;--------------------------------------
  2839. align 4
  2840. @@:
  2841.   ; bughere
  2842.         mov     eax, ecx
  2843.         mov     ebx, edx
  2844.         add     ebx, [img_background];IMG_BACKGROUND
  2845.         mov     ecx, esi
  2846.         call    memmove
  2847. ;--------------------------------------
  2848. align 4
  2849. .fin:
  2850.         ret
  2851. ;------------------------------------------------------------------------------
  2852. align 4
  2853. nosb5:
  2854.         cmp     ebx, 6
  2855.         jnz     nosb6
  2856. ;--------------------------------------
  2857. align 4
  2858. ;;Maxis use atomic bts for mutex 4.4.2009
  2859. @@:
  2860.         bts     dword [bgrlock], 0
  2861.         jnc     @f
  2862.         call    change_task
  2863.         jmp     @b
  2864. ;--------------------------------------
  2865. align 4
  2866. @@:
  2867.         mov     eax, [CURRENT_TASK]
  2868.         mov     [bgrlockpid], eax
  2869.         cmp     [img_background], static_background_data
  2870.         jz      .nomem
  2871.         stdcall user_alloc, [mem_BACKGROUND]
  2872.         mov     [esp+32], eax
  2873.         test    eax, eax
  2874.         jz      .nomem
  2875.         mov     ebx, eax
  2876.         shr     ebx, 12
  2877.         or      dword [page_tabs+(ebx-1)*4], MEM_BLOCK_DONT_FREE
  2878.         mov     esi, [img_background]
  2879.         shr     esi, 12
  2880.         mov     ecx, [mem_BACKGROUND]
  2881.         add     ecx, 0xFFF
  2882.         shr     ecx, 12
  2883. ;--------------------------------------
  2884. align 4
  2885. .z:
  2886.         mov     eax, [page_tabs+ebx*4]
  2887.         test    al, 1
  2888.         jz      @f
  2889.         call    free_page
  2890. ;--------------------------------------
  2891. align 4
  2892. @@:
  2893.         mov     eax, [page_tabs+esi*4]
  2894.         or      al, PG_UWR
  2895.         mov     [page_tabs+ebx*4], eax
  2896.         mov     eax, ebx
  2897.         shl     eax, 12
  2898.         invlpg  [eax]
  2899.         inc     ebx
  2900.         inc     esi
  2901.         loop    .z
  2902.         ret
  2903. ;--------------------------------------
  2904. align 4
  2905. .nomem:
  2906.         and     [bgrlockpid], 0
  2907.         mov     [bgrlock], 0
  2908. ;------------------------------------------------------------------------------
  2909. align 4
  2910. nosb6:
  2911.         cmp     ebx, 7
  2912.         jnz     nosb7
  2913.         cmp     [bgrlock], 0
  2914.         jz      .err
  2915.         mov     eax, [CURRENT_TASK]
  2916.         cmp     [bgrlockpid], eax
  2917.         jnz     .err
  2918.         mov     eax, ecx
  2919.         mov     ebx, ecx
  2920.         shr     eax, 12
  2921.         mov     ecx, [page_tabs+(eax-1)*4]
  2922.         test    cl, MEM_BLOCK_USED or MEM_BLOCK_DONT_FREE
  2923.         jz      .err
  2924.         jnp     .err
  2925.         push    eax
  2926.         shr     ecx, 12
  2927.         dec     ecx
  2928. ;--------------------------------------
  2929. align 4
  2930. @@:
  2931.         and     dword [page_tabs+eax*4], 0
  2932.         mov     edx, eax
  2933.         shl     edx, 12
  2934.         push    eax
  2935.         invlpg  [edx]
  2936.         pop     eax
  2937.         inc     eax
  2938.         loop    @b
  2939.         pop     eax
  2940.         and     dword [page_tabs+(eax-1)*4], not MEM_BLOCK_DONT_FREE
  2941.         stdcall user_free, ebx
  2942.         mov     [esp+32], eax
  2943.         and     [bgrlockpid], 0
  2944.         mov     [bgrlock], 0
  2945.         ret
  2946. ;--------------------------------------
  2947. align 4
  2948. .err:
  2949.         and     dword [esp+32], 0
  2950.         ret
  2951. ;------------------------------------------------------------------------------
  2952. align 4
  2953. nosb7:
  2954.         cmp     ebx, 8
  2955.         jnz     nosb8
  2956.  
  2957.         mov     ecx, [current_slot]
  2958.         xor     eax, eax
  2959.         xchg    eax, [ecx+APPDATA.draw_bgr_x]
  2960.         mov     [esp + 32], eax ; eax = [left]*65536 + [right]
  2961.         xor     eax, eax
  2962.         xchg    eax, [ecx+APPDATA.draw_bgr_y]
  2963.         mov     [esp + 20], eax ; ebx = [top]*65536 + [bottom]
  2964.         ret
  2965. ;------------------------------------------------------------------------------
  2966. align 4
  2967. nosb8:
  2968.         cmp     ebx, 9
  2969.         jnz     nosb9
  2970. ; ecx = [left]*65536 + [right]
  2971. ; edx = [top]*65536 + [bottom]
  2972.         mov     eax, [_display.width]
  2973.         mov     ebx, [_display.height]
  2974. ; check [right]
  2975.         cmp     cx, ax
  2976.         jae     .exit
  2977. ; check [left]
  2978.         ror     ecx, 16
  2979.         cmp     cx, ax
  2980.         jae     .exit
  2981. ; check [bottom]
  2982.         cmp     dx, bx
  2983.         jae     .exit
  2984. ; check [top]
  2985.         ror     edx, 16
  2986.         cmp     dx, bx
  2987.         jae     .exit
  2988.  
  2989.         movzx   eax, cx  ; [left]
  2990.         movzx   ebx, dx  ; [top]
  2991.  
  2992.         shr     ecx, 16 ; [right]
  2993.         shr     edx, 16 ; [bottom]
  2994.  
  2995.         mov     [background_defined], 1
  2996.  
  2997.         mov     [draw_data+32 + RECT.left], eax
  2998.         mov     [draw_data+32 + RECT.top], ebx
  2999.  
  3000.         mov     [draw_data+32 + RECT.right], ecx
  3001.         mov     [draw_data+32 + RECT.bottom], edx
  3002.  
  3003.         inc     [REDRAW_BACKGROUND]
  3004.         call    wakeup_osloop
  3005. ;--------------------------------------
  3006. align 4
  3007. .exit:
  3008.         ret
  3009. ;------------------------------------------------------------------------------
  3010. align 4
  3011. nosb9:
  3012.         ret
  3013. ;------------------------------------------------------------------------------
  3014. align 4
  3015. uglobal
  3016.   BG_Rect_X_left_right  dd   0x0
  3017.   BG_Rect_Y_top_bottom  dd   0x0
  3018. endg
  3019. ;------------------------------------------------------------------------------
  3020. align 4
  3021. force_redraw_background:
  3022.         and     [draw_data+32 + RECT.left], 0
  3023.         and     [draw_data+32 + RECT.top], 0
  3024.         push    eax ebx
  3025.         mov     eax, [_display.width]
  3026.         mov     ebx, [_display.height]
  3027.         dec     eax
  3028.         dec     ebx
  3029.         mov     [draw_data+32 + RECT.right], eax
  3030.         mov     [draw_data+32 + RECT.bottom], ebx
  3031.         pop     ebx eax
  3032.         inc     [REDRAW_BACKGROUND]
  3033.         call    wakeup_osloop
  3034.         ret
  3035. ;------------------------------------------------------------------------------
  3036. align 4
  3037. sys_getbackground:
  3038. ;    cmp   eax,1                                  ; SIZE
  3039.         dec     ebx
  3040.         jnz     nogb1
  3041.         mov     eax, [BgrDataWidth]
  3042.         shl     eax, 16
  3043.         mov     ax, word [BgrDataHeight]
  3044.         mov     [esp+32], eax
  3045.         ret
  3046. ;------------------------------------------------------------------------------
  3047. align 4
  3048. nogb1:
  3049. ;    cmp   eax,2                                  ; PIXEL
  3050.         dec     ebx
  3051.         jnz     nogb2
  3052.  
  3053.         mov     eax, [img_background]
  3054.         test    ecx, ecx
  3055.         jz      @f
  3056.         cmp     eax, static_background_data
  3057.         jz      .ret
  3058. ;--------------------------------------
  3059. align 4
  3060. @@:
  3061.         mov     ebx, [mem_BACKGROUND]
  3062.         add     ebx, 4095
  3063.         and     ebx, -4096
  3064.         sub     ebx, 4
  3065.         cmp     ecx, ebx
  3066.         ja      .ret
  3067.  
  3068.         mov     eax, [ecx+eax]
  3069.  
  3070.         and     eax, 0xFFFFFF
  3071.         mov     [esp+32], eax
  3072. ;--------------------------------------
  3073. align 4
  3074. .ret:
  3075.         ret
  3076. ;------------------------------------------------------------------------------
  3077. align 4
  3078. nogb2:
  3079.  
  3080. ;    cmp   eax,4                                  ; TILED / STRETCHED
  3081.         dec     ebx
  3082.         dec     ebx
  3083.         jnz     nogb4
  3084.         mov     eax, [BgrDrawMode]
  3085. ;--------------------------------------
  3086. align 4
  3087. nogb4:
  3088.         mov     [esp+32], eax
  3089.         ret
  3090. ;------------------------------------------------------------------------------
  3091. align 4
  3092. sys_getkey:
  3093.         mov     [esp + 32], dword 1
  3094.         ; test main buffer
  3095.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  3096.         movzx   ecx, word [WIN_STACK + ebx * 2]
  3097.         mov     edx, [TASK_COUNT]
  3098.         cmp     ecx, edx
  3099.         jne     .finish
  3100.         cmp     [KEY_COUNT], byte 0
  3101.         je      .finish
  3102.         movzx   ax, byte [KEY_BUFF + 120 + 2]
  3103.         shl     eax, 8
  3104.         mov     al, byte [KEY_BUFF]
  3105.         shl     eax, 8
  3106.         push    eax
  3107.         dec     byte [KEY_COUNT]
  3108.         and     byte [KEY_COUNT], 127
  3109.         movzx   ecx, byte [KEY_COUNT]
  3110.         add     ecx, 2
  3111.         mov     eax, KEY_BUFF + 1
  3112.         mov     ebx, KEY_BUFF
  3113.         call    memmove
  3114.         add     eax, 120 + 2
  3115.         add     ebx, 120 + 2
  3116.         call    memmove
  3117.         pop     eax
  3118. ;--------------------------------------
  3119. align 4
  3120. .ret_eax:
  3121.         mov     [esp + 32], eax
  3122.         ret
  3123. ;--------------------------------------
  3124. align 4
  3125. .finish:
  3126. ; test hotkeys buffer
  3127.         mov     ecx, hotkey_buffer
  3128. ;--------------------------------------
  3129. align 4
  3130. @@:
  3131.         cmp     [ecx], ebx
  3132.         jz      .found
  3133.         add     ecx, 8
  3134.         cmp     ecx, hotkey_buffer + 120 * 8
  3135.         jb      @b
  3136.         ret
  3137. ;--------------------------------------
  3138. align 4
  3139. .found:
  3140.         mov     ax, [ecx + 6]
  3141.         shl     eax, 16
  3142.         mov     ah, [ecx + 4]
  3143.         mov     al, 2
  3144.         and     dword [ecx + 4], 0
  3145.         and     dword [ecx], 0
  3146.         jmp     .ret_eax
  3147. ;------------------------------------------------------------------------------
  3148. align 4
  3149. sys_getbutton:
  3150.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  3151.         mov     [esp + 32], dword 1
  3152.         movzx   ecx, word [WIN_STACK + ebx * 2]
  3153.         mov     edx, [TASK_COUNT] ; less than 256 processes
  3154.         cmp     ecx, edx
  3155.         jne     .exit
  3156.         movzx   eax, byte [BTN_COUNT]
  3157.         test    eax, eax
  3158.         jz      .exit
  3159.         mov     eax, [BTN_BUFF]
  3160.         and     al, 0xFE                                    ; delete left button bit
  3161.         mov     [BTN_COUNT], byte 0
  3162.         mov     [esp + 32], eax
  3163. ;--------------------------------------
  3164. align 4
  3165. .exit:
  3166.         ret
  3167. ;------------------------------------------------------------------------------
  3168. align 4
  3169. sys_cpuusage:
  3170.  
  3171. ;  RETURN:
  3172. ;
  3173. ;  +00 dword     process cpu usage
  3174. ;  +04  word     position in windowing stack
  3175. ;  +06  word     windowing stack value at current position (cpu nro)
  3176. ;  +10 12 bytes  name
  3177. ;  +22 dword     start in mem
  3178. ;  +26 dword     used mem
  3179. ;  +30 dword     PID , process idenfification number
  3180. ;
  3181.         ; if given memory address belongs to kernel then error
  3182.         stdcall is_region_userspace, ebx, 0x4C
  3183.         jz      .addr_error
  3184.  
  3185.         cmp     ecx, -1 ; who am I ?
  3186.         jne     .no_who_am_i
  3187.         mov     ecx, [CURRENT_TASK]
  3188.   .no_who_am_i:
  3189.         cmp     ecx, max_processes
  3190.         ja      .nofillbuf
  3191.  
  3192. ; +4: word: position of the window of thread in the window stack
  3193.         mov     ax, [WIN_STACK + ecx * 2]
  3194.         mov     [ebx+4], ax
  3195. ; +6: word: number of the thread slot, which window has in the window stack
  3196. ;           position ecx (has no relation to the specific thread)
  3197.         mov     ax, [WIN_POS + ecx * 2]
  3198.         mov     [ebx+6], ax
  3199.  
  3200.         shl     ecx, 5
  3201.  
  3202. ; +0: dword: memory usage
  3203.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  3204.         mov     [ebx], eax
  3205. ; +10: 11 bytes: name of the process
  3206.         push    ecx
  3207.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  3208.         add     ebx, 10
  3209.         mov     ecx, 11
  3210.         call    memmove
  3211.         pop     ecx
  3212.  
  3213. ; +22: address of the process in memory
  3214. ; +26: size of used memory - 1
  3215.         push    edi
  3216.         lea     edi, [ebx+12]
  3217.         xor     eax, eax
  3218.         mov     edx, 0x100000*16
  3219.         cmp     ecx, 1 shl 5
  3220.         je      .os_mem
  3221.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.process]
  3222.         mov     edx, [edx+PROC.mem_used]
  3223.         mov     eax, std_application_base_address
  3224. .os_mem:
  3225.         stosd
  3226.         lea     eax, [edx-1]
  3227.         stosd
  3228.  
  3229. ; +30: PID/TID
  3230.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  3231.         stosd
  3232.  
  3233.     ; window position and size
  3234.         push    esi
  3235.         lea     esi, [ecx + window_data + WDATA.box]
  3236.         movsd
  3237.         movsd
  3238.         movsd
  3239.         movsd
  3240.  
  3241.     ; Process state (+50)
  3242.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  3243.         stosd
  3244.  
  3245.     ; Window client area box
  3246.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  3247.         movsd
  3248.         movsd
  3249.         movsd
  3250.         movsd
  3251.  
  3252.     ; Window state
  3253.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  3254.         stosb
  3255.  
  3256.     ; Event mask (+71)
  3257.         mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
  3258.         stosd
  3259.  
  3260.     ; Keyboard mode (+75)
  3261.         mov     al, byte [ecx*8 + SLOT_BASE + APPDATA.keyboard_mode]
  3262.         stosb
  3263.  
  3264.         pop     esi
  3265.         pop     edi
  3266.  
  3267. .nofillbuf:
  3268.     ; return number of processes
  3269.  
  3270.         mov     eax, [TASK_COUNT]
  3271.         mov     [esp+32], eax
  3272.         ret
  3273.  
  3274. .addr_error:    ; if given memory address is illegal
  3275.         mov     dword [esp+32], -1
  3276.         ret  
  3277.  
  3278. align 4
  3279. sys_clock:
  3280.         cli
  3281.   ; Mikhail Lisovin  xx Jan 2005
  3282.   @@:
  3283.         mov     al, 10
  3284.         out     0x70, al
  3285.         in      al, 0x71
  3286.         test    al, al
  3287.         jns     @f
  3288.         mov     esi, 1
  3289.         call    delay_ms
  3290.         jmp     @b
  3291.   @@:
  3292.   ; end Lisovin's fix
  3293.  
  3294.         xor     al, al        ; seconds
  3295.         out     0x70, al
  3296.         in      al, 0x71
  3297.         movzx   ecx, al
  3298.         mov     al, 02        ; minutes
  3299.         shl     ecx, 16
  3300.         out     0x70, al
  3301.         in      al, 0x71
  3302.         movzx   edx, al
  3303.         mov     al, 04        ; hours
  3304.         shl     edx, 8
  3305.         out     0x70, al
  3306.         in      al, 0x71
  3307.         add     ecx, edx
  3308.         movzx   edx, al
  3309.         add     ecx, edx
  3310.         sti
  3311.         mov     [esp + 32], ecx
  3312.         ret
  3313.  
  3314.  
  3315. align 4
  3316.  
  3317. sys_date:
  3318.  
  3319.         cli
  3320.   @@:
  3321.         mov     al, 10
  3322.         out     0x70, al
  3323.         in      al, 0x71
  3324.         test    al, al
  3325.         jns     @f
  3326.         mov     esi, 1
  3327.         call    delay_ms
  3328.         jmp     @b
  3329.   @@:
  3330.  
  3331.         mov     ch, 0
  3332.         mov     al, 7           ; date
  3333.         out     0x70, al
  3334.         in      al, 0x71
  3335.         mov     cl, al
  3336.         mov     al, 8           ; month
  3337.         shl     ecx, 16
  3338.         out     0x70, al
  3339.         in      al, 0x71
  3340.         mov     ch, al
  3341.         mov     al, 9           ; year
  3342.         out     0x70, al
  3343.         in      al, 0x71
  3344.         mov     cl, al
  3345.         sti
  3346.         mov     [esp+32], ecx
  3347.         ret
  3348.  
  3349.  
  3350. ; redraw status
  3351.  
  3352. sys_redrawstat:
  3353.         cmp     ebx, 1
  3354.         jne     no_widgets_away
  3355.         ; buttons away
  3356.         mov     ecx, [CURRENT_TASK]
  3357.   sys_newba2:
  3358.         mov     edi, [BTN_ADDR]
  3359.         cmp     [edi], dword 0  ; empty button list ?
  3360.         je      end_of_buttons_away
  3361.         movzx   ebx, word [edi]
  3362.         inc     ebx
  3363.         mov     eax, edi
  3364.   sys_newba:
  3365.         dec     ebx
  3366.         jz      end_of_buttons_away
  3367.  
  3368.         add     eax, 0x10
  3369.         cmp     cx, [eax]
  3370.         jnz     sys_newba
  3371.  
  3372.         push    eax ebx ecx
  3373.         mov     ecx, ebx
  3374.         inc     ecx
  3375.         shl     ecx, 4
  3376.         mov     ebx, eax
  3377.         add     eax, 0x10
  3378.         call    memmove
  3379.         dec     dword [edi]
  3380.         pop     ecx ebx eax
  3381.  
  3382.         jmp     sys_newba2
  3383.  
  3384.   end_of_buttons_away:
  3385.  
  3386.         ret
  3387.  
  3388.   no_widgets_away:
  3389.  
  3390.         cmp     ebx, 2
  3391.         jnz     srl1
  3392.  
  3393.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  3394.         add     edx, draw_data - CURRENT_TASK
  3395.         mov     [edx + RECT.left], 0
  3396.         mov     [edx + RECT.top], 0
  3397.         mov     eax, [_display.width]
  3398.         dec     eax
  3399.         mov     [edx + RECT.right], eax
  3400.         mov     eax, [_display.height]
  3401.         dec     eax
  3402.         mov     [edx + RECT.bottom], eax
  3403.  
  3404.   srl1:
  3405.         ret
  3406.  
  3407. ;ok - 100% work
  3408. ;nt - not tested
  3409. ;---------------------------------------------------------------------------------------------
  3410. ;eax
  3411. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3412. ;1 - change task. Ret nothing. Block. ok.
  3413. ;2 - performance control
  3414. ; ebx
  3415. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3416. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3417. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3418. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3419. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3420. ;eax
  3421. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3422. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3423. ;---------------------------------------------------------------------------------------------
  3424. iglobal
  3425. align 4
  3426. sheduler:
  3427.         dd      sys_sheduler.00
  3428.         dd      change_task
  3429.         dd      sys_sheduler.02
  3430.         dd      sys_sheduler.03
  3431.         dd      sys_sheduler.04
  3432. endg
  3433. sys_sheduler:
  3434. ;rewritten by <Lrz>  29.12.2009
  3435.         jmp     dword [sheduler+ebx*4]
  3436. ;.shed_counter:
  3437. .00:
  3438.         mov     eax, [context_counter]
  3439.         mov     [esp+32], eax
  3440.         ret
  3441.  
  3442. .02:
  3443. ;.perf_control:
  3444.         inc     ebx                     ;before ebx=2, ebx=3
  3445.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  3446.         jz      cache_disable
  3447.  
  3448.         dec     ebx                     ;ebx=2
  3449.         cmp     ebx, ecx                ;
  3450.         jz      cache_enable            ;if ecx=2 and ebx=2
  3451.  
  3452.         dec     ebx                     ;ebx=1
  3453.         cmp     ebx, ecx
  3454.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  3455.  
  3456.         dec     ebx
  3457.         test    ebx, ecx                ;ebx=0 and ecx=0
  3458.         jz      modify_pce              ;if ecx=0
  3459.  
  3460.         ret
  3461.  
  3462. .03:
  3463. ;.rdmsr_instr:
  3464. ;now counter in ecx
  3465. ;(edx:eax) esi:edi => edx:esi
  3466.         mov     eax, esi
  3467.         mov     ecx, edx
  3468.         rdmsr
  3469.         mov     [esp+32], eax
  3470.         mov     [esp+20], edx           ;ret in ebx?
  3471.         ret
  3472.  
  3473. .04:
  3474. ;.wrmsr_instr:
  3475. ;now counter in ecx
  3476. ;(edx:eax) esi:edi => edx:esi
  3477.         ; Fast Call MSR can't be destroy
  3478.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  3479.         ; включаются/выключаются расширенные возможности
  3480.         cmp     edx, MSR_SYSENTER_CS
  3481.         je      @f
  3482.         cmp     edx, MSR_SYSENTER_ESP
  3483.         je      @f
  3484.         cmp     edx, MSR_SYSENTER_EIP
  3485.         je      @f
  3486.         cmp     edx, MSR_AMD_STAR
  3487.         je      @f
  3488.  
  3489.         mov     eax, esi
  3490.         mov     ecx, edx
  3491.         wrmsr
  3492.         ; mov   [esp + 32], eax
  3493.         ; mov   [esp + 20], edx ;ret in ebx?
  3494. @@:
  3495.         ret
  3496.  
  3497. cache_disable:
  3498.         mov     eax, cr0
  3499.         or      eax, 01100000000000000000000000000000b
  3500.         mov     cr0, eax
  3501.         wbinvd  ;set MESI
  3502.         ret
  3503.  
  3504. cache_enable:
  3505.         mov     eax, cr0
  3506.         and     eax, 10011111111111111111111111111111b
  3507.         mov     cr0, eax
  3508.         ret
  3509.  
  3510. is_cache_enabled:
  3511.         mov     eax, cr0
  3512.         mov     ebx, eax
  3513.         and     eax, 01100000000000000000000000000000b
  3514.         jz      cache_disabled
  3515.         mov     [esp+32], ebx
  3516. cache_disabled:
  3517.         mov     dword [esp+32], eax;0
  3518.         ret
  3519.  
  3520. modify_pce:
  3521.         mov     eax, cr4
  3522. ;       mov ebx,0
  3523. ;       or  bx,100000000b ;pce
  3524. ;       xor eax,ebx ;invert pce
  3525.         bts     eax, 8;pce=cr4[8]
  3526.         mov     cr4, eax
  3527.         mov     [esp+32], eax
  3528.         ret
  3529. ;---------------------------------------------------------------------------------------------
  3530.  
  3531.  
  3532. iglobal
  3533.   cpustring db 'CPU',0
  3534. endg
  3535.  
  3536. uglobal
  3537. background_defined    db    0    ; diamond, 11.04.2006
  3538. endg
  3539. ;-----------------------------------------------------------------------------
  3540. align 4
  3541. checkmisc:
  3542.         cmp     [ctrl_alt_del], 1
  3543.         jne     nocpustart
  3544.  
  3545.         mov     ebp, cpustring
  3546.         call    fs_execute_from_sysdir
  3547.  
  3548.         mov     [ctrl_alt_del], 0
  3549. ;--------------------------------------
  3550. align 4
  3551. nocpustart:
  3552.         cmp     [mouse_active], 1
  3553.         jne     mouse_not_active
  3554.         mov     [mouse_active], 0
  3555.  
  3556.         xor     edi, edi
  3557.         mov     ebx, CURRENT_TASK
  3558.  
  3559.         mov     ecx, [TASK_COUNT]
  3560.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  3561.         shl     eax, 8
  3562.         push    eax
  3563.  
  3564.         movzx   eax, word [MOUSE_X]
  3565.         movzx   edx, word [MOUSE_Y]
  3566. ;--------------------------------------
  3567. align 4
  3568. .set_mouse_event:
  3569.         add     edi, sizeof.APPDATA
  3570.         add     ebx, sizeof.TASKDATA
  3571.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3572.         jz      .pos_filter
  3573.  
  3574.         cmp     edi, [esp]                      ; skip if filtration active
  3575.         jne     .skip
  3576. ;--------------------------------------
  3577. align 4
  3578. .pos_filter:
  3579.         test    [ebx+TASKDATA.event_mask], 0x40000000
  3580.         jz      .set
  3581.  
  3582.         mov     esi, [ebx-twdw+WDATA.box.left]
  3583.         cmp     eax, esi
  3584.         jb      .skip
  3585.         add     esi, [ebx-twdw+WDATA.box.width]
  3586.         cmp     eax, esi
  3587.         ja      .skip
  3588.  
  3589.         mov     esi, [ebx-twdw+WDATA.box.top]
  3590.         cmp     edx, esi
  3591.         jb      .skip
  3592.         add     esi, [ebx-twdw+WDATA.box.height]
  3593.         cmp     edx, esi
  3594.         ja      .skip
  3595. ;--------------------------------------
  3596. align 4
  3597. .set:
  3598.         or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b  ; set event 6
  3599. ;--------------------------------------
  3600. align 4
  3601. .skip:
  3602.         loop    .set_mouse_event
  3603.  
  3604.         pop     eax
  3605. ;--------------------------------------
  3606. align 4
  3607. mouse_not_active:
  3608.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  3609.         jz      nobackgr
  3610.  
  3611.         cmp     [background_defined], 0
  3612.         jz      nobackgr
  3613. ;--------------------------------------
  3614. align 4
  3615. backgr:
  3616.         mov     eax, [draw_data+32 + RECT.left]
  3617.         shl     eax, 16
  3618.         add     eax, [draw_data+32 + RECT.right]
  3619.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  3620.  
  3621.         mov     eax, [draw_data+32 + RECT.top]
  3622.         shl     eax, 16
  3623.         add     eax, [draw_data+32 + RECT.bottom]
  3624.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  3625.  
  3626.         call    drawbackground
  3627. ;        DEBUGF  1, "K : drawbackground\n"
  3628. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  3629. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  3630. ;--------- set event 5 start ----------
  3631.         push    ecx edi
  3632.         xor     edi, edi
  3633.         mov     ecx, [TASK_COUNT]
  3634. ;--------------------------------------
  3635. align 4
  3636. set_bgr_event:
  3637.         add     edi, sizeof.APPDATA
  3638.         mov     eax, [BG_Rect_X_left_right]
  3639.         mov     edx, [BG_Rect_Y_top_bottom]
  3640.         cmp     [edi+SLOT_BASE+APPDATA.draw_bgr_x], 0
  3641.         jz      .set
  3642. .join:
  3643.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3644.         jae     @f
  3645.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3646. @@:
  3647.         shr     eax, 16
  3648.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3649.         jbe     @f
  3650.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3651. @@:
  3652.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3653.         jae     @f
  3654.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3655. @@:
  3656.         shr     edx, 16
  3657.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3658.         jbe     @f
  3659.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3660. @@:
  3661.         jmp     .common
  3662. .set:
  3663.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_x], eax
  3664.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_y], edx
  3665. .common:
  3666.         or      [edi+SLOT_BASE+APPDATA.event_mask], 10000b  ; set event 5
  3667.         loop    set_bgr_event
  3668.         pop     edi ecx
  3669. ;--------- set event 5 stop -----------
  3670.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  3671.         jnz     backgr
  3672.  
  3673.         xor     eax, eax
  3674.         mov     [draw_data+32 + RECT.left], eax
  3675.         mov     [draw_data+32 + RECT.top], eax
  3676.         mov     [draw_data+32 + RECT.right], eax
  3677.         mov     [draw_data+32 + RECT.bottom], eax
  3678. ;--------------------------------------
  3679. align 4
  3680. nobackgr:
  3681. ; system shutdown request
  3682.         cmp     [SYS_SHUTDOWN], byte 0
  3683.         je      noshutdown
  3684.  
  3685.         mov     edx, [shutdown_processes]
  3686.  
  3687.         cmp     [SYS_SHUTDOWN], dl
  3688.         jne     noshutdown
  3689.  
  3690.         lea     ecx, [edx-1]
  3691.         mov     edx, OS_BASE+0x3040
  3692.         jecxz   no_mark_system_shutdown
  3693. ;--------------------------------------
  3694. align 4
  3695. markz:
  3696.         push    ecx edx
  3697.         cmp     [edx+TASKDATA.state], TSTATE_FREE
  3698.         jz      .nokill
  3699.         lea     edx, [(edx-(CURRENT_TASK and 1FFFFFFFh))*8+SLOT_BASE]
  3700.         cmp     [edx+APPDATA.process], sys_proc
  3701.         jz      .nokill
  3702.         call    request_terminate
  3703.         jmp     .common
  3704. .nokill:
  3705.         dec     byte [SYS_SHUTDOWN]
  3706.         xor     eax, eax
  3707. .common:
  3708.         pop     edx ecx
  3709.         test    eax, eax
  3710.         jz      @f
  3711.         mov     [edx+TASKDATA.state], TSTATE_ZOMBIE
  3712. @@:
  3713.         add     edx, 0x20
  3714.         loop    markz
  3715.         call    wakeup_osloop
  3716. ;--------------------------------------
  3717. align 4
  3718. @@:
  3719. no_mark_system_shutdown:
  3720.         dec     byte [SYS_SHUTDOWN]
  3721.         je      system_shutdown
  3722. ;--------------------------------------
  3723. align 4
  3724. noshutdown:
  3725.         mov     eax, [TASK_COUNT]           ; termination
  3726.         mov     ebx, TASK_DATA+TASKDATA.state
  3727.         mov     esi, 1
  3728. ;--------------------------------------
  3729. align 4
  3730. newct:
  3731.         mov     cl, [ebx]
  3732.         cmp     cl, byte 3
  3733.         jz      .terminate
  3734.  
  3735.         cmp     cl, byte 4
  3736.         jnz     .noterminate
  3737. .terminate:
  3738.         pushad
  3739.         mov     ecx, eax
  3740.         shl     ecx, 8
  3741.         add     ecx, SLOT_BASE
  3742.         call    restore_default_cursor_before_killing
  3743.         popad
  3744.  
  3745.         pushad
  3746.         call    terminate
  3747.         popad
  3748.         cmp     byte [SYS_SHUTDOWN], 0
  3749.         jz      .noterminate
  3750.         dec     byte [SYS_SHUTDOWN]
  3751.         je      system_shutdown
  3752.  
  3753. .noterminate:
  3754.         add     ebx, 0x20
  3755.         inc     esi
  3756.         dec     eax
  3757.         jnz     newct
  3758.         ret
  3759. ;-----------------------------------------------------------------------------
  3760. align 4
  3761. redrawscreen:
  3762. ; eax , if process window_data base is eax, do not set flag/limits
  3763.  
  3764.         pushad
  3765.         push    eax
  3766.  
  3767. ;;;         mov   ebx,2
  3768. ;;;         call  delay_hs
  3769.  
  3770.          ;mov   ecx,0               ; redraw flags for apps
  3771.         xor     ecx, ecx
  3772. ;--------------------------------------
  3773. align 4
  3774. newdw2:
  3775.         inc     ecx
  3776.         push    ecx
  3777.  
  3778.         mov     eax, ecx
  3779.         shl     eax, 5
  3780.         add     eax, window_data
  3781.  
  3782.         cmp     eax, [esp+4]
  3783.         je      not_this_task
  3784.                                    ; check if window in redraw area
  3785.         mov     edi, eax
  3786.  
  3787.         cmp     ecx, 1             ; limit for background
  3788.         jz      bgli
  3789.  
  3790.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3791.         test    eax, eax
  3792.         jz      @f
  3793.         mov     al, [eax + WDATA.z_modif]
  3794.         cmp     [edi + WDATA.z_modif], al
  3795.         jg      ricino
  3796.       @@:
  3797.  
  3798.         mov     eax, [edi + WDATA.box.left]
  3799.         mov     ebx, [edi + WDATA.box.top]
  3800.  
  3801.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3802.         cmp     ecx, ebx
  3803.         jb      ricino
  3804.  
  3805.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3806.         cmp     ecx, eax
  3807.         jb      ricino
  3808.  
  3809.         mov     eax, [edi + WDATA.box.left]
  3810.         mov     ebx, [edi + WDATA.box.top]
  3811.         mov     ecx, [edi + WDATA.box.width]
  3812.         mov     edx, [edi + WDATA.box.height]
  3813.         add     ecx, eax
  3814.         add     edx, ebx
  3815.  
  3816.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3817.         cmp     edx, eax
  3818.         jb      ricino
  3819.  
  3820.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3821.         cmp     ecx, eax
  3822.         jb      ricino
  3823. ;--------------------------------------
  3824. align 4
  3825. bgli:
  3826.         cmp     dword[esp], 1
  3827.         jnz     .az
  3828.  
  3829.         cmp     [REDRAW_BACKGROUND], 0
  3830.         jz      .az
  3831.  
  3832.         mov     dl, 0
  3833.         lea     eax, [edi+draw_data-window_data]
  3834.         mov     ebx, [draw_limits.left]
  3835.         cmp     ebx, [eax+RECT.left]
  3836.         jae     @f
  3837.  
  3838.         mov     [eax+RECT.left], ebx
  3839.         mov     dl, 1
  3840. ;--------------------------------------
  3841. align 4
  3842. @@:
  3843.         mov     ebx, [draw_limits.top]
  3844.         cmp     ebx, [eax+RECT.top]
  3845.         jae     @f
  3846.  
  3847.         mov     [eax+RECT.top], ebx
  3848.         mov     dl, 1
  3849. ;--------------------------------------
  3850. align 4
  3851. @@:
  3852.         mov     ebx, [draw_limits.right]
  3853.         cmp     ebx, [eax+RECT.right]
  3854.         jbe     @f
  3855.  
  3856.         mov     [eax+RECT.right], ebx
  3857.         mov     dl, 1
  3858. ;--------------------------------------
  3859. align 4
  3860. @@:
  3861.         mov     ebx, [draw_limits.bottom]
  3862.         cmp     ebx, [eax+RECT.bottom]
  3863.         jbe     @f
  3864.  
  3865.         mov     [eax+RECT.bottom], ebx
  3866.         mov     dl, 1
  3867. ;--------------------------------------
  3868. align 4
  3869. @@:
  3870.         add     [REDRAW_BACKGROUND], dl
  3871.         call    wakeup_osloop
  3872.         jmp     newdw8
  3873. ;--------------------------------------
  3874. align 4
  3875. .az:
  3876.         mov     eax, edi
  3877.         add     eax, draw_data-window_data
  3878.  
  3879.         mov     ebx, [draw_limits.left]        ; set limits
  3880.         mov     [eax + RECT.left], ebx
  3881.         mov     ebx, [draw_limits.top]
  3882.         mov     [eax + RECT.top], ebx
  3883.         mov     ebx, [draw_limits.right]
  3884.         mov     [eax + RECT.right], ebx
  3885.         mov     ebx, [draw_limits.bottom]
  3886.         mov     [eax + RECT.bottom], ebx
  3887.  
  3888.         sub     eax, draw_data-window_data
  3889.  
  3890.         cmp     dword [esp], 1
  3891.         jne     nobgrd
  3892.         inc     [REDRAW_BACKGROUND]
  3893.         call    wakeup_osloop
  3894. ;--------------------------------------
  3895. align 4
  3896. newdw8:
  3897. nobgrd:
  3898. ;--------------------------------------
  3899.         push    eax  edi ebp
  3900.         mov     edi, [esp+12]
  3901.         cmp     edi, 1
  3902.         je      .found
  3903.  
  3904.         mov     eax, [draw_limits.left]
  3905.         mov     ebx, [draw_limits.top]
  3906.         mov     ecx, [draw_limits.right]
  3907.         sub     ecx, eax
  3908.         test    ecx, ecx
  3909.         jz      .not_found
  3910.  
  3911.         mov     edx, [draw_limits.bottom]
  3912.         sub     edx, ebx
  3913.         test    edx, edx
  3914.         jz      .not_found
  3915.  
  3916. ; eax - x, ebx - y
  3917. ; ecx - size x, edx - size y
  3918.         add     ebx, edx
  3919. ;--------------------------------------
  3920. align 4
  3921. .start_y:
  3922.         push    ecx
  3923. ;--------------------------------------
  3924. align 4
  3925. .start_x:
  3926.         add     eax, ecx
  3927.         mov     ebp, [d_width_calc_area + ebx*4]
  3928.         add     ebp, [_display.win_map]
  3929.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3930.         cmp     ebp, edi
  3931.         jne     @f
  3932.  
  3933.         pop     ecx
  3934.         jmp     .found
  3935. ;--------------------------------------
  3936. align 4
  3937. @@:
  3938.         sub     eax, ecx
  3939.  
  3940.         dec     ecx
  3941.         jnz     .start_x
  3942.  
  3943.         pop     ecx
  3944.         dec     ebx
  3945.         dec     edx
  3946.         jnz     .start_y
  3947. ;--------------------------------------
  3948. align 4
  3949. .not_found:
  3950.         pop     ebp edi eax
  3951.         jmp     ricino
  3952. ;--------------------------------------
  3953. align 4
  3954. .found:
  3955.         pop     ebp edi eax
  3956.  
  3957.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3958. ;--------------------------------------
  3959. align 4
  3960. ricino:
  3961. not_this_task:
  3962.         pop     ecx
  3963.  
  3964.         cmp     ecx, [TASK_COUNT]
  3965.         jle     newdw2
  3966.  
  3967.         pop     eax
  3968.         popad
  3969.         ret
  3970. ;-----------------------------------------------------------------------------
  3971. align 4
  3972. calculatebackground:   ; background
  3973.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3974.         mov     eax, 0x01010101
  3975.         mov     ecx, [_display.win_map_size]
  3976.         shr     ecx, 2
  3977.         rep stosd
  3978.         mov     byte[window_data+32+WDATA.z_modif], ZPOS_DESKTOP
  3979.         mov     [REDRAW_BACKGROUND], 0
  3980.         ret
  3981. ;-----------------------------------------------------------------------------
  3982. uglobal
  3983.   imax    dd 0x0
  3984. endg
  3985. ;-----------------------------------------------------------------------------
  3986. align 4
  3987. delay_ms:     ; delay in 1/1000 sec
  3988.         pushad
  3989.  
  3990.         cmp     [hpet_base], 0
  3991.         jz      .no_hpet
  3992.         mov     eax, esi
  3993.         mov     edx, 1_000_000 ; ms to ns
  3994.         mul     edx
  3995.         mov     ebx, edx
  3996.         mov     ecx, eax
  3997.  
  3998.         push    ecx
  3999.         call    get_clock_ns
  4000.         pop     ecx
  4001.         mov     edi, edx
  4002.         mov     esi, eax
  4003. .wait:
  4004.         push    ecx
  4005.         call    get_clock_ns
  4006.         pop     ecx
  4007.         sub     eax, esi
  4008.         sbb     edx, edi
  4009.         sub     eax, ecx
  4010.         sbb     edx, ebx
  4011.         jc      .wait
  4012.         jmp     .done
  4013.  
  4014. .no_hpet:
  4015.         mov     ecx, esi
  4016.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  4017.         imul    ecx, 33941
  4018.         shr     ecx, 9
  4019.         ; </CPU clock fix>
  4020.  
  4021.         in      al, 0x61
  4022.         and     al, 0x10
  4023.         mov     ah, al
  4024.         cld
  4025.  
  4026. .cnt1:
  4027.         in      al, 0x61
  4028.         and     al, 0x10
  4029.         cmp     al, ah
  4030.         jz      .cnt1
  4031.  
  4032.         mov     ah, al
  4033.         loop    .cnt1
  4034.  
  4035. .done:
  4036.         popad
  4037.         ret
  4038. ;-----------------------------------------------------------------------------
  4039. align 4
  4040. set_app_param:
  4041.         mov     edi, [TASK_BASE]
  4042.         mov     eax, ebx
  4043.         btr     eax, 3                           ; move MOUSE_FILTRATION
  4044.         mov     ebx, [current_slot]              ; bit into event_filter
  4045.         setc    byte [ebx+APPDATA.event_filter]
  4046.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  4047.         mov     [esp+32], eax                    ; return old mask value
  4048.         ret
  4049. ;-----------------------------------------------------------------------------
  4050.  
  4051. ; this is for syscall
  4052. proc delay_hs_unprotected
  4053.         call    unprotect_from_terminate
  4054.         call    delay_hs
  4055.         call    protect_from_terminate
  4056.         ret
  4057. endp
  4058.  
  4059. if 1
  4060. align 4
  4061. delay_hs:     ; delay in 1/100 secs
  4062. ; ebx = delay time
  4063.  
  4064.         pushad
  4065.         push    ebx
  4066.         xor     esi, esi
  4067.         mov     ecx, MANUAL_DESTROY
  4068.         call    create_event
  4069.         test    eax, eax
  4070.         jz      .done
  4071.  
  4072.         mov     ebx, edx
  4073.         mov     ecx, [esp]
  4074.         push    edx
  4075.         push    eax
  4076.         call    wait_event_timeout
  4077.         pop     eax
  4078.         pop     ebx
  4079.         call    destroy_event
  4080. .done:
  4081.         add     esp, 4
  4082.         popad
  4083.         ret
  4084.  
  4085. else
  4086.  
  4087. align 4
  4088. delay_hs:     ; delay in 1/100 secs
  4089. ; ebx = delay time
  4090.         push    ecx
  4091.         push    edx
  4092.  
  4093.         mov     edx, [timer_ticks]
  4094. ;--------------------------------------
  4095. align 4
  4096. newtic:
  4097.         mov     ecx, [timer_ticks]
  4098.         sub     ecx, edx
  4099.         cmp     ecx, ebx
  4100.         jae     zerodelay
  4101.  
  4102.         call    change_task
  4103.  
  4104.         jmp     newtic
  4105. ;--------------------------------------
  4106. align 4
  4107. zerodelay:
  4108.         pop     edx
  4109.         pop     ecx
  4110.         ret
  4111. end if
  4112.  
  4113. ;-----------------------------------------------------------------------------
  4114. align 16        ;very often call this subrutine
  4115. memmove:       ; memory move in bytes
  4116. ; eax = from
  4117. ; ebx = to
  4118. ; ecx = no of bytes
  4119.         test    ecx, ecx
  4120.         jle     .ret
  4121.  
  4122.         push    esi edi ecx
  4123.  
  4124.         mov     edi, ebx
  4125.         mov     esi, eax
  4126.  
  4127.         test    ecx, not 11b
  4128.         jz      @f
  4129.  
  4130.         push    ecx
  4131.         shr     ecx, 2
  4132.         rep movsd
  4133.         pop     ecx
  4134.         and     ecx, 11b
  4135.         jz      .finish
  4136. ;--------------------------------------
  4137. align 4
  4138. @@:
  4139.         rep movsb
  4140. ;--------------------------------------
  4141. align 4
  4142. .finish:
  4143.         pop     ecx edi esi
  4144. ;--------------------------------------
  4145. align 4
  4146. .ret:
  4147.         ret
  4148. ;-----------------------------------------------------------------------------
  4149. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  4150. ;align 4
  4151. ;
  4152. ;read_floppy_file:
  4153. ;
  4154. ;; as input
  4155. ;;
  4156. ;; eax pointer to file
  4157. ;; ebx file lenght
  4158. ;; ecx start 512 byte block number
  4159. ;; edx number of blocks to read
  4160. ;; esi pointer to return/work area (atleast 20 000 bytes)
  4161. ;;
  4162. ;;
  4163. ;; on return
  4164. ;;
  4165. ;; eax = 0 command succesful
  4166. ;;       1 no fd base and/or partition defined
  4167. ;;       2 yet unsupported FS
  4168. ;;       3 unknown FS
  4169. ;;       4 partition not defined at hd
  4170. ;;       5 file not found
  4171. ;; ebx = size of file
  4172. ;
  4173. ;     mov   edi,[TASK_BASE]
  4174. ;     add   edi,0x10
  4175. ;     add   esi,[edi]
  4176. ;     add   eax,[edi]
  4177. ;
  4178. ;     pushad
  4179. ;     mov  edi,esi
  4180. ;     add  edi,1024
  4181. ;     mov  esi,0x100000+19*512
  4182. ;     sub  ecx,1
  4183. ;     shl  ecx,9
  4184. ;     add  esi,ecx
  4185. ;     shl  edx,9
  4186. ;     mov  ecx,edx
  4187. ;     cld
  4188. ;     rep  movsb
  4189. ;     popad
  4190. ;
  4191. ;     mov   [esp+36],eax
  4192. ;     mov   [esp+24],ebx
  4193. ;     ret
  4194.  
  4195.  
  4196.  
  4197. align 4
  4198. set_io_access_rights:
  4199.         push    edi eax
  4200.         mov     edi, tss._io_map_0
  4201. ;     mov   ecx,eax
  4202. ;     and   ecx,7    ; offset in byte
  4203. ;     shr   eax,3    ; number of byte
  4204. ;     add   edi,eax
  4205. ;     mov   ebx,1
  4206. ;     shl   ebx,cl
  4207.         test    ebp, ebp
  4208. ;     cmp   ebp,0                ; enable access - ebp = 0
  4209.         jnz     .siar1
  4210. ;     not   ebx
  4211. ;     and   [edi],byte bl
  4212.         btr     [edi], eax
  4213.         pop     eax edi
  4214.         ret
  4215. .siar1:
  4216.         bts     [edi], eax
  4217.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4218.         pop     eax edi
  4219.         ret
  4220. ;reserve/free group of ports
  4221. ;  * eax = 46 - number function
  4222. ;  * ebx = 0 - reserve, 1 - free
  4223. ;  * ecx = number start arrea of ports
  4224. ;  * edx = number end arrea of ports (include last number of port)
  4225. ;Return value:
  4226. ;  * eax = 0 - succesful
  4227. ;  * eax = 1 - error
  4228. ;  * The system has reserve this ports:
  4229. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  4230. ;destroys eax,ebx, ebp
  4231. r_f_port_area:
  4232.  
  4233.         test    ebx, ebx
  4234.         jnz     free_port_area
  4235. ;     je    r_port_area
  4236. ;     jmp   free_port_area
  4237.  
  4238. ;   r_port_area:
  4239.  
  4240. ;     pushad
  4241.  
  4242.         cmp     ecx, edx      ; beginning > end ?
  4243.         ja      rpal1
  4244.         cmp     edx, 65536
  4245.         jae     rpal1
  4246.         mov     eax, [RESERVED_PORTS]
  4247.         test    eax, eax      ; no reserved areas ?
  4248.         je      rpal2
  4249.         cmp     eax, 255      ; max reserved
  4250.         jae     rpal1
  4251.  rpal3:
  4252.         mov     ebx, eax
  4253.         shl     ebx, 4
  4254.         add     ebx, RESERVED_PORTS
  4255.         cmp     ecx, [ebx+8]
  4256.         ja      rpal4
  4257.         cmp     edx, [ebx+4]
  4258.         jae     rpal1
  4259. ;     jb    rpal4
  4260. ;     jmp   rpal1
  4261.  rpal4:
  4262.         dec     eax
  4263.         jnz     rpal3
  4264.         jmp     rpal2
  4265.    rpal1:
  4266. ;     popad
  4267. ;     mov   eax,1
  4268.         xor     eax, eax
  4269.         inc     eax
  4270.         ret
  4271.    rpal2:
  4272. ;     popad
  4273.      ; enable port access at port IO map
  4274.         cli
  4275.         pushad                        ; start enable io map
  4276.  
  4277.         cmp     edx, 65536;16384
  4278.         jae     no_unmask_io; jge
  4279.         mov     eax, ecx
  4280. ;       push    ebp
  4281.         xor     ebp, ebp               ; enable - eax = port
  4282. new_port_access:
  4283. ;     pushad
  4284.         call    set_io_access_rights
  4285. ;     popad
  4286.         inc     eax
  4287.         cmp     eax, edx
  4288.         jbe     new_port_access
  4289. ;       pop     ebp
  4290. no_unmask_io:
  4291.         popad                         ; end enable io map
  4292.         sti
  4293.  
  4294.         mov     eax, [RESERVED_PORTS]
  4295.         add     eax, 1
  4296.         mov     [RESERVED_PORTS], eax
  4297.         shl     eax, 4
  4298.         add     eax, RESERVED_PORTS
  4299.         mov     ebx, [TASK_BASE]
  4300.         mov     ebx, [ebx+TASKDATA.pid]
  4301.         mov     [eax], ebx
  4302.         mov     [eax+4], ecx
  4303.         mov     [eax+8], edx
  4304.  
  4305.         xor     eax, eax
  4306.         ret
  4307.  
  4308. free_port_area:
  4309.  
  4310. ;     pushad
  4311.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  4312.         test    eax, eax
  4313.         jz      frpal2
  4314.         mov     ebx, [TASK_BASE]
  4315.         mov     ebx, [ebx+TASKDATA.pid]
  4316.    frpal3:
  4317.         mov     edi, eax
  4318.         shl     edi, 4
  4319.         add     edi, RESERVED_PORTS
  4320.         cmp     ebx, [edi]
  4321.         jne     frpal4
  4322.         cmp     ecx, [edi+4]
  4323.         jne     frpal4
  4324.         cmp     edx, [edi+8]
  4325.         jne     frpal4
  4326.         jmp     frpal1
  4327.    frpal4:
  4328.         dec     eax
  4329.         jnz     frpal3
  4330.    frpal2:
  4331. ;     popad
  4332.         inc     eax
  4333.         ret
  4334.    frpal1:
  4335.         push    ecx
  4336.         mov     ecx, 256
  4337.         sub     ecx, eax
  4338.         shl     ecx, 4
  4339.         mov     esi, edi
  4340.         add     esi, 16
  4341.         cld
  4342.         rep movsb
  4343.  
  4344.         dec     dword [RESERVED_PORTS]
  4345. ;popad
  4346. ;disable port access at port IO map
  4347.  
  4348. ;     pushad                        ; start disable io map
  4349.         pop     eax     ;start port
  4350.         cmp     edx, 65536;16384
  4351.         jge     no_mask_io
  4352.  
  4353. ;     mov   eax,ecx
  4354.         xor     ebp, ebp
  4355.         inc     ebp
  4356. new_port_access_disable:
  4357. ;     pushad
  4358. ;     mov   ebp,1                  ; disable - eax = port
  4359.         call    set_io_access_rights
  4360. ;     popad
  4361.         inc     eax
  4362.         cmp     eax, edx
  4363.         jbe     new_port_access_disable
  4364. no_mask_io:
  4365. ;     popad                         ; end disable io map
  4366.         xor     eax, eax
  4367.         ret
  4368. ;-----------------------------------------------------------------------------
  4369. align 4
  4370. drawbackground:
  4371. dbrv20:
  4372.         cmp     [BgrDrawMode], dword 1
  4373.         jne     bgrstr
  4374.         call    vesa20_drawbackground_tiled
  4375. ;        call    [draw_pointer]
  4376.         call    __sys_draw_pointer
  4377.         ret
  4378. ;--------------------------------------
  4379. align 4
  4380. bgrstr:
  4381.         call    vesa20_drawbackground_stretch
  4382. ;        call    [draw_pointer]
  4383.         call    __sys_draw_pointer
  4384.         ret
  4385. ;-----------------------------------------------------------------------------
  4386. align 4
  4387. syscall_putimage:                       ; PutImage
  4388. sys_putimage:
  4389.         test    ecx, 0x80008000
  4390.         jnz     .exit
  4391.         test    ecx, 0x0000FFFF
  4392.         jz      .exit
  4393.         test    ecx, 0xFFFF0000
  4394.         jnz     @f
  4395. ;--------------------------------------
  4396. align 4
  4397. .exit:
  4398.         ret
  4399. ;--------------------------------------
  4400. align 4
  4401. @@:
  4402.         mov     edi, [current_slot]
  4403.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  4404.         rol     edx, 16
  4405.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  4406.         rol     edx, 16
  4407. ;--------------------------------------
  4408. align 4
  4409. .forced:
  4410.         push    ebp esi 0
  4411.         mov     ebp, putimage_get24bpp
  4412.         mov     esi, putimage_init24bpp
  4413. ;--------------------------------------
  4414. align 4
  4415. sys_putimage_bpp:
  4416.         call    vesa20_putimage
  4417.         pop     ebp esi ebp
  4418.         ret
  4419. ;        jmp     [draw_pointer]
  4420. ;-----------------------------------------------------------------------------
  4421. align 4
  4422. sys_putimage_palette:
  4423. ; ebx = pointer to image
  4424. ; ecx = [xsize]*65536 + [ysize]
  4425. ; edx = [xstart]*65536 + [ystart]
  4426. ; esi = number of bits per pixel, must be 8, 24 or 32
  4427. ; edi = pointer to palette
  4428. ; ebp = row delta
  4429.         mov     eax, [CURRENT_TASK]
  4430.         shl     eax, 8
  4431.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4432.         rol     edx, 16
  4433.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4434.         rol     edx, 16
  4435. ;--------------------------------------
  4436. align 4
  4437. .forced:
  4438.         cmp     esi, 1
  4439.         jnz     @f
  4440.         push    edi
  4441.         mov     eax, [edi+4]
  4442.         sub     eax, [edi]
  4443.         push    eax
  4444.         push    dword [edi]
  4445.         push    0ffffff80h
  4446.         mov     edi, esp
  4447.         call    put_mono_image
  4448.         add     esp, 12
  4449.         pop     edi
  4450.         ret
  4451. ;--------------------------------------
  4452. align 4
  4453. @@:
  4454.         cmp     esi, 2
  4455.         jnz     @f
  4456.         push    edi
  4457.         push    0ffffff80h
  4458.         mov     edi, esp
  4459.         call    put_2bit_image
  4460.         pop     eax
  4461.         pop     edi
  4462.         ret
  4463. ;--------------------------------------
  4464. align 4
  4465. @@:
  4466.         cmp     esi, 4
  4467.         jnz     @f
  4468.         push    edi
  4469.         push    0ffffff80h
  4470.         mov     edi, esp
  4471.         call    put_4bit_image
  4472.         pop     eax
  4473.         pop     edi
  4474.         ret
  4475. ;--------------------------------------
  4476. align 4
  4477. @@:
  4478.         push    ebp esi ebp
  4479.         cmp     esi, 8
  4480.         jnz     @f
  4481.         mov     ebp, putimage_get8bpp
  4482.         mov     esi, putimage_init8bpp
  4483.         jmp     sys_putimage_bpp
  4484. ;--------------------------------------
  4485. align 4
  4486. @@:
  4487.         cmp     esi, 9
  4488.         jnz     @f
  4489.         mov     ebp, putimage_get9bpp
  4490.         mov     esi, putimage_init9bpp
  4491.         jmp     sys_putimage_bpp
  4492. ;--------------------------------------
  4493. align 4
  4494. @@:
  4495.         cmp     esi, 15
  4496.         jnz     @f
  4497.         mov     ebp, putimage_get15bpp
  4498.         mov     esi, putimage_init15bpp
  4499.         jmp     sys_putimage_bpp
  4500. ;--------------------------------------
  4501. align 4
  4502. @@:
  4503.         cmp     esi, 16
  4504.         jnz     @f
  4505.         mov     ebp, putimage_get16bpp
  4506.         mov     esi, putimage_init16bpp
  4507.         jmp     sys_putimage_bpp
  4508. ;--------------------------------------
  4509. align 4
  4510. @@:
  4511.         cmp     esi, 24
  4512.         jnz     @f
  4513.         mov     ebp, putimage_get24bpp
  4514.         mov     esi, putimage_init24bpp
  4515.         jmp     sys_putimage_bpp
  4516. ;--------------------------------------
  4517. align 4
  4518. @@:
  4519.         cmp     esi, 32
  4520.         jnz     @f
  4521.         mov     ebp, putimage_get32bpp
  4522.         mov     esi, putimage_init32bpp
  4523.         jmp     sys_putimage_bpp
  4524. ;--------------------------------------
  4525. align 4
  4526. @@:
  4527.         pop     ebp esi ebp
  4528.         ret
  4529. ;-----------------------------------------------------------------------------
  4530. align 4
  4531. put_mono_image:
  4532.         push    ebp esi ebp
  4533.         mov     ebp, putimage_get1bpp
  4534.         mov     esi, putimage_init1bpp
  4535.         jmp     sys_putimage_bpp
  4536. ;-----------------------------------------------------------------------------
  4537. align 4
  4538. put_2bit_image:
  4539.         push    ebp esi ebp
  4540.         mov     ebp, putimage_get2bpp
  4541.         mov     esi, putimage_init2bpp
  4542.         jmp     sys_putimage_bpp
  4543. ;-----------------------------------------------------------------------------
  4544. align 4
  4545. put_4bit_image:
  4546.         push    ebp esi ebp
  4547.         mov     ebp, putimage_get4bpp
  4548.         mov     esi, putimage_init4bpp
  4549.         jmp     sys_putimage_bpp
  4550. ;-----------------------------------------------------------------------------
  4551. align 4
  4552. putimage_init24bpp:
  4553.         lea     eax, [eax*3]
  4554. putimage_init8bpp:
  4555. putimage_init9bpp:
  4556.         ret
  4557. ;-----------------------------------------------------------------------------
  4558. align 16
  4559. putimage_get24bpp:
  4560.         movzx   eax, byte [esi+2]
  4561.         shl     eax, 16
  4562.         mov     ax, [esi]
  4563.         add     esi, 3
  4564.         ret     4
  4565. ;-----------------------------------------------------------------------------
  4566. align 16
  4567. putimage_get8bpp:
  4568.         movzx   eax, byte [esi]
  4569.         push    edx
  4570.         mov     edx, [esp+8]
  4571.         mov     eax, [edx+eax*4]
  4572.         pop     edx
  4573.         inc     esi
  4574.         ret     4
  4575. ;-----------------------------------------------------------------------------
  4576. align 16
  4577. putimage_get9bpp:
  4578.         lodsb
  4579.         mov     ah, al
  4580.         shl     eax, 8
  4581.         mov     al, ah
  4582.         ret     4
  4583. ;-----------------------------------------------------------------------------
  4584. align 4
  4585. putimage_init1bpp:
  4586.         add     eax, ecx
  4587.         push    ecx
  4588.         add     eax, 7
  4589.         add     ecx, 7
  4590.         shr     eax, 3
  4591.         shr     ecx, 3
  4592.         sub     eax, ecx
  4593.         pop     ecx
  4594.         ret
  4595. ;-----------------------------------------------------------------------------
  4596. align 16
  4597. putimage_get1bpp:
  4598.         push    edx
  4599.         mov     edx, [esp+8]
  4600.         mov     al, [edx]
  4601.         add     al, al
  4602.         jnz     @f
  4603.         lodsb
  4604.         adc     al, al
  4605. @@:
  4606.         mov     [edx], al
  4607.         sbb     eax, eax
  4608.         and     eax, [edx+8]
  4609.         add     eax, [edx+4]
  4610.         pop     edx
  4611.         ret     4
  4612. ;-----------------------------------------------------------------------------
  4613. align 4
  4614. putimage_init2bpp:
  4615.         add     eax, ecx
  4616.         push    ecx
  4617.         add     ecx, 3
  4618.         add     eax, 3
  4619.         shr     ecx, 2
  4620.         shr     eax, 2
  4621.         sub     eax, ecx
  4622.         pop     ecx
  4623.         ret
  4624. ;-----------------------------------------------------------------------------
  4625. align 16
  4626. putimage_get2bpp:
  4627.         push    edx
  4628.         mov     edx, [esp+8]
  4629.         mov     al, [edx]
  4630.         mov     ah, al
  4631.         shr     al, 6
  4632.         shl     ah, 2
  4633.         jnz     .nonewbyte
  4634.         lodsb
  4635.         mov     ah, al
  4636.         shr     al, 6
  4637.         shl     ah, 2
  4638.         add     ah, 1
  4639. .nonewbyte:
  4640.         mov     [edx], ah
  4641.         mov     edx, [edx+4]
  4642.         movzx   eax, al
  4643.         mov     eax, [edx+eax*4]
  4644.         pop     edx
  4645.         ret     4
  4646. ;-----------------------------------------------------------------------------
  4647. align 4
  4648. putimage_init4bpp:
  4649.         add     eax, ecx
  4650.         push    ecx
  4651.         add     ecx, 1
  4652.         add     eax, 1
  4653.         shr     ecx, 1
  4654.         shr     eax, 1
  4655.         sub     eax, ecx
  4656.         pop     ecx
  4657.         ret
  4658. ;-----------------------------------------------------------------------------
  4659. align 16
  4660. putimage_get4bpp:
  4661.         push    edx
  4662.         mov     edx, [esp+8]
  4663.         add     byte [edx], 80h
  4664.         jc      @f
  4665.         movzx   eax, byte [edx+1]
  4666.         mov     edx, [edx+4]
  4667.         and     eax, 0x0F
  4668.         mov     eax, [edx+eax*4]
  4669.         pop     edx
  4670.         ret     4
  4671. @@:
  4672.         movzx   eax, byte [esi]
  4673.         add     esi, 1
  4674.         mov     [edx+1], al
  4675.         shr     eax, 4
  4676.         mov     edx, [edx+4]
  4677.         mov     eax, [edx+eax*4]
  4678.         pop     edx
  4679.         ret     4
  4680. ;-----------------------------------------------------------------------------
  4681. align 4
  4682. putimage_init32bpp:
  4683.         shl     eax, 2
  4684.         ret
  4685. ;-----------------------------------------------------------------------------
  4686. align 16
  4687. putimage_get32bpp:
  4688.         lodsd
  4689.         ret     4
  4690. ;-----------------------------------------------------------------------------
  4691. align 4
  4692. putimage_init15bpp:
  4693. putimage_init16bpp:
  4694.         add     eax, eax
  4695.         ret
  4696. ;-----------------------------------------------------------------------------
  4697. align 16
  4698. putimage_get15bpp:
  4699. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4700.         push    ecx edx
  4701.         movzx   eax, word [esi]
  4702.         add     esi, 2
  4703.         mov     ecx, eax
  4704.         mov     edx, eax
  4705.         and     eax, 0x1F
  4706.         and     ecx, 0x1F shl 5
  4707.         and     edx, 0x1F shl 10
  4708.         shl     eax, 3
  4709.         shl     ecx, 6
  4710.         shl     edx, 9
  4711.         or      eax, ecx
  4712.         or      eax, edx
  4713.         pop     edx ecx
  4714.         ret     4
  4715. ;-----------------------------------------------------------------------------
  4716. align 16
  4717. putimage_get16bpp:
  4718. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4719.         push    ecx edx
  4720.         movzx   eax, word [esi]
  4721.         add     esi, 2
  4722.         mov     ecx, eax
  4723.         mov     edx, eax
  4724.         and     eax, 0x1F
  4725.         and     ecx, 0x3F shl 5
  4726.         and     edx, 0x1F shl 11
  4727.         shl     eax, 3
  4728.         shl     ecx, 5
  4729.         shl     edx, 8
  4730.         or      eax, ecx
  4731.         or      eax, edx
  4732.         pop     edx ecx
  4733.         ret     4
  4734. ;-----------------------------------------------------------------------------
  4735. ;align 4
  4736. ; eax x beginning
  4737. ; ebx y beginning
  4738. ; ecx x end
  4739.         ; edx y end
  4740. ; edi color
  4741. ;__sys_drawbar:
  4742. ;        mov     esi, [current_slot]
  4743. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4744. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4745. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4746. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4747. ;--------------------------------------
  4748. ;align 4
  4749. ;.forced:
  4750. ;        call    vesa20_drawbar
  4751. ;        call    [draw_pointer]
  4752. ;        ret
  4753. ;-----------------------------------------------------------------------------
  4754. align 4
  4755. kb_write_wait_ack:
  4756.  
  4757.         push    ecx edx
  4758.  
  4759.         mov     dl, al
  4760.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4761. .wait_output_ready:
  4762.         in      al, 0x64
  4763.         test    al, 2
  4764.         jz      @f
  4765.         loop    .wait_output_ready
  4766.         mov     ah, 1
  4767.         jmp     .nothing
  4768. @@:
  4769.         mov     al, dl
  4770.         out     0x60, al
  4771.         mov     ecx, 0xfffff; last 0xffff, new value in view of fast CPU's
  4772. .wait_ack:
  4773.         in      al, 0x64
  4774.         test    al, 1
  4775.         jnz     @f
  4776.         loop    .wait_ack
  4777.         mov     ah, 1
  4778.         jmp     .nothing
  4779. @@:
  4780.         in      al, 0x60
  4781.         xor     ah, ah
  4782.  
  4783. .nothing:
  4784.         pop     edx ecx
  4785.  
  4786.         ret
  4787. ;-----------------------------------------------------------------------------
  4788.  
  4789. setmouse:  ; set mousepicture -pointer
  4790.            ; ps2 mouse enable
  4791.  
  4792. ;        mov     [MOUSE_PICTURE], dword mousepointer
  4793.  
  4794.         cli
  4795.  
  4796.         ret
  4797.  
  4798. if used _rdtsc
  4799. _rdtsc:
  4800.         bt      [cpu_caps], CAPS_TSC
  4801.         jnc     ret_rdtsc
  4802.         rdtsc
  4803.         ret
  4804.    ret_rdtsc:
  4805.         mov     edx, 0xffffffff
  4806.         mov     eax, 0xffffffff
  4807.         ret
  4808. end if
  4809.  
  4810. sys_msg_board_str:
  4811.  
  4812.         pushad
  4813.    @@:
  4814.         cmp     [esi], byte 0
  4815.         je      @f
  4816.         mov     ebx, 1
  4817.         movzx   ecx, byte [esi]
  4818.         call    sys_msg_board
  4819.         inc     esi
  4820.         jmp     @b
  4821.    @@:
  4822.         popad
  4823.         ret
  4824.  
  4825. sys_msg_board_byte:
  4826. ; in: al = byte to display
  4827. ; out: nothing
  4828. ; destroys: nothing
  4829.         pushad
  4830.         mov     ecx, 2
  4831.         shl     eax, 24
  4832.         jmp     @f
  4833.  
  4834. sys_msg_board_word:
  4835. ; in: ax = word to display
  4836. ; out: nothing
  4837. ; destroys: nothing
  4838.         pushad
  4839.         mov     ecx, 4
  4840.         shl     eax, 16
  4841.         jmp     @f
  4842.  
  4843. sys_msg_board_dword:
  4844. ; in: eax = dword to display
  4845. ; out: nothing
  4846. ; destroys: nothing
  4847.         pushad
  4848.         mov     ecx, 8
  4849. @@:
  4850.         push    ecx
  4851.         rol     eax, 4
  4852.         push    eax
  4853.         and     al, 0xF
  4854.         cmp     al, 10
  4855.         sbb     al, 69h
  4856.         das
  4857.         mov     cl, al
  4858.         xor     ebx, ebx
  4859.         inc     ebx
  4860.         call    sys_msg_board
  4861.         pop     eax
  4862.         pop     ecx
  4863.         loop    @b
  4864.         popad
  4865.         ret
  4866.  
  4867. msg_board_data_size = 65536 ; Must be power of two
  4868.  
  4869. uglobal
  4870. msg_board_data  rb  msg_board_data_size
  4871. msg_board_count dd  ?
  4872. endg
  4873.  
  4874. iglobal
  4875. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4876. endg
  4877.  
  4878. sys_msg_board:
  4879. ; ebx=1 -> write, cl = byte to write
  4880. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4881.         push    eax ebx
  4882.         mov     eax, ebx
  4883.         mov     ebx, ecx
  4884.         mov     ecx, [msg_board_count]
  4885.         cmp     eax, 1
  4886.         jne     .read
  4887.  
  4888. if defined debug_com_base
  4889.         push    dx ax
  4890. @@: ; wait for empty transmit register
  4891.         mov     dx, debug_com_base+5
  4892.         in      al, dx
  4893.         test    al, 1 shl 5
  4894.         jz      @r
  4895.         mov     dx, debug_com_base      ; Output the byte
  4896.         mov     al, bl
  4897.         out     dx, al
  4898.         pop     ax dx
  4899. end if
  4900.  
  4901.         mov     [msg_board_data+ecx], bl
  4902.         cmp     byte [debug_direct_print], 1
  4903.         jnz     .end
  4904.         pusha
  4905.         lea     edx, [msg_board_data+ecx]
  4906.         mov     ecx, 0x40FFFFFF
  4907.         mov     ebx, [msg_board_pos]
  4908.         mov     edi, 1
  4909.         mov     esi, 1
  4910.         call    dtext
  4911.         popa
  4912.         add     word [msg_board_pos+2], 6
  4913.         cmp     word [msg_board_pos+2], 105*6
  4914.         jnc     @f
  4915.         cmp     bl, 10
  4916.         jnz     .end
  4917. @@:
  4918.         mov     word [msg_board_pos+2], 42*6
  4919.         add     word [msg_board_pos], 10
  4920.         mov     eax, [_display.height]
  4921.         sub     eax, 10
  4922.         cmp     ax, word [msg_board_pos]
  4923.         jnc     @f
  4924.         mov     word [msg_board_pos], 10
  4925. @@:
  4926.         pusha
  4927.         mov     eax, [msg_board_pos]
  4928.         movzx   ebx, ax
  4929.         shr     eax, 16
  4930.         mov     edx, 105*6
  4931.         xor     ecx, ecx
  4932.         mov     edi, 1
  4933.         mov     esi, 9
  4934. @@:
  4935.         call    hline
  4936.         inc     ebx
  4937.         dec     esi
  4938.         jnz     @b
  4939.         popa
  4940. .end:
  4941.         inc     ecx
  4942.         and     ecx, msg_board_data_size - 1
  4943.         mov     [msg_board_count], ecx
  4944. .ret:
  4945.         pop     ebx eax
  4946.         ret
  4947.  
  4948. @@:
  4949.         mov     [esp+32], ecx
  4950.         mov     [esp+20], ecx
  4951.         jmp     .ret
  4952.  
  4953. .read:
  4954.         cmp     eax, 2
  4955.         jne     .ret
  4956.         test    ecx, ecx
  4957.         jz      @b
  4958.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4959.         mov     eax, msg_board_data+1
  4960.         mov     ebx, msg_board_data
  4961.         movzx   edx, byte [ebx]
  4962.         call    memmove
  4963.         dec     [msg_board_count]
  4964.         mov     [esp + 32], edx ;eax
  4965.         mov     [esp + 20], dword 1
  4966.         ret
  4967.  
  4968. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4969. ;; 66 sys function.                                                ;;
  4970. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4971. ;; out eax                                                         ;;
  4972. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4973. iglobal
  4974. align 4
  4975. f66call:
  4976.            dd sys_process_def.1   ; 1 = set keyboard mode
  4977.            dd sys_process_def.2   ; 2 = get keyboard mode
  4978.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4979.            dd sys_process_def.4   ; 4 = set system-wide hotkey
  4980.            dd sys_process_def.5   ; 5 = delete installed hotkey
  4981.            dd sys_process_def.6   ; 6 = disable input, work only hotkeys
  4982.            dd sys_process_def.7   ; 7 = enable input, opposition to f.66.6
  4983. endg
  4984. ;-----------------------------------------------------------------------------
  4985. align 4
  4986. sys_process_def:
  4987.         dec     ebx
  4988.         cmp     ebx, 7
  4989.         jae     .not_support    ;if >=8 then or eax,-1
  4990.  
  4991.         mov     edi, [CURRENT_TASK]
  4992.         jmp     dword [f66call+ebx*4]
  4993.  
  4994. .not_support:
  4995.         or      eax, -1
  4996.         ret
  4997. ;-----------------------------------------------------------------------------
  4998. align 4
  4999. .1:
  5000.         shl     edi, 8
  5001.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  5002.  
  5003.         ret
  5004. ;-----------------------------------------------------------------------------
  5005. align 4
  5006. .2:                             ; 2 = get keyboard mode
  5007.         shl     edi, 8
  5008.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  5009.         mov     [esp+32], eax
  5010.         ret
  5011. ;-----------------------------------------------------------------------------
  5012. align 4
  5013. .3:                             ;3 = get keyboard ctrl, alt, shift
  5014.         mov     eax, [kb_state]
  5015.         mov     [esp+32], eax
  5016.         ret
  5017. ;-----------------------------------------------------------------------------
  5018. align 4
  5019. .4:
  5020.         mov     eax, hotkey_list
  5021. @@:
  5022.         cmp     dword [eax+8], 0
  5023.         jz      .found_free
  5024.         add     eax, 16
  5025.         cmp     eax, hotkey_list+16*256
  5026.         jb      @b
  5027.         mov     dword [esp+32], 1
  5028.         ret
  5029. .found_free:
  5030.         mov     [eax+8], edi
  5031.         mov     [eax+4], edx
  5032.         movzx   ecx, cl
  5033.         lea     ecx, [hotkey_scancodes+ecx*4]
  5034.         mov     edx, [ecx]
  5035.         mov     [eax], edx
  5036.         mov     [ecx], eax
  5037.         mov     [eax+12], ecx
  5038.         test    edx, edx
  5039.         jz      @f
  5040.         mov     [edx+12], eax
  5041. @@:
  5042.         and     dword [esp+32], 0
  5043.         ret
  5044. ;-----------------------------------------------------------------------------
  5045. align 4
  5046. .5:
  5047.         movzx   ebx, cl
  5048.         lea     ebx, [hotkey_scancodes+ebx*4]
  5049.         mov     eax, [ebx]
  5050. .scan:
  5051.         test    eax, eax
  5052.         jz      .notfound
  5053.         cmp     [eax+8], edi
  5054.         jnz     .next
  5055.         cmp     [eax+4], edx
  5056.         jz      .found
  5057. .next:
  5058.         mov     eax, [eax]
  5059.         jmp     .scan
  5060. .notfound:
  5061.         mov     dword [esp+32], 1
  5062.         ret
  5063. .found:
  5064.         mov     ecx, [eax]
  5065.         jecxz   @f
  5066.         mov     edx, [eax+12]
  5067.         mov     [ecx+12], edx
  5068. @@:
  5069.         mov     ecx, [eax+12]
  5070.         mov     edx, [eax]
  5071.         mov     [ecx], edx
  5072.         xor     edx, edx
  5073.         mov     [eax+4], edx
  5074.         mov     [eax+8], edx
  5075.         mov     [eax+12], edx
  5076.         mov     [eax], edx
  5077.         mov     [esp+32], edx
  5078.         ret
  5079. ;-----------------------------------------------------------------------------
  5080. align 4
  5081. .6:
  5082.         pushfd
  5083.         cli
  5084.         mov     eax, [PID_lock_input]
  5085.         test    eax, eax
  5086.         jnz     @f
  5087. ; get current PID
  5088.         mov     eax, [CURRENT_TASK]
  5089.         shl     eax, 5
  5090.         mov     eax, [eax+CURRENT_TASK+TASKDATA.pid]
  5091. ; set current PID for lock input
  5092.         mov     [PID_lock_input], eax
  5093. @@:
  5094.         popfd
  5095.         ret
  5096. ;-----------------------------------------------------------------------------
  5097. align 4
  5098. .7:
  5099.         mov     eax, [PID_lock_input]
  5100.         test    eax, eax
  5101.         jz      @f
  5102. ; get current PID
  5103.         mov     ebx, [CURRENT_TASK]
  5104.         shl     ebx, 5
  5105.         mov     ebx, [ebx+CURRENT_TASK+TASKDATA.pid]
  5106. ; compare current lock input with current PID
  5107.         cmp     ebx, eax
  5108.         jne     @f
  5109.  
  5110.         xor     eax, eax
  5111.         mov     [PID_lock_input], eax
  5112. @@:
  5113.         ret
  5114. ;-----------------------------------------------------------------------------
  5115. uglobal
  5116.   PID_lock_input dd 0x0
  5117. endg
  5118. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5119. ;; 61 sys function.                                                ;;
  5120. ;; in eax=61,ebx in [1..3]                                         ;;
  5121. ;; out eax                                                         ;;
  5122. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5123. iglobal
  5124. align 4
  5125. f61call:
  5126.            dd sys_gs.1   ; resolution
  5127.            dd sys_gs.2   ; bits per pixel
  5128.            dd sys_gs.3   ; bytes per scanline
  5129. endg
  5130.  
  5131.  
  5132. align 4
  5133.  
  5134. sys_gs:                         ; direct screen access
  5135.         dec     ebx
  5136.         cmp     ebx, 2
  5137.         ja      .not_support
  5138.         jmp     dword [f61call+ebx*4]
  5139. .not_support:
  5140.         or      [esp+32], dword -1
  5141.         ret
  5142.  
  5143.  
  5144. .1:                             ; resolution
  5145.         mov     eax, [_display.width]
  5146.         shl     eax, 16
  5147.         mov     ax, word [_display.height]
  5148.         mov     [esp+32], eax
  5149.         ret
  5150. .2:                             ; bits per pixel
  5151.         mov     eax, [_display.bits_per_pixel]
  5152.         mov     [esp+32], eax
  5153.         ret
  5154. .3:                             ; bytes per scanline
  5155.         mov     eax, [_display.lfb_pitch]
  5156.         mov     [esp+32], eax
  5157.         ret
  5158.  
  5159. align 4  ;  system functions
  5160.  
  5161. syscall_setpixel:                       ; SetPixel
  5162.  
  5163.         mov     eax, ebx
  5164.         mov     ebx, ecx
  5165.         mov     ecx, edx
  5166.         mov     edx, [TASK_BASE]
  5167.         add     eax, [edx-twdw+WDATA.box.left]
  5168.         add     ebx, [edx-twdw+WDATA.box.top]
  5169.         mov     edi, [current_slot]
  5170.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  5171.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  5172.         xor     edi, edi ; no force
  5173.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
  5174. ;        jmp     [putpixel]
  5175.         jmp     __sys_putpixel
  5176.  
  5177. align 4
  5178.  
  5179. syscall_writetext:                      ; WriteText
  5180.  
  5181.         mov     eax, [TASK_BASE]
  5182.         mov     ebp, [eax-twdw+WDATA.box.left]
  5183.         push    esi
  5184.         mov     esi, [current_slot]
  5185.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5186.         shl     ebp, 16
  5187.         add     ebp, [eax-twdw+WDATA.box.top]
  5188.         add     bp, word[esi+APPDATA.wnd_clientbox.top]
  5189.         pop     esi
  5190.         test    ecx, 0x08000000  ; redirect the output to the user area
  5191.         jnz     @f
  5192.         add     ebx, ebp
  5193. align 4
  5194. @@:
  5195.         mov     eax, edi
  5196.         test    ecx, 0x08000000  ; redirect the output to the user area
  5197.         jnz     dtext
  5198.         xor     edi, edi
  5199.         jmp     dtext
  5200.  
  5201. align 4
  5202.  
  5203. syscall_drawrect:                       ; DrawRect
  5204.  
  5205.         mov     edi, edx ; color + gradient
  5206.         and     edi, 0x80FFFFFF
  5207.         test    bx, bx  ; x.size
  5208.         je      .drectr
  5209.         test    cx, cx ; y.size
  5210.         je      .drectr
  5211.  
  5212.         mov     eax, ebx ; bad idea
  5213.         mov     ebx, ecx
  5214.  
  5215.         movzx   ecx, ax ; ecx - x.size
  5216.         shr     eax, 16 ; eax - x.coord
  5217.         movzx   edx, bx ; edx - y.size
  5218.         shr     ebx, 16 ; ebx - y.coord
  5219.         mov     esi, [current_slot]
  5220.  
  5221.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  5222.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  5223.         add     ecx, eax
  5224.         add     edx, ebx
  5225. ;        jmp     [drawbar]
  5226.         jmp     vesa20_drawbar
  5227. .drectr:
  5228.         ret
  5229.  
  5230. align 4
  5231. syscall_getscreensize:                  ; GetScreenSize
  5232.         mov     ax, word [_display.width]
  5233.         dec     ax
  5234.         shl     eax, 16
  5235.         mov     ax, word [_display.height]
  5236.         dec     ax
  5237.         mov     [esp + 32], eax
  5238.         ret
  5239. ;-----------------------------------------------------------------------------
  5240. align 4
  5241. syscall_cdaudio:
  5242. ; ECX - position of CD/DVD-drive
  5243. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  5244. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  5245. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  5246.         cmp     ecx, 11
  5247.         ja      .exit
  5248.  
  5249.         mov     eax, ecx
  5250.         shr     eax, 2
  5251.         lea     eax, [eax*5]
  5252.         mov     al, [eax+DRIVE_DATA+1]
  5253.  
  5254.         push    ecx ebx
  5255.         mov     ebx, ecx
  5256.         and     ebx, 11b
  5257.         shl     ebx, 1
  5258.         mov     cl, 6
  5259.         sub     cl, bl
  5260.         shr     al, cl
  5261.         test    al, 2 ; it's not an ATAPI device
  5262.         pop     ebx ecx
  5263.  
  5264.         jz      .exit
  5265.  
  5266.         cmp     ebx, 4
  5267.         je      .eject
  5268.  
  5269.         cmp     ebx, 5
  5270.         je      .load
  5271. ;--------------------------------------
  5272. .exit:
  5273.         ret
  5274. ;--------------------------------------
  5275. .load:
  5276.         call    .reserve
  5277.         call    LoadMedium
  5278.         jmp     .free
  5279. ;--------------------------------------
  5280. .eject:
  5281.         call    .reserve
  5282.         call    clear_CD_cache
  5283.         call    allow_medium_removal
  5284.         call    EjectMedium
  5285.         jmp     .free
  5286. ;--------------------------------------
  5287. .reserve:
  5288.         call    reserve_cd
  5289.  
  5290.         mov     ebx, ecx
  5291.         inc     ebx
  5292.         mov     [cdpos], ebx
  5293.  
  5294.         mov     eax, ecx
  5295.         shr     eax, 1
  5296.         and     eax, 1
  5297.         inc     eax
  5298.         mov     [ChannelNumber], al
  5299.         mov     eax, ecx
  5300.         and     eax, 1
  5301.         mov     [DiskNumber], al
  5302.         call    reserve_cd_channel
  5303.         ret
  5304. ;--------------------------------------
  5305. .free:
  5306.         call    free_cd_channel
  5307.         and     [cd_status], 0
  5308.         ret
  5309. ;-----------------------------------------------------------------------------
  5310. align 4
  5311. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  5312.         cmp     ebx, [_display.width]
  5313.         jb      @f
  5314.         cmp     ecx, [_display.height]
  5315.         jb      @f
  5316.         xor     eax, eax
  5317.         jmp     .store
  5318. ;--------------------------------------
  5319. align 4
  5320. @@:
  5321.         mov     eax, [d_width_calc_area + ecx*4]
  5322.         add     eax, [_display.win_map]
  5323.         movzx   eax, byte[eax+ebx]        ; get value for current point
  5324. ;--------------------------------------
  5325. align 4
  5326. .store:
  5327.         mov     [esp + 32], eax
  5328.         ret
  5329. ;-----------------------------------------------------------------------------
  5330. align 4
  5331. syscall_getpixel:                       ; GetPixel
  5332.         mov     ecx, [_display.width]
  5333.         xor     edx, edx
  5334.         mov     eax, ebx
  5335.         div     ecx
  5336.         mov     ebx, edx
  5337.         xchg    eax, ebx
  5338.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5339.         call    dword [GETPIXEL]; eax - x, ebx - y
  5340.         mov     [esp + 32], ecx
  5341.         ret
  5342. ;-----------------------------------------------------------------------------
  5343. align 4
  5344. syscall_getarea:
  5345. ;eax = 36
  5346. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5347. ;ecx = [size x]*65536 + [size y]
  5348. ;edx = [start x]*65536 + [start y]
  5349.         pushad
  5350.         mov     edi, ebx
  5351.         mov     eax, edx
  5352.         shr     eax, 16
  5353.         mov     ebx, edx
  5354.         and     ebx, 0xffff
  5355.         dec     eax
  5356.         dec     ebx
  5357.      ; eax - x, ebx - y
  5358.         mov     edx, ecx
  5359.  
  5360.         shr     ecx, 16
  5361.         and     edx, 0xffff
  5362.         mov     esi, ecx
  5363.      ; ecx - size x, edx - size y
  5364.  
  5365.         mov     ebp, edx
  5366.         lea     ebp, [ebp*3]
  5367.         imul    ebp, esi
  5368.         stdcall is_region_userspace, edi, ebp
  5369.         jz      .exit
  5370.  
  5371.         mov     ebp, edx
  5372.         dec     ebp
  5373.         lea     ebp, [ebp*3]
  5374.  
  5375.         imul    ebp, esi
  5376.  
  5377.         mov     esi, ecx
  5378.         dec     esi
  5379.         lea     esi, [esi*3]
  5380.  
  5381.         add     ebp, esi
  5382.         add     ebp, edi
  5383.  
  5384.         add     ebx, edx
  5385. ;--------------------------------------
  5386. align 4
  5387. .start_y:
  5388.         push    ecx edx
  5389. ;--------------------------------------
  5390. align 4
  5391. .start_x:
  5392.         push    eax ebx ecx
  5393.         add     eax, ecx
  5394.  
  5395.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5396.         call    dword [GETPIXEL]; eax - x, ebx - y
  5397.  
  5398.         mov     [ebp], cx
  5399.         shr     ecx, 16
  5400.         mov     [ebp+2], cl
  5401.  
  5402.         pop     ecx ebx eax
  5403.         sub     ebp, 3
  5404.         dec     ecx
  5405.         jnz     .start_x
  5406.         pop     edx ecx
  5407.         dec     ebx
  5408.         dec     edx
  5409.         jnz     .start_y
  5410.        
  5411. .exit:
  5412.         popad
  5413.         ret
  5414. ;-----------------------------------------------------------------------------
  5415. align 4
  5416. syscall_putarea_backgr:
  5417. ;eax = 25
  5418. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5419. ;ecx = [size x]*65536 + [size y]
  5420. ;edx = [start x]*65536 + [start y]
  5421.         pushad
  5422.         mov     edi, ebx
  5423.         mov     eax, edx
  5424.         shr     eax, 16
  5425.         mov     ebx, edx
  5426.         and     ebx, 0xffff
  5427.         dec     eax
  5428.         dec     ebx
  5429. ; eax - x, ebx - y
  5430.         mov     edx, ecx
  5431.         shr     ecx, 16
  5432.         and     edx, 0xffff
  5433.         mov     esi, ecx
  5434. ; ecx - size x, edx - size y
  5435.         mov     ebp, edx
  5436.        
  5437.         lea     ebp, [ebp*4]
  5438.         imul    ebp, esi
  5439.         stdcall is_region_userspace, edi, ebp
  5440.         jz      .exit
  5441.  
  5442.         mov     ebp, edx
  5443.  
  5444.         dec     ebp
  5445.         shl     ebp, 2
  5446.  
  5447.         imul    ebp, esi
  5448.  
  5449.         mov     esi, ecx
  5450.         dec     esi
  5451.         shl     esi, 2
  5452.  
  5453.         add     ebp, esi
  5454.         add     ebp, edi
  5455.  
  5456.         add     ebx, edx
  5457. ;--------------------------------------
  5458. align 4
  5459. .start_y:
  5460.         push    ecx edx
  5461. ;--------------------------------------
  5462. align 4
  5463. .start_x:
  5464.         push    eax ecx
  5465.         add     eax, ecx
  5466.  
  5467.         mov     ecx, [ebp]
  5468.         rol     ecx, 8
  5469.         test    cl, cl        ; transparensy = 0
  5470.         jz      .no_put
  5471.  
  5472.         xor     cl, cl
  5473.         ror     ecx, 8
  5474.  
  5475.         pushad
  5476.         mov     edx, [d_width_calc_area + ebx*4]
  5477.         add     edx, [_display.win_map]
  5478.         movzx   edx, byte [eax+edx]
  5479.         cmp     dl, byte 1
  5480.         jne     @f
  5481.  
  5482.         call    dword [PUTPIXEL]; eax - x, ebx - y
  5483. ;--------------------------------------
  5484. align 4
  5485. @@:
  5486.         popad
  5487. ;--------------------------------------
  5488. align 4
  5489. .no_put:
  5490.         pop     ecx eax
  5491.  
  5492.         sub     ebp, 4
  5493.         dec     ecx
  5494.         jnz     .start_x
  5495.  
  5496.         pop     edx ecx
  5497.         dec     ebx
  5498.         dec     edx
  5499.         jnz     .start_y
  5500.  
  5501. .exit:
  5502.         popad
  5503.         ret
  5504. ;-----------------------------------------------------------------------------
  5505. align 4
  5506. syscall_drawline:                       ; DrawLine
  5507.  
  5508.         mov     edi, [TASK_BASE]
  5509.         movzx   eax, word[edi-twdw+WDATA.box.left]
  5510.         mov     ebp, eax
  5511.         mov     esi, [current_slot]
  5512.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5513.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  5514.         add     ebp, ebx
  5515.         shl     eax, 16
  5516.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  5517.         add     eax, ebp
  5518.         mov     ebp, ebx
  5519.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  5520.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  5521.         add     ebp, ecx
  5522.         shl     ebx, 16
  5523.         xor     edi, edi
  5524.         add     ebx, ebp
  5525.         mov     ecx, edx
  5526. ;        jmp     [draw_line]
  5527.         jmp     __sys_draw_line
  5528.  
  5529.  
  5530. align 4
  5531. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5532.  
  5533.         call    r_f_port_area
  5534.         mov     [esp+32], eax
  5535.         ret
  5536.  
  5537. align 4
  5538. syscall_threads:                        ; CreateThreads
  5539. ;
  5540. ;   ecx=thread entry point
  5541. ;   edx=thread stack pointer
  5542. ;
  5543. ; on return : eax = pid
  5544.  
  5545.         xor     ebx, ebx
  5546.         call    new_sys_threads
  5547.  
  5548.         mov     [esp+32], eax
  5549.         ret
  5550.  
  5551. align 4
  5552.  
  5553. paleholder:
  5554.         ret
  5555. ;------------------------------------------------------------------------------
  5556. align 4
  5557. calculate_fast_getting_offset_for_WinMapAddress:
  5558. ; calculate data area for fast getting offset to _WinMapAddress
  5559.         xor     eax, eax
  5560.         mov     ecx, [_display.height]
  5561.         mov     edi, d_width_calc_area
  5562.         cld
  5563. @@:
  5564.         stosd
  5565.         add     eax, [_display.width]
  5566.         dec     ecx
  5567.         jnz     @r
  5568.         ret
  5569. ;------------------------------------------------------------------------------
  5570. align 4
  5571. calculate_fast_getting_offset_for_LFB:
  5572. ; calculate data area for fast getting offset to LFB
  5573.         xor     eax, eax
  5574.         mov     ecx, [_display.height]
  5575.         mov     edi, BPSLine_calc_area
  5576.         cld
  5577. @@:
  5578.         stosd
  5579.         add     eax, [_display.lfb_pitch]
  5580.         dec     ecx
  5581.         jnz     @r
  5582.         ret
  5583. ;------------------------------------------------------------------------------
  5584. align 4
  5585. set_screen:
  5586. ; in:
  5587. ; eax - new Screen_Max_X
  5588. ; ecx - new BytesPerScanLine
  5589. ; edx - new Screen_Max_Y
  5590.  
  5591.         pushfd
  5592.         cli
  5593.  
  5594.         mov     [_display.lfb_pitch], ecx
  5595.  
  5596.         mov     [screen_workarea.right], eax
  5597.         mov     [screen_workarea.bottom], edx
  5598.  
  5599.         push    ebx
  5600.         push    esi
  5601.         push    edi
  5602.  
  5603.         pushad
  5604.  
  5605.         cmp     [do_not_touch_winmap], 1
  5606.         je      @f
  5607.  
  5608.         stdcall kernel_free, [_display.win_map]
  5609.  
  5610.         mov     eax, [_display.width]
  5611.         mul     [_display.height]
  5612.         mov     [_display.win_map_size], eax
  5613.  
  5614.         stdcall kernel_alloc, eax
  5615.         mov     [_display.win_map], eax
  5616.         test    eax, eax
  5617.         jz      .epic_fail
  5618. ; store for f.18.24
  5619.         mov     eax, [_display.width]
  5620.         mov     [display_width_standard], eax
  5621.  
  5622.         mov     eax, [_display.height]
  5623.         mov     [display_height_standard], eax
  5624. @@:
  5625.         call    calculate_fast_getting_offset_for_WinMapAddress
  5626. ; for Qemu or non standart video cards
  5627. ; Unfortunately [BytesPerScanLine] does not always
  5628. ;                             equal to [_display.width] * [ScreenBPP] / 8
  5629.         call    calculate_fast_getting_offset_for_LFB
  5630.         popad
  5631.  
  5632.         call    repos_windows
  5633.         xor     eax, eax
  5634.         xor     ebx, ebx
  5635.         mov     ecx, [_display.width]
  5636.         mov     edx, [_display.height]
  5637.         dec     ecx
  5638.         dec     edx
  5639.         call    calculatescreen
  5640.         pop     edi
  5641.         pop     esi
  5642.         pop     ebx
  5643.  
  5644.         popfd
  5645.         ret
  5646.  
  5647. .epic_fail:
  5648.         hlt                     ; Houston, we've had a problem
  5649.  
  5650. ; --------------- APM ---------------------
  5651. uglobal
  5652. apm_entry       dp      0
  5653. apm_vf          dd      0
  5654. endg
  5655.  
  5656. align 4
  5657. sys_apm:
  5658.         xor     eax, eax
  5659.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  5660.         jne     @f
  5661.         inc     eax
  5662.         or      dword [esp + 44], eax   ; error
  5663.         add     eax, 7
  5664.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  5665.         ret
  5666.  
  5667. @@:
  5668. ;       xchg    eax, ecx
  5669. ;       xchg    ebx, ecx
  5670.  
  5671.         cmp     dx, 3
  5672.         ja      @f
  5673.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  5674.         mov     eax, [apm_vf]
  5675.         mov     [esp + 32], eax
  5676.         shr     eax, 16
  5677.         mov     [esp + 28], eax
  5678.         ret
  5679.  
  5680. @@:
  5681.  
  5682.         mov     esi, [master_tab+(OS_BASE shr 20)]
  5683.         xchg    [master_tab], esi
  5684.         push    esi
  5685.         mov     edi, cr3
  5686.         mov     cr3, edi                ;flush TLB
  5687.  
  5688.         call    pword [apm_entry]       ;call APM BIOS
  5689.  
  5690.         xchg    eax, [esp]
  5691.         mov     [master_tab], eax
  5692.         mov     eax, cr3
  5693.         mov     cr3, eax
  5694.         pop     eax
  5695.  
  5696.         mov     [esp + 4 ], edi
  5697.         mov     [esp + 8], esi
  5698.         mov     [esp + 20], ebx
  5699.         mov     [esp + 24], edx
  5700.         mov     [esp + 28], ecx
  5701.         mov     [esp + 32], eax
  5702.         setc    al
  5703.         and     [esp + 44], byte 0xfe
  5704.         or      [esp + 44], al
  5705.         ret
  5706. ; -----------------------------------------
  5707.  
  5708. align 4
  5709. undefined_syscall:                      ; Undefined system call
  5710.         mov     [esp + 32], dword -1
  5711.         ret
  5712.  
  5713. ; check if given memory region lays in lower 2gb (userspace memory) or not
  5714. align 4
  5715. proc is_region_userspace stdcall, base:dword, len:dword
  5716. ; in:
  5717. ;      base = base address of region
  5718. ;      len = lenght of region
  5719. ; out: ZF = 1 if region in userspace memory
  5720. ;      ZF = 0 otherwise
  5721.         push    eax ebx
  5722.         mov     eax, [base]
  5723.  
  5724.         cmp     eax, OS_BASE
  5725.         ja      @f
  5726.  
  5727.         add     eax, [len]
  5728.         cmp     eax, OS_BASE
  5729.         ja      @f
  5730.  
  5731.         mov     eax, 1
  5732.         jmp     .ret
  5733. @@:
  5734.         xor     eax, eax
  5735. .ret:
  5736.         test    eax, eax
  5737.         pop     ebx eax
  5738.         ret
  5739. endp
  5740.  
  5741. if ~ lang eq sp
  5742. diff16 "end of .text segment",0,$
  5743. end if
  5744.  
  5745. include "data32.inc"
  5746.  
  5747. __REV__ = __REV
  5748.  
  5749. if ~ lang eq sp
  5750. diff16 "end of kernel code",0,$
  5751. end if
  5752.