Subversion Repositories Kolibri OS

Rev

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

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