Subversion Repositories Kolibri OS

Rev

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