Subversion Repositories Kolibri OS

Rev

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