Subversion Repositories Kolibri OS

Rev

Rev 3508 | Rev 3534 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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