Subversion Repositories Kolibri OS

Rev

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