Subversion Repositories Kolibri OS

Rev

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