Subversion Repositories Kolibri OS

Rev

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