Subversion Repositories Kolibri OS

Rev

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

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