Subversion Repositories Kolibri OS

Rev

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