Subversion Repositories Kolibri OS

Rev

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