Subversion Repositories Kolibri OS

Rev

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