Subversion Repositories Kolibri OS

Rev

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