Subversion Repositories Kolibri OS

Rev

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