Subversion Repositories Kolibri OS

Rev

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