Subversion Repositories Kolibri OS

Rev

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