Subversion Repositories Kolibri OS

Rev

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