Subversion Repositories Kolibri OS

Rev

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