Subversion Repositories Kolibri OS

Rev

Rev 4713 | Rev 4741 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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