Subversion Repositories Kolibri OS

Rev

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

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