Subversion Repositories Kolibri OS

Rev

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