Subversion Repositories Kolibri OS

Rev

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

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