Subversion Repositories Kolibri OS

Rev

Rev 4429 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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