Subversion Repositories Kolibri OS

Rev

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