Subversion Repositories Kolibri OS

Rev

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