Subversion Repositories Kolibri OS

Rev

Rev 4611 | Rev 4641 | 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: 4624 $
  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. ;     cmp  ecx,1
  2252.         dec     ecx
  2253.         jnz     full_table
  2254.   small_table:
  2255.         call    for_all_tables
  2256.         mov     ecx, 10
  2257.         cld
  2258.         rep movsb
  2259.         ret
  2260.    for_all_tables:
  2261.         mov     edi, edx
  2262.         mov     esi, DRIVE_DATA
  2263.         ret
  2264.   full_table:
  2265. ;     cmp  ecx,2
  2266.         dec     ecx
  2267.         jnz     exit_for_anyone
  2268.         call    for_all_tables
  2269.         mov     ecx, DRIVE_DATA_SIZE/4
  2270.         cld
  2271.         rep movsd
  2272.         ret
  2273. ;------------------------------------------------------------------------------
  2274. sysfn_lastkey:          ; 18.12 = return 0 (backward compatibility)
  2275.         and     dword [esp+32], 0
  2276.         ret
  2277. ;------------------------------------------------------------------------------
  2278. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2279.         mov     edi, ecx
  2280.         mov     esi, version_inf
  2281.         mov     ecx, version_end-version_inf
  2282.         rep movsb
  2283.         ret
  2284. ;------------------------------------------------------------------------------
  2285. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2286.      ;wait retrace functions
  2287.  sys_wait_retrace:
  2288.         mov     edx, 0x3da
  2289.  WaitRetrace_loop:
  2290.         in      al, dx
  2291.         test    al, 1000b
  2292.         jz      WaitRetrace_loop
  2293.         and     [esp+32], dword 0
  2294.         ret
  2295. ;------------------------------------------------------------------------------
  2296. align 4
  2297. sysfn_centermouse:      ; 18.15 = mouse centered
  2298. ; removed here by <Lrz>
  2299. ;     call  mouse_centered
  2300. ;* mouse centered - start code- Mario79
  2301. ;mouse_centered:
  2302. ;        push  eax
  2303.         mov     eax, [Screen_Max_X]
  2304.         shr     eax, 1
  2305.         mov     [MOUSE_X], ax
  2306.         mov     eax, [Screen_Max_Y]
  2307.         shr     eax, 1
  2308.         mov     [MOUSE_Y], ax
  2309.         call    wakeup_osloop
  2310. ;        ret
  2311. ;* mouse centered - end code- Mario79
  2312.         xor     eax, eax
  2313.         and     [esp+32], eax
  2314. ;        pop   eax
  2315.         ret
  2316. ;------------------------------------------------------------------------------
  2317. align 4
  2318. sysfn_mouse_acceleration: ; 18.19 = set/get mouse features
  2319.         test    ecx, ecx; get mouse speed factor
  2320.         jnz     .set_mouse_acceleration
  2321.         xor     eax, eax
  2322.         mov     ax, [mouse_speed_factor]
  2323.         mov     [esp+32], eax
  2324.         ret
  2325.  .set_mouse_acceleration:
  2326. ;     cmp  ecx,1  ; set mouse speed factor
  2327.         dec     ecx
  2328.         jnz     .get_mouse_delay
  2329.         mov     [mouse_speed_factor], dx
  2330.         ret
  2331.  .get_mouse_delay:
  2332. ;     cmp  ecx,2  ; get mouse delay
  2333.         dec     ecx
  2334.         jnz     .set_mouse_delay
  2335.         mov     eax, [mouse_delay]
  2336.         mov     [esp+32], eax
  2337.         ret
  2338.  .set_mouse_delay:
  2339. ;     cmp  ecx,3  ; set mouse delay
  2340.         dec     ecx
  2341.         jnz     .set_pointer_position
  2342.         mov     [mouse_delay], edx
  2343.         ret
  2344.  .set_pointer_position:
  2345. ;     cmp  ecx,4  ; set mouse pointer position
  2346.         dec     ecx
  2347.         jnz     .set_mouse_button
  2348.         cmp     dx, word[Screen_Max_Y]
  2349.         ja      .end
  2350.         rol     edx, 16
  2351.         cmp     dx, word[Screen_Max_X]
  2352.         ja      .end
  2353.         mov     [MOUSE_X], edx
  2354.         mov     [mouse_active], 1
  2355.         call    wakeup_osloop
  2356.         ret
  2357.  .set_mouse_button:
  2358. ;     cmp   ecx,5  ; set mouse button features
  2359.         dec     ecx
  2360.         jnz     .end
  2361.         mov     [BTN_DOWN], dl
  2362.         mov     [mouse_active], 1
  2363.         call    wakeup_osloop
  2364.  .end:
  2365.         ret
  2366. ;------------------------------------------------------------------------------
  2367. sysfn_getfreemem:
  2368.         mov     eax, [pg_data.pages_free]
  2369.         shl     eax, 2
  2370.         mov     [esp+32], eax
  2371.         ret
  2372. ;------------------------------------------------------------------------------
  2373. sysfn_getallmem:
  2374.         mov     eax, [MEM_AMOUNT]
  2375.         shr     eax, 10
  2376.         mov     [esp+32], eax
  2377.         ret
  2378. ;------------------------------------------------------------------------------
  2379. sysfn_pid_to_slot:
  2380.         mov     eax, ecx
  2381.         call    pid_to_slot
  2382.         mov     [esp+32], eax
  2383.         ret
  2384. ;------------------------------------------------------------------------------
  2385. sysfn_min_rest_window:
  2386.         pushad
  2387.         mov     eax, edx ; ebx - operating
  2388.         shr     ecx, 1
  2389.         jnc     @f
  2390.         call    pid_to_slot
  2391. @@:
  2392.         or      eax, eax ; eax - number of slot
  2393.         jz      .error
  2394.         cmp     eax, 255    ; varify maximal slot number
  2395.         ja      .error
  2396.         movzx   eax, word [WIN_STACK + eax*2]
  2397.         shr     ecx, 1
  2398.         jc      .restore
  2399.  ; .minimize:
  2400.         call    minimize_window
  2401.         jmp     .exit
  2402. .restore:
  2403.         call    restore_minimized_window
  2404. .exit:
  2405.         popad
  2406.         xor     eax, eax
  2407.         mov     [esp+32], eax
  2408.         ret
  2409. .error:
  2410.         popad
  2411.         xor     eax, eax
  2412.         dec     eax
  2413.         mov     [esp+32], eax
  2414.         ret
  2415. ;------------------------------------------------------------------------------
  2416. sysfn_min_windows:
  2417.         call    minimize_all_window
  2418.         mov     [esp+32], eax
  2419.         call    change_task
  2420.         ret
  2421. ;------------------------------------------------------------------------------
  2422. sysfn_set_screen_sizes:
  2423.         cmp     [SCR_MODE], word 0x13
  2424.         jbe     .exit
  2425.  
  2426.         cmp     [_display.select_cursor], select_cursor
  2427.         jne     .exit
  2428.  
  2429.         cmp     ecx, [display_width_standard]
  2430.         ja      .exit
  2431.  
  2432.         cmp     edx, [display_height_standard]
  2433.         ja      .exit
  2434.  
  2435.         pushfd
  2436.         cli
  2437.         mov     eax, ecx
  2438.         mov     ecx, [_display.pitch]
  2439.         mov     [_display.width], eax
  2440.         dec     eax
  2441.         mov     [_display.height], edx
  2442.         dec     edx
  2443. ; eax - new Screen_Max_X
  2444. ; edx - new Screen_Max_Y
  2445.         mov     [do_not_touch_winmap], 1
  2446.         call    set_screen
  2447.         mov     [do_not_touch_winmap], 0
  2448.         popfd
  2449.         call    change_task
  2450. .exit:
  2451.         ret
  2452. ;------------------------------------------------------------------------------
  2453. uglobal
  2454. screen_workarea RECT
  2455. display_width_standard dd 0
  2456. display_height_standard dd 0
  2457. do_not_touch_winmap db 0
  2458. window_minimize db 0
  2459. sound_flag      db 0
  2460.  
  2461. endg
  2462.  
  2463. UID_NONE=0
  2464. UID_MENUETOS=1   ;official
  2465. UID_KOLIBRI=2    ;russian
  2466.  
  2467. iglobal
  2468. version_inf:
  2469.         db 0,7,7,0  ; version 0.7.7.0
  2470.         db 0
  2471. .rev    dd __REV__
  2472. version_end:
  2473. endg
  2474. ;------------------------------------------------------------------------------
  2475. align 4
  2476. sys_cachetodiskette:
  2477.         cmp     ebx, 1
  2478.         jb      .no_floppy_save
  2479.         cmp     ebx, 2
  2480.         ja      .no_floppy_save
  2481.         call    save_image
  2482.         mov     [esp + 32], eax
  2483.         ret
  2484. .no_floppy_save:
  2485.         mov     [esp + 32], dword 1
  2486.         ret
  2487. ;------------------------------------------------------------------------------
  2488. uglobal
  2489. ;  bgrchanged  dd  0x0
  2490. align 4
  2491. bgrlockpid dd 0
  2492. bgrlock db 0
  2493. endg
  2494. ;------------------------------------------------------------------------------
  2495. align 4
  2496. sys_background:
  2497.         cmp     ebx, 1                     ; BACKGROUND SIZE
  2498.         jnz     nosb1
  2499.         test    ecx, ecx
  2500.         jz      sbgrr
  2501.  
  2502.         test    edx, edx
  2503.         jz      sbgrr
  2504. ;--------------------------------------
  2505. align 4
  2506. @@:
  2507. ;;Maxis use atomic bts for mutexes  4.4.2009
  2508.         bts     dword [bgrlock], 0
  2509.         jnc     @f
  2510.         call    change_task
  2511.         jmp     @b
  2512. ;--------------------------------------
  2513. align 4
  2514. @@:
  2515.         mov     [BgrDataWidth], ecx
  2516.         mov     [BgrDataHeight], edx
  2517. ;    mov   [bgrchanged],1
  2518.  
  2519.         pushad
  2520. ; return memory for old background
  2521.         mov     eax, [img_background]
  2522.         cmp     eax, static_background_data
  2523.         jz      @f
  2524.         stdcall kernel_free, eax
  2525. ;--------------------------------------
  2526. align 4
  2527. @@:
  2528. ; calculate RAW size
  2529.         xor     eax, eax
  2530.         inc     eax
  2531.         cmp     [BgrDataWidth], eax
  2532.         jae     @f
  2533.         mov     [BgrDataWidth], eax
  2534. ;--------------------------------------
  2535. align 4
  2536. @@:
  2537.         cmp     [BgrDataHeight], eax
  2538.         jae     @f
  2539.         mov     [BgrDataHeight], eax
  2540. ;--------------------------------------
  2541. align 4
  2542. @@:
  2543.         mov     eax, [BgrDataWidth]
  2544.         imul    eax, [BgrDataHeight]
  2545.         lea     eax, [eax*3]
  2546. ; it is reserved with aligned to the boundary of 4 KB pages,
  2547. ; otherwise there may be exceptions a page fault for vesa20_drawbackground_tiled
  2548. ; because the 32 bit read is used for  high performance: "mov eax,[esi]"
  2549.         shr     eax, 12
  2550.         inc     eax
  2551.         shl     eax, 12
  2552.         mov     [mem_BACKGROUND], eax
  2553. ; get memory for new background
  2554.         stdcall kernel_alloc, eax
  2555.         test    eax, eax
  2556.         jz      .memfailed
  2557.         mov     [img_background], eax
  2558.         jmp     .exit
  2559. ;--------------------------------------
  2560. align 4
  2561. .memfailed:
  2562. ; revert to static monotone data
  2563.         mov     [img_background], static_background_data
  2564.         xor     eax, eax
  2565.         inc     eax
  2566.         mov     [BgrDataWidth], eax
  2567.         mov     [BgrDataHeight], eax
  2568.         mov     [mem_BACKGROUND], 4
  2569. ;--------------------------------------
  2570. align 4
  2571. .exit:
  2572.         popad
  2573.         mov     [bgrlock], 0
  2574. ;--------------------------------------
  2575. align 4
  2576. sbgrr:
  2577.         ret
  2578. ;------------------------------------------------------------------------------
  2579. align 4
  2580. nosb1:
  2581.         cmp     ebx, 2                     ; SET PIXEL
  2582.         jnz     nosb2
  2583.  
  2584.         mov     eax, [img_background]
  2585.         test    ecx, ecx
  2586.         jz      @f
  2587.         cmp     eax, static_background_data
  2588.         jz      .ret
  2589. ;--------------------------------------
  2590. align 4
  2591. @@:
  2592.         mov     ebx, [mem_BACKGROUND]
  2593.         add     ebx, 4095
  2594.         and     ebx, -4096
  2595.         sub     ebx, 4
  2596.         cmp     ecx, ebx
  2597.         ja      .ret
  2598.  
  2599.         mov     ebx, [eax+ecx]
  2600.         and     ebx, 0xFF000000;255*256*256*256
  2601.         and     edx, 0x00FFFFFF;255*256*256+255*256+255
  2602.         add     edx, ebx
  2603.         mov     [eax+ecx], edx
  2604. ;--------------------------------------
  2605. align 4
  2606. .ret:
  2607.         ret
  2608. ;------------------------------------------------------------------------------
  2609. align 4
  2610. nosb2:
  2611.         cmp     ebx, 3                     ; DRAW BACKGROUND
  2612.         jnz     nosb3
  2613. ;--------------------------------------
  2614. align 4
  2615. draw_background_temp:
  2616.         mov     [background_defined], 1
  2617.         call    force_redraw_background
  2618. ;--------------------------------------
  2619. align 4
  2620. nosb31:
  2621.         ret
  2622. ;------------------------------------------------------------------------------
  2623. align 4
  2624. nosb3:
  2625.         cmp     ebx, 4                     ; TILED / STRETCHED
  2626.         jnz     nosb4
  2627.         cmp     ecx, [BgrDrawMode]
  2628.         je      nosb41
  2629.         mov     [BgrDrawMode], ecx
  2630. ;--------------------------------------
  2631. align 4
  2632. nosb41:
  2633.         ret
  2634. ;------------------------------------------------------------------------------
  2635. align 4
  2636. nosb4:
  2637.         cmp     ebx, 5                     ; BLOCK MOVE TO BGR
  2638.         jnz     nosb5
  2639.         cmp     [img_background], static_background_data
  2640.         jnz     @f
  2641.         test    edx, edx
  2642.         jnz     .fin
  2643.         cmp     esi, 4
  2644.         ja      .fin
  2645. ;--------------------------------------
  2646. align 4
  2647. @@:
  2648.   ; bughere
  2649.         mov     eax, ecx
  2650.         mov     ebx, edx
  2651.         add     ebx, [img_background];IMG_BACKGROUND
  2652.         mov     ecx, esi
  2653.         call    memmove
  2654. ;--------------------------------------
  2655. align 4
  2656. .fin:
  2657.         ret
  2658. ;------------------------------------------------------------------------------
  2659. align 4
  2660. nosb5:
  2661.         cmp     ebx, 6
  2662.         jnz     nosb6
  2663. ;--------------------------------------
  2664. align 4
  2665. ;;Maxis use atomic bts for mutex 4.4.2009
  2666. @@:
  2667.         bts     dword [bgrlock], 0
  2668.         jnc     @f
  2669.         call    change_task
  2670.         jmp     @b
  2671. ;--------------------------------------
  2672. align 4
  2673. @@:
  2674.         mov     eax, [CURRENT_TASK]
  2675.         mov     [bgrlockpid], eax
  2676.         cmp     [img_background], static_background_data
  2677.         jz      .nomem
  2678.         stdcall user_alloc, [mem_BACKGROUND]
  2679.         mov     [esp+32], eax
  2680.         test    eax, eax
  2681.         jz      .nomem
  2682.         mov     ebx, eax
  2683.         shr     ebx, 12
  2684.         or      dword [page_tabs+(ebx-1)*4], DONT_FREE_BLOCK
  2685.         mov     esi, [img_background]
  2686.         shr     esi, 12
  2687.         mov     ecx, [mem_BACKGROUND]
  2688.         add     ecx, 0xFFF
  2689.         shr     ecx, 12
  2690. ;--------------------------------------
  2691. align 4
  2692. .z:
  2693.         mov     eax, [page_tabs+ebx*4]
  2694.         test    al, 1
  2695.         jz      @f
  2696.         call    free_page
  2697. ;--------------------------------------
  2698. align 4
  2699. @@:
  2700.         mov     eax, [page_tabs+esi*4]
  2701.         or      al, PG_UW
  2702.         mov     [page_tabs+ebx*4], eax
  2703.         mov     eax, ebx
  2704.         shl     eax, 12
  2705.         invlpg  [eax]
  2706.         inc     ebx
  2707.         inc     esi
  2708.         loop    .z
  2709.         ret
  2710. ;--------------------------------------
  2711. align 4
  2712. .nomem:
  2713.         and     [bgrlockpid], 0
  2714.         mov     [bgrlock], 0
  2715. ;------------------------------------------------------------------------------
  2716. align 4
  2717. nosb6:
  2718.         cmp     ebx, 7
  2719.         jnz     nosb7
  2720.         cmp     [bgrlock], 0
  2721.         jz      .err
  2722.         mov     eax, [CURRENT_TASK]
  2723.         cmp     [bgrlockpid], eax
  2724.         jnz     .err
  2725.         mov     eax, ecx
  2726.         mov     ebx, ecx
  2727.         shr     eax, 12
  2728.         mov     ecx, [page_tabs+(eax-1)*4]
  2729.         test    cl, USED_BLOCK+DONT_FREE_BLOCK
  2730.         jz      .err
  2731.         jnp     .err
  2732.         push    eax
  2733.         shr     ecx, 12
  2734.         dec     ecx
  2735. ;--------------------------------------
  2736. align 4
  2737. @@:
  2738.         and     dword [page_tabs+eax*4], 0
  2739.         mov     edx, eax
  2740.         shl     edx, 12
  2741.         push    eax
  2742.         invlpg  [edx]
  2743.         pop     eax
  2744.         inc     eax
  2745.         loop    @b
  2746.         pop     eax
  2747.         and     dword [page_tabs+(eax-1)*4], not DONT_FREE_BLOCK
  2748.         stdcall user_free, ebx
  2749.         mov     [esp+32], eax
  2750.         and     [bgrlockpid], 0
  2751.         mov     [bgrlock], 0
  2752.         ret
  2753. ;--------------------------------------
  2754. align 4
  2755. .err:
  2756.         and     dword [esp+32], 0
  2757.         ret
  2758. ;------------------------------------------------------------------------------
  2759. align 4
  2760. nosb7:
  2761.         cmp     ebx, 8
  2762.         jnz     nosb8
  2763.  
  2764.         mov     ecx, [current_slot]
  2765.         xor     eax, eax
  2766.         xchg    eax, [ecx+APPDATA.draw_bgr_x]
  2767.         mov     [esp + 32], eax ; eax = [left]*65536 + [right]
  2768.         xor     eax, eax
  2769.         xchg    eax, [ecx+APPDATA.draw_bgr_y]
  2770.         mov     [esp + 20], eax ; ebx = [top]*65536 + [bottom]
  2771.         ret
  2772. ;------------------------------------------------------------------------------
  2773. align 4
  2774. nosb8:
  2775.         cmp     ebx, 9
  2776.         jnz     nosb9
  2777. ; ecx = [left]*65536 + [right]
  2778. ; edx = [top]*65536 + [bottom]
  2779.         mov     eax, [Screen_Max_X]
  2780.         mov     ebx, [Screen_Max_Y]
  2781. ; check [right]
  2782.         cmp     cx, ax
  2783.         ja      .exit
  2784. ; check [left]
  2785.         ror     ecx, 16
  2786.         cmp     cx, ax
  2787.         ja      .exit
  2788. ; check [bottom]
  2789.         cmp     dx, bx
  2790.         ja      .exit
  2791. ; check [top]
  2792.         ror     edx, 16
  2793.         cmp     dx, bx
  2794.         ja      .exit
  2795.  
  2796.         movzx   eax, cx  ; [left]
  2797.         movzx   ebx, dx  ; [top]
  2798.  
  2799.         shr     ecx, 16 ; [right]
  2800.         shr     edx, 16 ; [bottom]
  2801.  
  2802.         mov     [background_defined], 1
  2803.  
  2804.         mov     [draw_data+32 + RECT.left], eax
  2805.         mov     [draw_data+32 + RECT.top], ebx
  2806.  
  2807.         mov     [draw_data+32 + RECT.right], ecx
  2808.         mov     [draw_data+32 + RECT.bottom], edx
  2809.  
  2810.         inc     byte[REDRAW_BACKGROUND]
  2811.         call    wakeup_osloop
  2812. ;--------------------------------------
  2813. align 4
  2814. .exit:
  2815.         ret
  2816. ;------------------------------------------------------------------------------
  2817. align 4
  2818. nosb9:
  2819.         ret
  2820. ;------------------------------------------------------------------------------
  2821. align 4
  2822. uglobal
  2823.   BG_Rect_X_left_right  dd   0x0
  2824.   BG_Rect_Y_top_bottom  dd   0x0
  2825. endg
  2826. ;------------------------------------------------------------------------------
  2827. align 4
  2828. force_redraw_background:
  2829.         and     [draw_data+32 + RECT.left], 0
  2830.         and     [draw_data+32 + RECT.top], 0
  2831.         push    eax ebx
  2832.         mov     eax, [Screen_Max_X]
  2833.         mov     ebx, [Screen_Max_Y]
  2834.         mov     [draw_data+32 + RECT.right], eax
  2835.         mov     [draw_data+32 + RECT.bottom], ebx
  2836.         pop     ebx eax
  2837.         inc     byte[REDRAW_BACKGROUND]
  2838.         call    wakeup_osloop
  2839.         ret
  2840. ;------------------------------------------------------------------------------
  2841. align 4
  2842. sys_getbackground:
  2843. ;    cmp   eax,1                                  ; SIZE
  2844.         dec     ebx
  2845.         jnz     nogb1
  2846.         mov     eax, [BgrDataWidth]
  2847.         shl     eax, 16
  2848.         mov     ax, word [BgrDataHeight]
  2849.         mov     [esp+32], eax
  2850.         ret
  2851. ;------------------------------------------------------------------------------
  2852. align 4
  2853. nogb1:
  2854. ;    cmp   eax,2                                  ; PIXEL
  2855.         dec     ebx
  2856.         jnz     nogb2
  2857.  
  2858.         mov     eax, [img_background]
  2859.         test    ecx, ecx
  2860.         jz      @f
  2861.         cmp     eax, static_background_data
  2862.         jz      .ret
  2863. ;--------------------------------------
  2864. align 4
  2865. @@:
  2866.         mov     ebx, [mem_BACKGROUND]
  2867.         add     ebx, 4095
  2868.         and     ebx, -4096
  2869.         sub     ebx, 4
  2870.         cmp     ecx, ebx
  2871.         ja      .ret
  2872.  
  2873.         mov     eax, [ecx+eax]
  2874.  
  2875.         and     eax, 0xFFFFFF
  2876.         mov     [esp+32], eax
  2877. ;--------------------------------------
  2878. align 4
  2879. .ret:
  2880.         ret
  2881. ;------------------------------------------------------------------------------
  2882. align 4
  2883. nogb2:
  2884.  
  2885. ;    cmp   eax,4                                  ; TILED / STRETCHED
  2886.         dec     ebx
  2887.         dec     ebx
  2888.         jnz     nogb4
  2889.         mov     eax, [BgrDrawMode]
  2890. ;--------------------------------------
  2891. align 4
  2892. nogb4:
  2893.         mov     [esp+32], eax
  2894.         ret
  2895. ;------------------------------------------------------------------------------
  2896. align 4
  2897. sys_getkey:
  2898.         mov     [esp + 32], dword 1
  2899.         ; test main buffer
  2900.         mov     ebx, [CURRENT_TASK]                          ; TOP OF WINDOW STACK
  2901.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2902.         mov     edx, [TASK_COUNT]
  2903.         cmp     ecx, edx
  2904.         jne     .finish
  2905.         cmp     [KEY_COUNT], byte 0
  2906.         je      .finish
  2907.         movzx   ax, byte [KEY_BUFF + 120 + 2]
  2908.         shl     eax, 8
  2909.         mov     al, byte [KEY_BUFF]
  2910.         shl     eax, 8
  2911.         push    eax
  2912.         dec     byte [KEY_COUNT]
  2913.         and     byte [KEY_COUNT], 127
  2914.         movzx   ecx, byte [KEY_COUNT]
  2915.         add     ecx, 2
  2916.         mov     eax, KEY_BUFF + 1
  2917.         mov     ebx, KEY_BUFF
  2918.         call    memmove
  2919.         add     eax, 120 + 2
  2920.         add     ebx, 120 + 2
  2921.         call    memmove
  2922.         pop     eax
  2923. ;--------------------------------------
  2924. align 4
  2925. .ret_eax:
  2926.         mov     [esp + 32], eax
  2927.         ret
  2928. ;--------------------------------------
  2929. align 4
  2930. .finish:
  2931. ; test hotkeys buffer
  2932.         mov     ecx, hotkey_buffer
  2933. ;--------------------------------------
  2934. align 4
  2935. @@:
  2936.         cmp     [ecx], ebx
  2937.         jz      .found
  2938.         add     ecx, 8
  2939.         cmp     ecx, hotkey_buffer + 120 * 8
  2940.         jb      @b
  2941.         ret
  2942. ;--------------------------------------
  2943. align 4
  2944. .found:
  2945.         mov     ax, [ecx + 6]
  2946.         shl     eax, 16
  2947.         mov     ah, [ecx + 4]
  2948.         mov     al, 2
  2949.         and     dword [ecx + 4], 0
  2950.         and     dword [ecx], 0
  2951.         jmp     .ret_eax
  2952. ;------------------------------------------------------------------------------
  2953. align 4
  2954. sys_getbutton:
  2955.         mov     ebx, [CURRENT_TASK]                         ; TOP OF WINDOW STACK
  2956.         mov     [esp + 32], dword 1
  2957.         movzx   ecx, word [WIN_STACK + ebx * 2]
  2958.         mov     edx, [TASK_COUNT] ; less than 256 processes
  2959.         cmp     ecx, edx
  2960.         jne     .exit
  2961.         movzx   eax, byte [BTN_COUNT]
  2962.         test    eax, eax
  2963.         jz      .exit
  2964.         mov     eax, [BTN_BUFF]
  2965.         and     al, 0xFE                                    ; delete left button bit
  2966.         mov     [BTN_COUNT], byte 0
  2967.         mov     [esp + 32], eax
  2968. ;--------------------------------------
  2969. align 4
  2970. .exit:
  2971.         ret
  2972. ;------------------------------------------------------------------------------
  2973. align 4
  2974. sys_cpuusage:
  2975.  
  2976. ;  RETURN:
  2977. ;
  2978. ;  +00 dword     process cpu usage
  2979. ;  +04  word     position in windowing stack
  2980. ;  +06  word     windowing stack value at current position (cpu nro)
  2981. ;  +10 12 bytes  name
  2982. ;  +22 dword     start in mem
  2983. ;  +26 dword     used mem
  2984. ;  +30 dword     PID , process idenfification number
  2985. ;
  2986.  
  2987.         cmp     ecx, -1 ; who am I ?
  2988.         jne     .no_who_am_i
  2989.         mov     ecx, [CURRENT_TASK]
  2990.   .no_who_am_i:
  2991.         cmp     ecx, max_processes
  2992.         ja      .nofillbuf
  2993.  
  2994. ; +4: word: position of the window of thread in the window stack
  2995.         mov     ax, [WIN_STACK + ecx * 2]
  2996.         mov     [ebx+4], ax
  2997. ; +6: word: number of the thread slot, which window has in the window stack
  2998. ;           position ecx (has no relation to the specific thread)
  2999.         mov     ax, [WIN_POS + ecx * 2]
  3000.         mov     [ebx+6], ax
  3001.  
  3002.         shl     ecx, 5
  3003.  
  3004. ; +0: dword: memory usage
  3005.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.cpu_usage]
  3006.         mov     [ebx], eax
  3007. ; +10: 11 bytes: name of the process
  3008.         push    ecx
  3009.         lea     eax, [ecx*8+SLOT_BASE+APPDATA.app_name]
  3010.         add     ebx, 10
  3011.         mov     ecx, 11
  3012.         call    memmove
  3013.         pop     ecx
  3014.  
  3015. ; +22: address of the process in memory
  3016. ; +26: size of used memory - 1
  3017.         push    edi
  3018.         lea     edi, [ebx+12]
  3019.         xor     eax, eax
  3020.         mov     edx, 0x100000*16
  3021.         cmp     ecx, 1 shl 5
  3022.         je      .os_mem
  3023.         mov     edx, [SLOT_BASE+ecx*8+APPDATA.mem_size]
  3024.         mov     eax, std_application_base_address
  3025. .os_mem:
  3026.         stosd
  3027.         lea     eax, [edx-1]
  3028.         stosd
  3029.  
  3030. ; +30: PID/TID
  3031.         mov     eax, [ecx+CURRENT_TASK+TASKDATA.pid]
  3032.         stosd
  3033.  
  3034.     ; window position and size
  3035.         push    esi
  3036.         lea     esi, [ecx + window_data + WDATA.box]
  3037.         movsd
  3038.         movsd
  3039.         movsd
  3040.         movsd
  3041.  
  3042.     ; Process state (+50)
  3043.         mov     eax, dword [ecx+CURRENT_TASK+TASKDATA.state]
  3044.         stosd
  3045.  
  3046.     ; Window client area box
  3047.         lea     esi, [ecx*8 + SLOT_BASE + APPDATA.wnd_clientbox]
  3048.         movsd
  3049.         movsd
  3050.         movsd
  3051.         movsd
  3052.  
  3053.     ; Window state
  3054.         mov     al, [ecx+window_data+WDATA.fl_wstate]
  3055.         stosb
  3056.  
  3057.     ; Event mask (+71)
  3058.         mov     EAX, dword [ECX+CURRENT_TASK+TASKDATA.event_mask]
  3059.         stosd
  3060.  
  3061.     ; Keyboard mode (+75)
  3062.         mov     al, byte [ecx*8 + SLOT_BASE + APPDATA.keyboard_mode]
  3063.         stosb
  3064.  
  3065.         pop     esi
  3066.         pop     edi
  3067.  
  3068. .nofillbuf:
  3069.     ; return number of processes
  3070.  
  3071.         mov     eax, [TASK_COUNT]
  3072.         mov     [esp+32], eax
  3073.         ret
  3074.  
  3075. align 4
  3076. sys_clock:
  3077.         cli
  3078.   ; Mikhail Lisovin  xx Jan 2005
  3079.   @@:
  3080.         mov     al, 10
  3081.         out     0x70, al
  3082.         in      al, 0x71
  3083.         test    al, al
  3084.         jns     @f
  3085.         mov     esi, 1
  3086.         call    delay_ms
  3087.         jmp     @b
  3088.   @@:
  3089.   ; end Lisovin's fix
  3090.  
  3091.         xor     al, al        ; seconds
  3092.         out     0x70, al
  3093.         in      al, 0x71
  3094.         movzx   ecx, al
  3095.         mov     al, 02        ; minutes
  3096.         shl     ecx, 16
  3097.         out     0x70, al
  3098.         in      al, 0x71
  3099.         movzx   edx, al
  3100.         mov     al, 04        ; hours
  3101.         shl     edx, 8
  3102.         out     0x70, al
  3103.         in      al, 0x71
  3104.         add     ecx, edx
  3105.         movzx   edx, al
  3106.         add     ecx, edx
  3107.         sti
  3108.         mov     [esp + 32], ecx
  3109.         ret
  3110.  
  3111.  
  3112. align 4
  3113.  
  3114. sys_date:
  3115.  
  3116.         cli
  3117.   @@:
  3118.         mov     al, 10
  3119.         out     0x70, al
  3120.         in      al, 0x71
  3121.         test    al, al
  3122.         jns     @f
  3123.         mov     esi, 1
  3124.         call    delay_ms
  3125.         jmp     @b
  3126.   @@:
  3127.  
  3128.         mov     ch, 0
  3129.         mov     al, 7           ; date
  3130.         out     0x70, al
  3131.         in      al, 0x71
  3132.         mov     cl, al
  3133.         mov     al, 8           ; month
  3134.         shl     ecx, 16
  3135.         out     0x70, al
  3136.         in      al, 0x71
  3137.         mov     ch, al
  3138.         mov     al, 9           ; year
  3139.         out     0x70, al
  3140.         in      al, 0x71
  3141.         mov     cl, al
  3142.         sti
  3143.         mov     [esp+32], ecx
  3144.         ret
  3145.  
  3146.  
  3147. ; redraw status
  3148.  
  3149. sys_redrawstat:
  3150.         cmp     ebx, 1
  3151.         jne     no_widgets_away
  3152.         ; buttons away
  3153.         mov     ecx, [CURRENT_TASK]
  3154.   sys_newba2:
  3155.         mov     edi, [BTN_ADDR]
  3156.         cmp     [edi], dword 0  ; empty button list ?
  3157.         je      end_of_buttons_away
  3158.         movzx   ebx, word [edi]
  3159.         inc     ebx
  3160.         mov     eax, edi
  3161.   sys_newba:
  3162.         dec     ebx
  3163.         jz      end_of_buttons_away
  3164.  
  3165.         add     eax, 0x10
  3166.         cmp     cx, [eax]
  3167.         jnz     sys_newba
  3168.  
  3169.         push    eax ebx ecx
  3170.         mov     ecx, ebx
  3171.         inc     ecx
  3172.         shl     ecx, 4
  3173.         mov     ebx, eax
  3174.         add     eax, 0x10
  3175.         call    memmove
  3176.         dec     dword [edi]
  3177.         pop     ecx ebx eax
  3178.  
  3179.         jmp     sys_newba2
  3180.  
  3181.   end_of_buttons_away:
  3182.  
  3183.         ret
  3184.  
  3185.   no_widgets_away:
  3186.  
  3187.         cmp     ebx, 2
  3188.         jnz     srl1
  3189.  
  3190.         mov     edx, [TASK_BASE]      ; return whole screen draw area for this app
  3191.         add     edx, draw_data - CURRENT_TASK
  3192.         mov     [edx + RECT.left], 0
  3193.         mov     [edx + RECT.top], 0
  3194.         mov     eax, [Screen_Max_X]
  3195.         mov     [edx + RECT.right], eax
  3196.         mov     eax, [Screen_Max_Y]
  3197.         mov     [edx + RECT.bottom], eax
  3198.  
  3199.   srl1:
  3200.         ret
  3201.  
  3202. ;ok - 100% work
  3203. ;nt - not tested
  3204. ;---------------------------------------------------------------------------------------------
  3205. ;eax
  3206. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  3207. ;1 - change task. Ret nothing. Block. ok.
  3208. ;2 - performance control
  3209. ; ebx
  3210. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  3211. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  3212. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  3213. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  3214. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  3215. ;eax
  3216. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3217. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  3218. ;---------------------------------------------------------------------------------------------
  3219. iglobal
  3220. align 4
  3221. sheduler:
  3222.         dd      sys_sheduler.00
  3223.         dd      change_task
  3224.         dd      sys_sheduler.02
  3225.         dd      sys_sheduler.03
  3226.         dd      sys_sheduler.04
  3227. endg
  3228. sys_sheduler:
  3229. ;rewritten by <Lrz>  29.12.2009
  3230.         jmp     dword [sheduler+ebx*4]
  3231. ;.shed_counter:
  3232. .00:
  3233.         mov     eax, [context_counter]
  3234.         mov     [esp+32], eax
  3235.         ret
  3236.  
  3237. .02:
  3238. ;.perf_control:
  3239.         inc     ebx                     ;before ebx=2, ebx=3
  3240.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  3241.         jz      cache_disable
  3242.  
  3243.         dec     ebx                     ;ebx=2
  3244.         cmp     ebx, ecx                ;
  3245.         jz      cache_enable            ;if ecx=2 and ebx=2
  3246.  
  3247.         dec     ebx                     ;ebx=1
  3248.         cmp     ebx, ecx
  3249.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  3250.  
  3251.         dec     ebx
  3252.         test    ebx, ecx                ;ebx=0 and ecx=0
  3253.         jz      modify_pce              ;if ecx=0
  3254.  
  3255.         ret
  3256.  
  3257. .03:
  3258. ;.rdmsr_instr:
  3259. ;now counter in ecx
  3260. ;(edx:eax) esi:edi => edx:esi
  3261.         mov     eax, esi
  3262.         mov     ecx, edx
  3263.         rdmsr
  3264.         mov     [esp+32], eax
  3265.         mov     [esp+20], edx           ;ret in ebx?
  3266.         ret
  3267.  
  3268. .04:
  3269. ;.wrmsr_instr:
  3270. ;now counter in ecx
  3271. ;(edx:eax) esi:edi => edx:esi
  3272.         ; Fast Call MSR can't be destroy
  3273.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  3274.         ; включаются/выключаются расширенные возможности
  3275.         cmp     edx, MSR_SYSENTER_CS
  3276.         je      @f
  3277.         cmp     edx, MSR_SYSENTER_ESP
  3278.         je      @f
  3279.         cmp     edx, MSR_SYSENTER_EIP
  3280.         je      @f
  3281.         cmp     edx, MSR_AMD_STAR
  3282.         je      @f
  3283.  
  3284.         mov     eax, esi
  3285.         mov     ecx, edx
  3286.         wrmsr
  3287.         ; mov   [esp + 32], eax
  3288.         ; mov   [esp + 20], edx ;ret in ebx?
  3289. @@:
  3290.         ret
  3291.  
  3292. cache_disable:
  3293.         mov     eax, cr0
  3294.         or      eax, 01100000000000000000000000000000b
  3295.         mov     cr0, eax
  3296.         wbinvd  ;set MESI
  3297.         ret
  3298.  
  3299. cache_enable:
  3300.         mov     eax, cr0
  3301.         and     eax, 10011111111111111111111111111111b
  3302.         mov     cr0, eax
  3303.         ret
  3304.  
  3305. is_cache_enabled:
  3306.         mov     eax, cr0
  3307.         mov     ebx, eax
  3308.         and     eax, 01100000000000000000000000000000b
  3309.         jz      cache_disabled
  3310.         mov     [esp+32], ebx
  3311. cache_disabled:
  3312.         mov     dword [esp+32], eax;0
  3313.         ret
  3314.  
  3315. modify_pce:
  3316.         mov     eax, cr4
  3317. ;       mov ebx,0
  3318. ;       or  bx,100000000b ;pce
  3319. ;       xor eax,ebx ;invert pce
  3320.         bts     eax, 8;pce=cr4[8]
  3321.         mov     cr4, eax
  3322.         mov     [esp+32], eax
  3323.         ret
  3324. ;---------------------------------------------------------------------------------------------
  3325.  
  3326.  
  3327. ; check if pixel is allowed to be drawn
  3328.  
  3329. ;checkpixel:
  3330. ;        push    eax edx
  3331.  
  3332. ;;        mov     edx, [Screen_Max_X] ; screen x size
  3333. ;;        inc     edx
  3334. ;;        imul    edx, ebx
  3335. ;        mov     edx, [d_width_calc_area + ebx*4]
  3336. ;        add     eax, [_WinMapAddress]
  3337. ;        mov     dl, [eax+edx]; lea eax, [...]
  3338.  
  3339. ;        xor     ecx, ecx
  3340. ;        mov     eax, [CURRENT_TASK]
  3341. ;        cmp     al, dl
  3342. ;        setne   cl
  3343.  
  3344. ;        pop     edx eax
  3345. ;        ret
  3346.  
  3347. iglobal
  3348.   cpustring db 'CPU',0
  3349. endg
  3350.  
  3351. uglobal
  3352. background_defined    db    0    ; diamond, 11.04.2006
  3353. endg
  3354. ;-----------------------------------------------------------------------------
  3355. align 4
  3356. checkmisc:
  3357.         cmp     [ctrl_alt_del], 1
  3358.         jne     nocpustart
  3359.  
  3360.         mov     ebp, cpustring
  3361.         call    fs_execute_from_sysdir
  3362.  
  3363.         mov     [ctrl_alt_del], 0
  3364. ;--------------------------------------
  3365. align 4
  3366. nocpustart:
  3367.         cmp     [mouse_active], 1
  3368.         jne     mouse_not_active
  3369.         mov     [mouse_active], 0
  3370.  
  3371.         xor     edi, edi
  3372.         mov     ebx, CURRENT_TASK
  3373.  
  3374.         mov     ecx, [TASK_COUNT]
  3375.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  3376.         shl     eax, 8
  3377.         push    eax
  3378.  
  3379.         movzx   eax, word [MOUSE_X]
  3380.         movzx   edx, word [MOUSE_Y]
  3381. ;--------------------------------------
  3382. align 4
  3383. .set_mouse_event:
  3384.         add     edi, 256
  3385.         add     ebx, 32
  3386.         test    [ebx+TASKDATA.event_mask], 0x80000000
  3387.         jz      .pos_filter
  3388.  
  3389.         cmp     edi, [esp]                      ; skip if filtration active
  3390.         jne     .skip
  3391. ;--------------------------------------
  3392. align 4
  3393. .pos_filter:
  3394.         test    [ebx+TASKDATA.event_mask], 0x40000000
  3395.         jz      .set
  3396.  
  3397.         mov     esi, [ebx-twdw+WDATA.box.left]
  3398.         cmp     eax, esi
  3399.         jb      .skip
  3400.         add     esi, [ebx-twdw+WDATA.box.width]
  3401.         cmp     eax, esi
  3402.         ja      .skip
  3403.  
  3404.         mov     esi, [ebx-twdw+WDATA.box.top]
  3405.         cmp     edx, esi
  3406.         jb      .skip
  3407.         add     esi, [ebx-twdw+WDATA.box.height]
  3408.         cmp     edx, esi
  3409.         ja      .skip
  3410. ;--------------------------------------
  3411. align 4
  3412. .set:
  3413.         or      [edi+SLOT_BASE+APPDATA.event_mask], 100000b  ; set event 6
  3414. ;--------------------------------------
  3415. align 4
  3416. .skip:
  3417.         loop    .set_mouse_event
  3418.  
  3419.         pop     eax
  3420. ;--------------------------------------
  3421. align 4
  3422. mouse_not_active:
  3423.         cmp     byte[REDRAW_BACKGROUND], 0         ; background update ?
  3424.         jz      nobackgr
  3425.  
  3426.         cmp     [background_defined], 0
  3427.         jz      nobackgr
  3428. ;--------------------------------------
  3429. align 4
  3430. backgr:
  3431.         mov     eax, [draw_data+32 + RECT.left]
  3432.         shl     eax, 16
  3433.         add     eax, [draw_data+32 + RECT.right]
  3434.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  3435.  
  3436.         mov     eax, [draw_data+32 + RECT.top]
  3437.         shl     eax, 16
  3438.         add     eax, [draw_data+32 + RECT.bottom]
  3439.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  3440.  
  3441.         call    drawbackground
  3442. ;        DEBUGF  1, "K : drawbackground\n"
  3443. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  3444. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  3445. ;--------- set event 5 start ----------
  3446.         push    ecx edi
  3447.         xor     edi, edi
  3448.         mov     ecx, [TASK_COUNT]
  3449. ;--------------------------------------
  3450. align 4
  3451. set_bgr_event:
  3452.         add     edi, 256
  3453.         mov     eax, [BG_Rect_X_left_right]
  3454.         mov     edx, [BG_Rect_Y_top_bottom]
  3455.         cmp     [edi+SLOT_BASE+APPDATA.draw_bgr_x], 0
  3456.         jz      .set
  3457. .join:
  3458.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3459.         jae     @f
  3460.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x], ax
  3461. @@:
  3462.         shr     eax, 16
  3463.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3464.         jbe     @f
  3465.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_x+2], ax
  3466. @@:
  3467.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3468.         jae     @f
  3469.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y], dx
  3470. @@:
  3471.         shr     edx, 16
  3472.         cmp     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3473.         jbe     @f
  3474.         mov     word [edi+SLOT_BASE+APPDATA.draw_bgr_y+2], dx
  3475. @@:
  3476.         jmp     .common
  3477. .set:
  3478.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_x], eax
  3479.         mov     [edi+SLOT_BASE+APPDATA.draw_bgr_y], edx
  3480. .common:
  3481.         or      [edi+SLOT_BASE+APPDATA.event_mask], 10000b  ; set event 5
  3482.         loop    set_bgr_event
  3483.         pop     edi ecx
  3484. ;--------- set event 5 stop -----------
  3485.         dec     byte[REDRAW_BACKGROUND]    ; got new update request?
  3486.         jnz     backgr
  3487.  
  3488.         xor     eax, eax
  3489.         mov     [draw_data+32 + RECT.left], eax
  3490.         mov     [draw_data+32 + RECT.top], eax
  3491.         mov     [draw_data+32 + RECT.right], eax
  3492.         mov     [draw_data+32 + RECT.bottom], eax
  3493. ;--------------------------------------
  3494. align 4
  3495. nobackgr:
  3496. ; system shutdown request
  3497.         cmp     [SYS_SHUTDOWN], byte 0
  3498.         je      noshutdown
  3499.  
  3500.         mov     edx, [shutdown_processes]
  3501.  
  3502.         cmp     [SYS_SHUTDOWN], dl
  3503.         jne     noshutdown
  3504.  
  3505.         lea     ecx, [edx-1]
  3506.         mov     edx, OS_BASE+0x3040
  3507.         jecxz   no_mark_system_shutdown
  3508. ;--------------------------------------
  3509. align 4
  3510. markz:
  3511.         push    ecx edx
  3512.         cmp     [edx+TASKDATA.state], 9
  3513.         jz      .nokill
  3514.         lea     edx, [(edx-(CURRENT_TASK and 1FFFFFFFh))*8+SLOT_BASE]
  3515.         cmp     [edx+APPDATA.dir_table], sys_pgdir - OS_BASE
  3516.         jz      .nokill
  3517.         call    request_terminate
  3518.         jmp     .common
  3519. .nokill:
  3520.         dec     byte [SYS_SHUTDOWN]
  3521.         xor     eax, eax
  3522. .common:
  3523.         pop     edx ecx
  3524.         test    eax, eax
  3525.         jz      @f
  3526.         mov     [edx+TASKDATA.state], byte 3
  3527. @@:
  3528.         add     edx, 0x20
  3529.         loop    markz
  3530.         call    wakeup_osloop
  3531. ;--------------------------------------
  3532. align 4
  3533. @@:
  3534. no_mark_system_shutdown:
  3535.         dec     byte [SYS_SHUTDOWN]
  3536.         je      system_shutdown
  3537. ;--------------------------------------
  3538. align 4
  3539. noshutdown:
  3540.         mov     eax, [TASK_COUNT]           ; termination
  3541.         mov     ebx, TASK_DATA+TASKDATA.state
  3542.         mov     esi, 1
  3543. ;--------------------------------------
  3544. align 4
  3545. newct:
  3546.         mov     cl, [ebx]
  3547.         cmp     cl, byte 3
  3548.         jz      .terminate
  3549.  
  3550.         cmp     cl, byte 4
  3551.         jnz     .noterminate
  3552. .terminate:
  3553.         pushad
  3554.         mov     ecx, eax
  3555.         shl     ecx, 8
  3556.         add     ecx, SLOT_BASE
  3557.         call    restore_default_cursor_before_killing
  3558.         popad
  3559.  
  3560.         pushad
  3561.         call    terminate
  3562.         popad
  3563.         cmp     byte [SYS_SHUTDOWN], 0
  3564.         jz      .noterminate
  3565.         dec     byte [SYS_SHUTDOWN]
  3566.         je      system_shutdown
  3567.  
  3568. .noterminate:
  3569.         add     ebx, 0x20
  3570.         inc     esi
  3571.         dec     eax
  3572.         jnz     newct
  3573.         ret
  3574. ;-----------------------------------------------------------------------------
  3575. align 4
  3576. redrawscreen:
  3577. ; eax , if process window_data base is eax, do not set flag/limits
  3578.  
  3579.         pushad
  3580.         push    eax
  3581.  
  3582. ;;;         mov   ebx,2
  3583. ;;;         call  delay_hs
  3584.  
  3585.          ;mov   ecx,0               ; redraw flags for apps
  3586.         xor     ecx, ecx
  3587. ;--------------------------------------
  3588. align 4
  3589. newdw2:
  3590.         inc     ecx
  3591.         push    ecx
  3592.  
  3593.         mov     eax, ecx
  3594.         shl     eax, 5
  3595.         add     eax, window_data
  3596.  
  3597.         cmp     eax, [esp+4]
  3598.         je      not_this_task
  3599.                                    ; check if window in redraw area
  3600.         mov     edi, eax
  3601.  
  3602.         cmp     ecx, 1             ; limit for background
  3603.         jz      bgli
  3604.  
  3605.         mov     eax, [edi + WDATA.box.left]
  3606.         mov     ebx, [edi + WDATA.box.top]
  3607.  
  3608.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3609.         cmp     ecx, ebx
  3610.         jb      ricino
  3611.  
  3612.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3613.         cmp     ecx, eax
  3614.         jb      ricino
  3615.  
  3616.         mov     eax, [edi + WDATA.box.left]
  3617.         mov     ebx, [edi + WDATA.box.top]
  3618.         mov     ecx, [edi + WDATA.box.width]
  3619.         mov     edx, [edi + WDATA.box.height]
  3620.         add     ecx, eax
  3621.         add     edx, ebx
  3622.  
  3623.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3624.         cmp     edx, eax
  3625.         jb      ricino
  3626.  
  3627.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3628.         cmp     ecx, eax
  3629.         jb      ricino
  3630. ;--------------------------------------
  3631. align 4
  3632. bgli:
  3633.         cmp     dword[esp], 1
  3634.         jnz     .az
  3635.  
  3636.         cmp     byte[REDRAW_BACKGROUND], 0
  3637.         jz      .az
  3638.  
  3639.         mov     dl, 0
  3640.         lea     eax, [edi+draw_data-window_data]
  3641.         mov     ebx, [draw_limits.left]
  3642.         cmp     ebx, [eax+RECT.left]
  3643.         jae     @f
  3644.  
  3645.         mov     [eax+RECT.left], ebx
  3646.         mov     dl, 1
  3647. ;--------------------------------------
  3648. align 4
  3649. @@:
  3650.         mov     ebx, [draw_limits.top]
  3651.         cmp     ebx, [eax+RECT.top]
  3652.         jae     @f
  3653.  
  3654.         mov     [eax+RECT.top], ebx
  3655.         mov     dl, 1
  3656. ;--------------------------------------
  3657. align 4
  3658. @@:
  3659.         mov     ebx, [draw_limits.right]
  3660.         cmp     ebx, [eax+RECT.right]
  3661.         jbe     @f
  3662.  
  3663.         mov     [eax+RECT.right], ebx
  3664.         mov     dl, 1
  3665. ;--------------------------------------
  3666. align 4
  3667. @@:
  3668.         mov     ebx, [draw_limits.bottom]
  3669.         cmp     ebx, [eax+RECT.bottom]
  3670.         jbe     @f
  3671.  
  3672.         mov     [eax+RECT.bottom], ebx
  3673.         mov     dl, 1
  3674. ;--------------------------------------
  3675. align 4
  3676. @@:
  3677.         add     byte[REDRAW_BACKGROUND], dl
  3678.         call    wakeup_osloop
  3679.         jmp     newdw8
  3680. ;--------------------------------------
  3681. align 4
  3682. .az:
  3683.         mov     eax, edi
  3684.         add     eax, draw_data-window_data
  3685.  
  3686.         mov     ebx, [draw_limits.left]        ; set limits
  3687.         mov     [eax + RECT.left], ebx
  3688.         mov     ebx, [draw_limits.top]
  3689.         mov     [eax + RECT.top], ebx
  3690.         mov     ebx, [draw_limits.right]
  3691.         mov     [eax + RECT.right], ebx
  3692.         mov     ebx, [draw_limits.bottom]
  3693.         mov     [eax + RECT.bottom], ebx
  3694.  
  3695.         sub     eax, draw_data-window_data
  3696.  
  3697.         cmp     dword [esp], 1
  3698.         jne     nobgrd
  3699.         inc     byte[REDRAW_BACKGROUND]
  3700.         call    wakeup_osloop
  3701. ;--------------------------------------
  3702. align 4
  3703. newdw8:
  3704. nobgrd:
  3705. ;--------------------------------------
  3706.         push    eax  edi ebp
  3707.         mov     edi, [esp+12]
  3708.         cmp     edi, 1
  3709.         je      .found
  3710.  
  3711.         mov     eax, [draw_limits.left]
  3712.         mov     ebx, [draw_limits.top]
  3713.         mov     ecx, [draw_limits.right]
  3714.         sub     ecx, eax
  3715.         test    ecx, ecx
  3716.         jz      .not_found
  3717.  
  3718.         mov     edx, [draw_limits.bottom]
  3719.         sub     edx, ebx
  3720.         test    edx, edx
  3721.         jz      .not_found
  3722.  
  3723. ; eax - x, ebx - y
  3724. ; ecx - size x, edx - size y
  3725.         add     ebx, edx
  3726. ;--------------------------------------
  3727. align 4
  3728. .start_y:
  3729.         push    ecx
  3730. ;--------------------------------------
  3731. align 4
  3732. .start_x:
  3733.         add     eax, ecx
  3734.         mov     ebp, [d_width_calc_area + ebx*4]
  3735.         add     ebp, [_WinMapAddress]
  3736.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3737.         cmp     ebp, edi
  3738.         jne     @f
  3739.  
  3740.         pop     ecx
  3741.         jmp     .found
  3742. ;--------------------------------------
  3743. align 4
  3744. @@:
  3745.         sub     eax, ecx
  3746.  
  3747.         dec     ecx
  3748.         jnz     .start_x
  3749.  
  3750.         pop     ecx
  3751.         dec     ebx
  3752.         dec     edx
  3753.         jnz     .start_y
  3754. ;--------------------------------------
  3755. align 4
  3756. .not_found:
  3757.         pop     ebp edi eax
  3758.         jmp     ricino
  3759. ;--------------------------------------
  3760. align 4
  3761. .found:
  3762.         pop     ebp edi eax
  3763.  
  3764.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3765. ;--------------------------------------
  3766. align 4
  3767. ricino:
  3768. not_this_task:
  3769.         pop     ecx
  3770.  
  3771.         cmp     ecx, [TASK_COUNT]
  3772.         jle     newdw2
  3773.  
  3774.         pop     eax
  3775.         popad
  3776.         ret
  3777. ;-----------------------------------------------------------------------------
  3778. align 4
  3779. calculatebackground:   ; background
  3780.         mov     edi, [_WinMapAddress]              ; set os to use all pixels
  3781.         mov     eax, 0x01010101
  3782.         mov     ecx, [_WinMapSize]
  3783.         shr     ecx, 2
  3784.         rep stosd
  3785.  
  3786.         mov     byte[REDRAW_BACKGROUND], 0            ; do not draw background!
  3787.         ret
  3788. ;-----------------------------------------------------------------------------
  3789. uglobal
  3790.   imax    dd 0x0
  3791. endg
  3792. ;-----------------------------------------------------------------------------
  3793. align 4
  3794. delay_ms:     ; delay in 1/1000 sec
  3795.         push    eax
  3796.         push    ecx
  3797.  
  3798.         mov     ecx, esi
  3799.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3800.         imul    ecx, 33941
  3801.         shr     ecx, 9
  3802.         ; </CPU clock fix>
  3803.  
  3804.         in      al, 0x61
  3805.         and     al, 0x10
  3806.         mov     ah, al
  3807.         cld
  3808. ;--------------------------------------
  3809. align 4
  3810. cnt1:
  3811.         in      al, 0x61
  3812.         and     al, 0x10
  3813.         cmp     al, ah
  3814.         jz      cnt1
  3815.  
  3816.         mov     ah, al
  3817.         loop    cnt1
  3818.  
  3819.         pop     ecx
  3820.         pop     eax
  3821.         ret
  3822. ;-----------------------------------------------------------------------------
  3823. align 4
  3824. set_app_param:
  3825.         mov     edi, [TASK_BASE]
  3826.         mov     eax, ebx
  3827.         btr     eax, 3                           ; move MOUSE_FILTRATION
  3828.         mov     ebx, [current_slot]              ; bit into event_filter
  3829.         setc    byte [ebx+APPDATA.event_filter]
  3830.         xchg    eax, [edi + TASKDATA.event_mask] ; set new event mask
  3831.         mov     [esp+32], eax                    ; return old mask value
  3832.         ret
  3833. ;-----------------------------------------------------------------------------
  3834.  
  3835. ; this is for syscall
  3836. proc delay_hs_unprotected
  3837.         call    unprotect_from_terminate
  3838.         call    delay_hs
  3839.         call    protect_from_terminate
  3840.         ret
  3841. endp
  3842.  
  3843. if 1
  3844. align 4
  3845. delay_hs:     ; delay in 1/100 secs
  3846. ; ebx = delay time
  3847.  
  3848.         pushad
  3849.         push    ebx
  3850.         xor     esi, esi
  3851.         mov     ecx, MANUAL_DESTROY
  3852.         call    create_event
  3853.         test    eax, eax
  3854.         jz      .done
  3855.  
  3856.         mov     ebx, edx
  3857.         mov     ecx, [esp]
  3858.         push    edx
  3859.         push    eax
  3860.         call    wait_event_timeout
  3861.         pop     eax
  3862.         pop     ebx
  3863.         call    destroy_event
  3864. .done:
  3865.         add     esp, 4
  3866.         popad
  3867.         ret
  3868.  
  3869. else
  3870.  
  3871. align 4
  3872. delay_hs:     ; delay in 1/100 secs
  3873. ; ebx = delay time
  3874.         push    ecx
  3875.         push    edx
  3876.  
  3877.         mov     edx, [timer_ticks]
  3878. ;--------------------------------------
  3879. align 4
  3880. newtic:
  3881.         mov     ecx, [timer_ticks]
  3882.         sub     ecx, edx
  3883.         cmp     ecx, ebx
  3884.         jae     zerodelay
  3885.  
  3886.         call    change_task
  3887.  
  3888.         jmp     newtic
  3889. ;--------------------------------------
  3890. align 4
  3891. zerodelay:
  3892.         pop     edx
  3893.         pop     ecx
  3894.         ret
  3895. end if
  3896.  
  3897. ;-----------------------------------------------------------------------------
  3898. align 16        ;very often call this subrutine
  3899. memmove:       ; memory move in bytes
  3900. ; eax = from
  3901. ; ebx = to
  3902. ; ecx = no of bytes
  3903.         test    ecx, ecx
  3904.         jle     .ret
  3905.  
  3906.         push    esi edi ecx
  3907.  
  3908.         mov     edi, ebx
  3909.         mov     esi, eax
  3910.  
  3911.         test    ecx, not 11b
  3912.         jz      @f
  3913.  
  3914.         push    ecx
  3915.         shr     ecx, 2
  3916.         rep movsd
  3917.         pop     ecx
  3918.         and     ecx, 11b
  3919.         jz      .finish
  3920. ;--------------------------------------
  3921. align 4
  3922. @@:
  3923.         rep movsb
  3924. ;--------------------------------------
  3925. align 4
  3926. .finish:
  3927.         pop     ecx edi esi
  3928. ;--------------------------------------
  3929. align 4
  3930. .ret:
  3931.         ret
  3932. ;-----------------------------------------------------------------------------
  3933. ; <diamond> Sysfunction 34, read_floppy_file, is obsolete. Use 58 or 70 function instead.
  3934. ;align 4
  3935. ;
  3936. ;read_floppy_file:
  3937. ;
  3938. ;; as input
  3939. ;;
  3940. ;; eax pointer to file
  3941. ;; ebx file lenght
  3942. ;; ecx start 512 byte block number
  3943. ;; edx number of blocks to read
  3944. ;; esi pointer to return/work area (atleast 20 000 bytes)
  3945. ;;
  3946. ;;
  3947. ;; on return
  3948. ;;
  3949. ;; eax = 0 command succesful
  3950. ;;       1 no fd base and/or partition defined
  3951. ;;       2 yet unsupported FS
  3952. ;;       3 unknown FS
  3953. ;;       4 partition not defined at hd
  3954. ;;       5 file not found
  3955. ;; ebx = size of file
  3956. ;
  3957. ;     mov   edi,[TASK_BASE]
  3958. ;     add   edi,0x10
  3959. ;     add   esi,[edi]
  3960. ;     add   eax,[edi]
  3961. ;
  3962. ;     pushad
  3963. ;     mov  edi,esi
  3964. ;     add  edi,1024
  3965. ;     mov  esi,0x100000+19*512
  3966. ;     sub  ecx,1
  3967. ;     shl  ecx,9
  3968. ;     add  esi,ecx
  3969. ;     shl  edx,9
  3970. ;     mov  ecx,edx
  3971. ;     cld
  3972. ;     rep  movsb
  3973. ;     popad
  3974. ;
  3975. ;     mov   [esp+36],eax
  3976. ;     mov   [esp+24],ebx
  3977. ;     ret
  3978.  
  3979.  
  3980.  
  3981. align 4
  3982. set_io_access_rights:
  3983.         push    edi eax
  3984.         mov     edi, tss._io_map_0
  3985. ;     mov   ecx,eax
  3986. ;     and   ecx,7    ; offset in byte
  3987. ;     shr   eax,3    ; number of byte
  3988. ;     add   edi,eax
  3989. ;     mov   ebx,1
  3990. ;     shl   ebx,cl
  3991.         test    ebp, ebp
  3992. ;     cmp   ebp,0                ; enable access - ebp = 0
  3993.         jnz     .siar1
  3994. ;     not   ebx
  3995. ;     and   [edi],byte bl
  3996.         btr     [edi], eax
  3997.         pop     eax edi
  3998.         ret
  3999. .siar1:
  4000.         bts     [edi], eax
  4001.   ;  or    [edi],byte bl        ; disable access - ebp = 1
  4002.         pop     eax edi
  4003.         ret
  4004. ;reserve/free group of ports
  4005. ;  * eax = 46 - number function
  4006. ;  * ebx = 0 - reserve, 1 - free
  4007. ;  * ecx = number start arrea of ports
  4008. ;  * edx = number end arrea of ports (include last number of port)
  4009. ;Return value:
  4010. ;  * eax = 0 - succesful
  4011. ;  * eax = 1 - error
  4012. ;  * The system has reserve this ports:
  4013. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  4014. ;destroys eax,ebx, ebp
  4015. r_f_port_area:
  4016.  
  4017.         test    ebx, ebx
  4018.         jnz     free_port_area
  4019. ;     je    r_port_area
  4020. ;     jmp   free_port_area
  4021.  
  4022. ;   r_port_area:
  4023.  
  4024. ;     pushad
  4025.  
  4026.         cmp     ecx, edx      ; beginning > end ?
  4027.         ja      rpal1
  4028.         cmp     edx, 65536
  4029.         jae     rpal1
  4030.         mov     eax, [RESERVED_PORTS]
  4031.         test    eax, eax      ; no reserved areas ?
  4032.         je      rpal2
  4033.         cmp     eax, 255      ; max reserved
  4034.         jae     rpal1
  4035.  rpal3:
  4036.         mov     ebx, eax
  4037.         shl     ebx, 4
  4038.         add     ebx, RESERVED_PORTS
  4039.         cmp     ecx, [ebx+8]
  4040.         ja      rpal4
  4041.         cmp     edx, [ebx+4]
  4042.         jae     rpal1
  4043. ;     jb    rpal4
  4044. ;     jmp   rpal1
  4045.  rpal4:
  4046.         dec     eax
  4047.         jnz     rpal3
  4048.         jmp     rpal2
  4049.    rpal1:
  4050. ;     popad
  4051. ;     mov   eax,1
  4052.         xor     eax, eax
  4053.         inc     eax
  4054.         ret
  4055.    rpal2:
  4056. ;     popad
  4057.      ; enable port access at port IO map
  4058.         cli
  4059.         pushad                        ; start enable io map
  4060.  
  4061.         cmp     edx, 65536;16384
  4062.         jae     no_unmask_io; jge
  4063.         mov     eax, ecx
  4064. ;       push    ebp
  4065.         xor     ebp, ebp               ; enable - eax = port
  4066. new_port_access:
  4067. ;     pushad
  4068.         call    set_io_access_rights
  4069. ;     popad
  4070.         inc     eax
  4071.         cmp     eax, edx
  4072.         jbe     new_port_access
  4073. ;       pop     ebp
  4074. no_unmask_io:
  4075.         popad                         ; end enable io map
  4076.         sti
  4077.  
  4078.         mov     eax, [RESERVED_PORTS]
  4079.         add     eax, 1
  4080.         mov     [RESERVED_PORTS], eax
  4081.         shl     eax, 4
  4082.         add     eax, RESERVED_PORTS
  4083.         mov     ebx, [TASK_BASE]
  4084.         mov     ebx, [ebx+TASKDATA.pid]
  4085.         mov     [eax], ebx
  4086.         mov     [eax+4], ecx
  4087.         mov     [eax+8], edx
  4088.  
  4089.         xor     eax, eax
  4090.         ret
  4091.  
  4092. free_port_area:
  4093.  
  4094. ;     pushad
  4095.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  4096.         test    eax, eax
  4097.         jz      frpal2
  4098.         mov     ebx, [TASK_BASE]
  4099.         mov     ebx, [ebx+TASKDATA.pid]
  4100.    frpal3:
  4101.         mov     edi, eax
  4102.         shl     edi, 4
  4103.         add     edi, RESERVED_PORTS
  4104.         cmp     ebx, [edi]
  4105.         jne     frpal4
  4106.         cmp     ecx, [edi+4]
  4107.         jne     frpal4
  4108.         cmp     edx, [edi+8]
  4109.         jne     frpal4
  4110.         jmp     frpal1
  4111.    frpal4:
  4112.         dec     eax
  4113.         jnz     frpal3
  4114.    frpal2:
  4115. ;     popad
  4116.         inc     eax
  4117.         ret
  4118.    frpal1:
  4119.         push    ecx
  4120.         mov     ecx, 256
  4121.         sub     ecx, eax
  4122.         shl     ecx, 4
  4123.         mov     esi, edi
  4124.         add     esi, 16
  4125.         cld
  4126.         rep movsb
  4127.  
  4128.         dec     dword [RESERVED_PORTS]
  4129. ;popad
  4130. ;disable port access at port IO map
  4131.  
  4132. ;     pushad                        ; start disable io map
  4133.         pop     eax     ;start port
  4134.         cmp     edx, 65536;16384
  4135.         jge     no_mask_io
  4136.  
  4137. ;     mov   eax,ecx
  4138.         xor     ebp, ebp
  4139.         inc     ebp
  4140. new_port_access_disable:
  4141. ;     pushad
  4142. ;     mov   ebp,1                  ; disable - eax = port
  4143.         call    set_io_access_rights
  4144. ;     popad
  4145.         inc     eax
  4146.         cmp     eax, edx
  4147.         jbe     new_port_access_disable
  4148. no_mask_io:
  4149. ;     popad                         ; end disable io map
  4150.         xor     eax, eax
  4151.         ret
  4152. ;-----------------------------------------------------------------------------
  4153. align 4
  4154. drawbackground:
  4155. dbrv20:
  4156.         cmp     [BgrDrawMode], dword 1
  4157.         jne     bgrstr
  4158.         call    vesa20_drawbackground_tiled
  4159. ;        call    [draw_pointer]
  4160.         call    __sys_draw_pointer
  4161.         ret
  4162. ;--------------------------------------
  4163. align 4
  4164. bgrstr:
  4165.         call    vesa20_drawbackground_stretch
  4166. ;        call    [draw_pointer]
  4167.         call    __sys_draw_pointer
  4168.         ret
  4169. ;-----------------------------------------------------------------------------
  4170. align 4
  4171. syscall_putimage:                       ; PutImage
  4172. sys_putimage:
  4173.         test    ecx, 0x80008000
  4174.         jnz     .exit
  4175.         test    ecx, 0x0000FFFF
  4176.         jz      .exit
  4177.         test    ecx, 0xFFFF0000
  4178.         jnz     @f
  4179. ;--------------------------------------
  4180. align 4
  4181. .exit:
  4182.         ret
  4183. ;--------------------------------------
  4184. align 4
  4185. @@:
  4186.         mov     edi, [current_slot]
  4187.         add     dx, word[edi+APPDATA.wnd_clientbox.top]
  4188.         rol     edx, 16
  4189.         add     dx, word[edi+APPDATA.wnd_clientbox.left]
  4190.         rol     edx, 16
  4191. ;--------------------------------------
  4192. align 4
  4193. .forced:
  4194.         push    ebp esi 0
  4195.         mov     ebp, putimage_get24bpp
  4196.         mov     esi, putimage_init24bpp
  4197. ;--------------------------------------
  4198. align 4
  4199. sys_putimage_bpp:
  4200.         call    vesa20_putimage
  4201.         pop     ebp esi ebp
  4202.         ret
  4203. ;        jmp     [draw_pointer]
  4204. ;-----------------------------------------------------------------------------
  4205. align 4
  4206. sys_putimage_palette:
  4207. ; ebx = pointer to image
  4208. ; ecx = [xsize]*65536 + [ysize]
  4209. ; edx = [xstart]*65536 + [ystart]
  4210. ; esi = number of bits per pixel, must be 8, 24 or 32
  4211. ; edi = pointer to palette
  4212. ; ebp = row delta
  4213.         mov     eax, [CURRENT_TASK]
  4214.         shl     eax, 8
  4215.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.top]
  4216.         rol     edx, 16
  4217.         add     dx, word [eax+SLOT_BASE+APPDATA.wnd_clientbox.left]
  4218.         rol     edx, 16
  4219. ;--------------------------------------
  4220. align 4
  4221. .forced:
  4222.         cmp     esi, 1
  4223.         jnz     @f
  4224.         push    edi
  4225.         mov     eax, [edi+4]
  4226.         sub     eax, [edi]
  4227.         push    eax
  4228.         push    dword [edi]
  4229.         push    0ffffff80h
  4230.         mov     edi, esp
  4231.         call    put_mono_image
  4232.         add     esp, 12
  4233.         pop     edi
  4234.         ret
  4235. ;--------------------------------------
  4236. align 4
  4237. @@:
  4238.         cmp     esi, 2
  4239.         jnz     @f
  4240.         push    edi
  4241.         push    0ffffff80h
  4242.         mov     edi, esp
  4243.         call    put_2bit_image
  4244.         pop     eax
  4245.         pop     edi
  4246.         ret
  4247. ;--------------------------------------
  4248. align 4
  4249. @@:
  4250.         cmp     esi, 4
  4251.         jnz     @f
  4252.         push    edi
  4253.         push    0ffffff80h
  4254.         mov     edi, esp
  4255.         call    put_4bit_image
  4256.         pop     eax
  4257.         pop     edi
  4258.         ret
  4259. ;--------------------------------------
  4260. align 4
  4261. @@:
  4262.         push    ebp esi ebp
  4263.         cmp     esi, 8
  4264.         jnz     @f
  4265.         mov     ebp, putimage_get8bpp
  4266.         mov     esi, putimage_init8bpp
  4267.         jmp     sys_putimage_bpp
  4268. ;--------------------------------------
  4269. align 4
  4270. @@:
  4271.         cmp     esi, 9
  4272.         jnz     @f
  4273.         mov     ebp, putimage_get9bpp
  4274.         mov     esi, putimage_init9bpp
  4275.         jmp     sys_putimage_bpp
  4276. ;--------------------------------------
  4277. align 4
  4278. @@:
  4279.         cmp     esi, 15
  4280.         jnz     @f
  4281.         mov     ebp, putimage_get15bpp
  4282.         mov     esi, putimage_init15bpp
  4283.         jmp     sys_putimage_bpp
  4284. ;--------------------------------------
  4285. align 4
  4286. @@:
  4287.         cmp     esi, 16
  4288.         jnz     @f
  4289.         mov     ebp, putimage_get16bpp
  4290.         mov     esi, putimage_init16bpp
  4291.         jmp     sys_putimage_bpp
  4292. ;--------------------------------------
  4293. align 4
  4294. @@:
  4295.         cmp     esi, 24
  4296.         jnz     @f
  4297.         mov     ebp, putimage_get24bpp
  4298.         mov     esi, putimage_init24bpp
  4299.         jmp     sys_putimage_bpp
  4300. ;--------------------------------------
  4301. align 4
  4302. @@:
  4303.         cmp     esi, 32
  4304.         jnz     @f
  4305.         mov     ebp, putimage_get32bpp
  4306.         mov     esi, putimage_init32bpp
  4307.         jmp     sys_putimage_bpp
  4308. ;--------------------------------------
  4309. align 4
  4310. @@:
  4311.         pop     ebp esi ebp
  4312.         ret
  4313. ;-----------------------------------------------------------------------------
  4314. align 4
  4315. put_mono_image:
  4316.         push    ebp esi ebp
  4317.         mov     ebp, putimage_get1bpp
  4318.         mov     esi, putimage_init1bpp
  4319.         jmp     sys_putimage_bpp
  4320. ;-----------------------------------------------------------------------------
  4321. align 4
  4322. put_2bit_image:
  4323.         push    ebp esi ebp
  4324.         mov     ebp, putimage_get2bpp
  4325.         mov     esi, putimage_init2bpp
  4326.         jmp     sys_putimage_bpp
  4327. ;-----------------------------------------------------------------------------
  4328. align 4
  4329. put_4bit_image:
  4330.         push    ebp esi ebp
  4331.         mov     ebp, putimage_get4bpp
  4332.         mov     esi, putimage_init4bpp
  4333.         jmp     sys_putimage_bpp
  4334. ;-----------------------------------------------------------------------------
  4335. align 4
  4336. putimage_init24bpp:
  4337.         lea     eax, [eax*3]
  4338. putimage_init8bpp:
  4339. putimage_init9bpp:
  4340.         ret
  4341. ;-----------------------------------------------------------------------------
  4342. align 16
  4343. putimage_get24bpp:
  4344.         movzx   eax, byte [esi+2]
  4345.         shl     eax, 16
  4346.         mov     ax, [esi]
  4347.         add     esi, 3
  4348.         ret     4
  4349. ;-----------------------------------------------------------------------------
  4350. align 16
  4351. putimage_get8bpp:
  4352.         movzx   eax, byte [esi]
  4353.         push    edx
  4354.         mov     edx, [esp+8]
  4355.         mov     eax, [edx+eax*4]
  4356.         pop     edx
  4357.         inc     esi
  4358.         ret     4
  4359. ;-----------------------------------------------------------------------------
  4360. align 16
  4361. putimage_get9bpp:
  4362.         lodsb
  4363.         mov     ah, al
  4364.         shl     eax, 8
  4365.         mov     al, ah
  4366.         ret     4
  4367. ;-----------------------------------------------------------------------------
  4368. align 4
  4369. putimage_init1bpp:
  4370.         add     eax, ecx
  4371.         push    ecx
  4372.         add     eax, 7
  4373.         add     ecx, 7
  4374.         shr     eax, 3
  4375.         shr     ecx, 3
  4376.         sub     eax, ecx
  4377.         pop     ecx
  4378.         ret
  4379. ;-----------------------------------------------------------------------------
  4380. align 16
  4381. putimage_get1bpp:
  4382.         push    edx
  4383.         mov     edx, [esp+8]
  4384.         mov     al, [edx]
  4385.         add     al, al
  4386.         jnz     @f
  4387.         lodsb
  4388.         adc     al, al
  4389. @@:
  4390.         mov     [edx], al
  4391.         sbb     eax, eax
  4392.         and     eax, [edx+8]
  4393.         add     eax, [edx+4]
  4394.         pop     edx
  4395.         ret     4
  4396. ;-----------------------------------------------------------------------------
  4397. align 4
  4398. putimage_init2bpp:
  4399.         add     eax, ecx
  4400.         push    ecx
  4401.         add     ecx, 3
  4402.         add     eax, 3
  4403.         shr     ecx, 2
  4404.         shr     eax, 2
  4405.         sub     eax, ecx
  4406.         pop     ecx
  4407.         ret
  4408. ;-----------------------------------------------------------------------------
  4409. align 16
  4410. putimage_get2bpp:
  4411.         push    edx
  4412.         mov     edx, [esp+8]
  4413.         mov     al, [edx]
  4414.         mov     ah, al
  4415.         shr     al, 6
  4416.         shl     ah, 2
  4417.         jnz     .nonewbyte
  4418.         lodsb
  4419.         mov     ah, al
  4420.         shr     al, 6
  4421.         shl     ah, 2
  4422.         add     ah, 1
  4423. .nonewbyte:
  4424.         mov     [edx], ah
  4425.         mov     edx, [edx+4]
  4426.         movzx   eax, al
  4427.         mov     eax, [edx+eax*4]
  4428.         pop     edx
  4429.         ret     4
  4430. ;-----------------------------------------------------------------------------
  4431. align 4
  4432. putimage_init4bpp:
  4433.         add     eax, ecx
  4434.         push    ecx
  4435.         add     ecx, 1
  4436.         add     eax, 1
  4437.         shr     ecx, 1
  4438.         shr     eax, 1
  4439.         sub     eax, ecx
  4440.         pop     ecx
  4441.         ret
  4442. ;-----------------------------------------------------------------------------
  4443. align 16
  4444. putimage_get4bpp:
  4445.         push    edx
  4446.         mov     edx, [esp+8]
  4447.         add     byte [edx], 80h
  4448.         jc      @f
  4449.         movzx   eax, byte [edx+1]
  4450.         mov     edx, [edx+4]
  4451.         and     eax, 0x0F
  4452.         mov     eax, [edx+eax*4]
  4453.         pop     edx
  4454.         ret     4
  4455. @@:
  4456.         movzx   eax, byte [esi]
  4457.         add     esi, 1
  4458.         mov     [edx+1], al
  4459.         shr     eax, 4
  4460.         mov     edx, [edx+4]
  4461.         mov     eax, [edx+eax*4]
  4462.         pop     edx
  4463.         ret     4
  4464. ;-----------------------------------------------------------------------------
  4465. align 4
  4466. putimage_init32bpp:
  4467.         shl     eax, 2
  4468.         ret
  4469. ;-----------------------------------------------------------------------------
  4470. align 16
  4471. putimage_get32bpp:
  4472.         lodsd
  4473.         ret     4
  4474. ;-----------------------------------------------------------------------------
  4475. align 4
  4476. putimage_init15bpp:
  4477. putimage_init16bpp:
  4478.         add     eax, eax
  4479.         ret
  4480. ;-----------------------------------------------------------------------------
  4481. align 16
  4482. putimage_get15bpp:
  4483. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  4484.         push    ecx edx
  4485.         movzx   eax, word [esi]
  4486.         add     esi, 2
  4487.         mov     ecx, eax
  4488.         mov     edx, eax
  4489.         and     eax, 0x1F
  4490.         and     ecx, 0x1F shl 5
  4491.         and     edx, 0x1F shl 10
  4492.         shl     eax, 3
  4493.         shl     ecx, 6
  4494.         shl     edx, 9
  4495.         or      eax, ecx
  4496.         or      eax, edx
  4497.         pop     edx ecx
  4498.         ret     4
  4499. ;-----------------------------------------------------------------------------
  4500. align 16
  4501. putimage_get16bpp:
  4502. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  4503.         push    ecx edx
  4504.         movzx   eax, word [esi]
  4505.         add     esi, 2
  4506.         mov     ecx, eax
  4507.         mov     edx, eax
  4508.         and     eax, 0x1F
  4509.         and     ecx, 0x3F shl 5
  4510.         and     edx, 0x1F shl 11
  4511.         shl     eax, 3
  4512.         shl     ecx, 5
  4513.         shl     edx, 8
  4514.         or      eax, ecx
  4515.         or      eax, edx
  4516.         pop     edx ecx
  4517.         ret     4
  4518. ;-----------------------------------------------------------------------------
  4519. ;align 4
  4520. ; eax x beginning
  4521. ; ebx y beginning
  4522. ; ecx x end
  4523.         ; edx y end
  4524. ; edi color
  4525. ;__sys_drawbar:
  4526. ;        mov     esi, [current_slot]
  4527. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  4528. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  4529. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  4530. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  4531. ;--------------------------------------
  4532. ;align 4
  4533. ;.forced:
  4534. ;        call    vesa20_drawbar
  4535. ;        call    [draw_pointer]
  4536. ;        ret
  4537. ;-----------------------------------------------------------------------------
  4538. align 4
  4539. kb_read:
  4540.  
  4541.         push    ecx edx
  4542.  
  4543.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4544.       kr_loop:
  4545.         in      al, 0x64
  4546.         test    al, 1
  4547.         jnz     kr_ready
  4548.         loop    kr_loop
  4549.         mov     ah, 1
  4550.         jmp     kr_exit
  4551.       kr_ready:
  4552.         push    ecx
  4553.         mov     ecx, 32
  4554.       kr_delay:
  4555.         loop    kr_delay
  4556.         pop     ecx
  4557.         in      al, 0x60
  4558.         xor     ah, ah
  4559.       kr_exit:
  4560.  
  4561.         pop     edx ecx
  4562.  
  4563.         ret
  4564. ;-----------------------------------------------------------------------------
  4565. align 4
  4566. kb_write:
  4567.  
  4568.         push    ecx edx
  4569.  
  4570.         mov     dl, al
  4571. ;        mov     ecx,0x1ffff ; last 0xffff, new value in view of fast CPU's
  4572. ;      kw_loop1:
  4573. ;        in      al,0x64
  4574. ;        test    al,0x20
  4575. ;        jz      kw_ok1
  4576. ;        loop    kw_loop1
  4577. ;        mov     ah,1
  4578. ;        jmp     kw_exit
  4579. ;      kw_ok1:
  4580.         in      al, 0x60
  4581.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4582.       kw_loop:
  4583.         in      al, 0x64
  4584.         test    al, 2
  4585.         jz      kw_ok
  4586.         loop    kw_loop
  4587.         mov     ah, 1
  4588.         jmp     kw_exit
  4589.       kw_ok:
  4590.         mov     al, dl
  4591.         out     0x60, al
  4592.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4593.       kw_loop3:
  4594.         in      al, 0x64
  4595.         test    al, 2
  4596.         jz      kw_ok3
  4597.         loop    kw_loop3
  4598.         mov     ah, 1
  4599.         jmp     kw_exit
  4600.       kw_ok3:
  4601.         mov     ah, 8
  4602.       kw_loop4:
  4603.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4604.       kw_loop5:
  4605.         in      al, 0x64
  4606.         test    al, 1
  4607.         jnz     kw_ok4
  4608.         loop    kw_loop5
  4609.         dec     ah
  4610.         jnz     kw_loop4
  4611.       kw_ok4:
  4612.         xor     ah, ah
  4613.       kw_exit:
  4614.  
  4615.         pop     edx ecx
  4616.  
  4617.         ret
  4618. ;-----------------------------------------------------------------------------
  4619. align 4
  4620. kb_cmd:
  4621.  
  4622.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4623.       c_wait:
  4624.         in      al, 0x64
  4625.         test    al, 2
  4626.         jz      c_send
  4627.         loop    c_wait
  4628.         jmp     c_error
  4629.       c_send:
  4630.         mov     al, bl
  4631.         out     0x64, al
  4632.         mov     ecx, 0x1ffff; last 0xffff, new value in view of fast CPU's
  4633.       c_accept:
  4634.         in      al, 0x64
  4635.         test    al, 2
  4636.         jz      c_ok
  4637.         loop    c_accept
  4638.       c_error:
  4639.         mov     ah, 1
  4640.         jmp     c_exit
  4641.       c_ok:
  4642.         xor     ah, ah
  4643.       c_exit:
  4644.         ret
  4645.  
  4646.  
  4647. setmouse:  ; set mousepicture -pointer
  4648.            ; ps2 mouse enable
  4649.  
  4650. ;        mov     [MOUSE_PICTURE], dword mousepointer
  4651.  
  4652.         cli
  4653.  
  4654.         ret
  4655.  
  4656. if used _rdtsc
  4657. _rdtsc:
  4658.         bt      [cpu_caps], CAPS_TSC
  4659.         jnc     ret_rdtsc
  4660.         rdtsc
  4661.         ret
  4662.    ret_rdtsc:
  4663.         mov     edx, 0xffffffff
  4664.         mov     eax, 0xffffffff
  4665.         ret
  4666. end if
  4667.  
  4668. sys_msg_board_str:
  4669.  
  4670.         pushad
  4671.    @@:
  4672.         cmp     [esi], byte 0
  4673.         je      @f
  4674.         mov     ebx, 1
  4675.         movzx   ecx, byte [esi]
  4676.         call    sys_msg_board
  4677.         inc     esi
  4678.         jmp     @b
  4679.    @@:
  4680.         popad
  4681.         ret
  4682.  
  4683. sys_msg_board_byte:
  4684. ; in: al = byte to display
  4685. ; out: nothing
  4686. ; destroys: nothing
  4687.         pushad
  4688.         mov     ecx, 2
  4689.         shl     eax, 24
  4690.         jmp     @f
  4691.  
  4692. sys_msg_board_word:
  4693. ; in: ax = word to display
  4694. ; out: nothing
  4695. ; destroys: nothing
  4696.         pushad
  4697.         mov     ecx, 4
  4698.         shl     eax, 16
  4699.         jmp     @f
  4700.  
  4701. sys_msg_board_dword:
  4702. ; in: eax = dword to display
  4703. ; out: nothing
  4704. ; destroys: nothing
  4705.         pushad
  4706.         mov     ecx, 8
  4707. @@:
  4708.         push    ecx
  4709.         rol     eax, 4
  4710.         push    eax
  4711.         and     al, 0xF
  4712.         cmp     al, 10
  4713.         sbb     al, 69h
  4714.         das
  4715.         mov     cl, al
  4716.         xor     ebx, ebx
  4717.         inc     ebx
  4718.         call    sys_msg_board
  4719.         pop     eax
  4720.         pop     ecx
  4721.         loop    @b
  4722.         popad
  4723.         ret
  4724.  
  4725. msg_board_data_size = 65536 ; Must be power of two
  4726.  
  4727. uglobal
  4728.   msg_board_data  rb msg_board_data_size
  4729.   msg_board_count dd 0x0
  4730. endg
  4731.  
  4732. sys_msg_board:
  4733.  
  4734. ; ebx=1 : write :  bl byte to write
  4735. ; ebx=2 :  read :  ebx=0 -> no data, ebx=1 -> data in al
  4736.  
  4737.         push    eax ebx                 ; Save eax and ebx, since we're restoring their order required.
  4738.         mov     eax, ebx
  4739.         mov     ebx, ecx
  4740.  
  4741.         mov     ecx, [msg_board_count]
  4742.         cmp     eax, 1
  4743.         jne     .smbl1
  4744.  
  4745. if defined debug_com_base
  4746.  
  4747.         push    dx ax
  4748.  
  4749.        @@:                              ; Wait for empty transmit register  (yes, this slows down system..)
  4750.         mov     dx, debug_com_base+5
  4751.         in      al, dx
  4752.         test    al, 1 shl 5
  4753.         jz      @r
  4754.  
  4755.         mov     dx, debug_com_base      ; Output the byte
  4756.         mov     al, bl
  4757.         out     dx, al
  4758.  
  4759.         pop     ax dx
  4760.  
  4761. end if
  4762.  
  4763.         mov     [msg_board_data+ecx], bl
  4764. ; // if debug_direct_print == 1
  4765.         cmp     byte [debug_direct_print], 1
  4766.         jnz     @f
  4767.         pusha
  4768. iglobal
  4769. msg_board_pos   dd      (42*6)*65536+10 ; for printing debug output on the screen
  4770. endg
  4771.         lea     edx, [msg_board_data+ecx]
  4772.         mov     ecx, 0x40FFFFFF
  4773.         mov     ebx, [msg_board_pos]
  4774.         mov     edi, 1
  4775.         mov     esi, 1
  4776.         call    dtext
  4777.         popa
  4778.         add     word [msg_board_pos+2], 6
  4779.         cmp     bl, 10
  4780.         jnz     @f
  4781.         mov     word [msg_board_pos+2], (42*6)
  4782.         add     word [msg_board_pos], 10
  4783.         mov     ax, word [Screen_Max_Y]
  4784.         cmp     word [msg_board_pos], ax
  4785.         jbe     @f
  4786.         mov     word [msg_board_pos], 10
  4787. @@:
  4788. ; // end if
  4789.  
  4790. if 0
  4791.         pusha
  4792.         mov     al, bl
  4793.         mov     edx, 402h
  4794.         out     dx, al
  4795.         popa
  4796. end if
  4797.         inc     ecx
  4798.         and     ecx, msg_board_data_size - 1
  4799.         mov     [msg_board_count], ecx
  4800.  
  4801.         pop     ebx eax
  4802.         ret
  4803. .smbl1:
  4804.         cmp     eax, 2
  4805.         jne     .smbl2
  4806.         test    ecx, ecx
  4807.         jz      .smbl21
  4808.  
  4809.         add     esp, 8                  ; Returning data in ebx and eax, so no need to restore them.
  4810.         mov     eax, msg_board_data+1
  4811.         mov     ebx, msg_board_data
  4812.         movzx   edx, byte [ebx]
  4813.         call    memmove
  4814.         dec     [msg_board_count]
  4815.         mov     [esp + 32], edx ;eax
  4816.         mov     [esp + 20], dword 1
  4817.         ret
  4818. .smbl21:
  4819.         mov     [esp+32], ecx
  4820.         mov     [esp+20], ecx
  4821. .smbl2:
  4822.         pop     ebx eax
  4823.         ret
  4824.  
  4825. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4826. ;; 66 sys function.                                                ;;
  4827. ;; in eax=66,ebx in [0..5],ecx,edx                                 ;;
  4828. ;; out eax                                                         ;;
  4829. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4830. iglobal
  4831. align 4
  4832. f66call:
  4833.            dd sys_process_def.1   ; 1 = set keyboard mode
  4834.            dd sys_process_def.2   ; 2 = get keyboard mode
  4835.            dd sys_process_def.3   ; 3 = get keyboard ctrl, alt, shift
  4836.            dd sys_process_def.4   ; 4 = set system-wide hotkey
  4837.            dd sys_process_def.5   ; 5 = delete installed hotkey
  4838.            dd sys_process_def.6   ; 6 = disable input, work only hotkeys
  4839.            dd sys_process_def.7   ; 7 = enable input, opposition to f.66.6
  4840. endg
  4841. ;-----------------------------------------------------------------------------
  4842. align 4
  4843. sys_process_def:
  4844.         dec     ebx
  4845.         cmp     ebx, 7
  4846.         jae     .not_support    ;if >=8 then or eax,-1
  4847.  
  4848.         mov     edi, [CURRENT_TASK]
  4849.         jmp     dword [f66call+ebx*4]
  4850.  
  4851. .not_support:
  4852.         or      eax, -1
  4853.         ret
  4854. ;-----------------------------------------------------------------------------
  4855. align 4
  4856. .1:
  4857.         shl     edi, 8
  4858.         mov     [edi+SLOT_BASE + APPDATA.keyboard_mode], cl
  4859.  
  4860.         ret
  4861. ;-----------------------------------------------------------------------------
  4862. align 4
  4863. .2:                             ; 2 = get keyboard mode
  4864.         shl     edi, 8
  4865.         movzx   eax, byte [SLOT_BASE+edi + APPDATA.keyboard_mode]
  4866.         mov     [esp+32], eax
  4867.         ret
  4868. ;-----------------------------------------------------------------------------
  4869. align 4
  4870. .3:                             ;3 = get keyboard ctrl, alt, shift
  4871.         mov     eax, [kb_state]
  4872.         mov     [esp+32], eax
  4873.         ret
  4874. ;-----------------------------------------------------------------------------
  4875. align 4
  4876. .4:
  4877.         mov     eax, hotkey_list
  4878. @@:
  4879.         cmp     dword [eax+8], 0
  4880.         jz      .found_free
  4881.         add     eax, 16
  4882.         cmp     eax, hotkey_list+16*256
  4883.         jb      @b
  4884.         mov     dword [esp+32], 1
  4885.         ret
  4886. .found_free:
  4887.         mov     [eax+8], edi
  4888.         mov     [eax+4], edx
  4889.         movzx   ecx, cl
  4890.         lea     ecx, [hotkey_scancodes+ecx*4]
  4891.         mov     edx, [ecx]
  4892.         mov     [eax], edx
  4893.         mov     [ecx], eax
  4894.         mov     [eax+12], ecx
  4895.         test    edx, edx
  4896.         jz      @f
  4897.         mov     [edx+12], eax
  4898. @@:
  4899.         and     dword [esp+32], 0
  4900.         ret
  4901. ;-----------------------------------------------------------------------------
  4902. align 4
  4903. .5:
  4904.         movzx   ebx, cl
  4905.         lea     ebx, [hotkey_scancodes+ebx*4]
  4906.         mov     eax, [ebx]
  4907. .scan:
  4908.         test    eax, eax
  4909.         jz      .notfound
  4910.         cmp     [eax+8], edi
  4911.         jnz     .next
  4912.         cmp     [eax+4], edx
  4913.         jz      .found
  4914. .next:
  4915.         mov     eax, [eax]
  4916.         jmp     .scan
  4917. .notfound:
  4918.         mov     dword [esp+32], 1
  4919.         ret
  4920. .found:
  4921.         mov     ecx, [eax]
  4922.         jecxz   @f
  4923.         mov     edx, [eax+12]
  4924.         mov     [ecx+12], edx
  4925. @@:
  4926.         mov     ecx, [eax+12]
  4927.         mov     edx, [eax]
  4928.         mov     [ecx], edx
  4929.         xor     edx, edx
  4930.         mov     [eax+4], edx
  4931.         mov     [eax+8], edx
  4932.         mov     [eax+12], edx
  4933.         mov     [eax], edx
  4934.         mov     [esp+32], edx
  4935.         ret
  4936. ;-----------------------------------------------------------------------------
  4937. align 4
  4938. .6:
  4939.         pushfd
  4940.         cli
  4941.         mov     eax, [PID_lock_input]
  4942.         test    eax, eax
  4943.         jnz     @f
  4944. ; get current PID
  4945.         mov     eax, [CURRENT_TASK]
  4946.         shl     eax, 5
  4947.         mov     eax, [eax+CURRENT_TASK+TASKDATA.pid]
  4948. ; set current PID for lock input
  4949.         mov     [PID_lock_input], eax
  4950. @@:
  4951.         popfd
  4952.         ret
  4953. ;-----------------------------------------------------------------------------
  4954. align 4
  4955. .7:
  4956.         mov     eax, [PID_lock_input]
  4957.         test    eax, eax
  4958.         jz      @f
  4959. ; get current PID
  4960.         mov     ebx, [CURRENT_TASK]
  4961.         shl     ebx, 5
  4962.         mov     ebx, [ebx+CURRENT_TASK+TASKDATA.pid]
  4963. ; compare current lock input with current PID
  4964.         cmp     ebx, eax
  4965.         jne     @f
  4966.  
  4967.         xor     eax, eax
  4968.         mov     [PID_lock_input], eax
  4969. @@:
  4970.         ret
  4971. ;-----------------------------------------------------------------------------
  4972. uglobal
  4973.   PID_lock_input dd 0x0
  4974. endg
  4975. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4976. ;; 61 sys function.                                                ;;
  4977. ;; in eax=61,ebx in [1..3]                                         ;;
  4978. ;; out eax                                                         ;;
  4979. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4980. iglobal
  4981. align 4
  4982. f61call:
  4983.            dd sys_gs.1   ; resolution
  4984.            dd sys_gs.2   ; bits per pixel
  4985.            dd sys_gs.3   ; bytes per scanline
  4986. endg
  4987.  
  4988.  
  4989. align 4
  4990.  
  4991. sys_gs:                         ; direct screen access
  4992.         dec     ebx
  4993.         cmp     ebx, 2
  4994.         ja      .not_support
  4995.         jmp     dword [f61call+ebx*4]
  4996. .not_support:
  4997.         or      [esp+32], dword -1
  4998.         ret
  4999.  
  5000.  
  5001. .1:                             ; resolution
  5002.         mov     eax, [Screen_Max_X]
  5003.         shl     eax, 16
  5004.         mov     ax, word [Screen_Max_Y]
  5005.         add     eax, 0x00010001
  5006.         mov     [esp+32], eax
  5007.         ret
  5008. .2:                             ; bits per pixel
  5009.         mov     eax, [_display.bpp]
  5010.         mov     [esp+32], eax
  5011.         ret
  5012. .3:                             ; bytes per scanline
  5013.         mov     eax, [_display.pitch]
  5014.         mov     [esp+32], eax
  5015.         ret
  5016.  
  5017. align 4  ;  system functions
  5018.  
  5019. syscall_setpixel:                       ; SetPixel
  5020.  
  5021.         mov     eax, ebx
  5022.         mov     ebx, ecx
  5023.         mov     ecx, edx
  5024.         mov     edx, [TASK_BASE]
  5025.         add     eax, [edx-twdw+WDATA.box.left]
  5026.         add     ebx, [edx-twdw+WDATA.box.top]
  5027.         mov     edi, [current_slot]
  5028.         add     eax, [edi+APPDATA.wnd_clientbox.left]
  5029.         add     ebx, [edi+APPDATA.wnd_clientbox.top]
  5030.         xor     edi, edi ; no force
  5031.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 save to mouseunder area
  5032. ;        jmp     [putpixel]
  5033.         jmp     __sys_putpixel
  5034.  
  5035. align 4
  5036.  
  5037. syscall_writetext:                      ; WriteText
  5038.  
  5039.         mov     eax, [TASK_BASE]
  5040.         mov     ebp, [eax-twdw+WDATA.box.left]
  5041.         push    esi
  5042.         mov     esi, [current_slot]
  5043.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5044.         shl     ebp, 16
  5045.         add     ebp, [eax-twdw+WDATA.box.top]
  5046.         add     bp, word[esi+APPDATA.wnd_clientbox.top]
  5047.         pop     esi
  5048.         test    ecx, 0x08000000  ; redirect the output to the user area
  5049.         jnz     @f
  5050.         add     ebx, ebp
  5051. align 4
  5052. @@:
  5053.         mov     eax, edi
  5054.         test    ecx, 0x08000000  ; redirect the output to the user area
  5055.         jnz     dtext
  5056.         xor     edi, edi
  5057.         jmp     dtext
  5058.  
  5059. align 4
  5060.  
  5061. syscall_drawrect:                       ; DrawRect
  5062.  
  5063.         mov     edi, edx ; color + gradient
  5064.         and     edi, 0x80FFFFFF
  5065.         test    bx, bx  ; x.size
  5066.         je      .drectr
  5067.         test    cx, cx ; y.size
  5068.         je      .drectr
  5069.  
  5070.         mov     eax, ebx ; bad idea
  5071.         mov     ebx, ecx
  5072.  
  5073.         movzx   ecx, ax ; ecx - x.size
  5074.         shr     eax, 16 ; eax - x.coord
  5075.         movzx   edx, bx ; edx - y.size
  5076.         shr     ebx, 16 ; ebx - y.coord
  5077.         mov     esi, [current_slot]
  5078.  
  5079.         add     eax, [esi + APPDATA.wnd_clientbox.left]
  5080.         add     ebx, [esi + APPDATA.wnd_clientbox.top]
  5081.         add     ecx, eax
  5082.         add     edx, ebx
  5083. ;        jmp     [drawbar]
  5084.         jmp     vesa20_drawbar
  5085. .drectr:
  5086.         ret
  5087.  
  5088. align 4
  5089. syscall_getscreensize:                  ; GetScreenSize
  5090.         mov     ax, word [Screen_Max_X]
  5091.         shl     eax, 16
  5092.         mov     ax, word [Screen_Max_Y]
  5093.         mov     [esp + 32], eax
  5094.         ret
  5095.  
  5096. align 4
  5097.  
  5098. syscall_cdaudio:                        ; CD
  5099.  
  5100.         cmp     ebx, 4
  5101.         jb      .audio
  5102.         jz      .eject
  5103.         cmp     ebx, 5
  5104.         jnz     .ret
  5105. .load:
  5106.         call    .reserve
  5107.         call    LoadMedium
  5108.         ;call    .free
  5109.         jmp     .free
  5110. ;        ret
  5111. .eject:
  5112.         call    .reserve
  5113.         call    clear_CD_cache
  5114.         call    allow_medium_removal
  5115.         call    EjectMedium
  5116. ;        call    .free
  5117.         jmp     .free
  5118. ;        ret
  5119. .audio:
  5120.         call    sys_cd_audio
  5121.         mov     [esp+36-4], eax
  5122. .ret:
  5123.         ret
  5124.  
  5125. .reserve:
  5126.         call    reserve_cd
  5127.         mov     eax, ecx
  5128.         shr     eax, 1
  5129.         and     eax, 1
  5130.         inc     eax
  5131.         mov     [ChannelNumber], ax
  5132.         mov     eax, ecx
  5133.         and     eax, 1
  5134.         mov     [DiskNumber], al
  5135.         call    reserve_cd_channel
  5136.         and     ebx, 3
  5137.         inc     ebx
  5138.         mov     [cdpos], ebx
  5139.         add     ebx, ebx
  5140.         mov     cl, 8
  5141.         sub     cl, bl
  5142.         mov     al, [DRIVE_DATA+1]
  5143.         shr     al, cl
  5144.         test    al, 2
  5145.         jz      .free;.err
  5146.         ret
  5147. .free:
  5148.         call    free_cd_channel
  5149.         and     [cd_status], 0
  5150.         ret
  5151. .err:
  5152.         call    .free
  5153. ;        pop     eax
  5154.         ret
  5155. ;-----------------------------------------------------------------------------
  5156. align 4
  5157. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  5158.         cmp     ebx, [Screen_Max_X]
  5159.         jbe     @f
  5160.         cmp     ecx, [Screen_Max_Y]
  5161.         jbe     @f
  5162.         xor     eax, eax
  5163.         jmp     .store
  5164. ;--------------------------------------
  5165. align 4
  5166. @@:
  5167.         mov     eax, [d_width_calc_area + ecx*4]
  5168.         add     eax, [_WinMapAddress]
  5169.         movzx   eax, byte[eax+ebx]        ; get value for current point
  5170. ;--------------------------------------
  5171. align 4
  5172. .store:
  5173.         mov     [esp + 32], eax
  5174.         ret
  5175. ;-----------------------------------------------------------------------------
  5176. align 4
  5177. syscall_getpixel:                       ; GetPixel
  5178.         mov     ecx, [Screen_Max_X]
  5179.         inc     ecx
  5180.         xor     edx, edx
  5181.         mov     eax, ebx
  5182.         div     ecx
  5183.         mov     ebx, edx
  5184.         xchg    eax, ebx
  5185.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5186.         call    dword [GETPIXEL]; eax - x, ebx - y
  5187.         mov     [esp + 32], ecx
  5188.         ret
  5189. ;-----------------------------------------------------------------------------
  5190. align 4
  5191. syscall_getarea:
  5192. ;eax = 36
  5193. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5194. ;ecx = [size x]*65536 + [size y]
  5195. ;edx = [start x]*65536 + [start y]
  5196.         pushad
  5197.         mov     edi, ebx
  5198.         mov     eax, edx
  5199.         shr     eax, 16
  5200.         mov     ebx, edx
  5201.         and     ebx, 0xffff
  5202.         dec     eax
  5203.         dec     ebx
  5204.      ; eax - x, ebx - y
  5205.         mov     edx, ecx
  5206.  
  5207.         shr     ecx, 16
  5208.         and     edx, 0xffff
  5209.         mov     esi, ecx
  5210.      ; ecx - size x, edx - size y
  5211.  
  5212.         mov     ebp, edx
  5213.         dec     ebp
  5214.         lea     ebp, [ebp*3]
  5215.  
  5216.         imul    ebp, esi
  5217.  
  5218.         mov     esi, ecx
  5219.         dec     esi
  5220.         lea     esi, [esi*3]
  5221.  
  5222.         add     ebp, esi
  5223.         add     ebp, edi
  5224.  
  5225.         add     ebx, edx
  5226. ;--------------------------------------
  5227. align 4
  5228. .start_y:
  5229.         push    ecx edx
  5230. ;--------------------------------------
  5231. align 4
  5232. .start_x:
  5233.         push    eax ebx ecx
  5234.         add     eax, ecx
  5235.  
  5236.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  5237.         call    dword [GETPIXEL]; eax - x, ebx - y
  5238.  
  5239.         mov     [ebp], cx
  5240.         shr     ecx, 16
  5241.         mov     [ebp+2], cl
  5242.  
  5243.         pop     ecx ebx eax
  5244.         sub     ebp, 3
  5245.         dec     ecx
  5246.         jnz     .start_x
  5247.         pop     edx ecx
  5248.         dec     ebx
  5249.         dec     edx
  5250.         jnz     .start_y
  5251.         popad
  5252.         ret
  5253. ;-----------------------------------------------------------------------------
  5254. align 4
  5255. syscall_putarea_backgr:
  5256. ;eax = 25
  5257. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  5258. ;ecx = [size x]*65536 + [size y]
  5259. ;edx = [start x]*65536 + [start y]
  5260.         pushad
  5261.         mov     edi, ebx
  5262.         mov     eax, edx
  5263.         shr     eax, 16
  5264.         mov     ebx, edx
  5265.         and     ebx, 0xffff
  5266.         dec     eax
  5267.         dec     ebx
  5268. ; eax - x, ebx - y
  5269.         mov     edx, ecx
  5270.         shr     ecx, 16
  5271.         and     edx, 0xffff
  5272.         mov     esi, ecx
  5273. ; ecx - size x, edx - size y
  5274.         mov     ebp, edx
  5275.         dec     ebp
  5276.         shl     ebp, 2
  5277.  
  5278.         imul    ebp, esi
  5279.  
  5280.         mov     esi, ecx
  5281.         dec     esi
  5282.         shl     esi, 2
  5283.  
  5284.         add     ebp, esi
  5285.         add     ebp, edi
  5286.  
  5287.         add     ebx, edx
  5288. ;--------------------------------------
  5289. align 4
  5290. .start_y:
  5291.         push    ecx edx
  5292. ;--------------------------------------
  5293. align 4
  5294. .start_x:
  5295.         push    eax ecx
  5296.         add     eax, ecx
  5297.  
  5298.         mov     ecx, [ebp]
  5299.         rol     ecx, 8
  5300.         test    cl, cl        ; transparensy = 0
  5301.         jz      .no_put
  5302.  
  5303.         xor     cl, cl
  5304.         ror     ecx, 8
  5305.  
  5306.         pushad
  5307.         mov     edx, [d_width_calc_area + ebx*4]
  5308.         add     edx, [_WinMapAddress]
  5309.         movzx   edx, byte [eax+edx]
  5310.         cmp     dl, byte 1
  5311.         jne     @f
  5312.  
  5313.         call    dword [PUTPIXEL]; eax - x, ebx - y
  5314. ;--------------------------------------
  5315. align 4
  5316. @@:
  5317.         popad
  5318. ;--------------------------------------
  5319. align 4
  5320. .no_put:
  5321.         pop     ecx eax
  5322.  
  5323.         sub     ebp, 4
  5324.         dec     ecx
  5325.         jnz     .start_x
  5326.  
  5327.         pop     edx ecx
  5328.         dec     ebx
  5329.         dec     edx
  5330.         jnz     .start_y
  5331.  
  5332.         popad
  5333.         ret
  5334. ;-----------------------------------------------------------------------------
  5335. align 4
  5336. syscall_drawline:                       ; DrawLine
  5337.  
  5338.         mov     edi, [TASK_BASE]
  5339.         movzx   eax, word[edi-twdw+WDATA.box.left]
  5340.         mov     ebp, eax
  5341.         mov     esi, [current_slot]
  5342.         add     ebp, [esi+APPDATA.wnd_clientbox.left]
  5343.         add     ax, word[esi+APPDATA.wnd_clientbox.left]
  5344.         add     ebp, ebx
  5345.         shl     eax, 16
  5346.         movzx   ebx, word[edi-twdw+WDATA.box.top]
  5347.         add     eax, ebp
  5348.         mov     ebp, ebx
  5349.         add     ebp, [esi+APPDATA.wnd_clientbox.top]
  5350.         add     bx, word[esi+APPDATA.wnd_clientbox.top]
  5351.         add     ebp, ecx
  5352.         shl     ebx, 16
  5353.         xor     edi, edi
  5354.         add     ebx, ebp
  5355.         mov     ecx, edx
  5356. ;        jmp     [draw_line]
  5357.         jmp     __sys_draw_line
  5358.  
  5359.  
  5360. align 4
  5361. syscall_reserveportarea:                ; ReservePortArea and FreePortArea
  5362.  
  5363.         call    r_f_port_area
  5364.         mov     [esp+32], eax
  5365.         ret
  5366.  
  5367. align 4
  5368. syscall_threads:                        ; CreateThreads
  5369. ;
  5370. ;   ecx=thread entry point
  5371. ;   edx=thread stack pointer
  5372. ;
  5373. ; on return : eax = pid
  5374.  
  5375.         xor     ebx, ebx
  5376.         call    new_sys_threads
  5377.  
  5378.         mov     [esp+32], eax
  5379.         ret
  5380.  
  5381. align 4
  5382.  
  5383. paleholder:
  5384.         ret
  5385. ;------------------------------------------------------------------------------
  5386. align 4
  5387. calculate_fast_getting_offset_for_WinMapAddress:
  5388. ; calculate data area for fast getting offset to _WinMapAddress
  5389.         xor     eax, eax
  5390.         mov     ecx, [_display.height]
  5391.         mov     edi, d_width_calc_area
  5392.         cld
  5393. @@:
  5394.         stosd
  5395.         add     eax, [_display.width]
  5396.         dec     ecx
  5397.         jnz     @r
  5398.         ret
  5399. ;------------------------------------------------------------------------------
  5400. align 4
  5401. calculate_fast_getting_offset_for_LFB:
  5402. ; calculate data area for fast getting offset to LFB
  5403.         xor     eax, eax
  5404.         mov     ecx, [_display.height]
  5405.         mov     edi, BPSLine_calc_area
  5406.         cld
  5407. @@:
  5408.         stosd
  5409.         add     eax, [_display.pitch]
  5410.         dec     ecx
  5411.         jnz     @r
  5412.         ret
  5413. ;------------------------------------------------------------------------------
  5414. align 4
  5415. set_screen:
  5416. ; in:
  5417. ; eax - new Screen_Max_X
  5418. ; ecx - new BytesPerScanLine
  5419. ; edx - new Screen_Max_Y
  5420.  
  5421.         pushfd
  5422.         cli
  5423.  
  5424.         mov     [Screen_Max_X], eax
  5425.         mov     [Screen_Max_Y], edx
  5426.         mov     [_display.pitch], ecx
  5427.  
  5428.         mov     [screen_workarea.right], eax
  5429.         mov     [screen_workarea.bottom], edx
  5430.  
  5431.         push    ebx
  5432.         push    esi
  5433.         push    edi
  5434.  
  5435.         pushad
  5436.  
  5437.         cmp     [do_not_touch_winmap], 1
  5438.         je      @f
  5439.  
  5440.         stdcall kernel_free, [_WinMapAddress]
  5441.  
  5442.         mov     eax, [_display.width]
  5443.         mul     [_display.height]
  5444.         mov     [_WinMapSize], eax
  5445.  
  5446.         stdcall kernel_alloc, eax
  5447.         mov     [_WinMapAddress], eax
  5448.         test    eax, eax
  5449.         jz      .epic_fail
  5450. ; store for f.18.24
  5451.         mov     eax, [_display.width]
  5452.         mov     [display_width_standard], eax
  5453.  
  5454.         mov     eax, [_display.height]
  5455.         mov     [display_height_standard], eax
  5456. @@:
  5457.         call    calculate_fast_getting_offset_for_WinMapAddress
  5458. ; for Qemu or non standart video cards
  5459. ; Unfortunately [BytesPerScanLine] does not always
  5460. ;                             equal to [_display.width] * [ScreenBPP] / 8
  5461.         call    calculate_fast_getting_offset_for_LFB
  5462.         popad
  5463.  
  5464.         call    repos_windows
  5465.         xor     eax, eax
  5466.         xor     ebx, ebx
  5467.         mov     ecx, [Screen_Max_X]
  5468.         mov     edx, [Screen_Max_Y]
  5469.         call    calculatescreen
  5470.         pop     edi
  5471.         pop     esi
  5472.         pop     ebx
  5473.  
  5474.         popfd
  5475.         ret
  5476.  
  5477. .epic_fail:
  5478.         hlt                     ; Houston, we've had a problem
  5479.  
  5480. ; --------------- APM ---------------------
  5481. uglobal
  5482. apm_entry       dp      0
  5483. apm_vf          dd      0
  5484. endg
  5485.  
  5486. align 4
  5487. sys_apm:
  5488.         xor     eax, eax
  5489.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  5490.         jne     @f
  5491.         inc     eax
  5492.         or      dword [esp + 44], eax   ; error
  5493.         add     eax, 7
  5494.         mov     dword [esp + 32], eax   ; 32-bit protected-mode interface not supported
  5495.         ret
  5496.  
  5497. @@:
  5498. ;       xchg    eax, ecx
  5499. ;       xchg    ebx, ecx
  5500.  
  5501.         cmp     dx, 3
  5502.         ja      @f
  5503.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  5504.         mov     eax, [apm_vf]
  5505.         mov     [esp + 32], eax
  5506.         shr     eax, 16
  5507.         mov     [esp + 28], eax
  5508.         ret
  5509.  
  5510. @@:
  5511.  
  5512.         mov     esi, [master_tab+(OS_BASE shr 20)]
  5513.         xchg    [master_tab], esi
  5514.         push    esi
  5515.         mov     edi, cr3
  5516.         mov     cr3, edi                ;flush TLB
  5517.  
  5518.         call    pword [apm_entry]       ;call APM BIOS
  5519.  
  5520.         xchg    eax, [esp]
  5521.         mov     [master_tab], eax
  5522.         mov     eax, cr3
  5523.         mov     cr3, eax
  5524.         pop     eax
  5525.  
  5526.         mov     [esp + 4 ], edi
  5527.         mov     [esp + 8], esi
  5528.         mov     [esp + 20], ebx
  5529.         mov     [esp + 24], edx
  5530.         mov     [esp + 28], ecx
  5531.         mov     [esp + 32], eax
  5532.         setc    al
  5533.         and     [esp + 44], byte 0xfe
  5534.         or      [esp + 44], al
  5535.         ret
  5536. ; -----------------------------------------
  5537.  
  5538. align 4
  5539.  
  5540. undefined_syscall:                      ; Undefined system call
  5541.         mov     [esp + 32], dword -1
  5542.         ret
  5543.  
  5544. align 4
  5545. system_shutdown:          ; shut down the system
  5546.  
  5547.         cmp     byte [BOOT_VARS+0x9030], 1
  5548.         jne     @F
  5549.         ret
  5550. @@:
  5551.         call    stop_all_services
  5552.         movi    eax, 3
  5553.         call    sys_cd_audio
  5554.  
  5555. yes_shutdown_param:
  5556.         cli
  5557.  
  5558. if ~ defined extended_primary_loader
  5559. ; load kernel.mnt to 0x7000:0
  5560.         mov     ebx, kernel_file_load
  5561.         pushad
  5562.         call    file_system_lfn
  5563.         popad
  5564.  
  5565.         mov     esi, restart_kernel_4000+OS_BASE+0x10000 ; move kernel re-starter to 0x4000:0
  5566.         mov     edi, OS_BASE+0x40000
  5567.         mov     ecx, 1000
  5568.         rep movsb
  5569. end if
  5570.  
  5571. ;        mov     esi, BOOT_VAR    ; restore 0x0 - 0xffff
  5572. ;        mov     edi, OS_BASE
  5573. ;        mov     ecx, 0x10000/4
  5574. ;        cld
  5575. ;        rep movsd
  5576.  
  5577.         call    IRQ_mask_all
  5578.  
  5579. if 0
  5580.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  5581.         mov     word [OS_BASE+0x467+2], 0x1000
  5582.  
  5583.         mov     al, 0x0F
  5584.         out     0x70, al
  5585.         mov     al, 0x05
  5586.         out     0x71, al
  5587.  
  5588.         mov     al, 0xFE
  5589.         out     0x64, al
  5590.  
  5591.         hlt
  5592.         jmp     $-1
  5593.  
  5594. else
  5595.         cmp     byte [OS_BASE + 0x9030], 2
  5596.         jnz     no_acpi_power_off
  5597.  
  5598. ; scan for RSDP
  5599. ; 1) The first 1 Kb of the Extended BIOS Data Area (EBDA).
  5600.         movzx   eax, word [OS_BASE + 0x40E]
  5601.         shl     eax, 4
  5602.         jz      @f
  5603.         mov     ecx, 1024/16
  5604.         call    scan_rsdp
  5605.         jnc     .rsdp_found
  5606. @@:
  5607. ; 2) The BIOS read-only memory space between 0E0000h and 0FFFFFh.
  5608.         mov     eax, 0xE0000
  5609.         mov     ecx, 0x2000
  5610.         call    scan_rsdp
  5611.         jc      no_acpi_power_off
  5612. .rsdp_found:
  5613.         mov     esi, [eax+16]   ; esi contains physical address of the RSDT
  5614.         mov     ebp, [ipc_tmp]
  5615.         stdcall map_page, ebp, esi, PG_MAP
  5616.         lea     eax, [esi+1000h]
  5617.         lea     edx, [ebp+1000h]
  5618.         stdcall map_page, edx, eax, PG_MAP
  5619.         and     esi, 0xFFF
  5620.         add     esi, ebp
  5621.         cmp     dword [esi], 'RSDT'
  5622.         jnz     no_acpi_power_off
  5623.         mov     ecx, [esi+4]
  5624.         sub     ecx, 24h
  5625.         jbe     no_acpi_power_off
  5626.         shr     ecx, 2
  5627.         add     esi, 24h
  5628. .scan_fadt:
  5629.         lodsd
  5630.         mov     ebx, eax
  5631.         lea     eax, [ebp+2000h]
  5632.         stdcall map_page, eax, ebx, PG_MAP
  5633.         lea     eax, [ebp+3000h]
  5634.         add     ebx, 0x1000
  5635.         stdcall map_page, eax, ebx, PG_MAP
  5636.         and     ebx, 0xFFF
  5637.         lea     ebx, [ebx+ebp+2000h]
  5638.         cmp     dword [ebx], 'FACP'
  5639.         jz      .fadt_found
  5640.         loop    .scan_fadt
  5641.         jmp     no_acpi_power_off
  5642. .fadt_found:
  5643. ; ebx is linear address of FADT
  5644.         mov     edi, [ebx+40] ; physical address of the DSDT
  5645.         lea     eax, [ebp+4000h]
  5646.         stdcall map_page, eax, edi, PG_MAP
  5647.         lea     eax, [ebp+5000h]
  5648.         lea     esi, [edi+0x1000]
  5649.         stdcall map_page, eax, esi, PG_MAP
  5650.         and     esi, 0xFFF
  5651.         sub     edi, esi
  5652.         cmp     dword [esi+ebp+4000h], 'DSDT'
  5653.         jnz     no_acpi_power_off
  5654.         mov     eax, [esi+ebp+4004h] ; DSDT length
  5655.         sub     eax, 36+4
  5656.         jbe     no_acpi_power_off
  5657.         add     esi, 36
  5658. .scan_dsdt:
  5659.         cmp     dword [esi+ebp+4000h], '_S5_'
  5660.         jnz     .scan_dsdt_cont
  5661.         cmp     byte [esi+ebp+4000h+4], 12h ; DefPackage opcode
  5662.         jnz     .scan_dsdt_cont
  5663.         mov     dl, [esi+ebp+4000h+6]
  5664.         cmp     dl, 4 ; _S5_ package must contain 4 bytes
  5665.                       ; ...in theory; in practice, VirtualBox has 2 bytes
  5666.         ja      .scan_dsdt_cont
  5667.         cmp     dl, 1
  5668.         jb      .scan_dsdt_cont
  5669.         lea     esi, [esi+ebp+4000h+7]
  5670.         xor     ecx, ecx
  5671.         cmp     byte [esi], 0 ; 0 means zero byte, 0Ah xx means byte xx
  5672.         jz      @f
  5673.         cmp     byte [esi], 0xA
  5674.         jnz     no_acpi_power_off
  5675.         inc     esi
  5676.         mov     cl, [esi]
  5677. @@:
  5678.         inc     esi
  5679.         cmp     dl, 2
  5680.         jb      @f
  5681.         cmp     byte [esi], 0
  5682.         jz      @f
  5683.         cmp     byte [esi], 0xA
  5684.         jnz     no_acpi_power_off
  5685.         inc     esi
  5686.         mov     ch, [esi]
  5687. @@:
  5688.         jmp     do_acpi_power_off
  5689. .scan_dsdt_cont:
  5690.         inc     esi
  5691.         cmp     esi, 0x1000
  5692.         jb      @f
  5693.         sub     esi, 0x1000
  5694.         add     edi, 0x1000
  5695.         push    eax
  5696.         lea     eax, [ebp+4000h]
  5697.         stdcall map_page, eax, edi, PG_MAP
  5698.         push    PG_MAP
  5699.         lea     eax, [edi+1000h]
  5700.         push    eax
  5701.         lea     eax, [ebp+5000h]
  5702.         push    eax
  5703.         stdcall map_page
  5704.         pop     eax
  5705. @@:
  5706.         dec     eax
  5707.         jnz     .scan_dsdt
  5708.         jmp     no_acpi_power_off
  5709. do_acpi_power_off:
  5710.         mov     edx, [ebx+48]
  5711.         test    edx, edx
  5712.         jz      .nosmi
  5713.         mov     al, [ebx+52]
  5714.         out     dx, al
  5715.         mov     edx, [ebx+64]
  5716. @@:
  5717.         in      ax, dx
  5718.         test    al, 1
  5719.         jz      @b
  5720. .nosmi:
  5721.         and     cx, 0x0707
  5722.         shl     cx, 2
  5723.         or      cx, 0x2020
  5724.         mov     edx, [ebx+64]
  5725.         in      ax, dx
  5726.         and     ax, 203h
  5727.         or      ah, cl
  5728.         out     dx, ax
  5729.         mov     edx, [ebx+68]
  5730.         test    edx, edx
  5731.         jz      @f
  5732.         in      ax, dx
  5733.         and     ax, 203h
  5734.         or      ah, ch
  5735.         out     dx, ax
  5736. @@:
  5737.         jmp     $
  5738.  
  5739.  
  5740. no_acpi_power_off:
  5741.         mov     word [OS_BASE+0x467+0], pr_mode_exit
  5742.         mov     word [OS_BASE+0x467+2], 0x1000
  5743.  
  5744.         mov     al, 0x0F
  5745.         out     0x70, al
  5746.         mov     al, 0x05
  5747.         out     0x71, al
  5748.  
  5749.         mov     al, 0xFE
  5750.         out     0x64, al
  5751.  
  5752.         hlt
  5753.         jmp     $-1
  5754.  
  5755. scan_rsdp:
  5756.         add     eax, OS_BASE
  5757. .s:
  5758.         cmp     dword [eax], 'RSD '
  5759.         jnz     .n
  5760.         cmp     dword [eax+4], 'PTR '
  5761.         jnz     .n
  5762.         xor     edx, edx
  5763.         xor     esi, esi
  5764. @@:
  5765.         add     dl, [eax+esi]
  5766.         inc     esi
  5767.         cmp     esi, 20
  5768.         jnz     @b
  5769.         test    dl, dl
  5770.         jz      .ok
  5771. .n:
  5772.         add     eax, 10h
  5773.         loop    .s
  5774.         stc
  5775. .ok:
  5776.         ret
  5777. end if
  5778.  
  5779. if ~ lang eq sp
  5780. diff16 "end of .text segment",0,$
  5781. end if
  5782.  
  5783. include "data32.inc"
  5784.  
  5785. __REV__ = __REV
  5786.  
  5787. if ~ lang eq sp
  5788. diff16 "end of kernel code",0,$
  5789. end if
  5790.