Subversion Repositories Kolibri OS

Rev

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