Subversion Repositories Kolibri OS

Rev

Rev 5116 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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