Subversion Repositories Kolibri OS

Rev

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