Subversion Repositories Kolibri OS

Rev

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