Subversion Repositories Kolibri OS

Rev

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

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