Subversion Repositories Kolibri OS

Rev

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