Subversion Repositories Kolibri OS

Rev

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