Subversion Repositories Kolibri OS

Rev

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