Subversion Repositories Kolibri OS

Rev

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