Subversion Repositories Kolibri OS

Rev

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