Subversion Repositories Kolibri OS

Rev

Rev 4307 | Rev 4381 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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