Subversion Repositories Kolibri OS

Rev

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