Subversion Repositories Kolibri OS

Rev

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

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