Subversion Repositories Kolibri OS

Rev

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