Subversion Repositories Kolibri OS

Rev

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