Subversion Repositories Kolibri OS

Rev

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