Subversion Repositories Kolibri OS

Rev

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