Subversion Repositories Kolibri OS

Rev

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