Subversion Repositories Kolibri OS

Rev

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