Subversion Repositories Kolibri OS

Rev

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