Subversion Repositories Kolibri OS

Rev

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

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