Subversion Repositories Kolibri OS

Rev

Rev 8086 | Rev 8089 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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