Subversion Repositories Kolibri OS

Rev

Rev 9613 | Rev 9692 | 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: 9614 $
  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     [edx+APPDATA.wnd_number], dh
  1185.         mov     byte [edx+APPDATA.tid], dh    ;?
  1186.         mov     eax, edx
  1187.         shr     eax, 3
  1188.         add     eax, TASK_TABLE - (SLOT_BASE shr 3)
  1189.         mov     byte [eax+TASKDATA.pid], dh
  1190.  
  1191.         ret
  1192. endp
  1193.  
  1194. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1195. ;                                                                    ;
  1196. ;                    MAIN OS LOOP START                              ;
  1197. ;                                                                    ;
  1198. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1199. align 32
  1200. osloop:
  1201.         mov     edx, osloop_has_work?
  1202.         xor     ecx, ecx
  1203.         call    Wait_events
  1204.         xor     eax, eax
  1205.         xchg    eax, [osloop_nonperiodic_work]
  1206.         test    eax, eax
  1207.         jz      .no_periodic
  1208.  
  1209.         call    __sys_draw_pointer
  1210.         call    window_check_events
  1211.         call    mouse_check_events
  1212.         call    checkmisc
  1213.         call    checkVga_N13
  1214. ;--------------------------------------
  1215. .no_periodic:
  1216.         call    stack_handler
  1217.         call    check_fdd_motor_status
  1218.         call    check_ATAPI_device_event
  1219.         call    check_lights_state
  1220.         call    check_timers
  1221.  
  1222.         jmp     osloop
  1223. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1224. ;                                                                    ;
  1225. ;                      MAIN OS LOOP END                              ;
  1226. ;                                                                    ;
  1227. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1228. proc osloop_has_work?
  1229.         cmp     [osloop_nonperiodic_work], 0
  1230.         jnz     .yes
  1231.         call    stack_handler_has_work?
  1232.         jnz     .yes
  1233.         call    check_fdd_motor_status_has_work?
  1234.         jnz     .yes
  1235.         call    check_ATAPI_device_event_has_work?
  1236.         jnz     .yes
  1237.         call    check_lights_state_has_work?
  1238.         jnz     .yes
  1239.         call    check_timers_has_work?
  1240.         jnz     .yes
  1241. .no:
  1242.         xor     eax, eax
  1243.         ret
  1244. .yes:
  1245.         xor     eax, eax
  1246.         inc     eax
  1247.         ret
  1248. endp
  1249.  
  1250. proc wakeup_osloop
  1251.         mov     [osloop_nonperiodic_work], 1
  1252.         ret
  1253. endp
  1254.  
  1255. uglobal
  1256. align 4
  1257. osloop_nonperiodic_work dd      ?
  1258. endg
  1259.  
  1260. uglobal
  1261. align 64
  1262. idle_addr       rb      64
  1263. endg
  1264.  
  1265. idle_thread:
  1266.         sti
  1267.  
  1268. ; The following code can be executed by all CPUs in the system.
  1269. ; All other parts of the kernel do not expect multi-CPU.
  1270. ; Also, APs don't even have a stack here.
  1271. ; Beware. Don't do anything here. Anything at all.
  1272. idle_loop:
  1273.         cmp     [use_mwait_for_idle], 0
  1274.         jnz     idle_loop_mwait
  1275.  
  1276. idle_loop_hlt:
  1277.         hlt
  1278.         jmp     idle_loop_hlt
  1279.  
  1280. idle_loop_mwait:
  1281.         mov     eax, idle_addr
  1282.         xor     ecx, ecx
  1283.         xor     edx, edx
  1284.         monitor
  1285.         xor     ecx, ecx
  1286.         mov     eax, 20h        ; or 10h
  1287.         mwait
  1288.         jmp     idle_loop_mwait
  1289.  
  1290.  
  1291. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1292. ;                                                                      ;
  1293. ;                   INCLUDED SYSTEM FILES                              ;
  1294. ;                                                                      ;
  1295. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1296.  
  1297.  
  1298. include "kernel32.inc"
  1299.  
  1300.  
  1301. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1302. ;                                                                      ;
  1303. ;                       KERNEL FUNCTIONS                               ;
  1304. ;                                                                      ;
  1305. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1306.  
  1307. reserve_irqs_ports:
  1308.  
  1309.  
  1310. ; RESERVE PORTS
  1311.         mov     eax, RESERVED_PORTS
  1312.         mov     ecx, 1
  1313.  
  1314.         mov     [eax], dword 4
  1315.  
  1316.         mov     [eax+16], ecx
  1317.         mov     [eax+16+4], dword 0
  1318.         mov     [eax+16+8], dword 0x2D
  1319.  
  1320.         mov     [eax+32], ecx
  1321.         mov     [eax+32+4], dword 0x30
  1322.         mov     [eax+32+8], dword 0x4D
  1323.  
  1324.         mov     [eax+48], ecx
  1325.         mov     [eax+48+4], dword 0x50
  1326.         mov     [eax+48+8], dword 0xDF
  1327.  
  1328.         mov     [eax+64], ecx
  1329.         mov     [eax+64+4], dword 0xE5
  1330.         mov     [eax+64+8], dword 0xFF
  1331.  
  1332.         ret
  1333.  
  1334.  
  1335. iglobal
  1336.   process_number dd 0x2
  1337. endg
  1338.  
  1339. set_variables:
  1340.  
  1341.         mov     ecx, 0x16                    ; flush port 0x60
  1342. .fl60:
  1343.         in      al, 0x60
  1344.         loop    .fl60
  1345.         push    eax
  1346.  
  1347.         mov     ax, [BOOT.y_res]
  1348.         shr     ax, 1
  1349.         shl     eax, 16
  1350.         mov     ax, [BOOT.x_res]
  1351.         shr     ax, 1
  1352.         mov     [MOUSE_X], eax
  1353.         call    wakeup_osloop
  1354.  
  1355.         xor     eax, eax
  1356.         mov     [BTN_ADDR], dword BUTTON_INFO ; address of button list
  1357.  
  1358.         mov     byte [KEY_COUNT], al              ; keyboard buffer
  1359.         mov     byte [BTN_COUNT], al              ; button buffer
  1360. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  1361.  
  1362.         pop     eax
  1363.         ret
  1364. ;-----------------------------------------------------------------------------
  1365.  
  1366. align 4
  1367. display_number:
  1368. ; add check pointers
  1369.         test    bl, bl
  1370.         jz      @f
  1371.         stdcall is_region_userspace, ecx, 1
  1372.         jz      @f
  1373.         ret
  1374. @@:
  1375.         test    esi, 0x08000000
  1376.         jz      @f
  1377.         stdcall is_region_userspace, edi, 1
  1378.         jz      @f
  1379.         ret
  1380. @@:
  1381. ;It is not optimization
  1382.         mov     eax, ebx
  1383.         mov     ebx, ecx
  1384.         mov     ecx, edx
  1385.         mov     edx, esi
  1386.         mov     esi, edi
  1387. ; eax = print type, al=0 -> ebx is number
  1388. ;                   al=1 -> ebx is pointer
  1389. ;                   ah=0 -> display decimal
  1390. ;                   ah=1 -> display hexadecimal
  1391. ;                   ah=2 -> display binary
  1392. ;                   eax bits 16-21 = number of digits to display (0-32)
  1393. ;                   eax bits 22-31 = reserved
  1394. ;
  1395. ; ebx = number or pointer
  1396. ; ecx = x shl 16 + y
  1397. ; edx = color
  1398.         xor     edi, edi
  1399. display_number_force:
  1400.         push    eax
  1401.         and     eax, 0x3fffffff
  1402.         cmp     eax, 0xffff     ; length > 0 ?
  1403.         pop     eax
  1404.         jge     cont_displ
  1405.         ret
  1406.    cont_displ:
  1407.         push    eax
  1408.         and     eax, 0x3fffffff
  1409.         cmp     eax, 61*0x10000  ; length <= 60 ?
  1410.         pop     eax
  1411.         jb      cont_displ2
  1412.         ret
  1413.    cont_displ2:
  1414.  
  1415.         pushad
  1416.  
  1417.         cmp     al, 1            ; ecx is a pointer ?
  1418.         jne     displnl1
  1419.         mov     ebp, ebx
  1420.         add     ebp, 4
  1421.         mov     ebp, [ebp+std_application_base_address]
  1422.         mov     ebx, [ebx+std_application_base_address]
  1423.  displnl1:
  1424.         sub     esp, 64
  1425.  
  1426.         test    ah, ah            ; DECIMAL
  1427.         jnz     no_display_desnum
  1428.         shr     eax, 16
  1429.         and     eax, 0xC03f
  1430. ;     and   eax,0x3f
  1431.         push    eax
  1432.         and     eax, 0x3f
  1433.         mov     edi, esp
  1434.         add     edi, 4+64-1
  1435.         mov     ecx, eax
  1436.         mov     eax, ebx
  1437.         mov     ebx, 10
  1438.  d_desnum:
  1439.         xor     edx, edx
  1440.         call    division_64_bits
  1441.         div     ebx
  1442.         add     dl, 48
  1443.         mov     [edi], dl
  1444.         dec     edi
  1445.         loop    d_desnum
  1446.         pop     eax
  1447.         call    normalize_number
  1448.         call    draw_num_text
  1449.         add     esp, 64
  1450.         popad
  1451.         ret
  1452.    no_display_desnum:
  1453.  
  1454.         cmp     ah, 0x01         ; HEXADECIMAL
  1455.         jne     no_display_hexnum
  1456.         shr     eax, 16
  1457.         and     eax, 0xC03f
  1458. ;     and   eax,0x3f
  1459.         push    eax
  1460.         and     eax, 0x3f
  1461.         mov     edi, esp
  1462.         add     edi, 4+64-1
  1463.         mov     ecx, eax
  1464.         mov     eax, ebx
  1465.         mov     ebx, 16
  1466.    d_hexnum:
  1467.         xor     edx, edx
  1468.         call    division_64_bits
  1469.         div     ebx
  1470.    hexletters = __fdo_hexdigits
  1471.         add     edx, hexletters
  1472.         mov     dl, [edx]
  1473.         mov     [edi], dl
  1474.         dec     edi
  1475.         loop    d_hexnum
  1476.         pop     eax
  1477.         call    normalize_number
  1478.         call    draw_num_text
  1479.         add     esp, 64
  1480.         popad
  1481.         ret
  1482.    no_display_hexnum:
  1483.  
  1484.         cmp     ah, 0x02         ; BINARY
  1485.         jne     no_display_binnum
  1486.         shr     eax, 16
  1487.         and     eax, 0xC03f
  1488. ;     and   eax,0x3f
  1489.         push    eax
  1490.         and     eax, 0x3f
  1491.         mov     edi, esp
  1492.         add     edi, 4+64-1
  1493.         mov     ecx, eax
  1494.         mov     eax, ebx
  1495.         mov     ebx, 2
  1496.    d_binnum:
  1497.         xor     edx, edx
  1498.         call    division_64_bits
  1499.         div     ebx
  1500.         add     dl, 48
  1501.         mov     [edi], dl
  1502.         dec     edi
  1503.         loop    d_binnum
  1504.         pop     eax
  1505.         call    normalize_number
  1506.         call    draw_num_text
  1507.         add     esp, 64
  1508.         popad
  1509.         ret
  1510.    no_display_binnum:
  1511.  
  1512.         add     esp, 64
  1513.         popad
  1514.         ret
  1515.  
  1516. normalize_number:
  1517.         test    ah, 0x80
  1518.         jz      .continue
  1519.         mov     ecx, 48
  1520.         and     eax, 0x3f
  1521. @@:
  1522.         inc     edi
  1523.         cmp     [edi], cl
  1524.         jne     .continue
  1525.         dec     eax
  1526.         cmp     eax, 1
  1527.         ja      @r
  1528.         mov     al, 1
  1529. .continue:
  1530.         and     eax, 0x3f
  1531.         ret
  1532.  
  1533. division_64_bits:
  1534.         test    [esp+1+4], byte 0x40
  1535.         jz      .continue
  1536.         push    eax
  1537.         mov     eax, ebp
  1538.         div     ebx
  1539.         mov     ebp, eax
  1540.         pop     eax
  1541. .continue:
  1542.         ret
  1543.  
  1544. draw_num_text:
  1545.         mov     esi, eax
  1546.         mov     edx, 64+4
  1547.         sub     edx, eax
  1548.         add     edx, esp
  1549.         mov     ebx, [esp+64+32-8+4]
  1550. ; add window start x & y
  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, [current_slot]
  1616.         mov     eax, [edi+APPDATA.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, [current_slot]
  1713.         mov     ebx, [edi+APPDATA.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, [current_slot]
  1925.         mov     edx, [edx+APPDATA.tid]
  1926.         mov     edx, [TASK_BASE]           ; delete
  1927.         mov     edx, [edx+TASKDATA.pid]    ;
  1928.         call    socket_process_end
  1929.         popa
  1930. ;--------------------------------------
  1931.         mov     ecx, [current_slot]
  1932.         mov     eax, [ecx+APPDATA.tls_base]
  1933.         test    eax, eax
  1934.         jz      @F
  1935.  
  1936.         stdcall user_free, eax
  1937. @@:
  1938.  
  1939.         mov     eax, [current_slot]
  1940.         mov     [eax+APPDATA.state], TSTATE_ZOMBIE
  1941.         mov     eax, [TASK_BASE]                       ;
  1942.         mov     [eax+TASKDATA.state], TSTATE_ZOMBIE    ; delete
  1943.         call    wakeup_osloop
  1944.  
  1945. .waitterm:            ; wait here for termination
  1946.         call    change_task
  1947.         jmp     .waitterm
  1948. ;------------------------------------------------------------------------------
  1949. align 4
  1950. restore_default_cursor_before_killing:
  1951.         pushfd
  1952.         cli
  1953.         mov     eax, [def_cursor]
  1954.         mov     [ecx+APPDATA.cursor], eax
  1955.  
  1956.         movzx   eax, word [MOUSE_Y]
  1957.         movzx   ebx, word [MOUSE_X]
  1958.         mov     eax, [d_width_calc_area + eax*4]
  1959.  
  1960.         add     eax, [_display.win_map]
  1961.         movzx   edx, byte [ebx+eax]
  1962.         shl     edx, 8
  1963.         mov     esi, [edx+SLOT_BASE+APPDATA.cursor]
  1964.  
  1965.         cmp     esi, [current_cursor]
  1966.         je      @f
  1967.  
  1968.         push    esi
  1969.         call    [_display.select_cursor]
  1970.         mov     [current_cursor], esi
  1971. @@:
  1972.         mov     [redrawmouse_unconditional], 1
  1973.         call    wakeup_osloop
  1974.         popfd
  1975.         ret
  1976. ;------------------------------------------------------------------------------
  1977. iglobal
  1978. align 4
  1979. sys_system_table:
  1980.         dd      sysfn_deactivate        ; 1 = deactivate window
  1981.         dd      sysfn_terminate         ; 2 = terminate thread
  1982.         dd      sysfn_activate          ; 3 = activate window
  1983.         dd      sysfn_getidletime       ; 4 = get idle time
  1984.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1985.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1986.         dd      sysfn_getactive         ; 7 = get active window
  1987.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1988.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1989.         dd      sysfn_minimize          ; 10 = minimize window
  1990.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1991.         dd      undefined_syscall       ; 12 = get last pressed key. function removed. sysfn_lastkey
  1992.         dd      sysfn_getversion        ; 13 = get kernel version
  1993.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1994.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1995.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1996.         dd      sysfn_getallmem         ; 17 = get total memory size
  1997.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1998.                                         ;                 instead of slot
  1999.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  2000.         dd      sysfn_meminfo           ; 20 = get extended memory info
  2001.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  2002.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  2003.         dd      sysfn_min_windows       ; 23 = minimize all windows
  2004.         dd      sysfn_set_screen_sizes  ; 24 = set screen sizes for Vesa
  2005.  
  2006.         dd      sysfn_zmodif            ; 25 = get/set window z modifier  ;Fantomer
  2007. sysfn_num = ($ - sys_system_table)/4
  2008. endg
  2009. ;------------------------------------------------------------------------------
  2010. sys_system:
  2011.         dec     ebx
  2012.         cmp     ebx, sysfn_num
  2013.         jae     @f
  2014.         jmp     dword [sys_system_table + ebx*4]
  2015. @@:
  2016.         ret
  2017. ;------------------------------------------------------------------------------
  2018. sysfn_shutdown:          ; 18.9 = system shutdown
  2019.         cmp     ecx, SYSTEM_SHUTDOWN
  2020.         jl      exit_for_anyone
  2021.         cmp     ecx, SYSTEM_RESTART
  2022.         jg      exit_for_anyone
  2023.         mov     [BOOT.shutdown_type], cl
  2024.  
  2025.         mov     eax, [thread_count]
  2026.         mov     [SYS_SHUTDOWN], al
  2027.         mov     [shutdown_processes], eax
  2028.         call    wakeup_osloop
  2029.         and     dword [esp+32], 0
  2030.  exit_for_anyone:
  2031.         ret
  2032.   uglobal
  2033.    shutdown_processes:
  2034.                        dd 0x0
  2035.   endg
  2036. ;------------------------------------------------------------------------------
  2037. ; in: eax -- APPDATA ptr
  2038. ; out: Z/z -- is/not kernel thread
  2039. is_kernel_thread:
  2040.         mov     eax, [eax+APPDATA.process]
  2041.         cmp     eax, [SLOT_BASE+2*sizeof.APPDATA+APPDATA.process]       ; OS
  2042.         ret
  2043. ;------------------------------------------------------------------------------
  2044. sysfn_terminate:        ; 18.2 = TERMINATE
  2045.         push    ecx
  2046.         cmp     ecx, 2
  2047.         jb      noprocessterminate
  2048.         mov     edx, [thread_count]
  2049.         cmp     ecx, edx
  2050.         ja      noprocessterminate
  2051.         mov     eax, [thread_count]
  2052.         shl     ecx, BSF sizeof.TASKDATA
  2053.         mov     edx, [ecx+TASK_TABLE+TASKDATA.pid]
  2054.         add     ecx, TASK_TABLE+TASKDATA.state
  2055.         cmp     byte [ecx], TSTATE_FREE
  2056.         jz      noprocessterminate
  2057.         push    eax
  2058.         lea     eax, [(ecx-(TASK_TABLE and 1FFFFFFFh)-TASKDATA.state)*8+SLOT_BASE]
  2059.         call    is_kernel_thread
  2060.         pop     eax
  2061.         jz      noprocessterminate
  2062.         push    ecx edx
  2063.         lea     edx, [(ecx-(TASK_TABLE and 1FFFFFFFh)-TASKDATA.state)*8+SLOT_BASE]
  2064.         call    request_terminate
  2065.         pop     edx ecx
  2066.         test    eax, eax
  2067.         jz      noprocessterminate
  2068. ;--------------------------------------
  2069. ; terminate all network sockets it used
  2070.         pusha
  2071.         mov     eax, edx
  2072.         call    socket_process_end
  2073.         popa
  2074. ;--------------------------------------
  2075.         cmp     [_display.select_cursor], 0
  2076.         je      .restore_end
  2077. ; restore default cursor before killing
  2078.         pusha
  2079.         mov     ecx, [esp+32]
  2080.         shl     ecx, 8
  2081.         add     ecx, SLOT_BASE
  2082.         mov     eax, [def_cursor]
  2083.         cmp     [ecx+APPDATA.cursor], eax
  2084.         je      @f
  2085.         call    restore_default_cursor_before_killing
  2086. @@:
  2087.         popa
  2088. .restore_end:
  2089. ;--------------------------------------
  2090.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2091.         mov     [ecx], byte 3; clear possible i40's
  2092.         call    wakeup_osloop
  2093.      ;call MEM_Heap_UnLock
  2094.  
  2095.         cmp     edx, [application_table_owner]; clear app table stat
  2096.         jne     noatsc
  2097.         call    unlock_application_table
  2098. noatsc:
  2099. noprocessterminate:
  2100.         add     esp, 4
  2101.         ret
  2102. ;------------------------------------------------------------------------------
  2103. sysfn_terminate2:
  2104. ;lock application_table_status mutex
  2105. .table_status:
  2106.         call    lock_application_table
  2107.         mov     eax, ecx
  2108.         call    pid_to_slot
  2109.         test    eax, eax
  2110.         jz      .not_found
  2111.         mov     ecx, eax
  2112.         cli
  2113.         call    sysfn_terminate
  2114.         call    unlock_application_table
  2115.         sti
  2116.         and     dword [esp+32], 0
  2117.         ret
  2118. .not_found:
  2119.         call    unlock_application_table
  2120.         or      dword [esp+32], -1
  2121.         ret
  2122. ;------------------------------------------------------------------------------
  2123. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2124.         cmp     ecx, 2
  2125.         jb      .nowindowdeactivate
  2126.         cmp     ecx, [thread_count]
  2127.         ja      .nowindowdeactivate
  2128.  
  2129.         movzx   esi, word [WIN_STACK + ecx*2]
  2130.         cmp     esi, 1
  2131.         je      .nowindowdeactivate ; already deactive
  2132.  
  2133.         mov     edi, ecx
  2134.         shl     edi, 5
  2135.         add     edi, window_data
  2136.         movzx   esi, word [WIN_STACK + ecx * 2]
  2137.         lea     esi, [WIN_POS + esi * 2]
  2138.         call    window._.window_deactivate
  2139.         call    syscall_display_settings.calculateScreen
  2140.         call    syscall_display_settings.redrawScreen
  2141. .nowindowdeactivate:
  2142.         ret
  2143. ;------------------------------------------------------------------------------
  2144. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2145.         cmp     ecx, 2
  2146.         jb      .nowindowactivate
  2147.         cmp     ecx, [thread_count]
  2148.         ja      .nowindowactivate
  2149. ;-------------------------------------
  2150. @@:
  2151. ; If the window is captured and moved by the user,
  2152. ; then you can't change the position in window stack!!!
  2153.         mov     al, [mouse.active_sys_window.action]
  2154.         and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
  2155.         test    al, al
  2156.         jz      @f
  2157.         call    change_task
  2158.         jmp     @b
  2159. @@:
  2160. ;-------------------------------------
  2161.         mov     [window_minimize], 2; restore window if minimized
  2162.         call    wakeup_osloop
  2163.  
  2164.         movzx   esi, word [WIN_STACK + ecx*2]
  2165.         cmp     esi, [thread_count]
  2166.         je      .nowindowactivate; already active
  2167.  
  2168.         mov     edi, ecx
  2169.         shl     edi, 5
  2170.         add     edi, window_data
  2171.         movzx   esi, word [WIN_STACK + ecx * 2]
  2172.         lea     esi, [WIN_POS + esi * 2]
  2173.         call    waredraw
  2174. .nowindowactivate:
  2175.         ret
  2176. ;------------------------------------------------------------------------------
  2177. align 4
  2178. sysfn_zmodif:
  2179. ;18,25,1 - get z_modif
  2180. ;18,25,2 - set z_modif
  2181. ;edx = -1(for current task) or TID
  2182. ;esi(for 2) = new value z_modif
  2183. ;return:
  2184. ;1:   eax = z_modif
  2185. ;2: eax=0(fail),1(success) for set z_modif
  2186.  
  2187.         cmp     edx, -1
  2188.         jne     @f
  2189.         mov     edx, [current_slot_idx]
  2190.      @@:
  2191.         cmp     edx, [thread_count]
  2192.         ja      .fail
  2193.         cmp     edx, 1
  2194.         je      .fail
  2195.  
  2196.         mov     eax, edx
  2197.         shl     edx, 5
  2198.         ;shl     edx, 8
  2199.  
  2200.         ;cmp     [edx + SLOT_BASE + APPDATA.state], TSTATE_FREE
  2201.         cmp     [edx + TASK_TABLE + TASKDATA.state], TSTATE_FREE
  2202.         je      .fail
  2203.  
  2204.         ;shr     edx, 3
  2205.         cmp     ecx, 1
  2206.         jnz     .set_zmod
  2207.  
  2208.         mov     al, [edx + window_data + WDATA.z_modif]
  2209.         jmp     .exit
  2210.  
  2211. .set_zmod:
  2212.         cmp     ecx, 2
  2213.         jnz     .fail
  2214.  
  2215.         mov     ebx, esi
  2216.         mov     esi, eax
  2217.  
  2218.         cmp     bl, ZPOS_ALWAYS_TOP
  2219.         jg      .fail
  2220.  
  2221.         mov     [edx + window_data + WDATA.z_modif], bl
  2222.  
  2223.         mov     eax, [edx + window_data + WDATA.box.left]
  2224.         mov     ebx, [edx + window_data + WDATA.box.top]
  2225.         mov     ecx, [edx + window_data + WDATA.box.width]
  2226.         mov     edx, [edx + window_data + WDATA.box.height]
  2227.         add     ecx, eax
  2228.         add     edx, ebx
  2229.         call    window._.set_screen
  2230.         call    window._.set_top_wnd
  2231.         call    window._.redraw_top_wnd
  2232.  
  2233.         shl     esi, 5
  2234.         mov     [esi + window_data + WDATA.fl_redraw], 1
  2235.  
  2236.  
  2237.         mov     eax, 1
  2238.         jmp     .exit
  2239. .fail:
  2240.         xor     eax, eax
  2241. .exit:
  2242.         mov     [esp+32], eax
  2243.         ret
  2244.  
  2245. ;------------------------------------------------------------------------------
  2246. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2247.         ;mov     eax, [TASK_TABLE+32+TASKDATA.cpu_usage]
  2248.         mov     eax, [SLOT_BASE+APPDATA.cpu_usage]
  2249.         mov     [esp+32], eax
  2250.         ret
  2251. ;------------------------------------------------------------------------------
  2252. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2253.         mov     eax, dword [cpu_freq]
  2254.         mov     [esp+32], eax
  2255.         ret
  2256. ;------------------------------------------------------------------------------
  2257. get_cpu_freq:
  2258.         mov     eax, dword [cpu_freq]
  2259.         mov     edx, dword [cpu_freq+4]
  2260.         ret
  2261. ;  SAVE ramdisk to /hd/1/kolibri.img
  2262. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2263.    include 'blkdev/rdsave.inc'
  2264. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2265. ;------------------------------------------------------------------------------
  2266. align 4
  2267. sysfn_getactive:        ; 18.7 = get active window
  2268.         mov     eax, [thread_count]
  2269.         movzx   eax, word [WIN_POS + eax*2]
  2270.         mov     [esp+32], eax
  2271.         ret
  2272. ;------------------------------------------------------------------------------
  2273. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2274. ;     cmp  ecx,1
  2275.         dec     ecx
  2276.         jnz     nogetsoundflag
  2277.         movzx   eax, byte [sound_flag]; get sound_flag
  2278.         mov     [esp+32], eax
  2279.         ret
  2280.  nogetsoundflag:
  2281. ;     cmp  ecx,2
  2282.         dec     ecx
  2283.         jnz     nosoundflag
  2284.         xor     byte [sound_flag], 1
  2285.  nosoundflag:
  2286.         ret
  2287. ;------------------------------------------------------------------------------
  2288. sysfn_minimize:         ; 18.10 = minimize window
  2289.         mov     [window_minimize], 1
  2290.         call    wakeup_osloop
  2291.         ret
  2292. ;------------------------------------------------------------------------------
  2293. align 4
  2294. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2295.         dec     ecx
  2296.         jnz     .exit
  2297. .small_table:
  2298.         stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
  2299.         jnz     .exit
  2300.         mov     edi, edx
  2301.         mov     esi, DRIVE_DATA
  2302.         mov     ecx, DRIVE_DATA_SIZE ;10
  2303.         cld
  2304.         rep movsb
  2305. .exit:
  2306.         ret
  2307. ;------------------------------------------------------------------------------
  2308. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2309.         ; if given memory address belongs to kernel then error
  2310.         stdcall is_region_userspace, ecx, version_end-version_inf
  2311.         jnz     .addr_error
  2312.  
  2313.         mov     edi, ecx
  2314.         mov     esi, version_inf
  2315.         mov     ecx, version_end-version_inf
  2316.         rep movsb
  2317.         ret
  2318. .addr_error:    ; if given memory address is illegal
  2319.         mov     dword [esp+32], -1
  2320.         ret  
  2321. ;------------------------------------------------------------------------------
  2322. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2323.      ;wait retrace functions
  2324.  sys_wait_retrace:
  2325.         mov     edx, 0x3da
  2326.  WaitRetrace_loop:
  2327.         in      al, dx
  2328.         test    al, 1000b
  2329.         jz      WaitRetrace_loop
  2330.         and     [esp+32], dword 0
  2331.         ret
  2332. ;------------------------------------------------------------------------------
  2333. align 4
  2334. sysfn_centermouse:      ; 18.15 = mouse centered
  2335.         mov     eax, [_display.width]
  2336.         shr     eax, 1
  2337.         mov     [MOUSE_X], ax
  2338.         mov     eax, [_display.height]
  2339.         shr     eax, 1
  2340.         mov     [MOUSE_Y], ax
  2341.         call    wakeup_osloop
  2342.         xor     eax, eax
  2343.         and     [esp+32], eax
  2344.         ret
  2345. ;------------------------------------------------------------------------------
  2346. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2347.         cmp     ecx, 8
  2348.         jnc     @f
  2349.         jmp     dword [.table+ecx*4]
  2350. .get_mouse_acceleration:
  2351.         xor     eax, eax
  2352.         mov     ax, [mouse_speed_factor]
  2353.         mov     [esp+32], eax
  2354.         ret
  2355. .set_mouse_acceleration:
  2356.         mov     [mouse_speed_factor], dx
  2357.         ret
  2358. .get_mouse_delay:
  2359.         xor     eax, eax
  2360.         mov     al, [mouse_delay]
  2361.         mov     [esp+32], eax
  2362.         ret
  2363. .set_mouse_delay:
  2364.         mov     [mouse_delay], dl
  2365. @@:
  2366.         ret
  2367. .set_pointer_position:
  2368.         cmp     dx, word[_display.height]
  2369.         jae     @b
  2370.         rol     edx, 16
  2371.         cmp     dx, word[_display.width]
  2372.         jae     @b
  2373.         mov     [MOUSE_X], edx
  2374.         mov     [mouse_active], 1
  2375.         jmp     wakeup_osloop
  2376. .set_mouse_button:
  2377.         mov     [BTN_DOWN], edx
  2378.         mov     [mouse_active], 1
  2379.         jmp     wakeup_osloop
  2380. .get_doubleclick_delay:
  2381.         xor     eax, eax
  2382.         mov     al, [mouse_doubleclick_delay]
  2383.         mov     [esp+32], eax
  2384.         ret
  2385. .set_doubleclick_delay:
  2386.         mov     [mouse_doubleclick_delay], dl
  2387.         ret
  2388. align 4
  2389. .table:
  2390. dd      .get_mouse_acceleration
  2391. dd      .set_mouse_acceleration
  2392. dd      .get_mouse_delay
  2393. dd      .set_mouse_delay
  2394. dd      .set_pointer_position
  2395. dd      .set_mouse_button
  2396. dd      .get_doubleclick_delay
  2397. dd      .set_doubleclick_delay
  2398. ;------------------------------------------------------------------------------
  2399. sysfn_getfreemem:
  2400.         mov     eax, [pg_data.pages_free]
  2401.         shl     eax, 2
  2402.         mov     [esp+32], eax
  2403.         ret
  2404. ;------------------------------------------------------------------------------
  2405. sysfn_getallmem:
  2406.         mov     eax, [MEM_AMOUNT]
  2407.         shr     eax, 10
  2408.         mov     [esp+32], eax
  2409.         ret
  2410. ;------------------------------------------------------------------------------
  2411. sysfn_pid_to_slot:
  2412.         mov     eax, ecx
  2413.         call    pid_to_slot
  2414.         mov     [esp+32], eax
  2415.         ret
  2416. ;------------------------------------------------------------------------------
  2417. sysfn_min_rest_window:
  2418.         pushad
  2419.         mov     eax, edx ; ebx - operating
  2420.         shr     ecx, 1
  2421.         jnc     @f
  2422.         call    pid_to_slot
  2423. @@:
  2424.         or      eax, eax ; eax - number of slot
  2425.         jz      .error
  2426.         cmp     eax, 255    ; varify maximal slot number
  2427.         ja      .error
  2428.         movzx   eax, word [WIN_STACK + eax*2]
  2429.         shr     ecx, 1
  2430.         jc      .restore
  2431.  ; .minimize:
  2432.         call    minimize_window
  2433.         jmp     .exit
  2434. .restore:
  2435.         call    restore_minimized_window
  2436. .exit:
  2437.         popad
  2438.         xor     eax, eax
  2439.         mov     [esp+32], eax
  2440.         ret
  2441. .error:
  2442.         popad
  2443.         xor     eax, eax
  2444.         dec     eax
  2445.         mov     [esp+32], eax
  2446.         ret
  2447. ;------------------------------------------------------------------------------
  2448. sysfn_min_windows:
  2449.         call    minimize_all_window
  2450.         mov     [esp+32], eax
  2451.         call    change_task
  2452.         ret
  2453. ;------------------------------------------------------------------------------
  2454. sysfn_set_screen_sizes:
  2455.         cmp     [SCR_MODE], word 0x13
  2456.         jbe     .exit
  2457.  
  2458.         cmp     [_display.select_cursor], select_cursor
  2459.         jne     .exit
  2460.  
  2461.         cmp     ecx, [display_width_standard]
  2462.         ja      .exit
  2463.  
  2464.         cmp     edx, [display_height_standard]
  2465.         ja      .exit
  2466.  
  2467.         pushfd
  2468.         cli
  2469.         mov     eax, ecx
  2470.         mov     ecx, [_display.lfb_pitch]
  2471.         mov     [_display.width], eax
  2472.         dec     eax
  2473.         mov     [_display.height], edx
  2474.         dec     edx
  2475. ; eax - new Screen_Max_X
  2476. ; edx - new Screen_Max_Y
  2477.         mov     [do_not_touch_winmap], 1
  2478.         call    set_screen
  2479.         mov     [do_not_touch_winmap], 0
  2480.         popfd
  2481.         call    change_task
  2482. .exit:
  2483.         ret
  2484. ;------------------------------------------------------------------------------
  2485. uglobal
  2486. screen_workarea RECT
  2487. display_width_standard dd 0
  2488. display_height_standard dd 0
  2489. do_not_touch_winmap db 0
  2490. window_minimize db 0
  2491. sound_flag      db 0
  2492.  
  2493. endg
  2494.  
  2495. ;UID_NONE=0
  2496. ;UID_KOLIBRI=2    ;russian
  2497.  
  2498. iglobal
  2499. version_inf:
  2500.         db 0,7,7,0  ; version 0.7.7.0
  2501.         db 0
  2502. .rev    dd __REV__
  2503. version_end:
  2504. endg
  2505. ;------------------------------------------------------------------------------
  2506. align 4
  2507. sys_cachetodiskette:
  2508.         cmp     ebx, 1
  2509.         jb      .no_floppy_save
  2510.         cmp     ebx, 2
  2511.         ja      .no_floppy_save
  2512.         call    save_image
  2513.         mov     [esp + 32], eax
  2514.         ret
  2515. .no_floppy_save:
  2516.         mov     [esp + 32], dword 1
  2517.         ret
  2518. ;------------------------------------------------------------------------------
  2519.  
  2520. align 4
  2521. sys_getkey:
  2522.         mov     [esp + 32], dword 1
  2523.         ; test main buffer
  2524.         mov     ebx, [current_slot_idx]                          ; TOP OF WINDOW STACK
  2525.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2526.         mov     edx, [thread_count]
  2527.         cmp     ecx, edx
  2528.         jne     .finish
  2529.         cmp     [KEY_COUNT], byte 0
  2530.         je      .finish
  2531.         movzx   ax, byte [KEY_BUFF + 120 + 2]
  2532.         shl     eax, 8
  2533.         mov     al, byte [KEY_BUFF]
  2534.         shl     eax, 8
  2535.         push    eax
  2536.         dec     byte [KEY_COUNT]
  2537.         and     byte [KEY_COUNT], 127
  2538.         movzx   ecx, byte [KEY_COUNT]
  2539.         add     ecx, 2
  2540.         mov     eax, KEY_BUFF + 1
  2541.         mov     ebx, KEY_BUFF
  2542.         call    memmove
  2543.         add     eax, 120 + 2
  2544.         add     ebx, 120 + 2
  2545.         call    memmove
  2546.         pop     eax
  2547. ;--------------------------------------
  2548. align 4
  2549. .ret_eax:
  2550.         mov     [esp + 32], eax
  2551.         ret
  2552. ;--------------------------------------
  2553. align 4
  2554. .finish:
  2555. ; test hotkeys buffer
  2556.         mov     ecx, hotkey_buffer
  2557. ;--------------------------------------
  2558. align 4
  2559. @@:
  2560.         cmp     [ecx], ebx
  2561.         jz      .found
  2562.         add     ecx, 8
  2563.         cmp     ecx, hotkey_buffer + 120 * 8
  2564.         jb      @b
  2565.         ret
  2566. ;--------------------------------------
  2567. align 4
  2568. .found:
  2569.         mov     ax, [ecx + 6]
  2570.         shl     eax, 16
  2571.         mov     ah, [ecx + 4]
  2572.         mov     al, 2
  2573.         and     dword [ecx + 4], 0
  2574.         and     dword [ecx], 0
  2575.         jmp     .ret_eax
  2576. ;------------------------------------------------------------------------------
  2577. align 4
  2578. sys_getbutton:
  2579.         mov     ebx, [current_slot_idx]                         ; TOP OF WINDOW STACK
  2580.         mov     [esp + 32], dword 1
  2581.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2582.         mov     edx, [thread_count] ; less than 256 processes
  2583.         cmp     ecx, edx
  2584.         jne     .exit
  2585.         movzx   eax, byte [BTN_COUNT]
  2586.         test    eax, eax
  2587.         jz      .exit
  2588.         mov     eax, [BTN_BUFF]
  2589.         and     al, 0xFE                                    ; delete left button bit
  2590.         mov     [BTN_COUNT], byte 0
  2591.         mov     [esp + 32], eax
  2592. ;--------------------------------------
  2593. align 4
  2594. .exit:
  2595.         ret
  2596. ;------------------------------------------------------------------------------
  2597. align 4
  2598. sys_cpuusage:
  2599.  
  2600. ;  RETURN:
  2601. ;
  2602. ;  +00 dword     process cpu usage
  2603. ;  +04  word     position in windowing stack
  2604. ;  +06  word     windowing stack value at current position (cpu nro)
  2605. ;  +10 12 bytes  name
  2606. ;  +22 dword     start in mem
  2607. ;  +26 dword     used mem
  2608. ;  +30 dword     PID , process idenfification number
  2609. ;
  2610.         ; if given memory address belongs to kernel then error
  2611.         stdcall is_region_userspace, ebx, 0x4C
  2612.         jnz     .addr_error
  2613.  
  2614.         cmp     ecx, -1 ; who am I ?
  2615.         jne     .no_who_am_i
  2616.         mov     ecx, [current_slot_idx]
  2617.   .no_who_am_i:
  2618.         cmp     ecx, max_processes
  2619.         ja      .nofillbuf
  2620.  
  2621. ; +4: word: position of the window of thread in the window stack
  2622.         mov     ax, [WIN_STACK + ecx * 2]
  2623.         mov     [ebx+4], ax
  2624. ; +6: word: number of the thread slot, which window has in the window stack
  2625. ;           position ecx (has no relation to the specific thread)
  2626.         mov     ax, [WIN_POS + ecx * 2]
  2627.         mov     [ebx+6], ax
  2628.  
  2629.         shl     ecx, 8   ;5=32 8=256
  2630.  
  2631. ; +0: dword: memory usage
  2632.         ;mov     eax, [ecx+TASK_TABLE+TASKDATA.cpu_usage]
  2633.         mov     eax, [ecx-sizeof.APPDATA+SLOT_BASE+APPDATA.cpu_usage]
  2634.         mov     [ebx], eax
  2635. ; +10: 11 bytes: name of the process
  2636.         push    ecx
  2637.         lea     eax, [ecx+SLOT_BASE+APPDATA.app_name]
  2638.         add     ebx, 10
  2639.         mov     ecx, 11
  2640.         call    memmove
  2641.         pop     ecx
  2642.  
  2643.         shr     ecx, 3
  2644. ; +22: address of the process in memory
  2645. ; +26: size of used memory - 1
  2646.         push    edi
  2647.         lea     edi, [ebx+12]
  2648.         xor     eax, eax
  2649.         mov     edx, 0x100000*16
  2650.         cmp     ecx, 1 shl 5
  2651.         je      .os_mem
  2652.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.process]
  2653.         mov     edx, [edx+PROC.mem_used]
  2654.         mov     eax, std_application_base_address
  2655. .os_mem:
  2656.         stosd
  2657.         lea     eax, [edx-1]
  2658.         stosd
  2659.  
  2660. ; +30: PID/TID
  2661.         mov     eax, [ecx*8 + SLOT_BASE + APPDATA.tid]
  2662.         mov     eax, [ecx+TASK_TABLE+TASKDATA.pid]
  2663.         stosd
  2664.  
  2665.     ; window position and size
  2666.         push    esi
  2667.         lea     esi, [ecx + window_data + WDATA.box]
  2668.         movsd
  2669.         movsd
  2670.         movsd
  2671.         movsd
  2672.  
  2673.     ; Process state (+50)
  2674.         movzx   eax, byte [ecx*8 + SLOT_BASE + APPDATA.state]
  2675.         movzx   eax, byte [ecx+TASK_TABLE+TASKDATA.state]
  2676.         stosd
  2677.  
  2678.     ; Window client area box
  2679.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  2680.         movsd
  2681.         movsd
  2682.         movsd
  2683.         movsd
  2684.  
  2685.     ; Window state
  2686.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  2687.         stosb
  2688.  
  2689.     ; Event mask (+71)
  2690.         mov     EAX, dword [ecx*8 + SLOT_BASE + APPDATA.event_mask]
  2691.         stosd
  2692.  
  2693.     ; Keyboard mode (+75)
  2694.         mov     al, byte [ecx*8 + SLOT_BASE + APPDATA.keyboard_mode]
  2695.         stosb
  2696.  
  2697.         pop     esi
  2698.         pop     edi
  2699.  
  2700. .nofillbuf:
  2701.     ; return number of processes
  2702.  
  2703.         mov     eax, [thread_count]
  2704.         mov     [esp+32], eax
  2705.         ret
  2706.  
  2707. .addr_error:    ; if given memory address is illegal
  2708.         mov     dword [esp+32], -1
  2709.         ret  
  2710.  
  2711.  
  2712. ; redraw status
  2713. align 4
  2714. sys_redrawstat:
  2715.         cmp     ebx, 1
  2716.         jne     no_widgets_away
  2717.         ; buttons away
  2718.         mov     ecx, [current_slot_idx]
  2719.   sys_newba2:
  2720.         mov     edi, [BTN_ADDR]
  2721.         cmp     [edi], dword 0  ; empty button list ?
  2722.         je      end_of_buttons_away
  2723.         movzx   ebx, word [edi]
  2724.         inc     ebx
  2725.         mov     eax, edi
  2726.   sys_newba:
  2727.         dec     ebx
  2728.         jz      end_of_buttons_away
  2729.  
  2730.         add     eax, 0x10
  2731.         cmp     cx, [eax]
  2732.         jnz     sys_newba
  2733.  
  2734.         push    eax ebx ecx
  2735.         mov     ecx, ebx
  2736.         inc     ecx
  2737.         shl     ecx, 4
  2738.         mov     ebx, eax
  2739.         add     eax, 0x10
  2740.         call    memmove
  2741.         dec     dword [edi]
  2742.         pop     ecx ebx eax
  2743.  
  2744.         jmp     sys_newba2
  2745.  
  2746.   end_of_buttons_away:
  2747.  
  2748.         ret
  2749.  
  2750.   no_widgets_away:
  2751.  
  2752.         cmp     ebx, 2
  2753.         jnz     srl1
  2754.  
  2755.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  2756.         add     edx, draw_data - TASK_TABLE
  2757.         mov     [edx + RECT.left], 0
  2758.         mov     [edx + RECT.top], 0
  2759.         mov     eax, [_display.width]
  2760.         dec     eax
  2761.         mov     [edx + RECT.right], eax
  2762.         mov     eax, [_display.height]
  2763.         dec     eax
  2764.         mov     [edx + RECT.bottom], eax
  2765.  
  2766.   srl1:
  2767.         ret
  2768.  
  2769. ;ok - 100% work
  2770. ;nt - not tested
  2771. ;---------------------------------------------------------------------------------------------
  2772. ;eax
  2773. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2774. ;1 - change task. Ret nothing. Block. ok.
  2775. ;2 - performance control
  2776. ; ebx
  2777. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2778. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2779. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2780. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2781. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2782. ;eax
  2783. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2784. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2785. ;---------------------------------------------------------------------------------------------
  2786. iglobal
  2787. align 4
  2788. sheduler:
  2789.         dd      sys_sheduler.00
  2790.         dd      change_task
  2791.         dd      sys_sheduler.02
  2792.         dd      sys_sheduler.03
  2793.         dd      sys_sheduler.04
  2794. endg
  2795. sys_sheduler:
  2796. ;rewritten by <Lrz>  29.12.2009
  2797.         jmp     dword [sheduler+ebx*4]
  2798. ;.shed_counter:
  2799. .00:
  2800.         mov     eax, [context_counter]
  2801.         mov     [esp+32], eax
  2802.         ret
  2803.  
  2804. .02:
  2805. ;.perf_control:
  2806.         inc     ebx                     ;before ebx=2, ebx=3
  2807.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  2808.         jz      cache_disable
  2809.  
  2810.         dec     ebx                     ;ebx=2
  2811.         cmp     ebx, ecx                ;
  2812.         jz      cache_enable            ;if ecx=2 and ebx=2
  2813.  
  2814.         dec     ebx                     ;ebx=1
  2815.         cmp     ebx, ecx
  2816.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  2817.  
  2818.         dec     ebx
  2819.         test    ebx, ecx                ;ebx=0 and ecx=0
  2820.         jz      modify_pce              ;if ecx=0
  2821.  
  2822.         ret
  2823.  
  2824. .03:
  2825. ;.rdmsr_instr:
  2826. ;now counter in ecx
  2827. ;(edx:eax) esi:edi => edx:esi
  2828.         mov     eax, esi
  2829.         mov     ecx, edx
  2830.         rdmsr
  2831.         mov     [esp+32], eax
  2832.         mov     [esp+20], edx           ;ret in ebx?
  2833.         ret
  2834.  
  2835. .04:
  2836. ;.wrmsr_instr:
  2837. ;now counter in ecx
  2838. ;(edx:eax) esi:edi => edx:esi
  2839.         ; Fast Call MSR can't be destroy
  2840.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  2841.         ; включаются/выключаются расширенные возможности
  2842.         cmp     edx, MSR_SYSENTER_CS
  2843.         je      @f
  2844.         cmp     edx, MSR_SYSENTER_ESP
  2845.         je      @f
  2846.         cmp     edx, MSR_SYSENTER_EIP
  2847.         je      @f
  2848.         cmp     edx, MSR_AMD_STAR
  2849.         je      @f
  2850.  
  2851.         mov     eax, esi
  2852.         mov     ecx, edx
  2853.         wrmsr
  2854.         ; mov   [esp + 32], eax
  2855.         ; mov   [esp + 20], edx ;ret in ebx?
  2856. @@:
  2857.         ret
  2858.  
  2859. cache_disable:
  2860.         mov     eax, cr0
  2861.         or      eax, 01100000000000000000000000000000b
  2862.         mov     cr0, eax
  2863.         wbinvd  ;set MESI
  2864.         ret
  2865.  
  2866. cache_enable:
  2867.         mov     eax, cr0
  2868.         and     eax, 10011111111111111111111111111111b
  2869.         mov     cr0, eax
  2870.         ret
  2871.  
  2872. is_cache_enabled:
  2873.         mov     eax, cr0
  2874.         mov     ebx, eax
  2875.         and     eax, 01100000000000000000000000000000b
  2876.         jz      cache_disabled
  2877.         mov     [esp+32], ebx
  2878. cache_disabled:
  2879.         mov     dword [esp+32], eax;0
  2880.         ret
  2881.  
  2882. modify_pce:
  2883.         mov     eax, cr4
  2884. ;       mov ebx,0
  2885. ;       or  bx,100000000b ;pce
  2886. ;       xor eax,ebx ;invert pce
  2887.         bts     eax, 8;pce=cr4[8]
  2888.         mov     cr4, eax
  2889.         mov     [esp+32], eax
  2890.         ret
  2891. ;---------------------------------------------------------------------------------------------
  2892.  
  2893.  
  2894. iglobal
  2895.   cpustring db 'CPU',0
  2896. endg
  2897.  
  2898. uglobal
  2899. background_defined    db    0    ; diamond, 11.04.2006
  2900. endg
  2901. ;-----------------------------------------------------------------------------
  2902. align 4
  2903. checkmisc:
  2904.         cmp     [ctrl_alt_del], 1
  2905.         jne     nocpustart
  2906.  
  2907.         mov     ebp, cpustring
  2908.         call    fs_execute_from_sysdir
  2909.  
  2910.         mov     [ctrl_alt_del], 0
  2911. ;--------------------------------------
  2912. align 4
  2913. nocpustart:
  2914.         cmp     [mouse_active], 1
  2915.         jne     mouse_not_active
  2916.         mov     [mouse_active], 0
  2917.  
  2918.         xor     edi, edi
  2919.         mov     ebx, TASK_TABLE
  2920.  
  2921.         mov     ecx, [thread_count]
  2922.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  2923.         shl     eax, 8
  2924.         push    eax
  2925.  
  2926.         movzx   eax, word [MOUSE_X]
  2927.         movzx   edx, word [MOUSE_Y]
  2928. ;--------------------------------------
  2929. align 4
  2930. .set_mouse_event:
  2931.         add     edi, sizeof.APPDATA
  2932.         add     ebx, sizeof.TASKDATA
  2933.         test    [edi + SLOT_BASE + APPDATA.event_mask], 0x80000000
  2934.         jz      .pos_filter
  2935.  
  2936.         cmp     edi, [esp]                      ; skip if filtration active
  2937.         jne     .skip
  2938. ;--------------------------------------
  2939. align 4
  2940. .pos_filter:
  2941.         test    [edi + SLOT_BASE + APPDATA.event_mask], 0x40000000
  2942.         jz      .set
  2943.  
  2944.         mov     esi, [ebx-twdw+WDATA.box.left]
  2945.         cmp     eax, esi
  2946.         jb      .skip
  2947.         add     esi, [ebx-twdw+WDATA.box.width]
  2948.         cmp     eax, esi
  2949.         ja      .skip
  2950.  
  2951.         mov     esi, [ebx-twdw+WDATA.box.top]
  2952.         cmp     edx, esi
  2953.         jb      .skip
  2954.         add     esi, [ebx-twdw+WDATA.box.height]
  2955.         cmp     edx, esi
  2956.         ja      .skip
  2957. ;--------------------------------------
  2958. align 4
  2959. .set:
  2960.         or      [edi+SLOT_BASE+APPDATA.occurred_events], EVENT_MOUSE
  2961. ;--------------------------------------
  2962. align 4
  2963. .skip:
  2964.         loop    .set_mouse_event
  2965.  
  2966.         pop     eax
  2967. ;--------------------------------------
  2968. align 4
  2969. mouse_not_active:
  2970.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  2971.         jz      nobackgr
  2972.  
  2973.         cmp     [background_defined], 0
  2974.         jz      nobackgr
  2975. ;--------------------------------------
  2976. align 4
  2977. backgr:
  2978.         mov     eax, [draw_data+32 + RECT.left]
  2979.         shl     eax, 16
  2980.         add     eax, [draw_data+32 + RECT.right]
  2981.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  2982.  
  2983.         mov     eax, [draw_data+32 + RECT.top]
  2984.         shl     eax, 16
  2985.         add     eax, [draw_data+32 + RECT.bottom]
  2986.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  2987.  
  2988.         call    drawbackground
  2989. ;        DEBUGF  1, "K : drawbackground\n"
  2990. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  2991. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  2992. ;--------- set event 5 start ----------
  2993.         push    ecx edi
  2994.         xor     edi, edi
  2995.         mov     ecx, [thread_count]
  2996. ;--------------------------------------
  2997. align 4
  2998. set_bgr_event:
  2999.         add     edi, sizeof.APPDATA
  3000.         mov     eax, [BG_Rect_X_left_right]
  3001.         mov     edx, [BG_Rect_Y_top_bottom]
  3002.         cmp     [edi+SLOT_BASE+APPDATA.draw_bgr_x], 0
  3003.         jz      .set
  3004. .join:
  3005.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3006.         jae     @f
  3007.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3008. @@:
  3009.         shr     eax, 16
  3010.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3011.         jbe     @f
  3012.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3013. @@:
  3014.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3015.         jae     @f
  3016.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3017. @@:
  3018.         shr     edx, 16
  3019.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3020.         jbe     @f
  3021.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3022. @@:
  3023.         jmp     .common
  3024. .set:
  3025.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_x], eax
  3026.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_y], edx
  3027. .common:
  3028.         or      [edi+SLOT_BASE+APPDATA.occurred_events], EVENT_BACKGROUND
  3029.         loop    set_bgr_event
  3030.         pop     edi ecx
  3031. ;--------- set event 5 stop -----------
  3032.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  3033.         jnz     backgr
  3034.  
  3035.         xor     eax, eax
  3036.         mov     [draw_data+32 + RECT.left], eax
  3037.         mov     [draw_data+32 + RECT.top], eax
  3038.         mov     [draw_data+32 + RECT.right], eax
  3039.         mov     [draw_data+32 + RECT.bottom], eax
  3040. ;--------------------------------------
  3041. align 4
  3042. nobackgr:
  3043. ; system shutdown request
  3044.         cmp     [SYS_SHUTDOWN], byte 0
  3045.         je      noshutdown
  3046.  
  3047.         mov     edx, [shutdown_processes]
  3048.  
  3049.         cmp     [SYS_SHUTDOWN], dl
  3050.         jne     noshutdown
  3051.  
  3052.         lea     ecx, [edx-1]
  3053.         mov     edx, OS_BASE+0x3040
  3054.         jecxz   no_mark_system_shutdown
  3055. ;--------------------------------------
  3056. align 4
  3057. markz:
  3058.         push    ecx edx
  3059.         cmp     [edx+TASKDATA.state], TSTATE_FREE
  3060.         jz      .nokill
  3061.         lea     edx, [(edx-(TASK_TABLE and 1FFFFFFFh))*8+SLOT_BASE]
  3062.         cmp     [edx+APPDATA.process], sys_proc
  3063.         jz      .nokill
  3064.         call    request_terminate
  3065.         jmp     .common
  3066. .nokill:
  3067.         dec     byte [SYS_SHUTDOWN]
  3068.         xor     eax, eax
  3069. .common:
  3070.         pop     edx ecx
  3071.         test    eax, eax
  3072.         jz      @f
  3073.         mov     [edx+TASKDATA.state], TSTATE_ZOMBIE
  3074.         ;mov     [edx+APPDATA.state], TSTATE_ZOMBIE
  3075. @@:
  3076.         add     edx, 0x20
  3077.         loop    markz
  3078.         call    wakeup_osloop
  3079. ;--------------------------------------
  3080. align 4
  3081. @@:
  3082. no_mark_system_shutdown:
  3083.         dec     byte [SYS_SHUTDOWN]
  3084.         je      system_shutdown
  3085. ;--------------------------------------
  3086. align 4
  3087. noshutdown:
  3088.         mov     eax, [thread_count]           ; termination
  3089.         mov     ebx, TASK_DATA+TASKDATA.state
  3090.         mov     esi, 1
  3091. ;--------------------------------------
  3092. align 4
  3093. newct:
  3094.         mov     cl, [ebx]
  3095.         cmp     cl, byte 3
  3096.         jz      .terminate
  3097.  
  3098.         cmp     cl, byte 4
  3099.         jnz     .noterminate
  3100. .terminate:
  3101.         pushad
  3102.         mov     ecx, eax
  3103.         shl     ecx, 8
  3104.         add     ecx, SLOT_BASE
  3105.         call    restore_default_cursor_before_killing
  3106.         popad
  3107.  
  3108.         pushad
  3109.         call    terminate
  3110.         popad
  3111.         cmp     byte [SYS_SHUTDOWN], 0
  3112.         jz      .noterminate
  3113.         dec     byte [SYS_SHUTDOWN]
  3114.         je      system_shutdown
  3115.  
  3116. .noterminate:
  3117.         add     ebx, 0x20
  3118.         inc     esi
  3119.         dec     eax
  3120.         jnz     newct
  3121.         ret
  3122. ;-----------------------------------------------------------------------------
  3123. align 4
  3124. redrawscreen:
  3125. ; eax , if process window_data base is eax, do not set flag/limits
  3126.  
  3127.         pushad
  3128.         push    eax
  3129.  
  3130. ;;;         mov   ebx,2
  3131. ;;;         call  delay_hs
  3132.  
  3133.          ;mov   ecx,0               ; redraw flags for apps
  3134.         xor     ecx, ecx
  3135. ;--------------------------------------
  3136. align 4
  3137. newdw2:
  3138.         inc     ecx
  3139.         push    ecx
  3140.  
  3141.         mov     eax, ecx
  3142.         shl     eax, 5
  3143.         add     eax, window_data
  3144.  
  3145.         cmp     eax, [esp+4]
  3146.         je      not_this_task
  3147.                                    ; check if window in redraw area
  3148.         mov     edi, eax
  3149.  
  3150.         cmp     ecx, 1             ; limit for background
  3151.         jz      bgli
  3152.  
  3153.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3154.         test    eax, eax
  3155.         jz      @f
  3156.         mov     al, [eax + WDATA.z_modif]
  3157.         cmp     [edi + WDATA.z_modif], al
  3158.         jg      ricino
  3159.       @@:
  3160.  
  3161.         mov     eax, [edi + WDATA.box.left]
  3162.         mov     ebx, [edi + WDATA.box.top]
  3163.  
  3164.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3165.         cmp     ecx, ebx
  3166.         jb      ricino
  3167.  
  3168.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3169.         cmp     ecx, eax
  3170.         jb      ricino
  3171.  
  3172.         mov     eax, [edi + WDATA.box.left]
  3173.         mov     ebx, [edi + WDATA.box.top]
  3174.         mov     ecx, [edi + WDATA.box.width]
  3175.         mov     edx, [edi + WDATA.box.height]
  3176.         add     ecx, eax
  3177.         add     edx, ebx
  3178.  
  3179.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3180.         cmp     edx, eax
  3181.         jb      ricino
  3182.  
  3183.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3184.         cmp     ecx, eax
  3185.         jb      ricino
  3186. ;--------------------------------------
  3187. align 4
  3188. bgli:
  3189.         cmp     dword[esp], 1
  3190.         jnz     .az
  3191.  
  3192.         cmp     [REDRAW_BACKGROUND], 0
  3193.         jz      .az
  3194.  
  3195.         mov     dl, 0
  3196.         lea     eax, [edi+draw_data-window_data]
  3197.         mov     ebx, [draw_limits.left]
  3198.         cmp     ebx, [eax+RECT.left]
  3199.         jae     @f
  3200.  
  3201.         mov     [eax+RECT.left], ebx
  3202.         mov     dl, 1
  3203. ;--------------------------------------
  3204. align 4
  3205. @@:
  3206.         mov     ebx, [draw_limits.top]
  3207.         cmp     ebx, [eax+RECT.top]
  3208.         jae     @f
  3209.  
  3210.         mov     [eax+RECT.top], ebx
  3211.         mov     dl, 1
  3212. ;--------------------------------------
  3213. align 4
  3214. @@:
  3215.         mov     ebx, [draw_limits.right]
  3216.         cmp     ebx, [eax+RECT.right]
  3217.         jbe     @f
  3218.  
  3219.         mov     [eax+RECT.right], ebx
  3220.         mov     dl, 1
  3221. ;--------------------------------------
  3222. align 4
  3223. @@:
  3224.         mov     ebx, [draw_limits.bottom]
  3225.         cmp     ebx, [eax+RECT.bottom]
  3226.         jbe     @f
  3227.  
  3228.         mov     [eax+RECT.bottom], ebx
  3229.         mov     dl, 1
  3230. ;--------------------------------------
  3231. align 4
  3232. @@:
  3233.         add     [REDRAW_BACKGROUND], dl
  3234.         call    wakeup_osloop
  3235.         jmp     newdw8
  3236. ;--------------------------------------
  3237. align 4
  3238. .az:
  3239.         mov     eax, edi
  3240.         add     eax, draw_data-window_data
  3241.  
  3242.         mov     ebx, [draw_limits.left]        ; set limits
  3243.         mov     [eax + RECT.left], ebx
  3244.         mov     ebx, [draw_limits.top]
  3245.         mov     [eax + RECT.top], ebx
  3246.         mov     ebx, [draw_limits.right]
  3247.         mov     [eax + RECT.right], ebx
  3248.         mov     ebx, [draw_limits.bottom]
  3249.         mov     [eax + RECT.bottom], ebx
  3250.  
  3251.         sub     eax, draw_data-window_data
  3252.  
  3253.         cmp     dword [esp], 1
  3254.         jne     nobgrd
  3255.         inc     [REDRAW_BACKGROUND]
  3256.         call    wakeup_osloop
  3257. ;--------------------------------------
  3258. align 4
  3259. newdw8:
  3260. nobgrd:
  3261. ;--------------------------------------
  3262.         push    eax  edi ebp
  3263.         mov     edi, [esp+12]
  3264.         cmp     edi, 1
  3265.         je      .found
  3266.  
  3267.         mov     eax, [draw_limits.left]
  3268.         mov     ebx, [draw_limits.top]
  3269.         mov     ecx, [draw_limits.right]
  3270.         sub     ecx, eax
  3271.         test    ecx, ecx
  3272.         jz      .not_found
  3273.  
  3274.         mov     edx, [draw_limits.bottom]
  3275.         sub     edx, ebx
  3276.         test    edx, edx
  3277.         jz      .not_found
  3278.  
  3279. ; eax - x, ebx - y
  3280. ; ecx - size x, edx - size y
  3281.         add     ebx, edx
  3282. ;--------------------------------------
  3283. align 4
  3284. .start_y:
  3285.         push    ecx
  3286. ;--------------------------------------
  3287. align 4
  3288. .start_x:
  3289.         add     eax, ecx
  3290.         mov     ebp, [d_width_calc_area + ebx*4]
  3291.         add     ebp, [_display.win_map]
  3292.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3293.         cmp     ebp, edi
  3294.         jne     @f
  3295.  
  3296.         pop     ecx
  3297.         jmp     .found
  3298. ;--------------------------------------
  3299. align 4
  3300. @@:
  3301.         sub     eax, ecx
  3302.  
  3303.         dec     ecx
  3304.         jns     .start_x
  3305.  
  3306.         pop     ecx
  3307.         dec     ebx
  3308.         dec     edx
  3309.         jns     .start_y
  3310. ;--------------------------------------
  3311. align 4
  3312. .not_found:
  3313.         pop     ebp edi eax
  3314.         jmp     ricino
  3315. ;--------------------------------------
  3316. align 4
  3317. .found:
  3318.         pop     ebp edi eax
  3319.  
  3320.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3321. ;--------------------------------------
  3322. align 4
  3323. ricino:
  3324. not_this_task:
  3325.         pop     ecx
  3326.  
  3327.         cmp     ecx, [thread_count]
  3328.         jle     newdw2
  3329.  
  3330.         pop     eax
  3331.         popad
  3332.         ret
  3333. ;-----------------------------------------------------------------------------
  3334. align 4
  3335. calculatebackground:   ; background
  3336.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3337.         mov     eax, 0x01010101
  3338.         mov     ecx, [_display.win_map_size]
  3339.         shr     ecx, 2
  3340.         rep stosd
  3341.         mov     byte[window_data+32+WDATA.z_modif], ZPOS_DESKTOP
  3342.         mov     [REDRAW_BACKGROUND], 0
  3343.         ret
  3344. ;-----------------------------------------------------------------------------
  3345. uglobal
  3346.   imax    dd 0x0
  3347. endg
  3348. ;-----------------------------------------------------------------------------
  3349. align 4
  3350. delay_ms:     ; delay in 1/1000 sec
  3351.         pushad
  3352.  
  3353.         cmp     [hpet_base], 0
  3354.         jz      .no_hpet
  3355.         mov     eax, esi
  3356.         mov     edx, 1_000_000 ; ms to ns
  3357.         mul     edx
  3358.         mov     ebx, edx
  3359.         mov     ecx, eax
  3360.  
  3361.         push    ecx
  3362.         call    get_clock_ns
  3363.         pop     ecx
  3364.         mov     edi, edx
  3365.         mov     esi, eax
  3366. .wait:
  3367.         push    ecx
  3368.         call    get_clock_ns
  3369.         pop     ecx
  3370.         sub     eax, esi
  3371.         sbb     edx, edi
  3372.         sub     eax, ecx
  3373.         sbb     edx, ebx
  3374.         jc      .wait
  3375.         jmp     .done
  3376.  
  3377. .no_hpet:
  3378.         mov     ecx, esi
  3379.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3380.         imul    ecx, 33941
  3381.         shr     ecx, 9
  3382.         ; </CPU clock fix>
  3383.  
  3384.         in      al, 0x61
  3385.         and     al, 0x10
  3386.         mov     ah, al
  3387.         cld
  3388.  
  3389. .cnt1:
  3390.         in      al, 0x61
  3391.         and     al, 0x10
  3392.         cmp     al, ah
  3393.         jz      .cnt1
  3394.  
  3395.         mov     ah, al
  3396.         loop    .cnt1
  3397.  
  3398. .done:
  3399.         popad
  3400.         ret
  3401. ;-----------------------------------------------------------------------------
  3402. align 4
  3403. set_app_param:
  3404.         mov     edi, [current_slot]
  3405.         xchg    ebx, [edi + APPDATA.event_mask] ; set new event mask
  3406.         mov     [esp+32], ebx                    ; return old mask value
  3407.         ret
  3408. ;-----------------------------------------------------------------------------
  3409.  
  3410. ; this is for syscall
  3411. proc delay_hs_unprotected
  3412.         call    unprotect_from_terminate
  3413.         call    delay_hs
  3414.         call    protect_from_terminate
  3415.         ret
  3416. endp
  3417.  
  3418. if 1
  3419. align 4
  3420. delay_hs:     ; delay in 1/100 secs
  3421. ; ebx = delay time
  3422.  
  3423.         pushad
  3424.         push    ebx
  3425.         xor     esi, esi
  3426.         mov     ecx, MANUAL_DESTROY
  3427.         call    create_event
  3428.         test    eax, eax
  3429.         jz      .done
  3430.  
  3431.         mov     ebx, edx
  3432.         mov     ecx, [esp]
  3433.         push    edx
  3434.         push    eax
  3435.         call    wait_event_timeout
  3436.         pop     eax
  3437.         pop     ebx
  3438.         call    destroy_event
  3439. .done:
  3440.         add     esp, 4
  3441.         popad
  3442.         ret
  3443.  
  3444. else
  3445.  
  3446. align 4
  3447. delay_hs:     ; delay in 1/100 secs
  3448. ; ebx = delay time
  3449.         push    ecx
  3450.         push    edx
  3451.  
  3452.         mov     edx, [timer_ticks]
  3453. ;--------------------------------------
  3454. align 4
  3455. newtic:
  3456.         mov     ecx, [timer_ticks]
  3457.         sub     ecx, edx
  3458.         cmp     ecx, ebx
  3459.         jae     zerodelay
  3460.  
  3461.         call    change_task
  3462.  
  3463.         jmp     newtic
  3464. ;--------------------------------------
  3465. align 4
  3466. zerodelay:
  3467.         pop     edx
  3468.         pop     ecx
  3469.         ret
  3470. end if
  3471.  
  3472. ;-----------------------------------------------------------------------------
  3473. align 16        ;very often call this subrutine
  3474. memmove:       ; memory move in bytes
  3475. ; eax = from
  3476. ; ebx = to
  3477. ; ecx = no of bytes
  3478.         test    ecx, ecx
  3479.         jle     .ret
  3480.  
  3481.         push    esi edi ecx
  3482.  
  3483.         mov     edi, ebx
  3484.         mov     esi, eax
  3485.  
  3486.         test    ecx, not 11b
  3487.         jz      @f
  3488.  
  3489.         push    ecx
  3490.         shr     ecx, 2
  3491.         rep movsd
  3492.         pop     ecx
  3493.         and     ecx, 11b
  3494.         jz      .finish
  3495. ;--------------------------------------
  3496. align 4
  3497. @@:
  3498.         rep movsb
  3499. ;--------------------------------------
  3500. align 4
  3501. .finish:
  3502.         pop     ecx edi esi
  3503. ;--------------------------------------
  3504. align 4
  3505. .ret:
  3506.         ret
  3507. ;-----------------------------------------------------------------------------
  3508.  
  3509. align 4
  3510. set_io_access_rights:
  3511.         push    edi eax
  3512.         mov     edi, tss._io_map_0
  3513. ;     mov   ecx,eax
  3514. ;     and   ecx,7    ; offset in byte
  3515. ;     shr   eax,3    ; number of byte
  3516. ;     add   edi,eax
  3517. ;     mov   ebx,1
  3518. ;     shl   ebx,cl
  3519.         test    ebp, ebp
  3520. ;     cmp   ebp,0                ; enable access - ebp = 0
  3521.         jnz     .siar1
  3522. ;     not   ebx
  3523. ;     and   [edi],byte bl
  3524.         btr     [edi], eax
  3525.         pop     eax edi
  3526.         ret
  3527. .siar1:
  3528.         bts     [edi], eax
  3529.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  3530.         pop     eax edi
  3531.         ret
  3532.  
  3533. align 4
  3534. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  3535.  
  3536.         call    r_f_port_area
  3537.         mov     [esp+32], eax
  3538.         ret
  3539.  
  3540. ;reserve/free group of ports
  3541. ;  * eax = 46 - number function
  3542. ;  * ebx = 0 - reserve, 1 - free
  3543. ;  * ecx = number start arrea of ports
  3544. ;  * edx = number end arrea of ports (include last number of port)
  3545. ;Return value:
  3546. ;  * eax = 0 - succesful
  3547. ;  * eax = 1 - error
  3548. ;  * The system has reserve this ports:
  3549. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3550. ;destroys eax,ebx, ebp
  3551. r_f_port_area:
  3552.  
  3553.         test    ebx, ebx
  3554.         jnz     free_port_area
  3555. ;     je    r_port_area
  3556. ;     jmp   free_port_area
  3557.  
  3558. ;   r_port_area:
  3559.  
  3560. ;     pushad
  3561.  
  3562.         cmp     ecx, edx      ; beginning > end ?
  3563.         ja      rpal1
  3564.         cmp     edx, 65536
  3565.         jae     rpal1
  3566.         mov     eax, [RESERVED_PORTS]
  3567.         test    eax, eax      ; no reserved areas ?
  3568.         je      rpal2
  3569.         cmp     eax, 255      ; max reserved
  3570.         jae     rpal1
  3571.  rpal3:
  3572.         mov     ebx, eax
  3573.         shl     ebx, 4
  3574.         add     ebx, RESERVED_PORTS
  3575.         cmp     ecx, [ebx+8]
  3576.         ja      rpal4
  3577.         cmp     edx, [ebx+4]
  3578.         jae     rpal1
  3579. ;     jb    rpal4
  3580. ;     jmp   rpal1
  3581.  rpal4:
  3582.         dec     eax
  3583.         jnz     rpal3
  3584.         jmp     rpal2
  3585.    rpal1:
  3586. ;     popad
  3587. ;     mov   eax,1
  3588.         xor     eax, eax
  3589.         inc     eax
  3590.         ret
  3591.    rpal2:
  3592. ;     popad
  3593.      ; enable port access at port IO map
  3594.         cli
  3595.         pushad                        ; start enable io map
  3596.  
  3597.         cmp     edx, 65536;16384
  3598.         jae     no_unmask_io; jge
  3599.         mov     eax, ecx
  3600. ;       push    ebp
  3601.         xor     ebp, ebp               ; enable - eax = port
  3602. new_port_access:
  3603. ;     pushad
  3604.         call    set_io_access_rights
  3605. ;     popad
  3606.         inc     eax
  3607.         cmp     eax, edx
  3608.         jbe     new_port_access
  3609. ;       pop     ebp
  3610. no_unmask_io:
  3611.         popad                         ; end enable io map
  3612.         sti
  3613.  
  3614.         mov     eax, [RESERVED_PORTS]
  3615.         add     eax, 1
  3616.         mov     [RESERVED_PORTS], eax
  3617.         shl     eax, 4
  3618.         add     eax, RESERVED_PORTS
  3619.         ;mov     ebx, [current_slot]
  3620.         ;mov     ebx, [ebx+APPDATA.tid]
  3621.         mov     ebx, [TASK_BASE]
  3622.         mov     ebx, [ebx+TASKDATA.pid]
  3623.         mov     [eax], ebx
  3624.         mov     [eax+4], ecx
  3625.         mov     [eax+8], edx
  3626.  
  3627.         xor     eax, eax
  3628.         ret
  3629.  
  3630. free_port_area:
  3631.  
  3632. ;     pushad
  3633.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3634.         test    eax, eax
  3635.         jz      frpal2
  3636.         ;mov     ebx, [current_slot]
  3637.         ;mov     ebx, [ebx+APPDATA.tid]
  3638.         mov     ebx, [TASK_BASE]
  3639.         mov     ebx, [ebx+TASKDATA.pid]
  3640.    frpal3:
  3641.         mov     edi, eax
  3642.         shl     edi, 4
  3643.         add     edi, RESERVED_PORTS
  3644.         cmp     ebx, [edi]
  3645.         jne     frpal4
  3646.         cmp     ecx, [edi+4]
  3647.         jne     frpal4
  3648.         cmp     edx, [edi+8]
  3649.         jne     frpal4
  3650.         jmp     frpal1
  3651.    frpal4:
  3652.         dec     eax
  3653.         jnz     frpal3
  3654.    frpal2:
  3655. ;     popad
  3656.         inc     eax
  3657.         ret
  3658.    frpal1:
  3659.         push    ecx
  3660.         mov     ecx, 256
  3661.         sub     ecx, eax
  3662.         shl     ecx, 4
  3663.         mov     esi, edi
  3664.         add     esi, 16
  3665.         cld
  3666.         rep movsb
  3667.  
  3668.         dec     dword [RESERVED_PORTS]
  3669. ;popad
  3670. ;disable port access at port IO map
  3671.  
  3672. ;     pushad                        ; start disable io map
  3673.         pop     eax     ;start port
  3674.         cmp     edx, 65536;16384
  3675.         jge     no_mask_io
  3676.  
  3677. ;     mov   eax,ecx
  3678.         xor     ebp, ebp
  3679.         inc     ebp
  3680. new_port_access_disable:
  3681. ;     pushad
  3682. ;     mov   ebp,1                  ; disable - eax = port
  3683.         call    set_io_access_rights
  3684. ;     popad
  3685.         inc     eax
  3686.         cmp     eax, edx
  3687.         jbe     new_port_access_disable
  3688. no_mask_io:
  3689. ;     popad                         ; end disable io map
  3690.         xor     eax, eax
  3691.         ret
  3692. ;-----------------------------------------------------------------------------
  3693. align 4
  3694. drawbackground:
  3695. dbrv20:
  3696.         cmp     [BgrDrawMode], dword 1
  3697.         jne     bgrstr
  3698.         call    vesa20_drawbackground_tiled
  3699. ;        call    [draw_pointer]
  3700.         call    __sys_draw_pointer
  3701.         ret
  3702. ;--------------------------------------
  3703. align 4
  3704. bgrstr:
  3705.         call    vesa20_drawbackground_stretch
  3706. ;        call    [draw_pointer]
  3707.         call    __sys_draw_pointer
  3708.         ret
  3709. ;-----------------------------------------------------------------------------
  3710. align 4
  3711. syscall_putimage:                       ; PutImage
  3712. ; add check pointer
  3713.         push    ecx
  3714.         mov     ax, cx
  3715.         shr     ecx, 16
  3716.         imul    eax, ecx
  3717.         lea     eax, [eax*3]
  3718.         stdcall is_region_userspace, ebx, eax
  3719.         pop     ecx
  3720.         jnz     sys_putimage.exit
  3721.  
  3722. sys_putimage:
  3723.         test    ecx, 0x80008000
  3724.         jnz     .exit
  3725.         test    ecx, 0x0000FFFF
  3726.         jz      .exit
  3727.         test    ecx, 0xFFFF0000
  3728.         jnz     @f
  3729. ;--------------------------------------
  3730. align 4
  3731. .exit:
  3732.         ret
  3733. ;--------------------------------------
  3734. align 4
  3735. @@:
  3736.         mov     edi, [current_slot]
  3737.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  3738.         rol     edx, 16
  3739.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  3740.         rol     edx, 16
  3741. ;--------------------------------------
  3742. align 4
  3743. .forced:
  3744.         push    ebp esi 0
  3745.         mov     ebp, putimage_get24bpp
  3746.         mov     esi, putimage_init24bpp
  3747. ;--------------------------------------
  3748. align 4
  3749. sys_putimage_bpp:
  3750.         call    vesa20_putimage
  3751.         pop     ebp esi ebp
  3752.         ret
  3753. ;        jmp     [draw_pointer]
  3754. ;-----------------------------------------------------------------------------
  3755. align 4
  3756. sys_putimage_palette:
  3757. ; ebx = pointer to image
  3758. ; ecx = [xsize]*65536 + [ysize]
  3759. ; edx = [xstart]*65536 + [ystart]
  3760. ; esi = number of bits per pixel, must be 8, 24 or 32
  3761. ; edi = pointer to palette
  3762. ; ebp = row delta
  3763. ; check pointer
  3764.         push    ecx
  3765.         mov     ax, cx
  3766.         shr     ecx, 16
  3767.         imul    eax, ecx
  3768.         stdcall is_region_userspace, ebx, eax
  3769.         pop     ecx
  3770.         jnz     sys_putimage.exit
  3771.  
  3772.         mov     eax, [current_slot_idx]
  3773.         shl     eax, 8
  3774.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  3775.         rol     edx, 16
  3776.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  3777.         rol     edx, 16
  3778. ;--------------------------------------
  3779. align 4
  3780. .forced:
  3781.         cmp     esi, 1
  3782.         jnz     @f
  3783.         push    edi
  3784.         mov     eax, [edi+4]
  3785.         sub     eax, [edi]
  3786.         push    eax
  3787.         push    dword [edi]
  3788.         push    0ffffff80h
  3789.         mov     edi, esp
  3790.         call    put_mono_image
  3791.         add     esp, 12
  3792.         pop     edi
  3793.         ret
  3794. ;--------------------------------------
  3795. align 4
  3796. @@:
  3797.         cmp     esi, 2
  3798.         jnz     @f
  3799.         push    edi
  3800.         push    0ffffff80h
  3801.         mov     edi, esp
  3802.         call    put_2bit_image
  3803.         pop     eax
  3804.         pop     edi
  3805.         ret
  3806. ;--------------------------------------
  3807. align 4
  3808. @@:
  3809.         cmp     esi, 4
  3810.         jnz     @f
  3811.         push    edi
  3812.         push    0ffffff80h
  3813.         mov     edi, esp
  3814.         call    put_4bit_image
  3815.         pop     eax
  3816.         pop     edi
  3817.         ret
  3818. ;--------------------------------------
  3819. align 4
  3820. @@:
  3821.         push    ebp esi ebp
  3822.         cmp     esi, 8
  3823.         jnz     @f
  3824.         mov     ebp, putimage_get8bpp
  3825.         mov     esi, putimage_init8bpp
  3826.         jmp     sys_putimage_bpp
  3827. ;--------------------------------------
  3828. align 4
  3829. @@:
  3830.         cmp     esi, 9
  3831.         jnz     @f
  3832.         mov     ebp, putimage_get9bpp
  3833.         mov     esi, putimage_init9bpp
  3834.         jmp     sys_putimage_bpp
  3835. ;--------------------------------------
  3836. align 4
  3837. @@:
  3838.         cmp     esi, 15
  3839.         jnz     @f
  3840.         mov     ebp, putimage_get15bpp
  3841.         mov     esi, putimage_init15bpp
  3842.         jmp     sys_putimage_bpp
  3843. ;--------------------------------------
  3844. align 4
  3845. @@:
  3846.         cmp     esi, 16
  3847.         jnz     @f
  3848.         mov     ebp, putimage_get16bpp
  3849.         mov     esi, putimage_init16bpp
  3850.         jmp     sys_putimage_bpp
  3851. ;--------------------------------------
  3852. align 4
  3853. @@:
  3854.         cmp     esi, 24
  3855.         jnz     @f
  3856.         mov     ebp, putimage_get24bpp
  3857.         mov     esi, putimage_init24bpp
  3858.         jmp     sys_putimage_bpp
  3859. ;--------------------------------------
  3860. align 4
  3861. @@:
  3862.         cmp     esi, 32
  3863.         jnz     @f
  3864.         mov     ebp, putimage_get32bpp
  3865.         mov     esi, putimage_init32bpp
  3866.         jmp     sys_putimage_bpp
  3867. ;--------------------------------------
  3868. align 4
  3869. @@:
  3870.         pop     ebp esi ebp
  3871.         ret
  3872. ;-----------------------------------------------------------------------------
  3873. align 4
  3874. put_mono_image:
  3875.         push    ebp esi ebp
  3876.         mov     ebp, putimage_get1bpp
  3877.         mov     esi, putimage_init1bpp
  3878.         jmp     sys_putimage_bpp
  3879. ;-----------------------------------------------------------------------------
  3880. align 4
  3881. put_2bit_image:
  3882.         push    ebp esi ebp
  3883.         mov     ebp, putimage_get2bpp
  3884.         mov     esi, putimage_init2bpp
  3885.         jmp     sys_putimage_bpp
  3886. ;-----------------------------------------------------------------------------
  3887. align 4
  3888. put_4bit_image:
  3889.         push    ebp esi ebp
  3890.         mov     ebp, putimage_get4bpp
  3891.         mov     esi, putimage_init4bpp
  3892.         jmp     sys_putimage_bpp
  3893. ;-----------------------------------------------------------------------------
  3894. align 4
  3895. putimage_init24bpp:
  3896.         lea     eax, [eax*3]
  3897. putimage_init8bpp:
  3898. putimage_init9bpp:
  3899.         ret
  3900. ;-----------------------------------------------------------------------------
  3901. align 16
  3902. putimage_get24bpp:
  3903.         movzx   eax, byte [esi+2]
  3904.         shl     eax, 16
  3905.         mov     ax, [esi]
  3906.         add     esi, 3
  3907.         ret     4
  3908. ;-----------------------------------------------------------------------------
  3909. align 16
  3910. putimage_get8bpp:
  3911.         movzx   eax, byte [esi]
  3912.         push    edx
  3913.         mov     edx, [esp+8]
  3914.         mov     eax, [edx+eax*4]
  3915.         pop     edx
  3916.         inc     esi
  3917.         ret     4
  3918. ;-----------------------------------------------------------------------------
  3919. align 16
  3920. putimage_get9bpp:
  3921.         lodsb
  3922.         mov     ah, al
  3923.         shl     eax, 8
  3924.         mov     al, ah
  3925.         ret     4
  3926. ;-----------------------------------------------------------------------------
  3927. align 4
  3928. putimage_init1bpp:
  3929.         add     eax, ecx
  3930.         push    ecx
  3931.         add     eax, 7
  3932.         add     ecx, 7
  3933.         shr     eax, 3
  3934.         shr     ecx, 3
  3935.         sub     eax, ecx
  3936.         pop     ecx
  3937.         ret
  3938. ;-----------------------------------------------------------------------------
  3939. align 16
  3940. putimage_get1bpp:
  3941.         push    edx
  3942.         mov     edx, [esp+8]
  3943.         mov     al, [edx]
  3944.         add     al, al
  3945.         jnz     @f
  3946.         lodsb
  3947.         adc     al, al
  3948. @@:
  3949.         mov     [edx], al
  3950.         sbb     eax, eax
  3951.         and     eax, [edx+8]
  3952.         add     eax, [edx+4]
  3953.         pop     edx
  3954.         ret     4
  3955. ;-----------------------------------------------------------------------------
  3956. align 4
  3957. putimage_init2bpp:
  3958.         add     eax, ecx
  3959.         push    ecx
  3960.         add     ecx, 3
  3961.         add     eax, 3
  3962.         shr     ecx, 2
  3963.         shr     eax, 2
  3964.         sub     eax, ecx
  3965.         pop     ecx
  3966.         ret
  3967. ;-----------------------------------------------------------------------------
  3968. align 16
  3969. putimage_get2bpp:
  3970.         push    edx
  3971.         mov     edx, [esp+8]
  3972.         mov     al, [edx]
  3973.         mov     ah, al
  3974.         shr     al, 6
  3975.         shl     ah, 2
  3976.         jnz     .nonewbyte
  3977.         lodsb
  3978.         mov     ah, al
  3979.         shr     al, 6
  3980.         shl     ah, 2
  3981.         add     ah, 1
  3982. .nonewbyte:
  3983.         mov     [edx], ah
  3984.         mov     edx, [edx+4]
  3985.         movzx   eax, al
  3986.         mov     eax, [edx+eax*4]
  3987.         pop     edx
  3988.         ret     4
  3989. ;-----------------------------------------------------------------------------
  3990. align 4
  3991. putimage_init4bpp:
  3992.         add     eax, ecx
  3993.         push    ecx
  3994.         add     ecx, 1
  3995.         inc     eax      ;add   eax, 1
  3996.         shr     ecx, 1
  3997.         shr     eax, 1
  3998.         sub     eax, ecx
  3999.         pop     ecx
  4000.         ret
  4001. ;-----------------------------------------------------------------------------
  4002. align 16
  4003. putimage_get4bpp:
  4004.         push    edx
  4005.         mov     edx, [esp+8]
  4006.         add     byte [edx], 80h
  4007.         jc      @f
  4008.         movzx   eax, byte [edx+1]
  4009.         mov     edx, [edx+4]
  4010.         and     eax, 0x0F
  4011.         mov     eax, [edx+eax*4]
  4012.         pop     edx
  4013.         ret     4
  4014. @@:
  4015.         movzx   eax, byte [esi]
  4016.         add     esi, 1
  4017.         mov     [edx+1], al
  4018.         shr     eax, 4
  4019.         mov     edx, [edx+4]
  4020.         mov     eax, [edx+eax*4]
  4021.         pop     edx
  4022.         ret     4
  4023. ;-----------------------------------------------------------------------------
  4024. align 4
  4025. putimage_init32bpp:
  4026.         shl     eax, 2
  4027.         ret
  4028. ;-----------------------------------------------------------------------------
  4029. align 16
  4030. putimage_get32bpp:
  4031.         lodsd
  4032.         ret     4
  4033. ;-----------------------------------------------------------------------------
  4034. align 4
  4035. putimage_init15bpp:
  4036. putimage_init16bpp:
  4037.         add     eax, eax
  4038.         ret
  4039. ;-----------------------------------------------------------------------------
  4040. align 16
  4041. putimage_get15bpp:
  4042. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4043.         push    ecx edx
  4044.         movzx   eax, word [esi]
  4045.         add     esi, 2
  4046.         mov     ecx, eax
  4047.         mov     edx, eax
  4048.         and     eax, 0x1F
  4049.         and     ecx, 0x1F shl 5
  4050.         and     edx, 0x1F shl 10
  4051.         shl     eax, 3
  4052.         shl     ecx, 6
  4053.         shl     edx, 9
  4054.         or      eax, ecx
  4055.         or      eax, edx
  4056.         pop     edx ecx
  4057.         ret     4
  4058. ;-----------------------------------------------------------------------------
  4059. align 16
  4060. putimage_get16bpp:
  4061. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4062.         push    ecx edx
  4063.         movzx   eax, word [esi]
  4064.         add     esi, 2
  4065.         mov     ecx, eax
  4066.         mov     edx, eax
  4067.         and     eax, 0x1F
  4068.         and     ecx, 0x3F shl 5
  4069.         and     edx, 0x1F shl 11
  4070.         shl     eax, 3
  4071.         shl     ecx, 5
  4072.         shl     edx, 8
  4073.         or      eax, ecx
  4074.         or      eax, edx
  4075.         pop     edx ecx
  4076.         ret     4
  4077. ;-----------------------------------------------------------------------------
  4078. ;align 4
  4079. ; eax x beginning
  4080. ; ebx y beginning
  4081. ; ecx x end
  4082.         ; edx y end
  4083. ; edi color
  4084. ;__sys_drawbar:
  4085. ;        mov     esi, [current_slot]
  4086. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4087. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4088. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4089. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4090. ;--------------------------------------
  4091. ;align 4
  4092. ;.forced:
  4093. ;        call    vesa20_drawbar
  4094. ;        call    [draw_pointer]
  4095. ;        ret
  4096. ;-----------------------------------------------------------------------------
  4097. align 4
  4098. kb_write_wait_ack:
  4099.  
  4100.         push    ecx edx
  4101.  
  4102.         mov     dl, al
  4103.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4104. .wait_output_ready:
  4105.         in      al, 0x64
  4106.         test    al, 2
  4107.         jz      @f
  4108.         loop    .wait_output_ready
  4109.         mov     ah, 1
  4110.         jmp     .nothing
  4111. @@:
  4112.         mov     al, dl
  4113.         out     0x60, al
  4114.         mov     ecx, 0xfffff; last 0xffff, new value in view of fast CPU's
  4115. .wait_ack:
  4116.         in      al, 0x64
  4117.         test    al, 1
  4118.         jnz     @f
  4119.         loop    .wait_ack
  4120.         mov     ah, 1
  4121.         jmp     .nothing
  4122. @@:
  4123.         in      al, 0x60
  4124.         xor     ah, ah
  4125.  
  4126. .nothing:
  4127.         pop     edx ecx
  4128.  
  4129.         ret
  4130. ;-----------------------------------------------------------------------------
  4131.  
  4132. if used _rdtsc
  4133. _rdtsc:
  4134.         bt      [cpu_caps], CAPS_TSC
  4135.         jnc     ret_rdtsc
  4136.         rdtsc
  4137.         ret
  4138.    ret_rdtsc:
  4139.         mov     edx, 0xffffffff
  4140.         mov     eax, 0xffffffff
  4141.         ret
  4142. end if
  4143.  
  4144. sys_msg_board_str:
  4145.  
  4146.         pushad
  4147.    @@:
  4148.         cmp     [esi], byte 0
  4149.         je      @f
  4150.         mov     ebx, 1
  4151.         movzx   ecx, byte [esi]
  4152.         call    sys_msg_board
  4153.         inc     esi
  4154.         jmp     @b
  4155.    @@:
  4156.         popad
  4157.         ret
  4158.  
  4159. sys_msg_board_byte:
  4160. ; in: al = byte to display
  4161. ; out: nothing
  4162. ; destroys: nothing
  4163.         pushad
  4164.         mov     ecx, 2
  4165.         shl     eax, 24
  4166.         jmp     @f
  4167.  
  4168. sys_msg_board_word:
  4169. ; in: ax = word to display
  4170. ; out: nothing
  4171. ; destroys: nothing
  4172.         pushad
  4173.         mov     ecx, 4
  4174.         shl     eax, 16
  4175.         jmp     @f
  4176.  
  4177. sys_msg_board_dword:
  4178. ; in: eax = dword to display
  4179. ; out: nothing
  4180. ; destroys: nothing
  4181.         pushad
  4182.         mov     ecx, 8
  4183. @@:
  4184.         push    ecx
  4185.         rol     eax, 4
  4186.         push    eax
  4187.         and     al, 0xF
  4188.         cmp     al, 10
  4189.         sbb     al, 69h
  4190.         das
  4191.         mov     cl, al
  4192.         xor     ebx, ebx
  4193.         inc     ebx
  4194.         call    sys_msg_board
  4195.         pop     eax
  4196.         pop     ecx
  4197.         loop    @b
  4198.         popad
  4199.         ret
  4200.  
  4201. msg_board_data_size = 65536 ; Must be power of two
  4202.  
  4203. uglobal
  4204. msg_board_data  rb  msg_board_data_size
  4205. msg_board_count dd  ?
  4206. endg
  4207.  
  4208. iglobal
  4209. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4210. endg
  4211.  
  4212. sys_msg_board:
  4213. ; ebx=1 -> write, cl = byte to write
  4214. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4215.         push    eax ebx
  4216.         mov     eax, ebx
  4217.         mov     ebx, ecx
  4218.         mov     ecx, [msg_board_count]
  4219.         cmp     eax, 1
  4220.         jne     .read
  4221.  
  4222. if defined debug_com_base
  4223.         push    dx ax
  4224. @@: ; wait for empty transmit register
  4225.         mov     dx, debug_com_base+5
  4226.         in      al, dx
  4227.         test    al, 1 shl 5
  4228.         jz      @r
  4229.         mov     dx, debug_com_base      ; Output the byte
  4230.         mov     al, bl
  4231.         out     dx, al
  4232.         pop     ax dx
  4233. end if
  4234.  
  4235.         mov     [msg_board_data+ecx], bl
  4236.         cmp     byte [debug_direct_print], 1
  4237.         jnz     .end
  4238.         pusha
  4239.         lea     edx, [msg_board_data+ecx]
  4240.         mov     ecx, 0x40FFFFFF
  4241.         mov     ebx, [msg_board_pos]
  4242.         mov     edi, 1
  4243.         mov     esi, edi ;1
  4244.         call    dtext
  4245.         popa
  4246.         add     word [msg_board_pos+2], 6
  4247.         cmp     word [msg_board_pos+2], 105*6
  4248.         jnc     @f
  4249.         cmp     bl, 10
  4250.         jnz     .end
  4251. @@:
  4252.         mov     word [msg_board_pos+2], 42*6
  4253.         add     word [msg_board_pos], 10
  4254.         mov     eax, [_display.height]
  4255.         sub     eax, 10
  4256.         cmp     ax, word [msg_board_pos]
  4257.         jnc     @f
  4258.         mov     word [msg_board_pos], 10
  4259. @@:
  4260.         pusha
  4261.         mov     eax, [msg_board_pos]
  4262.         movzx   ebx, ax
  4263.         shr     eax, 16
  4264.         mov     edx, 105*6
  4265.         xor     ecx, ecx
  4266.         mov     edi, 1
  4267.         mov     esi, 9
  4268. @@:
  4269.         call    hline
  4270.         inc     ebx
  4271.         dec     esi
  4272.         jnz     @b
  4273.         popa
  4274. .end:
  4275.         inc     ecx
  4276.         and     ecx, msg_board_data_size - 1
  4277.         mov     [msg_board_count], ecx
  4278. .ret:
  4279.         pop     ebx eax
  4280.         ret
  4281.  
  4282. @@:
  4283.         mov     [esp+32], ecx
  4284.         mov     [esp+20], ecx
  4285.         jmp     .ret
  4286.  
  4287. .read:
  4288.         cmp     eax, 2
  4289.         jne     .ret
  4290.         test    ecx, ecx
  4291.         jz      @b
  4292.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4293.         mov     eax, msg_board_data+1
  4294.         mov     ebx, msg_board_data
  4295.         movzx   edx, byte [ebx]
  4296.         call    memmove
  4297.         dec     [msg_board_count]
  4298.         mov     [esp + 32], edx ;eax
  4299.         mov     [esp + 20], dword 1
  4300.         ret
  4301.  
  4302. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4303. ;; 66 sys function.                                                ;;
  4304. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4305. ;; out eax                                                         ;;
  4306. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4307. iglobal
  4308. align 4
  4309. f66call:
  4310.            dd sys_process_def.1   ; 1 = set keyboard mode
  4311.            dd sys_process_def.2   ; 2 = get keyboard mode
  4312.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4313.            dd sys_process_def.4   ; 4 = set system-wide hotkey
  4314.            dd sys_process_def.5   ; 5 = delete installed hotkey
  4315.            dd sys_process_def.6   ; 6 = disable input, work only hotkeys
  4316.            dd sys_process_def.7   ; 7 = enable input, opposition to f.66.6
  4317. endg
  4318. ;-----------------------------------------------------------------------------
  4319. align 4
  4320. sys_process_def:
  4321.         dec     ebx
  4322.         cmp     ebx, 7
  4323.         jae     .not_support    ;if >=8 then or eax,-1
  4324.  
  4325.         mov     edi, [current_slot_idx]
  4326.         jmp     dword [f66call+ebx*4]
  4327.  
  4328. .not_support:
  4329.         or      eax, -1
  4330.         ret
  4331. ;-----------------------------------------------------------------------------
  4332. align 4
  4333. .1:
  4334.         shl     edi, 8
  4335.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4336.  
  4337.         ret
  4338. ;-----------------------------------------------------------------------------
  4339. align 4
  4340. .2:                             ; 2 = get keyboard mode
  4341.         shl     edi, 8
  4342.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4343.         mov     [esp+32], eax
  4344.         ret
  4345. ;-----------------------------------------------------------------------------
  4346. align 4
  4347. .3:                             ;3 = get keyboard ctrl, alt, shift
  4348.         mov     eax, [kb_state]
  4349.         mov     [esp+32], eax
  4350.         ret
  4351. ;-----------------------------------------------------------------------------
  4352. align 4
  4353. .4:
  4354.         mov     eax, hotkey_list
  4355. @@:
  4356.         cmp     dword [eax+8], 0
  4357.         jz      .found_free
  4358.         add     eax, 16
  4359.         cmp     eax, hotkey_list+16*256
  4360.         jb      @b
  4361.         mov     dword [esp+32], 1
  4362.         ret
  4363. .found_free:
  4364.         mov     [eax+8], edi
  4365.         mov     [eax+4], edx
  4366.         movzx   ecx, cl
  4367.         lea     ecx, [hotkey_scancodes+ecx*4]
  4368.         mov     edx, [ecx]
  4369.         mov     [eax], edx
  4370.         mov     [ecx], eax
  4371.         mov     [eax+12], ecx
  4372.         test    edx, edx
  4373.         jz      @f
  4374.         mov     [edx+12], eax
  4375. @@:
  4376.         and     dword [esp+32], 0
  4377.         ret
  4378. ;-----------------------------------------------------------------------------
  4379. align 4
  4380. .5:
  4381.         movzx   ebx, cl
  4382.         lea     ebx, [hotkey_scancodes+ebx*4]
  4383.         mov     eax, [ebx]
  4384. .scan:
  4385.         test    eax, eax
  4386.         jz      .notfound
  4387.         cmp     [eax+8], edi
  4388.         jnz     .next
  4389.         cmp     [eax+4], edx
  4390.         jz      .found
  4391. .next:
  4392.         mov     eax, [eax]
  4393.         jmp     .scan
  4394. .notfound:
  4395.         mov     dword [esp+32], 1
  4396.         ret
  4397. .found:
  4398.         mov     ecx, [eax]
  4399.         jecxz   @f
  4400.         mov     edx, [eax+12]
  4401.         mov     [ecx+12], edx
  4402. @@:
  4403.         mov     ecx, [eax+12]
  4404.         mov     edx, [eax]
  4405.         mov     [ecx], edx
  4406.         xor     edx, edx
  4407.         mov     [eax+4], edx
  4408.         mov     [eax+8], edx
  4409.         mov     [eax+12], edx
  4410.         mov     [eax], edx
  4411.         mov     [esp+32], edx
  4412.         ret
  4413. ;-----------------------------------------------------------------------------
  4414. align 4
  4415. .6:
  4416.         pushfd
  4417.         cli
  4418.         mov     eax, [PID_lock_input]
  4419.         test    eax, eax
  4420.         jnz     @f
  4421. ; get current PID
  4422.         mov     eax, [current_slot_idx]
  4423.         shl     eax, 5
  4424.         mov     eax, [eax+TASK_TABLE+TASKDATA.pid]
  4425.         ;shl     eax, 8
  4426.         ;mov     eax, [eax+SLOT_BASE+APPDATA.tid]
  4427. ; set current PID for lock input
  4428.         mov     [PID_lock_input], eax
  4429. @@:
  4430.         popfd
  4431.         ret
  4432. ;-----------------------------------------------------------------------------
  4433. align 4
  4434. .7:
  4435.         mov     eax, [PID_lock_input]
  4436.         test    eax, eax
  4437.         jz      @f
  4438. ; get current PID
  4439.         mov     ebx, [current_slot_idx]
  4440.         shl     ebx, 5
  4441.         mov     ebx, [ebx+TASK_TABLE+TASKDATA.pid]
  4442.         ;shl     ebx, 8
  4443.         ;mov     ebx, [ebx+SLOT_BASE+APPDATA.tid]
  4444. ; compare current lock input with current PID
  4445.         cmp     ebx, eax
  4446.         jne     @f
  4447.  
  4448.         xor     eax, eax
  4449.         mov     [PID_lock_input], eax
  4450. @@:
  4451.         ret
  4452. ;-----------------------------------------------------------------------------
  4453. uglobal
  4454.   PID_lock_input dd 0x0
  4455. endg
  4456. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4457. ;; 61 sys function.                                                ;;
  4458. ;; in eax=61,ebx in [1..3]                                         ;;
  4459. ;; out eax                                                         ;;
  4460. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4461. iglobal
  4462. align 4
  4463. f61call:
  4464.            dd sys_gs.1   ; resolution
  4465.            dd sys_gs.2   ; bits per pixel
  4466.            dd sys_gs.3   ; bytes per scanline
  4467. endg
  4468.  
  4469.  
  4470. align 4
  4471.  
  4472. sys_gs:                         ; direct screen access
  4473.         dec     ebx
  4474.         cmp     ebx, 2
  4475.         ja      .not_support
  4476.         jmp     dword [f61call+ebx*4]
  4477. .not_support:
  4478.         or      [esp+32], dword -1
  4479.         ret
  4480.  
  4481.  
  4482. .1:                             ; resolution
  4483.         mov     eax, [_display.width]
  4484.         shl     eax, 16
  4485.         mov     ax, word [_display.height]
  4486.         mov     [esp+32], eax
  4487.         ret
  4488. .2:                             ; bits per pixel
  4489.         mov     eax, [_display.bits_per_pixel]
  4490.         mov     [esp+32], eax
  4491.         ret
  4492. .3:                             ; bytes per scanline
  4493.         mov     eax, [_display.lfb_pitch]
  4494.         mov     [esp+32], eax
  4495.         ret
  4496.  
  4497. align 4
  4498. syscall_getscreensize:                  ; GetScreenSize
  4499.         mov     ax, word [_display.width]
  4500.         dec     ax
  4501.         shl     eax, 16
  4502.         mov     ax, word [_display.height]
  4503.         dec     ax
  4504.         mov     [esp + 32], eax
  4505.         ret
  4506. ;-----------------------------------------------------------------------------
  4507. align 4
  4508. syscall_cdaudio:
  4509. ; ECX - position of CD/DVD-drive
  4510. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  4511. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  4512. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  4513.         cmp     ecx, 11
  4514.         ja      .exit
  4515.  
  4516.         mov     eax, ecx
  4517.         shr     eax, 2
  4518.         lea     eax, [eax*5]
  4519.         mov     al, [eax+DRIVE_DATA+1]
  4520.  
  4521.         push    ecx ebx
  4522.         mov     ebx, ecx
  4523.         and     ebx, 11b
  4524.         shl     ebx, 1
  4525.         mov     cl, 6
  4526.         sub     cl, bl
  4527.         shr     al, cl
  4528.         test    al, 2 ; it's not an ATAPI device
  4529.         pop     ebx ecx
  4530.  
  4531.         jz      .exit
  4532.  
  4533.         cmp     ebx, 4
  4534.         je      .eject
  4535.  
  4536.         cmp     ebx, 5
  4537.         je      .load
  4538. ;--------------------------------------
  4539. .exit:
  4540.         ret
  4541. ;--------------------------------------
  4542. .load:
  4543.         call    .reserve
  4544.         call    LoadMedium
  4545.         jmp     .free
  4546. ;--------------------------------------
  4547. .eject:
  4548.         call    .reserve
  4549.         call    clear_CD_cache
  4550.         call    allow_medium_removal
  4551.         call    EjectMedium
  4552.         jmp     .free
  4553. ;--------------------------------------
  4554. .reserve:
  4555.         call    reserve_cd
  4556.  
  4557.         mov     ebx, ecx
  4558.         inc     ebx
  4559.         mov     [cdpos], ebx
  4560.  
  4561.         mov     eax, ecx
  4562.         shr     eax, 1
  4563.         and     eax, 1
  4564.         inc     eax
  4565.         mov     [ChannelNumber], al
  4566.         mov     eax, ecx
  4567.         and     eax, 1
  4568.         mov     [DiskNumber], al
  4569.         call    reserve_cd_channel
  4570.         ret
  4571. ;--------------------------------------
  4572. .free:
  4573.         call    free_cd_channel
  4574.         and     [cd_status], 0
  4575.         ret
  4576. ;-----------------------------------------------------------------------------
  4577. align 4
  4578. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  4579.         cmp     ebx, [_display.width]
  4580.         jb      @f
  4581.         cmp     ecx, [_display.height]
  4582.         jb      @f
  4583.         xor     eax, eax
  4584.         jmp     .store
  4585. ;--------------------------------------
  4586. align 4
  4587. @@:
  4588.         mov     eax, [d_width_calc_area + ecx*4]
  4589.         add     eax, [_display.win_map]
  4590.         movzx   eax, byte[eax+ebx]        ; get value for current point
  4591. ;--------------------------------------
  4592. align 4
  4593. .store:
  4594.         mov     [esp + 32], eax
  4595.         ret
  4596. ;-----------------------------------------------------------------------------
  4597. align 4
  4598. syscall_getpixel:                       ; GetPixel
  4599.         mov     ecx, [_display.width]
  4600.         xor     edx, edx
  4601.         mov     eax, ebx
  4602.         div     ecx
  4603.         mov     ebx, edx
  4604.         xchg    eax, ebx
  4605.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4606.         call    dword [GETPIXEL]; eax - x, ebx - y
  4607.         mov     [esp + 32], ecx
  4608.         ret
  4609. ;-----------------------------------------------------------------------------
  4610. align 4
  4611. syscall_getarea:
  4612. ;eax = 36
  4613. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4614. ;ecx = [size x]*65536 + [size y]
  4615. ;edx = [start x]*65536 + [start y]
  4616.         pushad
  4617.         mov     edi, ebx
  4618.         mov     eax, edx
  4619.         shr     eax, 16
  4620.         mov     ebx, edx
  4621.         and     ebx, 0xffff
  4622.         dec     eax
  4623.         dec     ebx
  4624.      ; eax - x, ebx - y
  4625.         mov     edx, ecx
  4626.  
  4627.         shr     ecx, 16
  4628.         and     edx, 0xffff
  4629.         mov     esi, ecx
  4630.      ; ecx - size x, edx - size y
  4631.  
  4632.         mov     ebp, edx
  4633.         lea     ebp, [ebp*3]
  4634.         imul    ebp, esi
  4635.         stdcall is_region_userspace, edi, ebp
  4636.         jnz     .exit
  4637.  
  4638.         mov     ebp, edx
  4639.         dec     ebp
  4640.         lea     ebp, [ebp*3]
  4641.  
  4642.         imul    ebp, esi
  4643.  
  4644.         mov     esi, ecx
  4645.         dec     esi
  4646.         lea     esi, [esi*3]
  4647.  
  4648.         add     ebp, esi
  4649.         add     ebp, edi
  4650.  
  4651.         add     ebx, edx
  4652. ;--------------------------------------
  4653. align 4
  4654. .start_y:
  4655.         push    ecx edx
  4656. ;--------------------------------------
  4657. align 4
  4658. .start_x:
  4659.         push    eax ebx ecx
  4660.         add     eax, ecx
  4661.  
  4662.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4663.         call    dword [GETPIXEL]; eax - x, ebx - y
  4664.  
  4665.         mov     [ebp], cx
  4666.         shr     ecx, 16
  4667.         mov     [ebp+2], cl
  4668.  
  4669.         pop     ecx ebx eax
  4670.         sub     ebp, 3
  4671.         dec     ecx
  4672.         jnz     .start_x
  4673.         pop     edx ecx
  4674.         dec     ebx
  4675.         dec     edx
  4676.         jnz     .start_y
  4677.        
  4678. .exit:
  4679.         popad
  4680.         ret
  4681. ;-----------------------------------------------------------------------------
  4682.  
  4683. align 4
  4684. syscall_threads:                        ; CreateThreads
  4685. ;
  4686. ;   ecx=thread entry point
  4687. ;   edx=thread stack pointer
  4688. ;
  4689. ; on return : eax = pid
  4690.  
  4691.         xor     ebx, ebx
  4692.         call    new_sys_threads
  4693.  
  4694.         mov     [esp+32], eax
  4695.         ret
  4696.  
  4697. ;------------------------------------------------------------------------------
  4698. align 4
  4699. calculate_fast_getting_offset_for_WinMapAddress:
  4700. ; calculate data area for fast getting offset to _WinMapAddress
  4701.         xor     eax, eax
  4702.         mov     ecx, [_display.height]
  4703.         mov     edi, d_width_calc_area
  4704.         cld
  4705. @@:
  4706.         stosd
  4707.         add     eax, [_display.width]
  4708.         dec     ecx
  4709.         jnz     @r
  4710.         ret
  4711. ;------------------------------------------------------------------------------
  4712. align 4
  4713. calculate_fast_getting_offset_for_LFB:
  4714. ; calculate data area for fast getting offset to LFB
  4715.         xor     eax, eax
  4716.         mov     ecx, [_display.height]
  4717.         mov     edi, BPSLine_calc_area
  4718.         cld
  4719. @@:
  4720.         stosd
  4721.         add     eax, [_display.lfb_pitch]
  4722.         dec     ecx
  4723.         jnz     @r
  4724.         ret
  4725. ;------------------------------------------------------------------------------
  4726. align 4
  4727. set_screen:
  4728. ; in:
  4729. ; eax - new Screen_Max_X
  4730. ; ecx - new BytesPerScanLine
  4731. ; edx - new Screen_Max_Y
  4732.  
  4733.         pushfd
  4734.         cli
  4735.  
  4736.         mov     [_display.lfb_pitch], ecx
  4737.  
  4738.         mov     [screen_workarea.right], eax
  4739.         mov     [screen_workarea.bottom], edx
  4740.  
  4741.         push    ebx
  4742.         push    esi
  4743.         push    edi
  4744.  
  4745.         pushad
  4746.  
  4747.         cmp     [do_not_touch_winmap], 1
  4748.         je      @f
  4749.  
  4750.         stdcall kernel_free, [_display.win_map]
  4751.  
  4752.         mov     eax, [_display.width]
  4753.         mul     [_display.height]
  4754.         mov     [_display.win_map_size], eax
  4755.  
  4756.         stdcall kernel_alloc, eax
  4757.         mov     [_display.win_map], eax
  4758.         test    eax, eax
  4759.         jz      .epic_fail
  4760. ; store for f.18.24
  4761.         mov     eax, [_display.width]
  4762.         mov     [display_width_standard], eax
  4763.  
  4764.         mov     eax, [_display.height]
  4765.         mov     [display_height_standard], eax
  4766. @@:
  4767.         call    calculate_fast_getting_offset_for_WinMapAddress
  4768. ; for Qemu or non standart video cards
  4769. ; Unfortunately [BytesPerScanLine] does not always
  4770. ;                             equal to [_display.width] * [ScreenBPP] / 8
  4771.         call    calculate_fast_getting_offset_for_LFB
  4772.         popad
  4773.  
  4774.         call    repos_windows
  4775.         xor     eax, eax
  4776.         xor     ebx, ebx
  4777.         mov     ecx, [_display.width]
  4778.         mov     edx, [_display.height]
  4779.         dec     ecx
  4780.         dec     edx
  4781.         call    calculatescreen
  4782.         pop     edi
  4783.         pop     esi
  4784.         pop     ebx
  4785.  
  4786.         popfd
  4787.         ret
  4788.  
  4789. .epic_fail:
  4790.         hlt                     ; Houston, we've had a problem
  4791.  
  4792. ; --------------- APM ---------------------
  4793. uglobal
  4794. apm_entry       dp      0
  4795. apm_vf          dd      0
  4796. endg
  4797.  
  4798. align 4
  4799. sys_apm:
  4800.         xor     eax, eax
  4801.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4802.         jne     @f
  4803.         inc     eax
  4804.         or      dword [esp + 44], eax   ; error
  4805.         add     eax, 7
  4806.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  4807.         ret
  4808.  
  4809. @@:
  4810. ;       xchg    eax, ecx
  4811. ;       xchg    ebx, ecx
  4812.  
  4813.         cmp     dx, 3
  4814.         ja      @f
  4815.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4816.         mov     eax, [apm_vf]
  4817.         mov     [esp + 32], eax
  4818.         shr     eax, 16
  4819.         mov     [esp + 28], eax
  4820.         ret
  4821.  
  4822. @@:
  4823.  
  4824.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4825.         xchg    [master_tab], esi
  4826.         push    esi
  4827.         mov     edi, cr3
  4828.         mov     cr3, edi                ;flush TLB
  4829.  
  4830.         call    pword [apm_entry]       ;call APM BIOS
  4831.  
  4832.         xchg    eax, [esp]
  4833.         mov     [master_tab], eax
  4834.         mov     eax, cr3
  4835.         mov     cr3, eax
  4836.         pop     eax
  4837.  
  4838.         mov     [esp + 4 ], edi
  4839.         mov     [esp + 8], esi
  4840.         mov     [esp + 20], ebx
  4841.         mov     [esp + 24], edx
  4842.         mov     [esp + 28], ecx
  4843.         mov     [esp + 32], eax
  4844.         setc    al
  4845.         and     [esp + 44], byte 0xfe
  4846.         or      [esp + 44], al
  4847.         ret
  4848. ; -----------------------------------------
  4849.  
  4850. align 4
  4851. undefined_syscall:                      ; Undefined system call
  4852.         mov     [esp + 32], dword -1
  4853.         ret
  4854.  
  4855. align 4
  4856. ; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
  4857. ; @param base Base address of region
  4858. ; @param len Lenght of region
  4859. ; @return ZF = 1 if region in userspace memory,
  4860. ;         ZF = 0 otherwise
  4861. proc is_region_userspace stdcall, base:dword, len:dword
  4862.         push    eax
  4863.         mov     eax, [base]
  4864.  
  4865.         cmp     eax, OS_BASE-1
  4866.         ja      @f              ; zf
  4867.  
  4868.         add     eax, [len]
  4869.         jc      @f              ; zf
  4870.         cmp     eax, OS_BASE
  4871.         ja      @f              ; zf
  4872.  
  4873.         cmp     eax, eax        ; ZF
  4874. @@:
  4875.         pop     eax
  4876.         ret
  4877. endp
  4878.  
  4879. if ~ lang eq sp
  4880. diff16 "end of .text segment",0,$
  4881. end if
  4882.  
  4883. include "data32.inc"
  4884.  
  4885. __REV__ = __REV
  4886.  
  4887. if ~ lang eq sp
  4888. diff16 "end of kernel code",0,$
  4889. end if
  4890.