Subversion Repositories Kolibri OS

Rev

Rev 4993 | Blame | Last modification | View Log | Download | RSS feed

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