Subversion Repositories Kolibri OS

Rev

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