Subversion Repositories Kolibri OS

Rev

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