Subversion Repositories Kolibri OS

Rev

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