Subversion Repositories Kolibri OS

Rev

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

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